tesseract  4.0.0-1-g2a2b
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 51 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 76 of file params.h.

78  {
79  int i;
80  for (i = 0; i < global_vec.size(); ++i) {
81  if (strcmp(global_vec[i]->name_str(), name) == 0) return global_vec[i];
82  }
83  for (i = 0; i < member_vec.size(); ++i) {
84  if (strcmp(member_vec[i]->name_str(), name) == 0) return member_vec[i];
85  }
86  return nullptr;
87  }
int size() const
Definition: genericvector.h:71

◆ GetParamAsString()

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

Definition at line 133 of file params.cpp.

135  {
136  // Look for the parameter among string parameters.
137  StringParam *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
138  member_params->string_params);
139  if (sp) {
140  *value = sp->string();
141  return true;
142  }
143  // Look for the parameter among int parameters.
144  IntParam *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
145  member_params->int_params);
146  if (ip) {
147  char buf[128];
148  snprintf(buf, sizeof(buf), "%d", int32_t(*ip));
149  *value = buf;
150  return true;
151  }
152  // Look for the parameter among bool parameters.
153  BoolParam *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
154  member_params->bool_params);
155  if (bp != nullptr) {
156  *value = BOOL8(*bp) ? "1": "0";
157  return true;
158  }
159  // Look for the parameter among double parameters.
160  DoubleParam *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
161  member_params->double_params);
162  if (dp != nullptr) {
163  char buf[128];
164  snprintf(buf, sizeof(buf), "%g", double(*dp));
165  *value = buf;
166  return true;
167  }
168  return false;
169 }
GenericVector< IntParam * > int_params
Definition: params.h:44
GenericVector< BoolParam * > bool_params
Definition: params.h:45
const char * string() const
Definition: strngs.cpp:196
GenericVector< StringParam * > string_params
Definition: params.h:46
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:32
unsigned char BOOL8
Definition: host.h:34
GenericVector< DoubleParam * > double_params
Definition: params.h:47

◆ PrintParams()

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

Definition at line 171 of file params.cpp.

171  {
172  int v, i;
173  int num_iterations = (member_params == nullptr) ? 1 : 2;
174  for (v = 0; v < num_iterations; ++v) {
175  const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
176  for (i = 0; i < vec->int_params.size(); ++i) {
177  fprintf(fp, "%s\t%d\t%s\n", vec->int_params[i]->name_str(),
178  (int32_t)(*vec->int_params[i]), vec->int_params[i]->info_str());
179  }
180  for (i = 0; i < vec->bool_params.size(); ++i) {
181  fprintf(fp, "%s\t%d\t%s\n", vec->bool_params[i]->name_str(),
182  (BOOL8)(*vec->bool_params[i]), vec->bool_params[i]->info_str());
183  }
184  for (int i = 0; i < vec->string_params.size(); ++i) {
185  fprintf(fp, "%s\t%s\t%s\n", vec->string_params[i]->name_str(),
186  vec->string_params[i]->string(), vec->string_params[i]->info_str());
187  }
188  for (int i = 0; i < vec->double_params.size(); ++i) {
189  fprintf(fp, "%s\t%g\t%s\n", vec->double_params[i]->name_str(),
190  (double)(*vec->double_params[i]), vec->double_params[i]->info_str());
191  }
192  }
193 }
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:32
unsigned char BOOL8
Definition: host.h:34

◆ ReadParamsFile()

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

Definition at line 39 of file params.cpp.

41  {
42  int16_t nameoffset; // offset for real name
43 
44  if (*file == PLUS) {
45  nameoffset = 1;
46  } else if (*file == MINUS) {
47  nameoffset = 1;
48  } else {
49  nameoffset = 0;
50  }
51 
52  TFile fp;
53  if (!fp.Open(file + nameoffset, nullptr)) {
54  tprintf("read_params_file: Can't open %s\n", file + nameoffset);
55  return true;
56  }
57  return ReadParamsFromFp(constraint, &fp, member_params);
58 }
#define PLUS
Definition: params.cpp:28
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
static bool ReadParamsFromFp(SetParamConstraint constraint, TFile *fp, ParamsVectors *member_params)
Definition: params.cpp:60
#define MINUS
Definition: params.cpp:29

◆ ReadParamsFromFp()

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

Definition at line 60 of file params.cpp.

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

◆ RemoveParam()

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

Definition at line 90 of file params.h.

90  {
91  for (int i = 0; i < vec->size(); ++i) {
92  if ((*vec)[i] == param_ptr) {
93  vec->remove(i);
94  return;
95  }
96  }
97  }
int size() const
Definition: genericvector.h:71
void remove(int index)

◆ ResetToDefaults()

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

Definition at line 196 of file params.cpp.

196  {
197  int v, i;
198  int num_iterations = (member_params == nullptr) ? 1 : 2;
199  for (v = 0; v < num_iterations; ++v) {
200  ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
201  for (i = 0; i < vec->int_params.size(); ++i) {
202  vec->int_params[i]->ResetToDefault();
203  }
204  for (i = 0; i < vec->bool_params.size(); ++i) {
205  vec->bool_params[i]->ResetToDefault();
206  }
207  for (int i = 0; i < vec->string_params.size(); ++i) {
208  vec->string_params[i]->ResetToDefault();
209  }
210  for (int i = 0; i < vec->double_params.size(); ++i) {
211  vec->double_params[i]->ResetToDefault();
212  }
213  }
214 }
GenericVector< IntParam * > int_params
Definition: params.h:44
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:32

◆ SetParam()

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

Definition at line 89 of file params.cpp.

91  {
92  // Look for the parameter among string parameters.
93  StringParam *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
94  member_params->string_params);
95  if (sp != nullptr && sp->constraint_ok(constraint)) sp->set_value(value);
96  if (*value == '\0') return (sp != nullptr);
97 
98  // Look for the parameter among int parameters.
99  int intval;
100  IntParam *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
101  member_params->int_params);
102  if (ip && ip->constraint_ok(constraint) && sscanf(value, "%d", &intval) == 1)
103  ip->set_value(intval);
104 
105  // Look for the parameter among bool parameters.
106  BoolParam *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
107  member_params->bool_params);
108  if (bp != nullptr && bp->constraint_ok(constraint)) {
109  if (*value == 'T' || *value == 't' ||
110  *value == 'Y' || *value == 'y' || *value == '1') {
111  bp->set_value(true);
112  } else if (*value == 'F' || *value == 'f' ||
113  *value == 'N' || *value == 'n' || *value == '0') {
114  bp->set_value(false);
115  }
116  }
117 
118  // Look for the parameter among double parameters.
119  double doubleval;
120  DoubleParam *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
121  member_params->double_params);
122  if (dp != nullptr && dp->constraint_ok(constraint)) {
123 #ifdef EMBEDDED
124  doubleval = strtofloat(value);
125 #else
126  if (sscanf(value, "%lf", &doubleval) == 1)
127 #endif
128  dp->set_value(doubleval);
129  }
130  return (sp || ip || bp || dp);
131 }
GenericVector< IntParam * > int_params
Definition: params.h:44
GenericVector< BoolParam * > bool_params
Definition: params.h:45
GenericVector< StringParam * > string_params
Definition: params.h:46
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:32
GenericVector< DoubleParam * > double_params
Definition: params.h:47

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