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

This commit is contained in:
2020-03-08 13:11:01 +00:00
parent 0831beaeec
commit 222a58d207
4 changed files with 132 additions and 55 deletions

View File

@@ -1,13 +1,23 @@
#include "pip.h" #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() { int main() {
//piCout << __sysoemname__; //piCout << __sysoemname__;
const char * s = "Eng, Русский №!123"; const char * s = "Eng, Русский №!123";
PIString str = PIString::fromUTF8(s); PIString str = PIString::fromUTF8(s);
piCout << str; //piCout << PIChar::fromUTF8("a").isDigit();
piCout << str.toLowerCase(); //piCout << PIChar::fromUTF8("1").isDigit();
piCout << str.toUpperCase(); 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;}); /*str.forEach([](PIChar c){piCout << c; return c;});
PIFile f("1.txt", PIIODevice::ReadWrite); PIFile f("1.txt", PIIODevice::ReadWrite);
f.clear(); f.clear();
@@ -19,5 +29,8 @@ int main() {
PIFile f("1.txt", PIIODevice::ReadWrite); PIFile f("1.txt", PIIODevice::ReadWrite);
f.clear(); f.clear();
f << c;*/ f << c;*/
//PIConfig conf("spec_core.conf", PIIODevice::ReadOnly);
//print(&conf.rootEntry());
return 0; return 0;
} }

View File

@@ -76,31 +76,45 @@ ushort charFromCodepage(const char * c, int size, const char * codepage, int * t
if (taken) *taken = ret; if (taken) *taken = ret;
return buffer; return buffer;
//printf("request %d\n", sz); //printf("request %d\n", sz);
# endif # else
#endif
wchar_t wc(0); wchar_t wc(0);
mbtowc(0, 0, 0); // reset mbtowc mbtowc(0, 0, 0); // reset mbtowc
ret = mbtowc(&wc, c, size); ret = mbtowc(&wc, c, size);
//printf("mbtowc = %d\n", ret); //printf("mbtowc = %d\n", ret);
if (ret < 1) return 0; if (ret < 1) return 0;
return ushort(int(wc)); 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 return
#ifdef PIP_ICU #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 #else
# ifdef WINDOWS # 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 # 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
#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) { PIChar::PIChar(const char * c, int * bytes) {
ch = charFromCodepage(c, 4, __syslocname__, 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 { 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 { 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 { 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 { bool PIChar::operator <=(const PIChar & o) const {
return charCompare(&ch, &(o.ch)) <= 0; return charCompare(*this, o) <= 0;
} }
bool PIChar::isDigit() const { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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) { PICout operator <<(PICout s, const PIChar & v) {
s.space(); s.space();
s.setControl(0, true); s.setControl(0, true);
if (v.isAscii()) s << char(v.ch);
else {
#ifdef PIP_ICU #ifdef PIP_ICU
UErrorCode e((UErrorCode)0); UErrorCode e((UErrorCode)0);
UConverter * cc = ucnv_open(__syslocname__, &e); UConverter * cc = ucnv_open(__syslocname__, &e);
@@ -319,15 +380,16 @@ PICout operator <<(PICout s, const PIChar & v) {
s << v.toSystem(); s << v.toSystem();
//else //else
// s << v.toConsole1Byte(); // s << v.toConsole1Byte();
#endif #else
if (v.isAscii()) s << char(v.ch); {
else {
char tc[8]; char tc[8];
wctomb(0, 0); wctomb(0, 0);
int sz = wctomb(tc, v.ch); int sz = wctomb(tc, v.ch);
for (int b = 0; b < sz; ++b) for (int b = 0; b < sz; ++b)
s << tc[b]; s << tc[b];
} }
#endif
}
s.restoreControl(); s.restoreControl();
return s; return s;
} }

View File

@@ -236,8 +236,7 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
delete[] buffer; delete[] buffer;
return; return;
//printf("request %d\n", sz); //printf("request %d\n", sz);
# endif # else
#endif
wchar_t wc; wchar_t wc;
mbtowc(0,0,0); // reset mbtowc mbtowc(0,0,0); // reset mbtowc
//qDebug() << "FromChars ..."; //qDebug() << "FromChars ...";
@@ -251,6 +250,8 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
//qDebug() << "2" << c; //qDebug() << "2" << c;
} }
//qDebug() << "FromChars done" << size(); //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); 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')); data_.push_back(uchar('\0'));
return; return;
# endif # else
#endif
wchar_t wc; wchar_t wc;
char tc[8]; char tc[8];
wctomb(0, 0); wctomb(0, 0);
@@ -376,6 +376,8 @@ void PIString::buildData(const char * cp) const {
data_.push_back(uchar(tc[b])); data_.push_back(uchar(tc[b]));
} }
data_.push_back(uchar('\0')); data_.push_back(uchar('\0'));
# endif
#endif
} }
@@ -475,7 +477,7 @@ PIString & PIString::operator +=(const wchar_t * str) {
delete[] c;*/ delete[] c;*/
int i = -1; int i = -1;
while (str[++i]) while (str[++i])
push_back(PIChar(int(str[i]))); push_back(PIChar(ushort(str[i])));
return *this; return *this;
} }
@@ -511,8 +513,8 @@ bool PIString::operator <(const PIString & str) const {
if (size() < l) return true; if (size() < l) return true;
if (size() > l) return false; if (size() > l) return false;
for (uint i = 0; i < l; ++i) { for (uint i = 0; i < l; ++i) {
if (str[i] == at(i)) continue; if (at(i) == str[i]) continue;
if (str[i] < at(i)) return true; if (at(i) < str[i]) return true;
else return false; else return false;
} }
return false; return false;
@@ -524,8 +526,8 @@ bool PIString::operator >(const PIString & str) const {
if (size() < l) return false; if (size() < l) return false;
if (size() > l) return true; if (size() > l) return true;
for (uint i = 0; i < l; ++i) { for (uint i = 0; i < l; ++i) {
if (str[i] == at(i)) continue; if (at(i) == str[i]) continue;
if (str[i] < at(i)) return false; if (at(i) < str[i]) return false;
else return true; else return true;
} }
return false; return false;

View File

@@ -213,7 +213,7 @@ PIString PIFile::readLine() {
return PIString::fromCodepage((const char *)str.data(), defaultCharset()); return PIString::fromCodepage((const char *)str.data(), defaultCharset());
} }
//cout << "readline: " << str << endl; //cout << "readline: " << str << endl;
return PIString(str); return PIString::fromUTF8(str);
} }