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