tesseract  4.1.0
tesseract::ParamUtils Class Reference

#include <params.h>

Static Public Member Functions

static bool ReadParamsFile (const char *file, SetParamConstraint constraint, ParamsVectors *member_params)
 
static bool ReadParamsFromFp (SetParamConstraint constraint, TFile *fp, ParamsVectors *member_params)
 
static bool SetParam (const char *name, const char *value, SetParamConstraint constraint, ParamsVectors *member_params)
 
template<class T >
static T * FindParam (const char *name, const GenericVector< T * > &global_vec, const GenericVector< T * > &member_vec)
 
template<class T >
static void RemoveParam (T *param_ptr, GenericVector< T * > *vec)
 
static bool GetParamAsString (const char *name, const ParamsVectors *member_params, STRING *value)
 
static void PrintParams (FILE *fp, const ParamsVectors *member_params)
 
static void ResetToDefaults (ParamsVectors *member_params)
 

Detailed Description

Definition at line 50 of file params.h.

Member Function Documentation

template<class T >
static T* tesseract::ParamUtils::FindParam ( const char *  name,
const GenericVector< T * > &  global_vec,
const GenericVector< T * > &  member_vec 
)
inlinestatic

Definition at line 74 of file params.h.

75  {
76  int i;
77  for (i = 0; i < global_vec.size(); ++i) {
78  if (strcmp(global_vec[i]->name_str(), name) == 0) return global_vec[i];
79  }
80  for (i = 0; i < member_vec.size(); ++i) {
81  if (strcmp(member_vec[i]->name_str(), name) == 0) return member_vec[i];
82  }
83  return nullptr;
84  }
int size() const
Definition: genericvector.h:70
bool tesseract::ParamUtils::GetParamAsString ( const char *  name,
const ParamsVectors member_params,
STRING value 
)
static

Definition at line 142 of file params.cpp.

144  {
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 }
GenericVector< IntParam * > int_params
Definition: params.h:43
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:35
GenericVector< BoolParam * > bool_params
Definition: params.h:44
const char * string() const
Definition: strngs.cpp:194
GenericVector< StringParam * > string_params
Definition: params.h:45
GenericVector< DoubleParam * > double_params
Definition: params.h:46
void tesseract::ParamUtils::PrintParams ( FILE *  fp,
const ParamsVectors member_params 
)
static

Definition at line 180 of file params.cpp.

180  {
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 }
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:35
bool tesseract::ParamUtils::ReadParamsFile ( const char *  file,
SetParamConstraint  constraint,
ParamsVectors member_params 
)
static

Definition at line 42 of file params.cpp.

44  {
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 }
static bool ReadParamsFromFp(SetParamConstraint constraint, TFile *fp, ParamsVectors *member_params)
Definition: params.cpp:63
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
#define PLUS
Definition: params.cpp:32
#define MINUS
Definition: params.cpp:33
bool tesseract::ParamUtils::ReadParamsFromFp ( SetParamConstraint  constraint,
TFile fp,
ParamsVectors member_params 
)
static

Definition at line 63 of file params.cpp.

64  {
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 }
#define MAX_PATH
Definition: platform.h:29
void chomp_string(char *str)
Definition: helpers.h:77
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
template<class T >
static void tesseract::ParamUtils::RemoveParam ( T *  param_ptr,
GenericVector< T * > *  vec 
)
inlinestatic

Definition at line 87 of file params.h.

87  {
88  for (int i = 0; i < vec->size(); ++i) {
89  if ((*vec)[i] == param_ptr) {
90  vec->remove(i);
91  return;
92  }
93  }
94  }
void remove(int index)
int size() const
Definition: genericvector.h:70
void tesseract::ParamUtils::ResetToDefaults ( ParamsVectors member_params)
static

Definition at line 205 of file params.cpp.

205  {
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 }
GenericVector< IntParam * > int_params
Definition: params.h:43
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:35
bool tesseract::ParamUtils::SetParam ( const char *  name,
const char *  value,
SetParamConstraint  constraint,
ParamsVectors member_params 
)
static

Definition at line 92 of file params.cpp.

94  {
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 }
GenericVector< IntParam * > int_params
Definition: params.h:43
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:35
GenericVector< BoolParam * > bool_params
Definition: params.h:44
GenericVector< StringParam * > string_params
Definition: params.h:45
GenericVector< DoubleParam * > double_params
Definition: params.h:46

The documentation for this class was generated from the following files: