tesseract 3.04.01

ccutil/params.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        params.cpp
00003  * Description: Initialization and setting of Tesseract parameters.
00004  * Author:      Ray Smith
00005  * Created:     Fri Feb 22 16:22:34 GMT 1991
00006  *
00007  * (C) Copyright 1991, Hewlett-Packard Ltd.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #include          <stdio.h>
00021 #include          <string.h>
00022 #include          <stdlib.h>
00023 
00024 #include          "genericvector.h"
00025 #include          "scanutils.h"
00026 #include          "tprintf.h"
00027 #include          "params.h"
00028 
00029 #define PLUS          '+'        //flag states
00030 #define MINUS         '-'
00031 #define EQUAL         '='
00032 
00033 tesseract::ParamsVectors *GlobalParams() {
00034   static tesseract::ParamsVectors *global_params =
00035     new tesseract::ParamsVectors();
00036   return global_params;
00037 }
00038 
00039 namespace tesseract {
00040 
00041 bool ParamUtils::ReadParamsFile(const char *file,
00042                                 SetParamConstraint constraint,
00043                                 ParamsVectors *member_params) {
00044   inT16 nameoffset;              // offset for real name
00045   FILE *fp;                      // file pointer
00046                                  // iterators
00047 
00048   if (*file == PLUS) {
00049     nameoffset = 1;
00050   } else if (*file == MINUS) {
00051     nameoffset = 1;
00052   } else {
00053     nameoffset = 0;
00054   }
00055 
00056   fp = fopen(file + nameoffset, "rb");
00057   if (fp == NULL) {
00058     tprintf("read_params_file: Can't open %s\n", file + nameoffset);
00059     return true;
00060   }
00061   const bool anyerr = ReadParamsFromFp(fp, -1, constraint, member_params);
00062   fclose(fp);
00063   return anyerr;
00064 }
00065 
00066 bool ParamUtils::ReadParamsFromFp(FILE *fp, inT64 end_offset,
00067                                   SetParamConstraint constraint,
00068                                   ParamsVectors *member_params) {
00069   char line[MAX_PATH];           // input line
00070   bool anyerr = false;           // true if any error
00071   bool foundit;                  // found parameter
00072   char *valptr;                  // value field
00073 
00074   while ((end_offset < 0 || ftell(fp) < end_offset) &&
00075          fgets(line, MAX_PATH, fp)) {
00076     if (line[0] != '\n' && line[0] != '#') {
00077       chomp_string(line);  // remove newline
00078       for (valptr = line; *valptr && *valptr != ' ' && *valptr != '\t';
00079         valptr++);
00080       if (*valptr) {             // found blank
00081         *valptr = '\0';          // make name a string
00082         do
00083           valptr++;              // find end of blanks
00084         while (*valptr == ' ' || *valptr == '\t');
00085       }
00086       foundit = SetParam(line, valptr, constraint, member_params);
00087 
00088       if (!foundit) {
00089         anyerr = true;         // had an error
00090         tprintf("read_params_file: parameter not found: %s\n", line);
00091         exit(1);
00092       }
00093     }
00094   }
00095   return anyerr;
00096 }
00097 
00098 bool ParamUtils::SetParam(const char *name, const char* value,
00099                           SetParamConstraint constraint,
00100                           ParamsVectors *member_params) {
00101   // Look for the parameter among string parameters.
00102   StringParam *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
00103                                            member_params->string_params);
00104   if (sp != NULL && sp->constraint_ok(constraint)) sp->set_value(value);
00105   if (*value == '\0') return (sp != NULL);
00106 
00107   // Look for the parameter among int parameters.
00108   int intval;
00109   IntParam *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
00110                                      member_params->int_params);
00111   if (ip && ip->constraint_ok(constraint) &&
00112       sscanf(value, INT32FORMAT, &intval) == 1) ip->set_value(intval);
00113 
00114   // Look for the parameter among bool parameters.
00115   BoolParam *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
00116                                        member_params->bool_params);
00117   if (bp != NULL && bp->constraint_ok(constraint)) {
00118     if (*value == 'T' || *value == 't' ||
00119         *value == 'Y' || *value == 'y' || *value == '1') {
00120       bp->set_value(true);
00121     } else if (*value == 'F' || *value == 'f' ||
00122                 *value == 'N' || *value == 'n' || *value == '0') {
00123       bp->set_value(false);
00124     }
00125   }
00126 
00127   // Look for the parameter among double parameters.
00128   double doubleval;
00129   DoubleParam *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
00130                                            member_params->double_params);
00131   if (dp != NULL && dp->constraint_ok(constraint)) {
00132 #ifdef EMBEDDED
00133       doubleval = strtofloat(value);
00134 #else
00135       if (sscanf(value, "%lf", &doubleval) == 1)
00136 #endif
00137       dp->set_value(doubleval);
00138   }
00139   return (sp || ip || bp || dp);
00140 }
00141 
00142 bool ParamUtils::GetParamAsString(const char *name,
00143                                   const ParamsVectors* member_params,
00144                                   STRING *value) {
00145   // Look for the parameter among string parameters.
00146   StringParam *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
00147                                            member_params->string_params);
00148   if (sp) {
00149     *value = sp->string();
00150     return true;
00151   }
00152   // Look for the parameter among int parameters.
00153   IntParam *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
00154                                      member_params->int_params);
00155   if (ip) {
00156     char buf[128];
00157     snprintf(buf, sizeof(buf), "%d", inT32(*ip));
00158     *value = buf;
00159     return true;
00160   }
00161   // Look for the parameter among bool parameters.
00162   BoolParam *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
00163                                        member_params->bool_params);
00164   if (bp != NULL) {
00165     *value = BOOL8(*bp) ? "1": "0";
00166     return true;
00167   }
00168   // Look for the parameter among double parameters.
00169   DoubleParam *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
00170                                            member_params->double_params);
00171   if (dp != NULL) {
00172     char buf[128];
00173     snprintf(buf, sizeof(buf), "%g", double(*dp));
00174     *value = buf;
00175     return true;
00176   }
00177   return false;
00178 }
00179 
00180 void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params) {
00181   int v, i;
00182   int num_iterations = (member_params == NULL) ? 1 : 2;
00183   for (v = 0; v < num_iterations; ++v) {
00184     const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
00185     for (i = 0; i < vec->int_params.size(); ++i) {
00186       fprintf(fp, "%s\t%d\t%s\n", vec->int_params[i]->name_str(),
00187               (inT32)(*vec->int_params[i]), vec->int_params[i]->info_str());
00188     }
00189     for (i = 0; i < vec->bool_params.size(); ++i) {
00190       fprintf(fp, "%s\t%d\t%s\n", vec->bool_params[i]->name_str(),
00191               (BOOL8)(*vec->bool_params[i]), vec->bool_params[i]->info_str());
00192     }
00193     for (int i = 0; i < vec->string_params.size(); ++i) {
00194       fprintf(fp, "%s\t%s\t%s\n", vec->string_params[i]->name_str(),
00195               vec->string_params[i]->string(), vec->string_params[i]->info_str());
00196     }
00197     for (int i = 0; i < vec->double_params.size(); ++i) {
00198       fprintf(fp, "%s\t%g\t%s\n", vec->double_params[i]->name_str(),
00199               (double)(*vec->double_params[i]), vec->double_params[i]->info_str());
00200     }
00201   }
00202 }
00203 
00204 // Resets all parameters back to default values;
00205 void ParamUtils::ResetToDefaults(ParamsVectors* member_params) {
00206   int v, i;
00207   int num_iterations = (member_params == NULL) ? 1 : 2;
00208   for (v = 0; v < num_iterations; ++v) {
00209     ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
00210     for (i = 0; i < vec->int_params.size(); ++i) {
00211       vec->int_params[i]->ResetToDefault();
00212     }
00213     for (i = 0; i < vec->bool_params.size(); ++i) {
00214       vec->bool_params[i]->ResetToDefault();
00215     }
00216     for (int i = 0; i < vec->string_params.size(); ++i) {
00217       vec->string_params[i]->ResetToDefault();
00218     }
00219     for (int i = 0; i < vec->double_params.size(); ++i) {
00220       vec->double_params[i]->ResetToDefault();
00221     }
00222   }
00223 }
00224 
00225 }  // namespace tesseract
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines