Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Cfg Class Reference

#include <Cfg.h>

List of all members.

Static Public Member Functions

void RegistryToString (std::string &s, const Registry &r)
void StringToRegistry (Registry &r, const char *s)

Private Member Functions

 Cfg ()


Constructor & Destructor Documentation

Cfg::Cfg  )  [private]
 


Member Function Documentation

void Cfg::RegistryToString std::string &  s,
const Registry r
[static]
 

Definition at line 18 of file Cfg.cxx.

References Registry::Get(), Registry::Key(), and s().

Referenced by testRegistryToString().

00019 {
00020 //======================================================================
00021 // Convert the Registry r to a string dump
00022 //======================================================================
00023   Registry::RegistryKey rk = r.Key();
00024   const char* s;
00025 
00026   std::ostringstream ss;
00027   while ( (s=rk()) ) {
00028     // Try to extract the value for this key
00029     char        c   = 0;
00030     const char* cs  = 0;
00031     int         i   = 0;
00032     double      d   = 0;
00033     Registry    reg;
00034     bool isChar     = r.Get(s,c);
00035     bool isCharStar = r.Get(s,cs);
00036     bool isInt      = r.Get(s,i);
00037     bool isDouble   = r.Get(s,d);
00038     bool isRegistry = r.Get(s,reg);
00039 
00040     ss << s << "=";
00041     if      (isChar)     { ss << c;  }
00042     else if (isCharStar) { ss << "'"<<cs<<"'"; }
00043     else if (isInt)      { ss << i;  }
00044     else if (isDouble)   { ss << d; if (rint(d)==d) ss << ".0"; }
00045     else if (isRegistry) { ss << reg; } // Don't think this works...
00046     else assert("Unknown type or bad key in registry"==0);
00047     ss << " ";
00048   }
00049   x = ss.str();
00050 }

void Cfg::StringToRegistry Registry r,
const char *  s
[static]
 

Definition at line 54 of file Cfg.cxx.

References UtilString::atob(), UtilString::IsBool(), UtilString::IsFloat(), UtilString::IsInt(), len, s(), and Registry::Set().

Referenced by CfgConfigurable::Set(), testRegistryToString(), and testStringToRegistry().

00055 {
00056 //======================================================================
00057 // Convert the string s to a Registry. Format of s is:
00058 //
00059 // toggle=on pdq=true a=1 b=2.0 name=Mark longstring='some long text' 
00060 //
00061 //======================================================================
00062   // Parse string out into keys and values
00063   int len = strlen(s);
00064   char *scopy = new char[len+1];
00065   strcpy(scopy,s);
00066   
00067   // Skip ahead until we find an equal sign
00068   char* cKey   = 0;
00069   char* cEqual = 0;
00070   char* cValue = 0;
00071   char* cEnd   = 0;
00072   for (int i=0; i<len; ++i) {
00073     // Pick off the equal sign...
00074     if (scopy[i] == '=') {
00075       cEqual = scopy+i;
00076       *cEqual = '\0';
00077       
00078       // Step back to find the start of the key
00079       for (cKey=cEqual-1; *cKey==' ' && cKey>scopy; --cKey) *cKey = '\0';
00080       for (; *cKey!=' ' && *cKey!=',' && *cKey!='\0' && cKey>scopy; --cKey);
00081       for (; *cKey==' ' || *cKey=='\0'; ++cKey);
00082 
00083       // Step forward to get the start of the value
00084       for (cValue=cEqual+1; *cValue==' '&&*cValue!='\0'; ++cValue) {
00085         *cValue = '\0';
00086       }
00087       while (*cValue==' ') ++cValue;
00088       
00089       // Handle special case of long strings in single or double quotes
00090       bool isString = false;
00091       if (*cValue=='\'' || *cValue=='\"') {
00092         isString = true;
00093         char stringDelim = *cValue;
00094         ++cValue;
00095         for (cEnd = cValue; *cEnd!='\0' && *cEnd!=stringDelim; ++cEnd);
00096       }
00097       else {
00098         for (cEnd = cValue; *cEnd!='\0'&& *cEnd!=' '&& *cEnd!=','; ++cEnd);
00099       }
00100       *cEnd = '\0';
00101 
00102       // Convert value to correct data type
00103       if ( isString ) {
00104         r.Set(cKey,cValue);
00105       }
00106       else if (UtilString::IsInt(cValue)) {
00107         int i = atoi(cValue);
00108         r.Set(cKey,i);
00109       }
00110       else if (UtilString::IsFloat(cValue)) {
00111         double d = atof(cValue);
00112         r.Set(cKey,d);
00113       }
00114       else if (UtilString::IsBool(cValue)) {
00115         bool b = UtilString::atob(cValue);
00116         r.Set(cKey,b);
00117       }
00118       else {
00119         // Single character stored at char
00120         if (strlen(cValue)==1) { 
00121           char c = *cValue; 
00122           r.Set(cKey,c); 
00123         }
00124         else {
00125           // Longer string stored as string
00126           r.Set(cKey,cValue);
00127         }
00128       }
00129     }
00130     cEqual = cEnd+1;
00131   }
00132 
00133   delete [] scopy;
00134 }


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 15:00:52 2008 for loon by  doxygen 1.3.9.1