io-funcs.h
Go to the documentation of this file.
1 // base/io-funcs.h
2 
3 // Copyright 2009-2011 Microsoft Corporation; Saarland University;
4 // Jan Silovsky; Yanmin Qian
5 // 2016 Xiaohui Zhang
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 
13 // http://www.apache.org/licenses/LICENSE-2.0
14 
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 #ifndef KALDI_BASE_IO_FUNCS_H_
23 #define KALDI_BASE_IO_FUNCS_H_
24 
25 // This header only contains some relatively low-level I/O functions.
26 // The full Kaldi I/O declarations are in ../util/kaldi-io.h
27 // and ../util/kaldi-table.h
28 // They were put in util/ in order to avoid making the Matrix library
29 // dependent on them.
30 
31 #include <cctype>
32 #include <vector>
33 #include <string>
34 
35 #include "base/kaldi-common.h"
36 #include "base/io-funcs-inl.h"
37 
38 namespace kaldi {
39 
40 
41 
42 /*
43  This comment describes the Kaldi approach to I/O. All objects can be written
44  and read in two modes: binary and text. In addition we want to make the I/O
45  work if we redefine the typedef "BaseFloat" between floats and doubles.
46  We also want to have control over whitespace in text mode without affecting
47  the meaning of the file, for pretty-printing purposes.
48 
49  Errors are handled by throwing a KaldiFatalError exception.
50 
51  For integer and floating-point types (and boolean values):
52 
53  WriteBasicType(std::ostream &, bool binary, const T&);
54  ReadBasicType(std::istream &, bool binary, T*);
55 
56  and we expect these functions to be defined in such a way that they work when
57  the type T changes between float and double, so you can read float into double
58  and vice versa]. Note that for efficiency and space-saving reasons, the Vector
59  and Matrix classes do not use these functions [but they preserve the type
60  interchangeability in their own way]
61 
62  For a class (or struct) C:
63  class C {
64  ..
65  Write(std::ostream &, bool binary, [possibly extra optional args for specific classes]) const;
66  Read(std::istream &, bool binary, [possibly extra optional args for specific classes]);
67  ..
68  }
69  NOTE: The only actual optional args we used are the "add" arguments in
70  Vector/Matrix classes, which specify whether we should sum the data already
71  in the class with the data being read.
72 
73  For types which are typedef's involving stl classes, I/O is as follows:
74  typedef std::vector<std::pair<A, B> > MyTypedefName;
75 
76  The user should define something like:
77 
78  WriteMyTypedefName(std::ostream &, bool binary, const MyTypedefName &t);
79  ReadMyTypedefName(std::ostream &, bool binary, MyTypedefName *t);
80 
81  The user would have to write these functions.
82 
83  For a type std::vector<T>:
84 
85  void WriteIntegerVector(std::ostream &os, bool binary, const std::vector<T> &v);
86  void ReadIntegerVector(std::istream &is, bool binary, std::vector<T> *v);
87 
88  For other types, e.g. vectors of pairs, the user should create a routine of the
89  type WriteMyTypedefName. This is to avoid introducing confusing templated functions;
90  we could easily create templated functions to handle most of these cases but they
91  would have to share the same name.
92 
93  It also often happens that the user needs to write/read special tokens as part
94  of a file. These might be class headers, or separators/identifiers in the class.
95  We provide special functions for manipulating these. These special tokens must
96  be nonempty and must not contain any whitespace.
97 
98  void WriteToken(std::ostream &os, bool binary, const char*);
99  void WriteToken(std::ostream &os, bool binary, const std::string & token);
100  int Peek(std::istream &is, bool binary);
101  void ReadToken(std::istream &is, bool binary, std::string *str);
102  void PeekToken(std::istream &is, bool binary, std::string *str);
103 
104  WriteToken writes the token and one space (whether in binary or text mode).
105 
106  Peek returns the first character of the next token, by consuming whitespace
107  (in text mode) and then returning the peek() character. It returns -1 at EOF;
108  it doesn't throw. It's useful if a class can have various forms based on
109  typedefs and virtual classes, and wants to know which version to read.
110 
111  ReadToken allows the caller to obtain the next token. PeekToken works just
112  like ReadToken, but seeks back to the beginning of the token. A subsequent
113  call to ReadToken will read the same token again. This is useful when
114  different object types are written to the same file; using PeekToken one can
115  decide which of the objects to read.
116 
117  There is currently no special functionality for writing/reading strings (where the strings
118  contain data rather than "special tokens" that are whitespace-free and nonempty). This is
119  because Kaldi is structured in such a way that strings don't appear, except as OpenFst symbol
120  table entries (and these have their own format).
121 
122 
123  NOTE: you should not call ReadIntegerType and WriteIntegerType with types,
124  such as int and size_t, that are machine-independent -- at least not
125  if you want your file formats to port between machines. Use int32 and
126  int64 where necessary. There is no way to detect this using compile-time
127  assertions because C++ only keeps track of the internal representation of
128  the type.
129 */
130 
133 
134 
137 template<class T> void WriteBasicType(std::ostream &os, bool binary, T t);
138 
141 template<class T> void ReadBasicType(std::istream &is, bool binary, T *t);
142 
143 
144 // Declare specialization for bool.
145 template<>
146 void WriteBasicType<bool>(std::ostream &os, bool binary, bool b);
147 
148 template <>
149 void ReadBasicType<bool>(std::istream &is, bool binary, bool *b);
150 
151 // Declare specializations for float and double.
152 template<>
153 void WriteBasicType<float>(std::ostream &os, bool binary, float f);
154 
155 template<>
156 void WriteBasicType<double>(std::ostream &os, bool binary, double f);
157 
158 template<>
159 void ReadBasicType<float>(std::istream &is, bool binary, float *f);
160 
161 template<>
162 void ReadBasicType<double>(std::istream &is, bool binary, double *f);
163 
164 // Define ReadBasicType that accepts an "add" parameter to add to
165 // the destination. Caution: if used in Read functions, be careful
166 // to initialize the parameters concerned to zero in the default
167 // constructor.
168 template<class T>
169 inline void ReadBasicType(std::istream &is, bool binary, T *t, bool add) {
170  if (!add) {
171  ReadBasicType(is, binary, t);
172  } else {
173  T tmp = T(0);
174  ReadBasicType(is, binary, &tmp);
175  *t += tmp;
176  }
177 }
178 
180 template<class T> inline void WriteIntegerVector(std::ostream &os, bool binary,
181  const std::vector<T> &v);
182 
184 template<class T> inline void ReadIntegerVector(std::istream &is, bool binary,
185  std::vector<T> *v);
186 
188 template<class T>
189 inline void WriteIntegerPairVector(std::ostream &os, bool binary,
190  const std::vector<std::pair<T, T> > &v);
191 
193 template<class T>
194 inline void ReadIntegerPairVector(std::istream &is, bool binary,
195  std::vector<std::pair<T, T> > *v);
196 
199 void WriteToken(std::ostream &os, bool binary, const char *token);
200 void WriteToken(std::ostream &os, bool binary, const std::string & token);
201 
204 int Peek(std::istream &is, bool binary);
205 
211 void ReadToken(std::istream &is, bool binary, std::string *token);
212 
218 int PeekToken(std::istream &is, bool binary);
219 
222 void ExpectToken(std::istream &is, bool binary, const char *token);
223 void ExpectToken(std::istream &is, bool binary, const std::string & token);
224 
228 void ExpectPretty(std::istream &is, bool binary, const char *token);
229 void ExpectPretty(std::istream &is, bool binary, const std::string & token);
230 
232 
233 
237 inline void InitKaldiOutputStream(std::ostream &os, bool binary);
238 
242 inline bool InitKaldiInputStream(std::istream &is, bool *binary);
243 
244 } // end namespace kaldi.
245 #endif // KALDI_BASE_IO_FUNCS_H_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void WriteBasicType< float >(std::ostream &os, bool binary, float f)
Definition: io-funcs.cc:51
bool InitKaldiInputStream(std::istream &is, bool *binary)
Initialize an opened stream for reading by detecting the binary header and.
Definition: io-funcs-inl.h:306
void WriteIntegerPairVector(std::ostream &os, bool binary, const std::vector< std::pair< T, T > > &v)
Function for writing STL vectors of pairs of integer types.
Definition: io-funcs-inl.h:93
void ExpectPretty(std::istream &is, bool binary, const char *token)
ExpectPretty attempts to read the text in "token", but only in non-binary mode.
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
void WriteBasicType< bool >(std::ostream &os, bool binary, bool b)
Definition: io-funcs.cc:26
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
void ReadIntegerPairVector(std::istream &is, bool binary, std::vector< std::pair< T, T > > *v)
Function for reading STL vector of pairs of integer types.
Definition: io-funcs-inl.h:131
int Peek(std::istream &is, bool binary)
Peek consumes whitespace (if binary == false) and then returns the peek() value of the stream...
Definition: io-funcs.cc:145
void WriteBasicType< double >(std::ostream &os, bool binary, double f)
Definition: io-funcs.cc:62
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
Definition: io-funcs-inl.h:232
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:191
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
int PeekToken(std::istream &is, bool binary)
PeekToken will return the first character of the next token, or -1 if end of file.
Definition: io-funcs.cc:170
void ReadBasicType< bool >(std::istream &is, bool binary, bool *b)
Definition: io-funcs.cc:34
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
Definition: io-funcs-inl.h:198
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34
void InitKaldiOutputStream(std::ostream &os, bool binary)
InitKaldiOutputStream initializes an opened stream for writing by writing an optional binary header a...
Definition: io-funcs-inl.h:291
void ReadBasicType< double >(std::istream &is, bool binary, double *d)
Definition: io-funcs.cc:98
void ReadBasicType< float >(std::istream &is, bool binary, float *f)
Definition: io-funcs.cc:73