00001 #include "registry.h"
00002
00007 #include <glutm/glut.h>
00008
00009 #include <misc/string.h>
00010
00011 #include <cstdio>
00012 #include <fstream>
00013 using namespace std;
00014
00016
00017 GltRegistry::GltRegistry()
00018 {
00019 }
00020
00021 GltRegistry::~GltRegistry()
00022 {
00023 }
00024
00025 void
00026 GltRegistry::set(const GltFieldPtr &root)
00027 {
00028 _root = root;
00029 }
00030
00031 void
00032 GltRegistry::clear()
00033 {
00034 _root.clear();
00035 }
00036
00037 void
00038 GltRegistry::reset()
00039 {
00040 if (_root.get())
00041 _root->reset();
00042 }
00043
00044 bool
00045 GltRegistry::write(ostream &os) const
00046 {
00047 if (_root.get())
00048 return _root->write(os);
00049
00050 return false;
00051 }
00052
00053 bool
00054 GltRegistry::read(istream &is)
00055 {
00056 string file;
00057 readStream(is,file);
00058 return set(file);
00059 }
00060
00061 bool
00062 GltRegistry::write(const std::string &filename) const
00063 {
00064 ofstream os(filename.c_str());
00065 return write(os);
00066 }
00067
00068 bool
00069 GltRegistry::read(const std::string &filename)
00070 {
00071 ifstream is(filename.c_str());
00072 return read(is);
00073 }
00074
00075 bool
00076 GltRegistry::set(const std::string &name,const std::string &value)
00077 {
00078
00079
00080
00081 vector<string> path;
00082 stringSplit(path,name,".");
00083 if (path.size()==0)
00084 return false;
00085
00086
00087
00088 GltFields *pos = dynamic_cast<GltFields *>(_root.get());
00089
00090 if (!pos)
00091 return false;
00092
00093
00094
00095 if (pos->name()!=path[0])
00096 return false;
00097
00098
00099
00100 for (uint32 i=1; pos!=NULL && i<path.size(); i++)
00101 {
00102 bool found = false;
00103
00104
00105
00106 for (uint32 j=0; j<pos->_fields.size(); j++)
00107 if (pos->_fields[j]->name()==path[i])
00108 {
00109 found = true;
00110
00111 if (i+1==path.size())
00112 return pos->_fields[j]->set(value);
00113 else
00114 pos = dynamic_cast<GltFields *>(pos->_fields[j].get());
00115
00116 break;
00117 }
00118
00119
00120
00121 if (!found)
00122 return false;
00123 }
00124
00125 return false;
00126 }
00127
00128 bool
00129 GltRegistry::set(const std::string &settings)
00130 {
00131 bool ok = true;
00132
00133
00134
00135 vector<string> lines;
00136 stringSplit(lines,settings,"\n");
00137
00138
00139
00140 for (uint32 i=0; i<lines.size(); i++)
00141 {
00142
00143
00144 string::size_type j = lines[i].find_first_of(":");
00145
00146
00147
00148 if (j==string::npos)
00149 {
00150 ok = false;
00151 continue;
00152 }
00153
00154
00155
00156 string::size_type k = lines[i].find_first_not_of(" \t",j+1);
00157
00158
00159
00160 const string name = lines[i].substr(0,j);
00161 const string val = (k==string::npos ? "" : lines[i].substr(k));
00162
00163
00164
00165 set(name,val);
00166 }
00167
00168 return ok;
00169 }
00170
00171
00172
00173
00174
00175 #ifdef GLT_WIN32
00176
00177 bool
00178 GltRegistry::writeRegistry(const std::string &key) const
00179 {
00180 HKEY subKey = NULL;
00181 LONG res = RegCreateKeyEx(HKEY_CURRENT_USER,key.c_str(),0,NULL,REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&subKey,NULL);
00182
00183 if(res!=ERROR_SUCCESS)
00184 return false;
00185
00186 if (_root.get())
00187 _root->writeRegistry(subKey);
00188
00189 if (subKey)
00190 RegCloseKey(subKey);
00191
00192 return true;
00193 }
00194
00195 bool
00196 GltRegistry::readRegistry(const std::string &key)
00197 {
00198 HKEY subKey = NULL;
00199 LONG res = RegOpenKeyEx(HKEY_CURRENT_USER,key.c_str(),0,KEY_READ,&subKey);
00200
00201 if(res!=ERROR_SUCCESS)
00202 return false;
00203
00204 if (_root.get())
00205 _root->readRegistry(subKey);
00206
00207 if (subKey)
00208 RegCloseKey(subKey);
00209
00210 return true;
00211 }
00212
00213 #endif
00214