diff options
author | fluxgen <fluxgen> | 2002-01-18 00:47:33 (GMT) |
---|---|---|
committer | fluxgen <fluxgen> | 2002-01-18 00:47:33 (GMT) |
commit | 5d72cb458406fc42f7ee68d8f8dbdf99d6f9a192 (patch) | |
tree | 60783e5381495907c00a8ca3ba074a1429dfd5b5 | |
parent | 34f9c5e44685a56baf66b38dda30bc062548cc66 (diff) | |
download | fluxbox-5d72cb458406fc42f7ee68d8f8dbdf99d6f9a192.zip fluxbox-5d72cb458406fc42f7ee68d8f8dbdf99d6f9a192.tar.bz2 |
added Resource.cc/.hh
-rw-r--r-- | src/Resource.cc | 116 | ||||
-rw-r--r-- | src/Resource.hh | 107 |
2 files changed, 223 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 | |||
28 | using namespace std; | ||
29 | |||
30 | bool ResourceManager::m_init = false; | ||
31 | |||
32 | //-------- load ----------- | ||
33 | // loads a resourcefile | ||
34 | // returns true on success | ||
35 | // else false | ||
36 | //------------------------- | ||
37 | bool 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 | //------------------------------------- | ||
75 | bool 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 | |||
110 | void ResourceManager::ensureXrmIsInitialize() { | ||
111 | if (!m_init) { | ||
112 | XrmInitialize(); | ||
113 | m_init = true; | ||
114 | } | ||
115 | } | ||
116 | |||
diff --git a/src/Resource.hh b/src/Resource.hh new file mode 100644 index 0000000..38bea30 --- /dev/null +++ b/src/Resource.hh | |||
@@ -0,0 +1,107 @@ | |||
1 | // Resource.hh | ||
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 | #ifndef _RESOURCE_HH_ | ||
23 | #define _RESOURCE_HH_ | ||
24 | |||
25 | #include "NotCopyable.hh" | ||
26 | #include <string> | ||
27 | #include <list> | ||
28 | |||
29 | class Resource_base:private NotCopyable | ||
30 | { | ||
31 | public: | ||
32 | |||
33 | virtual void setFromString(char const *strval)=0; | ||
34 | virtual void setDefaultValue()=0; | ||
35 | |||
36 | virtual std::string getString()=0; | ||
37 | inline std::string& getAltName() { return m_altname; } | ||
38 | inline std::string& getName() { return m_name; } | ||
39 | |||
40 | protected: | ||
41 | Resource_base(const std::string &name, const std::string &altname): | ||
42 | m_name(name), m_altname(altname) | ||
43 | { | ||
44 | |||
45 | } | ||
46 | virtual ~Resource_base(){ }; | ||
47 | private: | ||
48 | std::string m_name, m_altname; | ||
49 | }; | ||
50 | |||
51 | class ResourceManager; | ||
52 | |||
53 | template <typename T> | ||
54 | class Resource:public Resource_base | ||
55 | { | ||
56 | public: | ||
57 | Resource(ResourceManager &rm, T val, | ||
58 | const std::string &name, const std::string &altname): | ||
59 | Resource_base(name, altname), | ||
60 | m_value(val), m_defaultval(val), | ||
61 | m_rm(rm) | ||
62 | { | ||
63 | m_rm.addResource(*this); | ||
64 | } | ||
65 | ~Resource() { | ||
66 | m_rm.removeResource(*this); | ||
67 | } | ||
68 | |||
69 | inline void setDefaultValue() { m_value = m_defaultval; } | ||
70 | void setFromString(const char *strval); | ||
71 | inline Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;} | ||
72 | |||
73 | std::string getString(); | ||
74 | inline T& operator*(void) { return m_value; } | ||
75 | inline T *operator->(void) { return &m_value; } | ||
76 | private: | ||
77 | T m_value, m_defaultval; | ||
78 | ResourceManager &m_rm; | ||
79 | }; | ||
80 | |||
81 | class ResourceManager | ||
82 | { | ||
83 | public: | ||
84 | typedef std::list<Resource_base *> ResourceList; | ||
85 | |||
86 | ResourceManager(){ } | ||
87 | |||
88 | bool load(const char *filename); | ||
89 | bool save(const char *filename, const char *mergefilename=0); | ||
90 | template <class T> | ||
91 | void addResource(Resource<T> &r) { | ||
92 | m_resourcelist.push_back(&r); | ||
93 | m_resourcelist.unique(); | ||
94 | } | ||
95 | template <class T> | ||
96 | void removeResource(Resource<T> &r) { | ||
97 | m_resourcelist.remove(&r); | ||
98 | } | ||
99 | private: | ||
100 | static inline void ensureXrmIsInitialize(); | ||
101 | static bool m_init; | ||
102 | ResourceList m_resourcelist; | ||
103 | |||
104 | }; | ||
105 | |||
106 | |||
107 | #endif //_RESOURCE_HH_ | ||