FST  openfst-1.8.3
OpenFst Library
fstcompress-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 // Compresses/decompresses an FST.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 
24 #include <fst/flags.h>
25 #include <fst/log.h>
27 #include <fst/util.h>
28 #include <fst/script/arg-packs.h>
29 #include <fst/script/fst-class.h>
30 
31 DECLARE_string(arc_type);
32 DECLARE_bool(decode);
33 
34 int fstcompress_main(int argc, char **argv) {
35  namespace s = fst::script;
38 
39  std::string usage = "Compresses/decompresses an FST.\n\n Usage: ";
40  usage += argv[0];
41  usage += " [in.fst [out.fstz]]\n";
42  usage += " --decode [in.fstz [out.fst]]\n";
43 
44  SET_FLAGS(usage.c_str(), &argc, &argv, true);
45  if (argc > 3) {
46  ShowUsage();
47  return 1;
48  }
49 
50  const std::string in_name =
51  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
52  const std::string out_name =
53  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
54 
55  if (FST_FLAGS_decode) {
56  VectorFstClass fst(FST_FLAGS_arc_type);
57  if (!s::Decompress(in_name, &fst)) {
58  FSTERROR() << "Decompression failed";
59  return 1;
60  }
61  return !fst.Write(out_name);
62  } else {
63  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
64  if (!ifst) return 1;
65  if (!s::Compress(*ifst, out_name)) {
66  FSTERROR() << "Compression failed";
67  return 1;
68  }
69  return 0;
70  }
71 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
DECLARE_string(arc_type)
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
#define FSTERROR()
Definition: util.h:56
bool Compress(const FstClass &fst, const std::string &source)
DECLARE_bool(decode)
bool Decompress(const std::string &source, MutableFstClass *fst)
int fstcompress_main(int argc, char **argv)