Tesseract  3.02
tesseract::ParamUtils Class Reference

#include <params.h>

List of all members.

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 
) [inline, static]

Definition at line 77 of file params.h.

                                                            {
    int i;
    for (i = 0; i < global_vec.size(); ++i) {
      if (strcmp(global_vec[i]->name_str(), name) == 0) return global_vec[i];
    }
    for (i = 0; i < member_vec.size(); ++i) {
      if (strcmp(member_vec[i]->name_str(), name) == 0) return member_vec[i];
    }
    return NULL;
  }
bool tesseract::ParamUtils::GetParamAsString ( const char *  name,
const ParamsVectors member_params,
STRING value 
) [static]

Definition at line 150 of file params.cpp.

                                                 {
  // Look for the parameter among string parameters.
  StringParam *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
                                           member_params->string_params);
  if (sp) {
    *value = sp->string();
    return true;
  }
  // Look for the parameter among int parameters.
  IntParam *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
                                     member_params->int_params);
  if (ip) {
    char buf[128];
    snprintf(buf, sizeof(buf), "%d", inT32(*ip));
    *value = buf;
    return true;
  }
  // Look for the parameter among bool parameters.
  BoolParam *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
                                       member_params->bool_params);
  if (bp != NULL) {
    *value = BOOL8(*bp) ? "1": "0";
    return true;
  }
  // Look for the parameter among double parameters.
  DoubleParam *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
                                           member_params->double_params);
  if (dp != NULL) {
    char buf[128];
    snprintf(buf, sizeof(buf), "%g", double(*dp));
    *value = buf;
    return true;
  }
  return false;
}
void tesseract::ParamUtils::PrintParams ( FILE *  fp,
const ParamsVectors member_params 
) [static]

Definition at line 188 of file params.cpp.

                                                                         {
  int v, i;
  int num_iterations = (member_params == NULL) ? 1 : 2;
  for (v = 0; v < num_iterations; ++v) {
    const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
    for (i = 0; i < vec->int_params.size(); ++i) {
      fprintf(fp, "%s\t%d\n", vec->int_params[i]->name_str(),
              (inT32)(*vec->int_params[i]));
    }
    for (i = 0; i < vec->bool_params.size(); ++i) {
      fprintf(fp, "%s\t%d\n", vec->bool_params[i]->name_str(),
              (BOOL8)(*vec->bool_params[i]));
    }
    for (int i = 0; i < vec->string_params.size(); ++i) {
      fprintf(fp, "%s\t%s\n", vec->string_params[i]->name_str(),
              vec->string_params[i]->string());
    }
    for (int i = 0; i < vec->double_params.size(); ++i) {
      fprintf(fp, "%s\t%g\n", vec->double_params[i]->name_str(),
              (double)(*vec->double_params[i]));
    }
  }
}
bool tesseract::ParamUtils::ReadParamsFile ( const char *  file,
SetParamConstraint  constraint,
ParamsVectors member_params 
) [static]

Definition at line 43 of file params.cpp.

                                                              {
  char flag;                     // file flag
  inT16 nameoffset;              // offset for real name
  FILE *fp;                      // file pointer
                                 // iterators

  if (*file == PLUS) {
    flag = PLUS;                 // file has flag
    nameoffset = 1;
  } else if (*file == MINUS) {
    flag = MINUS;
    nameoffset = 1;
  } else {
    flag = EQUAL;
    nameoffset = 0;
  }

  fp = fopen(file + nameoffset, "rb");
  if (fp == NULL) {
    tprintf("read_params_file: Can't open %s\n", file + nameoffset);
    return true;
  }
  return ReadParamsFromFp(fp, -1, constraint, member_params);
  fclose(fp);
}
bool tesseract::ParamUtils::ReadParamsFromFp ( FILE *  fp,
inT64  end_offset,
SetParamConstraint  constraint,
ParamsVectors member_params 
) [static]

Definition at line 71 of file params.cpp.

                                                                {
  char line[MAX_PATH];           // input line
  bool anyerr = false;           // true if any error
  bool foundit;                  // found parameter
  inT16 length;                  // length of line
  char *valptr;                  // value field

  while ((end_offset < 0 || ftell(fp) < end_offset) &&
         fgets(line, MAX_PATH, fp)) {
    if (line[0] != '\n' && line[0] != '#') {
      length = strlen (line);
      if (line[length - 1] == '\n')
        line[length - 1] = '\0';  // cut newline
      for (valptr = line; *valptr && *valptr != ' ' && *valptr != '\t';
        valptr++);
      if (*valptr) {             // found blank
        *valptr = '\0';          // make name a string
        do
          valptr++;              // find end of blanks
        while (*valptr == ' ' || *valptr == '\t');
      }
      foundit = SetParam(line, valptr, constraint, member_params);

      if (!foundit) {
        anyerr = true;         // had an error
        tprintf("read_params_file: parameter not found: %s\n", line);
        exit(1);
      }
    }
  }
  return anyerr;
}
template<class T >
static void tesseract::ParamUtils::RemoveParam ( T *  param_ptr,
GenericVector< T * > *  vec 
) [inline, static]

Definition at line 91 of file params.h.

                                                                 {
    for (int i = 0; i < vec->size(); ++i) {
      if ((*vec)[i] == param_ptr) {
        vec->remove(i);
        return;
      }
    }
  }
bool tesseract::ParamUtils::SetParam ( const char *  name,
const char *  value,
SetParamConstraint  constraint,
ParamsVectors member_params 
) [static]

Definition at line 106 of file params.cpp.

                                                        {
  // Look for the parameter among string parameters.
  StringParam *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
                                           member_params->string_params);
  if (sp != NULL && sp->constraint_ok(constraint)) sp->set_value(value);
  if (*value == '\0') return (sp != NULL);

  // Look for the parameter among int parameters.
  int intval;
  IntParam *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
                                     member_params->int_params);
  if (ip && ip->constraint_ok(constraint) &&
      sscanf(value, INT32FORMAT, &intval) == 1) ip->set_value(intval);

  // Look for the parameter among bool parameters.
  BoolParam *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
                                       member_params->bool_params);
  if (bp != NULL && bp->constraint_ok(constraint)) {
    if (*value == 'T' || *value == 't' ||
        *value == 'Y' || *value == 'y' || *value == '1') {
      bp->set_value(true);
    } else if (*value == 'F' || *value == 'f' ||
                *value == 'N' || *value == 'n' || *value == '0') {
      bp->set_value(false);
    }
  }

  // Look for the parameter among double parameters.
  double doubleval;
  DoubleParam *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
                                           member_params->double_params);
  if (dp != NULL && dp->constraint_ok(constraint)) {
#ifdef EMBEDDED
      doubleval = strtofloat(value);
#else
      if (sscanf(value, "%lf", &doubleval) == 1)
#endif
      dp->set_value(doubleval);
  }
  return (sp || ip || bp || dp);
}

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