FST  openfst-1.7.3
OpenFst Library
strings.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 
4 #include <cmath>
5 #include <string>
6 
7 #include <fst/flags.h>
9 #include <fstream>
10 
11 DEFINE_string(far_field_separator, "\t",
12  "Set of characters used as a separator between printed fields");
13 
14 namespace fst {
15 
16 // Computes the minimal length required to encode each line number as a decimal
17 // number, or zero if the number of lines could not be determined because the
18 // file was not seekable.
19 int KeySize(const char *filename) {
20  std::ifstream istrm(filename);
21  istrm.seekg(0);
22  // TODO(jrosenstock): Change this to is_regular_file when we can use C++17.
23  // Stream not seekable. This is really a hack to approximate is_regular_file.
24  // What we really want is that opening and reading the file twice gives the
25  // same result, which is only true for regular files. There may be devices
26  // that don't return an error on seek. At least we are able to catch the
27  // common cases of /dev/stdin and fifos.
28  if (istrm.rdstate() & std::ios_base::failbit) {
29  return 0;
30  }
31  std::string s;
32  int nline = 0;
33  while (getline(istrm, s)) ++nline;
34  return nline ? ceil(log10(nline + 1)) : 1;
35 }
36 
37 } // namespace fst
int KeySize(const char *filename)
Definition: strings.cc:19
DEFINE_string(far_field_separator,"\t","Set of characters used as a separator between printed fields")