Page principale | Liste des namespaces | Hiérarchie des classes | Liste par ordre alphabétique | Liste des composants | Liste des fichiers | Composants | Déclarations

ConfigReader.h

Aller à la documentation de ce fichier.
00001 /*
00002  * TITLE:      ConfigReader.h
00003  *
00004  * PURPOSE:    This class can be used as a general configuration file reader
00005  *             ala .ini reading files. It will read file formats as follows:
00006  *             # - comment character. Anything afterwards is ignored
00007  *               - blank lines are ignored
00008  *             <identifier> <ignored stuff> = <data>  <data2> ... <datan>
00009  *
00010  *             strings after teh identifier (first space after text)
00011  *             is ignored anything after the equal sign, ignoring
00012  *             leading spaces is stored as a string. Any characters
00013  *             other than spaces (and tabs) are legal for the
00014  *             identifier as limited by isspace() function
00015  *
00016  *             Call the ReadFile function to parse the file. Call the
00017  *             FindString funciton with your identifier to retrieve
00018  *             hte string data on the right hand side of equal sign
00019  *             ignoring leading and trailing spaces.
00020  *
00021  *             Print function will output the data stored in the list
00022  *             for debugging purposes
00023  *
00024  *             All data structures allocated are cleaned up on destruction
00025  *
00026  * WRITTEN BY: Brett Browning
00027  * (c) Carnegie Mellon University, 2001
00028  *
00029  * REVISION HISTORY:
00030  * Dec 18th, 2001 - Created - BB
00031  *
00032  * TO Do List:
00033  * - Change to tree structure or STL supported template if warranted - BB
00034  */
00040 #ifndef __CONFIGREADER_H__
00041 #define __CONFIGREADER_H__
00042 
00043 #include <vector>
00044 
00045 #include <stdio.h>
00046 
00047 /* data types */
00048 #define CR_DOUBLE      0
00049 #define CR_INT         1
00050 #define CR_STRING      2
00051 #define CR_CHAR        3
00052 
00053 // macros to access data variables
00054 // xxVAR
00055 // D - double data type
00056 // I - int data type
00057 // C - char type
00058 // S - string type
00059 // V - vector data type (no V means single data
00060 #define DVAR(dmapptr)    (*((dmapptr)->data.ddata))
00061 #define VDVAR(dmapptr)   ((dmapptr)->data.ddata)
00062 
00063 #define IVAR(dmapptr)    (*((dmapptr)->data.idata))
00064 #define VIVAR(dmapptr)   ((dmapptr)->data.idata)
00065 
00066 #define CVAR(dmapptr)    (*((dmapptr)->data.cdata))
00067 #define SVAR(dmapptr)    ((dmapptr)->data.cdata)
00068 
00069 // macros for determining the size of an array
00070 // -> a good sanity check
00071 #define VARSIZE(dmapptr) ((dmapptr)->veccount)
00072 
00073 // data map structure that stores everything in it
00074 struct DataMap {
00075   bool cached;
00076   char *fname;
00077   int type;
00078   char *dataname;
00079   uint veccount;
00080   uint vecsize;
00081   union Data {
00082     double *ddata;
00083     int *idata;
00084     char *cdata;
00085   } data;
00086 
00087   DataMap  ();
00088   ~DataMap ();
00089 };
00090 
00091 typedef DataMap *PDataMap;
00092 
00093 
00094 class ConfigFile {
00095 private:
00096   struct Element {
00097     char *string;
00098     char *data;
00099     int str_size, data_size;
00100 
00101     Element  ();
00102     Element  (const Element& e);
00103     ~Element ();
00104   };
00105 
00106   std::vector<Element> elements;
00107 
00108   bool AddElement(char *str, char *data);
00109   void DeleteList(void);
00110   void Chomp(char *str);
00111   void ChopUp(char *str);
00112   
00113 public:
00114   ConfigFile(void);
00115   ~ConfigFile(void);
00116 
00117   bool ReadFile(char *fname);
00118 
00119   // return the next entry (non-delimiter) in the string
00120   char *GetNextEntry(char *str);
00121 
00122   // remove white space from start of string
00123   char *StripFront(char *start);
00124   bool StripTail(char *start);
00125 
00126   // add null character at first white space in string
00127   bool TerminateString(char *start);
00128 
00129   // tries to find the element string in the tree
00130   // if successful returns a pointer to the corresponding
00131   // data, if not returns null
00132   char *FindString(char *str);
00133   void Print(FILE *f);
00134   void Print(void) {
00135     Print(stdout);
00136   }
00137 
00138   int ReadVector(DataMap *dmap, char *str, int _type);
00139   int ReadString(DataMap *dmap, char *str);
00140 };
00141 
00142 class ConfigReader {
00143 private:
00144   std::vector<char *> filelist;
00145   std::vector<ConfigFile> filereaders;
00146 
00147   int FindFile(char *fname) {
00148     for(uint i=0; i<filelist.size(); i++)
00149       if (strcmp(filelist[i], fname) == 0) return i;
00150     return -1;
00151   }
00152 
00153   ConfigFile *FindReader(char *fname) {
00154     int i = FindFile(fname);
00155     if (i >= 0) return &filereaders[i];
00156     return NULL;
00157   }
00158 
00159   bool ReadFile(char *_fname);
00160 
00161   char *path;
00162 
00163 public:
00164   std::vector<PDataMap> datamap;
00165 
00166   ConfigReader(void);
00167   ~ConfigReader(void);
00168 
00169   // Reread the file list and update the mapping tables
00170   void UpdateFiles(void);
00171 
00172   // Add a new data mapping
00173   DataMap *FindDataMapping(char *_fname, char *_dataname, int _type);
00174 
00175   // Update a data mapping (also used when first added)
00176   bool ConfigReader::UpdateDataMap(PDataMap dmap);
00177 
00178   // Add a new data mapping and also sanity check the number of vectors
00179   DataMap *FindDataMapping(char *_fname, char *_dataname, int _type, 
00180                uint _count) {
00181     DataMap *rval = FindDataMapping(_fname, _dataname, _type);
00182     if (rval == NULL)
00183       return rval;
00184     if (rval->veccount < _count)
00185       return NULL;
00186     return rval;
00187   }
00188 
00189   void Print(FILE *f);
00190   void Print(void) { 
00191     Print(stdout); }
00192 
00193 };
00194 
00195 // The Global Reader
00196 extern ConfigReader configreader;
00197 
00198 // Quick Macros
00199 #define CR_DECLARE(varname) static PDataMap varname;
00200 
00201 #define CR_SETUP(filename,varname,type) \
00202   varname = \
00203     configreader.FindDataMapping(#filename, #varname, type); \
00204   if (!varname) abort();
00205 
00206 #define CR_SETUP_CR(cr,filename,varname,type) \
00207   varname = \
00208     (cr).FindDataMapping("##filename", "##varname",type); \
00209   if (!varname) abort();
00210 
00211 
00212 #endif /* __CONFIGREADER_H__ */
00213 

Généré le Mon Mar 1 01:29:41 2004 par doxygen 1.3.3