From df3b2a27662167892c922d4dd1714dc748079010 Mon Sep 17 00:00:00 2001 From: fluxgen Date: Sun, 18 May 2003 21:55:16 +0000 Subject: moved to FbTk, and change DirHelper to Directory --- src/DirHelper.cc | 84 ---------------------------- src/DirHelper.hh | 53 ------------------ src/Resource.cc | 119 --------------------------------------- src/Resource.hh | 142 ----------------------------------------------- src/XrmDatabaseHelper.hh | 78 -------------------------- 5 files changed, 476 deletions(-) delete mode 100644 src/DirHelper.cc delete mode 100644 src/DirHelper.hh delete mode 100644 src/Resource.cc delete mode 100644 src/Resource.hh delete mode 100644 src/XrmDatabaseHelper.hh 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 @@ -// DirHelper.cc -// Copyright (c) 2002 Henrik Kinnunen (fluxgen at users.sourceforge.net) -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -// $Id: DirHelper.cc,v 1.2 2003/02/15 01:42:17 fluxgen Exp $ - -#include "DirHelper.hh" - -DirHelper::DirHelper(const char *dir):m_dir(0), -m_num_entries(0) { - if (dir != 0) - open(dir); -} - -DirHelper::~DirHelper() { - if (m_dir != 0) - close(); -} - -void DirHelper::rewind() { - if (m_dir != 0) - rewinddir(m_dir); -} - -struct dirent *DirHelper::read() { - if (m_dir == 0) - return 0; - - return readdir(m_dir); -} - -std::string DirHelper::readFilename() { - dirent *ent = read(); - if (ent == 0) - return ""; - return (ent->d_name ? ent->d_name : ""); -} - -void DirHelper::close() { - if (m_dir != 0) { - closedir(m_dir); - m_dir = 0; - m_num_entries = 0; - } -} - - -bool DirHelper::open(const char *dir) { - if (dir == 0) - return false; - - if (m_dir != 0) - close(); - - m_dir = opendir(dir); - if (m_dir == 0) // successfull loading? - return false; - - // get number of entries - while (read()) - m_num_entries++; - - rewind(); // go back to start - - return true; -} - 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 @@ -// DirHelper.hh -// Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -// $Id: DirHelper.hh,v 1.2 2003/02/15 01:41:50 fluxgen Exp $ - -#ifndef DIRHELPER_HH -#define DIRHELPER_HH - -#include "NotCopyable.hh" - -#include -#include -#include - -/// Wrapper class for DIR * routines -class DirHelper: private FbTk::NotCopyable { -public: - explicit DirHelper(const char *dir = 0); - ~DirHelper(); - - void rewind(); - /// gets next dirent info struct in directory - struct dirent * read(); - /// reads next filename in directory - std::string readFilename(); - void close(); - bool open(const char *dir); - /// @return number of entries in the directory - size_t entries() const { return m_num_entries; } -private: - DIR *m_dir; - size_t m_num_entries; ///< number of file entries in directory -}; - -#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 @@ -// Resource.cc -// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -// $Id: Resource.cc,v 1.4 2002/12/01 13:41:58 rathnor Exp $ - -#include "Resource.hh" -#include "XrmDatabaseHelper.hh" - -#include -#include - -using namespace std; - -bool ResourceManager::m_init = false; - -//-------- load ----------- -// loads a resourcefile -// returns true on success -// else false -//------------------------- -bool ResourceManager::load(const char *filename) { - assert(filename); - - ensureXrmIsInitialize(); - - XrmDatabaseHelper database; - database = XrmGetFileDatabase(filename); - if (database==0) - return false; - - XrmValue value; - char *value_type; - - //get list and go throu all the resources and load them - ResourceList::iterator i = m_resourcelist.begin(); - ResourceList::iterator i_end = m_resourcelist.end(); - for (; i != i_end; ++i) { - - Resource_base *resource = *i; - if (XrmGetResource(*database, resource->name().c_str(), - resource->altName().c_str(), &value_type, &value)) - resource->setFromString(value.addr); - else { - cerr<<"Failed to read: "<name()<setDefaultValue(); - } - } - - return true; -} - -//-------------- save ----------------- -// Saves all the resource to a file -// returns 0 on success -// else negative value representing -// the error -//------------------------------------- -bool ResourceManager::save(const char *filename, const char *mergefilename) { - assert(filename); - - ensureXrmIsInitialize(); - - XrmDatabaseHelper database; - - string rc_string; - ResourceList::iterator i = m_resourcelist.begin(); - ResourceList::iterator i_end = m_resourcelist.end(); - //write all resources to database - for (; i != i_end; ++i) { - Resource_base *resource = *i; - rc_string = resource->name() + string(": ") + resource->getString(); - XrmPutLineResource(&*database, rc_string.c_str()); - } - - if (database==0) - return false; - - //check if we want to merge a database - if (mergefilename) { - XrmDatabaseHelper olddatabase(mergefilename); - if (olddatabase == 0) // did we load the file? - return false; - - XrmMergeDatabases(*database, &*olddatabase); // merge databases - XrmPutFileDatabase(*olddatabase, filename); // save database to file - - *database = 0; // don't try to destroy the database - } else // save database to file - XrmPutFileDatabase(*database, filename); - - return true; -} - -void ResourceManager::ensureXrmIsInitialize() { - if (!m_init) { - XrmInitialize(); - m_init = true; - } -} - 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 @@ -// Resource.hh -// Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net) -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -// $Id: Resource.hh,v 1.12 2003/05/10 13:44:24 fluxgen Exp $ - -#ifndef RESOURCE_HH -#define RESOURCE_HH - -#include "NotCopyable.hh" -#include -#include - -/// Base class for resources, this is only used in ResourceManager -class Resource_base:private FbTk::NotCopyable -{ -public: - virtual ~Resource_base() { }; - - /// set from string value - virtual void setFromString(char const *strval) = 0; - /// set default value - virtual void setDefaultValue() = 0; - /// get string value - virtual std::string getString() = 0; - /// get alternative name of this resource - inline const std::string& altName() const { return m_altname; } - /// get name of this resource - inline const std::string& name() const { return m_name; } - -protected: - Resource_base(const std::string &name, const std::string &altname): - m_name(name), m_altname(altname) - { } - -private: - std::string m_name; ///< name of this resource - std::string m_altname; ///< alternative name -}; - -template -class Resource; - -class ResourceManager -{ -public: - typedef std::list ResourceList; - - ResourceManager() { } - virtual ~ResourceManager() {} - - /// Load all resources registered to this class - /// @return true on success - virtual bool load(const char *filename); - - /// Save all resouces registered to this class - /// @return true on success - virtual bool save(const char *filename, const char *mergefilename=0); - - /// Add resource to list, only used in Resource - template - void addResource(Resource &r) { - m_resourcelist.push_back(&r); - m_resourcelist.unique(); - } - - /// Remove a specific resource, only used in Resource - template - void removeResource(Resource &r) { - m_resourcelist.remove(&r); - } - -protected: - static void ensureXrmIsInitialize(); - -private: - static bool m_init; - ResourceList m_resourcelist; -}; - - -/// Real resource class -/** - * usage: Resource someresource(resourcemanager, 10, "someresourcename", "somealternativename"); \n - * and then implement setFromString and getString \n - * example: \n - * template <> \n - * void Resource::setFromString(const char *str) { \n - * *(*this) = atoi(str); \n - * } - */ -template -class Resource:public Resource_base -{ -public: - Resource(ResourceManager &rm, T val, - const std::string &name, const std::string &altname): - Resource_base(name, altname), - m_value(val), m_defaultval(val), - m_rm(rm) - { - m_rm.addResource(*this); // add this to resource handler - } - virtual ~Resource() { - m_rm.removeResource(*this); // remove this from resource handler - } - - inline void setDefaultValue() { m_value = m_defaultval; } - /// sets resource from string, specialized, must be implemented - void setFromString(const char *strval); - inline Resource& operator = (const T& newvalue) { m_value = newvalue; return *this;} - /// specialized, must be implemented - /// @return string value of resource - std::string getString(); - - inline T& operator*() { return m_value; } - inline const T& operator*() const { return m_value; } - inline T *operator->() { return &m_value; } - inline const T *operator->() const { return &m_value; } -private: - T m_value, m_defaultval; - ResourceManager &m_rm; -}; - -#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 @@ -// XrmDatabaseHelper.hh -// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -// $Id: XrmDatabaseHelper.hh,v 1.7 2002/12/02 22:12:09 fluxgen Exp $ - -// This is a helper for XrmDatabase -// when database goes out of scope -// the XrmDatabase variable will be destroyed. - -#ifndef XRMDATABASEHELPER_HH -#define XRMDATABASEHELPER_HH - -#include -#include - -/** - Helper class for XrmDatabase. -*/ -class XrmDatabaseHelper -{ -public: - XrmDatabaseHelper(char const * filename=0) - : m_database(0) - { if (filename != 0) load(filename); } - - ~XrmDatabaseHelper() { - close(); - } - - /// assignment operator - XrmDatabaseHelper& operator=(const XrmDatabase& database) { - if (m_database!=0) - XrmDestroyDatabase(m_database); - m_database = database; - return *this; - } - bool load(const char *filename) { - if (filename == 0) - return false; - XrmDatabase db = XrmGetFileDatabase(filename); - if (db == 0) - return false; - close(); // close old database - m_database = db; // set new and return true - return true; - } - void close() { - if (m_database != 0) { - XrmDestroyDatabase(m_database); - m_database = 0; - } - } - bool operator == (const XrmDatabase& database) { return m_database == database; } - XrmDatabase & operator*(void) { return m_database; } - -private: - XrmDatabase m_database; -}; - -#endif //_XRMDATABASEHELPER_HH_ -- cgit v0.11.2