1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Configuration.hh for Blackbox - an X11 Window manager
3 // Copyright (c) 2002 - 2002 Ben Jansens (ben@orodu.net)
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
24 #include "../config.h"
25 #endif // HAVE_CONFIG_H
30 #endif // HAVE_STDLIB_H
33 #include "Configuration.hh"
40 bool Configuration::_initialized = False;
42 Configuration::Configuration(const string &file, bool autosave) {
53 Configuration::Configuration(bool autosave) {
63 Configuration::~Configuration() {
64 if (_database != NULL)
65 XrmDestroyDatabase(_database);
68 void Configuration::setFile(const string &file) {
72 void Configuration::setAutoSave(bool autosave) {
76 void Configuration::save() {
77 assert(_database != NULL);
78 XrmPutFileDatabase(_database, _file.c_str());
82 bool Configuration::load() {
83 if (_database != NULL)
84 XrmDestroyDatabase(_database);
86 if (NULL == (_database = XrmGetFileDatabase(_file.c_str())))
91 bool Configuration::merge(const string &file, bool overwrite) {
92 if (XrmCombineFileDatabase(file.c_str(), &_database, overwrite) == 0)
100 void Configuration::create() {
101 if (_database != NULL)
102 XrmDestroyDatabase(_database);
104 assert(NULL != (_database = XrmGetStringDatabase("")));
107 void Configuration::setValue(const string &rname, bool value) {
108 assert(_database != NULL);
110 const char *val = (value ? "True" : "False");
111 string rc_string = rname + ": " + val;
112 XrmPutLineResource(&_database, rc_string.c_str());
119 void Configuration::setValue(const string &rname, unsigned long value) {
120 assert(_database != NULL);
122 string rc_string = rname + ": " + itostring(value);
123 XrmPutLineResource(&_database, rc_string.c_str());
130 void Configuration::setValue(const string &rname, long value) {
131 assert(_database != NULL);
133 string rc_string = rname + ": " + itostring(value);
134 XrmPutLineResource(&_database, rc_string.c_str());
141 void Configuration::setValue(const string &rname, const char *value) {
142 assert(_database != NULL);
143 assert(value != NULL);
145 string rc_string = rname + ": " + value;
146 XrmPutLineResource(&_database, rc_string.c_str());
153 void Configuration::setValue(const string &rname, const string &value) {
154 assert(_database != NULL);
156 string rc_string = rname + ": " + value;
157 XrmPutLineResource(&_database, rc_string.c_str());
164 bool Configuration::getValue(const string &rname, bool &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)
174 string val = retvalue.addr;
175 if (val == "True" || val == "True")
182 bool Configuration::getValue(const string &rname, long &value) const {
183 assert(_database != NULL);
185 string rclass = createClassName(rname);
189 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
190 &rettype, &retvalue) || retvalue.addr == NULL)
193 value = strtol(retvalue.addr, &end, 10);
194 if (end == retvalue.addr)
199 bool Configuration::getValue(const string &rname, unsigned long &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)
210 value = strtoul(retvalue.addr, &end, 10);
211 if (end == retvalue.addr)
216 bool Configuration::getValue(const string &rname,
217 string &value) const {
218 assert(_database != NULL);
220 string rclass = createClassName(rname);
224 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
225 &rettype, &retvalue) || retvalue.addr == NULL)
227 value = retvalue.addr;
232 string Configuration::createClassName(const string &rname) const {
233 string rclass(rname);
235 string::iterator it = rclass.begin(), end = rclass.end();
239 if (it == end) break;
240 it = std::find(it, rclass.end(), '.');
241 if (it == end) break;
243 if (it == end) break;
249 char Configuration::toUpper(char c) const {
250 if (c >= 'a' && c <= 'z')
251 return c - 'a' + 'A';