Cfg Class Reference

#include <Cfg.h>

List of all members.

Static Public Member Functions

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

Private Member Functions

 Cfg ()

Detailed Description

Definition at line 18 of file Cfg.h.


Constructor & Destructor Documentation

Cfg::Cfg (  )  [private]

Member Function Documentation

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

Definition at line 20 of file Cfg.cxx.

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

Referenced by testRegistryToString().

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

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

Definition at line 56 of file Cfg.cxx.

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

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

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


The documentation for this class was generated from the following files:

Generated on 22 Nov 2017 for loon by  doxygen 1.6.1