tesseract  4.1.0
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 
27 #include "genericvector.h"
28 #include "host.h" // platform.h, windows.h for MAX_PATH
29 #include "tprintf.h"
30 #include "params.h"
31 
32 #define PLUS '+' //flag states
33 #define MINUS '-'
34 
36  static tesseract::ParamsVectors global_params = tesseract::ParamsVectors();
37  return &global_params;
38 }
39 
40 namespace tesseract {
41 
42 bool ParamUtils::ReadParamsFile(const char *file,
43  SetParamConstraint constraint,
44  ParamsVectors *member_params) {
45  int16_t nameoffset; // offset for real name
46 
47  if (*file == PLUS) {
48  nameoffset = 1;
49  } else if (*file == MINUS) {
50  nameoffset = 1;
51  } else {
52  nameoffset = 0;
53  }
54 
55  TFile fp;
56  if (!fp.Open(file + nameoffset, nullptr)) {
57  tprintf("read_params_file: Can't open %s\n", file + nameoffset);
58  return true;
59  }
60  return ReadParamsFromFp(constraint, &fp, member_params);
61 }
62 
64  ParamsVectors *member_params) {
65  char line[MAX_PATH]; // input line
66  bool anyerr = false; // true if any error
67  bool foundit; // found parameter
68  char *valptr; // value field
69 
70  while (fp->FGets(line, MAX_PATH) != nullptr) {
71  if (line[0] != '\r' && line[0] != '\n' && line[0] != '#') {
72  chomp_string(line); // remove newline
73  for (valptr = line; *valptr && *valptr != ' ' && *valptr != '\t';
74  valptr++);
75  if (*valptr) { // found blank
76  *valptr = '\0'; // make name a string
77  do
78  valptr++; // find end of blanks
79  while (*valptr == ' ' || *valptr == '\t');
80  }
81  foundit = SetParam(line, valptr, constraint, member_params);
82 
83  if (!foundit) {
84  anyerr = true; // had an error
85  tprintf("Warning: Parameter not found: %s\n", line);
86  }
87  }
88  }
89  return anyerr;
90 }
91 
92 bool ParamUtils::SetParam(const char *name, const char* value,
93  SetParamConstraint constraint,
94  ParamsVectors *member_params) {
95  // Look for the parameter among string parameters.
96  auto *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
97  member_params->string_params);
98  if (sp != nullptr && sp->constraint_ok(constraint)) sp->set_value(value);
99  if (*value == '\0') return (sp != nullptr);
100 
101  // Look for the parameter among int parameters.
102  auto *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
103  member_params->int_params);
104  if (ip && ip->constraint_ok(constraint)) {
105  int intval = INT_MIN;
106  std::stringstream stream(value);
107  stream.imbue(std::locale::classic());
108  stream >> intval;
109  if (intval != INT_MIN) {
110  ip->set_value(intval);
111  }
112  }
113 
114  // Look for the parameter among bool parameters.
115  auto *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
116  member_params->bool_params);
117  if (bp != nullptr && bp->constraint_ok(constraint)) {
118  if (*value == 'T' || *value == 't' ||
119  *value == 'Y' || *value == 'y' || *value == '1') {
120  bp->set_value(true);
121  } else if (*value == 'F' || *value == 'f' ||
122  *value == 'N' || *value == 'n' || *value == '0') {
123  bp->set_value(false);
124  }
125  }
126 
127  // Look for the parameter among double parameters.
128  auto *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
129  member_params->double_params);
130  if (dp != nullptr && dp->constraint_ok(constraint)) {
131  double doubleval = NAN;
132  std::stringstream stream(value);
133  stream.imbue(std::locale::classic());
134  stream >> doubleval;
135  if (!std::isnan(doubleval)) {
136  dp->set_value(doubleval);
137  }
138  }
139  return (sp || ip || bp || dp);
140 }
141 
142 bool ParamUtils::GetParamAsString(const char *name,
143  const ParamsVectors* member_params,
144  STRING *value) {
145  // Look for the parameter among string parameters.
146  auto *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
147  member_params->string_params);
148  if (sp) {
149  *value = sp->string();
150  return true;
151  }
152  // Look for the parameter among int parameters.
153  auto *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
154  member_params->int_params);
155  if (ip) {
156  char buf[128];
157  snprintf(buf, sizeof(buf), "%d", int32_t(*ip));
158  *value = buf;
159  return true;
160  }
161  // Look for the parameter among bool parameters.
162  auto *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
163  member_params->bool_params);
164  if (bp != nullptr) {
165  *value = bool(*bp) ? "1": "0";
166  return true;
167  }
168  // Look for the parameter among double parameters.
169  auto *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
170  member_params->double_params);
171  if (dp != nullptr) {
172  char buf[128];
173  snprintf(buf, sizeof(buf), "%g", double(*dp));
174  *value = buf;
175  return true;
176  }
177  return false;
178 }
179 
180 void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params) {
181  int v, i;
182  int num_iterations = (member_params == nullptr) ? 1 : 2;
183  for (v = 0; v < num_iterations; ++v) {
184  const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
185  for (i = 0; i < vec->int_params.size(); ++i) {
186  fprintf(fp, "%s\t%d\t%s\n", vec->int_params[i]->name_str(),
187  (int32_t)(*vec->int_params[i]), vec->int_params[i]->info_str());
188  }
189  for (i = 0; i < vec->bool_params.size(); ++i) {
190  fprintf(fp, "%s\t%d\t%s\n", vec->bool_params[i]->name_str(),
191  bool(*vec->bool_params[i]), vec->bool_params[i]->info_str());
192  }
193  for (int i = 0; i < vec->string_params.size(); ++i) {
194  fprintf(fp, "%s\t%s\t%s\n", vec->string_params[i]->name_str(),
195  vec->string_params[i]->string(), vec->string_params[i]->info_str());
196  }
197  for (int i = 0; i < vec->double_params.size(); ++i) {
198  fprintf(fp, "%s\t%g\t%s\n", vec->double_params[i]->name_str(),
199  (double)(*vec->double_params[i]), vec->double_params[i]->info_str());
200  }
201  }
202 }
203 
204 // Resets all parameters back to default values;
206  int v, i;
207  int num_iterations = (member_params == nullptr) ? 1 : 2;
208  for (v = 0; v < num_iterations; ++v) {
209  ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
210  for (i = 0; i < vec->int_params.size(); ++i) {
211  vec->int_params[i]->ResetToDefault();
212  }
213  for (i = 0; i < vec->bool_params.size(); ++i) {
214  vec->bool_params[i]->ResetToDefault();
215  }
216  for (int i = 0; i < vec->string_params.size(); ++i) {
217  vec->string_params[i]->ResetToDefault();
218  }
219  for (int i = 0; i < vec->double_params.size(); ++i) {
220  vec->double_params[i]->ResetToDefault();
221  }
222  }
223 }
224 
225 } // namespace tesseract
SetParamConstraint
Definition: params.h:35
static bool GetParamAsString(const char *name, const ParamsVectors *member_params, STRING *value)
Definition: params.cpp:142
Definition: strngs.h:45
char * FGets(char *buffer, int buffer_size)
Definition: serialis.cpp:248
GenericVector< IntParam * > int_params
Definition: params.h:43
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:35
static void PrintParams(FILE *fp, const ParamsVectors *member_params)
Definition: params.cpp:180
static bool ReadParamsFromFp(SetParamConstraint constraint, TFile *fp, ParamsVectors *member_params)
Definition: params.cpp:63
#define MAX_PATH
Definition: platform.h:29
static void ResetToDefaults(ParamsVectors *member_params)
Definition: params.cpp:205
GenericVector< BoolParam * > bool_params
Definition: params.h:44
bool Open(const STRING &filename, FileReader reader)
Definition: serialis.cpp:196
void chomp_string(char *str)
Definition: helpers.h:77
const char * string() const
Definition: strngs.cpp:194
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
static bool SetParam(const char *name, const char *value, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:92
GenericVector< StringParam * > string_params
Definition: params.h:45
GenericVector< DoubleParam * > double_params
Definition: params.h:46
#define PLUS
Definition: params.cpp:32
static bool ReadParamsFile(const char *file, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:42
#define MINUS
Definition: params.cpp:33