TDbiCfg.cxx

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////
00002 // $Id: TDbiCfg.cxx,v 1.1 2011/01/18 05:49:19 finch Exp $
00003 //
00004 // Some utility functions for working with configurations 
00005 //
00006 // messier@indiana.edu
00007 ////////////////////////////////////////////////////////////////////////
00008 #include <map>
00009 #include <sstream>
00010 #include <cassert>
00011 #include <cstdlib>
00012 #include <cmath>
00013 #include "TDbiRegistry.hxx"
00014 #include "TDbiCfg.hxx"
00015 #include "UtilString.hxx"
00016 
00017 //......................................................................
00018 
00019 void TDbiCfg::TDbiRegistryToString(std::string& x, const TDbiRegistry& r) 
00020 {
00021 //======================================================================
00022 // Convert the TDbiRegistry r to a string dump
00023 //======================================================================
00024   TDbiRegistry::TDbiRegistryKey rk = r.Key();
00025   const char* s;
00026 
00027   std::ostringstream ss;
00028   while ( (s=rk()) ) {
00029     // Try to extract the value for this key
00030     char        c   = 0;
00031     const char* cs  = 0;
00032     int         i   = 0;
00033     double      d   = 0;
00034     TDbiRegistry    reg;
00035     bool isChar     = r.Get(s,c);
00036     bool isCharStar = r.Get(s,cs);
00037     bool isInt      = r.Get(s,i);
00038     bool isDouble   = r.Get(s,d);
00039     bool isTDbiRegistry = r.Get(s,reg);
00040 
00041     ss << s << "=";
00042     if      (isChar)     { ss << c;  }
00043     else if (isCharStar) { ss << "'"<<cs<<"'"; }
00044     else if (isInt)      { ss << i;  }
00045     else if (isDouble)   { ss << d; if (rint(d)==d) ss << ".0"; }
00046     else if (isTDbiRegistry) { ss << reg; } // Don't think this works...
00047     else assert("Unknown type or bad key in registry"==0);
00048     ss << " ";
00049   }
00050   x = ss.str();
00051 }
00052 
00053 //......................................................................
00054 
00055 void TDbiCfg::StringToTDbiRegistry(TDbiRegistry& r, const char* s) 
00056 {
00057 //======================================================================
00058 // Convert the string s to a TDbiRegistry. Format of s is:
00059 //
00060 // toggle=on pdq=true a=1 b=2.0 name=Mark longstring='some long text' 
00061 //
00062 //======================================================================
00063   // Parse string out into keys and values
00064   int len = strlen(s);
00065   char *scopy = new char[len+1];
00066   strcpy(scopy,s);
00067   
00068   // Skip ahead until we find an equal sign
00069   char* cKey   = 0;
00070   char* cEqual = 0;
00071   char* cValue = 0;
00072   char* cEnd   = 0;
00073   for (int i=0; i<len; ++i) {
00074     // Pick off the equal sign...
00075     if (scopy[i] == '=') {
00076       cEqual = scopy+i;
00077       *cEqual = '\0';
00078       
00079       // Step back to find the start of the key
00080       for (cKey=cEqual-1; *cKey==' ' && cKey>scopy; --cKey) *cKey = '\0';
00081       for (; *cKey!=' ' && *cKey!=',' && *cKey!='\0' && cKey>scopy; --cKey);
00082       for (; *cKey==' ' || *cKey=='\0'; ++cKey);
00083 
00084       // Step forward to get the start of the value
00085       for (cValue=cEqual+1; *cValue==' '&&*cValue!='\0'; ++cValue) {
00086         *cValue = '\0';
00087       }
00088       while (*cValue==' ') ++cValue;
00089       
00090       // Handle special case of long strings in single or double quotes
00091       bool isString = false;
00092       if (*cValue=='\'' || *cValue=='\"') {
00093         isString = true;
00094         char stringDelim = *cValue;
00095         ++cValue;
00096         for (cEnd = cValue; *cEnd!='\0' && *cEnd!=stringDelim; ++cEnd);
00097       }
00098       else {
00099         for (cEnd = cValue; *cEnd!='\0'&& *cEnd!=' '&& *cEnd!=','; ++cEnd);
00100       }
00101       *cEnd = '\0';
00102 
00103       // Convert value to correct data type
00104       if ( isString ) {
00105         r.Set(cKey,cValue);
00106       }
00107       else if (UtilString::IsInt(cValue)) {
00108         int i = atoi(cValue);
00109         r.Set(cKey,i);
00110       }
00111       else if (UtilString::IsFloat(cValue)) {
00112         double d = atof(cValue);
00113         r.Set(cKey,d);
00114       }
00115       else if (UtilString::IsBool(cValue)) {
00116         bool b = UtilString::atob(cValue);
00117         r.Set(cKey,b);
00118       }
00119       else {
00120         // Single character stored at char
00121         if (strlen(cValue)==1) { 
00122           char c = *cValue; 
00123           r.Set(cKey,c); 
00124         }
00125         else {
00126           // Longer string stored as string
00127           r.Set(cKey,cValue);
00128         }
00129       }
00130     }
00131     cEqual = cEnd+1;
00132   }
00133 
00134   delete [] scopy;
00135 }
00136 
00137 ////////////////////////////////////////////////////////////////////////

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1