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