1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
5 #endif // HAVE_CONFIG_H
10 #endif // HAVE_STDLIB_H
13 #include "configuration.hh"
22 bool Configuration::_initialized = False;
24 Configuration::Configuration(const string &file, bool autosave) {
35 Configuration::Configuration(bool autosave) {
45 Configuration::~Configuration() {
46 if (_database != NULL)
47 XrmDestroyDatabase(_database);
50 void Configuration::setFile(const string &file) {
54 void Configuration::setAutoSave(bool autosave) {
58 void Configuration::save() {
59 assert(_database != NULL);
60 XrmPutFileDatabase(_database, _file.c_str());
64 bool Configuration::load() {
65 if (_database != NULL)
66 XrmDestroyDatabase(_database);
68 if (NULL == (_database = XrmGetFileDatabase(_file.c_str())))
73 bool Configuration::merge(const string &file, bool overwrite) {
74 if (XrmCombineFileDatabase(file.c_str(), &_database, overwrite) == 0)
82 void Configuration::create() {
83 if (_database != NULL)
84 XrmDestroyDatabase(_database);
86 assert(NULL != (_database = XrmGetStringDatabase("")));
89 void Configuration::setValue(const string &rname, bool value) {
90 assert(_database != NULL);
92 const char *val = (value ? "True" : "False");
93 string rc_string = rname + ": " + val;
94 XrmPutLineResource(&_database, rc_string.c_str());
101 void Configuration::setValue(const string &rname, unsigned long value) {
102 assert(_database != NULL);
104 string rc_string = rname + ": " + itostring(value);
105 XrmPutLineResource(&_database, rc_string.c_str());
112 void Configuration::setValue(const string &rname, long value) {
113 assert(_database != NULL);
115 string rc_string = rname + ": " + itostring(value);
116 XrmPutLineResource(&_database, rc_string.c_str());
123 void Configuration::setValue(const string &rname, const char *value) {
124 assert(_database != NULL);
125 assert(value != NULL);
127 string rc_string = rname + ": " + value;
128 XrmPutLineResource(&_database, rc_string.c_str());
135 void Configuration::setValue(const string &rname, const string &value) {
136 assert(_database != NULL);
138 string rc_string = rname + ": " + value;
139 XrmPutLineResource(&_database, rc_string.c_str());
146 bool Configuration::getValue(const string &rname, bool &value) const {
147 assert(_database != NULL);
149 string rclass = createClassName(rname);
153 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
154 &rettype, &retvalue) || retvalue.addr == NULL)
156 string val = retvalue.addr;
157 if (val == "True" || val == "True")
164 bool Configuration::getValue(const string &rname, long &value) const {
165 assert(_database != NULL);
167 string rclass = createClassName(rname);
171 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
172 &rettype, &retvalue) || retvalue.addr == NULL)
175 value = strtol(retvalue.addr, &end, 10);
176 if (end == retvalue.addr)
181 bool Configuration::getValue(const string &rname, unsigned long &value) const {
182 assert(_database != NULL);
184 string rclass = createClassName(rname);
188 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
189 &rettype, &retvalue) || retvalue.addr == NULL)
192 value = strtoul(retvalue.addr, &end, 10);
193 if (end == retvalue.addr)
198 bool Configuration::getValue(const string &rname,
199 string &value) const {
200 assert(_database != NULL);
202 string rclass = createClassName(rname);
206 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
207 &rettype, &retvalue) || retvalue.addr == NULL)
209 value = retvalue.addr;
214 string Configuration::createClassName(const string &rname) const {
215 string rclass(rname);
217 string::iterator it = rclass.begin(), end = rclass.end();
221 if (it == end) break;
222 it = std::find(it, rclass.end(), '.');
223 if (it == end) break;
225 if (it == end) break;
231 char Configuration::toUpper(char c) const {
232 if (c >= 'a' && c <= 'z')
233 return c - 'a' + 'A';