1// main.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@google.com (Michael Riley)
17// Modified: jpr@google.com (Jake Ratkiewicz) to not use new arc-dispatch
18//
19// \file
20// Definitions and functions for invoking and using Far main
21// functions that support multiple and extensible arc types.
22
23#include <string>
24#include <vector>
25using std::vector;
26
27#include <iostream>
28#include <fstream>
29#include <fst/extensions/far/main.h>
30
31namespace fst {
32
33// Return the 'FarType' value corresponding to a far type name.
34FarType FarTypeFromString(const string &str) {
35  FarType type = FAR_DEFAULT;
36  if (str == "stlist")
37    type = FAR_STLIST;
38  else if (str == "sttable")
39    type = FAR_STTABLE;
40  else if (str == "default")
41    type = FAR_DEFAULT;
42  return type;
43}
44
45
46// Return the textual name  corresponding to a 'FarType;.
47string FarTypeToString(FarType type) {
48  switch (type) {
49    case FAR_STLIST:
50      return "stlist";
51    case FAR_STTABLE:
52      return "sttable";
53    case FAR_DEFAULT:
54      return "default";
55    default:
56      return "<unknown>";
57  }
58}
59
60FarEntryType StringToFarEntryType(const string &s) {
61  if (s == "line") {
62    return FET_LINE;
63  } else if (s == "file") {
64    return FET_FILE;
65  } else {
66    FSTERROR() << "Unknown FAR entry type: " << s;
67    return FET_LINE;  // compiler requires return
68  }
69}
70
71FarTokenType StringToFarTokenType(const string &s) {
72  if (s == "symbol") {
73    return FTT_SYMBOL;
74  } else if (s == "byte") {
75    return FTT_BYTE;
76  } else if (s == "utf8") {
77    return FTT_UTF8;
78  } else {
79    FSTERROR() << "Unknown FAR entry type: " << s;
80    return FTT_SYMBOL;  // compiler requires return
81  }
82}
83
84
85string LoadArcTypeFromFar(const string &far_fname) {
86  FarHeader hdr;
87
88  if (far_fname.empty()) {
89    LOG(ERROR) << "Reading FAR from standard in not supported";
90    return "";
91  }
92
93  if (!hdr.Read(far_fname)) {
94    LOG(ERROR) << "Error reading FAR: " << far_fname;
95    return "";
96  }
97
98  string atype = hdr.ArcType();
99  if (atype == "unknown") {
100    LOG(ERROR) << "Empty FST archive: " << far_fname;
101    return "";
102  }
103
104  return atype;
105}
106
107string LoadArcTypeFromFst(const string &fst_fname) {
108  FstHeader hdr;
109  ifstream in(fst_fname.c_str(), ifstream::in | ifstream::binary);
110  if (!hdr.Read(in, fst_fname)) {
111    LOG(ERROR) << "Error reading FST: " << fst_fname;
112    return "";
113  }
114
115  return hdr.ArcType();
116}
117
118}  // namespace fst
119