Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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)
 

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

Definition at line 150 of file params.cpp.

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

Definition at line 188 of file params.cpp.

188  {
189  int v, i;
190  int num_iterations = (member_params == NULL) ? 1 : 2;
191  for (v = 0; v < num_iterations; ++v) {
192  const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
193  for (i = 0; i < vec->int_params.size(); ++i) {
194  fprintf(fp, "%s\t%d\n", vec->int_params[i]->name_str(),
195  (inT32)(*vec->int_params[i]));
196  }
197  for (i = 0; i < vec->bool_params.size(); ++i) {
198  fprintf(fp, "%s\t%d\n", vec->bool_params[i]->name_str(),
199  (BOOL8)(*vec->bool_params[i]));
200  }
201  for (int i = 0; i < vec->string_params.size(); ++i) {
202  fprintf(fp, "%s\t%s\n", vec->string_params[i]->name_str(),
203  vec->string_params[i]->string());
204  }
205  for (int i = 0; i < vec->double_params.size(); ++i) {
206  fprintf(fp, "%s\t%g\n", vec->double_params[i]->name_str(),
207  (double)(*vec->double_params[i]));
208  }
209  }
210 }
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:35
unsigned char BOOL8
Definition: host.h:113
#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 43 of file params.cpp.

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

Definition at line 71 of file params.cpp.

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

Definition at line 106 of file params.cpp.

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

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