tesseract  5.0.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 std::vector< T * > &global_vec, const std::vector< T * > &member_vec)
 
template<class T >
static void RemoveParam (T *param_ptr, std::vector< T * > *vec)
 
static bool GetParamAsString (const char *name, const ParamsVectors *member_params, std::string *value)
 
static void PrintParams (FILE *fp, const ParamsVectors *member_params)
 
static void ResetToDefaults (ParamsVectors *member_params)
 

Detailed Description

Definition at line 53 of file params.h.

Member Function Documentation

◆ FindParam()

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

Definition at line 75 of file params.h.

76  {
77  for (auto *param : global_vec) {
78  if (strcmp(param->name_str(), name) == 0) {
79  return param;
80  }
81  }
82  for (auto *param : member_vec) {
83  if (strcmp(param->name_str(), name) == 0) {
84  return param;
85  }
86  }
87  return nullptr;
88  }

◆ GetParamAsString()

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

Definition at line 130 of file params.cpp.

131  {
132  // Look for the parameter among string parameters.
133  auto *sp =
134  FindParam<StringParam>(name, GlobalParams()->string_params, 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, member_params->int_params);
141  if (ip) {
142  *value = std::to_string(int32_t(*ip));
143  return true;
144  }
145  // Look for the parameter among bool parameters.
146  auto *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params, member_params->bool_params);
147  if (bp != nullptr) {
148  *value = bool(*bp) ? "1" : "0";
149  return true;
150  }
151  // Look for the parameter among double parameters.
152  auto *dp =
153  FindParam<DoubleParam>(name, GlobalParams()->double_params, member_params->double_params);
154  if (dp != nullptr) {
155  std::ostringstream stream;
156  stream.imbue(std::locale::classic());
157  stream << double(*dp);
158  *value = stream.str();
159  return true;
160  }
161  return false;
162 }
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:36
std::vector< BoolParam * > bool_params
Definition: params.h:47
std::vector< StringParam * > string_params
Definition: params.h:48
std::vector< IntParam * > int_params
Definition: params.h:46
std::vector< DoubleParam * > double_params
Definition: params.h:49

◆ PrintParams()

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

Definition at line 164 of file params.cpp.

164  {
165  int num_iterations = (member_params == nullptr) ? 1 : 2;
166  std::ostringstream stream;
167  stream.imbue(std::locale::classic());
168  for (int v = 0; v < num_iterations; ++v) {
169  const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
170  for (auto int_param : vec->int_params) {
171  stream << int_param->name_str() << '\t' << (int32_t)(*int_param) << '\t'
172  << int_param->info_str() << '\n';
173  }
174  for (auto bool_param : vec->bool_params) {
175  stream << bool_param->name_str() << '\t' << bool(*bool_param) << '\t'
176  << bool_param->info_str() << '\n';
177  }
178  for (auto string_param : vec->string_params) {
179  stream << string_param->name_str() << '\t' << string_param->c_str() << '\t'
180  << string_param->info_str() << '\n';
181  }
182  for (auto double_param : vec->double_params) {
183  stream << double_param->name_str() << '\t' << (double)(*double_param) << '\t'
184  << double_param->info_str() << '\n';
185  }
186  }
187  fprintf(fp, "%s", stream.str().c_str());
188 }

◆ ReadParamsFile()

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

Definition at line 41 of file params.cpp.

42  {
43  TFile fp;
44  if (!fp.Open(file, nullptr)) {
45  tprintf("read_params_file: Can't open %s\n", file);
46  return true;
47  }
48  return ReadParamsFromFp(constraint, &fp, member_params);
49 }
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
static bool ReadParamsFromFp(SetParamConstraint constraint, TFile *fp, ParamsVectors *member_params)
Definition: params.cpp:51

◆ ReadParamsFromFp()

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

Definition at line 51 of file params.cpp.

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

◆ RemoveParam()

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

Definition at line 91 of file params.h.

91  {
92  for (auto it = vec->begin(); it != vec->end(); ++it) {
93  if (*it == param_ptr) {
94  vec->erase(it);
95  break;
96  }
97  }
98  }

◆ ResetToDefaults()

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

Definition at line 191 of file params.cpp.

191  {
192  int num_iterations = (member_params == nullptr) ? 1 : 2;
193  for (int v = 0; v < num_iterations; ++v) {
194  ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
195  for (auto &param : vec->int_params) {
196  param->ResetToDefault();
197  }
198  for (auto &param : vec->bool_params) {
199  param->ResetToDefault();
200  }
201  for (auto &param : vec->string_params) {
202  param->ResetToDefault();
203  }
204  for (auto &param : vec->double_params) {
205  param->ResetToDefault();
206  }
207  }
208 }

◆ SetParam()

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

Definition at line 81 of file params.cpp.

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

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