18.03.2013 - Bug fixes, add in/out speed diagnostic to PIProtocol, fixed PIConsole tab switch segfault, PIObject EVENT / EVENT_HANDLER mechanism update - new EVENT macros that use EVENT_HANDLER with raiseEvent implementation.

This allow compile check event for CONNECT and use EVENT as CONNECT target, also raise event now is simple execute EVENT function.
This commit is contained in:
peri4
2013-03-18 12:07:44 +04:00
parent cfc5eed75e
commit 66c53a27fc
72 changed files with 4407 additions and 960 deletions

123
pistring.h Executable file → Normal file
View File

@@ -1,7 +1,7 @@
/*
PIP - Platform Independent Primitives
String
Copyright (C) 2012 Ivan Pelipenko peri4ko@gmail.com
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com
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
@@ -38,19 +38,23 @@ public:
PIString & operator +=(const string & str) {appendFromChars(str.c_str(), str.length()); return *this;}
PIString & operator +=(const PIByteArray & ba) {appendFromChars((const char * )ba.data(), ba.size_s()); return *this;}
PIString & operator +=(const PIString & str);
#ifdef HAS_LOCALE
PIString & operator +=(const wstring & str);
#endif
//PIString(const char c) {*this += c;}
PIString(const PIChar c) {piMonitor.strings++; piMonitor.containers--;*this += c;}
PIString(const char * str) {piMonitor.strings++; piMonitor.containers--;*this += str;}
PIString(const wchar_t * str) {piMonitor.strings++; piMonitor.containers--;*this += str;}
PIString(const string & str) {piMonitor.strings++; piMonitor.containers--;*this += str;}
PIString(const wstring & str) {piMonitor.strings++; piMonitor.containers--;*this += str;}
PIString(const PIByteArray & ba) {piMonitor.strings++; piMonitor.containers--;*this += ba;}
PIString(const char * str, const int len) {piMonitor.strings++; piMonitor.containers--;*this += string(str, len);}
PIString(const int len, const char c) {piMonitor.strings++; piMonitor.containers--;for (int i = 0; i < len; ++i) push_back(c);}
PIString(const int len, const PIChar & c) {piMonitor.strings++; piMonitor.containers--;for (int i = 0; i < len; ++i) push_back(c);}
PIString(const PIString & str) {piMonitor.strings++; piMonitor.containers--;*this += str;}
PIString(const PIChar c) {reserve(256); piMonitor.strings++; piMonitor.containers--; *this += c;}
PIString(const char * str) {reserve(256); piMonitor.strings++; piMonitor.containers--; *this += str;}
PIString(const wchar_t * str) {reserve(256); piMonitor.strings++; piMonitor.containers--; *this += str;}
PIString(const string & str) {reserve(256); piMonitor.strings++; piMonitor.containers--; *this += str;}
#ifdef HAS_LOCALE
PIString(const wstring & str) {reserve(256); piMonitor.strings++; piMonitor.containers--; *this += str;}
#endif
PIString(const PIByteArray & ba) {reserve(256); piMonitor.strings++; piMonitor.containers--; *this += ba;}
PIString(const char * str, const int len) {reserve(256); piMonitor.strings++; piMonitor.containers--; *this += string(str, len);}
PIString(const int len, const char c) {reserve(256); piMonitor.strings++; piMonitor.containers--; for (int i = 0; i < len; ++i) push_back(c);}
PIString(const int len, const PIChar & c) {reserve(256); piMonitor.strings++; piMonitor.containers--; for (int i = 0; i < len; ++i) push_back(c);}
PIString(const PIString & str) {reserve(256); piMonitor.strings++; piMonitor.containers--; *this += str;}
~PIString() {piMonitor.strings--; piMonitor.containers++;}
operator const char*() {return data();}
@@ -133,16 +137,18 @@ public:
int lengthAscii() const;
const char * data() const;
const string stdString() const {return convertToStd();}
#ifdef HAS_LOCALE
wstring stdWString() const {return convertToWString();}
PIByteArray toByteArray() {string s(convertToStd()); return PIByteArray(s.c_str(), s.length());}
#endif
PIByteArray toByteArray() const {const char * d = data(); return PIByteArray(d, lengthAscii());}
PIStringList split(const PIString & delim) const;
PIString toUpperCase() const;
PIString toLowerCase() const;
#ifdef QNX
PIString toNativeDecimalPoints() const {PIString s(*this); return s;}
#else
#ifdef HAS_LOCALE
PIString toNativeDecimalPoints() const {PIString s(*this); if (currentLocale == 0) return s; return s.replaceAll(".", currentLocale->decimal_point);}
#else
PIString toNativeDecimalPoints() const {PIString s(*this); return s;}
#endif
int find(const char str, const int start = 0) const;
@@ -157,30 +163,43 @@ public:
int length() const {return size();}
bool isEmpty() const {return (size() == 0 || *this == "");}
bool toBool() const {PIString s(*this); if (atof(s.toNativeDecimalPoints().data()) > 0. || s.trimmed().toLowerCase() == "true") return true; return false;}
bool toBool() const {PIString s(*this); if (atof(s.toNativeDecimalPoints().data()) > 0. || s.trimmed().toLowerCase() == "true" || s.trimmed().toLowerCase() == "on") return true; return false;}
char toChar() const;
short toShort() const;
int toInt() const;
long toLong() const;
llong toLLong() const;
short toShort(int base = -1, bool * ok = 0) const {return short(toNumberBase(*this, base, ok));}
ushort toUShort(int base = -1, bool * ok = 0) const {return ushort(toNumberBase(*this, base, ok));}
int toInt(int base = -1, bool * ok = 0) const {return int(toNumberBase(*this, base, ok));}
uint toUInt(int base = -1, bool * ok = 0) const {return uint(toNumberBase(*this, base, ok));}
long toLong(int base = -1, bool * ok = 0) const {return long(toNumberBase(*this, base, ok));}
ulong toULong(int base = -1, bool * ok = 0) const {return ulong(toNumberBase(*this, base, ok));}
llong toLLong(int base = -1, bool * ok = 0) const {return toNumberBase(*this, base, ok);}
ullong toULLong(int base = -1, bool * ok = 0) const {return ullong(toNumberBase(*this, base, ok));}
float toFloat() const {PIString s(*this); return (float)atof(s.toNativeDecimalPoints().data());}
double toDouble() const {PIString s(*this); return atof(s.toNativeDecimalPoints().data());}
ldouble toLDouble() const {PIString s(*this); return atof(s.toNativeDecimalPoints().data());}
//inline PIString & setNumber(const char value) {clear(); *this += itos(value); return *this;}
PIString & setNumber(const int value) {clear(); *this += itos(value); return *this;}
PIString & setNumber(const short value) {clear(); *this += itos(value); return *this;}
PIString & setNumber(const long value) {clear(); *this += ltos(value); return *this;}
PIString & setNumber(const short value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
PIString & setNumber(const ushort value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
PIString & setNumber(const int value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
PIString & setNumber(const uint value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
PIString & setNumber(const long value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
PIString & setNumber(const ulong value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
PIString & setNumber(const llong value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
PIString & setNumber(const ullong value, int base = 10, bool * ok = 0) {clear(); *this += PIString::fromNumber(value, base, ok); return *this;}
PIString & setNumber(const float value) {clear(); *this += ftos(value); return *this;}
PIString & setNumber(const double value) {clear(); *this += dtos(value); return *this;}
PIString & setNumber(const ldouble value) {clear(); *this += dtos(value); return *this;}
PIString & setReadableSize(long bytes);
//inline static PIString fromNumber(const char value) {return PIString(itos(value));}
static PIString fromNumber(const int value) {return PIString(itos(value));}
static PIString fromNumber(const uint value) {return PIString(itos(value));}
static PIString fromNumber(const short value) {return PIString(itos(value));}
static PIString fromNumber(const long value) {return PIString(ltos(value));}
static PIString fromNumber(const short value, int base = 10, bool * ok = 0) {return fromNumberBase(llong(value), base, ok);}
static PIString fromNumber(const ushort value, int base = 10, bool * ok = 0) {return fromNumberBase(llong(value), base, ok);}
static PIString fromNumber(const int value, int base = 10, bool * ok = 0) {return fromNumberBase(llong(value), base, ok);}
static PIString fromNumber(const uint value, int base = 10, bool * ok = 0) {return fromNumberBase(llong(value), base, ok);}
static PIString fromNumber(const long value, int base = 10, bool * ok = 0) {return fromNumberBase(llong(value), base, ok);}
static PIString fromNumber(const ulong value, int base = 10, bool * ok = 0) {return fromNumberBase(llong(value), base, ok);}
static PIString fromNumber(const llong value, int base = 10, bool * ok = 0) {return fromNumberBase(value, base, ok);}
static PIString fromNumber(const ullong value, int base = 10, bool * ok = 0) {return fromNumberBase(llong(value), base, ok);}
static PIString fromNumber(const float value) {return PIString(ftos(value));}
static PIString fromNumber(const double value) {return PIString(dtos(value));}
static PIString fromNumber(const ldouble value) {return PIString(dtos(value));}
@@ -188,9 +207,52 @@ public:
static PIString readableSize(long bytes) {PIString s; s.setReadableSize(bytes); return s;}
private:
static const char toBaseN[];
static const int fromBaseN[];
static PIString fromNumberBase(const llong value, int base = 10, bool * ok = 0) {
if (base < 2 || base > 40) {if (ok != 0) *ok = false; return PIString();}
if (ok != 0) *ok = true;
if (base == 10) return itos(value);
PIString ret;
llong v = value < 0 ? -value : value, cn, b = base;
while (v >= base) {
cn = v % b;
v /= b;
//cout << int(cn) << ", " << int(v) << endl;
ret.push_front(PIChar(toBaseN[cn]));
}
if (v > 0) ret.push_front(PIChar(toBaseN[v]));
if (value < 0) ret.push_front('-');
return ret;
}
static llong toNumberBase(const PIString & value, int base = -1, bool * ok = 0) {
PIString v = value.trimmed();
if (base < 0) {
int ind = v.find("0x");
if (ind == 0 || ind == 1) {v.remove(ind, 2); base = 16;}
else base = 10;
} else
if (base < 2 || base > 40) {if (ok != 0) *ok = false; return 0;}
v.reverse();
if (ok != 0) *ok = true;
llong ret = 0, m = 1;
int cs;
piForeachC (PIChar & i, v) {
if (i == PIChar('-')) {ret = -ret; continue;}
//cout << i << ", " << fromBaseN[int(i.toAscii())] << ", " << m << endl;
cs = fromBaseN[int(i.toAscii())];
if (cs < 0 || cs >= base) break;
ret += cs * m;
m *= base;
}
return ret;
}
void appendFromChars(const char * c, int s);
string convertToStd() const;
#ifdef HAS_LOCALE
wstring convertToWString() const {wstring s; for (int i = 0; i < length(); ++i) s.push_back(at(i).toWChar()); return s;}
#endif
PIByteArray data_;
//string std_string;
@@ -201,6 +263,9 @@ private:
inline std::ostream & operator <<(std::ostream & s, const PIString & v) {for (int i = 0; i < v.length(); ++i) s << v[i]; return s;}
inline std::istream & operator >>(std::istream & s, PIString & v) {string ss; s >> ss; v << PIString(ss); return s;}
inline PIByteArray & operator <<(PIByteArray & s, const PIString & v) {s << v.size_s(); for (int i = 0; i < v.length(); ++i) s << v[i]; return s;}
inline PIByteArray & operator >>(PIByteArray & s, PIString & v) {int sz; s >> sz; v.resize(sz); for (int i = 0; i < sz; ++i) s >> v[i]; return s;}
inline PIString operator +(const PIString & str, const PIString & f) {PIString s(str); s += f; return s;}
//inline PIString operator +(const PIString & f, const char c) {PIString s(f); s.push_back(c); return s;}
@@ -211,8 +276,8 @@ inline PIString operator +(const PIString & f, const string & str) {PIString s(f
inline PIString operator +(const char * str, const PIString & f) {return PIString(str) + f;}
inline PIString operator +(const string & str, const PIString & f) {return PIString(str) + f;}
char chrUpr(char c);
char chrLwr(char c);
inline char chrUpr(char c);
inline char chrLwr(char c);
class PIStringList: public PIVector<PIString>
{