before formatting

This commit is contained in:
2022-12-14 13:56:19 +03:00
parent c74ba871cd
commit 430a41fefc
14 changed files with 912 additions and 623 deletions

View File

@@ -29,6 +29,7 @@
#include "piiodevice.h"
#include "piiostream.h"
// clang-format off
#define PICONFIG_GET_VALUE \
Entry & getValue(const PIString & vname, const char * def, bool * exists = 0) {return getValue(vname, PIString(def), exists);} \
Entry & getValue(const PIString & vname, const PIStringList & def, bool * exists = 0) {return getValue(vname, def.join("%|%"), exists);} \
@@ -55,13 +56,13 @@
Entry & getValue(const PIString & vname, const ulong def, bool * exists = 0) const {return getValue(vname, PIString::fromNumber(def), exists);} \
Entry & getValue(const PIString & vname, const float def, bool * exists = 0) const {return getValue(vname, PIString::fromNumber(def), exists);} \
Entry & getValue(const PIString & vname, const double def, bool * exists = 0) const {return getValue(vname, PIString::fromNumber(def), exists);}
// clang-format on
class PIP_EXPORT PIConfig
{
class PIP_EXPORT PIConfig {
friend class Entry;
friend class Branch;
public:
public:
//! Contructs and read configuration file at path "path" in mode "mode"
PIConfig(const PIString & path, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
@@ -76,18 +77,21 @@ public:
class Entry;
class PIP_EXPORT Branch: public PIVector<Entry * > {
class PIP_EXPORT Branch: public PIVector<Entry *> {
friend class PIConfig;
friend class Entry;
#ifdef PIP_STD_IOSTREAM
friend std::ostream & operator <<(std::ostream & s, const Branch & v);
friend std::ostream & operator<<(std::ostream & s, const Branch & v);
#endif
friend PICout operator <<(PICout s, const Branch & v);
friend PICout operator<<(PICout s, const Branch & v);
public:
Branch() {;}
Branch() { ; }
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0);
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0) const {return const_cast<Branch * >(this)->getValue(vname, def, exists);}
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0) const {
return const_cast<Branch *>(this)->getValue(vname, def, exists);
}
PICONFIG_GET_VALUE
Branch allLeaves();
@@ -95,125 +99,226 @@ public:
Branch getLeaves();
Branch getBranches();
Branch & filter(const PIString & f);
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;}
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;
}
private:
bool entryExists(const Entry * e, const PIString & name) const;
void allLeaves(Branch & b, Entry * e) {piForeach (Entry * i, e->_children) {if (i->isLeaf()) b << i; else allLeaves(b, i);}}
void allLeaves(Branch & b, Entry * e) {
piForeach(Entry * i, e->_children) {
if (i->isLeaf())
b << i;
else
allLeaves(b, i);
}
}
#ifdef PIP_STD_IOSTREAM
void coutt(std::ostream & s, const PIString & p) const {piForeachC (Entry * i, *this) i->coutt(s, p);}
void coutt(std::ostream & s, const PIString & p) const {
piForeachC(Entry * i, *this)
i->coutt(s, p);
}
#endif
void piCoutt(PICout s, const PIString & p) const {piForeachC (Entry * i, *this) i->piCoutt(s, p);}
void piCoutt(PICout s, const PIString & p) const {
piForeachC(Entry * i, *this)
i->piCoutt(s, p);
}
static Entry _empty;
PIString delim;
};
class PIP_EXPORT Entry {
friend class PIConfig;
friend class Branch;
public:
Entry() {_parent = 0; _line = -1;}
Entry() {
_parent = 0;
_line = -1;
}
//! Returns parent entry, or 0 if there is no parent (root of default value)
Entry * parent() const {return _parent;}
Entry * parent() const { return _parent; }
//! Returns children count
int childCount() const {return _children.size_s();}
int childCount() const { return _children.size_s(); }
//! Returns children as \a PIConfig::Branch
Branch & children() const {_children.delim = delim; return _children;}
Branch & children() const {
_children.delim = delim;
return _children;
}
//! Returns child at index "index"
Entry * child(const int index) const {return _children[index];}
Entry * child(const int index) const { return _children[index]; }
//! Returns first child with name "name"
Entry * findChild(const PIString & name) {piForeach (Entry * i, _children) if (i->_name == name) return i; return 0;}
Entry * findChild(const PIString & name) {
piForeach(Entry * i, _children)
if (i->_name == name) return i;
return 0;
}
//! Returns first child with name "name"
const Entry * findChild(const PIString & name) const {piForeachC (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;
}
//! Returns \b true if there is no children
bool isLeaf() const {return _children.isEmpty();}
bool isLeaf() const { return _children.isEmpty(); }
//! Returns name
const PIString & name() const {return _name;}
const PIString & name() const { return _name; }
//! Returns value
const PIString & value() const {return _value;}
const PIString & value() const { return _value; }
//! Returns type
const PIString & type() const {return _type;}
const PIString & type() const { return _type; }
//! Returns comment
const PIString & comment() const {return _comment;}
const PIString & comment() const { return _comment; }
/** \brief Returns full name, i.e. name as it looks in file
* \details In case of default entry full name always is empty
* \snippet piconfig.cpp fullName */
const PIString & fullName() const {return _full_name;}
const PIString & fullName() const { return _full_name; }
//! Set name to "value" and returns this
Entry & setName(const PIString & value) {_name = value; return *this;}
Entry & setName(const PIString & value) {
_name = value;
return *this;
}
//! Set type to "value" and returns this
Entry & setType(const PIString & value) {_type = value; return *this;}
Entry & setType(const PIString & value) {
_type = value;
return *this;
}
//! Set comment to "value" and returns this
Entry & setComment(const PIString & value) {_comment = value; return *this;}
Entry & setComment(const PIString & value) {
_comment = value;
return *this;
}
//! Set value to "value" and returns this
Entry & setValue(const PIString & value) {_value = value; return *this;}
Entry & setValue(const PIString & value) {
_value = value;
return *this;
}
//! Set value to "value" and returns this. Type is set to "l"
Entry & setValue(const PIStringList & value) {setValue(value.join("%|%")); setType("l"); return *this;}
Entry & setValue(const PIStringList & value) {
setValue(value.join("%|%"));
setType("l");
return *this;
}
//! Set value to "value" and returns this. Type is set to "s"
Entry & setValue(const char * value) {setValue(PIString(value)); setType("s"); return *this;}
Entry & setValue(const char * value) {
setValue(PIString(value));
setType("s");
return *this;
}
//! Set value to "value" and returns this. Type is set to "b"
Entry & setValue(const bool value) {setValue(PIString::fromBool(value)); setType("b"); return *this;}
Entry & setValue(const bool value) {
setValue(PIString::fromBool(value));
setType("b");
return *this;
}
//! Set value to "value" and returns this. Type is set to "s"
Entry & setValue(const char value) {setValue(PIString(1, value)); setType("s"); return *this;}
Entry & setValue(const char value) {
setValue(PIString(1, value));
setType("s");
return *this;
}
//! Set value to "value" and returns this. Type is set to "n"
Entry & setValue(const short value) {setValue(PIString::fromNumber(value)); setType("n"); return *this;}
Entry & setValue(const short value) {
setValue(PIString::fromNumber(value));
setType("n");
return *this;
}
//! Set value to "value" and returns this. Type is set to "n"
Entry & setValue(const int value) {setValue(PIString::fromNumber(value)); setType("n"); return *this;}
Entry & setValue(const int value) {
setValue(PIString::fromNumber(value));
setType("n");
return *this;
}
//! Set value to "value" and returns this. Type is set to "n"
Entry & setValue(const long value) {setValue(PIString::fromNumber(value)); setType("n"); return *this;}
Entry & setValue(const long value) {
setValue(PIString::fromNumber(value));
setType("n");
return *this;
}
//! Set value to "value" and returns this. Type is set to "n"
Entry & setValue(const uchar value) {setValue(PIString::fromNumber(value)); setType("n"); return *this;}
Entry & setValue(const uchar value) {
setValue(PIString::fromNumber(value));
setType("n");
return *this;
}
//! Set value to "value" and returns this. Type is set to "n"
Entry & setValue(const ushort value) {setValue(PIString::fromNumber(value)); setType("n"); return *this;}
Entry & setValue(const ushort value) {
setValue(PIString::fromNumber(value));
setType("n");
return *this;
}
//! Set value to "value" and returns this. Type is set to "n"
Entry & setValue(const uint value) {setValue(PIString::fromNumber(value)); setType("n"); return *this;}
Entry & setValue(const uint value) {
setValue(PIString::fromNumber(value));
setType("n");
return *this;
}
//! Set value to "value" and returns this. Type is set to "n"
Entry & setValue(const ulong value) {setValue(PIString::fromNumber(value)); setType("n"); return *this;}
Entry & setValue(const ulong value) {
setValue(PIString::fromNumber(value));
setType("n");
return *this;
}
//! Set value to "value" and returns this. Type is set to "f"
Entry & setValue(const float value) {setValue(PIString::fromNumber(value)); setType("f"); return *this;}
Entry & setValue(const float value) {
setValue(PIString::fromNumber(value));
setType("f");
return *this;
}
//! Set value to "value" and returns this. Type is set to "f"
Entry & setValue(const double value) {setValue(PIString::fromNumber(value)); setType("f"); return *this;}
Entry & setValue(const double value) {
setValue(PIString::fromNumber(value));
setType("f");
return *this;
}
/** \brief Returns entry with name "vname" and default value "def"
* \details If there is no suitable entry found, reference to default internal entry with
* value = "def" will be returned, and if "exists" not null it will be set to \b false */
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0);
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0) const {return const_cast<Entry * >(this)->getValue(vname, def, exists);}
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0) const {
return const_cast<Entry *>(this)->getValue(vname, def, exists);
}
PICONFIG_GET_VALUE
//! \fn Entry & getValue(const PIString & vname, const char * def, bool * exists = 0)
@@ -261,58 +366,68 @@ public:
//! If there is no children returns if name == "name". Else returns if any child has name == "name"
bool isEntryExists(const PIString & name) const {return entryExists(this, name);}
bool isEntryExists(const PIString & name) const { return entryExists(this, name); }
//! Convertion to boolean
bool toBool() const {return _value.toBool();}
bool toBool() const { return _value.toBool(); }
//! Convertion to char
char toChar() const {return (_value.isEmpty() ? 0 : _value[0].toAscii());}
char toChar() const { return (_value.isEmpty() ? 0 : _value[0].toAscii()); }
//! Convertion to short
short toShort() const {return _value.toShort();}
short toShort() const { return _value.toShort(); }
//! Convertion to int
int toInt() const {return _value.toInt();}
int toInt() const { return _value.toInt(); }
//! Convertion to long
long toLong() const {return _value.toLong();}
long toLong() const { return _value.toLong(); }
//! Convertion to uchar
uchar toUChar() const {return _value.toInt();}
uchar toUChar() const { return _value.toInt(); }
//! Convertion to ushort
ushort toUShort() const {return _value.toShort();}
ushort toUShort() const { return _value.toShort(); }
//! Convertion to uint
uint toUInt() const {return _value.toInt();}
uint toUInt() const { return _value.toInt(); }
//! Convertion to ulong
ulong toULong() const {return _value.toLong();}
ulong toULong() const { return _value.toLong(); }
//! Convertion to float
float toFloat() const {return _value.toFloat();}
float toFloat() const { return _value.toFloat(); }
//! Convertion to double
double toDouble() const {return _value.toDouble();}
double toDouble() const { return _value.toDouble(); }
//! Convertion to PIString
PIString toString() const {return _value;}
PIString toString() const { return _value; }
//! Convertion to PIStringList
PIStringList toStringList() const {return _value.split("%|%");}
PIStringList toStringList() const { return _value.split("%|%"); }
private:
static bool compare(PIConfig::Entry * const & f, PIConfig::Entry * const & s) {return f->_line < s->_line;}
static bool compare(PIConfig::Entry * const & f, PIConfig::Entry * const & s) { return f->_line < s->_line; }
bool entryExists(const Entry * e, const PIString & name) const;
void buildLine() {_all = _tab + _full_name + " = " + _value + " #" + _type + " " + _comment;}
void clear() {_children.clear(); _name = _value = _type = _comment = _all = PIString(); _line = 0; _parent = 0;}
void buildLine() { _all = _tab + _full_name + " = " + _value + " #" + _type + " " + _comment; }
void clear() {
_children.clear();
_name = _value = _type = _comment = _all = PIString();
_line = 0;
_parent = 0;
}
#ifdef PIP_STD_IOSTREAM
void coutt(std::ostream & s, const PIString & p) const;
#endif
void piCoutt(PICout s, const PIString & p) const;
void deleteBranch() {piForeach (Entry * i, _children) {i->deleteBranch(); delete i;}}
void deleteBranch() {
piForeach(Entry * i, _children) {
i->deleteBranch();
delete i;
}
}
static Entry _empty;
Entry * _parent;
@@ -342,7 +457,9 @@ public:
//! Returns top-level entry with name "vname", if doesn`t exists return entry with value "def" and set *exist to false
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0);
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0) const {return const_cast<PIConfig * >(this)->getValue(vname, def, exists);}
Entry & getValue(const PIString & vname, const PIString & def = PIString(), bool * exists = 0) const {
return const_cast<PIConfig *>(this)->getValue(vname, def, exists);
}
PICONFIG_GET_VALUE
@@ -390,66 +507,91 @@ public:
Branch getValues(const PIString & vname);
//! Set top-level entry with name "name" value to "value", type to "type" and if "write" immediate write to file. Add new entry if there is no suitable exists
//! Set top-level entry with name "name" value to "value", type to "type" and if "write" immediate write to file. Add new entry if there
//! is no suitable exists
void setValue(const PIString & name, const PIString & value, const PIString & type = "s", bool write = true);
//! Set top-level entry with name "name" value to "value", type to "l" and if "write" immediate write to file. Add new entry if there is no suitable exists
void setValue(const PIString & name, const PIStringList & value, bool write = true) {setValue(name, value.join("%|%"), "l", write);}
//! Set top-level entry with name "name" value to "value", type to "l" and if "write" immediate write to file. Add new entry if there is
//! no suitable exists
void setValue(const PIString & name, const PIStringList & value, bool write = true) { setValue(name, value.join("%|%"), "l", write); }
//! Set top-level entry with name "name" value to "value", type to "s" and if "write" immediate write to file. Add new entry if there is no suitable exists
void setValue(const PIString & name, const char * value, bool write = true) {setValue(name, PIString(value), "s", write);}
//! Set top-level entry with name "name" value to "value", type to "s" and if "write" immediate write to file. Add new entry if there is
//! no suitable exists
void setValue(const PIString & name, const char * value, bool write = true) { setValue(name, PIString(value), "s", write); }
//! Set top-level entry with name "name" value to "value", type to "b" and if "write" immediate write to file. Add new entry if there is no suitable exists
void setValue(const PIString & name, const bool value, bool write = true) {setValue(name, PIString::fromBool(value), "b", write);}
//! Set top-level entry with name "name" value to "value", type to "b" and if "write" immediate write to file. Add new entry if there is
//! no suitable exists
void setValue(const PIString & name, const bool value, bool write = true) { setValue(name, PIString::fromBool(value), "b", write); }
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is no suitable exists
void setValue(const PIString & name, const short value, bool write = true) {setValue(name, PIString::fromNumber(value), "n", write);}
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is
//! no suitable exists
void setValue(const PIString & name, const short value, bool write = true) { setValue(name, PIString::fromNumber(value), "n", write); }
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is no suitable exists
void setValue(const PIString & name, const int value, bool write = true) {setValue(name, PIString::fromNumber(value), "n", write);}
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is
//! no suitable exists
void setValue(const PIString & name, const int value, bool write = true) { setValue(name, PIString::fromNumber(value), "n", write); }
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is no suitable exists
void setValue(const PIString & name, const long value, bool write = true) {setValue(name, PIString::fromNumber(value), "n", write);}
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is
//! no suitable exists
void setValue(const PIString & name, const long value, bool write = true) { setValue(name, PIString::fromNumber(value), "n", write); }
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is no suitable exists
void setValue(const PIString & name, const uchar value, bool write = true) {setValue(name, PIString::fromNumber(value), "n", write);}
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is
//! no suitable exists
void setValue(const PIString & name, const uchar value, bool write = true) { setValue(name, PIString::fromNumber(value), "n", write); }
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is no suitable exists
void setValue(const PIString & name, const ushort value, bool write = true) {setValue(name, PIString::fromNumber(value), "n", write);}
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is
//! no suitable exists
void setValue(const PIString & name, const ushort value, bool write = true) { setValue(name, PIString::fromNumber(value), "n", write); }
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is no suitable exists
void setValue(const PIString & name, const uint value, bool write = true) {setValue(name, PIString::fromNumber(value), "n", write);}
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is
//! no suitable exists
void setValue(const PIString & name, const uint value, bool write = true) { setValue(name, PIString::fromNumber(value), "n", write); }
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is no suitable exists
void setValue(const PIString & name, const ulong value, bool write = true) {setValue(name, PIString::fromNumber(value), "n", write);}
//! Set top-level entry with name "name" value to "value", type to "n" and if "write" immediate write to file. Add new entry if there is
//! no suitable exists
void setValue(const PIString & name, const ulong value, bool write = true) { setValue(name, PIString::fromNumber(value), "n", write); }
//! Set top-level entry with name "name" value to "value", type to "f" and if "write" immediate write to file. Add new entry if there is no suitable exists
void setValue(const PIString & name, const float value, bool write = true) {setValue(name, PIString::fromNumber(value), "f", write);}
//! Set top-level entry with name "name" value to "value", type to "f" and if "write" immediate write to file. Add new entry if there is
//! no suitable exists
void setValue(const PIString & name, const float value, bool write = true) { setValue(name, PIString::fromNumber(value), "f", write); }
//! Set top-level entry with name "name" value to "value", type to "f" and if "write" immediate write to file. Add new entry if there is no suitable exists
void setValue(const PIString & name, const double value, bool write = true) {setValue(name, PIString::fromNumber(value), "f", write);}
//! Set top-level entry with name "name" value to "value", type to "f" and if "write" immediate write to file. Add new entry if there is
//! no suitable exists
void setValue(const PIString & name, const double value, bool write = true) { setValue(name, PIString::fromNumber(value), "f", write); }
//! Returns root entry
Entry & rootEntry() {return root;}
Entry & rootEntry() { return root; }
//! Returns top-level entries count
int entriesCount() const {return childCount(&root);}
int entriesCount() const { return childCount(&root); }
//! Returns if top-level entry with name "name" exists
bool isEntryExists(const PIString & name) const {return entryExists(&root, name);}
bool isEntryExists(const PIString & name) const { return entryExists(&root, name); }
//! Returns all top-level entries
Branch allTree() {Branch b; piForeach (Entry * i, root._children) b << i; b.delim = delim; return b;}
Branch allTree() {
Branch b;
piForeach(Entry * i, root._children)
b << i;
b.delim = delim;
return b;
}
//! Returns all entries without children
Branch allLeaves() {Branch b; allLeaves(b, &root); b.sort(Entry::compare); b.delim = delim; return b;}
Branch allLeaves() {
Branch b;
allLeaves(b, &root);
b.sort(Entry::compare);
b.delim = delim;
return b;
}
int entryIndex(const PIString & name);
PIString getName(uint number) {return entryByIndex(number)._name;}
PIString getValueByIndex(uint number) {return entryByIndex(number)._value;}
PIChar getType(uint number) {return entryByIndex(number)._type[0];}
PIString getComment(uint number) {return entryByIndex(number)._comment;}
PIString getName(uint number) { return entryByIndex(number)._name; }
PIString getValueByIndex(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);
@@ -470,10 +612,14 @@ public:
void writeAll();
//! Returns current tree delimiter, default "."
const PIString & delimiter() const {return delim;}
const PIString & delimiter() const { return delim; }
//! Set current tree delimiter
void setDelimiter(const PIString & d) {delim = d; setEntryDelim(&root, d); readAll();}
void setDelimiter(const PIString & d) {
delim = d;
setEntryDelim(&root, d);
readAll();
}
private:
PIConfig(const PIString & path, PIStringList dirs);
@@ -486,40 +632,75 @@ private:
void _seekToBeginDev();
PIString _readLineDev();
void _writeDev(const PIString & l);
int childCount(const Entry * e) const {int c = 0; piForeachC (Entry * i, e->_children) c += childCount(i); c += e->_children.size_s(); return c;}
int childCount(const Entry * e) const {
int c = 0;
piForeachC(Entry * 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) {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;}
Entry & entryByIndex(const int index) {Branch b = allLeaves(); if (index < 0 || index >= b.size_s()) return empty; return *(b[index]);}
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;
}
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 deleteEntry(Entry * e) {
piForeach(Entry * i, e->_children)
deleteEntry(i);
delete e;
}
PIString getPrefixFromLine(PIString line, bool * exists);
void updateIncludes();
PIString parseLine(PIString v);
void parse();
bool own_dev = false, internal = false;
PIVector<PIConfig * > includes, inc_devs;
PIVector<PIConfig *> includes, inc_devs;
Branch all_includes;
PIIODevice * dev = nullptr;
PIIODevice * dev = nullptr;
PIIOTextStream * stream = nullptr;
PIString delim;
PIStringList incdirs;
Entry root, empty;
uint lines = 0;
PIStringList other;
};
#ifdef PIP_STD_IOSTREAM
PIP_EXPORT std::ostream & operator <<(std::ostream & s, const PIConfig::Branch & v);
PIP_EXPORT std::ostream & operator <<(std::ostream & s, const PIConfig::Entry & v);
PIP_EXPORT std::ostream & operator<<(std::ostream & s, const PIConfig::Branch & v);
PIP_EXPORT std::ostream & operator<<(std::ostream & s, const PIConfig::Entry & v);
#endif
inline PICout operator <<(PICout s, const PIConfig::Branch & v) {s.saveAndSetControls(0); v.piCoutt(s, ""); s.restoreControls(); return s;}
inline PICout operator <<(PICout s, const PIConfig::Entry & v) {
inline PICout operator<<(PICout s, const PIConfig::Branch & v) {
s.saveAndSetControls(0);
v.piCoutt(s, "");
s.restoreControls();
return s;
}
inline PICout operator<<(PICout s, const PIConfig::Entry & v) {
s << v.value() << "(" << v.type() << v.comment() << ")";
return s;
}

View File

@@ -26,19 +26,18 @@
#ifndef PISERIAL_H
#define PISERIAL_H
#include "pitimer.h"
#include "piiodevice.h"
#include "pitimer.h"
//! \ingroup IO
//! \~\brief
//! \~english Serial device.
//! \~russian Последовательный порт.
class PIP_EXPORT PISerial: public PIIODevice
{
class PIP_EXPORT PISerial: public PIIODevice {
PIIODEVICE(PISerial, "ser");
public:
//! \~english Contructs an empty %PISerial
//! \~russian Создает пустой %PISerial
explicit PISerial();
@@ -49,32 +48,32 @@ public:
//! \~russian Параметры PISerial
enum Parameters {
ParityControl /*! \~english Enable parity check and generate \~russian Включить генерацию и проверку контроля чётности */ = 0x1,
ParityOdd /*! \~english Parity is odd instead of even \~russian Нечётный контроль чётности вместо чётного */ = 0x2,
TwoStopBits /*! \~english Two stop bits instead of one \~russian Два стоповых бита вместо одного */ = 0x4
ParityOdd /*! \~english Parity is odd instead of even \~russian Нечётный контроль чётности вместо чётного */ = 0x2,
TwoStopBits /*! \~english Two stop bits instead of one \~russian Два стоповых бита вместо одного */ = 0x4
};
//! \~english Speed of PISerial
//! \~russian Скорость PISerial
enum Speed {
S50 /*! 50 baud */ = 50,
S75 /*! 75 baud */ = 75,
S110 /*! 110 baud */ = 110,
S300 /*! 300 baud */ = 300,
S600 /*! 600 baud */ = 600,
S1200 /*! 1200 baud */ = 1200,
S2400 /*! 2400 baud */ = 2400,
S4800 /*! 4800 baud */ = 4800,
S9600 /*! 9600 baud */ = 9600,
S14400 /*! 14400 baud */ = 14400,
S19200 /*! 19200 baud */ = 19200,
S38400 /*! 38400 baud */ = 38400,
S57600 /*! 57600 baud */ = 57600,
S115200 /*! 115200 baud */ = 115200,
S230400 /*! 230400 baud */ = 230400,
S460800 /*! 460800 baud */ = 460800,
S500000 /*! 500000 baud */ = 500000,
S576000 /*! 576000 baud */ = 576000,
S921600 /*! 921600 baud */ = 921600,
S50 /*! 50 baud */ = 50,
S75 /*! 75 baud */ = 75,
S110 /*! 110 baud */ = 110,
S300 /*! 300 baud */ = 300,
S600 /*! 600 baud */ = 600,
S1200 /*! 1200 baud */ = 1200,
S2400 /*! 2400 baud */ = 2400,
S4800 /*! 4800 baud */ = 4800,
S9600 /*! 9600 baud */ = 9600,
S14400 /*! 14400 baud */ = 14400,
S19200 /*! 19200 baud */ = 19200,
S38400 /*! 38400 baud */ = 38400,
S57600 /*! 57600 baud */ = 57600,
S115200 /*! 115200 baud */ = 115200,
S230400 /*! 230400 baud */ = 230400,
S460800 /*! 460800 baud */ = 460800,
S500000 /*! 500000 baud */ = 500000,
S576000 /*! 576000 baud */ = 576000,
S921600 /*! 921600 baud */ = 921600,
S1000000 /*! 1000000 baud */ = 1000000,
S1152000 /*! 1152000 baud */ = 1152000,
S1500000 /*! 1500000 baud */ = 1500000,
@@ -113,66 +112,87 @@ public:
//! \~english Device manufacturer
//! \~russian Описание производителя
PIString manufacturer;
};
//! \~english Contructs %PISerial with device name "device", speed "speed" and parameters "params"
//! \~russian Создает %PISerial с именем устройства "device", скоростью "speed" и параметрами "params"
explicit PISerial(const PIString & device, PISerial::Speed speed = S115200, PIFlags<PISerial::Parameters> params = 0);
//! \~english Set both input and output speed to "speed"
//! \~russian Устанавливает скорости приема и передачи в "speed"
void setSpeed(PISerial::Speed speed) {setProperty("outSpeed", (int)speed); setProperty("inSpeed", (int)speed); applySettings();}
void setSpeed(PISerial::Speed speed) {
setProperty("outSpeed", (int)speed);
setProperty("inSpeed", (int)speed);
applySettings();
}
//! \~english Set output speed to "speed"
//! \~russian Устанавливает скорость передачи в "speed"
void setOutSpeed(PISerial::Speed speed) {setProperty("outSpeed", (int)speed); applySettings();}
void setOutSpeed(PISerial::Speed speed) {
setProperty("outSpeed", (int)speed);
applySettings();
}
//! \~english Set input speed to "speed"
//! \~russian Устанавливает скорость приема в "speed"
void setInSpeed(PISerial::Speed speed) {setProperty("inSpeed", (int)speed); applySettings();}
void setInSpeed(PISerial::Speed speed) {
setProperty("inSpeed", (int)speed);
applySettings();
}
//! \~english Set device name to "dev"
//! \~russian Устанавливает имя устройства в "dev"
void setDevice(const PIString & dev) {setPath(dev); if (isOpened()) {close(); open();};}
void setDevice(const PIString & dev) {
setPath(dev);
if (isOpened()) {
close();
open();
};
}
//! \~english Set parameters to "parameters_"
//! \~russian Устанавливает параметры в "parameters_"
void setParameters(PIFlags<PISerial::Parameters> parameters_) {setProperty("parameters", (int)parameters_); applySettings();}
void setParameters(PIFlags<PISerial::Parameters> parameters_) {
setProperty("parameters", (int)parameters_);
applySettings();
}
//! \~english Set parameter "parameter" to "on" state
//! \~russian Устанавливает параметр "parameter" в "on"
void setParameter(PISerial::Parameters parameter, bool on = true);
//! \~english Returns if parameter "parameter" is set
//! \~russian Возвращает установлен ли параметр "parameter"
bool isParameterSet(PISerial::Parameters parameter) const;
//! \~english Returns parameters
//! \~russian Возвращает параметры
PIFlags<PISerial::Parameters> parameters() const {return (PIFlags<Parameters>)(property("parameters").toInt());}
PIFlags<PISerial::Parameters> parameters() const { return (PIFlags<Parameters>)(property("parameters").toInt()); }
//! \~english Set data bits count. Valid range is from 5 to 8, befault is 8
//! \~russian Устанавливает количество бит данных. Разрешены значения от 5 до 8, по умолчанию 8
void setDataBitsCount(int bits) {setProperty("dataBitsCount", bits); applySettings();}
void setDataBitsCount(int bits) {
setProperty("dataBitsCount", bits);
applySettings();
}
//! \~english Returns data bits count
//! \~russian Возвращает количество бит данных
int dataBitsCount() const {return property("dataBitsCount").toInt();}
int dataBitsCount() const { return property("dataBitsCount").toInt(); }
//! \~english Set pin number "number" to logic level "on". Valid numbers are 4 (DTR) and 7 (RTS)
//! \~russian Устанавливает пин с номером "number" в логический уровень "on". Разрешены номера 4 (DTR) и 7 (RTS)
bool setPin(int number, bool on);
//! \~english Returns pin number "number" logic level. Valid numbers range is from 1 to 9
//! \~russian Возвращает логический уровень пина с номером "number". Разрешены номера от 1 до 9
bool isPin(int number) const;
bool setLE(bool on); // useless function, just formally
bool setLE(bool on); // useless function, just formally
bool setDTR(bool on);
bool setRTS(bool on);
bool setCTS(bool on); // useless function, just formally
@@ -196,27 +216,30 @@ public:
//! \~russian Переключает состояние передачи в break
bool setBreak(bool enabled);
void setVTime(int t) {vtime = t; applySettings();}
void setVTime(int t) {
vtime = t;
applySettings();
}
//! \~english Returns device name
//! \~russian Возвращает имя устройства
PIString device() const {return path();}
PIString device() const { return path(); }
//! \~english Returns output speed
//! \~russian Возвращает скорость передачи
PISerial::Speed outSpeed() const {return (PISerial::Speed)(property("outSpeed").toInt());}
PISerial::Speed outSpeed() const { return (PISerial::Speed)(property("outSpeed").toInt()); }
//! \~english Returns input speed
//! \~russian Возвращает скорость приема
PISerial::Speed inSpeed() const {return (PISerial::Speed)(property("inSpeed").toInt());}
int VTime() const {return vtime;}
PISerial::Speed inSpeed() const { return (PISerial::Speed)(property("inSpeed").toInt()); }
int VTime() const { return vtime; }
//! \~english Discard all buffered input and output data
//! \~russian Откидывает все буферизированные данные для передачи и приема
virtual void flush() override;
int read(void * read_to, int max_size) {return readDevice(read_to, max_size);}
int read(void * read_to, int max_size) { return readDevice(read_to, max_size); }
//! \~english Read from device no more "max_size" bytes into "read_to" with "timeout_ms" timeout
//! \~russian Читает из устройства не более "max_size" байт в "read_to" с таймаутом "timeout_ms"
@@ -236,14 +259,14 @@ public:
//! \~english Write to device byte array "data". Returns if sended bytes count = size of "data"
//! \~russian Пишет в порт байтовый массив "data". Возвращает если количество записанных байт = размер "data"
bool send(const PIByteArray & data) {return send(data.data(), data.size_s());}
bool send(const PIByteArray & data) { return send(data.data(), data.size_s()); }
void interrupt() override;
//! \~english Returns all available speeds for serial devices
//! \~russian Возвращает все возможные скорости для устройств
static PIVector<int> availableSpeeds();
//! \~english Returns all available system devices path. If "test" each device will be tried to open
//! \~russian Возвращает пути всех доступных устройств в системе. Если "test", то каждое устройство будет опробовано на открытие
static PIStringList availableDevices(bool test = false);
@@ -258,29 +281,29 @@ public:
//! \~english device, default ""
//! \~russian устройство, по умолчанию ""
string device;
//! \~english input/output speed, default 115200
//! \~russian скорость чтения/записи, по умолчанию 115200
int speed;
//! \~english dataBitsCount, default 8
//! \~russian количесво бит данных, по умолчанию 8
int dataBitsCount;
//! \~english parityControl, default false
//! \~russian контроль четности, по умолчанию false
bool parityControl;
//! \~english parityOdd, default false
//! \~russian нечётный контроль четности, по умолчанию false
bool parityOdd;
//! \~english twoStopBits, default false
//! \~russian два стоповых бита, по умолчанию false
bool twoStopBits;
#endif
//! \}
//! \}
protected:
PIString constructFullPathDevice() const override;
void configureFromFullPathDevice(const PIString & full_path) override;
@@ -294,10 +317,10 @@ protected:
//! \~russian Базовое чтение
ssize_t readDevice(void * read_to, ssize_t max_size) override;
ssize_t writeDevice(const void * data, ssize_t max_size) override;
DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Sequential;}
DeviceInfoFlags deviceInfoFlags() const override { return PIIODevice::Sequential; }
//! Executes when any read function was successful. Default implementation does nothing
virtual void received(const void * data, int size) {;}
virtual void received(const void * data, int size) { ; }
void construct();
void applySettings();
@@ -313,36 +336,45 @@ protected:
int fd = -1, vtime = 10;
std::atomic_bool sending;
PITimeMeasurer tm_;
};
//! \relatesalso PICout
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
inline PICout operator <<(PICout s, const PISerial::DeviceInfo & v) {
inline PICout operator<<(PICout s, const PISerial::DeviceInfo & v) {
s << v.path << " (" << v.id() << ", \"" << v.manufacturer << "\", \"" << v.description << "\")";
return s;
}
//! \~english Compare operator
//! \~russian Оператор сравнения
inline bool operator ==(const PISerial::DeviceInfo & v0, const PISerial::DeviceInfo & v1) {return v0.path == v1.path;}
inline bool operator==(const PISerial::DeviceInfo & v0, const PISerial::DeviceInfo & v1) {
return v0.path == v1.path;
}
//! \~english Compare operator
//! \~russian Оператор сравнения
inline bool operator !=(const PISerial::DeviceInfo & v0, const PISerial::DeviceInfo & v1) {return v0.path != v1.path;}
inline bool operator!=(const PISerial::DeviceInfo & v0, const PISerial::DeviceInfo & v1) {
return v0.path != v1.path;
}
//! \relatesalso PIBinaryStream
//! \~english Store operator.
//! \~russian Оператор сохранения.
BINARY_STREAM_WRITE(PISerial::DeviceInfo) {s << v.vID << v.pID << v.path << v.description << v.manufacturer; return s;}
BINARY_STREAM_WRITE(PISerial::DeviceInfo) {
s << v.vID << v.pID << v.path << v.description << v.manufacturer;
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Restore operator.
//! \~russian Оператор извлечения.
BINARY_STREAM_READ (PISerial::DeviceInfo) {s >> v.vID >> v.pID >> v.path >> v.description >> v.manufacturer; return s;}
BINARY_STREAM_READ(PISerial::DeviceInfo) {
s >> v.vID >> v.pID >> v.path >> v.description >> v.manufacturer;
return s;
}
#endif // PISERIAL_H