05.11.2011 - stable version, 0.1.0, self-test program, work at GCC 2.95 - 4.5, VC 2010, MinGW, Linux, Windows, QNX
This commit is contained in:
169
piconfig.h
169
piconfig.h
@@ -4,18 +4,31 @@
|
||||
#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);}
|
||||
Entry & getValue(const PIString & vname, const char * def, bool * exist = 0) {return getValue(vname, PIString(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const PIStringList & def, bool * exist = 0) {return getValue(vname, def.join("%|%"), exist);} \
|
||||
Entry & getValue(const PIString & vname, const bool def, bool * exist = 0) {return getValue(vname, PIString::fromBool(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const short def, bool * exist = 0) {return getValue(vname, itos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const int def, bool * exist = 0) {return getValue(vname, itos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const long def, bool * exist = 0) {return getValue(vname, ltos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const uchar def, bool * exist = 0) {return getValue(vname, uitos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const ushort def, bool * exist = 0) {return getValue(vname, uitos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const uint def, bool * exist = 0) {return getValue(vname, uitos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const ulong def, bool * exist = 0) {return getValue(vname, ultos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const float def, bool * exist = 0) {return getValue(vname, ftos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const double def, bool * exist = 0) {return getValue(vname, dtos(def), exist);} \
|
||||
\
|
||||
Entry & getValue(const PIString & vname, const char * def, bool * exist = 0) const {return getValue(vname, PIString(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const PIStringList & def, bool * exist = 0) const {return getValue(vname, def.join("%|%"), exist);} \
|
||||
Entry & getValue(const PIString & vname, const bool def, bool * exist = 0) const {return getValue(vname, PIString::fromBool(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const short def, bool * exist = 0) const {return getValue(vname, itos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const int def, bool * exist = 0) const {return getValue(vname, itos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const long def, bool * exist = 0) const {return getValue(vname, ltos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const uchar def, bool * exist = 0) const {return getValue(vname, uitos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const ushort def, bool * exist = 0) const {return getValue(vname, uitos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const uint def, bool * exist = 0) const {return getValue(vname, uitos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const ulong def, bool * exist = 0) const {return getValue(vname, ultos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const float def, bool * exist = 0) const {return getValue(vname, ftos(def), exist);} \
|
||||
Entry & getValue(const PIString & vname, const double def, bool * exist = 0) const {return getValue(vname, dtos(def), exist);}
|
||||
|
||||
class PIConfig: public PIFile
|
||||
{
|
||||
@@ -23,7 +36,7 @@ class PIConfig: public PIFile
|
||||
friend class Branch;
|
||||
public:
|
||||
PIConfig(const PIString & path, PIFlags<Mode> mode = Read | Write);
|
||||
~PIConfig() {;}
|
||||
~PIConfig() {piForeach (Entry * i, root._children) deleteEntry(i);}
|
||||
|
||||
class Entry;
|
||||
|
||||
@@ -32,9 +45,10 @@ public:
|
||||
friend class Entry;
|
||||
friend std::ostream & operator <<(std::ostream & s, const Branch & v);
|
||||
public:
|
||||
inline Branch() {;}
|
||||
Branch() {;}
|
||||
|
||||
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exist = 0);
|
||||
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exist = 0) const {return const_cast<Branch * >(this)->getValue(vname, def, exist);}
|
||||
PICONFIG_GET_VALUE
|
||||
|
||||
Branch allLeaves();
|
||||
@@ -42,15 +56,15 @@ public:
|
||||
Branch getLeaves();
|
||||
Branch getBranches();
|
||||
Branch & filter(const PIString & f);
|
||||
inline bool isEntryExists(const PIString & name) const {piForeachC (Entry * 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;}
|
||||
bool isEntryExists(const PIString & name) const {piForeachC (Entry * i, *this) if (entryExists(i, name)) return true; return false;}
|
||||
int indexOf(const Entry * e) {for (int i = 0; i < size_s(); ++i) if (at(i) == e) return i; return -1;}
|
||||
|
||||
inline void clear() {piForeach (Entry * i, *this) delete i; PIVector<Entry * >::clear();}
|
||||
void clear() {piForeach (Entry * i, *this) delete i; PIVector<Entry * >::clear();}
|
||||
|
||||
private:
|
||||
bool entryExists(const Entry * e, const PIString & name) const;
|
||||
inline void allLeaves(Branch & b, Entry * e) {piForeach (Entry * i, e->_children) {if (i->isLeaf()) b << i; else allLeaves(b, i);}}
|
||||
inline void coutt(std::ostream & s, const PIString & p) const {piForeachC (Entry * i, *this) i->coutt(s, p);}
|
||||
void allLeaves(Branch & b, Entry * e) {piForeach (Entry * i, e->_children) {if (i->isLeaf()) b << i; else allLeaves(b, i);}}
|
||||
void coutt(std::ostream & s, const PIString & p) const {piForeachC (Entry * i, *this) i->coutt(s, p);}
|
||||
|
||||
static Entry _empty;
|
||||
PIString delim;
|
||||
@@ -61,46 +75,47 @@ public:
|
||||
friend class PIConfig;
|
||||
friend class Branch;
|
||||
public:
|
||||
inline Entry() {_parent = 0;}
|
||||
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) {piForeach (Entry * i, _children) if (i->_name == name) return i; return 0;}
|
||||
inline const Entry * findChild(const PIString & name) const {piForeachC (Entry * i, _children) if (i->_name == name) return i; return 0;}
|
||||
inline bool isLeaf() const {return _children.isEmpty();}
|
||||
Entry * parent() const {return _parent;}
|
||||
int childCount() {return _children.size_s();}
|
||||
Branch & children() {_children.delim = delim; return _children;}
|
||||
Entry * child(const int index) const {return _children[index];}
|
||||
Entry * findChild(const PIString & name) {piForeach (Entry * i, _children) if (i->_name == name) return i; return 0;}
|
||||
const Entry * findChild(const PIString & name) const {piForeachC (Entry * i, _children) if (i->_name == name) return i; return 0;}
|
||||
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 const PIString & fullName() const {return _full_name;}
|
||||
const PIString & name() const {return _name;}
|
||||
const PIString & value() const {return _value;}
|
||||
const PIString & type() const {return _type;}
|
||||
const PIString & comment() const {return _comment;}
|
||||
const PIString & fullName() const {return _full_name;}
|
||||
|
||||
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 & setName(const PIString & value) {_name = value; return *this;}
|
||||
Entry & setType(const PIString & value) {_type = value; return *this;}
|
||||
Entry & setComment(const PIString & value) {_comment = value; return *this;}
|
||||
Entry & setValue(const PIString & value) {_value = value; return *this;}
|
||||
Entry & setValue(const PIStringList & value) {setValue(value.join("%|%")); setType("l"); return *this;}
|
||||
Entry & setValue(const char * value) {setValue(PIString(value)); setType("s"); return *this;}
|
||||
Entry & setValue(const bool value) {setValue(btos(value)); setType("b"); return *this;}
|
||||
Entry & setValue(const char value) {setValue(PIString(1, value)); setType("s"); return *this;}
|
||||
Entry & setValue(const short value) {setValue(itos(value)); setType("n"); return *this;}
|
||||
Entry & setValue(const int value) {setValue(itos(value)); setType("n"); return *this;}
|
||||
Entry & setValue(const long value) {setValue(ltos(value)); setType("n"); return *this;}
|
||||
Entry & setValue(const uchar value) {setValue(uitos(value)); setType("n"); return *this;}
|
||||
Entry & setValue(const ushort value) {setValue(uitos(value)); setType("n"); return *this;}
|
||||
Entry & setValue(const uint value) {setValue(uitos(value)); setType("n"); return *this;}
|
||||
Entry & setValue(const ulong value) {setValue(ultos(value)); setType("n"); return *this;}
|
||||
Entry & setValue(const float value) {setValue(ftos(value)); setType("f"); return *this;}
|
||||
Entry & setValue(const double value) {setValue(dtos(value)); setType("f"); return *this;}
|
||||
|
||||
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exist = 0);
|
||||
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exist = 0) const {return const_cast<Entry * >(this)->getValue(vname, def, exist);}
|
||||
PICONFIG_GET_VALUE
|
||||
|
||||
Branch getValues(const PIString & vname);
|
||||
|
||||
inline bool isEntryExists(const PIString & name) const {return entryExists(this, name);}
|
||||
bool isEntryExists(const PIString & name) const {return entryExists(this, name);}
|
||||
|
||||
operator bool() {return _value.toBool();}
|
||||
operator char() {return (_value.isEmpty() ? 0 : _value[0].toAscii());}
|
||||
@@ -117,11 +132,11 @@ public:
|
||||
operator PIStringList() {return _value.split("%|%");}
|
||||
|
||||
private:
|
||||
inline static bool compare(const PIConfig::Entry * f, const PIConfig::Entry * s) {return f->_line < s->_line;}
|
||||
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; piForeachC (Entry * i, _children) i->coutt(s, nl);}
|
||||
void buildLine() {_all = _tab + _full_name + " = " + _value + " #" + _type + " " + _comment;}
|
||||
void clear() {_children.clear(); _name = _value = _type = _comment = _all = PIString(); _line = 0; _parent = 0;}
|
||||
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; piForeachC (Entry * i, _children) i->coutt(s, nl);}
|
||||
|
||||
static Entry _empty;
|
||||
Entry * _parent;
|
||||
@@ -138,36 +153,37 @@ public:
|
||||
};
|
||||
|
||||
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exist = 0);
|
||||
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exist = 0) const {return const_cast<PIConfig * >(this)->getValue(vname, def, exist);}
|
||||
PICONFIG_GET_VALUE
|
||||
|
||||
Branch getValues(const PIString & vname);
|
||||
|
||||
void setValue(const PIString & name, const PIString & value, const PIString & type = "s", 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);}
|
||||
void setValue(const PIString & name, const PIStringList & value, bool write = true) {setValue(name, value.join("%|%"), "l", write);}
|
||||
void setValue(const PIString & name, const char * value, bool write = true) {setValue(name, PIString(value), "s", write);}
|
||||
void setValue(const PIString & name, const bool value, bool write = true) {setValue(name, btos(value), "b", write);}
|
||||
void setValue(const PIString & name, const short value, bool write = true) {setValue(name, itos(value), "n", write);}
|
||||
void setValue(const PIString & name, const int value, bool write = true) {setValue(name, itos(value), "n", write);}
|
||||
void setValue(const PIString & name, const long value, bool write = true) {setValue(name, ltos(value), "n", write);}
|
||||
void setValue(const PIString & name, const uchar value, bool write = true) {setValue(name, uitos(value), "n", write);}
|
||||
void setValue(const PIString & name, const ushort value, bool write = true) {setValue(name, uitos(value), "n", write);}
|
||||
void setValue(const PIString & name, const uint value, bool write = true) {setValue(name, uitos(value), "n", write);}
|
||||
void setValue(const PIString & name, const ulong value, bool write = true) {setValue(name, ultos(value), "n", write);}
|
||||
void setValue(const PIString & name, const float value, bool write = true) {setValue(name, ftos(value), "f", write);}
|
||||
void setValue(const PIString & name, const double value, bool write = true) {setValue(name, dtos(value), "f", write);}
|
||||
|
||||
inline Entry & rootEntry() {return root;}
|
||||
inline int entriesCount() const {return childCount(&root);}
|
||||
inline bool isEntryExists(const PIString & name) const {return entryExists(&root, name);}
|
||||
Entry & rootEntry() {return root;}
|
||||
int entriesCount() const {return childCount(&root);}
|
||||
bool isEntryExists(const PIString & name) const {return entryExists(&root, name);}
|
||||
|
||||
inline Branch allTree() {Branch b; piForeach (Entry * 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;}
|
||||
Branch allTree() {Branch b; piForeach (Entry * i, root._children) b << i; return b;}
|
||||
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;}
|
||||
PIString getName(uint number) {return entryByIndex(number)._name;}
|
||||
PIString getValue(uint number) {return entryByIndex(number)._value;}
|
||||
PIChar getType(uint number) {return entryByIndex(number)._type[0];}
|
||||
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);
|
||||
@@ -190,8 +206,9 @@ private:
|
||||
void buildFullNames(Entry * e) {piForeach (Entry * 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) {piForeach (Entry * i, e->_children) {if ((!i->_value.isEmpty() && !i->isLeaf()) || i->isLeaf()) b << i; allLeaves(b, i);}}
|
||||
void setEntryDelim(Entry * e, const PIString & d) {piForeach (Entry * 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]);}
|
||||
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 deleteEntry(Entry * e) {piForeach (Entry * i, e->_children) deleteEntry(i); delete e;}
|
||||
void parse();
|
||||
|
||||
int centry;
|
||||
|
||||
Reference in New Issue
Block a user