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