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