FST  openfst-1.7.1
OpenFst Library
fstconvert-main.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 // Converts an FST to another type.
5 
6 #include <cstring>
7 
8 #include <memory>
9 #include <string>
10 
11 #include <fst/flags.h>
12 #include <fst/script/convert.h>
13 
14 DECLARE_string(fst_type);
15 
16 int fstconvert_main(int argc, char **argv) {
17  namespace s = fst::script;
19 
20  string usage = "Converts an FST to another type.\n\n Usage: ";
21  usage += argv[0];
22  usage += " [in.fst [out.fst]]\n";
23 
24  std::set_new_handler(FailedNewHandler);
25  SET_FLAGS(usage.c_str(), &argc, &argv, true);
26  if (argc > 3) {
27  ShowUsage();
28  return 1;
29  }
30 
31  const string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
32  const string out_name = argc > 2 ? argv[2] : "";
33 
34  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
35  if (!ifst) return 1;
36 
37  if (ifst->FstType() != FLAGS_fst_type) {
38  std::unique_ptr<FstClass> ofst(s::Convert(*ifst, FLAGS_fst_type));
39  if (!ofst) return 1;
40  return !ofst->Write(out_name);
41  } else {
42  return !ifst->Write(out_name);
43  }
44 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
Fst< Arc > * Convert(const Fst< Arc > &fst, const string &fst_type)
Definition: register.h:102
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
DECLARE_string(fst_type)
int fstconvert_main(int argc, char **argv)