git-svn-id: svn://db.shs.com.ru/pip@927 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
19
main.cpp
19
main.cpp
@@ -1,13 +1,23 @@
|
||||
#include "pip.h"
|
||||
|
||||
void print(PIConfig::Entry*e, PIString indent = "") {
|
||||
piCout << indent << e->name() << "=" << e->value();
|
||||
indent += " ";
|
||||
e->children().forEach([=](PIConfig::Entry*e)->PIConfig::Entry*{print(e, indent); return e;});
|
||||
}
|
||||
|
||||
int main() {
|
||||
//piCout << __sysoemname__;
|
||||
const char * s = "Eng, Русский №!123";
|
||||
PIString str = PIString::fromUTF8(s);
|
||||
piCout << str;
|
||||
piCout << str.toLowerCase();
|
||||
piCout << str.toUpperCase();
|
||||
//piCout << PIChar::fromUTF8("a").isDigit();
|
||||
//piCout << PIChar::fromUTF8("1").isDigit();
|
||||
piCout << PIChar::fromUTF8("щ") << PIChar::fromUTF8("щ").isLower() << PIChar::fromUTF8("щ").isUpper();
|
||||
piCout << PIChar::fromUTF8("Ц") << PIChar::fromUTF8("Ц").isLower() << PIChar::fromUTF8("Ц").isUpper();
|
||||
|
||||
//piCout << str;
|
||||
//piCout << str.toLowerCase();
|
||||
//piCout << str.toUpperCase();
|
||||
/*str.forEach([](PIChar c){piCout << c; return c;});
|
||||
PIFile f("1.txt", PIIODevice::ReadWrite);
|
||||
f.clear();
|
||||
@@ -19,5 +29,8 @@ int main() {
|
||||
PIFile f("1.txt", PIIODevice::ReadWrite);
|
||||
f.clear();
|
||||
f << c;*/
|
||||
|
||||
//PIConfig conf("spec_core.conf", PIIODevice::ReadOnly);
|
||||
//print(&conf.rootEntry());
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -76,31 +76,45 @@ ushort charFromCodepage(const char * c, int size, const char * codepage, int * t
|
||||
if (taken) *taken = ret;
|
||||
return buffer;
|
||||
//printf("request %d\n", sz);
|
||||
# endif
|
||||
#endif
|
||||
# else
|
||||
wchar_t wc(0);
|
||||
mbtowc(0, 0, 0); // reset mbtowc
|
||||
ret = mbtowc(&wc, c, size);
|
||||
//printf("mbtowc = %d\n", ret);
|
||||
if (ret < 1) return 0;
|
||||
return ushort(int(wc));
|
||||
# endif
|
||||
#endif
|
||||
return ushort(c[0]);
|
||||
}
|
||||
|
||||
|
||||
int charCompare(const ushort * f, const ushort * s) {
|
||||
int charCompare(const PIChar & f, const PIChar & s) {
|
||||
return
|
||||
#ifdef PIP_ICU
|
||||
u_strCompare((const UChar*)f, 1, (const UChar*)s, 1, FALSE);
|
||||
u_strCompare((const UChar*)f.toWCharPtr(), 1, (const UChar*)s.toWCharPtr(), 1, FALSE);
|
||||
#else
|
||||
# ifdef WINDOWS
|
||||
CompareStringW(LOCALE_USER_DEFAULT, 0, (PCNZWCH)f, 1, (PCNZWCH)s, 1);
|
||||
CompareStringW(LOCALE_USER_DEFAULT, 0, (PCNZWCH)f.toWCharPtr(), 1, (PCNZWCH)s.toWCharPtr(), 1) - 2;
|
||||
# else
|
||||
wcsncmp((const wchar_t *)f, (const wchar_t *)s, 1);
|
||||
wcsncmp((const wchar_t *)f.toWCharPtr(), (const wchar_t *)s.toWCharPtr(), 1);
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool winIsCharType(const ushort * ch, int type) {
|
||||
#ifdef WINDOWS
|
||||
WORD attr = 0;
|
||||
if (GetStringTypeW(CT_CTYPE1, (LPCWCH)ch, 1, &attr) == 0) return false;
|
||||
return ((attr & type) == type);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
PIChar::PIChar(const char * c, int * bytes) {
|
||||
ch = charFromCodepage(c, 4, __syslocname__, bytes);
|
||||
}
|
||||
@@ -135,67 +149,112 @@ bool PIChar::operator ==(const PIChar & o) const {
|
||||
|
||||
|
||||
bool PIChar::operator >(const PIChar & o) const {
|
||||
return charCompare(&ch, &(o.ch)) > 0;
|
||||
return charCompare(*this, o) > 0;
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::operator <(const PIChar & o) const {
|
||||
return charCompare(&ch, &(o.ch)) < 0;
|
||||
return charCompare(*this, o) < 0;
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::operator >=(const PIChar & o) const {
|
||||
return charCompare(&ch, &(o.ch)) >= 0;
|
||||
return charCompare(*this, o) >= 0;
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::operator <=(const PIChar & o) const {
|
||||
return charCompare(&ch, &(o.ch)) <= 0;
|
||||
return charCompare(*this, o) <= 0;
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::isDigit() const {
|
||||
return isdigit(ch) != 0;
|
||||
if (isAscii()) return isdigit(ch) != 0;
|
||||
#ifdef WINDOWS
|
||||
return winIsCharType(&ch, C1_DIGIT);
|
||||
#else
|
||||
return iswdigit(ch) != 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::isHex() const {
|
||||
return isxdigit(ch) != 0;
|
||||
if (isAscii()) return isxdigit(ch) != 0;
|
||||
#ifdef WINDOWS
|
||||
return winIsCharType(&ch, C1_XDIGIT);
|
||||
#else
|
||||
return iswxdigit(ch) != 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::isGraphical() const {
|
||||
return isgraph(ch) != 0;
|
||||
if (isAscii()) return isgraph(ch) != 0;
|
||||
#ifdef WINDOWS
|
||||
return !winIsCharType(&ch, C1_CNTRL);
|
||||
#else
|
||||
return iswgraph(ch) != 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::isControl() const {
|
||||
return iscntrl(ch) != 0;
|
||||
if (isAscii()) return iscntrl(ch) != 0;
|
||||
#ifdef WINDOWS
|
||||
return winIsCharType(&ch, C1_CNTRL);
|
||||
#else
|
||||
return iswcntrl(ch) != 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::isLower() const {
|
||||
return islower(ch) != 0;
|
||||
if (isAscii()) return islower(ch) != 0;
|
||||
#ifdef WINDOWS
|
||||
return winIsCharType(&ch, C1_LOWER);
|
||||
#else
|
||||
return iswlower(ch) != 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::isUpper() const {
|
||||
return isupper(ch) != 0;
|
||||
if (isAscii()) return isupper(ch) != 0;
|
||||
#ifdef WINDOWS
|
||||
return winIsCharType(&ch, C1_UPPER);
|
||||
#else
|
||||
return iswupper(ch) != 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::isPrint() const {
|
||||
return isprint(ch) != 0;
|
||||
if (isAscii()) return isprint(ch) != 0;
|
||||
#ifdef WINDOWS
|
||||
return !winIsCharType(&ch, C1_CNTRL);
|
||||
#else
|
||||
return iswprint(ch) != 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::isSpace() const {
|
||||
return isspace(ch) != 0;
|
||||
if (isAscii()) return isspace(ch) != 0;
|
||||
#ifdef WINDOWS
|
||||
return winIsCharType(&ch, C1_SPACE);
|
||||
#else
|
||||
return iswspace(ch) != 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::isAlpha() const {
|
||||
return isalpha(ch) != 0;
|
||||
if (isAscii()) return isalpha(ch) != 0;
|
||||
#ifdef WINDOWS
|
||||
return winIsCharType(&ch, C1_ALPHA);
|
||||
#else
|
||||
return iswalpha(ch) != 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -302,6 +361,8 @@ PIChar PIChar::toLower() const {
|
||||
PICout operator <<(PICout s, const PIChar & v) {
|
||||
s.space();
|
||||
s.setControl(0, true);
|
||||
if (v.isAscii()) s << char(v.ch);
|
||||
else {
|
||||
#ifdef PIP_ICU
|
||||
UErrorCode e((UErrorCode)0);
|
||||
UConverter * cc = ucnv_open(__syslocname__, &e);
|
||||
@@ -319,15 +380,16 @@ PICout operator <<(PICout s, const PIChar & v) {
|
||||
s << v.toSystem();
|
||||
//else
|
||||
// s << v.toConsole1Byte();
|
||||
#endif
|
||||
if (v.isAscii()) s << char(v.ch);
|
||||
else {
|
||||
#else
|
||||
{
|
||||
char tc[8];
|
||||
wctomb(0, 0);
|
||||
int sz = wctomb(tc, v.ch);
|
||||
for (int b = 0; b < sz; ++b)
|
||||
s << tc[b];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
s.restoreControl();
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -236,8 +236,7 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
|
||||
delete[] buffer;
|
||||
return;
|
||||
//printf("request %d\n", sz);
|
||||
# endif
|
||||
#endif
|
||||
# else
|
||||
wchar_t wc;
|
||||
mbtowc(0,0,0); // reset mbtowc
|
||||
//qDebug() << "FromChars ...";
|
||||
@@ -251,6 +250,8 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
|
||||
//qDebug() << "2" << c;
|
||||
}
|
||||
//qDebug() << "FromChars done" << size();
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -360,8 +361,7 @@ void PIString::buildData(const char * cp) const {
|
||||
WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)PIDeque<PIChar>::data(), PIDeque<PIChar>::size_s(), (LPSTR)data_.data(), data_.size_s(), NULL, NULL);
|
||||
data_.push_back(uchar('\0'));
|
||||
return;
|
||||
# endif
|
||||
#endif
|
||||
# else
|
||||
wchar_t wc;
|
||||
char tc[8];
|
||||
wctomb(0, 0);
|
||||
@@ -376,6 +376,8 @@ void PIString::buildData(const char * cp) const {
|
||||
data_.push_back(uchar(tc[b]));
|
||||
}
|
||||
data_.push_back(uchar('\0'));
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -475,7 +477,7 @@ PIString & PIString::operator +=(const wchar_t * str) {
|
||||
delete[] c;*/
|
||||
int i = -1;
|
||||
while (str[++i])
|
||||
push_back(PIChar(int(str[i])));
|
||||
push_back(PIChar(ushort(str[i])));
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -511,8 +513,8 @@ bool PIString::operator <(const PIString & str) const {
|
||||
if (size() < l) return true;
|
||||
if (size() > l) return false;
|
||||
for (uint i = 0; i < l; ++i) {
|
||||
if (str[i] == at(i)) continue;
|
||||
if (str[i] < at(i)) return true;
|
||||
if (at(i) == str[i]) continue;
|
||||
if (at(i) < str[i]) return true;
|
||||
else return false;
|
||||
}
|
||||
return false;
|
||||
@@ -524,8 +526,8 @@ bool PIString::operator >(const PIString & str) const {
|
||||
if (size() < l) return false;
|
||||
if (size() > l) return true;
|
||||
for (uint i = 0; i < l; ++i) {
|
||||
if (str[i] == at(i)) continue;
|
||||
if (str[i] < at(i)) return false;
|
||||
if (at(i) == str[i]) continue;
|
||||
if (at(i) < str[i]) return false;
|
||||
else return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -213,7 +213,7 @@ PIString PIFile::readLine() {
|
||||
return PIString::fromCodepage((const char *)str.data(), defaultCharset());
|
||||
}
|
||||
//cout << "readline: " << str << endl;
|
||||
return PIString(str);
|
||||
return PIString::fromUTF8(str);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user