2012-01-03 14:41:15 +00:00
|
|
|
#include <qse/cmn/slmb.h>
|
|
|
|
#include <qse/cmn/mbwc.h>
|
2008-12-30 04:49:25 +00:00
|
|
|
#include <qse/cmn/mem.h>
|
|
|
|
#include <qse/cmn/str.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include <qse/si/sio.h>
|
2008-12-30 04:49:25 +00:00
|
|
|
|
|
|
|
#include <locale.h>
|
2012-01-10 15:05:40 +00:00
|
|
|
#include <wchar.h>
|
2008-12-30 04:49:25 +00:00
|
|
|
|
2012-01-03 14:41:15 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2008-12-30 04:49:25 +00:00
|
|
|
#define R(f) \
|
|
|
|
do { \
|
|
|
|
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
|
|
|
|
if (f() == -1) return -1; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static int test1 (void)
|
2012-01-06 14:38:11 +00:00
|
|
|
{
|
|
|
|
qse_printf (QSE_T("slmblenmax=%d\n"), (int)qse_slmblenmax());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test2 (void)
|
2008-12-30 04:49:25 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const qse_mchar_t* x[] =
|
|
|
|
{
|
|
|
|
"\0",
|
2012-01-03 14:41:15 +00:00
|
|
|
"뛰어 올라봐", /* this text is in utf8. so some conversions fail on a non-utf8 locale */
|
2012-01-10 15:05:40 +00:00
|
|
|
#if defined(QSE_ENDIAN_BIG)
|
|
|
|
/* this text is in cp949. 뛰어올라봐 */
|
|
|
|
"\xD9\xB6\xEE\xBE \xC3\xBF\xF3\xB6\xC1\xBA",
|
|
|
|
#elif defined(QSE_ENDIAN_LITTLE)
|
|
|
|
/* this text is in cp949. 뛰어올라봐 */
|
|
|
|
"\xB6\xD9\xBE\xEE \xBF\xC3\xB6\xF3\xBA\xC1",
|
|
|
|
#else
|
|
|
|
# error ENDIAN UNKNOWN
|
|
|
|
#endif
|
2008-12-30 04:49:25 +00:00
|
|
|
"Fly to the universe"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < QSE_COUNTOF(x); i++)
|
|
|
|
{
|
2010-04-06 06:50:01 +00:00
|
|
|
qse_size_t k = qse_mbslen(x[i]);
|
2008-12-30 04:49:25 +00:00
|
|
|
qse_size_t j = 0;
|
|
|
|
qse_wchar_t wc;
|
|
|
|
|
|
|
|
if (k == 0) k++; /* for x[0] */
|
|
|
|
|
|
|
|
qse_printf (QSE_T("["));
|
|
|
|
while (j < k)
|
|
|
|
{
|
2012-01-10 15:05:40 +00:00
|
|
|
mbstate_t state;
|
|
|
|
qse_size_t y, ym, ymr;
|
|
|
|
|
|
|
|
y = qse_slmblen (&x[i][j], k-j);
|
|
|
|
ym = mblen (&x[i][j], k-j);
|
|
|
|
|
|
|
|
memset (&state, 0, sizeof(state));
|
|
|
|
ymr = mbrlen (&x[i][j], k-j, &state);
|
|
|
|
|
|
|
|
if (ym != ymr)
|
|
|
|
{
|
|
|
|
/* if this line is shown, the c runtime library is buggy.
|
|
|
|
* note that i assume we handle stateless encoding only
|
|
|
|
* since the state is initlized to 0 above all the time */
|
|
|
|
qse_printf (QSE_T("***buggy clib [mblen=%d],[mbrlen=%d]***"), (int)ym, (int)ymr);
|
|
|
|
}
|
2008-12-30 04:49:25 +00:00
|
|
|
|
|
|
|
if (y == 0)
|
|
|
|
{
|
2012-01-10 15:05:40 +00:00
|
|
|
qse_printf (QSE_T("***illegal sequence[y=%d][ym=%d][ymr=%d]***"), (int)y, (int)ym, (int)ymr);
|
2008-12-30 04:49:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (y > k-j)
|
|
|
|
{
|
|
|
|
qse_printf (QSE_T("***incomplete sequence***"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-03 14:41:15 +00:00
|
|
|
qse_size_t y2 = qse_slmbtoslwc (&x[i][j], y, &wc);
|
2008-12-30 04:49:25 +00:00
|
|
|
if (y2 != y)
|
|
|
|
{
|
|
|
|
qse_printf (QSE_T("***y(%d) != y2(%d). something is wrong*** "), (int)y, (int)y2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wc == L'\0')
|
|
|
|
{
|
|
|
|
qse_printf (QSE_T("'\\0'"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-11 09:25:33 +00:00
|
|
|
#ifdef QSE_CHAR_IS_MCHAR
|
2008-12-30 04:49:25 +00:00
|
|
|
qse_printf (QSE_T("%C"), wc);
|
|
|
|
#else
|
|
|
|
qse_printf (QSE_T("%c"), wc);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
j += y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qse_printf (QSE_T("] => %d bytes\n"), (int)k);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
static int test3 (void)
|
2008-12-30 04:49:25 +00:00
|
|
|
{
|
2012-01-06 14:38:11 +00:00
|
|
|
const qse_wchar_t unistr_kr[] =
|
2012-01-03 14:41:15 +00:00
|
|
|
{
|
|
|
|
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4!",*/
|
|
|
|
0xB108,
|
|
|
|
L' ',
|
|
|
|
0xBB50,
|
|
|
|
0xAC00,
|
|
|
|
L' ',
|
|
|
|
0xC798,
|
|
|
|
0xB0AC,
|
|
|
|
0xC5B4,
|
|
|
|
L'!',
|
|
|
|
L'\0'
|
|
|
|
};
|
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
const qse_wchar_t unistr_cn[] =
|
|
|
|
{
|
|
|
|
/* 智慧手機帶頭 */
|
|
|
|
/* \u667A\u6167\u624B\u6A5F\u5E36\u982D */
|
|
|
|
0x667A,
|
|
|
|
0x6167,
|
|
|
|
0x624B,
|
|
|
|
0x6A5F,
|
|
|
|
0x5E36,
|
|
|
|
0x982D,
|
|
|
|
L'\0'
|
|
|
|
};
|
|
|
|
|
2008-12-30 04:49:25 +00:00
|
|
|
const qse_wchar_t* x[] =
|
|
|
|
{
|
|
|
|
L"\0",
|
2012-01-03 14:41:15 +00:00
|
|
|
L"",
|
2012-01-06 14:38:11 +00:00
|
|
|
L"",
|
2008-12-30 04:49:25 +00:00
|
|
|
L"Fly to the universe"
|
|
|
|
};
|
|
|
|
char buf[100];
|
|
|
|
int i, j;
|
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
x[1] = unistr_kr;
|
|
|
|
x[2] = unistr_cn;
|
2012-01-03 14:41:15 +00:00
|
|
|
|
2008-12-30 04:49:25 +00:00
|
|
|
for (i = 0; i < QSE_COUNTOF(x); i++)
|
|
|
|
{
|
2012-01-03 14:41:15 +00:00
|
|
|
int nbytes = 0;
|
2010-04-06 06:50:01 +00:00
|
|
|
int len = qse_wcslen (x[i]);
|
2008-12-30 04:49:25 +00:00
|
|
|
if (len == 0) len++; /* for x[0] */
|
|
|
|
|
|
|
|
qse_printf (QSE_T("["));
|
|
|
|
for (j = 0; j < len; j++)
|
|
|
|
{
|
2012-01-03 14:41:15 +00:00
|
|
|
qse_size_t n = qse_slwctoslmb (x[i][j], buf, sizeof(buf) - 1);
|
2008-12-30 04:49:25 +00:00
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
qse_printf (QSE_T("***illegal character***"));
|
|
|
|
}
|
|
|
|
else if (n > sizeof(buf))
|
|
|
|
{
|
|
|
|
qse_printf (QSE_T("***buffer too small***"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (n == 1 && buf[0] == '\0')
|
|
|
|
{
|
|
|
|
qse_printf (QSE_T("'\\0'"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-03 14:41:15 +00:00
|
|
|
buf[n] = QSE_MT('\0');
|
|
|
|
qse_printf (QSE_T("%hs"), buf);
|
2008-12-30 04:49:25 +00:00
|
|
|
}
|
2012-01-03 14:41:15 +00:00
|
|
|
nbytes += n;
|
2008-12-30 04:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-03 14:41:15 +00:00
|
|
|
qse_printf (QSE_T("] => %d chars, %d bytes\n"), (int)len, (int)nbytes);
|
2008-12-30 04:49:25 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
2015-03-11 06:12:48 +00:00
|
|
|
int x;
|
|
|
|
|
2012-01-03 14:41:15 +00:00
|
|
|
#if defined(_WIN32)
|
2015-03-11 06:12:48 +00:00
|
|
|
char locale[100];
|
|
|
|
UINT codepage = GetConsoleOutputCP();
|
2012-01-03 14:41:15 +00:00
|
|
|
if (codepage == CP_UTF8)
|
|
|
|
{
|
|
|
|
/*SetConsoleOUtputCP (CP_UTF8);*/
|
2012-10-15 09:36:39 +00:00
|
|
|
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
|
2012-01-03 14:41:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-11 06:12:48 +00:00
|
|
|
sprintf (locale, ".%u", (unsigned int)codepage);
|
|
|
|
setlocale (LC_ALL, locale);
|
2015-04-27 08:37:57 +00:00
|
|
|
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
|
2012-01-03 14:41:15 +00:00
|
|
|
}
|
2012-01-06 14:38:11 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
WORD LangID = MAKELANGID(LANG_CHINESE, SUBLANG_DEFAULT);
|
|
|
|
SetThreadLocale(MAKELCID(LangID, SORT_DEFAULT));
|
|
|
|
/* SetThreadUILanguage(), SetThreadPreferredUILanguage(). */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-03 14:41:15 +00:00
|
|
|
#else
|
2015-03-11 06:12:48 +00:00
|
|
|
setlocale (LC_ALL, "");
|
2015-04-27 08:37:57 +00:00
|
|
|
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
|
2012-01-03 14:41:15 +00:00
|
|
|
#endif
|
2008-12-30 04:49:25 +00:00
|
|
|
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_open_stdsios ();
|
2008-12-30 04:49:25 +00:00
|
|
|
R (test1);
|
|
|
|
R (test2);
|
2012-01-06 14:38:11 +00:00
|
|
|
R (test3);
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_close_stdsios ();
|
2008-12-30 04:49:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|