aboutsummaryrefslogtreecommitdiff
path: root/src/Resource.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/Resource.cc')
-rw-r--r--src/Resource.cc119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/Resource.cc b/src/Resource.cc
new file mode 100644
index 0000000..cb871f1
--- /dev/null
+++ b/src/Resource.cc
@@ -0,0 +1,119 @@
1// Resource.cc
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
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.3 2002/07/20 09:51:26 fluxgen Exp $
23
24#include "Resource.hh"
25#include "XrmDatabaseHelper.hh"
26
27#include <iostream>
28#include <cassert>
29
30using namespace std;
31
32bool ResourceManager::m_init = false;
33
34//-------- load -----------
35// loads a resourcefile
36// returns true on success
37// else false
38//-------------------------
39bool ResourceManager::load(const char *filename) {
40 assert(filename);
41
42 ensureXrmIsInitialize();
43
44 XrmDatabaseHelper database;
45 database = XrmGetFileDatabase(filename);
46 if (database==0)
47 return false;
48
49 XrmValue value;
50 char *value_type;
51
52 //get list and go throu all the resources and load them
53 ResourceList::iterator i = m_resourcelist.begin();
54 ResourceList::iterator i_end = m_resourcelist.end();
55 for (; i != i_end; ++i) {
56
57 Resource_base *resource = *i;
58 if (XrmGetResource(*database, resource->name().c_str(),
59 resource->altName().c_str(), &value_type, &value))
60 resource->setFromString(value.addr);
61 else {
62 cerr<<"Failed to read: "<<resource->name()<<endl;
63 cerr<<"Setting default value"<<endl;
64 resource->setDefaultValue();
65 }
66 }
67
68 return true;
69}
70
71//-------------- save -----------------
72// Saves all the resource to a file
73// returns 0 on success
74// else negative value representing
75// the error
76//-------------------------------------
77bool ResourceManager::save(const char *filename, const char *mergefilename) {
78 assert(filename);
79
80 ensureXrmIsInitialize();
81
82 XrmDatabaseHelper database;
83
84 string rc_string;
85 ResourceList::iterator i = m_resourcelist.begin();
86 ResourceList::iterator i_end = m_resourcelist.end();
87 //write all resources to database
88 for (; i != i_end; ++i) {
89 Resource_base *resource = *i;
90 rc_string = resource->name() + string(": ") + resource->getString();
91 XrmPutLineResource(&*database, rc_string.c_str());
92 }
93
94 if (database==0)
95 return false;
96
97 //check if we want to merge a database
98 if (mergefilename) {
99 XrmDatabaseHelper olddatabase(mergefilename);
100 if (olddatabase == 0) // did we load the file?
101 return false;
102
103 XrmMergeDatabases(*database, &*olddatabase); // merge databases
104 XrmPutFileDatabase(*olddatabase, filename); // save database to file
105
106 *database = 0; // don't try to destroy the database
107 } else // save database to file
108 XrmPutFileDatabase(*database, filename);
109
110 return true;
111}
112
113void ResourceManager::ensureXrmIsInitialize() {
114 if (!m_init) {
115 XrmInitialize();
116 m_init = true;
117 }
118}
119