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