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