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