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

@@ -26,9 +26,14 @@
# define U_NOEXCEPT
# include "unicode/ucnv.h"
# include "unicode/ustring.h"
#else
# ifdef WINDOWS
# include <stringapiset.h>
# endif
#endif
char * __syslocname__ = 0;
char * __sysoemname__ = 0;
#endif
char * __utf8name__ = 0;
#ifdef BLACKBERRY
# include <ctype.h>
#endif
@@ -47,88 +52,64 @@ char * __sysoemname__ = 0;
*/
PIChar::PIChar(const char * c, int * bytes) {
ushort charFromCodepage(const char * c, int size, const char * codepage, int * taken = 0) {
if (!c || size <= 0) return 0;
if (c[0] < 0x80) return c[0];
int ret;
#ifdef PIP_ICU
UErrorCode e((UErrorCode)0);
UConverter * cc = ucnv_open(__syslocname__, &e);
UConverter * cc = ucnv_open(codepage, &e);
if (cc) {
UChar uc;
e = (UErrorCode)0;
int ret = ucnv_toUChars(cc, &uc, 1, c, 4, &e);
if (bytes) * bytes = ret;
ret = ucnv_toUChars(cc, &uc, 1, c, size, &e);
if (taken) *taken = ret;
ucnv_close(cc);
ch = uc;
return;
return ushort(uc);
}
#else
# ifdef WINDOWS
wchar_t buffer;
ret = MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, size, &buffer, 1);
if (ret <= 0) return 0;
if (taken) *taken = ret;
return buffer;
//printf("request %d\n", sz);
# endif
#endif
ch = *reinterpret_cast<const int * >(c);
wchar_t wc(0);
mbtowc(0, 0, 0); // reset mbtowc
ret = mbtowc(&wc, c, sizeof(ushort));
if (ret < 1 || ret > (int)sizeof(ushort)) return 0;
else return wc;
return c[0];
}
PIChar::PIChar(const char * c, int * bytes) {
ch = charFromCodepage(c, 4, __syslocname__, bytes);
}
PIChar PIChar::fromConsole(char c) {
PIChar ret;
#ifdef PIP_ICU
UErrorCode e((UErrorCode)0);
UConverter * cc = ucnv_open(__sysoemname__, &e);
if (cc) {
UChar uc;
e = (UErrorCode)0;
ucnv_toUChars(cc, &uc, 1, &c, 1, &e);
ucnv_close(cc);
ret.ch = uc;
return ret;
}
#endif
ret.ch = c;
ret.ch = charFromCodepage(&c, 1, __sysoemname__);
return ret;
}
PIChar PIChar::fromSystem(char c) {
PIChar ret;
#ifdef PIP_ICU
UErrorCode e((UErrorCode)0);
UConverter * cc = ucnv_open(__syslocname__, &e);
if (cc) {
UChar uc;
e = (UErrorCode)0;
ucnv_toUChars(cc, &uc, 1, &c, 1, &e);
ucnv_close(cc);
ret.ch = uc;
return ret;
}
#endif
ret.ch = c;
ret.ch = charFromCodepage(&c, 1, __syslocname__);
return ret;
}
PIChar PIChar::fromUTF8(const char * c) {
PIChar ret;
#ifdef PIP_ICU
UErrorCode e((UErrorCode)0);
UConverter * cc = ucnv_open("UTF-8", &e);
if (cc) {
UChar uc;
e = (UErrorCode)0;
ucnv_toUChars(cc, &uc, 1, c, 8, &e);
ucnv_close(cc);
ret.ch = uc;
return ret;
}
#endif
//#ifdef ANDROID
wchar_t wc(0);
mbtowc(0, 0, 0); // reset mbtowc
//mbstate_t s;
//mbrtowc(&wc, c, 4, &s);
int sz = 0;
sz = mbtowc(&wc, c, sizeof(ushort));
if (sz < 1 || sz > (int)sizeof(ushort)) ret.ch = 0;
else ret.ch = wc;
return ret;
//#endif
// ret.ch = *(ushort*)c;
int l = 0;
while (c[l] != '\0') ++l;
ret.ch = charFromCodepage(c, l, __utf8name__);
return ret;
}
@@ -255,6 +236,11 @@ char PIChar::toConsole1Byte() const {
ucnv_close(cc);
return uc[0];
}
#endif
#ifdef WINDOWS
char ret[4] = {0,0,0,0};
WideCharToMultiByte(CP_OEMCP, 0, (LPCWCH)&ch, 1, ret, 4, NULL, NULL);
return ret[0];
#endif
return toAscii();
}
@@ -272,6 +258,11 @@ char PIChar::toSystem() const {
ucnv_close(cc);
return uc[0];
}
#endif
#ifdef WINDOWS
char ret[4] = {0,0,0,0};
WideCharToMultiByte(CP_ACP, 0, (LPCWCH)&ch, 1, ret, 4, NULL, NULL);
return ret[0];
#endif
return toAscii();
}
@@ -316,7 +307,14 @@ PICout operator <<(PICout s, const PIChar & v) {
s << uc;
} else
#endif
#ifdef WINDOWS
//if (PICout::isBufferActive())
s << v.toSystem();
//else
// s << v.toConsole1Byte();
#else
s << v.toCharPtr();
#endif
s.restoreControl();
return s;
}