All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 (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

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
name_table name
#define NULL
Definition: host.h:144
bool tesseract::ParamUtils::GetParamAsString ( const char *  name,
const ParamsVectors member_params,
STRING value 
)
static

Definition at line 142 of file params.cpp.

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

Definition at line 180 of file params.cpp.

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

Definition at line 41 of file params.cpp.

43  {
44  inT16 nameoffset; // offset for real name
45  FILE *fp; // file pointer
46  // iterators
47 
48  if (*file == PLUS) {
49  nameoffset = 1;
50  } else if (*file == MINUS) {
51  nameoffset = 1;
52  } else {
53  nameoffset = 0;
54  }
55 
56  fp = fopen(file + nameoffset, "rb");
57  if (fp == NULL) {
58  tprintf("read_params_file: Can't open %s\n", file + nameoffset);
59  return true;
60  }
61  const bool anyerr = ReadParamsFromFp(fp, -1, constraint, member_params);
62  fclose(fp);
63  return anyerr;
64 }
#define MINUS
Definition: params.cpp:30
static bool ReadParamsFromFp(FILE *fp, inT64 end_offset, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:66
#define tprintf(...)
Definition: tprintf.h:31
#define PLUS
Definition: params.cpp:29
#define NULL
Definition: host.h:144
short inT16
Definition: host.h:100
bool tesseract::ParamUtils::ReadParamsFromFp ( FILE *  fp,
inT64  end_offset,
SetParamConstraint  constraint,
ParamsVectors member_params 
)
static

Definition at line 66 of file params.cpp.

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

Definition at line 205 of file params.cpp.

205  {
206  int v, i;
207  int num_iterations = (member_params == NULL) ? 1 : 2;
208  for (v = 0; v < num_iterations; ++v) {
209  ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
210  for (i = 0; i < vec->int_params.size(); ++i) {
211  vec->int_params[i]->ResetToDefault();
212  }
213  for (i = 0; i < vec->bool_params.size(); ++i) {
214  vec->bool_params[i]->ResetToDefault();
215  }
216  for (int i = 0; i < vec->string_params.size(); ++i) {
217  vec->string_params[i]->ResetToDefault();
218  }
219  for (int i = 0; i < vec->double_params.size(); ++i) {
220  vec->double_params[i]->ResetToDefault();
221  }
222  }
223 }
GenericVector< IntParam * > int_params
Definition: params.h:44
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:33
#define NULL
Definition: host.h:144
bool tesseract::ParamUtils::SetParam ( const char *  name,
const char *  value,
SetParamConstraint  constraint,
ParamsVectors member_params 
)
static

Definition at line 98 of file params.cpp.

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

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