git-svn-id: svn://db.shs.com.ru/pip@283 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2016-11-23 15:10:40 +00:00
parent 8f6e7d2cae
commit e66e78ac16
29 changed files with 973 additions and 973 deletions

View File

@@ -1,20 +1,20 @@
/*
PIP - Platform Independent Primitives
Config parser
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
PIP - Platform Independent Primitives
Config parser
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piconfig.h"
@@ -24,13 +24,13 @@
/*! \class PIConfig
* \brief Configuration file
* \details This class provide handle access to configuration file.
*
*
* \section PIConfig_sec0 Synopsis
* PIConfig reads configuration file and create internal dendritic
* representation of all entries of this file. You can easily read
* some values and write new.
* \image html piconfig.png
*
*
* %PIConfig supports also INI-style files with sections "[section]".
* In this case line with section name interpret as prefix to the next
* lines. For example, these configs are equal:
@@ -46,12 +46,12 @@
* []
* debug = true
* \endcode
*
*
* \section PIConfig_sec1 Concepts
* Each node of internal tree has type PIConfig::Entry. %PIConfig
* has one root element \a rootEntry(). Any entry of configuration file is a
* child of this element.
*
*
*/
/*! \class PIConfig::Entry
@@ -63,7 +63,7 @@
* * value
* * type
* * comment
*
*
* Each property is a PIString. These properties forms from text line with
* format: \code{.cpp} <name> = <value> #<type> <comment> \endcode
* Type and comment are optional fields. Type is a single letter immediately
@@ -75,14 +75,14 @@
* \a PIConfig::Branch. If there is no suitable entry to return, reference to
* internal instance of %PIConfig::Entry with "default" value will be returned.
* \snippet piconfig.cpp PIConfig::Entry
*
*
*/
/*! \class PIConfig::Branch
* \brief %Branch is a list of entries of configuration file
* \details %Branch provides some features to get entries lists.
* \snippet piconfig.cpp PIConfig::Branch
*
*
*/
@@ -735,7 +735,7 @@ void PIConfig::parse() {
type = "s";
comm = "";
}
//name = str.left(ind).trimmed();
tree = (prefix + str.left(ind).trimmed()).split(delim);
if (tree.front() == "include") {

View File

@@ -2,22 +2,22 @@
* \brief Configuration parser and writer
*/
/*
PIP - Platform Independent Primitives
Configuration parser and writer
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
PIP - Platform Independent Primitives
Configuration parser and writer
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PICONFIG_H
@@ -57,21 +57,21 @@ class PIP_EXPORT PIConfig
friend class Entry;
friend class Branch;
public:
//! Contructs and read configuration file at path "path" in mode "mode"
PIConfig(const PIString & path, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! Contructs and read configuration string "string" in mode "mode"
PIConfig(PIString * string, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! Contructs and read configuration from custom device "device" in mode "mode"
PIConfig(PIIODevice * device = 0, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
~PIConfig();
class Entry;
class PIP_EXPORT Branch: public PIVector<Entry * > {
friend class PIConfig;
friend class Entry;
@@ -79,11 +79,11 @@ public:
friend PICout operator <<(PICout s, const Branch & v);
public:
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);}
PICONFIG_GET_VALUE
Branch allLeaves();
Branch getValues(const PIString & name);
Branch getLeaves();
@@ -91,212 +91,212 @@ public:
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;}
//void clear() {piForeach (Entry * i, *this) delete i; PIVector<Entry * >::clear();}
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 coutt(std::ostream & s, const PIString & p) const {piForeachC (Entry * i, *this) i->coutt(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;}
//! Returns children count
int childCount() const {return _children.size_s();}
//! Returns children as \a PIConfig::Branch
Branch & children() const {_children.delim = delim; return _children;}
//! Returns child at index "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;}
//! 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;}
//! Returns \b true if there is no children
bool isLeaf() const {return _children.isEmpty();}
//! Returns name
const PIString & name() const {return _name;}
//! Returns value
const PIString & value() const {return _value;}
//! Returns type
const PIString & type() const {return _type;}
//! Returns 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;}
//! Set name to "value" and returns 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;}
//! Set comment to "value" and returns 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;}
//! Set value to "value" and returns this. Type is set to "l"
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;}
//! 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;}
//! 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;}
//! 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;}
//! 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;}
//! 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;}
//! 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;}
//! 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;}
//! 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;}
//! 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;}
//! 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;}
//! 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;}
/** \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);}
PICONFIG_GET_VALUE
//! \fn Entry & getValue(const PIString & vname, const char * def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const char * def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const PIStringList & def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const bool def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const short def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const int def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const long def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const uchar def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const ushort def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const uint def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const ulong def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const float def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const double def, bool * exists = 0)
//! \brief Returns entry with name "vname" and default value "def"
//! Find all entries with names with substrings "vname" and returns them as \a PIConfig::Branch
Branch getValues(const PIString & vname);
//! 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);}
//! Convertion to boolean
operator bool() {return _value.toBool();}
//! Convertion to char
operator char() {return (_value.isEmpty() ? 0 : _value[0].toAscii());}
//! Convertion to short
operator short() {return _value.toShort();}
//! Convertion to int
operator int() {return _value.toInt();}
//! Convertion to long
operator long() {return _value.toLong();}
//! Convertion to uchar
operator uchar() {return _value.toInt();}
//! Convertion to ushort
operator ushort() {return _value.toShort();}
//! Convertion to uint
operator uint() {return _value.toInt();}
//! Convertion to ulong
operator ulong() {return _value.toLong();}
//! Convertion to float
operator float() {return _value.toFloat();}
//! Convertion to double
operator double() {return _value.toDouble();}
//! Convertion to PIString
operator PIString() {return _value;}
//! Convertion to PIStringList
operator PIStringList() {return _value.split("%|%");}
private:
typedef PIConfig::Entry * EntryPtr;
static int compare(const EntryPtr * f, const EntryPtr * s) {return (*f)->_line == (*s)->_line ? 0 : (*f)->_line < (*s)->_line ? -1 : 1;}
@@ -306,7 +306,7 @@ public:
void coutt(std::ostream & s, const PIString & p) const {PIString nl = p + " "; if (!_value.isEmpty()) s << p << _name << " = " << _value << std::endl; else std::cout << p << _name << std::endl; piForeachC (Entry * i, _children) i->coutt(s, nl);}
void piCoutt(PICout s, const PIString & p) const {PIString nl = p + " "; if (!_value.isEmpty()) s << p << _name << " = " << _value << PICoutManipulators::NewLine; else std::cout << p << _name << std::endl; piForeachC (Entry * i, _children) i->piCoutt(s, nl);}
void deleteBranch() {piForeach (Entry * i, _children) {i->deleteBranch(); delete i;}}
static Entry _empty;
Entry * _parent;
mutable Branch _children;
@@ -320,151 +320,151 @@ public:
PIString delim;
int _line;
};
//! Read configuration file at path "path" in mode "mode"
bool open(const PIString & path, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! Read configuration string "string" in mode "mode"
bool open(PIString * string, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);\
bool isOpened() const;
//! 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);}
PICONFIG_GET_VALUE
//! \fn Entry & getValue(const PIString & vname, const char * def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const char * def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const PIStringList & def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const bool def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const short def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const int def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const long def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const uchar def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const ushort def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const uint def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const ulong def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const float def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! \fn Entry & getValue(const PIString & vname, const double def, bool * exists = 0)
//! \brief Returns top-level entry with name "vname" and default value "def"
//! Returns top-level entries with names with substrings "vname"
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
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 "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 "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 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 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 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 double value, bool write = true) {setValue(name, PIString::fromNumber(value), "f", write);}
//! Returns root entry
Entry & rootEntry() {return root;}
//! Returns top-level entries count
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);}
//! Returns all top-level entries
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;}
int entryIndex(const PIString & name);
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);
void setValue(uint number, const PIString & value, bool write = true);
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);
//! Remove all tree and device content
void clear();
//! Parse device and build internal tree
void readAll();
//! Write all internal tree to device
void writeAll();
//! Returns current tree delimiter, default "."
const PIString & delimiter() const {return delim;}
//! Set current tree delimiter
void setDelimiter(const PIString & d) {delim = d; setEntryDelim(&root, d); readAll();}
private:
PIConfig(const PIString & path, PIStringList dirs);
void _init();
@@ -486,7 +486,7 @@ private:
void updateIncludes();
PIString parseLine(PIString v);
void parse();
bool own_dev, internal;
PIVector<PIConfig * > includes, inc_devs;
Branch all_includes;
@@ -524,7 +524,7 @@ T readDeviceSetting(const PIString & name, const T & def, const PIConfig::Entry
return ret;
}
return em->getValue(name, def);
}
#endif // PICONFIG_H

View File

@@ -1,20 +1,20 @@
/*
PIP - Platform Independent Primitives
File
PIP - Platform Independent Primitives
File
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pifile.h"
@@ -64,14 +64,14 @@
/*! \class PIFile
* \brief Local file
*
*
* \section PIFile_sec0 Synopsis
* This class provide access to local file. You can manipulate
* binary content or use this class as text stream. To binary
* access there are function \a read(), \a write(), and many
* \a writeBinary() functions. For write variables to file in
* their text representation threr are many "<<" operators.
*
*
* \section PIFile_sec1 Position
* Each opened file has a read/write position - logical position
* in the file content you read from or you write to. You can
@@ -79,10 +79,10 @@
* \a seek(llong position) move position to position "position",
* \a seekToBegin() move position to the begin of file,
* \a seekToEnd() move position to the end of file.
*
*
*/
REGISTER_DEVICE(PIFile);
REGISTER_DEVICE(PIFile)
PIString PIFile::FileInfo::name() const {
@@ -122,14 +122,14 @@ PIString PIFile::FileInfo::dir() const {
PIFile::PIFile(): PIIODevice() {
fd = 0;
fd = 0;
fdi = -1;
setPrecision(5);
}
PIFile::PIFile(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(path, mode) {
fd = 0;
fd = 0;
fdi = -1;
setPrecision(5);
if (!path.isEmpty())
@@ -138,8 +138,8 @@ PIFile::PIFile(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(p
PIFile::PIFile(const PIFile & other) {
fd = 0;
fdi = -1;
fd = 0;
fdi = -1;
setPrecision(other.prec_);
setPath(other.path());
mode_ = other.mode_;
@@ -477,7 +477,7 @@ PIFile::FileInfo PIFile::fileInfo(const PIString & path) {
_stat_call_(path.data(), &fs);
int mode = fs.st_mode;
ret.size = fs.st_size;
ret.id_user = fs.st_uid;
ret.id_user = fs.st_uid;
ret.id_group = fs.st_gid;
#ifdef ANDROID
ret.time_access = PIDateTime::fromSystemTime(PISystemTime(fs.st_atime, fs.st_atime_nsec));

View File

@@ -1,20 +1,20 @@
/*
PIP - Platform Independent Primitives
Packets extractor
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
PIP - Platform Independent Primitives
Packets extractor
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pipacketextractor.h"
@@ -27,14 +27,14 @@
* This class implements packet recognition by various algorithms and custom
* validating from data stream. Stream is formed from child %PIIODevice
* passed from contructor or with function \a setDevice().
*
*
* \section PIPacketExtractor_work Principle of work
* %PIPacketExtractor works with child %PIIODevice. \a read and \a write
* functions directly call child device functions. You should start threaded
* read of \b extractor (not child device) to proper work. Extractor read data
* from child device, try to detect packet from readed data and raise
* \a packetReceived() event on success.
*
*
* \section PIPacketExtractor_algorithms Algorithms
* There are 6 algorithms: \n
* * PIPacketExtractor::None \n
@@ -63,13 +63,13 @@
* * PIPacketExtractor::Timeout \n
* Wait for first read, then read for \a timeout() milliseconds and raise
* \a packetReceived() event. \n
*
*
* \section PIPacketExtractor_control Control validating
* There are three parameters:
* * header content
* * header size
* * payload size
*
*
* Extractor can detect packet with compare your header with readed data.
* It is default implementation of function \a packetHeaderValidate().
* If header validating passed, function \a packetValidate() will be called.
@@ -77,10 +77,10 @@
* and takes next header. If both functions returns \b true extractor shifts
* by whole packet size.
* \image html packet_detection.png
*
*
* */
REGISTER_DEVICE(PIPacketExtractor);
REGISTER_DEVICE(PIPacketExtractor)
PIPacketExtractor::PIPacketExtractor(PIIODevice * device_, PIPacketExtractor::SplitMode mode) {

View File

@@ -2,22 +2,22 @@
* \brief Packets extractor
*/
/*
PIP - Platform Independent Primitives
Packets extractor
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
PIP - Platform Independent Primitives
Packets extractor
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@@ -34,7 +34,7 @@ class PIP_EXPORT PIPacketExtractor: public PIIODevice
PIIODEVICE(PIPacketExtractor)
friend class PIConnection;
public:
//! Extract algorithms
enum SplitMode {
None /** No data processing */ ,
@@ -44,91 +44,91 @@ public:
Size /** Detect packets with \a packetSize() */ ,
Timeout /** Wait for first read, then read for \a timeout() milliseconds */
};
//! Contructs extractor with child device "device_" and extract algorithm "mode"
PIPacketExtractor(PIIODevice * device_ = 0, SplitMode mode = None);
virtual ~PIPacketExtractor() {stop();}
//! Returns child %device
PIIODevice * device() {return dev;}
//! Set child %device to "device_"
void setDevice(PIIODevice * device_);
//! Returns buffer size
int bufferSize() const {return buffer_size;}
//! Set buffer size to "new_size" bytes, should be at least greater than whole packet size
void setBufferSize(int new_size) {buffer_size = new_size; buffer.resize(buffer_size); memset(buffer.data(), 0, buffer.size());}
void setHeaderCheckSlot(PacketExtractorCheckFunc f) {ret_func_header = f;}
void setFooterCheckSlot(PacketExtractorCheckFunc f) {ret_func_footer = f;}
void setPayloadCheckSlot(ReadRetFunc f) {ret_func_ = f;}
//! Set extract algorithm
void setSplitMode(SplitMode mode) {setProperty("splitMode", int(mode)); mode_ = mode;}
//! Set payload size, used for PIPacketExtractor::Header and PIPacketExtractor::Footer algorithms
void setPayloadSize(int size);
//! Set header data, used for PIPacketExtractor::Header and PIPacketExtractor::HeaderAndFooter algorithms
void setHeader(const PIByteArray & data);
//! Set footer data, used for PIPacketExtractor::Footer and PIPacketExtractor::HeaderAndFooter algorithms
void setFooter(const PIByteArray & data);
//! Set packet size, used for PIPacketExtractor::Size algorithm
void setPacketSize(int size) {setProperty("packetSize", size); packetSize_ = size;}
//! Set timeout in milliseconds, used for PIPacketExtractor::Timeout algorithm
void setTimeout(double msecs) {setProperty("timeout", msecs); time_ = msecs;}
//! Returns current extract algorithm
SplitMode splitMode() const {return (SplitMode)(property("splitMode").toInt());}
//! Returns current payload size, used for PIPacketExtractor::Header and PIPacketExtractor::Footer algorithms
int payloadSize() const {return property("payloadSize").toInt();}
//! Returns current header data, used for PIPacketExtractor::Header and PIPacketExtractor::HeaderAndFooter algorithms
PIByteArray header() const {return src_header;}
//! Returns current footer data, used for PIPacketExtractor::Footer and PIPacketExtractor::HeaderAndFooter algorithms
PIByteArray footer() const {return src_footer;}
//! Returns current packet size, used for PIPacketExtractor::Size algorithm
int packetSize() const {return property("packetSize").toInt();}
//! Returns current timeout in milliseconds, used for PIPacketExtractor::Timeout algorithm
double timeout() const {return property("timeout").toDouble();}
//! Returns missed by validating functions bytes count
ullong missedBytes() const {return missed;}
// //! Returns missed by validating functions packets count, = missedBytes() / packetSize
ullong missedPackets() const {/*if (packetSize_hf == 0) return missed; return missed / packetSize_hf*/; return missed_packets;}
//! Returns pointer to \a missedBytes() count. Useful for output to PIConsole
const ullong * missedBytes_ptr() const {return &missed;}
// //! Returns pointer to \a missedPackets() count. Useful for output to PIConsole
const ullong * missedPackets_ptr() const {return &missed_packets;}
//! Directly call \a read() function of child %device
int read(void * read_to, int max_size) {if (dev == 0) return -1; return dev->read(read_to, max_size);}
//! Directly call \a write() function of child %device
int write(const void * data, int max_size) {if (dev == 0) return -1; return dev->write(data, max_size);}
PIString constructFullPath() const;
EVENT2(packetReceived, uchar * , data, int, size)
//! \events
//! \{
@@ -138,7 +138,7 @@ public:
//! \}
protected:
/** \brief Function to validate header
* \param src Your header content
* \param rec Received header
@@ -152,7 +152,7 @@ protected:
* \param size Footer size
* \details Default implementation returns by-byte "src" with "rec" compare result */
virtual bool validateFooter(uchar * src, uchar * rec, int size) {if (ret_func_footer != 0) return ret_func_footer(ret_data_, src, rec, size); for (int i = 0; i < size; ++i) if (src[i] != rec[i]) return false; return true;}
/** \brief Function to validate payload
* \param rec Received payload
* \param size payload size

View File

@@ -124,7 +124,7 @@
*
*/
REGISTER_DEVICE(PISerial);
REGISTER_DEVICE(PISerial)
PRIVATE_DEFINITION_START(PISerial)

View File

@@ -2,14 +2,14 @@
#include "piconfig.h"
#ifdef PIP_USB
# ifdef WINDOWS
# include <lusb0_usb.h>
# else
# include <usb.h>
# endif
# ifdef WINDOWS
# include <lusb0_usb.h>
# else
# include <usb.h>
# endif
#endif
REGISTER_DEVICE(PIUSB);
REGISTER_DEVICE(PIUSB)
PIUSB::PIUSB(ushort vid, ushort pid): PIIODevice("", ReadWrite) {
@@ -218,7 +218,7 @@ bool PIUSB::openDevice() {
}
if (!desc_.configurations.isEmpty())
conf_ = desc_.configurations.front();
struct usb_interface_descriptor * is = dev->config->interface->altsetting;
int epn = is->bNumEndpoints;
eps.clear();
@@ -229,7 +229,7 @@ bool PIUSB::openDevice() {
if (eps.back().direction == Endpoint::Write && (eps.back().address == ep_write.address || ep_write.address == 0)) ep_write = eps.back();
if (eps.back().direction == Endpoint::Read && (eps.back().address == ep_read.address || ep_read.address == 0)) ep_read = eps.back();
}
//piCoutObj << "Device found at address:" << "Bus: " << dev->bus->dirname << ", Device: " << dev->filename;
found = true;
break;
@@ -249,7 +249,7 @@ bool PIUSB::openDevice() {
return false;
}// else piCoutObj << "ok";
//usb_reset(hdev);
//usb_set_configuration(hdev, 1);
//usb_set_altinterface(hdev, 0);
@@ -272,7 +272,7 @@ bool PIUSB::openDevice() {
return false;
} // else piCoutObj << "ok";
interface_claimed = intefrace_;
return true;
#else
return false;

View File

@@ -2,22 +2,22 @@
* \brief USB device
*/
/*
PIP - Platform Independent Primitives
USB, based on libusb
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
PIP - Platform Independent Primitives
USB, based on libusb
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIUSB_H
@@ -33,18 +33,18 @@ class PIP_EXPORT PIUSB: public PIIODevice
public:
PIUSB(ushort vid = 0, ushort pid = 0);
~PIUSB() {closeDevice();}
struct Endpoint {
Endpoint(uchar a = 0, uchar at = 0, ushort mps = 0) {address = a; attributes = at; max_packet_size = mps; parse();}
enum Direction {Write = 0, Read = 1};
enum TransferType {Control = 0, Isochronous = 1, Bulk = 2, Interrupt = 3};
enum SynchronisationType {NoSynchonisation= 0, Asynchronous = 2, Adaptive = 1, Synchronous = 3};
enum UsageType {DataEndpoint = 0, FeedbackEndpoint = 2, ExplicitFeedbackDataEndpoint = 1};
void parse();
bool isNull() const {return address == 0;}
uchar address;
uchar attributes;
ushort max_packet_size;
@@ -53,7 +53,7 @@ public:
SynchronisationType synchronisation_type;
UsageType usage_type;
};
struct Interface {
Interface() {index = value_to_select = class_code = subclass_code = protocol_code = 0;}
uchar index;
@@ -74,7 +74,7 @@ public:
bool remote_wakeup;
PIVector<PIUSB::Interface> interfaces;
};
struct Descriptor {
Descriptor() {memset(this, 0, sizeof(Descriptor));}
ushort usb_spec_number;
@@ -90,45 +90,45 @@ public:
uchar index_serial;
PIVector<PIUSB::Configuration> configurations;
};
const PIUSB::Descriptor & currentDescriptor() const {return desc_;}
const PIUSB::Configuration & currentConfiguration() const {return conf_;}
const PIUSB::Interface & currentInterface() const {return iface_;}
ushort vendorID() const {return vid_;}
ushort productID() const {return pid_;}
int deviceNumber() const {return property("deviceNumber").toInt();}
int timeoutRead() const {return property("timeoutRead").toInt();}
int timeoutWrite() const {return property("timeoutWrite").toInt();}
const PIUSB::Endpoint & endpointRead() const {return ep_read;}
const PIUSB::Endpoint & endpointWrite() const {return ep_write;}
const PIVector<PIUSB::Endpoint> & endpoints() const {return eps;}
PIVector<PIUSB::Endpoint> endpointsRead();
PIVector<PIUSB::Endpoint> endpointsWrite();
PIUSB::Endpoint getEndpointByAddress(uchar address);
void setVendorID(ushort vid) {vid_ = vid; setPath(PIString::fromNumber(vid_, 16).expandLeftTo(4, "0") + ":" + PIString::fromNumber(pid_, 16).expandLeftTo(4, "0"));}
void setProductID(ushort pid) {pid_ = pid; setPath(PIString::fromNumber(vid_, 16).expandLeftTo(4, "0") + ":" + PIString::fromNumber(pid_, 16).expandLeftTo(4, "0"));}
bool setConfiguration(uchar value);
bool setInterface(uchar value);
void setEndpointRead(const PIUSB::Endpoint & ep) {ep_read = ep;}
void setEndpointWrite(const PIUSB::Endpoint & ep) {ep_write = ep;}
void setDeviceNumber(int dn) {setProperty("deviceNumber", dn);}
void setTimeoutRead(int t) {setProperty("timeoutRead", t);}
void setTimeoutWrite(int t) {setProperty("timeoutWrite", t);}
int read(void * read_to, int max_size);
int write(const void * data, int max_size);
int controlWrite(const void * data, int max_size);
void flush();
PIString constructFullPath() const;
protected:
PIString fullPathPrefix() const {return "usb";}
void configureFromFullPath(const PIString & full_path);
@@ -136,7 +136,7 @@ protected:
//bool init();
bool openDevice();
bool closeDevice();
PIVector<PIUSB::Endpoint> eps;
ushort vid_, pid_;
int intefrace_, timeout_r, timeout_w;
@@ -146,7 +146,7 @@ protected:
Configuration conf_;
Interface iface_;
usb_dev_handle * hdev;
};
PICout operator <<(PICout s, const PIUSB::Endpoint & v);