1// util.h 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14// 15// 16// \file 17// FST utility inline definitions. 18 19#ifndef FST_LIB_UTIL_H__ 20#define FST_LIB_UTIL_H__ 21 22#include <iostream> 23 24namespace fst { 25 26// 27// UTILITIES FOR TYPE I/O 28// 29 30// Read some types from an input stream. 31 32// Generic case. 33template <typename T> 34inline istream &ReadType(istream &strm, T *t) { 35 return strm.read(reinterpret_cast<char *>(t), sizeof(T)); 36} 37 38// String case. 39inline istream &ReadType(istream &strm, string *s) { 40 s->clear(); 41 int32 ns = 0; 42 strm.read(reinterpret_cast<char *>(&ns), sizeof(ns)); 43 for (int i = 0; i < ns; ++i) { 44 char c; 45 strm.read(&c, 1); 46 *s += c; 47 } 48 return strm; 49} 50 51// Write some types to an output stream. 52 53// Generic case. 54template <typename T> 55inline ostream &WriteType(ostream &strm, const T t) { 56 return strm.write(reinterpret_cast<const char *>(&t), sizeof(T)); 57} 58 59// String case. 60inline ostream &WriteType(ostream &strm, const string s) { 61 int32 ns = s.size(); 62 strm.write(reinterpret_cast<const char *>(&ns), sizeof(ns)); 63 return strm.write(s.data(), ns); 64} 65 66 67} // namespace fst; 68 69#endif // FST_LIB_UTIL_H__ 70