aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfluxgen <fluxgen>2003-05-18 21:55:16 (GMT)
committerfluxgen <fluxgen>2003-05-18 21:55:16 (GMT)
commitdf3b2a27662167892c922d4dd1714dc748079010 (patch)
tree2e7c36313a80bedea955767f4c88d495b10283cc
parent8f50aca74b35911f0ef45db482e2c5f56043576e (diff)
downloadfluxbox_pavel-df3b2a27662167892c922d4dd1714dc748079010.zip
fluxbox_pavel-df3b2a27662167892c922d4dd1714dc748079010.tar.bz2
moved to FbTk, and change DirHelper to Directory
-rw-r--r--src/DirHelper.cc84
-rw-r--r--src/DirHelper.hh53
-rw-r--r--src/Resource.cc119
-rw-r--r--src/Resource.hh142
-rw-r--r--src/XrmDatabaseHelper.hh78
5 files changed, 0 insertions, 476 deletions
diff --git a/src/DirHelper.cc b/src/DirHelper.cc
deleted file mode 100644
index 19f7612..0000000
--- a/src/DirHelper.cc
+++ /dev/null
@@ -1,84 +0,0 @@
1// DirHelper.cc
2// Copyright (c) 2002 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: DirHelper.cc,v 1.2 2003/02/15 01:42:17 fluxgen Exp $
23
24#include "DirHelper.hh"
25
26DirHelper::DirHelper(const char *dir):m_dir(0),
27m_num_entries(0) {
28 if (dir != 0)
29 open(dir);
30}
31
32DirHelper::~DirHelper() {
33 if (m_dir != 0)
34 close();
35}
36
37void DirHelper::rewind() {
38 if (m_dir != 0)
39 rewinddir(m_dir);
40}
41
42struct dirent *DirHelper::read() {
43 if (m_dir == 0)
44 return 0;
45
46 return readdir(m_dir);
47}
48
49std::string DirHelper::readFilename() {
50 dirent *ent = read();
51 if (ent == 0)
52 return "";
53 return (ent->d_name ? ent->d_name : "");
54}
55
56void DirHelper::close() {
57 if (m_dir != 0) {
58 closedir(m_dir);
59 m_dir = 0;
60 m_num_entries = 0;
61 }
62}
63
64
65bool DirHelper::open(const char *dir) {
66 if (dir == 0)
67 return false;
68
69 if (m_dir != 0)
70 close();
71
72 m_dir = opendir(dir);
73 if (m_dir == 0) // successfull loading?
74 return false;
75
76 // get number of entries
77 while (read())
78 m_num_entries++;
79
80 rewind(); // go back to start
81
82 return true;
83}
84
diff --git a/src/DirHelper.hh b/src/DirHelper.hh
deleted file mode 100644
index c41066b..0000000
--- a/src/DirHelper.hh
+++ /dev/null
@@ -1,53 +0,0 @@
1// DirHelper.hh
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: DirHelper.hh,v 1.2 2003/02/15 01:41:50 fluxgen Exp $
23
24#ifndef DIRHELPER_HH
25#define DIRHELPER_HH
26
27#include "NotCopyable.hh"
28
29#include <sys/types.h>
30#include <dirent.h>
31#include <string>
32
33/// Wrapper class for DIR * routines
34class DirHelper: private FbTk::NotCopyable {
35public:
36 explicit DirHelper(const char *dir = 0);
37 ~DirHelper();
38
39 void rewind();
40 /// gets next dirent info struct in directory
41 struct dirent * read();
42 /// reads next filename in directory
43 std::string readFilename();
44 void close();
45 bool open(const char *dir);
46 /// @return number of entries in the directory
47 size_t entries() const { return m_num_entries; }
48private:
49 DIR *m_dir;
50 size_t m_num_entries; ///< number of file entries in directory
51};
52
53#endif // DIRHELPER_HH
diff --git a/src/Resource.cc b/src/Resource.cc
deleted file mode 100644
index fb96b71..0000000
--- a/src/Resource.cc
+++ /dev/null
@@ -1,119 +0,0 @@
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.4 2002/12/01 13:41:58 rathnor 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
diff --git a/src/Resource.hh b/src/Resource.hh
deleted file mode 100644
index d657b5b..0000000
--- a/src/Resource.hh
+++ /dev/null
@@ -1,142 +0,0 @@
1// Resource.hh
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.hh,v 1.12 2003/05/10 13:44:24 fluxgen Exp $
23
24#ifndef RESOURCE_HH
25#define RESOURCE_HH
26
27#include "NotCopyable.hh"
28#include <string>
29#include <list>
30
31/// Base class for resources, this is only used in ResourceManager
32class Resource_base:private FbTk::NotCopyable
33{
34public:
35 virtual ~Resource_base() { };
36
37 /// set from string value
38 virtual void setFromString(char const *strval) = 0;
39 /// set default value
40 virtual void setDefaultValue() = 0;
41 /// get string value
42 virtual std::string getString() = 0;
43 /// get alternative name of this resource
44 inline const std::string& altName() const { return m_altname; }
45 /// get name of this resource
46 inline const std::string& name() const { return m_name; }
47
48protected:
49 Resource_base(const std::string &name, const std::string &altname):
50 m_name(name), m_altname(altname)
51 { }
52
53private:
54 std::string m_name; ///< name of this resource
55 std::string m_altname; ///< alternative name
56};
57
58template <typename T>
59class Resource;
60
61class ResourceManager
62{
63public:
64 typedef std::list<Resource_base *> ResourceList;
65
66 ResourceManager() { }
67 virtual ~ResourceManager() {}
68
69 /// Load all resources registered to this class
70 /// @return true on success
71 virtual bool load(const char *filename);
72
73 /// Save all resouces registered to this class
74 /// @return true on success
75 virtual bool save(const char *filename, const char *mergefilename=0);
76
77 /// Add resource to list, only used in Resource<T>
78 template <class T>
79 void addResource(Resource<T> &r) {
80 m_resourcelist.push_back(&r);
81 m_resourcelist.unique();
82 }
83
84 /// Remove a specific resource, only used in Resource<T>
85 template <class T>
86 void removeResource(Resource<T> &r) {
87 m_resourcelist.remove(&r);
88 }
89
90protected:
91 static void ensureXrmIsInitialize();
92
93private:
94 static bool m_init;
95 ResourceList m_resourcelist;
96};
97
98
99/// Real resource class
100/**
101 * usage: Resource<int> someresource(resourcemanager, 10, "someresourcename", "somealternativename"); \n
102 * and then implement setFromString and getString \n
103 * example: \n
104 * template <> \n
105 * void Resource<int>::setFromString(const char *str) { \n
106 * *(*this) = atoi(str); \n
107 * }
108 */
109template <typename T>
110class Resource:public Resource_base
111{
112public:
113 Resource(ResourceManager &rm, T val,
114 const std::string &name, const std::string &altname):
115 Resource_base(name, altname),
116 m_value(val), m_defaultval(val),
117 m_rm(rm)
118 {
119 m_rm.addResource(*this); // add this to resource handler
120 }
121 virtual ~Resource() {
122 m_rm.removeResource(*this); // remove this from resource handler
123 }
124
125 inline void setDefaultValue() { m_value = m_defaultval; }
126 /// sets resource from string, specialized, must be implemented
127 void setFromString(const char *strval);
128 inline Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;}
129 /// specialized, must be implemented
130 /// @return string value of resource
131 std::string getString();
132
133 inline T& operator*() { return m_value; }
134 inline const T& operator*() const { return m_value; }
135 inline T *operator->() { return &m_value; }
136 inline const T *operator->() const { return &m_value; }
137private:
138 T m_value, m_defaultval;
139 ResourceManager &m_rm;
140};
141
142#endif // RESOURCE_HH
diff --git a/src/XrmDatabaseHelper.hh b/src/XrmDatabaseHelper.hh
deleted file mode 100644
index 0cd3f52..0000000
--- a/src/XrmDatabaseHelper.hh
+++ /dev/null
@@ -1,78 +0,0 @@
1// XrmDatabaseHelper.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// $Id: XrmDatabaseHelper.hh,v 1.7 2002/12/02 22:12:09 fluxgen Exp $
23
24// This is a helper for XrmDatabase
25// when database goes out of scope
26// the XrmDatabase variable will be destroyed.
27
28#ifndef XRMDATABASEHELPER_HH
29#define XRMDATABASEHELPER_HH
30
31#include <X11/Xlib.h>
32#include <X11/Xresource.h>
33
34/**
35 Helper class for XrmDatabase.
36*/
37class XrmDatabaseHelper
38{
39public:
40 XrmDatabaseHelper(char const * filename=0)
41 : m_database(0)
42 { if (filename != 0) load(filename); }
43
44 ~XrmDatabaseHelper() {
45 close();
46 }
47
48 /// assignment operator
49 XrmDatabaseHelper& operator=(const XrmDatabase& database) {
50 if (m_database!=0)
51 XrmDestroyDatabase(m_database);
52 m_database = database;
53 return *this;
54 }
55 bool load(const char *filename) {
56 if (filename == 0)
57 return false;
58 XrmDatabase db = XrmGetFileDatabase(filename);
59 if (db == 0)
60 return false;
61 close(); // close old database
62 m_database = db; // set new and return true
63 return true;
64 }
65 void close() {
66 if (m_database != 0) {
67 XrmDestroyDatabase(m_database);
68 m_database = 0;
69 }
70 }
71 bool operator == (const XrmDatabase& database) { return m_database == database; }
72 XrmDatabase & operator*(void) { return m_database; }
73
74private:
75 XrmDatabase m_database;
76};
77
78#endif //_XRMDATABASEHELPER_HH_