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 std::string &file, bool autosave) {
33 Configuration::Configuration(bool autosave) {
43 Configuration::~Configuration() {
44 if (_database != NULL)
45 XrmDestroyDatabase(_database);
48 void Configuration::setFile(const std::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 std::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 std::string &rname, bool value) {
88 assert(_database != NULL);
90 const char *val = (value ? "True" : "False");
91 std::string rc_string = rname + ": " + val;
92 XrmPutLineResource(&_database, rc_string.c_str());
99 void Configuration::setValue(const std::string &rname, unsigned long value) {
100 assert(_database != NULL);
102 std::string rc_string = rname + ": " + itostring(value);
103 XrmPutLineResource(&_database, rc_string.c_str());
110 void Configuration::setValue(const std::string &rname, long value) {
111 assert(_database != NULL);
113 std::string rc_string = rname + ": " + itostring(value);
114 XrmPutLineResource(&_database, rc_string.c_str());
121 void Configuration::setValue(const std::string &rname, const char *value) {
122 assert(_database != NULL);
123 assert(value != NULL);
125 std::string rc_string = rname + ": " + value;
126 XrmPutLineResource(&_database, rc_string.c_str());
133 void Configuration::setValue(const std::string &rname,
134 const std::string &value) {
135 assert(_database != NULL);
137 std::string rc_string = rname + ": " + value;
138 XrmPutLineResource(&_database, rc_string.c_str());
145 bool Configuration::getValue(const std::string &rname, bool &value) const {
146 assert(_database != NULL);
148 std::string rclass = createClassName(rname);
152 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
153 &rettype, &retvalue) || retvalue.addr == NULL)
155 std::string val = retvalue.addr;
156 if (val == "True" || val == "True")
163 bool Configuration::getValue(const std::string &rname, long &value) const {
164 assert(_database != NULL);
166 std::string rclass = createClassName(rname);
170 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
171 &rettype, &retvalue) || retvalue.addr == NULL)
174 value = strtol(retvalue.addr, &end, 10);
175 if (end == retvalue.addr)
180 bool Configuration::getValue(const std::string &rname, unsigned long &value) const {
181 assert(_database != NULL);
183 std::string rclass = createClassName(rname);
187 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
188 &rettype, &retvalue) || retvalue.addr == NULL)
191 value = strtoul(retvalue.addr, &end, 10);
192 if (end == retvalue.addr)
197 bool Configuration::getValue(const std::string &rname,
198 std::string &value) const {
199 assert(_database != NULL);
201 std::string rclass = createClassName(rname);
205 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
206 &rettype, &retvalue) || retvalue.addr == NULL)
208 value = retvalue.addr;
213 std::string Configuration::createClassName(const std::string &rname) const {
214 std::string rclass(rname);
216 std::string::iterator it = rclass.begin(), end = rclass.end();
220 if (it == end) break;
221 it = std::find(it, rclass.end(), '.');
222 if (it == end) break;
224 if (it == end) break;
230 char Configuration::toUpper(char c) const {
231 if (c >= 'a' && c <= 'z')
232 return c - 'a' + 'A';