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