FST  openfst-1.7.9
OpenFst Library
encode.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Definitions for encode table header.
5 
6 #include <fst/encode.h>
7 
8 namespace fst {
9 
10 bool EncodeTableHeader::Read(std::istream &strm, const std::string &source) {
11  int32 magic_number;
12  ReadType(strm, &magic_number);
13  if (magic_number == internal::kEncodeMagicNumber) {
14  ReadType(strm, &arctype_);
15  ReadType(strm, &flags_);
16  ReadType(strm, &size_);
17  } else if (magic_number == internal::kEncodeDeprecatedMagicNumber) {
18  // TODO(b/141172858): deprecated, remove by 2020-01-01.
19  uint32 flags;
20  ReadType(strm, &flags);
21  flags_ = flags;
22  int64 size;
23  ReadType(strm, &size);
24  size_ = size;
25  } else {
26  LOG(ERROR) << "EncodeTableHeader::Read: Bad encode table header: "
27  << source;
28  return false;
29  }
30  if (!strm) {
31  LOG(ERROR) << "EncodeTableHeader::Read: Read failed: " << source;
32  return false;
33  }
34  return true;
35 }
36 
37 bool EncodeTableHeader::Write(std::ostream &strm,
38  const std::string &source) const {
39  WriteType(strm, internal::kEncodeMagicNumber);
40  WriteType(strm, arctype_);
41  WriteType(strm, flags_);
42  WriteType(strm, size_);
43  strm.flush();
44  if (!strm) {
45  LOG(ERROR) << "EncodeTableHeader::Write: Write failed: " << source;
46  return false;
47  }
48  return true;
49 }
50 
51 } // namespace fst
#define LOG(type)
Definition: log.h:46
int64_t int64
Definition: types.h:27
std::ostream & WriteType(std::ostream &strm, const T t)
Definition: util.h:162
uint32_t uint32
Definition: types.h:31
bool Write(std::ostream &strm, const std::string &source) const
Definition: encode.cc:37
int32_t int32
Definition: types.h:26
std::istream & ReadType(std::istream &strm, T *t)
Definition: util.h:48
bool Read(std::istream &strm, const std::string &source)
Definition: encode.cc:10