FST  openfst-1.8.3
OpenFst Library
encodemapper-class.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 
19 
20 #include <cstdint>
21 #include <ios>
22 #include <iostream>
23 #include <istream>
24 #include <memory>
25 #include <string>
26 
27 #include <fst/log.h>
28 #include <fst/arc.h>
29 #include <fst/encode.h>
30 #include <fstream>
31 #include <fst/util.h>
32 #include <string_view>
33 
34 namespace fst {
35 namespace script {
36 namespace {
37 
38 // Helper methods.
39 
40 std::unique_ptr<EncodeMapperClass> ReadEncodeMapper(
41  std::istream &istrm, const std::string &source) {
42  if (!istrm) {
43  LOG(ERROR) << "ReadEncodeMapperClass: Can't open file: " << source;
44  return nullptr;
45  }
46  EncodeTableHeader hdr;
47  if (!hdr.Read(istrm, source)) return nullptr;
48  const auto &arc_type = hdr.ArcType();
49  // TODO(b/141172858): deprecated, remove by 2020-01-01.
50  if (arc_type.empty()) {
51  LOG(ERROR) << "Old-style EncodeMapper cannot be used with script interface";
52  return nullptr;
53  }
54  // The actual reader also consumes the header, so to be kind we rewind.
55  istrm.seekg(0, istrm.beg);
56  static const auto *reg =
58  const auto reader = reg->GetReader(arc_type);
59  if (!reader) {
60  LOG(ERROR) << "EncodeMapperClass::Read: Unknown arc type: " << arc_type;
61  return nullptr;
62  }
63  return reader(istrm, source);
64 }
65 
66 std::unique_ptr<EncodeMapperImplBase> CreateEncodeMapper(
67  std::string_view arc_type, uint8_t flags, EncodeType type) {
68  static const auto *reg =
70  auto creator = reg->GetCreator(arc_type);
71  if (!creator) {
72  FSTERROR() << "EncodeMapperClass: Unknown arc type: " << arc_type;
73  return nullptr;
74  }
75  return creator(flags, type);
76 }
77 
78 } // namespace
79 
80 EncodeMapperClass::EncodeMapperClass(std::string_view arc_type, uint8_t flags,
81  EncodeType type)
82  : impl_(CreateEncodeMapper(arc_type, flags, type)) {}
83 
84 std::unique_ptr<EncodeMapperClass> EncodeMapperClass::Read(
85  const std::string &source) {
86  if (!source.empty()) {
87  std::ifstream strm(source, std::ios_base::in | std::ios_base::binary);
88  return ReadEncodeMapper(strm, source);
89  } else {
90  return ReadEncodeMapper(std::cin, "standard input");
91  }
92 }
93 
94 std::unique_ptr<EncodeMapperClass> EncodeMapperClass::Read(
95  std::istream &strm, const std::string &source) {
96  return ReadEncodeMapper(strm, source);
97 }
98 
99 // Registration.
100 
104 
105 } // namespace script
106 } // namespace fst
REGISTER_ENCODEMAPPER_CLASS(StdArc)
static std::unique_ptr< EncodeMapperClass > Read(const std::string &source)
#define LOG(type)
Definition: log.h:53
EncodeType
Definition: encode.h:53
#define FSTERROR()
Definition: util.h:56