FST  openfst-1.8.3
OpenFst Library
encode.cc
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
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 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Definitions for encode table header.
19 
20 #include <fst/encode.h>
21 
22 #include <cstddef>
23 #include <cstdint>
24 #include <istream>
25 #include <ostream>
26 #include <string>
27 
28 #include <fst/log.h>
29 #include <fst/util.h>
30 
31 namespace fst {
32 
33 bool EncodeTableHeader::Read(std::istream &strm, std::string_view source) {
34  int32_t magic_number;
35  ReadType(strm, &magic_number);
36  switch (magic_number) {
38  ReadType(strm, &arctype_);
39  ReadType(strm, &flags_);
40  ReadType(strm, &size_);
41  break;
42  }
44  LOG(ERROR) << "This old-style Encoder is written in a deprecated "
45  "format and will soon cease to be readable. Please read "
46  "and re-write it in order to be future-proof.";
47  // TODO(b/141172858): deprecated, remove by 2020-01-01.
48  uint32_t flags;
49  ReadType(strm, &flags);
50  flags_ = flags;
51  int64_t size;
52  ReadType(strm, &size);
53  size_ = size;
54  break;
55  }
56  default: {
57  LOG(ERROR) << "EncodeTableHeader::Read: Bad encode table header: "
58  << source;
59  return false;
60  }
61  }
62  if (!strm) {
63  LOG(ERROR) << "EncodeTableHeader::Read: Read failed: " << source;
64  return false;
65  }
66  return true;
67 }
68 
69 bool EncodeTableHeader::Write(std::ostream &strm,
70  std::string_view source) const {
72  WriteType(strm, arctype_);
73  WriteType(strm, flags_);
74  WriteType(strm, size_);
75  strm.flush();
76  if (!strm) {
77  LOG(ERROR) << "EncodeTableHeader::Write: Write failed: " << source;
78  return false;
79  }
80  return true;
81 }
82 
83 } // namespace fst
constexpr int32_t kEncodeDeprecatedMagicNumber
Definition: encode.h:69
#define LOG(type)
Definition: log.h:53
std::ostream & WriteType(std::ostream &strm, const T t)
Definition: util.h:228
constexpr int32_t kEncodeMagicNumber
Definition: encode.h:67
bool Write(std::ostream &strm, std::string_view source) const
Definition: encode.cc:69
bool Read(std::istream &strm, std::string_view source)
Definition: encode.cc:33
std::istream & ReadType(std::istream &strm, T *t)
Definition: util.h:80