tesseract  4.00.00dev
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 NULL;
87  }
int size() const
Definition: genericvector.h:72

◆ GetParamAsString()

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

Definition at line 135 of file params.cpp.

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

◆ PrintParams()

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

Definition at line 173 of file params.cpp.

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

◆ ReadParamsFile()

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

Definition at line 40 of file params.cpp.

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

◆ ReadParamsFromFp()

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

Definition at line 61 of file params.cpp.

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

◆ 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  }
void remove(int index)
int size() const
Definition: genericvector.h:72

◆ ResetToDefaults()

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

Definition at line 198 of file params.cpp.

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

◆ SetParam()

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

Definition at line 91 of file params.cpp.

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

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