compat.cpp revision 8fc5a7f51e62cb4ae44a27bdf4176d04adc80ede
1// compat.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//
16// \file
17// Google compatibility definitions.
18
19#include <cstring>
20
21#include "fst/lib/compat.h"
22
23DEFINE_int32(v, 0, "verbose level");
24DEFINE_bool(help, false, "verbose level");
25DEFINE_string(tmpdir, "/tmp/", "temporary directory");
26
27static string prog_name;
28static string flag_usage;
29
30static void out_of_memory() {
31  fprintf(stderr, "%s: Memory allocation failed\n", prog_name.c_str());
32  exit(1);
33}
34
35void InitFst(const char *usage, int *argc, char ***argv, bool remove_flags) {
36  prog_name = (*argv)[0];
37  set_new_handler(out_of_memory);
38
39  flag_usage = usage;
40  int index = 1;
41  for (; index < *argc; ++index) {
42    string argval = (*argv)[index];
43
44    if (argval[0] != '-' || argval == "-")
45      break;
46    while (argval[0] == '-')
47      argval = argval.substr(1);  // remove initial '-'s
48
49    string arg = argval;
50    string val = "";
51
52    // split argval (arg=val) into arg and val
53    size_t pos = argval.find("=");
54    if (pos != string::npos) {
55      arg = argval.substr(0, pos);
56      val = argval.substr(pos + 1);
57    }
58
59    FlagRegister<bool> *bool_register =
60      FlagRegister<bool>::GetRegister();
61    if (bool_register->InitFlag(arg, val))
62      continue;
63    FlagRegister<string> *string_register =
64      FlagRegister<string>::GetRegister();
65    if (string_register->InitFlag(arg, val))
66      continue;
67    FlagRegister<int32> *int32_register =
68      FlagRegister<int32>::GetRegister();
69    if (int32_register->InitFlag(arg, val))
70      continue;
71    FlagRegister<int64> *int64_register =
72      FlagRegister<int64>::GetRegister();
73    if (int64_register->InitFlag(arg, val))
74      continue;
75    FlagRegister<double> *double_register =
76      FlagRegister<double>::GetRegister();
77    if (double_register->InitFlag(arg, val))
78      continue;
79
80    LOG(FATAL) << "FlagInit: Bad option: " << (*argv)[index];
81  }
82
83  if (remove_flags) {
84    for (int i = 0; i < *argc - index; ++i)
85      (*argv)[i + 1] = (*argv)[i + index];
86    *argc -= index - 1;
87  }
88
89  if (FLAGS_help) {
90    ShowUsage();
91    exit(1);
92  }
93}
94
95void ShowUsage() {
96  std::cout << flag_usage << "\n";
97  std::cout << "  Flags Description:\n";
98  FlagRegister<bool> *bool_register = FlagRegister<bool>::GetRegister();
99  bool_register->ShowUsage();
100  FlagRegister<string> *string_register = FlagRegister<string>::GetRegister();
101  string_register->ShowUsage();
102  FlagRegister<int32> *int32_register = FlagRegister<int32>::GetRegister();
103  int32_register->ShowUsage();
104  FlagRegister<int64> *int64_register = FlagRegister<int64>::GetRegister();
105  int64_register->ShowUsage();
106  FlagRegister<double> *double_register = FlagRegister<double>::GetRegister();
107  double_register->ShowUsage();
108}
109
110void SplitToVector(char* full, const char* delim, vector<char*>* vec,
111                   bool omit_empty_strings) {
112  char* next  = full;
113  while((next = strsep(&full, delim)) != NULL) {
114   if (omit_empty_strings && next[0] == '\0') continue;
115   vec->push_back(next);
116  }
117  // Add last element (or full string if no delimeter found):
118  if (full != NULL) {
119    vec->push_back(full);
120  }
121}
122
123
124MD5::MD5() {}  // ?OP?
125
126void MD5::Reset() {}  // ?OP?
127
128void MD5::Update(void const *data, int size) {}  // ?OP?
129
130string MD5::Digest() { return ""; }  // every matches! ?OP?
131
132
133Mutex::Mutex() {}  // ?OP?
134
135MutexLock::MutexLock(Mutex *mutex) {}  // ?OP?
136