ccutil/varable.cpp

Go to the documentation of this file.
00001 
00020 #include          "mfcpch.h"     //precompiled headers
00021 #include          <stdio.h>
00022 #include          <string.h>
00023 #include          <stdlib.h>
00024 #include          "tprintf.h"
00025 //#include                                      "ipeerr.h"
00026 #include          "varable.h"
00027 #include          "scanutils.h"
00028 
00029 #define PLUS          '+'        //flag states
00030 #define MINUS         '-'
00031 #define EQUAL         '='
00032 
00033 CLISTIZE (INT_VARIABLE)
00034 CLISTIZE (BOOL_VARIABLE) CLISTIZE (STRING_VARIABLE) CLISTIZE (double_VARIABLE)
00035 INT_VAR_FROM
00036 INT_VARIABLE::copy;
00037 INT_VARIABLE_CLIST
00038 INT_VARIABLE::head;              //global definition
00039 INT_VAR_TO
00040 INT_VARIABLE::replace;
00041 BOOL_VAR_FROM
00042 BOOL_VARIABLE::copy;
00043 BOOL_VARIABLE_CLIST
00044 BOOL_VARIABLE::head;             //global definition
00045 BOOL_VAR_TO
00046 BOOL_VARIABLE::replace;
00047 STRING_VAR_FROM
00048 STRING_VARIABLE::copy;
00049 STRING_VARIABLE_CLIST
00050 STRING_VARIABLE::head;           //global definition
00051 STRING_VAR_TO
00052 STRING_VARIABLE::replace;
00053 double_VAR_FROM
00054 double_VARIABLE::copy;
00055 double_VARIABLE_CLIST
00056 double_VARIABLE::head;           //global definition
00057 double_VAR_TO
00058 double_VARIABLE::replace;
00059 
00064 INT_VAR_FROM::INT_VAR_FROM() {  //constructor
00065   INT_VARIABLE_C_IT start_it = &INT_VARIABLE::head;
00066   INT_VARIABLE_C_IT end_it = &INT_VARIABLE::head;
00067 
00068   if (!start_it.empty ()) {
00069     while (!end_it.at_last ())
00070       end_it.forward ();
00071                                  //move to copy
00072     list.assign_to_sublist (&start_it, &end_it);
00073   }
00074 }
00075 
00076 
00080 INT_VAR_TO::INT_VAR_TO() {  //constructor
00081   INT_VARIABLE_C_IT start_it = &INT_VARIABLE::copy.list;
00082   INT_VARIABLE_C_IT end_it = &INT_VARIABLE::copy.list;
00083 
00084   if (!start_it.empty ()) {
00085     while (!end_it.at_last ())
00086       end_it.forward ();
00087     INT_VARIABLE::head.assign_to_sublist (&start_it, &end_it);
00088   }
00089 }
00090 
00091 
00097 INT_VARIABLE::INT_VARIABLE(                     //constructor
00098                            INT32 v,             //the variable
00099                            const char *vname,   //of variable
00100                            const char *comment  //info on variable
00101                           ) {
00102   INT_VARIABLE_C_IT it = &head;  //list iterator
00103 
00104   //tprintf("Constructing %s\n",vname);
00105   set_value(v);  //set the value
00106   name = vname;                  //strings must be static
00107   info = comment;
00108   it.add_before_stay_put (this); //add it to stack
00109 }
00110 
00111 
00112 INT_VARIABLE::~INT_VARIABLE (    //constructor
00113 ) {
00114   INT_VARIABLE_C_IT it = &head;  //list iterator
00115 
00116   for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
00117     if (it.data () == this)
00118       it.extract ();
00119 }
00120 
00121 
00125 INT_VARIABLE_CLIST *INT_VARIABLE::get_head() {  //access to static
00126   return &head;
00127 }
00128 
00129 
00133 void INT_VARIABLE::print(          //print full list
00134                          FILE *fp  //file to print on
00135                         ) {
00136   INT_VARIABLE_C_IT it = &head;  //list iterator
00137   INT_VARIABLE *elt;             //current element
00138 
00139   if (fp == stdout) {
00140     tprintf ("#Variables of type INT_VARIABLE:\n");
00141     for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
00142       elt = it.data ();
00143       tprintf ("%s %d #%s\n", elt->name, elt->value, elt->info);
00144     }
00145   }
00146   else {
00147     fprintf (fp, "#Variables of type INT_VARIABLE:\n");
00148     for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
00149       elt = it.data ();
00150       fprintf (fp, "%s " INT32FORMAT " #%s\n", elt->name, elt->value,
00151         elt->info);
00152     }
00153   }
00154 }
00155 
00156 
00161 BOOL_VAR_FROM::BOOL_VAR_FROM() {  //constructor
00162   BOOL_VARIABLE_C_IT start_it = &BOOL_VARIABLE::head;
00163   BOOL_VARIABLE_C_IT end_it = &BOOL_VARIABLE::head;
00164 
00165   if (!start_it.empty ()) {
00166     while (!end_it.at_last ())
00167       end_it.forward ();
00168                                  //move to copy
00169     list.assign_to_sublist (&start_it, &end_it);
00170   }
00171 }
00172 
00173 
00177 BOOL_VAR_TO::BOOL_VAR_TO() {  //constructor
00178   BOOL_VARIABLE_C_IT start_it = &BOOL_VARIABLE::copy.list;
00179   BOOL_VARIABLE_C_IT end_it = &BOOL_VARIABLE::copy.list;
00180 
00181   if (!start_it.empty ()) {
00182     while (!end_it.at_last ())
00183       end_it.forward ();
00184     BOOL_VARIABLE::head.assign_to_sublist (&start_it, &end_it);
00185   }
00186 }
00187 
00188 
00194 BOOL_VARIABLE::BOOL_VARIABLE(                     //constructor
00195                              BOOL8 v,             //the variable
00196                              const char *vname,   //of variable
00197                              const char *comment  //info on variable
00198                             ) {
00199   BOOL_VARIABLE_C_IT it = &head; //list iterator
00200 
00201   //tprintf("Constructing %s\n",vname);
00202   set_value(v);  //set the value
00203   name = vname;                  //strings must be static
00204   info = comment;
00205   it.add_before_stay_put (this); //add it to stack
00206 
00207 }
00208 
00209 
00213 BOOL_VARIABLE::~BOOL_VARIABLE () {
00214   BOOL_VARIABLE_C_IT it = &head; //list iterator
00215 
00216   for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
00217     if (it.data () == this)
00218       it.extract ();
00219 }
00220 
00221 
00225 BOOL_VARIABLE_CLIST *BOOL_VARIABLE::get_head() {  //access to static
00226   return &head;
00227 }
00228 
00229 
00233 void BOOL_VARIABLE::print(          //print full list
00234                           FILE *fp  //file to print on
00235                          ) {
00236   BOOL_VARIABLE_C_IT it = &head; //list iterator
00237   BOOL_VARIABLE *elt;            //current element
00238 
00239   if (fp == stdout) {
00240     tprintf ("#Variables of type BOOL_VARIABLE:\n");
00241     for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
00242       elt = it.data ();
00243       tprintf ("%s %c #%s\n",
00244         elt->name, elt->value ? 'T' : 'F', elt->info);
00245     }
00246   }
00247   else {
00248     fprintf (fp, "#Variables of type BOOL_VARIABLE:\n");
00249     for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
00250       elt = it.data ();
00251       fprintf (fp, "%s %c #%s\n",
00252         elt->name, elt->value ? 'T' : 'F', elt->info);
00253     }
00254   }
00255 }
00256 
00257 
00262 STRING_VAR_FROM::STRING_VAR_FROM() {  //constructor
00263   STRING_VARIABLE_C_IT start_it = &STRING_VARIABLE::head;
00264   STRING_VARIABLE_C_IT end_it = &STRING_VARIABLE::head;
00265 
00266   if (!start_it.empty ()) {
00267     while (!end_it.at_last ())
00268       end_it.forward ();
00269                                  //move to copy
00270     list.assign_to_sublist (&start_it, &end_it);
00271   }
00272 }
00273 
00274 
00278 STRING_VAR_TO::STRING_VAR_TO() {  //constructor
00279   STRING_VARIABLE_C_IT start_it = &STRING_VARIABLE::copy.list;
00280   STRING_VARIABLE_C_IT end_it = &STRING_VARIABLE::copy.list;
00281 
00282   if (!start_it.empty ()) {
00283     while (!end_it.at_last ())
00284       end_it.forward ();
00285     STRING_VARIABLE::head.assign_to_sublist (&start_it, &end_it);
00286   }
00287 }
00288 
00289 
00295 STRING_VARIABLE::STRING_VARIABLE (
00296                                  //constructor
00297 const char *v,                   //the variable
00298 const char *vname,               //of variable
00299 const char *comment              //info on variable
00300 ):
00301 value(v) { 
00302                                  //list iterator
00303   STRING_VARIABLE_C_IT it = &head;
00304 
00305   //tprintf("Constructing %s\n",vname);
00306   name = vname;                  //strings must be static
00307   info = comment;
00308   it.add_before_stay_put (this); //add it to stack
00309 }
00310 
00311 
00317 STRING_VARIABLE::~STRING_VARIABLE (
00318 ) {
00319                                  //list iterator
00320   STRING_VARIABLE_C_IT it = &head;
00321 
00322   for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
00323     if (it.data () == this)
00324       it.extract ();
00325 }
00326 
00327 
00331 STRING_VARIABLE_CLIST *STRING_VARIABLE::get_head() {  //access to static
00332   return &head;
00333 }
00334 
00335 
00339 void STRING_VARIABLE::print(          //print full list
00340                             FILE *fp  //file to print on
00341                            ) {
00342                                  //list iterator
00343   STRING_VARIABLE_C_IT it = &head;
00344   STRING_VARIABLE *elt;          //current element
00345 
00346   if (fp == stdout) {
00347     tprintf ("#Variables of type STRING_VARIABLE:\n");
00348     for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
00349       elt = it.data ();
00350       tprintf ("%s #%s %s\n", elt->name, elt->value.string (), elt->info);
00351     }
00352   }
00353   else {
00354     fprintf (fp, "#Variables of type STRING_VARIABLE:\n");
00355     for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
00356       elt = it.data ();
00357       fprintf (fp, "%s #%s %s\n",
00358         elt->name, elt->value.string (), elt->info);
00359     }
00360   }
00361 }
00362 
00363 
00368 double_VAR_FROM::double_VAR_FROM() {  //constructor
00369   double_VARIABLE_C_IT start_it = &double_VARIABLE::head;
00370   double_VARIABLE_C_IT end_it = &double_VARIABLE::head;
00371 
00372   if (!start_it.empty ()) {
00373     while (!end_it.at_last ())
00374       end_it.forward ();
00375                                  //move to copy
00376     list.assign_to_sublist (&start_it, &end_it);
00377   }
00378 }
00379 
00380 
00384 double_VAR_TO::double_VAR_TO() {  //constructor
00385   double_VARIABLE_C_IT start_it = &double_VARIABLE::copy.list;
00386   double_VARIABLE_C_IT end_it = &double_VARIABLE::copy.list;
00387 
00388   if (!start_it.empty ()) {
00389     while (!end_it.at_last ())
00390       end_it.forward ();
00391     double_VARIABLE::head.assign_to_sublist (&start_it, &end_it);
00392   }
00393 }
00394 
00395 
00399 double_VARIABLE::double_VARIABLE(                     //constructor
00400                                  double v,            //the variable
00401                                  const char *vname,   //of variable
00402                                  const char *comment  //info on variable
00403                                 ) {
00404                                  //list iterator
00405   double_VARIABLE_C_IT it = &head;
00406 
00407   //tprintf("Constructing %s\n",vname);
00408   set_value(v);  //set the value
00409   name = vname;                  //strings must be static
00410   info = comment;
00411   it.add_before_stay_put (this); //add it to stack
00412 }
00413 
00414 
00418 double_VARIABLE::~double_VARIABLE () {
00419                                  //list iterator
00420   double_VARIABLE_C_IT it = &head;
00421 
00422   for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
00423     if (it.data () == this)
00424       it.extract ();
00425 }
00426 
00427 
00431 double_VARIABLE_CLIST *double_VARIABLE::get_head() {  //access to static
00432   return &head;
00433 }
00434 
00435 
00439 void double_VARIABLE::print(          //print full list
00440                             FILE *fp  //file to print on
00441                            ) {
00442                                  //list iterator
00443   double_VARIABLE_C_IT it = &head;
00444   double_VARIABLE *elt;          //current element
00445 
00446   if (fp == stdout) {
00447     tprintf ("#Variables of type double_VARIABLE:\n");
00448     for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
00449       elt = it.data ();
00450       tprintf ("%s %lg #%s\n", elt->name, elt->value, elt->info);
00451     }
00452   }
00453   else {
00454     fprintf (fp, "#Variables of type double_VARIABLE:\n");
00455     for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) {
00456       elt = it.data ();
00457       fprintf (fp, "%s %g #%s\n", elt->name, elt->value, elt->info);
00458     }
00459   }
00460 }
00461 
00462 
00471 DLLSYM BOOL8 read_variables_file(                  //read the file
00472                                  const char *file  //name to read
00473                                 ) {
00474   BOOL8 anyerr;                  //true if any error
00475   char flag;                     //file flag
00476   BOOL8 foundit;                 //found variable
00477   INT16 length;                  //length of line
00478   INT16 nameoffset;              //offset for real name
00479   char *valptr;                  //value field
00480   char *stringend;               //end of string value
00481   FILE *fp;                      //file pointer
00482   INT32 intval;                  //value from file
00483   double doubleval;              //value form file
00484                                  //iterators
00485   INT_VARIABLE_C_IT int_it = &INT_VARIABLE::head;
00486   BOOL_VARIABLE_C_IT BOOL_it = &BOOL_VARIABLE::head;
00487   STRING_VARIABLE_C_IT STRING_it = &STRING_VARIABLE::head;
00488   double_VARIABLE_C_IT double_it = &double_VARIABLE::head;
00489   char line[MAX_PATH];           //input line
00490 
00491   anyerr = FALSE;
00492   if (*file == PLUS) {
00493     flag = PLUS;                 //file has flag
00494     nameoffset = 1;
00495   }
00496   else if (*file == MINUS) {
00497     flag = MINUS;
00498     nameoffset = 1;
00499   }
00500   else {
00501     flag = EQUAL;
00502     nameoffset = 0;
00503   }
00504 
00505   fp = fopen (file + nameoffset, "r");
00506   if (fp == NULL) {
00507     tprintf ("read_variables_file:Can't open %s", file + nameoffset);
00508     return TRUE;                 //can't open it
00509   }
00510   while (fgets (line, MAX_PATH, fp)) {
00511     if (line[0] != '\n' && line[0] != '#') {
00512       length = strlen (line);
00513       if (line[length - 1] == '\n')
00514         line[length - 1] = '\0'; //cut newline
00515       for (valptr = line; *valptr && *valptr != ' ' && *valptr != '\t';
00516         valptr++);
00517       if (*valptr) {             //found blank
00518         *valptr = '\0';          //make name a string
00519         do
00520 
00521         valptr++;              //find end of blanks
00522         while (*valptr == ' ' || *valptr == '\t');
00523 
00524         if (*valptr && *valptr != '#') {
00525                                  //last char in string
00526           stringend = valptr + strlen (valptr) - 1;
00527           while (stringend != valptr) {
00528             while (stringend != valptr
00529               && (*stringend == ' ' || *stringend == '\t'))
00530               //cut trailing blanks
00531               stringend--;
00532             stringend[1] = '\0'; //terminate string
00533 
00534             while (stringend != valptr
00535               && (*stringend != ' ' && *stringend != '\t'
00536               || stringend[1] != '#'))
00537               stringend--;       //find word start
00538           }
00539         }
00540       }
00541       foundit = FALSE;
00542 
00543                                  //find name
00544       for (STRING_it.mark_cycle_pt (); !STRING_it.cycled_list () && strcmp (line, STRING_it.data ()->name); STRING_it.forward ());
00545                                  //found it
00546       if (!STRING_it.cycled_list ()) {
00547         foundit = TRUE;          //found the varaible
00548         if (*valptr == '\0' || *valptr == '#')
00549           STRING_it.data ()->set_value ((char *) NULL);
00550         //no value
00551         else
00552                                  //set its value
00553           STRING_it.data ()->set_value (valptr);
00554       }
00555 
00556       if (*valptr) {
00557                                  //find name
00558         for (int_it.mark_cycle_pt (); !int_it.cycled_list () && strcmp (line, int_it.data ()->name); int_it.forward ());
00559                                  //found it
00560         if (!int_it.cycled_list ()
00561         && sscanf (valptr, INT32FORMAT, &intval) == 1) {
00562           foundit = TRUE;        //found the varaible
00563                                  //set its value
00564           int_it.data ()->set_value (intval);
00565         }
00566 
00567                                  //find name
00568         for (BOOL_it.mark_cycle_pt (); !BOOL_it.cycled_list () && strcmp (line, BOOL_it.data ()->name); BOOL_it.forward ());
00569                                  //found it
00570         if (!BOOL_it.cycled_list ()) {
00571           if (*valptr == 'T' || *valptr == 't'
00572           || *valptr == 'Y' || *valptr == 'y' || *valptr == '1') {
00573             foundit = TRUE;
00574             if (flag == MINUS)
00575               BOOL_it.data ()->set_value (FALSE);
00576             //set to false
00577             else
00578               BOOL_it.data ()->set_value (TRUE);
00579             //set to true
00580           }
00581           else if (*valptr == 'F' || *valptr == 'f'
00582             || *valptr == 'N' || *valptr == 'n'
00583           || *valptr == '0') {
00584             foundit = TRUE;
00585             if (flag == EQUAL)
00586               BOOL_it.data ()->set_value (FALSE);
00587             //set to false
00588           }
00589         }
00590 
00591                                  //find name
00592         for (double_it.mark_cycle_pt (); !double_it.cycled_list () && strcmp (line, double_it.data ()->name); double_it.forward ());
00593                                  //found it
00594         
00595         #ifdef EMBEDDED
00596         if (!double_it.cycled_list ()) {
00597           doubleval = strtofloat(valptr);
00598         #else
00599         if (!double_it.cycled_list ()
00600         && sscanf (valptr, "%lf", &doubleval) == 1) {
00601         #endif
00602           foundit = TRUE;        //found the varaible
00603           double_it.data ()->set_value (doubleval);
00604           //set its value
00605         }
00606 
00607         if (!foundit) {
00608           anyerr = TRUE;         //had an error
00609           tprintf ("read_variables_file:variable not found: %s",
00610             line);
00611         }
00612       }
00613       else if (!foundit) {
00614         anyerr = TRUE;           //had an error
00615         tprintf ("read_variables_file:No value for variable %s", line);
00616       }
00617     }
00618   }
00619   fclose(fp);  //close file
00620   return anyerr;
00621 }
00622 
00623 
00627 DLLSYM void print_variables(          //print all vars
00628                             FILE *fp  //file to print on
00629                            ) {
00630   INT_VARIABLE::print(fp);  //print INTs
00631   BOOL_VARIABLE::print(fp);  //print BOOLs
00632   STRING_VARIABLE::print(fp);  //print STRINGs
00633   double_VARIABLE::print(fp);  //print doubles
00634 }

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