29.07.2011 - fundamental new
This commit is contained in:
235
piconfig.h
235
piconfig.h
@@ -3,72 +3,205 @@
|
||||
|
||||
#include "pifile.h"
|
||||
|
||||
#define PICONFIG_GET_VALUE \
|
||||
inline Entry & getValue(const PIString & vname, const char * def, bool * exist = 0) {return getValue(vname, PIString(def), exist);} \
|
||||
inline Entry & getValue(const PIString & vname, const PIStringList & def, bool * exist = 0) {return getValue(vname, def.join("%|%"), exist);} \
|
||||
inline Entry & getValue(const PIString & vname, const bool def, bool * exist = 0) {return getValue(vname, PIString::fromBool(def), exist);} \
|
||||
inline Entry & getValue(const PIString & vname, const short def, bool * exist = 0) {return getValue(vname, itos(def), exist);} \
|
||||
inline Entry & getValue(const PIString & vname, const int def, bool * exist = 0) {return getValue(vname, itos(def), exist);} \
|
||||
inline Entry & getValue(const PIString & vname, const long def, bool * exist = 0) {return getValue(vname, ltos(def), exist);} \
|
||||
inline Entry & getValue(const PIString & vname, const uchar def, bool * exist = 0) {return getValue(vname, uitos(def), exist);} \
|
||||
inline Entry & getValue(const PIString & vname, const ushort def, bool * exist = 0) {return getValue(vname, uitos(def), exist);} \
|
||||
inline Entry & getValue(const PIString & vname, const uint def, bool * exist = 0) {return getValue(vname, uitos(def), exist);} \
|
||||
inline Entry & getValue(const PIString & vname, const ulong def, bool * exist = 0) {return getValue(vname, ultos(def), exist);} \
|
||||
inline Entry & getValue(const PIString & vname, const float def, bool * exist = 0) {return getValue(vname, ftos(def), exist);} \
|
||||
inline Entry & getValue(const PIString & vname, const double def, bool * exist = 0) {return getValue(vname, dtos(def), exist);}
|
||||
|
||||
class PIConfig: public PIFile
|
||||
{
|
||||
friend class Entry;
|
||||
friend class Branch;
|
||||
public:
|
||||
PIConfig(const PIString & path, Flags<Mode> mode = Read | Write);
|
||||
PIConfig(const PIString & path, PIFlags<Mode> mode = Read | Write);
|
||||
~PIConfig() {;}
|
||||
|
||||
class Entry;
|
||||
|
||||
class Branch: public PIVector<Entry * > {
|
||||
friend class PIConfig;
|
||||
friend class Entry;
|
||||
friend std::ostream & operator <<(std::ostream & s, const Branch & v);
|
||||
public:
|
||||
inline Branch() {;}
|
||||
|
||||
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exist = 0);
|
||||
PICONFIG_GET_VALUE
|
||||
|
||||
Branch allLeaves();
|
||||
Branch getValues(const PIString & name);
|
||||
Branch getLeaves();
|
||||
Branch getBranches();
|
||||
Branch & filter(const PIString & f);
|
||||
inline bool isEntryExists(const PIString & name) const {piForeachCA (i, *this) if (entryExists(i, name)) return true; return false;}
|
||||
inline int indexOf(const Entry * e) {for (int i = 0; i < size_s(); ++i) if (at(i) == e) return i; return -1;}
|
||||
|
||||
inline void clear() {piForeachA (i, *this) delete i; PIVector<Entry * >::clear();}
|
||||
|
||||
private:
|
||||
bool entryExists(const Entry * e, const PIString & name) const;
|
||||
inline void allLeaves(Branch & b, Entry * e) {piForeachCA (i, e->_children) {if (i->isLeaf()) b << i; else allLeaves(b, i);}}
|
||||
inline void coutt(std::ostream & s, const PIString & p) const {piForeachCA (i, *this) i->coutt(s, p);}
|
||||
|
||||
static Entry _empty;
|
||||
PIString delim;
|
||||
|
||||
};
|
||||
|
||||
class Entry {
|
||||
friend class PIConfig;
|
||||
friend class Branch;
|
||||
public:
|
||||
inline Entry() {_parent = 0;}
|
||||
|
||||
inline Entry * parent() const {return _parent;}
|
||||
inline int childCount() {return _children.size_s();}
|
||||
inline Branch & children() {_children.delim = delim; return _children;}
|
||||
inline Entry * child(const int index) const {return _children[index];}
|
||||
inline Entry * findChild(const PIString & name) {piForeachCA (i, _children) if (i->_name == name) return i; return 0;}
|
||||
inline const Entry * findChild(const PIString & name) const {piForeachCA (i, _children) if (i->_name == name) return i; return 0;}
|
||||
inline bool isLeaf() const {return _children.isEmpty();}
|
||||
|
||||
inline const PIString & name() const {return _name;}
|
||||
inline const PIString & value() const {return _value;}
|
||||
inline const PIString & type() const {return _type;}
|
||||
inline const PIString & comment() const {return _comment;}
|
||||
|
||||
inline Entry & setName(const PIString & value) {_name = value; return *this;}
|
||||
inline Entry & setType(const PIString & value) {_type = value; return *this;}
|
||||
inline Entry & setComment(const PIString & value) {_comment = value; return *this;}
|
||||
inline Entry & setValue(const PIString & value) {_value = value; return *this;}
|
||||
inline Entry & setValue(const PIStringList & value) {setValue(value.join("%|%")); setType("l"); return *this;}
|
||||
inline Entry & setValue(const char * value) {setValue(PIString(value)); setType("s"); return *this;}
|
||||
inline Entry & setValue(const bool value) {setValue(btos(value)); setType("b"); return *this;}
|
||||
inline Entry & setValue(const char value) {setValue(PIString(1, value)); setType("s"); return *this;}
|
||||
inline Entry & setValue(const short value) {setValue(itos(value)); setType("n"); return *this;}
|
||||
inline Entry & setValue(const int value) {setValue(itos(value)); setType("n"); return *this;}
|
||||
inline Entry & setValue(const long value) {setValue(ltos(value)); setType("n"); return *this;}
|
||||
inline Entry & setValue(const uchar value) {setValue(uitos(value)); setType("n"); return *this;}
|
||||
inline Entry & setValue(const ushort value) {setValue(uitos(value)); setType("n"); return *this;}
|
||||
inline Entry & setValue(const uint value) {setValue(uitos(value)); setType("n"); return *this;}
|
||||
inline Entry & setValue(const ulong value) {setValue(ultos(value)); setType("n"); return *this;}
|
||||
inline Entry & setValue(const float value) {setValue(ftos(value)); setType("f"); return *this;}
|
||||
inline Entry & setValue(const double value) {setValue(dtos(value)); setType("f"); return *this;}
|
||||
|
||||
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exist = 0);
|
||||
PICONFIG_GET_VALUE
|
||||
|
||||
Branch getValues(const PIString & vname);
|
||||
|
||||
inline bool isEntryExists(const PIString & name) const {return entryExists(this, name);}
|
||||
|
||||
operator bool() {return _value.toBool();}
|
||||
operator char() {return (_value.isEmpty() ? 0 : _value[0].toAscii());}
|
||||
operator short() {return _value.toShort();}
|
||||
operator int() {return _value.toInt();}
|
||||
operator long() {return _value.toLong();}
|
||||
operator uchar() {return _value.toInt();}
|
||||
operator ushort() {return _value.toShort();}
|
||||
operator uint() {return _value.toInt();}
|
||||
operator ulong() {return _value.toLong();}
|
||||
operator float() {return _value.toFloat();}
|
||||
operator double() {return _value.toDouble();}
|
||||
operator PIString() {return _value;}
|
||||
operator PIStringList() {return _value.split("%|%");}
|
||||
|
||||
private:
|
||||
inline static bool compare(const PIConfig::Entry * f, const PIConfig::Entry * s) {return f->_line < s->_line;}
|
||||
bool entryExists(const Entry * e, const PIString & name) const;
|
||||
inline void buildLine() {_all = _tab + _full_name + " = " + _value + " #" + _type + " " + _comment;}
|
||||
inline void clear() {_children.clear(); _name = _value = _type = _comment = _all = PIString(); _line = 0; _parent = 0;}
|
||||
inline void coutt(std::ostream & s, const PIString & p) const {PIString nl = p + " "; if (!_value.isEmpty()) s << p << _name << " = " << _value << endl; else cout << p << _name << endl; piForeachCA (i, _children) i->coutt(s, nl);}
|
||||
|
||||
static Entry _empty;
|
||||
Entry * _parent;
|
||||
Branch _children;
|
||||
PIString _tab;
|
||||
PIString _name;
|
||||
PIString _value;
|
||||
PIString _type;
|
||||
PIString _comment;
|
||||
PIString _all;
|
||||
PIString _full_name;
|
||||
PIString delim;
|
||||
int _line;
|
||||
};
|
||||
|
||||
PIString getValue(const PIString & vname, const PIString & def = "", bool * exist = 0) const;
|
||||
PIString getValue(const PIString & vname, const char * def = "", bool * exist = 0) const {return getValue(vname, PIString(def), exist);}
|
||||
PIStringList getValue(const PIString & vname, const PIStringList & def = PIStringList(), bool * exist = 0) const;
|
||||
bool getValue(const PIString & vname, const bool def = false, bool * exist = 0) const;
|
||||
char getValue(const PIString & vname, const char def = 0, bool * exist = 0) const;
|
||||
short getValue(const PIString & vname, const short def = 0, bool * exist = 0) const;
|
||||
int getValue(const PIString & vname, const int def = 0, bool * exist = 0) const;
|
||||
long getValue(const PIString & vname, const long def = 0, bool * exist = 0) const;
|
||||
uchar getValue(const PIString & vname, const uchar def = 0, bool * exist = 0) const;
|
||||
ushort getValue(const PIString & vname, const ushort def = 0, bool * exist = 0) const;
|
||||
uint getValue(const PIString & vname, const uint def = 0, bool * exist = 0) const;
|
||||
ulong getValue(const PIString & vname, const ulong def = 0, bool * exist = 0) const;
|
||||
float getValue(const PIString & vname, const float def = 0., bool * exist = 0) const;
|
||||
double getValue(const PIString & vname, const double def = 0., bool * exist = 0) const;
|
||||
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exist = 0);
|
||||
PICONFIG_GET_VALUE
|
||||
|
||||
Branch getValues(const PIString & vname);
|
||||
|
||||
void setValue(const PIString & name, const PIString & value, const PIString & type = "s", bool write = true);
|
||||
void setValue(const PIString & name, const PIStringList & value, bool write = true);
|
||||
void setValue(const PIString & name, const char * value, bool write = true);
|
||||
void setValue(const PIString & name, const bool value, bool write = true);
|
||||
void setValue(const PIString & name, const char value, bool write = true);
|
||||
void setValue(const PIString & name, const short value, bool write = true);
|
||||
void setValue(const PIString & name, const int value, bool write = true);
|
||||
void setValue(const PIString & name, const long value, bool write = true);
|
||||
void setValue(const PIString & name, const uchar value, bool write = true);
|
||||
void setValue(const PIString & name, const ushort value, bool write = true);
|
||||
void setValue(const PIString & name, const uint value, bool write = true);
|
||||
void setValue(const PIString & name, const ulong value, bool write = true);
|
||||
void setValue(const PIString & name, const float value, bool write = true);
|
||||
void setValue(const PIString & name, const double value, bool write = true);
|
||||
inline void setValue(const PIString & name, const PIStringList & value, bool write = true) {setValue(name, value.join("%|%"), "l", write);}
|
||||
inline void setValue(const PIString & name, const char * value, bool write = true) {setValue(name, PIString(value), "s", write);}
|
||||
inline void setValue(const PIString & name, const bool value, bool write = true) {setValue(name, btos(value), "b", write);}
|
||||
inline void setValue(const PIString & name, const short value, bool write = true) {setValue(name, itos(value), "n", write);}
|
||||
inline void setValue(const PIString & name, const int value, bool write = true) {setValue(name, itos(value), "n", write);}
|
||||
inline void setValue(const PIString & name, const long value, bool write = true) {setValue(name, ltos(value), "n", write);}
|
||||
inline void setValue(const PIString & name, const uchar value, bool write = true) {setValue(name, uitos(value), "n", write);}
|
||||
inline void setValue(const PIString & name, const ushort value, bool write = true) {setValue(name, uitos(value), "n", write);}
|
||||
inline void setValue(const PIString & name, const uint value, bool write = true) {setValue(name, uitos(value), "n", write);}
|
||||
inline void setValue(const PIString & name, const ulong value, bool write = true) {setValue(name, ultos(value), "n", write);}
|
||||
inline void setValue(const PIString & name, const float value, bool write = true) {setValue(name, ftos(value), "f", write);}
|
||||
inline void setValue(const PIString & name, const double value, bool write = true) {setValue(name, dtos(value), "f", write);}
|
||||
|
||||
PIString getValue(uint number) const {return settval[number];}
|
||||
PIString getName(uint number) const {return settname[number];}
|
||||
PIString getComment(uint number) const {return settcom[number];}
|
||||
inline Entry & rootEntry() {return root;}
|
||||
inline int entriesCount() const {return childCount(&root);}
|
||||
inline bool isEntryExists(const PIString & name) const {return entryExists(&root, name);}
|
||||
|
||||
inline Branch allTree() {Branch b; piForeachCA (i, root._children) b << i; return b;}
|
||||
inline Branch allLeaves() {Branch b; allLeaves(b, &root); std::sort(b.begin(), b.end(), Entry::compare); return b;}
|
||||
int entryIndex(const PIString & name);
|
||||
|
||||
inline PIString getName(uint number) {return entryByIndex(number)._name;}
|
||||
inline PIString getValue(uint number) {return entryByIndex(number)._value;}
|
||||
inline PIChar getType(uint number) {return entryByIndex(number)._type[0];}
|
||||
inline PIString getComment(uint number) {return entryByIndex(number)._comment;}
|
||||
|
||||
void addEntry(const PIString & name, const PIString & value, const PIString & type = "s", bool write = true);
|
||||
void setName(uint number, const PIString & name, bool write = true);
|
||||
void setValue(uint number, const PIString & value, bool write = true);
|
||||
bool existsValue(const PIString & name);
|
||||
char getType(uint number) const {return setttype[number][0];}
|
||||
int getNumber(const PIString & name);
|
||||
void setName(uint number, const PIString & name);
|
||||
void setType(uint number, const PIString & type);
|
||||
void setComment(uint number, const PIString & comment);
|
||||
int numValues() const {return settval.size();}
|
||||
void addLine(const PIString & name, const PIString & value, const PIString & type = "s");
|
||||
void insertLine(uint number, const PIString & name, const PIString & value, const PIString & type = "s");
|
||||
void deleteLine(const PIString & name);
|
||||
void deleteLine(uint number);
|
||||
void setType(uint number, const PIString & type, bool write = true);
|
||||
void setComment(uint number, const PIString & comment, bool write = true);
|
||||
|
||||
void removeEntry(const PIString & name, bool write = true);
|
||||
void removeEntry(uint number, bool write = true);
|
||||
|
||||
void readAll();
|
||||
void writeAll();
|
||||
|
||||
const PIString & delimiter() const {return delim;}
|
||||
void setDelimiter(const PIString & d) {delim = d; setEntryDelim(&root, d); readAll();}
|
||||
|
||||
private:
|
||||
int childCount(const Entry * e) const {int c = 0; piForeachCA (i, e->_children) c += childCount(i); c += e->_children.size_s(); return c;}
|
||||
bool entryExists(const Entry * e, const PIString & name) const;
|
||||
void buildFullNames(Entry * e) {piForeachCA (i, e->_children) {if (e != &root) i->_full_name = e->_full_name + delim + i->_name; else i->_full_name = i->_name; buildFullNames(i);}}
|
||||
void allLeaves(Branch & b, Entry * e) {piForeachCA (i, e->_children) {if ((!i->_value.isEmpty() && !i->isLeaf()) || i->isLeaf()) b << i; allLeaves(b, i);}}
|
||||
void setEntryDelim(Entry * e, const PIString & d) {piForeachCA (i, e->_children) setEntryDelim(i, d); e->delim = d;}
|
||||
inline Entry & entryByIndex(const int index) {Branch b = allLeaves(); if (index < 0 || index >= b.size_s()) return empty; return *(b[index]);}
|
||||
void removeEntry(Branch & b, Entry * e);
|
||||
void parse();
|
||||
|
||||
PIVector<PIString> settname;
|
||||
PIVector<PIString> settval;
|
||||
PIVector<PIString> settcom;
|
||||
PIVector<PIString> setttab;
|
||||
PIVector<PIString> setttype;
|
||||
PIVector<PIString> all;
|
||||
PIVector<uint> settlines;
|
||||
|
||||
int centry;
|
||||
PIString delim;
|
||||
Entry root, empty;
|
||||
uint lines;
|
||||
|
||||
PIStringList other;
|
||||
|
||||
};
|
||||
|
||||
inline std::ostream & operator <<(std::ostream & s, const PIConfig::Branch & v) {v.coutt(s, ""); return s;}
|
||||
inline std::ostream & operator <<(std::ostream & s, const PIConfig::Entry & v) {s << v.value(); return s;}
|
||||
|
||||
#endif // PICONFIG_H
|
||||
|
||||
Reference in New Issue
Block a user