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"
20 bool Configuration::_initialized = False;
22 Configuration::Configuration(const string &file, bool autosave) {
33 Configuration::Configuration(bool autosave) {
43 Configuration::~Configuration() {
44 if (_database != NULL)
45 XrmDestroyDatabase(_database);
48 void Configuration::setFile(const string &file) {
52 void Configuration::setAutoSave(bool autosave) {
56 void Configuration::save() {
57 assert(_database != NULL);
58 XrmPutFileDatabase(_database, _file.c_str());
62 bool Configuration::load() {
63 if (_database != NULL)
64 XrmDestroyDatabase(_database);
66 if (NULL == (_database = XrmGetFileDatabase(_file.c_str())))
71 bool Configuration::merge(const string &file, bool overwrite) {
72 if (XrmCombineFileDatabase(file.c_str(), &_database, overwrite) == 0)
80 void Configuration::create() {
81 if (_database != NULL)
82 XrmDestroyDatabase(_database);
84 assert(NULL != (_database = XrmGetStringDatabase("")));
87 void Configuration::setValue(const string &rname, bool value) {
88 assert(_database != NULL);
90 const char *val = (value ? "True" : "False");
91 string rc_string = rname + ": " + val;
92 XrmPutLineResource(&_database, rc_string.c_str());
99 void Configuration::setValue(const string &rname, unsigned long value) {
100 assert(_database != NULL);
102 string rc_string = rname + ": " + itostring(value);
103 XrmPutLineResource(&_database, rc_string.c_str());
110 void Configuration::setValue(const string &rname, long value) {
111 assert(_database != NULL);
113 string rc_string = rname + ": " + itostring(value);
114 XrmPutLineResource(&_database, rc_string.c_str());
121 void Configuration::setValue(const string &rname, const char *value) {
122 assert(_database != NULL);
123 assert(value != NULL);
125 string rc_string = rname + ": " + value;
126 XrmPutLineResource(&_database, rc_string.c_str());
133 void Configuration::setValue(const string &rname, const string &value) {
134 assert(_database != NULL);
136 string rc_string = rname + ": " + value;
137 XrmPutLineResource(&_database, rc_string.c_str());
144 bool Configuration::getValue(const string &rname, bool &value) const {
145 assert(_database != NULL);
147 string rclass = createClassName(rname);
151 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
152 &rettype, &retvalue) || retvalue.addr == NULL)
154 string val = retvalue.addr;
155 if (val == "True" || val == "True")
162 bool Configuration::getValue(const string &rname, long &value) const {
163 assert(_database != NULL);
165 string rclass = createClassName(rname);
169 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
170 &rettype, &retvalue) || retvalue.addr == NULL)
173 value = strtol(retvalue.addr, &end, 10);
174 if (end == retvalue.addr)
179 bool Configuration::getValue(const string &rname, unsigned long &value) const {
180 assert(_database != NULL);
182 string rclass = createClassName(rname);
186 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
187 &rettype, &retvalue) || retvalue.addr == NULL)
190 value = strtoul(retvalue.addr, &end, 10);
191 if (end == retvalue.addr)
196 bool Configuration::getValue(const string &rname,
197 string &value) const {
198 assert(_database != NULL);
200 string rclass = createClassName(rname);
204 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
205 &rettype, &retvalue) || retvalue.addr == NULL)
207 value = retvalue.addr;
212 string Configuration::createClassName(const string &rname) const {
213 string rclass(rname);
215 string::iterator it = rclass.begin(), end = rclass.end();
219 if (it == end) break;
220 it = std::find(it, rclass.end(), '.');
221 if (it == end) break;
223 if (it == end) break;
229 char Configuration::toUpper(char c) const {
230 if (c >= 'a' && c <= 'z')
231 return c - 'a' + 'A';