aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Resource.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/Resource.cc')
-rw-r--r--src/FbTk/Resource.cc205
1 files changed, 205 insertions, 0 deletions
diff --git a/src/FbTk/Resource.cc b/src/FbTk/Resource.cc
new file mode 100644
index 0000000..4c56dbb
--- /dev/null
+++ b/src/FbTk/Resource.cc
@@ -0,0 +1,205 @@
1// Resource.cc
2// Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.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// $Id: Resource.cc,v 1.4 2003/12/19 18:25:39 fluxgen Exp $
23
24#include "XrmDatabaseHelper.hh"
25#include "Resource.hh"
26
27#include <iostream>
28#include <cassert>
29
30using namespace std;
31
32namespace FbTk {
33
34ResourceManager::ResourceManager(const char *filename, bool lock_db) :
35 m_db_lock(0),
36 m_database(0),
37 m_filename(filename)
38{
39 ensureXrmIsInitialize();
40
41 if (lock_db)
42 lock();
43}
44
45ResourceManager::~ResourceManager() {
46 if (m_database)
47 delete m_database;
48}
49
50bool ResourceManager::m_init = false;
51
52/**
53 reloads all resources from resourcefile
54 @return true on success else false
55*/
56bool ResourceManager::load(const char *filename) {
57 m_filename = filename;
58
59 // force reload (lock will ensure it exists)
60 if (m_database) {
61 delete m_database;
62 m_database = 0;
63 }
64
65 lock();
66 if (!m_database) {
67 unlock();
68 return false;
69 }
70
71 XrmValue value;
72 char *value_type;
73
74 //get list and go throu all the resources and load them
75 ResourceList::iterator i = m_resourcelist.begin();
76 ResourceList::iterator i_end = m_resourcelist.end();
77 for (; i != i_end; ++i) {
78
79 Resource_base *resource = *i;
80 if (XrmGetResource(**m_database, resource->name().c_str(),
81 resource->altName().c_str(), &value_type, &value))
82 resource->setFromString(value.addr);
83 else {
84 cerr<<"Failed to read: "<<resource->name()<<endl;
85 cerr<<"Setting default value"<<endl;
86 resource->setDefaultValue();
87 }
88 }
89
90 unlock();
91
92 return true;
93}
94
95/**
96 Saves all the resource to a file
97 @return 0 on success else negative value representing the error
98*/
99bool ResourceManager::save(const char *filename, const char *mergefilename) {
100 assert(filename);
101
102 // empty database
103 XrmDatabaseHelper database;
104
105 string rc_string;
106 ResourceList::iterator i = m_resourcelist.begin();
107 ResourceList::iterator i_end = m_resourcelist.end();
108 //write all resources to database
109 for (; i != i_end; ++i) {
110 Resource_base *resource = *i;
111 rc_string = resource->name() + string(": ") + resource->getString();
112 XrmPutLineResource(&*database, rc_string.c_str());
113 }
114
115 if (database==0)
116 return false;
117
118 //check if we want to merge a database
119 if (mergefilename) {
120 // force reload of file
121 m_filename = mergefilename;
122 if (m_database)
123 delete m_database;
124 m_database = 0;
125
126 lock();
127
128 if (!m_database) {
129 unlock();
130 return false;
131 }
132
133 XrmMergeDatabases(*database, &**m_database); // merge databases
134 XrmPutFileDatabase(**m_database, filename); // save database to file
135
136 // don't try to destroy the database (XrmMergeDatabases destroys it)
137 *database = 0;
138 unlock();
139 } else // save database to file
140 XrmPutFileDatabase(*database, filename);
141
142 m_filename = filename;
143 return true;
144}
145
146Resource_base *ResourceManager::findResource(const std::string &resname) {
147 // find resource name
148 ResourceList::iterator i = m_resourcelist.begin();
149 ResourceList::iterator i_end = m_resourcelist.end();
150 for (; i != i_end; ++i) {
151 if ((*i)->name() == resname ||
152 (*i)->altName() == resname)
153 return *i;
154 }
155 return 0;
156}
157
158string ResourceManager::resourceValue(const std::string &resname) {
159 Resource_base *res = findResource(resname);
160 if (res != 0)
161 return res->getString();
162
163 return "";
164}
165
166void ResourceManager::setResourceValue(const std::string &resname, const std::string &value) {
167 Resource_base *res = findResource(resname);
168 if (res != 0)
169 res->setFromString(value.c_str());
170
171}
172
173void ResourceManager::ensureXrmIsInitialize() {
174 if (!m_init) {
175 XrmInitialize();
176 m_init = true;
177 }
178}
179
180ResourceManager &ResourceManager::lock() {
181 ++m_db_lock;
182 // if the lock was zero, then load the database
183 if ((m_db_lock == 1 || !m_database) &&
184 m_filename != "") {
185 m_database = new XrmDatabaseHelper(m_filename.c_str());
186
187 // check that the database loaded ok
188 if (m_database && *m_database == 0) {
189 // didn't work
190 delete m_database;
191 m_database = 0;
192 }
193 }
194
195 return *this;
196}
197
198void ResourceManager::unlock() {
199 if (--m_db_lock == 0 && m_database) {
200 delete m_database;
201 m_database = 0;
202 }
203}
204
205}; // end namespace FbTk