FST  openfst-1.8.3
OpenFst Library
fstconvert-main.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 // Converts an FST to another type.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 
24 #include <fst/flags.h>
25 #include <fst/script/convert.h>
26 #include <fst/script/fst-class.h>
27 
28 DECLARE_string(fst_type);
29 
30 int fstconvert_main(int argc, char **argv) {
31  namespace s = fst::script;
33 
34  std::string usage = "Converts an FST to another type.\n\n Usage: ";
35  usage += argv[0];
36  usage += " [in.fst [out.fst]]\n";
37 
38  SET_FLAGS(usage.c_str(), &argc, &argv, true);
39  if (argc > 3) {
40  ShowUsage();
41  return 1;
42  }
43 
44  const std::string in_name =
45  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
46  const std::string out_name =
47  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
48 
49  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
50  if (!ifst) return 1;
51 
52  if (ifst->FstType() != FST_FLAGS_fst_type) {
53  std::unique_ptr<FstClass> ofst(
54  s::Convert(*ifst, FST_FLAGS_fst_type));
55  if (!ofst) return 1;
56  return !ofst->Write(out_name);
57  } else {
58  return !ifst->Write(out_name);
59  }
60 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
DECLARE_string(fst_type)
int fstconvert_main(int argc, char **argv)
void Convert(FarReaderClass &reader, FarWriterClass &writer, std::string_view fst_type)
Definition: farscript.cc:71