fst.cpp revision 560eaab489316778f491132c7b05a647b098d2a0
1// fst.cc
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//
16// \file
17// FST definitions.
18
19#include "fst/lib/fst.h"
20
21// Include these so they are registered
22#include "fst/lib/const-fst.h"
23#include "fst/lib/vector-fst.h"
24
25// FST flag definitions
26
27DEFINE_bool(fst_verify_properties, false,
28            "Verify fst properties queried by TestProperties");
29
30DEFINE_string(fst_product_separator, ",",
31              "Character separator between printed weights"
32              " in a product semiring");
33
34DEFINE_bool(fst_default_cache_gc, true, "Enable garbage collection of cache");
35
36DEFINE_int64(fst_default_cache_gc_limit, 1<<20LL,
37             "Cache byte size that triggers garbage collection");
38
39namespace fst {
40
41// Register VectorFst and ConstFst for common arcs types
42
43REGISTER_FST(VectorFst, StdArc);
44REGISTER_FST(VectorFst, LogArc);
45REGISTER_FST(ConstFst, StdArc);
46REGISTER_FST(ConstFst, LogArc);
47
48// Identifies stream data as an FST (and its endianity)
49static const int32 kFstMagicNumber = 2125659606;
50
51// Check Fst magic number and read in Fst header.
52bool FstHeader::Read(istream &strm, const string &source) {
53  int32 magic_number = 0;
54  ReadType(strm, &magic_number);
55  if (magic_number != kFstMagicNumber) {
56    LOG(ERROR) << "FstHeader::Read: Bad FST header: " << source;
57    return false;
58  }
59
60  ReadType(strm, &fsttype_);
61  ReadType(strm, &arctype_);
62  ReadType(strm, &version_);
63  ReadType(strm, &flags_);
64  ReadType(strm, &properties_);
65  ReadType(strm, &start_);
66  ReadType(strm, &numstates_);
67  ReadType(strm, &numarcs_);
68  if (!strm) {
69    LOG(ERROR) << "FstHeader::Read: read failed: " << source;
70    return false;
71  }
72  return true;
73}
74
75// Write Fst magic number and Fst header.
76bool FstHeader::Write(ostream &strm, const string &source) const {
77  WriteType(strm, kFstMagicNumber);
78  WriteType(strm, fsttype_);
79  WriteType(strm, arctype_);
80  WriteType(strm, version_);
81  WriteType(strm, flags_);
82  WriteType(strm, properties_);
83  WriteType(strm, start_);
84  WriteType(strm, numstates_);
85  WriteType(strm, numarcs_);
86  if (!strm) {
87    LOG(ERROR) << "FstHeader::Write: write failed: " << source;
88    return false;
89  }
90  return true;
91}
92
93}
94