tesseract  5.0.0-alpha-619-ge9db
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 64 of file params.h.

Member Function Documentation

◆ FindParam()

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 88 of file params.h.

88  {
89  if ((*vec)[i] == param_ptr) {
90  vec->remove(i);
91  return;
92  }
93  }
94  }
95  // Fetches the value of the named param as a STRING. Returns false if not
96  // found.
97  static bool GetParamAsString(const char* name,
98  const ParamsVectors* member_params,

◆ GetParamAsString()

bool tesseract::ParamUtils::GetParamAsString ( const char *  name,
const ParamsVectors member_params,
STRING value 
)
static

Definition at line 129 of file params.cpp.

131  {
132  // Look for the parameter among string parameters.
133  auto *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
134  member_params->string_params);
135  if (sp) {
136  *value = sp->c_str();
137  return true;
138  }
139  // Look for the parameter among int parameters.
140  auto *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
141  member_params->int_params);
142  if (ip) {
143  char buf[128];
144  snprintf(buf, sizeof(buf), "%d", int32_t(*ip));
145  *value = buf;
146  return true;
147  }
148  // Look for the parameter among bool parameters.
149  auto *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
150  member_params->bool_params);
151  if (bp != nullptr) {
152  *value = bool(*bp) ? "1": "0";
153  return true;
154  }
155  // Look for the parameter among double parameters.
156  auto *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
157  member_params->double_params);
158  if (dp != nullptr) {
159  std::ostringstream stream;
160  stream.imbue(std::locale::classic());
161  stream << double(*dp);
162  *value = stream.str().c_str();
163  return true;
164  }
165  return false;
166 }

◆ PrintParams()

void tesseract::ParamUtils::PrintParams ( FILE *  fp,
const ParamsVectors member_params 
)
static

Definition at line 168 of file params.cpp.

168  {
169  int num_iterations = (member_params == nullptr) ? 1 : 2;
170  std::ostringstream stream;
171  stream.imbue(std::locale::classic());
172  for (int v = 0; v < num_iterations; ++v) {
173  const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
174  for (int i = 0; i < vec->int_params.size(); ++i) {
175  stream << vec->int_params[i]->name_str() << '\t' <<
176  (int32_t)(*vec->int_params[i]) << '\t' <<
177  vec->int_params[i]->info_str() << '\n';
178  }
179  for (int i = 0; i < vec->bool_params.size(); ++i) {
180  stream << vec->bool_params[i]->name_str() << '\t' <<
181  bool(*vec->bool_params[i]) << '\t' <<
182  vec->bool_params[i]->info_str() << '\n';
183  }
184  for (int i = 0; i < vec->string_params.size(); ++i) {
185  stream << vec->string_params[i]->name_str() << '\t' <<
186  vec->string_params[i]->c_str() << '\t' <<
187  vec->string_params[i]->info_str() << '\n';
188  }
189  for (int i = 0; i < vec->double_params.size(); ++i) {
190  stream << vec->double_params[i]->name_str() << '\t' <<
191  (double)(*vec->double_params[i]) << '\t' <<
192  vec->double_params[i]->info_str() << '\n';
193  }
194  }
195  fprintf(fp, "%s", stream.str().c_str());
196 }

◆ ReadParamsFile()

bool tesseract::ParamUtils::ReadParamsFile ( const char *  file,
SetParamConstraint  constraint,
ParamsVectors member_params 
)
static

Definition at line 39 of file params.cpp.

41  {
42  TFile fp;
43  if (!fp.Open(file, nullptr)) {
44  tprintf("read_params_file: Can't open %s\n", file);
45  return true;
46  }
47  return ReadParamsFromFp(constraint, &fp, member_params);
48 }

◆ ReadParamsFromFp()

bool tesseract::ParamUtils::ReadParamsFromFp ( SetParamConstraint  constraint,
TFile fp,
ParamsVectors member_params 
)
static

Definition at line 50 of file params.cpp.

51  {
52  char line[MAX_PATH]; // input line
53  bool anyerr = false; // true if any error
54  bool foundit; // found parameter
55  char *valptr; // value field
56 
57  while (fp->FGets(line, MAX_PATH) != nullptr) {
58  if (line[0] != '\r' && line[0] != '\n' && line[0] != '#') {
59  chomp_string(line); // remove newline
60  for (valptr = line; *valptr && *valptr != ' ' && *valptr != '\t';
61  valptr++);
62  if (*valptr) { // found blank
63  *valptr = '\0'; // make name a string
64  do
65  valptr++; // find end of blanks
66  while (*valptr == ' ' || *valptr == '\t');
67  }
68  foundit = SetParam(line, valptr, constraint, member_params);
69 
70  if (!foundit) {
71  anyerr = true; // had an error
72  tprintf("Warning: Parameter not found: %s\n", line);
73  }
74  }
75  }
76  return anyerr;
77 }

◆ RemoveParam()

template<class T >
static void tesseract::ParamUtils::RemoveParam ( T *  param_ptr,
GenericVector< T * > *  vec 
)
inlinestatic

Definition at line 101 of file params.h.

109  {

◆ ResetToDefaults()

void tesseract::ParamUtils::ResetToDefaults ( ParamsVectors member_params)
static

Definition at line 199 of file params.cpp.

199  {
200  int v, i;
201  int num_iterations = (member_params == nullptr) ? 1 : 2;
202  for (v = 0; v < num_iterations; ++v) {
203  ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
204  for (i = 0; i < vec->int_params.size(); ++i) {
205  vec->int_params[i]->ResetToDefault();
206  }
207  for (i = 0; i < vec->bool_params.size(); ++i) {
208  vec->bool_params[i]->ResetToDefault();
209  }
210  for (int i = 0; i < vec->string_params.size(); ++i) {
211  vec->string_params[i]->ResetToDefault();
212  }
213  for (int i = 0; i < vec->double_params.size(); ++i) {
214  vec->double_params[i]->ResetToDefault();
215  }
216  }
217 }

◆ SetParam()

bool tesseract::ParamUtils::SetParam ( const char *  name,
const char *  value,
SetParamConstraint  constraint,
ParamsVectors member_params 
)
static

Definition at line 79 of file params.cpp.

81  {
82  // Look for the parameter among string parameters.
83  auto *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
84  member_params->string_params);
85  if (sp != nullptr && sp->constraint_ok(constraint)) sp->set_value(value);
86  if (*value == '\0') return (sp != nullptr);
87 
88  // Look for the parameter among int parameters.
89  auto *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
90  member_params->int_params);
91  if (ip && ip->constraint_ok(constraint)) {
92  int intval = INT_MIN;
93  std::stringstream stream(value);
94  stream.imbue(std::locale::classic());
95  stream >> intval;
96  if (intval != INT_MIN) {
97  ip->set_value(intval);
98  }
99  }
100 
101  // Look for the parameter among bool parameters.
102  auto *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
103  member_params->bool_params);
104  if (bp != nullptr && bp->constraint_ok(constraint)) {
105  if (*value == 'T' || *value == 't' ||
106  *value == 'Y' || *value == 'y' || *value == '1') {
107  bp->set_value(true);
108  } else if (*value == 'F' || *value == 'f' ||
109  *value == 'N' || *value == 'n' || *value == '0') {
110  bp->set_value(false);
111  }
112  }
113 
114  // Look for the parameter among double parameters.
115  auto *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
116  member_params->double_params);
117  if (dp != nullptr && dp->constraint_ok(constraint)) {
118  double doubleval = NAN;
119  std::stringstream stream(value);
120  stream.imbue(std::locale::classic());
121  stream >> doubleval;
122  if (!std::isnan(doubleval)) {
123  dp->set_value(doubleval);
124  }
125  }
126  return (sp || ip || bp || dp);
127 }

The documentation for this class was generated from the following files:
tesseract::ParamsVectors::int_params
GenericVector< IntParam * > int_params
Definition: params.h:57
tesseract::ParamsVectors::double_params
GenericVector< DoubleParam * > double_params
Definition: params.h:60
tesseract::ParamsVectors::string_params
GenericVector< StringParam * > string_params
Definition: params.h:59
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
file
Definition: include_gunit.h:22
chomp_string
void chomp_string(char *str)
Definition: helpers.h:75
tesseract::ParamUtils::GetParamAsString
static bool GetParamAsString(const char *name, const ParamsVectors *member_params, STRING *value)
Definition: params.cpp:129
GlobalParams
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:32
tesseract::ParamUtils::ReadParamsFromFp
static bool ReadParamsFromFp(SetParamConstraint constraint, TFile *fp, ParamsVectors *member_params)
Definition: params.cpp:50
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
tesseract::ParamUtils::SetParam
static bool SetParam(const char *name, const char *value, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:79
MAX_PATH
#define MAX_PATH
Definition: platform.h:29
tesseract::ParamsVectors::bool_params
GenericVector< BoolParam * > bool_params
Definition: params.h:58