cutil/variables.cpp

Go to the documentation of this file.
00001 
00020 /*----------------------------------------------------------------------
00021           I n c l u d e s
00022 ----------------------------------------------------------------------*/
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 
00026 #include "variables.h"
00027 #include "callcpp.h"
00028 #include "listio.h"
00029 #include "globals.h"
00030 #include "scanutils.h"
00031 
00032 /*----------------------------------------------------------------------
00033           V a r i a b l e s
00034 ----------------------------------------------------------------------*/
00035 static LIST variable_list = NIL;
00036 //extern char                                   *demodir;
00037 
00038 VALUE dummy;
00039 
00040 /*----------------------------------------------------------------------
00041             Macros
00042 ----------------------------------------------------------------------*/
00048 #define scan_int(stripped,integer)             \
00049 if (stripped [2] == 'x')                     \
00050    sscanf (&stripped[3], "%x", &integer);    \
00051 else                                         \
00052    sscanf (&stripped[1], "%d", &integer)
00053 
00054 /*----------------------------------------------------------------------
00055           F u n c t i o n s
00056 ----------------------------------------------------------------------*/
00061 void free_variables() {
00062   destroy_nodes(variable_list, free);
00063   variable_list = NIL;
00064 }
00065 
00076 void add_ptr_variable(void *address,
00077                   const char *string,
00078                   VALUE default_value,
00079                   variables_io reader,
00080                   variables_io writer) {
00081   VARIABLE *this_var;
00082   this_var = (VARIABLE *) malloc (sizeof (VARIABLE));
00083 
00084   this_var->address = address;
00085   this_var->string = string;
00086   this_var->default_value = default_value;
00087   this_var->type_reader = reader;
00088   this_var->type_writer = writer;
00089 
00090   *((void **) this_var->address) = default_value.ptr_part;
00091   variable_list = push (variable_list, this_var);
00092 }
00093 
00094 
00100 void add_32bit_variable(void *address,
00101                         const char *string,
00102                         VALUE default_value,
00103                         variables_io reader,
00104                         variables_io writer) {
00105   VARIABLE *this_var;
00106   this_var = (VARIABLE *) malloc (sizeof (VARIABLE));
00107 
00108   this_var->address = address;
00109   this_var->string = string;
00110   this_var->default_value = default_value;
00111   this_var->type_reader = reader;
00112   this_var->type_writer = writer;
00113 
00114   *((int *) this_var->address) = default_value.int_part;
00115   variable_list = push (variable_list, this_var);
00116 }
00117 
00118 
00122 void float_read(VARIABLE *variable, char *string) {
00123   float f;
00124 
00125   #ifdef EMBEDDED
00126     // We have no sscanf with float functionality here
00127     *((float *) variable->address) = strtofloat(strip_line (string));
00128   #else
00129     sscanf (strip_line (string), "%f", &f);
00130     *((float *) variable->address) = f;
00131   #endif
00132 }
00133 
00134 
00138 void float_write(VARIABLE *variable, char *string) {
00139   float *f;
00140   f = (float *) variable->address;
00141   sprintf (string, "%s\t%4.2f", variable->string, *f);
00142 }
00143 
00144 
00148 void int_read(VARIABLE *variable, char *string) {
00149   char *stripped;
00150   int integer;
00151 
00152   stripped = strip_line (string);
00153   /* Add the value */
00154   if (stripped[0] == '+') {
00155     scan_int(stripped, integer);
00156     *((int *) variable->address) += integer;
00157   }
00158   else if (stripped[0] == '|') {
00159     scan_int(stripped, integer);
00160     *((int *) variable->address) = integer | *((int *) variable->address);
00161   }                              /* Subtract the value */
00162   else if (stripped[0] == '_') {
00163     scan_int(stripped, integer);
00164     *((int *) variable->address) = (~integer) &
00165       *((int *) variable->address);
00166   }
00167   else {
00168                                  /* Set the value */
00169     if (stripped[1] == 'x') {
00170       sscanf (&stripped[2], "%x", &integer);
00171     }
00172     else {
00173       sscanf (stripped, "%d", &integer);
00174     }
00175     *((int *) variable->address) = integer;
00176   }
00177 }
00178 
00179 
00183 void int_write(VARIABLE *variable, char *string) { 
00184   sprintf (string, "%s\t%d", variable->string, *((int *) variable->address));
00185 }
00186 
00187 
00197 void read_variables(const char *filename) { 
00198   int x = 0;
00199   char *this_string;
00200   LIST var_strings;
00201   char name[1024];
00202   FILE *fp;
00203   VARIABLE *this_var;
00204   /* Read the strings */
00205   if (filename == NULL || filename[0] == '\0')
00206     return;
00207 
00208   strcpy(name, demodir);
00209   strcat (name, "tessdata/tessconfigs/");
00210   strcat(name, filename);
00211   if ((fp = fopen (name, "r")) == NULL)
00212     strcpy(name, filename);
00213   else
00214     fclose(fp);
00215   var_strings = read_list (name);
00216   iterate(var_strings) {
00217     /* Get the name string */
00218     this_string = (char *) first (var_strings);
00219     if (this_string[0] != '#') {
00220       for (x = 0;
00221         x < strlen (this_string) && this_string[x] != ' '
00222         && this_string[x] != '\t'; x++);
00223       this_string[x] = '\0';
00224       /* Find variable record */
00225       this_var = (VARIABLE *) first (search (variable_list, this_string,
00226         same_var_name));
00227       if (this_var == 0) {
00228 #ifndef TEXT_VERBOSE
00229         cprintf ("error: Could not find variable '%s'\n", this_string);
00230         exit (1);                //?err_exit ();
00231 #else
00232         cprintf ("error: Could not find variable '%s', skipping\n", this_string);
00233       continue;
00234 #endif
00235       }
00236       /* Read the value */
00237       this_string[x] = '\t';
00238       (*(this_var->type_reader)) (this_var, this_string);
00239     }
00240   }
00241 }
00242 
00243 
00247 int same_var_name(void *item1,    //VARIABLE *variable,
00248                   void *item2) {  //char     *string)
00249   VARIABLE *variable;
00250   char *string;
00251 
00252   variable = (VARIABLE *) item1;
00253   string = (char *) item2;
00254 
00255   if (strcmp (variable->string, string))
00256     return (FALSE);
00257   else
00258     return (TRUE);
00259 }
00260 
00261 
00265 void string_read(VARIABLE *variable, char *string) { 
00266   char *value;
00267 
00268   value = strsave (strip_line (string));
00269   *((char **) variable->address) = value;
00270 }
00271 
00272 
00276 void string_write(VARIABLE *variable, char *string) { 
00277   sprintf (string, "%s\t%s", variable->string,
00278     *((char **) variable->address));
00279 }
00280 
00281 
00286 char *strip_line(char *string) { 
00287   int x;
00288   int y;
00289 
00290   /* Skip over name */
00291   for (x = 0;
00292     x < strlen (string) && string[x] != '\t' && string[x] != ' '; x++);
00293   /* Skip over whitespace */
00294   while (x < strlen (string) && (string[x] == '\t' || string[x] == ' '))
00295     x++;
00296   /* Strip trailing whitespace */
00297   for (y = strlen (string);
00298     y >= 0 && (string[y - 1] == '\t' || string[y - 1] == ' '); y--)
00299   string[y] = '\0';
00300   /* Return result */
00301   return (&string[x]);
00302 }

Generated on Wed Feb 28 19:49:10 2007 for Tesseract by  doxygen 1.5.1