1 // util.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 // Copyright 2005-2010 Google, Inc.
16 // Author: riley@google.com (Michael Riley)
17 //
18 // \file
19 // FST utility definitions.
20 
21 #include <cctype>
22 #include <string>
23 #include <fst/util.h>
24 #include <fst/mapped-file.h>
25 
26 // Utility flag definitions
27 
28 DEFINE_bool(fst_error_fatal, true,
29             "FST errors are fatal; o.w. return objects flagged as bad: "
30             " e.g., FSTs - kError prop. true, FST weights - not  a Member()");
31 
32 namespace fst {
33 
StrToInt64(const string & s,const string & src,size_t nline,bool allow_negative,bool * error)34 int64 StrToInt64(const string &s, const string &src, size_t nline,
35                  bool allow_negative, bool *error) {
36   int64 n;
37   const char *cs = s.c_str();
38   char *p;
39   if (error) *error = false;
40   n = strtoll(cs, &p, 10);
41   if (p < cs + s.size() || (!allow_negative && n < 0)) {
42     FSTERROR() << "StrToInt64: Bad integer = " << s
43                << "\", source = " << src << ", line = " << nline;
44     if (error) *error = true;
45     return 0;
46   }
47   return n;
48 }
49 
Int64ToStr(int64 n,string * s)50 void Int64ToStr(int64 n, string *s) {
51   ostringstream nstr;
52   nstr << n;
53   s->append(nstr.str().data(), nstr.str().size());
54 }
55 
ConvertToLegalCSymbol(string * s)56 void ConvertToLegalCSymbol(string *s) {
57   for (string::iterator it = s->begin(); it != s->end(); ++it)
58     if (!isalnum(*it)) *it = '_';
59 }
60 
61 // Skips over input characters to align to 'align' bytes. Returns
62 // false if can't align.
AlignInput(istream & strm)63 bool AlignInput(istream &strm) {
64   char c;
65   for (int i = 0; i < MappedFile::kArchAlignment; ++i) {
66     int64 pos = strm.tellg();
67     if (pos < 0) {
68       LOG(ERROR) << "AlignInput: can't determine stream position";
69       return false;
70     }
71     if (pos % MappedFile::kArchAlignment == 0) break;
72     strm.read(&c, 1);
73   }
74   return true;
75 }
76 
77 // Write null output characters to align to 'align' bytes. Returns
78 // false if can't align.
AlignOutput(ostream & strm)79 bool AlignOutput(ostream &strm) {
80   for (int i = 0; i < MappedFile::kArchAlignment; ++i) {
81     int64 pos = strm.tellp();
82     if (pos < 0) {
83       LOG(ERROR) << "AlignOutput: can't determine stream position";
84       return false;
85     }
86     if (pos % MappedFile::kArchAlignment == 0) break;
87     strm.write("", 1);
88   }
89   return true;
90 }
91 
92 
93 }  // namespace fst
94