summaryrefslogtreecommitdiff
path: root/src/syncevo/IniConfigNode.h
blob: 1c935d9649d2d30e4fee4eacec89fb715be9be6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2008-2009 Patrick Ohly <patrick.ohly@gmx.de>
 *
 * 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
 */

#ifndef INCL_EVOLUTION_INI_CONFIG_NODE
# define INCL_EVOLUTION_INI_CONFIG_NODE

#include <syncevo/ConfigNode.h>
#include <syncevo/DataBlob.h>

#include <string>
#include <list>

#include <syncevo/declarations.h>
SE_BEGIN_CXX

/**
 * A base class for .ini style data blobs.
 */
class IniBaseConfigNode: public ConfigNode {
  protected:
    boost::shared_ptr<DataBlob> m_data;
    bool m_modified;
    
    /**
     * Open or create a new blob. The blob will be read (if it exists)
     * but not created or written to unless flush() is called explicitly.
     */
    IniBaseConfigNode(const boost::shared_ptr<DataBlob> &data);

    /** 
     * 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(std::ostream &file) = 0;

  public:
    virtual void flush();
    virtual void reload() = 0;
    virtual std::string getName() const { return m_data->getName(); }
    virtual bool exists() const { return m_data->exists(); }
    virtual bool isReadOnly() const { return true; }
};

/**
 * This class started its life as the Posix implementation of the
 * ManagementNode in the Funambol C++ client library. Nowadays it is
 * part of the SyncEvolution 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>
 *
 */
class IniFileConfigNode : public IniBaseConfigNode {
    std::list<std::string> m_lines;

    void read();

 protected:
    virtual void toFile(std::ostream &file);

 public:
    IniFileConfigNode(const boost::shared_ptr<DataBlob> &data);
    IniFileConfigNode(const std::string &path, const std::string &fileName, bool readonly);

    /* keep underlying methods visible; our own setProperty() would hide them */
    using ConfigNode::setProperty;

    virtual InitStateString readProperty(const std::string &property) const;
    virtual void writeProperty(const std::string &property,
                               const InitStateString &value,
                               const std::string &comment = "");
    virtual void readProperties(ConfigProps &props) const;
    virtual void removeProperty(const std::string &property);
    virtual void clear();
    virtual void reload() { clear(); read(); }
};

/**
 * 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 and unset
 * properties are not stored.
 */
class IniHashConfigNode: public IniBaseConfigNode {
    std::map<std::string, std::string> m_props;
    /**
     * Map used to store pairs
     */
    void read();

 protected:

    virtual void toFile(std::ostream & file);

 public:
    IniHashConfigNode(const boost::shared_ptr<DataBlob> &data);
    IniHashConfigNode(const std::string &path, const std::string &fileName, bool readonly);
    virtual InitStateString readProperty(const std::string &property) const;
    virtual void writeProperty(const std::string &property,
                               const InitStateString &value,
                               const std::string &comment = "");
    virtual void readProperties(ConfigProps &props) const;
    virtual void writeProperties(const ConfigProps &props);
    virtual void removeProperty(const std::string &property);
    virtual void clear();
    virtual void reload() { clear(); read(); }
};


SE_END_CXX
#endif // INCL_EVOLUTION_INI_CONFIG_NODE