15.10.2012 - version 0.2.0 - PIIODevice - base of file, ethernet, serial and packets extractor. PIEthernet now support also TCP (client and server). PIConsole now can align labels in each column individually.
This commit is contained in:
54
pistring.cpp
Normal file → Executable file
54
pistring.cpp
Normal file → Executable file
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
String
|
||||
Copyright (C) 2011 Ivan Pelipenko peri4ko@gmail.com
|
||||
Copyright (C) 2012 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
|
||||
@@ -83,7 +83,7 @@ PIString & PIString::operator +=(const wchar_t * str) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
delete c;
|
||||
delete[] c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -313,6 +313,30 @@ PIString PIString::toLowerCase() const {
|
||||
}
|
||||
|
||||
|
||||
int PIString::lengthAscii() const {
|
||||
int j = 0;
|
||||
for (int i = 0; i < size_s(); ++i, ++j)
|
||||
if (!at(i).isAscii()) ++j;
|
||||
return j;
|
||||
}
|
||||
|
||||
|
||||
const char * PIString::data() const {
|
||||
PIByteArray & d_(*(const_cast<PIByteArray * >(&data_)));
|
||||
d_.clear();
|
||||
for (int i = 0, j = 0; i < size_s(); ++i, ++j) {
|
||||
if (at(i).isAscii())
|
||||
d_.push_back(uchar(at(i).toAscii()));
|
||||
else {
|
||||
d_.push_back((at(i).toCharPtr()[0])); ++j;
|
||||
d_.push_back((at(i).toCharPtr()[1]));
|
||||
}
|
||||
}
|
||||
d_.push_back(uchar('\0'));
|
||||
return (const char * )d_.data();
|
||||
}
|
||||
|
||||
|
||||
string PIString::convertToStd() const {
|
||||
string s;
|
||||
if (size() > 0) {
|
||||
@@ -332,7 +356,7 @@ string PIString::convertToStd() const {
|
||||
char PIString::toChar() const {
|
||||
PIString s(toNativeDecimalPoints());
|
||||
char v;
|
||||
sscanf(s.stdString().c_str(), "%c", &v);
|
||||
sscanf(s.data(), "%c", &v);
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -340,9 +364,9 @@ char PIString::toChar() const {
|
||||
short PIString::toShort() const {
|
||||
PIString s(trimmed().toLowerCase().toNativeDecimalPoints());
|
||||
short v;
|
||||
if (s.left(2) == "0x") {sscanf(s.stdString().c_str(), "%hx", &v); return v;}
|
||||
if (s.left(1) == "0") {sscanf(s.stdString().c_str(), "%ho", &v); return v;}
|
||||
sscanf(s.stdString().c_str(), "%hd", &v);
|
||||
if (s.left(2) == "0x") {sscanf(s.data(), "%hx", &v); return v;}
|
||||
if (s.left(1) == "0") {sscanf(s.data(), "%ho", &v); return v;}
|
||||
sscanf(s.data(), "%hd", &v);
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -350,9 +374,9 @@ short PIString::toShort() const {
|
||||
int PIString::toInt() const {
|
||||
PIString s(trimmed().toLowerCase().toNativeDecimalPoints());
|
||||
int v;
|
||||
if (s.left(2) == "0x") {sscanf(s.stdString().c_str(), "%x", &v); return v;}
|
||||
if (s.left(1) == "0") {sscanf(s.stdString().c_str(), "%o", &v); return v;}
|
||||
sscanf(s.stdString().c_str(), "%d", &v);
|
||||
if (s.left(2) == "0x") {sscanf(s.data(), "%x", &v); return v;}
|
||||
if (s.left(1) == "0") {sscanf(s.data(), "%o", &v); return v;}
|
||||
sscanf(s.data(), "%d", &v);
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -360,9 +384,9 @@ int PIString::toInt() const {
|
||||
long PIString::toLong() const {
|
||||
PIString s(trimmed().toLowerCase().toNativeDecimalPoints());
|
||||
long v;
|
||||
if (s.left(2) == "0x") {sscanf(s.stdString().c_str(), "%lx", &v); return v;}
|
||||
if (s.left(1) == "0") {sscanf(s.stdString().c_str(), "%lo", &v); return v;}
|
||||
sscanf(s.stdString().c_str(), "%ld", &v);
|
||||
if (s.left(2) == "0x") {sscanf(s.data(), "%lx", &v); return v;}
|
||||
if (s.left(1) == "0") {sscanf(s.data(), "%lo", &v); return v;}
|
||||
sscanf(s.data(), "%ld", &v);
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -370,9 +394,9 @@ long PIString::toLong() const {
|
||||
llong PIString::toLLong() const {
|
||||
PIString s(trimmed().toLowerCase().toNativeDecimalPoints());
|
||||
llong v;
|
||||
if (s.left(2) == "0x") {sscanf(s.stdString().c_str(), "%llx", &v); return v;}
|
||||
if (s.left(1) == "0") {sscanf(s.stdString().c_str(), "%llo", &v); return v;}
|
||||
sscanf(s.stdString().c_str(), "%lld", &v);
|
||||
if (s.left(2) == "0x") {sscanf(s.data(), "%llx", &v); return v;}
|
||||
if (s.left(1) == "0") {sscanf(s.data(), "%llo", &v); return v;}
|
||||
sscanf(s.data(), "%lld", &v);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user