]> Dogcows Code - chaz/openbox/blob - src/Resource.cc
5ff05657c28a7822991e0556ea82106e97e1a63a
[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 obResource::obResource(const std::string &file) {
39 setFile(file);
40 m_modified = false;
41 m_database = NULL;
42 m_autosave = true;
43 }
44
45 obResource::obResource() {
46 m_modified = false;
47 m_database = NULL;
48 m_autosave = true;
49 }
50
51 obResource::~obResource() {
52 if (m_database != NULL)
53 XrmDestroyDatabase(m_database);
54 }
55
56 void obResource::setFile(const std::string &file) {
57 m_file = file;
58 }
59
60 void obResource::setAutoSave(bool autosave) {
61 m_autosave = autosave;
62 }
63
64 void obResource::save() {
65 assert(m_file.c_str() != NULL);
66 assert(m_database != NULL);
67 XrmPutFileDatabase(m_database, m_file.c_str());
68 m_modified = false;
69 }
70
71 bool obResource::load() {
72 assert(m_file.c_str() != NULL);
73 if (m_database != NULL)
74 XrmDestroyDatabase(m_database);
75 m_modified = false;
76 if (NULL == (m_database = XrmGetFileDatabase(m_file.c_str())))
77 return false;
78 return true;
79 }
80
81 void obResource::setValue(const std::string &rname, bool value) {
82 assert(rname.c_str() != NULL);
83 assert(m_database != NULL);
84
85 const char *val = (value ? "True" : "False");
86 std::string rc_string = rname + ": " + val;
87 XrmPutLineResource(&m_database, rc_string.c_str());
88
89 m_modified = true;
90 if (m_autosave)
91 save();
92 }
93
94 void obResource::setValue(const std::string &rname, long value) {
95 assert(rname.c_str() != NULL);
96 assert(m_database != NULL);
97
98 char val[11];
99 sprintf(val, "%ld", value);
100 std::string rc_string = rname + ": " + val;
101 XrmPutLineResource(&m_database, rc_string.c_str());
102
103 m_modified = true;
104 if (m_autosave)
105 save();
106 }
107
108 void obResource::setValue(const std::string &rname, const char *value) {
109 assert(rname.c_str() != NULL);
110 assert(m_database != NULL);
111
112 std::string rc_string = rname + ": " + value;
113 XrmPutLineResource(&m_database, rc_string.c_str());
114
115 m_modified = true;
116 if (m_autosave)
117 save();
118 }
119
120 void obResource::setValue(const std::string &rname, const std::string &value) {
121 assert(rname.c_str() != NULL);
122 assert(m_database != NULL);
123
124 std::string rc_string = rname + ": " + value;
125 XrmPutLineResource(&m_database, rc_string.c_str());
126
127 m_modified = true;
128 if (m_autosave)
129 save();
130 }
131
132 bool obResource::getValue(const std::string &rname, const std::string &rclass,
133 bool &value) const {
134 assert(rname.c_str() != NULL);
135 assert(rclass.c_str() != NULL);
136 assert(m_database != NULL);
137
138 char *rettype;
139 XrmValue retvalue;
140 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
141 &rettype, &retvalue) || retvalue.addr == NULL)
142 return false;
143 std::string val = retvalue.addr;
144 if (val == "True")
145 value = true;
146 else
147 value = false;
148 return true;
149 }
150
151 bool obResource::getValue(const std::string &rname, const std::string &rclass,
152 long &value) const {
153 assert(rname.c_str() != NULL);
154 assert(rclass.c_str() != NULL);
155 assert(m_database != NULL);
156
157 char *rettype;
158 XrmValue retvalue;
159 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
160 &rettype, &retvalue) || retvalue.addr == NULL)
161 return false;
162 char *end;
163 value = strtol(retvalue.addr, &end, 10);
164 if (end == retvalue.addr)
165 return false;
166 return true;
167 }
168
169 bool obResource::getValue(const std::string &rname, const std::string &rclass,
170 std::string &value) const {
171 assert(rname.c_str() != NULL);
172 assert(rclass.c_str() != NULL);
173 assert(m_database != NULL);
174
175 char *rettype;
176 XrmValue retvalue;
177 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
178 &rettype, &retvalue) || retvalue.addr == NULL)
179 return false;
180 value = retvalue.addr;
181 return true;
182 }
This page took 0.039718 seconds and 4 git commands to generate.