]> icculus.org git repositories - mikachu/openbox.git/blob - src/Resource.cc
fixed memory leaks for strftime_format and rootcommand
[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 #include "Util.h"
24
25 #ifdef    HAVE_CONFIG_H
26 #  include "../config.h"
27 #endif // HAVE_CONFIG_H
28
29 #ifdef    HAVE_STDLIB_H
30 #  include <stdlib.h>
31 #endif // HAVE_STDLIB_H
32
33 #ifdef    HAVE_STDIO_H
34 #  include <stdio.h>
35 #endif // HAVE_STDIO_H
36
37 bool Resource::m_initialized = false;
38
39 Resource::Resource(const std::string &file) {
40   setFile(file);
41   m_modified = false;
42   m_database = NULL;
43   m_autosave = true;
44   if (!m_initialized) {
45     XrmInitialize();
46     m_initialized = true;
47   }
48 }
49
50 Resource::Resource() {
51   m_modified = false;
52   m_database = NULL;
53   m_autosave = true;
54   if (!m_initialized) {
55     XrmInitialize();
56     m_initialized = true;
57   }
58 }
59
60 Resource::~Resource() {
61   if (m_database != NULL)
62     XrmDestroyDatabase(m_database);
63 }
64
65 void Resource::setFile(const std::string &file) {
66   m_file = file;
67 }
68
69 void Resource::setAutoSave(bool autosave) {
70   m_autosave = autosave;
71 }
72
73 void Resource::save() {
74   ASSERT(m_database != NULL);
75   XrmPutFileDatabase(m_database, m_file.c_str());
76   m_modified = false;
77 }
78
79 bool Resource::load() {
80   if (m_database != NULL)
81     XrmDestroyDatabase(m_database);
82   m_modified = false;
83   if (NULL == (m_database = XrmGetFileDatabase(m_file.c_str())))
84     return false;
85   return true;
86 }
87
88 void Resource::create() {
89   if (m_database != NULL)
90     XrmDestroyDatabase(m_database);
91   m_modified = false;
92   ASSERT(NULL != (m_database = XrmGetStringDatabase("")));
93 }
94
95 void Resource::setValue(const std::string &rname, bool value) {
96   ASSERT(m_database != NULL);
97
98   const char *val = (value ? "True" : "False");
99   std::string rc_string = rname + ": " + val;
100   XrmPutLineResource(&m_database, rc_string.c_str());
101
102   m_modified = true;
103   if (m_autosave)
104     save();
105 }
106
107 void Resource::setValue(const std::string &rname, int value) {
108   setValue(rname, (long)value);
109 }
110
111 void Resource::setValue(const std::string &rname, long value) {
112   ASSERT(m_database != NULL);
113   
114   char val[11];
115   sprintf(val, "%ld", value);
116   std::string rc_string = rname + ": " + val;
117   XrmPutLineResource(&m_database, rc_string.c_str());
118
119   m_modified = true;
120   if (m_autosave)
121     save();
122 }
123
124 void Resource::setValue(const std::string &rname, const char *value) {
125   ASSERT(m_database != NULL);
126   ASSERT(value != NULL);
127   
128   std::string rc_string = rname + ": " + value;
129   XrmPutLineResource(&m_database, rc_string.c_str());
130
131   m_modified = true;
132   if (m_autosave)
133     save();
134 }
135
136 void Resource::setValue(const std::string &rname, const std::string &value) {
137   ASSERT(m_database != NULL);
138   
139   std::string rc_string = rname + ": " + value;
140   XrmPutLineResource(&m_database, rc_string.c_str());
141
142   m_modified = true;
143   if (m_autosave)
144     save();
145 }
146
147 bool Resource::getValue(const std::string &rname, const std::string &rclass,
148                           bool &value) const {
149   ASSERT(rclass.c_str() != NULL);
150   ASSERT(m_database != NULL);
151   
152   char *rettype;
153   XrmValue retvalue;
154   if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(), 
155                           &rettype, &retvalue) || retvalue.addr == NULL)
156     return false;
157   std::string val = retvalue.addr;
158   if (0 == strncasecmp(val.c_str(), "true", val.length()))
159     value = true;
160   else
161     value = false;
162   return true;
163 }
164
165 bool Resource::getValue(const std::string &rname, const std::string &rclass,
166                           long &value) const {
167   ASSERT(m_database != NULL);
168   
169   char *rettype;
170   XrmValue retvalue;
171   if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(), 
172                           &rettype, &retvalue) || retvalue.addr == NULL)
173     return false;
174   char *end;
175   value = strtol(retvalue.addr, &end, 10);
176   if (end == retvalue.addr)
177     return false;
178   return true;
179 }
180
181 bool Resource::getValue(const std::string &rname, const std::string &rclass,
182                           std::string &value) const {
183   ASSERT(m_database != NULL);
184   
185   char *rettype;
186   XrmValue retvalue;
187   if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(), 
188                           &rettype, &retvalue) || retvalue.addr == NULL)
189     return false;
190   value = retvalue.addr;
191   return true;
192 }