FST  openfst-1.8.2.post1
OpenFst Library
compile-strings.cc
Go to the documentation of this file.
1 // Copyright 2005-2020 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 #include <cmath>
19 #include <string>
20 
21 #include <fst/flags.h>
23 #include <fstream>
24 
25 DEFINE_string(far_field_separator, "\t",
26  "Set of characters used as a separator between printed fields");
27 
28 namespace fst {
29 namespace internal {
30 
31 // Computes the minimal length required to encode each line number as a decimal
32 // number, or zero if the number of lines could not be determined because the
33 // file was not seekable.
34 int KeySize(const std::string &source) {
35  std::ifstream istrm(source);
36  istrm.seekg(0);
37  // TODO(jrosenstock): Change this to is_regular_file when <filesystem> is
38  // no longer banned.
39  // Stream not seekable. This is really a hack to approximate is_regular_file.
40  // What we really want is that opening and reading the file twice gives the
41  // same result, which is only true for regular files. There may be devices
42  // that don't return an error on seek. At least we are able to catch the
43  // common cases of /dev/stdin and fifos.
44  if (istrm.rdstate() & std::ios_base::failbit) {
45  return 0;
46  }
47  std::string s;
48  int nline = 0;
49  while (std::getline(istrm, s)) ++nline;
50  return nline ? ceil(log10(nline + 1)) : 1;
51 }
52 
53 } // namespace internal
54 } // namespace fst
DEFINE_string(far_field_separator,"\t","Set of characters used as a separator between printed fields")
int KeySize(const std::string &source)