2008-03-06 23:23:13 +01:00
|
|
|
/*
|
2009-03-25 15:21:04 +01:00
|
|
|
* Copyright (C) 2008-2009 Patrick Ohly <patrick.ohly@gmx.de>
|
2009-04-30 18:14:03 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) version 3.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA
|
2008-03-06 23:23:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCL_EVOLUTION_FILE_CONFIG_NODE
|
|
|
|
# define INCL_EVOLUTION_FILE_CONFIG_NODE
|
|
|
|
|
2009-10-05 14:49:32 +02:00
|
|
|
#include <syncevo/ConfigNode.h>
|
2008-03-06 23:23:13 +01:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
2009-10-02 17:23:53 +02:00
|
|
|
|
2009-10-05 14:49:32 +02:00
|
|
|
#include <syncevo/declarations.h>
|
2009-10-02 17:23:53 +02:00
|
|
|
SE_BEGIN_CXX
|
2008-03-06 23:23:13 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2009-08-20 03:27:40 +02:00
|
|
|
/**
|
|
|
|
* A base class for file related config
|
|
|
|
*/
|
|
|
|
class FileBaseConfigNode: public ConfigNode {
|
|
|
|
protected:
|
|
|
|
string m_path;
|
|
|
|
string m_fileName;
|
|
|
|
bool m_modified;
|
|
|
|
const bool m_readonly;
|
|
|
|
bool m_exists;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open or create a new file. The file will be read (if it exists)
|
|
|
|
* but not create or written to unless flush() is called explicitly
|
|
|
|
*
|
|
|
|
* @param path node name, maps to directory
|
|
|
|
* @param fileName name of file inside that directory
|
|
|
|
* @param readonly do not create or write file, it must exist;
|
|
|
|
* flush() will throw an exception when changes would have to be written
|
|
|
|
*/
|
|
|
|
FileBaseConfigNode(const string &path, const string &fileName, bool readonly);
|
|
|
|
/**
|
|
|
|
* a virtual method to serial data structure to the file
|
|
|
|
* It is used by flush function to flush memory into disk file
|
|
|
|
*/
|
|
|
|
virtual void toFile(FILE* file) = 0;
|
|
|
|
public:
|
|
|
|
virtual void flush();
|
|
|
|
virtual string getName() const { return m_path + "/" + m_fileName; }
|
|
|
|
virtual bool exists() const { return m_exists; }
|
|
|
|
};
|
2008-03-06 23:23:13 +01:00
|
|
|
/**
|
|
|
|
* This class started its life as the Posix implementation of the
|
|
|
|
* ManagementNode in the Funambol C++ client library. Nowadays it is
|
|
|
|
* part of the SyncEvoluition ConfigTree (see there for details).
|
|
|
|
*
|
|
|
|
* Each node is mapped to one file whose location is determined by
|
|
|
|
* the ConfigTree when the node gets created. Each node represents
|
|
|
|
* one .ini file with entries of the type
|
|
|
|
* <property>\s*=\s*<value>\s*\n
|
|
|
|
*
|
|
|
|
* Comments look like:
|
|
|
|
* \s*# <comment>
|
|
|
|
*
|
|
|
|
* @todo rewrite with standard C++ containers
|
|
|
|
*/
|
2009-08-20 03:27:40 +02:00
|
|
|
class FileConfigNode : public FileBaseConfigNode {
|
2008-03-06 23:23:13 +01:00
|
|
|
list<string> m_lines;
|
|
|
|
|
|
|
|
void read();
|
|
|
|
|
2009-08-20 03:27:40 +02:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual void toFile(FILE* file);
|
|
|
|
|
2008-03-06 23:23:13 +01:00
|
|
|
public:
|
|
|
|
/**
|
2009-04-07 20:33:18 +02:00
|
|
|
* Open or create a new file. The file will be read (if it exists)
|
|
|
|
* but not create or written to unless flush() is called explicitly
|
2008-03-06 23:23:13 +01:00
|
|
|
*
|
|
|
|
* @param path node name, maps to directory
|
|
|
|
* @param fileName name of file inside that directory
|
2009-04-07 20:33:18 +02:00
|
|
|
* @param readonly do not create or write file, it must exist;
|
|
|
|
* flush() will throw an exception when changes would have to be written
|
2008-03-06 23:23:13 +01:00
|
|
|
*/
|
2009-04-07 20:33:18 +02:00
|
|
|
FileConfigNode(const string &path, const string &fileName, bool readonly);
|
2008-03-06 23:23:13 +01:00
|
|
|
|
2009-04-29 10:21:41 +02:00
|
|
|
/* keep underlying methods visible; our own setProperty() would hide them */
|
|
|
|
using ConfigNode::setProperty;
|
|
|
|
|
2009-08-20 03:27:40 +02:00
|
|
|
virtual string readProperty(const string &property) const;
|
|
|
|
virtual void setProperty(const string &property,
|
|
|
|
const string &value,
|
|
|
|
const string &comment = "",
|
|
|
|
const string *defValue = NULL);
|
2009-11-21 18:10:18 +01:00
|
|
|
virtual void readProperties(ConfigProps &props) const;
|
2009-08-20 03:27:40 +02:00
|
|
|
virtual void removeProperty(const string &property);
|
2009-09-29 22:23:31 +02:00
|
|
|
virtual void clear();
|
2009-08-20 03:27:40 +02:00
|
|
|
};
|
2008-03-19 12:29:14 +01:00
|
|
|
|
2009-08-20 03:27:40 +02:00
|
|
|
/**
|
|
|
|
* The main difference from FileConfigNode is to store pair of 'property-value'
|
|
|
|
* in a map to avoid O(n^2) string comparison
|
|
|
|
* Here comments for property default value are discarded.
|
|
|
|
*/
|
|
|
|
class HashFileConfigNode: public FileBaseConfigNode {
|
|
|
|
map<std::string, std::string> m_props;
|
|
|
|
/**
|
|
|
|
* Map used to store pairs
|
|
|
|
*/
|
|
|
|
void read();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual void toFile(FILE* file);
|
|
|
|
|
|
|
|
public:
|
|
|
|
HashFileConfigNode(const string &path, const string &fileName, bool readonly);
|
2008-03-06 23:23:13 +01:00
|
|
|
virtual string readProperty(const string &property) const;
|
|
|
|
virtual void setProperty(const string &property,
|
|
|
|
const string &value,
|
2008-03-30 23:50:51 +02:00
|
|
|
const string &comment = "",
|
|
|
|
const string *defValue = NULL);
|
2009-11-21 18:10:18 +01:00
|
|
|
virtual void readProperties(ConfigProps &props) const;
|
|
|
|
virtual void writeProperties(const ConfigProps &props);
|
2008-03-06 23:23:13 +01:00
|
|
|
virtual void removeProperty(const string &property);
|
2009-09-29 22:23:31 +02:00
|
|
|
virtual void clear();
|
2008-03-06 23:23:13 +01:00
|
|
|
};
|
|
|
|
|
2009-10-02 17:23:53 +02:00
|
|
|
|
|
|
|
SE_END_CXX
|
2008-03-06 23:23:13 +01:00
|
|
|
#endif
|