moved to FbTk, and change DirHelper to Directory

This commit is contained in:
fluxgen 2003-05-18 21:55:16 +00:00
parent 8f50aca74b
commit df3b2a2766
5 changed files with 0 additions and 476 deletions

View file

@ -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;
}

View file

@ -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 <sys/types.h>
#include <dirent.h>
#include <string>
/// 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

View file

@ -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 <iostream>
#include <cassert>
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: "<<resource->name()<<endl;
cerr<<"Setting default value"<<endl;
resource->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;
}
}

View file

@ -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 <string>
#include <list>
/// 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 <typename T>
class Resource;
class ResourceManager
{
public:
typedef std::list<Resource_base *> 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<T>
template <class T>
void addResource(Resource<T> &r) {
m_resourcelist.push_back(&r);
m_resourcelist.unique();
}
/// Remove a specific resource, only used in Resource<T>
template <class T>
void removeResource(Resource<T> &r) {
m_resourcelist.remove(&r);
}
protected:
static void ensureXrmIsInitialize();
private:
static bool m_init;
ResourceList m_resourcelist;
};
/// Real resource class
/**
* usage: Resource<int> someresource(resourcemanager, 10, "someresourcename", "somealternativename"); \n
* and then implement setFromString and getString \n
* example: \n
* template <> \n
* void Resource<int>::setFromString(const char *str) { \n
* *(*this) = atoi(str); \n
* }
*/
template <typename T>
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<T>& 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

View file

@ -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 <X11/Xlib.h>
#include <X11/Xresource.h>
/**
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_