All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 =
36  return global_params;
37 }
38 
39 namespace tesseract {
40 
41 bool ParamUtils::ReadParamsFile(const char *file,
42  SetParamConstraint constraint,
43  ParamsVectors *member_params) {
44  inT16 nameoffset; // offset for real name
45  FILE *fp; // file pointer
46  // iterators
47 
48  if (*file == PLUS) {
49  nameoffset = 1;
50  } else if (*file == MINUS) {
51  nameoffset = 1;
52  } else {
53  nameoffset = 0;
54  }
55 
56  fp = fopen(file + nameoffset, "rb");
57  if (fp == NULL) {
58  tprintf("read_params_file: Can't open %s\n", file + nameoffset);
59  return true;
60  }
61  const bool anyerr = ReadParamsFromFp(fp, -1, constraint, member_params);
62  fclose(fp);
63  return anyerr;
64 }
65 
66 bool ParamUtils::ReadParamsFromFp(FILE *fp, inT64 end_offset,
67  SetParamConstraint constraint,
68  ParamsVectors *member_params) {
69  char line[MAX_PATH]; // input line
70  bool anyerr = false; // true if any error
71  bool foundit; // found parameter
72  char *valptr; // value field
73 
74  while ((end_offset < 0 || ftell(fp) < end_offset) &&
75  fgets(line, MAX_PATH, fp)) {
76  if (line[0] != '\n' && line[0] != '#') {
77  chomp_string(line); // remove newline
78  for (valptr = line; *valptr && *valptr != ' ' && *valptr != '\t';
79  valptr++);
80  if (*valptr) { // found blank
81  *valptr = '\0'; // make name a string
82  do
83  valptr++; // find end of blanks
84  while (*valptr == ' ' || *valptr == '\t');
85  }
86  foundit = SetParam(line, valptr, constraint, member_params);
87 
88  if (!foundit) {
89  anyerr = true; // had an error
90  tprintf("read_params_file: parameter not found: %s\n", line);
91  exit(1);
92  }
93  }
94  }
95  return anyerr;
96 }
97 
98 bool ParamUtils::SetParam(const char *name, const char* value,
99  SetParamConstraint constraint,
100  ParamsVectors *member_params) {
101  // Look for the parameter among string parameters.
102  StringParam *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
103  member_params->string_params);
104  if (sp != NULL && sp->constraint_ok(constraint)) sp->set_value(value);
105  if (*value == '\0') return (sp != NULL);
106 
107  // Look for the parameter among int parameters.
108  int intval;
109  IntParam *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
110  member_params->int_params);
111  if (ip && ip->constraint_ok(constraint) &&
112  sscanf(value, INT32FORMAT, &intval) == 1) ip->set_value(intval);
113 
114  // Look for the parameter among bool parameters.
115  BoolParam *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
116  member_params->bool_params);
117  if (bp != NULL && 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  double doubleval;
129  DoubleParam *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
130  member_params->double_params);
131  if (dp != NULL && dp->constraint_ok(constraint)) {
132 #ifdef EMBEDDED
133  doubleval = strtofloat(value);
134 #else
135  if (sscanf(value, "%lf", &doubleval) == 1)
136 #endif
137  dp->set_value(doubleval);
138  }
139  return (sp || ip || bp || dp);
140 }
141 
143  const ParamsVectors* member_params,
144  STRING *value) {
145  // Look for the parameter among string parameters.
146  StringParam *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  IntParam *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(*ip));
158  *value = buf;
159  return true;
160  }
161  // Look for the parameter among bool parameters.
162  BoolParam *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
163  member_params->bool_params);
164  if (bp != NULL) {
165  *value = BOOL8(*bp) ? "1": "0";
166  return true;
167  }
168  // Look for the parameter among double parameters.
169  DoubleParam *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
170  member_params->double_params);
171  if (dp != NULL) {
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 == NULL) ? 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)(*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  (BOOL8)(*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 == NULL) ? 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
static void ResetToDefaults(ParamsVectors *member_params)
Definition: params.cpp:205
#define MINUS
Definition: params.cpp:30
static bool ReadParamsFromFp(FILE *fp, inT64 end_offset, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:66
bool constraint_ok(SetParamConstraint constraint) const
Definition: params.h:121
SetParamConstraint
Definition: params.h:36
#define tprintf(...)
Definition: tprintf.h:31
double strtofloat(const char *s)
Definition: scanutils.cpp:194
static void PrintParams(FILE *fp, const ParamsVectors *member_params)
Definition: params.cpp:180
#define INT32FORMAT
Definition: host.h:115
unsigned char BOOL8
Definition: host.h:113
static bool GetParamAsString(const char *name, const ParamsVectors *member_params, STRING *value)
Definition: params.cpp:142
void set_value(BOOL8 value)
Definition: params.h:179
void set_value(double value)
Definition: params.h:232
#define MAX_PATH
Definition: platform.h:41
#define PLUS
Definition: params.cpp:29
void chomp_string(char *str)
Definition: helpers.h:75
name_table name
GenericVector< IntParam * > int_params
Definition: params.h:44
static bool SetParam(const char *name, const char *value, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:98
GenericVector< BoolParam * > bool_params
Definition: params.h:45
GenericVector< DoubleParam * > double_params
Definition: params.h:47
void set_value(inT32 value)
Definition: params.h:155
static bool ReadParamsFile(const char *file, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:41
Definition: strngs.h:44
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:33
#define NULL
Definition: host.h:144
GenericVector< StringParam * > string_params
Definition: params.h:46
const char * string() const
Definition: params.h:203
void set_value(const STRING &value)
Definition: params.h:208
short inT16
Definition: host.h:100
int inT32
Definition: host.h:102
long long int inT64
Definition: host.h:108