Tesseract  3.02
tesseract-ocr/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          "mfcpch.h"     //precompiled headers
00021 
00022 #include          <stdio.h>
00023 #include          <string.h>
00024 #include          <stdlib.h>
00025 
00026 #include          "genericvector.h"
00027 #include          "scanutils.h"
00028 #include          "tprintf.h"
00029 #include          "params.h"
00030 
00031 #define PLUS          '+'        //flag states
00032 #define MINUS         '-'
00033 #define EQUAL         '='
00034 
00035 tesseract::ParamsVectors *GlobalParams() {
00036   static tesseract::ParamsVectors *global_params =
00037     new tesseract::ParamsVectors();
00038   return global_params;
00039 }
00040 
00041 namespace tesseract {
00042 
00043 bool ParamUtils::ReadParamsFile(const char *file,
00044                                 SetParamConstraint constraint,
00045                                 ParamsVectors *member_params) {
00046   char flag;                     // file flag
00047   inT16 nameoffset;              // offset for real name
00048   FILE *fp;                      // file pointer
00049                                  // iterators
00050 
00051   if (*file == PLUS) {
00052     flag = PLUS;                 // file has flag
00053     nameoffset = 1;
00054   } else if (*file == MINUS) {
00055     flag = MINUS;
00056     nameoffset = 1;
00057   } else {
00058     flag = EQUAL;
00059     nameoffset = 0;
00060   }
00061 
00062   fp = fopen(file + nameoffset, "rb");
00063   if (fp == NULL) {
00064     tprintf("read_params_file: Can't open %s\n", file + nameoffset);
00065     return true;
00066   }
00067   return ReadParamsFromFp(fp, -1, constraint, member_params);
00068   fclose(fp);
00069 }
00070 
00071 bool ParamUtils::ReadParamsFromFp(FILE *fp, inT64 end_offset,
00072                                   SetParamConstraint constraint,
00073                                   ParamsVectors *member_params) {
00074   char line[MAX_PATH];           // input line
00075   bool anyerr = false;           // true if any error
00076   bool foundit;                  // found parameter
00077   inT16 length;                  // length of line
00078   char *valptr;                  // value field
00079 
00080   while ((end_offset < 0 || ftell(fp) < end_offset) &&
00081          fgets(line, MAX_PATH, fp)) {
00082     if (line[0] != '\n' && line[0] != '#') {
00083       length = strlen (line);
00084       if (line[length - 1] == '\n')
00085         line[length - 1] = '\0';  // cut newline
00086       for (valptr = line; *valptr && *valptr != ' ' && *valptr != '\t';
00087         valptr++);
00088       if (*valptr) {             // found blank
00089         *valptr = '\0';          // make name a string
00090         do
00091           valptr++;              // find end of blanks
00092         while (*valptr == ' ' || *valptr == '\t');
00093       }
00094       foundit = SetParam(line, valptr, constraint, member_params);
00095 
00096       if (!foundit) {
00097         anyerr = true;         // had an error
00098         tprintf("read_params_file: parameter not found: %s\n", line);
00099         exit(1);
00100       }
00101     }
00102   }
00103   return anyerr;
00104 }
00105 
00106 bool ParamUtils::SetParam(const char *name, const char* value,
00107                           SetParamConstraint constraint,
00108                           ParamsVectors *member_params) {
00109   // Look for the parameter among string parameters.
00110   StringParam *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
00111                                            member_params->string_params);
00112   if (sp != NULL && sp->constraint_ok(constraint)) sp->set_value(value);
00113   if (*value == '\0') return (sp != NULL);
00114 
00115   // Look for the parameter among int parameters.
00116   int intval;
00117   IntParam *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
00118                                      member_params->int_params);
00119   if (ip && ip->constraint_ok(constraint) &&
00120       sscanf(value, INT32FORMAT, &intval) == 1) ip->set_value(intval);
00121 
00122   // Look for the parameter among bool parameters.
00123   BoolParam *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
00124                                        member_params->bool_params);
00125   if (bp != NULL && bp->constraint_ok(constraint)) {
00126     if (*value == 'T' || *value == 't' ||
00127         *value == 'Y' || *value == 'y' || *value == '1') {
00128       bp->set_value(true);
00129     } else if (*value == 'F' || *value == 'f' ||
00130                 *value == 'N' || *value == 'n' || *value == '0') {
00131       bp->set_value(false);
00132     }
00133   }
00134 
00135   // Look for the parameter among double parameters.
00136   double doubleval;
00137   DoubleParam *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
00138                                            member_params->double_params);
00139   if (dp != NULL && dp->constraint_ok(constraint)) {
00140 #ifdef EMBEDDED
00141       doubleval = strtofloat(value);
00142 #else
00143       if (sscanf(value, "%lf", &doubleval) == 1)
00144 #endif
00145       dp->set_value(doubleval);
00146   }
00147   return (sp || ip || bp || dp);
00148 }
00149 
00150 bool ParamUtils::GetParamAsString(const char *name,
00151                                   const ParamsVectors* member_params,
00152                                   STRING *value) {
00153   // Look for the parameter among string parameters.
00154   StringParam *sp = FindParam<StringParam>(name, GlobalParams()->string_params,
00155                                            member_params->string_params);
00156   if (sp) {
00157     *value = sp->string();
00158     return true;
00159   }
00160   // Look for the parameter among int parameters.
00161   IntParam *ip = FindParam<IntParam>(name, GlobalParams()->int_params,
00162                                      member_params->int_params);
00163   if (ip) {
00164     char buf[128];
00165     snprintf(buf, sizeof(buf), "%d", inT32(*ip));
00166     *value = buf;
00167     return true;
00168   }
00169   // Look for the parameter among bool parameters.
00170   BoolParam *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params,
00171                                        member_params->bool_params);
00172   if (bp != NULL) {
00173     *value = BOOL8(*bp) ? "1": "0";
00174     return true;
00175   }
00176   // Look for the parameter among double parameters.
00177   DoubleParam *dp = FindParam<DoubleParam>(name, GlobalParams()->double_params,
00178                                            member_params->double_params);
00179   if (dp != NULL) {
00180     char buf[128];
00181     snprintf(buf, sizeof(buf), "%g", double(*dp));
00182     *value = buf;
00183     return true;
00184   }
00185   return false;
00186 }
00187 
00188 void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params) {
00189   int v, i;
00190   int num_iterations = (member_params == NULL) ? 1 : 2;
00191   for (v = 0; v < num_iterations; ++v) {
00192     const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
00193     for (i = 0; i < vec->int_params.size(); ++i) {
00194       fprintf(fp, "%s\t%d\n", vec->int_params[i]->name_str(),
00195               (inT32)(*vec->int_params[i]));
00196     }
00197     for (i = 0; i < vec->bool_params.size(); ++i) {
00198       fprintf(fp, "%s\t%d\n", vec->bool_params[i]->name_str(),
00199               (BOOL8)(*vec->bool_params[i]));
00200     }
00201     for (int i = 0; i < vec->string_params.size(); ++i) {
00202       fprintf(fp, "%s\t%s\n", vec->string_params[i]->name_str(),
00203               vec->string_params[i]->string());
00204     }
00205     for (int i = 0; i < vec->double_params.size(); ++i) {
00206       fprintf(fp, "%s\t%g\n", vec->double_params[i]->name_str(),
00207               (double)(*vec->double_params[i]));
00208     }
00209   }
00210 }
00211 
00212 }  // namespace tesseract