tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/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   inT16 length;                  // length of line
00073   char *valptr;                  // value field
00074 
00075   while ((end_offset < 0 || ftell(fp) < end_offset) &&
00076          fgets(line, MAX_PATH, fp)) {
00077     if (line[0] != '\n' && line[0] != '#') {
00078       length = strlen (line);
00079       if (line[length - 1] == '\n')
00080         line[length - 1] = '\0';  // cut newline
00081       for (valptr = line; *valptr && *valptr != ' ' && *valptr != '\t';
00082         valptr++);
00083       if (*valptr) {             // found blank
00084         *valptr = '\0';          // make name a string
00085         do
00086           valptr++;              // find end of blanks
00087         while (*valptr == ' ' || *valptr == '\t');
00088       }
00089       foundit = SetParam(line, valptr, constraint, member_params);
00090 
00091       if (!foundit) {
00092         anyerr = true;         // had an error
00093         tprintf("read_params_file: parameter not found: %s\n", line);
00094         exit(1);
00095       }
00096     }
00097   }
00098   return anyerr;
00099 }
00100 
00101 bool ParamUtils::SetParam(const char *name, const char* value,
00102                           SetParamConstraint constraint,
00103                           ParamsVectors *member_params) {
00104   // Look for the parameter among string parameters.
00105   StringParam *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
00106                                            member_params->string_params);
00107   if (sp != NULL && sp->constraint_ok(constraint)) sp->set_value(value);
00108   if (*value == '\0') return (sp != NULL);
00109 
00110   // Look for the parameter among int parameters.
00111   int intval;
00112   IntParam *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
00113                                      member_params->int_params);
00114   if (ip && ip->constraint_ok(constraint) &&
00115       sscanf(value, INT32FORMAT, &intval) == 1) ip->set_value(intval);
00116 
00117   // Look for the parameter among bool parameters.
00118   BoolParam *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
00119                                        member_params->bool_params);
00120   if (bp != NULL && bp->constraint_ok(constraint)) {
00121     if (*value == 'T' || *value == 't' ||
00122         *value == 'Y' || *value == 'y' || *value == '1') {
00123       bp->set_value(true);
00124     } else if (*value == 'F' || *value == 'f' ||
00125                 *value == 'N' || *value == 'n' || *value == '0') {
00126       bp->set_value(false);
00127     }
00128   }
00129 
00130   // Look for the parameter among double parameters.
00131   double doubleval;
00132   DoubleParam *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
00133                                            member_params->double_params);
00134   if (dp != NULL && dp->constraint_ok(constraint)) {
00135 #ifdef EMBEDDED
00136       doubleval = strtofloat(value);
00137 #else
00138       if (sscanf(value, "%lf", &doubleval) == 1)
00139 #endif
00140       dp->set_value(doubleval);
00141   }
00142   return (sp || ip || bp || dp);
00143 }
00144 
00145 bool ParamUtils::GetParamAsString(const char *name,
00146                                   const ParamsVectors* member_params,
00147                                   STRING *value) {
00148   // Look for the parameter among string parameters.
00149   StringParam *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
00150                                            member_params->string_params);
00151   if (sp) {
00152     *value = sp->string();
00153     return true;
00154   }
00155   // Look for the parameter among int parameters.
00156   IntParam *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
00157                                      member_params->int_params);
00158   if (ip) {
00159     char buf[128];
00160     snprintf(buf, sizeof(buf), "%d", inT32(*ip));
00161     *value = buf;
00162     return true;
00163   }
00164   // Look for the parameter among bool parameters.
00165   BoolParam *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
00166                                        member_params->bool_params);
00167   if (bp != NULL) {
00168     *value = BOOL8(*bp) ? "1": "0";
00169     return true;
00170   }
00171   // Look for the parameter among double parameters.
00172   DoubleParam *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
00173                                            member_params->double_params);
00174   if (dp != NULL) {
00175     char buf[128];
00176     snprintf(buf, sizeof(buf), "%g", double(*dp));
00177     *value = buf;
00178     return true;
00179   }
00180   return false;
00181 }
00182 
00183 void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params) {
00184   int v, i;
00185   int num_iterations = (member_params == NULL) ? 1 : 2;
00186   for (v = 0; v < num_iterations; ++v) {
00187     const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
00188     for (i = 0; i < vec->int_params.size(); ++i) {
00189       fprintf(fp, "%s\t%d\n", vec->int_params[i]->name_str(),
00190               (inT32)(*vec->int_params[i]));
00191     }
00192     for (i = 0; i < vec->bool_params.size(); ++i) {
00193       fprintf(fp, "%s\t%d\n", vec->bool_params[i]->name_str(),
00194               (BOOL8)(*vec->bool_params[i]));
00195     }
00196     for (int i = 0; i < vec->string_params.size(); ++i) {
00197       fprintf(fp, "%s\t%s\n", vec->string_params[i]->name_str(),
00198               vec->string_params[i]->string());
00199     }
00200     for (int i = 0; i < vec->double_params.size(); ++i) {
00201       fprintf(fp, "%s\t%g\n", vec->double_params[i]->name_str(),
00202               (double)(*vec->double_params[i]));
00203     }
00204   }
00205 }
00206 
00207 // Resets all parameters back to default values;
00208 void ParamUtils::ResetToDefaults(ParamsVectors* member_params) {
00209   int v, i;
00210   int num_iterations = (member_params == NULL) ? 1 : 2;
00211   for (v = 0; v < num_iterations; ++v) {
00212     ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
00213     for (i = 0; i < vec->int_params.size(); ++i) {
00214       vec->int_params[i]->ResetToDefault();
00215     }
00216     for (i = 0; i < vec->bool_params.size(); ++i) {
00217       vec->bool_params[i]->ResetToDefault();
00218     }
00219     for (int i = 0; i < vec->string_params.size(); ++i) {
00220       vec->string_params[i]->ResetToDefault();
00221     }
00222     for (int i = 0; i < vec->double_params.size(); ++i) {
00223       vec->double_params[i]->ResetToDefault();
00224     }
00225   }
00226 }
00227 
00228 }  // namespace tesseract
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines