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 <sstream>
30#include <fst/extensions/far/main.h>
31
32namespace fst {
33
34// Return the 'FarType' value corresponding to a far type name.
35FarType FarTypeFromString(const string &str) {
36  FarType type = FAR_DEFAULT;
37  if (str == "fst")
38    type = FAR_FST;
39  else if (str == "stlist")
40    type = FAR_STLIST;
41  else if (str == "sttable")
42    type = FAR_STTABLE;
43  else if (str == "default")
44    type = FAR_DEFAULT;
45  return type;
46}
47
48
49// Return the textual name  corresponding to a 'FarType;.
50string FarTypeToString(FarType type) {
51  switch (type) {
52    case FAR_FST:
53      return "fst";
54    case FAR_STLIST:
55      return "stlist";
56    case FAR_STTABLE:
57      return "sttable";
58    case FAR_DEFAULT:
59      return "default";
60    default:
61      return "<unknown>";
62  }
63}
64
65FarEntryType StringToFarEntryType(const string &s) {
66  if (s == "line") {
67    return FET_LINE;
68  } else if (s == "file") {
69    return FET_FILE;
70  } else {
71    FSTERROR() << "Unknown FAR entry type: " << s;
72    return FET_LINE;  // compiler requires return
73  }
74}
75
76FarTokenType StringToFarTokenType(const string &s) {
77  if (s == "symbol") {
78    return FTT_SYMBOL;
79  } else if (s == "byte") {
80    return FTT_BYTE;
81  } else if (s == "utf8") {
82    return FTT_UTF8;
83  } else {
84    FSTERROR() << "Unknown FAR entry type: " << s;
85    return FTT_SYMBOL;  // compiler requires return
86  }
87}
88
89
90string LoadArcTypeFromFar(const string &far_fname) {
91  FarHeader hdr;
92
93  if (!hdr.Read(far_fname)) {
94    FSTERROR() << "Error reading FAR: " << far_fname;
95    return "";
96  }
97
98  string atype = hdr.ArcType();
99  if (atype == "unknown") {
100    FSTERROR() << "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    FSTERROR() << "Error reading FST: " << fst_fname;
112    return "";
113  }
114
115  return hdr.ArcType();
116}
117
118}  // namespace fst
119