tesseract  5.0.0-alpha-619-ge9db
params.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: params.cpp
3  * Description: Initialization and setting of Tesseract parameters.
4  * Author: Ray Smith
5  *
6  * (C) Copyright 1991, Hewlett-Packard Ltd.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  *
17  **********************************************************************/
18 
19 #include <climits> // for INT_MIN, INT_MAX
20 #include <cmath> // for NAN, std::isnan
21 #include <cstdio>
22 #include <cstring>
23 #include <cstdlib>
24 #include <locale> // for std::locale::classic
25 #include <sstream> // for std::stringstream
26 
28 #include "host.h" // tesseract/platform.h, windows.h for MAX_PATH
29 #include "tprintf.h"
30 #include "params.h"
31 
33  static tesseract::ParamsVectors global_params = tesseract::ParamsVectors();
34  return &global_params;
35 }
36 
37 namespace tesseract {
38 
40  SetParamConstraint constraint,
41  ParamsVectors *member_params) {
42  TFile fp;
43  if (!fp.Open(file, nullptr)) {
44  tprintf("read_params_file: Can't open %s\n", file);
45  return true;
46  }
47  return ReadParamsFromFp(constraint, &fp, member_params);
48 }
49 
51  ParamsVectors *member_params) {
52  char line[MAX_PATH]; // input line
53  bool anyerr = false; // true if any error
54  bool foundit; // found parameter
55  char *valptr; // value field
56 
57  while (fp->FGets(line, MAX_PATH) != nullptr) {
58  if (line[0] != '\r' && line[0] != '\n' && line[0] != '#') {
59  chomp_string(line); // remove newline
60  for (valptr = line; *valptr && *valptr != ' ' && *valptr != '\t';
61  valptr++);
62  if (*valptr) { // found blank
63  *valptr = '\0'; // make name a string
64  do
65  valptr++; // find end of blanks
66  while (*valptr == ' ' || *valptr == '\t');
67  }
68  foundit = SetParam(line, valptr, constraint, member_params);
69 
70  if (!foundit) {
71  anyerr = true; // had an error
72  tprintf("Warning: Parameter not found: %s\n", line);
73  }
74  }
75  }
76  return anyerr;
77 }
78 
79 bool ParamUtils::SetParam(const char *name, const char* value,
80  SetParamConstraint constraint,
81  ParamsVectors *member_params) {
82  // Look for the parameter among string parameters.
83  auto *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
84  member_params->string_params);
85  if (sp != nullptr && sp->constraint_ok(constraint)) sp->set_value(value);
86  if (*value == '\0') return (sp != nullptr);
87 
88  // Look for the parameter among int parameters.
89  auto *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
90  member_params->int_params);
91  if (ip && ip->constraint_ok(constraint)) {
92  int intval = INT_MIN;
93  std::stringstream stream(value);
94  stream.imbue(std::locale::classic());
95  stream >> intval;
96  if (intval != INT_MIN) {
97  ip->set_value(intval);
98  }
99  }
100 
101  // Look for the parameter among bool parameters.
102  auto *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
103  member_params->bool_params);
104  if (bp != nullptr && bp->constraint_ok(constraint)) {
105  if (*value == 'T' || *value == 't' ||
106  *value == 'Y' || *value == 'y' || *value == '1') {
107  bp->set_value(true);
108  } else if (*value == 'F' || *value == 'f' ||
109  *value == 'N' || *value == 'n' || *value == '0') {
110  bp->set_value(false);
111  }
112  }
113 
114  // Look for the parameter among double parameters.
115  auto *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
116  member_params->double_params);
117  if (dp != nullptr && dp->constraint_ok(constraint)) {
118  double doubleval = NAN;
119  std::stringstream stream(value);
120  stream.imbue(std::locale::classic());
121  stream >> doubleval;
122  if (!std::isnan(doubleval)) {
123  dp->set_value(doubleval);
124  }
125  }
126  return (sp || ip || bp || dp);
127 }
128 
129 bool ParamUtils::GetParamAsString(const char *name,
130  const ParamsVectors* member_params,
131  STRING *value) {
132  // Look for the parameter among string parameters.
133  auto *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
134  member_params->string_params);
135  if (sp) {
136  *value = sp->c_str();
137  return true;
138  }
139  // Look for the parameter among int parameters.
140  auto *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
141  member_params->int_params);
142  if (ip) {
143  char buf[128];
144  snprintf(buf, sizeof(buf), "%d", int32_t(*ip));
145  *value = buf;
146  return true;
147  }
148  // Look for the parameter among bool parameters.
149  auto *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
150  member_params->bool_params);
151  if (bp != nullptr) {
152  *value = bool(*bp) ? "1": "0";
153  return true;
154  }
155  // Look for the parameter among double parameters.
156  auto *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
157  member_params->double_params);
158  if (dp != nullptr) {
159  std::ostringstream stream;
160  stream.imbue(std::locale::classic());
161  stream << double(*dp);
162  *value = stream.str().c_str();
163  return true;
164  }
165  return false;
166 }
167 
168 void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params) {
169  int num_iterations = (member_params == nullptr) ? 1 : 2;
170  std::ostringstream stream;
171  stream.imbue(std::locale::classic());
172  for (int v = 0; v < num_iterations; ++v) {
173  const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
174  for (int i = 0; i < vec->int_params.size(); ++i) {
175  stream << vec->int_params[i]->name_str() << '\t' <<
176  (int32_t)(*vec->int_params[i]) << '\t' <<
177  vec->int_params[i]->info_str() << '\n';
178  }
179  for (int i = 0; i < vec->bool_params.size(); ++i) {
180  stream << vec->bool_params[i]->name_str() << '\t' <<
181  bool(*vec->bool_params[i]) << '\t' <<
182  vec->bool_params[i]->info_str() << '\n';
183  }
184  for (int i = 0; i < vec->string_params.size(); ++i) {
185  stream << vec->string_params[i]->name_str() << '\t' <<
186  vec->string_params[i]->c_str() << '\t' <<
187  vec->string_params[i]->info_str() << '\n';
188  }
189  for (int i = 0; i < vec->double_params.size(); ++i) {
190  stream << vec->double_params[i]->name_str() << '\t' <<
191  (double)(*vec->double_params[i]) << '\t' <<
192  vec->double_params[i]->info_str() << '\n';
193  }
194  }
195  fprintf(fp, "%s", stream.str().c_str());
196 }
197 
198 // Resets all parameters back to default values;
200  int v, i;
201  int num_iterations = (member_params == nullptr) ? 1 : 2;
202  for (v = 0; v < num_iterations; ++v) {
203  ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
204  for (i = 0; i < vec->int_params.size(); ++i) {
205  vec->int_params[i]->ResetToDefault();
206  }
207  for (i = 0; i < vec->bool_params.size(); ++i) {
208  vec->bool_params[i]->ResetToDefault();
209  }
210  for (int i = 0; i < vec->string_params.size(); ++i) {
211  vec->string_params[i]->ResetToDefault();
212  }
213  for (int i = 0; i < vec->double_params.size(); ++i) {
214  vec->double_params[i]->ResetToDefault();
215  }
216  }
217 }
218 
219 } // namespace tesseract
tesseract::ParamUtils::ReadParamsFile
static bool ReadParamsFile(const char *file, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:39
host.h
params.h
STRING
Definition: strngs.h:45
tesseract::SetParamConstraint
SetParamConstraint
Definition: params.h:49
tesseract::ParamsVectors::int_params
GenericVector< IntParam * > int_params
Definition: params.h:57
tesseract::ParamUtils::PrintParams
static void PrintParams(FILE *fp, const ParamsVectors *member_params)
Definition: params.cpp:168
tesseract::TFile::Open
bool Open(const STRING &filename, FileReader reader)
Definition: serialis.cpp:210
tesseract::ParamsVectors::double_params
GenericVector< DoubleParam * > double_params
Definition: params.h:60
genericvector.h
tesseract::ParamsVectors::string_params
GenericVector< StringParam * > string_params
Definition: params.h:59
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
file
Definition: include_gunit.h:22
chomp_string
void chomp_string(char *str)
Definition: helpers.h:75
tesseract::TFile
Definition: serialis.h:75
tesseract::ParamsVectors
Definition: params.h:56
tesseract::ParamUtils::GetParamAsString
static bool GetParamAsString(const char *name, const ParamsVectors *member_params, STRING *value)
Definition: params.cpp:129
GlobalParams
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:32
tesseract
Definition: baseapi.h:65
tprintf.h
tesseract::ParamUtils::ReadParamsFromFp
static bool ReadParamsFromFp(SetParamConstraint constraint, TFile *fp, ParamsVectors *member_params)
Definition: params.cpp:50
tesseract::TFile::FGets
char * FGets(char *buffer, int buffer_size)
Definition: serialis.cpp:262
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
tesseract::ParamUtils::SetParam
static bool SetParam(const char *name, const char *value, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:79
MAX_PATH
#define MAX_PATH
Definition: platform.h:29
tesseract::ParamUtils::ResetToDefaults
static void ResetToDefaults(ParamsVectors *member_params)
Definition: params.cpp:199
tesseract::ParamsVectors::bool_params
GenericVector< BoolParam * > bool_params
Definition: params.h:58