]> Dogcows Code - chaz/openbox/blob - otk/configuration.cc
fixed bugs, got otkapp to select on a fd, modded widget to make use of otkapp, press...
[chaz/openbox] / otk / configuration.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 #include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 extern "C" {
8 #ifdef HAVE_STDLIB_H
9 # include <stdlib.h>
10 #endif // HAVE_STDLIB_H
11 }
12
13 #include "configuration.hh"
14 #include "util.hh"
15
16 #include <algorithm>
17
18 namespace otk {
19
20 bool Configuration::_initialized = False;
21
22 Configuration::Configuration(const std::string &file, bool autosave) {
23 setFile(file);
24 _modified = False;
25 _database = NULL;
26 _autosave = autosave;
27 if (! _initialized) {
28 XrmInitialize();
29 _initialized = True;
30 }
31 }
32
33 Configuration::Configuration(bool autosave) {
34 _modified = False;
35 _database = NULL;
36 _autosave = autosave;
37 if (! _initialized) {
38 XrmInitialize();
39 _initialized = True;
40 }
41 }
42
43 Configuration::~Configuration() {
44 if (_database != NULL)
45 XrmDestroyDatabase(_database);
46 }
47
48 void Configuration::setFile(const std::string &file) {
49 _file = file;
50 }
51
52 void Configuration::setAutoSave(bool autosave) {
53 _autosave = autosave;
54 }
55
56 void Configuration::save() {
57 assert(_database != NULL);
58 XrmPutFileDatabase(_database, _file.c_str());
59 _modified = False;
60 }
61
62 bool Configuration::load() {
63 if (_database != NULL)
64 XrmDestroyDatabase(_database);
65 _modified = False;
66 if (NULL == (_database = XrmGetFileDatabase(_file.c_str())))
67 return False;
68 return True;
69 }
70
71 bool Configuration::merge(const std::string &file, bool overwrite) {
72 if (XrmCombineFileDatabase(file.c_str(), &_database, overwrite) == 0)
73 return False;
74 _modified = True;
75 if (_autosave)
76 save();
77 return True;
78 }
79
80 void Configuration::create() {
81 if (_database != NULL)
82 XrmDestroyDatabase(_database);
83 _modified = False;
84 assert(NULL != (_database = XrmGetStringDatabase("")));
85 }
86
87 void Configuration::setValue(const std::string &rname, bool value) {
88 assert(_database != NULL);
89
90 const char *val = (value ? "True" : "False");
91 std::string rc_string = rname + ": " + val;
92 XrmPutLineResource(&_database, rc_string.c_str());
93
94 _modified = True;
95 if (_autosave)
96 save();
97 }
98
99 void Configuration::setValue(const std::string &rname, unsigned long value) {
100 assert(_database != NULL);
101
102 std::string rc_string = rname + ": " + itostring(value);
103 XrmPutLineResource(&_database, rc_string.c_str());
104
105 _modified = True;
106 if (_autosave)
107 save();
108 }
109
110 void Configuration::setValue(const std::string &rname, long value) {
111 assert(_database != NULL);
112
113 std::string rc_string = rname + ": " + itostring(value);
114 XrmPutLineResource(&_database, rc_string.c_str());
115
116 _modified = True;
117 if (_autosave)
118 save();
119 }
120
121 void Configuration::setValue(const std::string &rname, const char *value) {
122 assert(_database != NULL);
123 assert(value != NULL);
124
125 std::string rc_string = rname + ": " + value;
126 XrmPutLineResource(&_database, rc_string.c_str());
127
128 _modified = True;
129 if (_autosave)
130 save();
131 }
132
133 void Configuration::setValue(const std::string &rname,
134 const std::string &value) {
135 assert(_database != NULL);
136
137 std::string rc_string = rname + ": " + value;
138 XrmPutLineResource(&_database, rc_string.c_str());
139
140 _modified = True;
141 if (_autosave)
142 save();
143 }
144
145 bool Configuration::getValue(const std::string &rname, bool &value) const {
146 assert(_database != NULL);
147
148 std::string rclass = createClassName(rname);
149
150 char *rettype;
151 XrmValue retvalue;
152 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
153 &rettype, &retvalue) || retvalue.addr == NULL)
154 return False;
155 std::string val = retvalue.addr;
156 if (val == "True" || val == "True")
157 value = True;
158 else
159 value = False;
160 return True;
161 }
162
163 bool Configuration::getValue(const std::string &rname, long &value) const {
164 assert(_database != NULL);
165
166 std::string rclass = createClassName(rname);
167
168 char *rettype;
169 XrmValue retvalue;
170 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
171 &rettype, &retvalue) || retvalue.addr == NULL)
172 return False;
173 char *end;
174 value = strtol(retvalue.addr, &end, 10);
175 if (end == retvalue.addr)
176 return False;
177 return True;
178 }
179
180 bool Configuration::getValue(const std::string &rname, unsigned long &value) const {
181 assert(_database != NULL);
182
183 std::string rclass = createClassName(rname);
184
185 char *rettype;
186 XrmValue retvalue;
187 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
188 &rettype, &retvalue) || retvalue.addr == NULL)
189 return False;
190 char *end;
191 value = strtoul(retvalue.addr, &end, 10);
192 if (end == retvalue.addr)
193 return False;
194 return True;
195 }
196
197 bool Configuration::getValue(const std::string &rname,
198 std::string &value) const {
199 assert(_database != NULL);
200
201 std::string rclass = createClassName(rname);
202
203 char *rettype;
204 XrmValue retvalue;
205 if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
206 &rettype, &retvalue) || retvalue.addr == NULL)
207 return False;
208 value = retvalue.addr;
209 return True;
210 }
211
212
213 std::string Configuration::createClassName(const std::string &rname) const {
214 std::string rclass(rname);
215
216 std::string::iterator it = rclass.begin(), end = rclass.end();
217 while (True) {
218 *it = toUpper(*it);
219 ++it;
220 if (it == end) break;
221 it = std::find(it, rclass.end(), '.');
222 if (it == end) break;
223 ++it;
224 if (it == end) break;
225 }
226 return rclass;
227 }
228
229
230 char Configuration::toUpper(char c) const {
231 if (c >= 'a' && c <= 'z')
232 return c - 'a' + 'A';
233 return c;
234 }
235
236 }
This page took 0.043485 seconds and 4 git commands to generate.