Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
params.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 * File: params.cpp
3 * Description: Initialization and setting of Tesseract parameters.
4 * Author: Ray Smith
5 * Created: Fri Feb 22 16:22:34 GMT 1991
6 *
7 * (C) Copyright 1991, Hewlett-Packard Ltd.
8 ** Licensed under the Apache License, Version 2.0 (the "License");
9 ** you may not use this file except in compliance with the License.
10 ** You may obtain a copy of the License at
11 ** http://www.apache.org/licenses/LICENSE-2.0
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 *
18 **********************************************************************/
19
20 #include "mfcpch.h" //precompiled headers
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "genericvector.h"
27 #include "scanutils.h"
28 #include "tprintf.h"
29 #include "params.h"
30
31 #define PLUS '+' //flag states
32 #define MINUS '-'
33 #define EQUAL '='
34
35 tesseract::ParamsVectors *GlobalParams() {
36 static tesseract::ParamsVectors *global_params =
37 new tesseract::ParamsVectors();
38 return global_params;
39 }
40
41 namespace tesseract {
42
43 bool ParamUtils::ReadParamsFile(const char *file,
44 SetParamConstraint constraint,
45 ParamsVectors *member_params) {
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 }
70
71 bool ParamUtils::ReadParamsFromFp(FILE *fp, inT64 end_offset,
72 SetParamConstraint constraint,
73 ParamsVectors *member_params) {
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 }
105
106 bool ParamUtils::SetParam(const char *name, const char* value,
107 SetParamConstraint constraint,
108 ParamsVectors *member_params) {
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 }
149
150 bool ParamUtils::GetParamAsString(const char *name,
151 const ParamsVectors* member_params,
152 STRING *value) {
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 }
187
188 void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params) {
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 }
211
212 } // namespace tesseract