00001
00002
00003
00004
00005
00006
00008 #include <map>
00009 #include <sstream>
00010 #include <cassert>
00011 #include <cmath>
00012 #include "Registry/Registry.h"
00013 #include "Configurable/Cfg.h"
00014 #include "Util/UtilString.h"
00015
00016
00017
00018 void Cfg::RegistryToString(std::string& x, const Registry& r)
00019 {
00020
00021
00022
00023 Registry::RegistryKey rk = r.Key();
00024 const char* s;
00025
00026 std::ostringstream ss;
00027 while ( (s=rk()) ) {
00028
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; }
00046 else assert("Unknown type or bad key in registry"==0);
00047 ss << " ";
00048 }
00049 x = ss.str();
00050 }
00051
00052
00053
00054 void Cfg::StringToRegistry(Registry& r, const char* s)
00055 {
00056
00057
00058
00059
00060
00061
00062
00063 int len = strlen(s);
00064 char *scopy = new char[len+1];
00065 strcpy(scopy,s);
00066
00067
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
00074 if (scopy[i] == '=') {
00075 cEqual = scopy+i;
00076 *cEqual = '\0';
00077
00078
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
00084 for (cValue=cEqual+1; *cValue==' '&&*cValue!='\0'; ++cValue) {
00085 *cValue = '\0';
00086 }
00087 while (*cValue==' ') ++cValue;
00088
00089
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
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
00120 if (strlen(cValue)==1) {
00121 char c = *cValue;
00122 r.Set(cKey,c);
00123 }
00124 else {
00125
00126 r.Set(cKey,cValue);
00127 }
00128 }
00129 }
00130 cEqual = cEnd+1;
00131 }
00132
00133 delete [] scopy;
00134 }
00135