]>
Dogcows Code - chaz/openbox/blob - src/Configuration.cc
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"
30 #endif // HAVE_STDLIB_H
34 bool Configuration::m_initialized
= false;
36 Configuration::Configuration(const string
&file
) {
41 if (! m_initialized
) {
47 Configuration::Configuration() {
51 if (! m_initialized
) {
57 Configuration::~Configuration() {
58 if (m_database
!= NULL
)
59 XrmDestroyDatabase(m_database
);
62 void Configuration::setFile(const string
&file
) {
66 void Configuration::setAutoSave(bool autosave
) {
67 m_autosave
= autosave
;
70 void Configuration::save() {
71 assert(m_database
!= NULL
);
72 XrmPutFileDatabase(m_database
, m_file
.c_str());
76 bool Configuration::load() {
77 if (m_database
!= NULL
)
78 XrmDestroyDatabase(m_database
);
80 if (NULL
== (m_database
= XrmGetFileDatabase(m_file
.c_str())))
85 void Configuration::create() {
86 if (m_database
!= NULL
)
87 XrmDestroyDatabase(m_database
);
89 assert(NULL
!= (m_database
= XrmGetStringDatabase("")));
92 void Configuration::setValue(const string
&rname
, bool value
) {
93 assert(m_database
!= NULL
);
95 const char *val
= (value
? "True" : "False");
96 string rc_string
= rname
+ ": " + val
;
97 XrmPutLineResource(&m_database
, rc_string
.c_str());
104 void Configuration::setValue(const string
&rname
, unsigned long value
) {
105 assert(m_database
!= NULL
);
107 string rc_string
= rname
+ ": " + itostring(value
);
108 XrmPutLineResource(&m_database
, rc_string
.c_str());
115 void Configuration::setValue(const string
&rname
, 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
, const char *value
) {
127 assert(m_database
!= NULL
);
128 assert(value
!= NULL
);
130 string rc_string
= rname
+ ": " + value
;
131 XrmPutLineResource(&m_database
, rc_string
.c_str());
138 void Configuration::setValue(const string
&rname
, const string
&value
) {
139 assert(m_database
!= NULL
);
141 string rc_string
= rname
+ ": " + value
;
142 XrmPutLineResource(&m_database
, rc_string
.c_str());
149 bool Configuration::getValue(const string
&rname
, bool &value
) const {
150 assert(m_database
!= NULL
);
152 string rclass
= createClassName(rname
);
156 if (0 == XrmGetResource(m_database
, rname
.c_str(), rclass
.c_str(),
157 &rettype
, &retvalue
) || retvalue
.addr
== NULL
)
159 string val
= retvalue
.addr
;
160 if (val
== "true" || val
== "True")
167 bool Configuration::getValue(const string
&rname
, long &value
) const {
168 assert(m_database
!= NULL
);
170 string rclass
= createClassName(rname
);
174 if (0 == XrmGetResource(m_database
, rname
.c_str(), rclass
.c_str(),
175 &rettype
, &retvalue
) || retvalue
.addr
== NULL
)
178 value
= strtol(retvalue
.addr
, &end
, 10);
179 if (end
== retvalue
.addr
)
184 bool Configuration::getValue(const string
&rname
, unsigned long &value
) const {
185 assert(m_database
!= NULL
);
187 string rclass
= createClassName(rname
);
191 if (0 == XrmGetResource(m_database
, rname
.c_str(), rclass
.c_str(),
192 &rettype
, &retvalue
) || retvalue
.addr
== NULL
)
195 value
= strtoul(retvalue
.addr
, &end
, 10);
196 if (end
== retvalue
.addr
)
201 bool Configuration::getValue(const string
&rname
,
202 string
&value
) const {
203 assert(m_database
!= NULL
);
205 string rclass
= createClassName(rname
);
209 if (0 == XrmGetResource(m_database
, rname
.c_str(), rclass
.c_str(),
210 &rettype
, &retvalue
) || retvalue
.addr
== NULL
)
212 value
= retvalue
.addr
;
217 string
Configuration::createClassName(const string
&rname
) const {
218 string
rclass(rname
);
220 string::iterator it
= rclass
.begin(), end
= rclass
.end();
224 if (it
== end
) break;
225 it
= std::find(it
, rclass
.end(), '.');
226 if (it
== end
) break;
228 if (it
== end
) break;
234 char Configuration::toUpper(char c
) const {
235 if (c
>= 'a' && c
<= 'z')
236 return c
- 'a' + 'A';
This page took 0.051444 seconds and 5 git commands to generate.