1 // Resource.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Jansens (ben@orodu.net)
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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.
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
30 #endif // HAVE_STDLIB_H
34 #endif // HAVE_STDIO_H
38 obResource::obResource(const std::string &file) {
45 obResource::obResource() {
51 obResource::~obResource() {
52 if (m_database != NULL)
53 XrmDestroyDatabase(m_database);
56 void obResource::setFile(const std::string &file) {
60 void obResource::setAutoSave(bool autosave) {
61 m_autosave = autosave;
64 void obResource::save() {
65 assert(m_database != NULL);
66 XrmPutFileDatabase(m_database, m_file.c_str());
70 bool obResource::load() {
71 if (m_database != NULL)
72 XrmDestroyDatabase(m_database);
74 if (NULL == (m_database = XrmGetFileDatabase(m_file.c_str())))
79 void obResource::setValue(const std::string &rname, bool value) {
80 assert(m_database != NULL);
82 const char *val = (value ? "True" : "False");
83 std::string rc_string = rname + ": " + val;
84 XrmPutLineResource(&m_database, rc_string.c_str());
91 void obResource::setValue(const std::string &rname, long value) {
92 assert(m_database != NULL);
95 sprintf(val, "%ld", value);
96 std::string rc_string = rname + ": " + val;
97 XrmPutLineResource(&m_database, rc_string.c_str());
104 void obResource::setValue(const std::string &rname, const char *value) {
105 assert(m_database != NULL);
107 std::string rc_string = rname + ": " + value;
108 XrmPutLineResource(&m_database, rc_string.c_str());
115 void obResource::setValue(const std::string &rname, const std::string &value) {
116 assert(m_database != NULL);
118 std::string rc_string = rname + ": " + value;
119 XrmPutLineResource(&m_database, rc_string.c_str());
126 bool obResource::getValue(const std::string &rname, const std::string &rclass,
128 assert(rclass.c_str() != NULL);
129 assert(m_database != NULL);
133 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
134 &rettype, &retvalue) || retvalue.addr == NULL)
136 std::string val = retvalue.addr;
137 if (0 == strncasecmp(val.c_str(), "true", val.length()))
144 bool obResource::getValue(const std::string &rname, const std::string &rclass,
146 assert(m_database != NULL);
150 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
151 &rettype, &retvalue) || retvalue.addr == NULL)
154 value = strtol(retvalue.addr, &end, 10);
155 if (end == retvalue.addr)
160 bool obResource::getValue(const std::string &rname, const std::string &rclass,
161 std::string &value) const {
162 assert(m_database != NULL);
166 if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(),
167 &rettype, &retvalue) || retvalue.addr == NULL)
169 value = retvalue.addr;