tesseract  3.05.02
tesseract::ParamUtils Class Reference

#include <params.h>

Static Public Member Functions

static bool TESS_API ReadParamsFile (const char *file, SetParamConstraint constraint, ParamsVectors *member_params)
 
static bool ReadParamsFromFp (FILE *fp, inT64 end_offset, SetParamConstraint constraint, 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 77 of file params.h.

79  {
80  int i;
81  for (i = 0; i < global_vec.size(); ++i) {
82  if (strcmp(global_vec[i]->name_str(), name) == 0) return global_vec[i];
83  }
84  for (i = 0; i < member_vec.size(); ++i) {
85  if (strcmp(member_vec[i]->name_str(), name) == 0) return member_vec[i];
86  }
87  return NULL;
88  }
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 141 of file params.cpp.

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

◆ PrintParams()

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

Definition at line 179 of file params.cpp.

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

◆ 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  FILE *fp; // file pointer
45  // iterators
46 
47  if (*file == PLUS) {
48  nameoffset = 1;
49  } else if (*file == MINUS) {
50  nameoffset = 1;
51  } else {
52  nameoffset = 0;
53  }
54 
55  fp = fopen(file + nameoffset, "rb");
56  if (fp == NULL) {
57  tprintf("read_params_file: Can't open %s\n", file + nameoffset);
58  return true;
59  }
60  const bool anyerr = ReadParamsFromFp(fp, -1, constraint, member_params);
61  fclose(fp);
62  return anyerr;
63 }
short inT16
Definition: host.h:33
#define MINUS
Definition: params.cpp:30
#define PLUS
Definition: params.cpp:29
#define tprintf(...)
Definition: tprintf.h:31
static bool ReadParamsFromFp(FILE *fp, inT64 end_offset, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:65

◆ ReadParamsFromFp()

bool tesseract::ParamUtils::ReadParamsFromFp ( FILE *  fp,
inT64  end_offset,
SetParamConstraint  constraint,
ParamsVectors member_params 
)
static

Definition at line 65 of file params.cpp.

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

◆ RemoveParam()

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

Definition at line 91 of file params.h.

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

◆ ResetToDefaults()

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

Definition at line 204 of file params.cpp.

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

◆ SetParam()

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

Definition at line 97 of file params.cpp.

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

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