Switch to unified view

a/src/query/dynconf.h b/src/query/dynconf.h
...
...
32
 *
32
 *
33
 * The storage is performed in a ConfSimple file, with subkeys and
33
 * The storage is performed in a ConfSimple file, with subkeys and
34
 * encodings which depend on the data stored. Under each section, the keys 
34
 * encodings which depend on the data stored. Under each section, the keys 
35
 * are sequential numeric, so this basically manages a set of lists.
35
 * are sequential numeric, so this basically manages a set of lists.
36
 *
36
 *
37
 * The code ensures that a a given value (as defined by the
38
 * DynConfEntry::equal() method is only stored once. If undesirable,
39
 * equal() should always return false.
37
 */
40
 */
38
41
39
#include <string>
42
#include <string>
40
#include <list>
43
#include <list>
41
#include <utility>
44
#include <utility>
42
45
43
#include "conftree.h"
46
#include "conftree.h"
47
#include "base64.h"
44
48
45
#ifndef NO_NAMESPACES
49
/** Interface for a stored object. */
46
using namespace std;
47
#endif
48
49
// Entry interface.
50
class DynConfEntry {
50
class DynConfEntry {
51
 public:
51
 public:
52
    virtual ~DynConfEntry() {}
52
    /** Decode object-as-string coming out from storage */
53
    virtual bool decode(const string &value) = 0;
53
    virtual bool decode(const std::string &value) = 0;
54
    /** Encode object state into state for storing */
54
    virtual bool encode(string& value) = 0;
55
    virtual bool encode(std::string& value) = 0;
56
    /** Compare objects */
55
    virtual bool equal(const DynConfEntry &other) = 0;
57
    virtual bool equal(const DynConfEntry &other) = 0;
56
};
58
};
57
59
58
60
/** Stored object specialization for generic string storage */
59
/** String storage generic object */
60
class RclSListEntry : public DynConfEntry {
61
class RclSListEntry : public DynConfEntry {
61
 public:
62
 public:
62
    RclSListEntry() {}
63
    RclSListEntry() 
64
    {
65
    }
63
    RclSListEntry(const string& v) : value(v) {}
66
    RclSListEntry(const std::string& v) 
64
    virtual ~RclSListEntry() {}
67
    : value(v) 
68
    {
69
    }
65
    virtual bool decode(const string &enc);
70
    virtual bool decode(const std::string &enc)
71
    {
72
  base64_decode(enc, value);
73
  return true;
74
    }
66
    virtual bool encode(string& enc);
75
    virtual bool encode(std::string& enc)
76
    {
77
  base64_encode(value, enc);
78
  return true;
79
    }
67
    virtual bool equal(const DynConfEntry& other);
80
    virtual bool equal(const DynConfEntry& other)
81
    {
82
  const RclSListEntry& e = dynamic_cast<const RclSListEntry&>(other);
83
  return e.value == value;
84
    }
68
85
69
    string value;
86
    std::string value;
70
};
87
};
71
88
72
/** The dynamic configuration class */
89
/** The dynamic configuration class */
73
class RclDynConf {
90
class RclDynConf {
74
 public:
91
 public:
75
    RclDynConf(const string &fn)
92
    RclDynConf(const std::string &fn)
76
  : m_data(fn.c_str()) {}
93
        : m_data(fn.c_str()) 
94
    {
95
    }
96
    bool ok() 
97
    {
77
    bool ok() {return m_data.getStatus() == ConfSimple::STATUS_RW;}
98
  return m_data.getStatus() == ConfSimple::STATUS_RW;
78
    string getFilename() {return m_data.getFilename();}
99
    }
100
    std::string getFilename() 
101
    {
102
  return m_data.getFilename();
103
    }
79
104
80
    // Generic methods
105
    // Generic methods
81
    bool eraseAll(const string& sk);
106
    bool eraseAll(const std::string& sk);
107
108
    /** Insert new entry for section sk
109
     * @param sk section this is for
110
     * @param n  new entry
111
     * @param s a scratch entry used for decoding and comparisons,
112
     *        avoiding templating the routine for the actual entry type.
113
     */
82
    bool insertNew(const string& sk, DynConfEntry &n, DynConfEntry &s, 
114
    bool insertNew(const std::string& sk, DynConfEntry &n, DynConfEntry &s, 
83
                   int maxlen = -1);
115
                   int maxlen = -1);
84
    template<typename Tp> list<Tp> getList(const string& sk);
116
    template<typename Tp> std::list<Tp> getList(const std::string& sk);
85
117
86
    // Specialized methods for simple string lists, designated by the
118
    // Specialized methods for simple string lists, designated by the
87
    // subkey value
119
    // subkey value
88
    bool enterString(const string sk, const string value, int maxlen = -1);
120
    bool enterString(const std::string sk, const std::string value, int maxlen = -1);
89
    list<string> getStringList(const string sk);
121
    std::list<std::string> getStringList(const std::string sk);
90
122
91
 private:
123
 private:
92
    unsigned int m_mlen;
124
    unsigned int m_mlen;
93
    ConfSimple   m_data;
125
    ConfSimple   m_data;
94
95
};
126
};
96
127
97
template<typename Tp> list<Tp> RclDynConf::getList(const string &sk)
128
template<typename Tp> std::list<Tp> RclDynConf::getList(const std::string &sk)
98
{
129
{
99
    list<Tp> mlist;
130
    std::list<Tp> mlist;
100
    Tp entry;
131
    Tp entry;
101
    vector<string> names = m_data.getNames(sk);
132
    std::vector<std::string> names = m_data.getNames(sk);
102
    for (vector<string>::const_iterator it = names.begin(); 
133
    for (std::vector<std::string>::const_iterator it = names.begin(); 
103
     it != names.end(); it++) {
134
     it != names.end(); it++) {
104
    string value;
135
    std::string value;
105
    if (m_data.get(*it, value, sk)) {
136
    if (m_data.get(*it, value, sk)) {
106
        if (!entry.decode(value))
137
        if (!entry.decode(value))
107
        continue;
138
        continue;
108
        mlist.push_front(entry);
139
        mlist.push_front(entry);
109
    }
140
    }
...
...
111
    return mlist;
142
    return mlist;
112
}
143
}
113
144
114
// Defined subkeys. Values in dynconf.cpp
145
// Defined subkeys. Values in dynconf.cpp
115
// History
146
// History
116
extern const string docHistSubKey;
147
extern const std::string docHistSubKey;
117
// All external indexes
148
// All external indexes
118
extern const string allEdbsSk;
149
extern const std::string allEdbsSk;
119
// Active external indexes
150
// Active external indexes
120
extern const string actEdbsSk;
151
extern const std::string actEdbsSk;
152
// Advanced search history
153
extern const std::string advSearchHistSk;
121
154
122
#endif /* _DYNCONF_H_INCLUDED_ */
155
#endif /* _DYNCONF_H_INCLUDED_ */