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