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.
23 #include "../config.h"
25 #include "Configuration.hh"
32 #endif // HAVE_STDLIB_H
36 bool Configuration::m_initialized = False;
38 Configuration::Configuration(const string &file, bool autosave) {
42 m_autosave = autosave;
43 if (! m_initialized) {
49 Configuration::Configuration(bool autosave) {
52 m_autosave = autosave;
53 if (! m_initialized) {
59 Configuration::~Configuration() {
60 if (m_database != NULL)
61 XrmDestroyDatabase(m_database);
64 void Configuration::setFile(const string &file) {
68 void Configuration::setAutoSave(bool autosave) {
69 m_autosave = autosave;
72 void Configuration::save() {
73 assert(m_database != NULL);
74 XrmPutFileDatabase(m_database, m_file.c_str());
78 bool Configuration::load() {
79 if (m_database != NULL)
80 XrmDestroyDatabase(m_database);
82 if (NULL == (m_database = XrmGetFileDatabase(m_file.c_str())))
87 bool Configuration::merge(const string &file, bool overwrite) {
88 if (XrmCombineFileDatabase(file.c_str(), &m_database, overwrite) == 0)
96 void Configuration::create() {
97 if (m_database != NULL)
98 XrmDestroyDatabase(m_database);
100 assert(NULL != (m_database = XrmGetStringDatabase("")));
103 void Configuration::setValue(const string &rname, bool value) {
104 assert(m_database != NULL);
106 const char *val = (value ? "True" : "False");
107 string rc_string = rname + ": " + val;
108 XrmPutLineResource(&m_database, rc_string.c_str());
115 void Configuration::setValue(const string &rname, unsigned long value) {
116 assert(m_database != NULL);
118 string rc_string = rname + ": " + itostring(value);
119 XrmPutLineResource(&m_database, rc_string.c_str());
126 void Configuration::setValue(const string &rname, long value) {
127 assert(m_database != NULL);
129 string rc_string = rname + ": " + itostring(value);
130 XrmPutLineResource(&m_database, rc_string.c_str());
137 void Configuration::setValue(const string &rname, const char *value) {
138 assert(m_database != NULL);
139 assert(value != NULL);
141 string rc_string = rname + ": " + value;
142 XrmPutLineResource(&m_database, rc_string.c_str());
149 void Configuration::setValue(const string &rname, const string &value) {
150 assert(m_database != NULL);
152 string rc_string = rname + ": " + value;
153 XrmPutLineResource(&m_database, rc_string.c_str());
160 bool Configuration::getValue(const string &rname, bool &value) const {
161 assert(m_database != NULL);
163 string rclass = createClassName(rname);
167 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
168 &rettype, &retvalue) || retvalue.addr == NULL)
170 string val = retvalue.addr;
171 if (val == "True" || val == "True")
178 bool Configuration::getValue(const string &rname, long &value) const {
179 assert(m_database != NULL);
181 string rclass = createClassName(rname);
185 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
186 &rettype, &retvalue) || retvalue.addr == NULL)
189 value = strtol(retvalue.addr, &end, 10);
190 if (end == retvalue.addr)
195 bool Configuration::getValue(const string &rname, unsigned long &value) const {
196 assert(m_database != NULL);
198 string rclass = createClassName(rname);
202 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
203 &rettype, &retvalue) || retvalue.addr == NULL)
206 value = strtoul(retvalue.addr, &end, 10);
207 if (end == retvalue.addr)
212 bool Configuration::getValue(const string &rname,
213 string &value) const {
214 assert(m_database != NULL);
216 string rclass = createClassName(rname);
220 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
221 &rettype, &retvalue) || retvalue.addr == NULL)
223 value = retvalue.addr;
228 string Configuration::createClassName(const string &rname) const {
229 string rclass(rname);
231 string::iterator it = rclass.begin(), end = rclass.end();
235 if (it == end) break;
236 it = std::find(it, rclass.end(), '.');
237 if (it == end) break;
239 if (it == end) break;
245 char Configuration::toUpper(char c) const {
246 if (c >= 'a' && c <= 'z')
247 return c - 'a' + 'A';