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

This commit is contained in:
2020-03-05 13:16:53 +00:00
parent d6f7e261b2
commit 5c6d9feb3d
6 changed files with 142 additions and 621 deletions

View File

@@ -21,6 +21,10 @@
#ifdef PIP_ICU
# define U_NOEXCEPT
# include "unicode/ucnv.h"
#else
# ifdef WINDOWS
# include <stringapiset.h>
# endif
#endif
#include <wchar.h>
#ifdef ANDROID
@@ -205,6 +209,7 @@ llong PIString::toNumberBase(const PIString & value, int base, bool * ok) {
void PIString::appendFromChars(const char * c, int s, const char * cp) {
if (s <= 0) return;
int sz;
#ifdef PIP_ICU
UErrorCode e((UErrorCode)0);
UConverter * cc = ucnv_open(cp, &e);
@@ -212,16 +217,27 @@ void PIString::appendFromChars(const char * c, int s, const char * cp) {
UChar * ucs = new UChar[s];
memset(ucs, 0, s * sizeof(UChar));
e = (UErrorCode)0;
int rs = ucnv_toUChars(cc, ucs, s, c, s, &e);
//printf("appendFromChars %d -> %d\n", s, rs);
for (int i = 0; i < rs; ++i)
int sz = ucnv_toUChars(cc, ucs, s, c, s, &e);
//printf("appendFromChars %d -> %d\n", s, sz);
for (int i = 0; i < sz; ++i)
push_back(PIChar(ucs[i]));
delete[] ucs;
ucnv_close(cc);
return;
}
#else
# ifdef WINDOWS
sz = MultiByteToWideChar((uint)(uintptr_t)cp, MB_ERR_INVALID_CHARS, c, s, 0, 0);
if (sz <= 0) return;
wchar_t * buffer = new wchar_t[sz];
MultiByteToWideChar((uint)(uintptr_t)cp, MB_ERR_INVALID_CHARS, c, s, buffer, sz);
for (int i = 0; i < sz; ++i)
push_back(PIChar((ushort)buffer[i]));
delete[] buffer;
return;
//printf("request %d\n", sz);
# endif
#endif
int sz;
wchar_t wc;
mbtowc(0,0,0); // reset mbtowc
//qDebug() << "FromChars ...";
@@ -242,11 +258,7 @@ PIString PIString::fromConsole(const char * s) {
int l = 0;
while (s[l] != '\0') ++l;
PIString ret;
if (l > 0) ret.appendFromChars(s, l
#ifdef PIP_ICU
, __sysoemname__
#endif
);
if (l > 0) ret.appendFromChars(s, l, __sysoemname__);
return ret;
}
@@ -256,7 +268,7 @@ PIString PIString::fromSystem(const char * s) {
int l = 0;
while (s[l] != '\0') ++l;
PIString ret;
if (l > 0) ret.appendFromChars(s, l);
if (l > 0) ret.appendFromChars(s, l, __syslocname__);
return ret;
}
@@ -265,11 +277,7 @@ PIString PIString::fromUTF8(const char * s) {
int l = 0;
while (s[l] != '\0') ++l;
PIString ret;
if (l > 0) ret.appendFromChars(s, l
#ifdef PIP_ICU
, "UTF-8"
#endif
);
if (l > 0) ret.appendFromChars(s, l, __utf8name__);
return ret;
}
@@ -277,11 +285,7 @@ PIString PIString::fromUTF8(const char * s) {
PIString PIString::fromUTF8(const PIByteArray & ba) {
PIString ret;
if (ba.isEmpty()) return ret;
ret.appendFromChars((const char*)ba.data(), ba.size()
#ifdef PIP_ICU
, "UTF-8"
#endif
);
ret.appendFromChars((const char*)ba.data(), ba.size(), __utf8name__);
return ret;
}
@@ -304,6 +308,10 @@ PIString PIString::fromCodepage(const char * s, const char * c) {
if (l > 0) ret.appendFromChars(s, l
#ifdef PIP_ICU
, c
#else
# ifdef WINDOWS
, __utf8name__
# endif
#endif
);
return ret;
@@ -319,60 +327,53 @@ PIString PIString::readableSize(llong bytes) {
void PIString::buildData(const char * cp) const {
data_.clear();
int sz = 0;
#ifdef PIP_ICU
UErrorCode e((UErrorCode)0);
UConverter * cc = ucnv_open(cp, &e);
if (cc) {
int rs = 0;//, ol = UCNV_GET_MAX_BYTES_FOR_STRING(size_s(), ucnv_getMaxCharSize(cc));
char uc[8];
for (int i = 0; i < size_s(); ++i) {
if (at(i).isAscii())
data_.push_back(uchar(at(i).unicode16Code()));
else {
e = (UErrorCode)0;
rs = ucnv_fromUChars(cc, uc, 8, (const UChar*)(PIDeque<PIChar>::data(i)), 1, &e);
//printf("conv %d\n", rs);
for (int j = 0; j < rs; ++j)
sz = ucnv_fromUChars(cc, uc, 8, (const UChar*)(PIDeque<PIChar>::data(i)), 1, &e);
for (int j = 0; j < sz; ++j)
data_.push_back(uc[j]);
}
}
//printf("buildData %d -> %d\n", size_s(), data_.size_s());
ucnv_close(cc);
data_.push_back('\0');
return;
}
#else
# ifdef WINDOWS
sz = WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)PIDeque<PIChar>::data(), PIDeque<PIChar>::size_s(), 0, 0, NULL, NULL);
//printf("WideCharToMultiByte %d %d\n", (uint)(uintptr_t)cp, sz);
if (sz <= 0) {
//printf("WideCharToMultiByte erro %d\n", GetLastError());
data_.push_back(uchar('\0'));
return;
}
data_.resize(sz);
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
wchar_t wc;
char tc[8];
// mbstate_t s;
// mbrlen(0,0,&s);
wctomb(0, 0);
int sz(0);
//printf("PIString::data %d\n", size_s());
for (int i = 0; i < size_s(); ++i) {
if (at(i).isAscii()) {
data_.push_back(uchar(at(i).toAscii()));
continue;
}
wc = at(i).toWChar();
// sz = piClampi(wcrtomb(tc, wc, &s), 0, 4);
sz = wctomb(tc, wc);
for (int b = 0; b < sz; ++b)
data_.push_back(uchar(tc[b]));
//printf("__%d_%d\n", i, wc);
/*tc = wc & 0xFF;
while (tc) {
data_.push_back(uchar(tc));
wc >>= 8;
tc = wc & 0xFF;
//printf("____%d\n", wc);
}*/
/*if (at(i).isAscii())
data_.push_back(uchar(at(i).toAscii()));
else {
data_.push_back((at(i).toCharPtr()[0])); ++j;
data_.push_back((at(i).toCharPtr()[1]));
}*/
}
data_.push_back(uchar('\0'));
}
@@ -390,21 +391,13 @@ void PIString::trimsubstr(int &st, int &fn) const {
const char * PIString::dataConsole() const {
buildData(
#ifdef PIP_ICU
__sysoemname__
#endif
);
buildData(__sysoemname__ );
return (const char *)(data_.data());
}
const char * PIString::dataUTF8() const {
buildData(
#ifdef PIP_ICU
"UTF-8"
#endif
);
buildData(__utf8name__);
return (const char *)(data_.data());
}
@@ -425,11 +418,7 @@ uint PIString::hash() const {
PIByteArray PIString::toUTF8() const {
if (isEmpty()) return data_.resized(0);
buildData(
#ifdef PIP_ICU
"UTF-8"
#endif
);
buildData(__utf8name__);
return data_.resized(data_.size_s() - 1);
}
@@ -439,6 +428,10 @@ PIByteArray PIString::toCharset(const char * c) const {
buildData(
#ifdef PIP_ICU
c
#else
# ifdef WINDOWS
__utf8name__
# endif
#endif
);
return data_.resized(data_.size_s() - 1);
@@ -450,7 +443,7 @@ PIString & PIString::operator +=(const char * str) {
if (!str) return *this;
int l = 0;
while (str[l] != '\0') ++l;
appendFromChars(str, l);
appendFromChars(str, l, __syslocname__);
return *this;
}
@@ -1119,17 +1112,11 @@ PICout operator <<(PICout s, const PIString & v) {
s.space();
s.quote();
s.setControl(0, true);
if (PICout::isBufferActive())
//if (PICout::isBufferActive())
s << v.data();
else {
#ifdef WINDOWS
PIByteArray d = v.toByteArray();
d.push_back(uchar('\0'));
s << (const char*)d.data();
#else
s << v.dataConsole();
#endif
}
//else {
// s << v.dataConsole();
//}
s.restoreControl();
s.quote();
return s;