]>
Dogcows Code - chaz/openbox/blob - src/Configuration.cc
be0cd096e7aa7de15ffb04c0fc6e0e9cc88d906e
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
) {
43 if (! m_initialized
) {
49 Configuration::Configuration() {
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 void Configuration::create() {
88 if (m_database
!= NULL
)
89 XrmDestroyDatabase(m_database
);
91 assert(NULL
!= (m_database
= XrmGetStringDatabase("")));
94 void Configuration::setValue(const string
&rname
, bool value
) {
95 assert(m_database
!= NULL
);
97 const char *val
= (value
? "True" : "False");
98 string rc_string
= rname
+ ": " + val
;
99 XrmPutLineResource(&m_database
, rc_string
.c_str());
106 void Configuration::setValue(const string
&rname
, unsigned long value
) {
107 assert(m_database
!= NULL
);
109 string rc_string
= rname
+ ": " + itostring(value
);
110 XrmPutLineResource(&m_database
, rc_string
.c_str());
117 void Configuration::setValue(const string
&rname
, long value
) {
118 assert(m_database
!= NULL
);
120 string rc_string
= rname
+ ": " + itostring(value
);
121 XrmPutLineResource(&m_database
, rc_string
.c_str());
128 void Configuration::setValue(const string
&rname
, const char *value
) {
129 assert(m_database
!= NULL
);
130 assert(value
!= NULL
);
132 string rc_string
= rname
+ ": " + value
;
133 XrmPutLineResource(&m_database
, rc_string
.c_str());
140 void Configuration::setValue(const string
&rname
, const string
&value
) {
141 assert(m_database
!= NULL
);
143 string rc_string
= rname
+ ": " + value
;
144 XrmPutLineResource(&m_database
, rc_string
.c_str());
151 bool Configuration::getValue(const string
&rname
, bool &value
) const {
152 assert(m_database
!= NULL
);
154 string rclass
= createClassName(rname
);
158 if (0 == XrmGetResource(m_database
, rname
.c_str(), rclass
.c_str(),
159 &rettype
, &retvalue
) || retvalue
.addr
== NULL
)
161 string val
= retvalue
.addr
;
162 if (val
== "true" || val
== "True")
169 bool Configuration::getValue(const string
&rname
, long &value
) const {
170 assert(m_database
!= NULL
);
172 string rclass
= createClassName(rname
);
176 if (0 == XrmGetResource(m_database
, rname
.c_str(), rclass
.c_str(),
177 &rettype
, &retvalue
) || retvalue
.addr
== NULL
)
180 value
= strtol(retvalue
.addr
, &end
, 10);
181 if (end
== retvalue
.addr
)
186 bool Configuration::getValue(const string
&rname
, unsigned long &value
) const {
187 assert(m_database
!= NULL
);
189 string rclass
= createClassName(rname
);
193 if (0 == XrmGetResource(m_database
, rname
.c_str(), rclass
.c_str(),
194 &rettype
, &retvalue
) || retvalue
.addr
== NULL
)
197 value
= strtoul(retvalue
.addr
, &end
, 10);
198 if (end
== retvalue
.addr
)
203 bool Configuration::getValue(const string
&rname
,
204 string
&value
) const {
205 assert(m_database
!= NULL
);
207 string rclass
= createClassName(rname
);
211 if (0 == XrmGetResource(m_database
, rname
.c_str(), rclass
.c_str(),
212 &rettype
, &retvalue
) || retvalue
.addr
== NULL
)
214 value
= retvalue
.addr
;
219 string
Configuration::createClassName(const string
&rname
) const {
220 string
rclass(rname
);
222 string::iterator it
= rclass
.begin(), end
= rclass
.end();
226 if (it
== end
) break;
227 it
= std::find(it
, rclass
.end(), '.');
228 if (it
== end
) break;
230 if (it
== end
) break;
236 char Configuration::toUpper(char c
) const {
237 if (c
>= 'a' && c
<= 'z')
238 return c
- 'a' + 'A';
This page took 0.049901 seconds and 4 git commands to generate.