]>
Dogcows Code - chaz/openbox/blob - src/Resource.cc
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.
26 # include "../config.h"
27 #endif // HAVE_CONFIG_H
31 #endif // HAVE_STDLIB_H
35 #endif // HAVE_STDIO_H
37 bool Resource::m_initialized
= false;
39 Resource::Resource(const std::string
&file
) {
50 Resource::Resource() {
60 Resource::~Resource() {
61 if (m_database
!= NULL
)
62 XrmDestroyDatabase(m_database
);
65 void Resource::setFile(const std::string
&file
) {
69 void Resource::setAutoSave(bool autosave
) {
70 m_autosave
= autosave
;
73 void Resource::save() {
74 ASSERT(m_database
!= NULL
);
75 XrmPutFileDatabase(m_database
, m_file
.c_str());
79 bool Resource::load() {
80 if (m_database
!= NULL
)
81 XrmDestroyDatabase(m_database
);
83 if (NULL
== (m_database
= XrmGetFileDatabase(m_file
.c_str())))
88 void Resource::create() {
89 if (m_database
!= NULL
)
90 XrmDestroyDatabase(m_database
);
92 ASSERT(NULL
!= (m_database
= XrmGetStringDatabase("")));
95 void Resource::setValue(const std::string
&rname
, bool value
) {
96 ASSERT(m_database
!= NULL
);
98 const char *val
= (value
? "True" : "False");
99 std::string rc_string
= rname
+ ": " + val
;
100 XrmPutLineResource(&m_database
, rc_string
.c_str());
107 void Resource::setValue(const std::string
&rname
, int value
) {
108 setValue(rname
, (long)value
);
111 void Resource::setValue(const std::string
&rname
, long value
) {
112 ASSERT(m_database
!= NULL
);
115 sprintf(val
, "%ld", value
);
116 std::string rc_string
= rname
+ ": " + val
;
117 XrmPutLineResource(&m_database
, rc_string
.c_str());
124 void Resource::setValue(const std::string
&rname
, const char *value
) {
125 ASSERT(m_database
!= NULL
);
126 ASSERT(value
!= NULL
);
128 std::string rc_string
= rname
+ ": " + value
;
129 XrmPutLineResource(&m_database
, rc_string
.c_str());
136 void Resource::setValue(const std::string
&rname
, const std::string
&value
) {
137 ASSERT(m_database
!= NULL
);
139 std::string rc_string
= rname
+ ": " + value
;
140 XrmPutLineResource(&m_database
, rc_string
.c_str());
147 bool Resource::getValue(const std::string
&rname
, const std::string
&rclass
,
149 ASSERT(rclass
.c_str() != NULL
);
150 ASSERT(m_database
!= NULL
);
154 if (0 == XrmGetResource(m_database
, rname
.c_str(), rclass
.c_str(),
155 &rettype
, &retvalue
) || retvalue
.addr
== NULL
)
157 std::string val
= retvalue
.addr
;
158 if (0 == strncasecmp(val
.c_str(), "true", val
.length()))
165 bool Resource::getValue(const std::string
&rname
, const std::string
&rclass
,
167 ASSERT(m_database
!= NULL
);
171 if (0 == XrmGetResource(m_database
, rname
.c_str(), rclass
.c_str(),
172 &rettype
, &retvalue
) || retvalue
.addr
== NULL
)
175 value
= strtol(retvalue
.addr
, &end
, 10);
176 if (end
== retvalue
.addr
)
181 bool Resource::getValue(const std::string
&rname
, const std::string
&rclass
,
182 std::string
&value
) const {
183 ASSERT(m_database
!= NULL
);
187 if (0 == XrmGetResource(m_database
, rname
.c_str(), rclass
.c_str(),
188 &rettype
, &retvalue
) || retvalue
.addr
== NULL
)
190 value
= retvalue
.addr
;
This page took 0.04222 seconds and 4 git commands to generate.