]> Dogcows Code - chaz/openbox/blob - src/Resource.cc
1f6f84380528ff2ddbb2bdc6f6b0c1f839504e17
[chaz/openbox] / src / Resource.cc
1 // Resource.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Jansens (ben@orodu.net)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22 #include "Resource.h"
23
24 #ifdef HAVE_CONFIG_H
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 #ifdef HAVE_STDLIB_H
29 # include <stdlib.h>
30 #endif // HAVE_STDLIB_H
31
32 #ifdef HAVE_STDIO_H
33 # include <stdio.h>
34 #endif // HAVE_STDIO_H
35
36 #include <assert.h>
37
38 bool obResource::m_initialized = false;
39
40 obResource::obResource(const std::string &file) {
41 setFile(file);
42 m_modified = false;
43 m_database = NULL;
44 m_autosave = true;
45 if (!m_initialized) {
46 XrmInitialize();
47 m_initialized = true;
48 }
49 }
50
51 obResource::obResource() {
52 m_modified = false;
53 m_database = NULL;
54 m_autosave = true;
55 if (!m_initialized) {
56 XrmInitialize();
57 m_initialized = true;
58 }
59 }
60
61 obResource::~obResource() {
62 if (m_database != NULL)
63 XrmDestroyDatabase(m_database);
64 }
65
66 void obResource::setFile(const std::string &file) {
67 m_file = file;
68 }
69
70 void obResource::setAutoSave(bool autosave) {
71 m_autosave = autosave;
72 }
73
74 void obResource::save() {
75 assert(m_database != NULL);
76 XrmPutFileDatabase(m_database, m_file.c_str());
77 m_modified = false;
78 }
79
80 bool obResource::load() {
81 if (m_database != NULL)
82 XrmDestroyDatabase(m_database);
83 m_modified = false;
84 if (NULL == (m_database = XrmGetFileDatabase(m_file.c_str())))
85 return false;
86 return true;
87 }
88
89 void obResource::setValue(const std::string &rname, bool value) {
90 assert(m_database != NULL);
91
92 const char *val = (value ? "True" : "False");
93 std::string rc_string = rname + ": " + val;
94 XrmPutLineResource(&m_database, rc_string.c_str());
95
96 m_modified = true;
97 if (m_autosave)
98 save();
99 }
100
101 void obResource::setValue(const std::string &rname, int value) {
102 setValue(rname, (long)value);
103 }
104
105 void obResource::setValue(const std::string &rname, long value) {
106 assert(m_database != NULL);
107
108 char val[11];
109 sprintf(val, "%ld", value);
110 std::string rc_string = rname + ": " + val;
111 XrmPutLineResource(&m_database, rc_string.c_str());
112
113 m_modified = true;
114 if (m_autosave)
115 save();
116 }
117
118 void obResource::setValue(const std::string &rname, const char *value) {
119 assert(m_database != NULL);
120
121 std::string rc_string = rname + ": " + value;
122 XrmPutLineResource(&m_database, rc_string.c_str());
123
124 m_modified = true;
125 if (m_autosave)
126 save();
127 }
128
129 void obResource::setValue(const std::string &rname, const std::string &value) {
130 assert(m_database != NULL);
131
132 std::string rc_string = rname + ": " + value;
133 XrmPutLineResource(&m_database, rc_string.c_str());
134
135 m_modified = true;
136 if (m_autosave)
137 save();
138 }
139
140 bool obResource::getValue(const std::string &rname, const std::string &rclass,
141 bool &value) const {
142 assert(rclass.c_str() != NULL);
143 assert(m_database != NULL);
144
145 char *rettype;
146 XrmValue retvalue;
147 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
148 &rettype, &retvalue) || retvalue.addr == NULL)
149 return false;
150 std::string val = retvalue.addr;
151 if (0 == strncasecmp(val.c_str(), "true", val.length()))
152 value = true;
153 else
154 value = false;
155 return true;
156 }
157
158 bool obResource::getValue(const std::string &rname, const std::string &rclass,
159 long &value) const {
160 assert(m_database != NULL);
161
162 char *rettype;
163 XrmValue retvalue;
164 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
165 &rettype, &retvalue) || retvalue.addr == NULL)
166 return false;
167 char *end;
168 value = strtol(retvalue.addr, &end, 10);
169 if (end == retvalue.addr)
170 return false;
171 return true;
172 }
173
174 bool obResource::getValue(const std::string &rname, const std::string &rclass,
175 std::string &value) const {
176 assert(m_database != NULL);
177
178 char *rettype;
179 XrmValue retvalue;
180 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
181 &rettype, &retvalue) || retvalue.addr == NULL)
182 return false;
183 value = retvalue.addr;
184 return true;
185 }
This page took 0.039395 seconds and 3 git commands to generate.