23 #ifndef KALDI_BASE_IO_FUNCS_INL_H_ 24 #define KALDI_BASE_IO_FUNCS_INL_H_ 1 39 char len_c = (std::numeric_limits<T>::is_signed ? 1 : -1)
40 * static_cast<char>(
sizeof(t));
42 os.write(reinterpret_cast<const char *>(&t),
sizeof(t));
45 os << static_cast<int16>(t) <<
" ";
50 KALDI_ERR <<
"Write failure in WriteBasicType.";
61 int len_c_in = is.get();
63 KALDI_ERR <<
"ReadBasicType: encountered end of stream.";
64 char len_c =
static_cast<char>(len_c_in), len_c_expected
65 = (std::numeric_limits<T>::is_signed ? 1 : -1)
66 *
static_cast<char>(
sizeof(*t));
67 if (len_c != len_c_expected) {
68 KALDI_ERR <<
"ReadBasicType: did not get expected integer type, " 69 <<
static_cast<int>(len_c)
70 <<
" vs. " << static_cast<int>(len_c_expected)
71 <<
". You can change this code to successfully" 72 <<
" read it later, if needed.";
75 is.read(reinterpret_cast<char *>(t),
sizeof(*t));
77 if (
sizeof(*t) == 1) {
86 KALDI_ERR <<
"Read failure in ReadBasicType, file position is " 87 << is.tellg() <<
", next char is " << is.peek();
94 const std::vector<std::pair<T, T> > &v) {
102 os.write(reinterpret_cast<const char *>(&vecsz),
sizeof(vecsz));
104 os.write(reinterpret_cast<const char *>(&(v[0])),
sizeof(T) * vecsz * 2);
112 typename std::vector<std::pair<T, T> >::const_iterator iter = v.begin(),
114 for (; iter != end; ++iter) {
116 os << static_cast<int16>(iter->first) <<
',' 117 << static_cast<int16>(iter->second) <<
' ';
119 os << iter->first <<
',' 120 << iter->second <<
' ';
125 KALDI_ERR <<
"Write failure in WriteIntegerPairVector.";
132 std::vector<std::pair<T, T> > *v) {
137 if (sz ==
sizeof(T)) {
140 KALDI_ERR <<
"ReadIntegerPairVector: expected to see type of size " 141 <<
sizeof(T) <<
", saw instead " << sz <<
", at file position " 145 is.read(reinterpret_cast<char *>(&vecsz),
sizeof(vecsz));
146 if (is.fail() || vecsz < 0)
goto bad;
149 is.read(reinterpret_cast<char *>(&((*v)[0])),
sizeof(T)*vecsz*2);
152 std::vector<std::pair<T, T> > tmp_v;
155 if (is.peek() !=
static_cast<int>(
'[')) {
156 KALDI_ERR <<
"ReadIntegerPairVector: expected to see [, saw " 157 << is.peek() <<
", at file position " << is.tellg();
161 while (is.peek() !=
static_cast<int>(
']')) {
162 if (
sizeof(T) == 1) {
163 int16 next_t1, next_t2;
165 if (is.fail())
goto bad;
166 if (is.peek() !=
static_cast<int>(
','))
167 KALDI_ERR <<
"ReadIntegerPairVector: expected to see ',', saw " 168 << is.peek() <<
", at file position " << is.tellg();
170 is >> next_t2 >> std::ws;
171 if (is.fail())
goto bad;
173 tmp_v.push_back(std::make_pair<T, T>((T)next_t1, (T)next_t2));
177 if (is.fail())
goto bad;
178 if (is.peek() !=
static_cast<int>(
','))
179 KALDI_ERR <<
"ReadIntegerPairVector: expected to see ',', saw " 180 << is.peek() <<
", at file position " << is.tellg();
182 is >> next_t2 >> std::ws;
183 if (is.fail())
goto bad;
185 tmp_v.push_back(std::pair<T, T>(next_t1, next_t2));
192 if (!is.fail())
return;
194 KALDI_ERR <<
"ReadIntegerPairVector: read failure at file position " 199 const std::vector<T> &v) {
207 os.write(reinterpret_cast<const char *>(&vecsz),
sizeof(vecsz));
209 os.write(reinterpret_cast<const char *>(&(v[0])),
sizeof(T)*vecsz);
217 typename std::vector<T>::const_iterator iter = v.begin(), end = v.end();
218 for (; iter != end; ++iter) {
220 os << static_cast<int16>(*iter) <<
" ";
227 KALDI_ERR <<
"Write failure in WriteIntegerVector.";
239 if (sz ==
sizeof(T)) {
242 KALDI_ERR <<
"ReadIntegerVector: expected to see type of size " 243 <<
sizeof(T) <<
", saw instead " << sz <<
", at file position " 247 is.read(reinterpret_cast<char *>(&vecsz),
sizeof(vecsz));
248 if (is.fail() || vecsz < 0)
goto bad;
251 is.read(reinterpret_cast<char *>(&((*v)[0])),
sizeof(T)*vecsz);
254 std::vector<T> tmp_v;
257 if (is.peek() !=
static_cast<int>(
'[')) {
258 KALDI_ERR <<
"ReadIntegerVector: expected to see [, saw " 259 << is.peek() <<
", at file position " << is.tellg();
263 while (is.peek() !=
static_cast<int>(
']')) {
264 if (
sizeof(T) == 1) {
266 is >> next_t >> std::ws;
267 if (is.fail())
goto bad;
269 tmp_v.push_back((T)next_t);
272 is >> next_t >> std::ws;
273 if (is.fail())
goto bad;
275 tmp_v.push_back(next_t);
282 if (!is.fail())
return;
284 KALDI_ERR <<
"ReadIntegerVector: read failure at file position " 300 if (os.precision() < 7)
311 if (is.peek() ==
'\0') {
313 if (is.peek() !=
'B') {
327 #endif // KALDI_BASE_IO_FUNCS_INL_H_ This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
bool InitKaldiInputStream(std::istream &is, bool *binary)
Initialize an opened stream for reading by detecting the binary header and.
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.
#define KALDI_ASSERT_IS_INTEGER_TYPE(I)
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...
void ReadIntegerPairVector(std::istream &is, bool binary, std::vector< std::pair< T, T > > *v)
Function for reading STL vector of pairs of integer types.
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
#define KALDI_PARANOID_ASSERT(cond)
#define KALDI_ASSERT(cond)
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
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...
void InitKaldiOutputStream(std::ostream &os, bool binary)
InitKaldiOutputStream initializes an opened stream for writing by writing an optional binary header a...