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