Commit a06ab694 authored by fallenstardust's avatar fallenstardust

update bufferio.h

把相关调用同步修改
parent 8ca8759a
...@@ -27,6 +27,7 @@ public: ...@@ -27,6 +27,7 @@ public:
inline static void WriteInt8(unsigned char*& p, char val) { inline static void WriteInt8(unsigned char*& p, char val) {
buffer_write<char>(p, val); buffer_write<char>(p, val);
} }
// return: string length
template<typename T1, typename T2> template<typename T1, typename T2>
inline static int CopyWStr(T1* src, T2* pstr, int bufsize) { inline static int CopyWStr(T1* src, T2* pstr, int bufsize) {
int l = 0; int l = 0;
...@@ -49,8 +50,8 @@ public: ...@@ -49,8 +50,8 @@ public:
return l; return l;
} }
// UTF-16/UTF-32 to UTF-8 // UTF-16/UTF-32 to UTF-8
template<size_t N> // return: string length
static int EncodeUTF8(const wchar_t* wsrc, char(&str)[N]) { static int EncodeUTF8String(const wchar_t* wsrc, char* str, int size) {
char* pstr = str; char* pstr = str;
while (*wsrc != 0) { while (*wsrc != 0) {
unsigned cur = *wsrc; unsigned cur = *wsrc;
...@@ -63,36 +64,36 @@ public: ...@@ -63,36 +64,36 @@ public:
codepoint_size = 3; codepoint_size = 3;
else else
codepoint_size = 4; codepoint_size = 4;
if (pstr - str + codepoint_size > N - 1) if (pstr - str + codepoint_size > size - 1)
break; break;
switch (codepoint_size) { switch (codepoint_size) {
case 1: case 1:
*pstr = (char)cur; *pstr = (char)cur;
break; break;
case 2: case 2:
pstr[0] = ((cur >> 6) & 0x1f) | 0xc0; pstr[0] = ((cur >> 6) & 0x1f) | 0xc0;
pstr[1] = (cur & 0x3f) | 0x80; pstr[1] = (cur & 0x3f) | 0x80;
break; break;
case 3: case 3:
pstr[0] = ((cur >> 12) & 0xf) | 0xe0; pstr[0] = ((cur >> 12) & 0xf) | 0xe0;
pstr[1] = ((cur >> 6) & 0x3f) | 0x80; pstr[1] = ((cur >> 6) & 0x3f) | 0x80;
pstr[2] = (cur & 0x3f) | 0x80; pstr[2] = (cur & 0x3f) | 0x80;
break; break;
case 4: case 4:
if (sizeof(wchar_t) == 2) { if (sizeof(wchar_t) == 2) {
cur = 0; cur = 0;
cur |= ((unsigned)*wsrc & 0x3ff) << 10; cur |= ((unsigned)*wsrc & 0x3ff) << 10;
++wsrc; ++wsrc;
cur |= (unsigned)*wsrc & 0x3ff; cur |= (unsigned)*wsrc & 0x3ff;
cur += 0x10000; cur += 0x10000;
} }
pstr[0] = ((cur >> 18) & 0x7) | 0xf0; pstr[0] = ((cur >> 18) & 0x7) | 0xf0;
pstr[1] = ((cur >> 12) & 0x3f) | 0x80; pstr[1] = ((cur >> 12) & 0x3f) | 0x80;
pstr[2] = ((cur >> 6) & 0x3f) | 0x80; pstr[2] = ((cur >> 6) & 0x3f) | 0x80;
pstr[3] = (cur & 0x3f) | 0x80; pstr[3] = (cur & 0x3f) | 0x80;
break; break;
default: default:
break; break;
} }
pstr += codepoint_size; pstr += codepoint_size;
wsrc++; wsrc++;
...@@ -101,8 +102,8 @@ public: ...@@ -101,8 +102,8 @@ public:
return pstr - str; return pstr - str;
} }
// UTF-8 to UTF-16/UTF-32 // UTF-8 to UTF-16/UTF-32
template<size_t N> // return: string length
static int DecodeUTF8(const char* src, wchar_t(&wstr)[N]) { static int DecodeUTF8String(const char* src, wchar_t* wstr, int size) {
const char* p = src; const char* p = src;
wchar_t* wp = wstr; wchar_t* wp = wstr;
while(*p != 0) { while(*p != 0) {
...@@ -116,7 +117,7 @@ public: ...@@ -116,7 +117,7 @@ public:
} }
else else
codepoint_size = 1; codepoint_size = 1;
if (wp - wstr + codepoint_size > N - 1) if (wp - wstr + codepoint_size > size - 1)
break; break;
if((cur & 0x80) == 0) { if((cur & 0x80) == 0) {
*wp = *p; *wp = *p;
...@@ -144,6 +145,14 @@ public: ...@@ -144,6 +145,14 @@ public:
*wp = 0; *wp = 0;
return wp - wstr; return wp - wstr;
} }
template<size_t N>
static int EncodeUTF8(const wchar_t* src, char(&dst)[N]) {
return EncodeUTF8String(src, dst, N);
}
template<size_t N>
static int DecodeUTF8(const char* src, wchar_t(&dst)[N]) {
return DecodeUTF8String(src, dst, N);
}
static int GetVal(const wchar_t* pstr) { static int GetVal(const wchar_t* pstr) {
unsigned int ret = 0; unsigned int ret = 0;
while(*pstr >= L'0' && *pstr <= L'9') { while(*pstr >= L'0' && *pstr <= L'9') {
......
...@@ -31,7 +31,7 @@ bool CAndroidGUIComboBox::OnEvent(const SEvent& event) { ...@@ -31,7 +31,7 @@ bool CAndroidGUIComboBox::OnEvent(const SEvent& event) {
contents = (char **)malloc(count * sizeof(char *)); contents = (char **)malloc(count * sizeof(char *));
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
content = (char *)malloc(256 * 4); content = (char *)malloc(256 * 4);
BufferIO::EncodeUTF8(getItem(i), content); BufferIO::EncodeUTF8String(getItem(i), content, 256 * 4);
*(contents + i) = content; *(contents + i) = content;
} }
android::showAndroidComboBoxCompat(ygo::mainGame->appMain, true, contents, count); android::showAndroidComboBoxCompat(ygo::mainGame->appMain, true, contents, count);
......
...@@ -37,7 +37,7 @@ public: ...@@ -37,7 +37,7 @@ public:
return false; return false;
} }
if (m_phostInfo != NULL) { if (m_phostInfo != NULL) {
BufferIO::DecodeUTF8(m_phostInfo, dst); BufferIO::DecodeUTF8String(m_phostInfo, dst, 256);
return true; return true;
} }
char formatParams[512] = { 0 }; char formatParams[512] = { 0 };
...@@ -58,7 +58,7 @@ public: ...@@ -58,7 +58,7 @@ public:
char * extraParams = formatParams + strlen(formatParams); char * extraParams = formatParams + strlen(formatParams);
sprintf(extraParams, "$%s", m_proomPasswd); sprintf(extraParams, "$%s", m_proomPasswd);
} }
BufferIO::DecodeUTF8(formatParams, dst); BufferIO::DecodeUTF8String(formatParams, dst, 256);
return true; return true;
} }
private: private:
......
...@@ -27,6 +27,7 @@ public: ...@@ -27,6 +27,7 @@ public:
inline static void WriteInt8(unsigned char*& p, char val) { inline static void WriteInt8(unsigned char*& p, char val) {
buffer_write<char>(p, val); buffer_write<char>(p, val);
} }
// return: string length
template<typename T1, typename T2> template<typename T1, typename T2>
inline static int CopyWStr(T1* src, T2* pstr, int bufsize) { inline static int CopyWStr(T1* src, T2* pstr, int bufsize) {
int l = 0; int l = 0;
...@@ -49,49 +50,93 @@ public: ...@@ -49,49 +50,93 @@ public:
return l; return l;
} }
// UTF-16/UTF-32 to UTF-8 // UTF-16/UTF-32 to UTF-8
static int EncodeUTF8(const wchar_t * wsrc, char * str) { // return: string length
static int EncodeUTF8String(const wchar_t* wsrc, char* str, int size) {
char* pstr = str; char* pstr = str;
while(*wsrc != 0) { while (*wsrc != 0) {
if(*wsrc < 0x80) { unsigned cur = *wsrc;
*str = (char)*wsrc; int codepoint_size = 0;
++str; if (cur < 0x80U)
} else if(*wsrc < 0x800) { codepoint_size = 1;
str[0] = ((*wsrc >> 6) & 0x1f) | 0xc0; else if (cur < 0x800U)
str[1] = ((*wsrc) & 0x3f) | 0x80; codepoint_size = 2;
str += 2; else if (cur < 0x10000U && (cur < 0xd800U || cur > 0xdfffU))
} else if(*wsrc < 0x10000 && (*wsrc < 0xd800 || *wsrc > 0xdfff)) { codepoint_size = 3;
str[0] = ((*wsrc >> 12) & 0xf) | 0xe0; else
str[1] = ((*wsrc >> 6) & 0x3f) | 0x80; codepoint_size = 4;
str[2] = ((*wsrc) & 0x3f) | 0x80; if (pstr - str + codepoint_size > size - 1)
str += 3; break;
} else { switch (codepoint_size) {
str[0] = ((*wsrc >> 18) & 0x7) | 0xf0; case 1:
str[1] = ((*wsrc >> 12) & 0x3f) | 0x80; *pstr = (char)cur;
str[2] = ((*wsrc >> 6) & 0x3f) | 0x80; break;
str[3] = ((*wsrc) & 0x3f) | 0x80; case 2:
str += 4; pstr[0] = ((cur >> 6) & 0x1f) | 0xc0;
pstr[1] = (cur & 0x3f) | 0x80;
break;
case 3:
pstr[0] = ((cur >> 12) & 0xf) | 0xe0;
pstr[1] = ((cur >> 6) & 0x3f) | 0x80;
pstr[2] = (cur & 0x3f) | 0x80;
break;
case 4:
if (sizeof(wchar_t) == 2) {
cur = 0;
cur |= ((unsigned)*wsrc & 0x3ff) << 10;
++wsrc;
cur |= (unsigned)*wsrc & 0x3ff;
cur += 0x10000;
}
pstr[0] = ((cur >> 18) & 0x7) | 0xf0;
pstr[1] = ((cur >> 12) & 0x3f) | 0x80;
pstr[2] = ((cur >> 6) & 0x3f) | 0x80;
pstr[3] = (cur & 0x3f) | 0x80;
break;
default:
break;
} }
pstr += codepoint_size;
wsrc++; wsrc++;
} }
*str = 0; *pstr = 0;
return str - pstr; return pstr - str;
} }
// UTF-8 to UTF-16/UTF-32 // UTF-8 to UTF-16/UTF-32
static int DecodeUTF8(const char * src, wchar_t * wstr) { // return: string length
static int DecodeUTF8String(const char* src, wchar_t* wstr, int size) {
const char* p = src; const char* p = src;
wchar_t* wp = wstr; wchar_t* wp = wstr;
while(*p != 0) { while(*p != 0) {
if((*p & 0x80) == 0) { const unsigned cur = (unsigned)*p & 0xff;
int codepoint_size = 0;
if ((cur & 0xf8) == 0xf0) {
if (sizeof(wchar_t) == 2)
codepoint_size = 2;
else
codepoint_size = 1;
}
else
codepoint_size = 1;
if (wp - wstr + codepoint_size > size - 1)
break;
if((cur & 0x80) == 0) {
*wp = *p; *wp = *p;
p++; p++;
} else if((*p & 0xe0) == 0xc0) { } else if((cur & 0xe0) == 0xc0) {
*wp = (((unsigned)p[0] & 0x1f) << 6) | ((unsigned)p[1] & 0x3f); *wp = (((unsigned)p[0] & 0x1f) << 6) | ((unsigned)p[1] & 0x3f);
p += 2; p += 2;
} else if((*p & 0xf0) == 0xe0) { } else if((cur & 0xf0) == 0xe0) {
*wp = (((unsigned)p[0] & 0xf) << 12) | (((unsigned)p[1] & 0x3f) << 6) | ((unsigned)p[2] & 0x3f); *wp = (((unsigned)p[0] & 0xf) << 12) | (((unsigned)p[1] & 0x3f) << 6) | ((unsigned)p[2] & 0x3f);
p += 3; p += 3;
} else if((*p & 0xf8) == 0xf0) { } else if((cur & 0xf8) == 0xf0) {
*wp = (((unsigned)p[0] & 0x7) << 18) | (((unsigned)p[1] & 0x3f) << 12) | (((unsigned)p[2] & 0x3f) << 6) | ((unsigned)p[3] & 0x3f); if (sizeof(wchar_t) == 2) {
unsigned unicode = (((unsigned)p[0] & 0x7) << 18) | (((unsigned)p[1] & 0x3f) << 12) | (((unsigned)p[2] & 0x3f) << 6) | ((unsigned)p[3] & 0x3f);
unicode -= 0x10000;
*wp++ = (unicode >> 10) | 0xd800;
*wp = (unicode & 0x3ff) | 0xdc00;
} else {
*wp = (((unsigned)p[0] & 0x7) << 18) | (((unsigned)p[1] & 0x3f) << 12) | (((unsigned)p[2] & 0x3f) << 6) | ((unsigned)p[3] & 0x3f);
}
p += 4; p += 4;
} else } else
p++; p++;
...@@ -100,6 +145,14 @@ public: ...@@ -100,6 +145,14 @@ public:
*wp = 0; *wp = 0;
return wp - wstr; return wp - wstr;
} }
template<size_t N>
static int EncodeUTF8(const wchar_t* src, char(&dst)[N]) {
return EncodeUTF8String(src, dst, N);
}
template<size_t N>
static int DecodeUTF8(const char* src, wchar_t(&dst)[N]) {
return DecodeUTF8String(src, dst, N);
}
static int GetVal(const wchar_t* pstr) { static int GetVal(const wchar_t* pstr) {
unsigned int ret = 0; unsigned int ret = 0;
while(*pstr >= L'0' && *pstr <= L'9') { while(*pstr >= L'0' && *pstr <= L'9') {
......
package cn.garymb.ygomobile.ui.home; package cn.garymb.ygomobile.ui.home;
import android.annotation.SuppressLint;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.util.Log; import android.util.Log;
import android.util.SparseArray; import android.util.SparseArray;
...@@ -309,6 +310,7 @@ public class ImageUpdater implements DialogInterface.OnCancelListener { ...@@ -309,6 +310,7 @@ public class ImageUpdater implements DialogInterface.OnCancelListener {
} }
} }
@SuppressLint("StringFormatInvalid")
private void onEnd() { private void onEnd() {
synchronized (mCardStatus) { synchronized (mCardStatus) {
mCardStatus.clear(); mCardStatus.clear();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment