revised qse_mbstowcs()/qse_mbsntowcsn()/qse_wcstombs()/qse_wcsntombsn().

changed the parts affected by the revision works including deleting unnecessary functions
This commit is contained in:
2011-12-11 15:43:19 +00:00
parent 2faee1f23f
commit 566e13d874
17 changed files with 1159 additions and 855 deletions

View File

@ -6,7 +6,7 @@ AM_CPPFLAGS = \
-I$(includedir)
bin_PROGRAMS = xma fma pma chr str sll dll lda oht htb rbt fio01 fio02 pio sio time main main2 rex01 env path01 tre01 fmt01 fmt02 fs01
bin_PROGRAMS = xma fma pma chr str01 str02 str03 sll dll lda oht htb rbt fio01 fio02 pio sio time main main2 rex01 env path01 tre01 fmt01 fmt02 fs01
LDFLAGS = -L../../lib/cmn
LDADD = -lqsecmn
@ -15,7 +15,9 @@ xma_SOURCES = xma.c
fma_SOURCES = fma.c
pma_SOURCES = pma.c
chr_SOURCES = chr.c
str_SOURCES = str.c
str01_SOURCES = str01.c
str02_SOURCES = str02.c
str03_SOURCES = str03.c
sll_SOURCES = sll.c
dll_SOURCES = dll.c
lda_SOURCES = lda.c

View File

@ -35,12 +35,12 @@ POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
bin_PROGRAMS = xma$(EXEEXT) fma$(EXEEXT) pma$(EXEEXT) chr$(EXEEXT) \
str$(EXEEXT) sll$(EXEEXT) dll$(EXEEXT) lda$(EXEEXT) \
oht$(EXEEXT) htb$(EXEEXT) rbt$(EXEEXT) fio01$(EXEEXT) \
fio02$(EXEEXT) pio$(EXEEXT) sio$(EXEEXT) time$(EXEEXT) \
main$(EXEEXT) main2$(EXEEXT) rex01$(EXEEXT) env$(EXEEXT) \
path01$(EXEEXT) tre01$(EXEEXT) fmt01$(EXEEXT) fmt02$(EXEEXT) \
fs01$(EXEEXT)
str01$(EXEEXT) str02$(EXEEXT) str03$(EXEEXT) sll$(EXEEXT) \
dll$(EXEEXT) lda$(EXEEXT) oht$(EXEEXT) htb$(EXEEXT) \
rbt$(EXEEXT) fio01$(EXEEXT) fio02$(EXEEXT) pio$(EXEEXT) \
sio$(EXEEXT) time$(EXEEXT) main$(EXEEXT) main2$(EXEEXT) \
rex01$(EXEEXT) env$(EXEEXT) path01$(EXEEXT) tre01$(EXEEXT) \
fmt01$(EXEEXT) fmt02$(EXEEXT) fs01$(EXEEXT)
subdir = samples/cmn
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
@ -141,10 +141,18 @@ am_sll_OBJECTS = sll.$(OBJEXT)
sll_OBJECTS = $(am_sll_OBJECTS)
sll_LDADD = $(LDADD)
sll_DEPENDENCIES =
am_str_OBJECTS = str.$(OBJEXT)
str_OBJECTS = $(am_str_OBJECTS)
str_LDADD = $(LDADD)
str_DEPENDENCIES =
am_str01_OBJECTS = str01.$(OBJEXT)
str01_OBJECTS = $(am_str01_OBJECTS)
str01_LDADD = $(LDADD)
str01_DEPENDENCIES =
am_str02_OBJECTS = str02.$(OBJEXT)
str02_OBJECTS = $(am_str02_OBJECTS)
str02_LDADD = $(LDADD)
str02_DEPENDENCIES =
am_str03_OBJECTS = str03.$(OBJEXT)
str03_OBJECTS = $(am_str03_OBJECTS)
str03_LDADD = $(LDADD)
str03_DEPENDENCIES =
am_time_OBJECTS = time.$(OBJEXT)
time_OBJECTS = $(am_time_OBJECTS)
time_LDADD = $(LDADD)
@ -176,14 +184,16 @@ SOURCES = $(chr_SOURCES) $(dll_SOURCES) $(env_SOURCES) \
$(htb_SOURCES) $(lda_SOURCES) $(main_SOURCES) $(main2_SOURCES) \
$(oht_SOURCES) $(path01_SOURCES) $(pio_SOURCES) $(pma_SOURCES) \
$(rbt_SOURCES) $(rex01_SOURCES) $(sio_SOURCES) $(sll_SOURCES) \
$(str_SOURCES) $(time_SOURCES) $(tre01_SOURCES) $(xma_SOURCES)
$(str01_SOURCES) $(str02_SOURCES) $(str03_SOURCES) \
$(time_SOURCES) $(tre01_SOURCES) $(xma_SOURCES)
DIST_SOURCES = $(chr_SOURCES) $(dll_SOURCES) $(env_SOURCES) \
$(fio01_SOURCES) $(fio02_SOURCES) $(fma_SOURCES) \
$(fmt01_SOURCES) $(fmt02_SOURCES) $(fs01_SOURCES) \
$(htb_SOURCES) $(lda_SOURCES) $(main_SOURCES) $(main2_SOURCES) \
$(oht_SOURCES) $(path01_SOURCES) $(pio_SOURCES) $(pma_SOURCES) \
$(rbt_SOURCES) $(rex01_SOURCES) $(sio_SOURCES) $(sll_SOURCES) \
$(str_SOURCES) $(time_SOURCES) $(tre01_SOURCES) $(xma_SOURCES)
$(str01_SOURCES) $(str02_SOURCES) $(str03_SOURCES) \
$(time_SOURCES) $(tre01_SOURCES) $(xma_SOURCES)
ETAGS = etags
CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@ -339,7 +349,9 @@ xma_SOURCES = xma.c
fma_SOURCES = fma.c
pma_SOURCES = pma.c
chr_SOURCES = chr.c
str_SOURCES = str.c
str01_SOURCES = str01.c
str02_SOURCES = str02.c
str03_SOURCES = str03.c
sll_SOURCES = sll.c
dll_SOURCES = dll.c
lda_SOURCES = lda.c
@ -500,9 +512,15 @@ sio$(EXEEXT): $(sio_OBJECTS) $(sio_DEPENDENCIES)
sll$(EXEEXT): $(sll_OBJECTS) $(sll_DEPENDENCIES)
@rm -f sll$(EXEEXT)
$(LINK) $(sll_OBJECTS) $(sll_LDADD) $(LIBS)
str$(EXEEXT): $(str_OBJECTS) $(str_DEPENDENCIES)
@rm -f str$(EXEEXT)
$(LINK) $(str_OBJECTS) $(str_LDADD) $(LIBS)
str01$(EXEEXT): $(str01_OBJECTS) $(str01_DEPENDENCIES)
@rm -f str01$(EXEEXT)
$(LINK) $(str01_OBJECTS) $(str01_LDADD) $(LIBS)
str02$(EXEEXT): $(str02_OBJECTS) $(str02_DEPENDENCIES)
@rm -f str02$(EXEEXT)
$(LINK) $(str02_OBJECTS) $(str02_LDADD) $(LIBS)
str03$(EXEEXT): $(str03_OBJECTS) $(str03_DEPENDENCIES)
@rm -f str03$(EXEEXT)
$(LINK) $(str03_OBJECTS) $(str03_LDADD) $(LIBS)
time$(EXEEXT): $(time_OBJECTS) $(time_DEPENDENCIES)
@rm -f time$(EXEEXT)
$(LINK) $(time_OBJECTS) $(time_LDADD) $(LIBS)
@ -540,7 +558,9 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rex01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sio.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sll.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str02.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str03.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/time.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tre01.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xma.Po@am__quote@

View File

@ -7,12 +7,12 @@ static int test_main (int argc, qse_char_t* argv[], qse_char_t* envp[])
for (i = 0; i < argc; i++)
{
qse_printf (QSE_T("%d => [%s]\n"), i, argv[i]);
qse_printf (QSE_T("ARG %d => [%s]\n"), i, argv[i]);
}
for (i = 0; envp[i]; i++)
{
qse_printf (QSE_T("%d => [%s]\n"), i, envp[i]);
qse_printf (QSE_T("ENV %d => [%s]\n"), i, envp[i]);
}
return 0;

View File

@ -161,281 +161,6 @@ static int test4 ()
return 0;
}
static int test5 (void)
{
int i;
const qse_mchar_t* x[] =
{
"\0\0\0",
"뛰어 올라봐. 멀리멀리 잘난척하기는",
"Fly to the universe"
};
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_size_t k = qse_mbslen(x[i]);
qse_size_t j = 0;
qse_wchar_t wc;
qse_wchar_t buf[10];
if (k == 0) k = 3; /* for x[0] */
qse_printf (QSE_T("["));
while (j < k)
{
qse_size_t wlen = QSE_COUNTOF(buf);
qse_size_t y = qse_mbsntowcsn (&x[i][j], k-j, buf, &wlen);
if (y <= 0)
{
qse_printf (QSE_T("***illegal or incomplete sequence***"));
break;
}
if (wlen > 0 && buf[0] == QSE_T('\0'))
{
while (wlen > 0 && buf[--wlen] == QSE_T('\0'))
{
qse_printf (QSE_T("\\0"));
}
}
else
{
qse_printf (QSE_T("%.*s"), (int)wlen, buf);
}
j += y;
}
qse_printf (QSE_T("] => %d bytes\n"), (int)k);
}
return 0;
}
static int test6 (void)
{
int i;
const qse_mchar_t* x[] =
{
QSE_MT(""),
QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"),
QSE_MT("Fly to the universe. eat my shorts.")
};
qse_wchar_t buf[5];
qse_wchar_t buf2[50];
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_size_t n;
qse_size_t wlen = QSE_COUNTOF(buf);
n = qse_mbstowcs (x[i], buf, &wlen);
qse_printf (QSE_T("[%hs]=>"), x[i]);
/* the string should not be properly null terminated
* if buf is shorter than the necesary buffer size needed for
* full conversion */
qse_wcsxncpy (buf2, QSE_COUNTOF(buf2), buf, wlen);
qse_printf (QSE_T("%s %d chars %d bytes [%ls]\n"),
((x[i][n] == QSE_WT('\0') && wlen < QSE_COUNTOF(buf))? QSE_T("FULL"): QSE_T("PARTIAL")),
(int)wlen, (int)n, buf2);
qse_fflush (QSE_STDOUT);
qse_sio_puts (QSE_SIO_OUT, QSE_T("<<"));
qse_sio_puts (QSE_SIO_OUT, buf2);
qse_sio_puts (QSE_SIO_OUT, QSE_T(">>\n"));
qse_sio_flush (QSE_SIO_OUT);
}
qse_printf (QSE_T("-----------------\n"));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_size_t n;
qse_size_t wlen = QSE_COUNTOF(buf2);
n = qse_mbstowcs (x[i], buf2, &wlen);
qse_printf (QSE_T("%s %d chars %d bytes [%.*ls]\n"),
((x[i][n] == QSE_WT('\0') && wlen < QSE_COUNTOF(buf))? QSE_T("FULL"): QSE_T("PARTIAL")),
(int)wlen, (int)n, (int)wlen, buf2);
qse_fflush (QSE_STDOUT);
qse_sio_puts (QSE_SIO_OUT, QSE_T("<<"));
qse_sio_puts (QSE_SIO_OUT, buf2);
qse_sio_puts (QSE_SIO_OUT, QSE_T(">>\n"));
qse_sio_flush (QSE_SIO_OUT);
}
return 0;
}
static int test7 (void)
{
const qse_wchar_t* x[] =
{
L"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",
L"Fly to the universe, kick you ass"
};
qse_mchar_t buf[15];
int i, j;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
size_t len = wcslen(x[i]);
qse_size_t n;
if (len == 0) len = 20; /* for x[0] */
qse_printf (QSE_T("["));
for (j = 0; j < len; j += n)
{
qse_size_t mlen = sizeof(buf);
n = qse_wcsntombsn (&x[i][j], len-j, buf, &mlen);
if (n <= 0)
{
qse_printf (QSE_T("***illegal character or buffer not large***"));
break;
}
if (mlen > 0 && buf[0] == QSE_T('\0'))
{
while (mlen > 0 && buf[--mlen] == QSE_T('\0'))
{
qse_printf (QSE_T("\\0"));
}
}
else
{
qse_printf (QSE_T("%.*hs"), (int)mlen, buf);
#if 0
#ifdef QSE_CHAR_IS_MCHAR
qse_printf (QSE_T("%.*s"), (int)mlen, buf);
#else
#ifdef _WIN32
qse_printf (QSE_T("%.*S"), (int)mlen, buf);
#else
/* at least on linux and macosx, wprintf seems
* to taks preceision as the number of wide
* characters with %s. */
qse_printf (QSE_T("%.*S"), n, buf);
#endif
#endif
#endif
}
}
qse_printf (QSE_T("] => %d chars\n"), (int)len);
}
return 0;
}
static int test8 (void)
{
const qse_wchar_t* x[] =
{
L"",
L"\uB108 \uBB50\uAC00\uC798\uB0AC\uC5B4?",
L"Fly to the universe, kick you ass"
};
qse_mchar_t buf[10];
qse_mchar_t buf2[100];
int i, j;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_size_t n;
qse_size_t mlen = sizeof(buf);
memset (buf, QSE_MT('A'), sizeof(buf));
n = qse_wcstombs (x[i], buf, &mlen);
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
((x[i][n] == QSE_WT('\0') && mlen < sizeof(buf))? QSE_T("FULL"): QSE_T("PARTIAL")),
(int)n, (int)mlen, buf);
}
qse_printf (QSE_T("-----------------\n"));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_size_t n;
qse_size_t mlen = sizeof(buf2);
memset (buf2, 'A', sizeof(buf2));
n = qse_wcstombs (x[i], buf2, &mlen);
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
((x[i][n] == QSE_WT('\0') && mlen < sizeof(buf2))? QSE_T("FULL"): QSE_T("PARTIAL")),
(int)n, (int)mlen, buf2);
}
qse_printf (QSE_T("-----------------\n"));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_size_t n;
const qse_wchar_t* p = x[i];
while (1)
{
qse_size_t mlen;
memset (buf, 'A', sizeof(buf));
mlen = sizeof(buf);
n = qse_wcstombs (p, buf, &mlen);
if (n == 0) break;
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
((x[i][n] == QSE_WT('\0') && mlen < sizeof(buf))? QSE_T("FULL"): QSE_T("PARTIAL")),
(int)n, (int)mlen, buf);
p += n;
}
}
return 0;
}
static int test9 (void)
{
qse_mchar_t buf[24];
int i;
const qse_wchar_t* x[] =
{
L"",
L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",
L"A\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",
L"AB\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",
L"ABC\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",
L"ABCD\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",
L"ABCDEFGHIJKLMNOPQRSTUV",
L"ABCDEFGHIJKLMNOPQRSTUVW",
L"ABCDEFGHIJKLMNOPQRSTUVWX",
L"ABCDEFGHIJKLMNOPQRSTUVWXY",
L"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
};
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_printf (QSE_T("[%ls] => "), x[i]);
if (qse_wcstombsrigid (x[i], buf, QSE_COUNTOF(buf)) <= -1)
{
qse_printf (QSE_T("ERROR\n"));
}
else
{
qse_printf (QSE_T("[%hs]\n"), buf);
}
}
return 0;
}
static int test10 (void)
{
qse_wchar_t* wa[] = { QSE_WT("hello"), QSE_WT(","), QSE_WT("world"), QSE_NULL };
@ -693,11 +418,6 @@ int main ()
R (test2);
R (test3);
R (test4);
R (test5);
R (test6);
R (test7);
R (test8);
R (test9);
R (test10);
R (test11);

245
qse/samples/cmn/str02.c Normal file
View File

@ -0,0 +1,245 @@
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/stdio.h>
#include <qse/cmn/sio.h>
#include <locale.h>
#include <wchar.h>
#include <string.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#define R(f) \
do { \
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
if (f() == -1) return -1; \
} while (0)
static int test1 (void)
{
int i;
const qse_mchar_t* x[] =
{
QSE_MT(""),
QSE_MT("이거슨"),
QSE_MT("뭐냐이거"),
QSE_MT("過去一個月"),
QSE_MT("是成功的建商"),
QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"),
QSE_MT("Fly to the universe. eat my shorts.")
};
qse_wchar_t buf[5];
qse_wchar_t buf2[50];
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int n;
qse_size_t wlen = QSE_COUNTOF(buf), wlen1;
qse_size_t mlen, mlen1;
n = qse_mbstowcs (x[i], &mlen, buf, &wlen);
qse_printf (QSE_T("[%hs]=>"), x[i]);
qse_mbstowcs (x[i], &mlen1, QSE_NULL, &wlen1);
/* the string should not be properly null terminated
* if buf is shorter than the necesary buffer size needed for
* full conversion */
qse_wcsxncpy (buf2, QSE_COUNTOF(buf2), buf, wlen);
qse_printf (QSE_T("%s %d chars %d bytes (IF FULL %d chars %d bytes) [%ls]\n"),
((n == -3)? QSE_T("INCOMPLETE-SEQ"): (n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
(int)wlen, (int)mlen, (int)wlen1, (int)mlen1, buf2);
qse_fflush (QSE_STDOUT);
qse_sio_puts (QSE_SIO_OUT, QSE_T("<<"));
qse_sio_puts (QSE_SIO_OUT, buf2);
qse_sio_puts (QSE_SIO_OUT, QSE_T(">>\n"));
qse_sio_flush (QSE_SIO_OUT);
}
qse_printf (QSE_T("-----------------\n"));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int n;
qse_size_t wlen = QSE_COUNTOF(buf2), wlen1;
qse_size_t mlen, mlen1;
n = qse_mbstowcs (x[i], &mlen, buf2, &wlen);
qse_mbstowcs (x[i], &mlen1, QSE_NULL, &wlen1);
qse_printf (QSE_T("%s %d chars %d bytes (IF FULL %d chars %d bytes) [%.*ls]\n"),
((n == -3)? QSE_T("INCOMPLETE-SEQ"): (n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
(int)wlen, (int)mlen, (int)wlen, (int)wlen1, (int)mlen1, buf2);
qse_fflush (QSE_STDOUT);
qse_sio_puts (QSE_SIO_OUT, QSE_T("<<"));
qse_sio_puts (QSE_SIO_OUT, buf2);
qse_sio_puts (QSE_SIO_OUT, QSE_T(">>\n"));
qse_sio_flush (QSE_SIO_OUT);
}
return 0;
}
static int test2 (void)
{
int i;
const qse_mchar_t* x[] =
{
QSE_MT("\0\0\0"),
QSE_MT("이거슨"),
QSE_MT("뭐냐이거"),
QSE_MT("過去一個月"),
QSE_MT("是成功的建商"),
QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"),
QSE_MT("Fly to the universe")
};
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_size_t k = qse_mbslen(x[i]);
qse_size_t j = 0;
qse_wchar_t buf[2];
qse_size_t wlen, mlen;
int iter = 0;
if (k == 0) k = 3; /* for x[0] */
mlen = k;
qse_mbsntowcsn (&x[i][j], &mlen, QSE_NULL, &wlen);
qse_printf (QSE_T("expecting %d chars, %d bytes ["), wlen, mlen);
while (j < k)
{
wlen = QSE_COUNTOF(buf);
mlen = k - j;
int y = qse_mbsntowcsn (&x[i][j], &mlen, buf, &wlen);
if (y <= -1 && y != -2)
{
qse_printf (QSE_T("***illegal or incomplete sequence***"));
break;
}
if (wlen > 0 && buf[0] == QSE_T('\0'))
{
while (wlen > 0 && buf[--wlen] == QSE_T('\0'))
{
qse_printf (QSE_T("\\0"));
}
}
else
{
qse_printf (QSE_T("%.*s"), (int)wlen, buf);
}
j += mlen;
iter++;
}
qse_printf (QSE_T("] => %d bytes, %d iterations\n"), (int)k, (int)iter);
}
return 0;
}
static int test3 (void)
{
int i;
const qse_mchar_t* x[] =
{
QSE_MT(""),
QSE_MT("이거슨"),
QSE_MT("뭐냐이거"),
QSE_MT("過去一個月"),
QSE_MT("是成功的建商"),
QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"),
QSE_MT("Fly to the universe")
};
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_wchar_t* wcs;
wcs = qse_mbstowcsdup (x[i], QSE_MMGR_GETDFL());
if (wcs == QSE_NULL)
{
qse_printf (QSE_T("[ERROR]"));
}
else
{
qse_printf (QSE_T("[%ls]\n"), wcs);
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), wcs);
}
}
return 0;
}
static int test4 (void)
{
const qse_mchar_t* x[] =
{
QSE_MT("\0\0\0"),
QSE_MT("이거슨"),
QSE_MT("뭐냐이거"),
QSE_MT("過去一個月"),
QSE_MT("是成功的建商"),
QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"),
QSE_MT("Fly to the universe"),
QSE_NULL
};
qse_wchar_t* wcs;
wcs = qse_mbsatowcsdup (x, QSE_MMGR_GETDFL());
if (wcs == QSE_NULL)
{
qse_printf (QSE_T("[ERROR]\n"));
}
else
{
qse_printf (QSE_T("[%ls]\n"), wcs);
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), wcs);
}
return 0;
}
int main ()
{
#if defined(_WIN32)
char codepage[100];
UINT old_cp = GetConsoleOutputCP();
SetConsoleOutputCP (CP_UTF8);
/* TODO: on windows this set locale only affects those mbcs fucntions in clib.
* it doesn't support utf8 i guess find a working way. the following won't work
sprintf (codepage, ".%d", GetACP());
setlocale (LC_ALL, codepage);
*/
#else
setlocale (LC_ALL, "");
#endif
qse_printf (QSE_T("--------------------------------------------------------------------------------\n"));
qse_printf (QSE_T("Set the environment LANG to a Unicode locale such as UTF-8 if you see the illegal XXXXX errors. If you see such errors in Unicode locales, this program might be buggy. It is normal to see such messages in non-Unicode locales as it uses Unicode data\n"));
qse_printf (QSE_T("--------------------------------------------------------------------------------\n"));
R (test1);
R (test2);
R (test3);
R (test4);
#if defined(_WIN32)
SetConsoleOutputCP (old_cp);
#endif
return 0;
}

324
qse/samples/cmn/str03.c Normal file
View File

@ -0,0 +1,324 @@
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/stdio.h>
#include <qse/cmn/sio.h>
#include <locale.h>
#include <wchar.h>
#include <string.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#define R(f) \
do { \
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
if (f() == -1) return -1; \
} while (0)
static int test1 (void)
{
const qse_wchar_t unistr[] =
{
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",*/
0xB108,
L' ',
0xBB50,
0xAC00,
L' ',
0xC798,
0xB0AC,
0xC5B4,
L'?',
L'\0'
};
const qse_wchar_t* x[] =
{
L"",
L"",
L"Fly to the universe, kick your ass"
};
qse_mchar_t buf[10];
qse_mchar_t buf2[100];
int i;
x[1] = unistr;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int n;
qse_size_t wlen, mlen;
n = qse_wcstombs (x[i], &wlen, QSE_NULL, &mlen);
qse_printf (QSE_T("[%ls] n=%d, wcslen()=%d, wlen=%d, mlen=%d\n"),
x[i], (int)n, (int)qse_wcslen(x[i]), (int)wlen, (int)mlen);
}
qse_printf (QSE_T("-----------------\n"));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int n;
qse_size_t wlen, mlen;
mlen = QSE_COUNTOF(buf) - 1;
qse_mbsset (buf, QSE_MT('A'), QSE_COUNTOF(buf));
n = qse_wcstombs (x[i], &wlen, buf, &mlen);
QSE_ASSERT (buf[QSE_COUNTOF(buf)-1] == QSE_MT('A'));
buf[QSE_COUNTOF(buf)-1] = QSE_MT('\0');
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
((n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
(int)wlen, (int)mlen, buf);
}
qse_printf (QSE_T("-----------------\n"));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int n;
qse_size_t wlen, mlen;
mlen = QSE_COUNTOF(buf2);
n = qse_wcstombs (x[i], &wlen, buf2, &mlen);
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
((n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
(int)wlen, (int)mlen, buf2);
}
qse_printf (QSE_T("-----------------\n"));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int n;
const qse_wchar_t* p = x[i];
while (*p)
{
qse_size_t wlen, mlen;
mlen = QSE_COUNTOF(buf) - 1;
n = qse_wcstombs (p, &wlen, buf, &mlen);
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
((n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
(int)wlen, (int)mlen, buf);
p += wlen;
}
}
return 0;
}
static int test2 (void)
{
const qse_wchar_t unistr[] =
{
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",*/
0xB108,
L' ',
0xBB50,
0xAC00,
L' ',
0xC798,
0xB0AC,
0xC5B4,
L'?',
L'\0'
};
const qse_wchar_t* x[] =
{
L"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
L"",
L"Fly to the universe, kick your ass"
};
qse_mchar_t buf[9];
int i, j;
x[1] = unistr;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_size_t len, wlen, mlen, iter;
int n;
len = qse_wcslen(x[i]);
if (len == 0) len = 20; /* for x[0] */
qse_printf (QSE_T("Converting %d wide-character - "), (int)len);
wlen = len;
n = qse_wcsntombsn (&x[i][j], &wlen, QSE_NULL, &mlen);
if (n == -1)
{
qse_printf (QSE_T("***illegal character[mlen=%d/wlen=%d]*** ["), (int)mlen, (int)wlen);
}
else
{
/* -2 is never returned if i don't provide the multibyte buffer */
qse_printf (QSE_T("%d multibyte characters to be produced from %d wide characters ["), (int)mlen, (int)wlen);
}
for (j = 0, iter = 0; j < len; j += wlen)
{
iter = iter + 1;
wlen = len - j;
mlen = QSE_COUNTOF(buf);
n = qse_wcsntombsn (&x[i][j], &wlen, buf, &mlen);
if (n == -1)
{
qse_printf (QSE_T("***illegal character*** wlen=%d/mlen=%d/n=%d"), (int)(len-j), (int)mlen, (int)n);
break;
}
#if 0
else if (n == -2)
{
qse_printf (QSE_T("***buffer not large*** wlen=%d/mlen=%d/n=%d"), (int)(len-j), (int)mlen, (int)n);
break;
}
#endif
if (mlen > 0 && buf[0] == QSE_T('\0'))
{
while (mlen > 0 && buf[--mlen] == QSE_T('\0'))
{
qse_printf (QSE_T("\\0"));
}
}
else
{
#if defined(QSE_CHAR_IS_MCHAR) || defined(_WIN32)
qse_printf (QSE_T("%.*hs"), (int)mlen, buf);
#else
/* at least on linux and macosx, wprintf seems
* to taks preceision as the number of wide
* characters with %s. */
/* THIS HACK SHOULD BE REMOVED ONCE I IMPLEMENTE
* MY OWN qse_printf */
qse_printf (QSE_T("%.*hs"), (int)wlen, buf);
#endif
}
}
qse_printf (QSE_T("] => %d iterations\n"), (int)iter);
}
return 0;
}
static int test3 (void)
{
const qse_wchar_t unistr[] =
{
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",*/
0xB108,
L' ',
0xBB50,
0xAC00,
L' ',
0xC798,
0xB0AC,
0xC5B4,
L'?',
L'\0'
};
const qse_wchar_t* x[] =
{
L"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
L"",
L"Fly to the universe, kick your ass"
};
int i;
x[1] = unistr;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_mchar_t* m = qse_wcstombsdup (x[i], QSE_MMGR_GETDFL());
if (m == QSE_NULL)
{
qse_printf (QSE_T("[ERROR]\n"), m);
}
else
{
qse_printf (QSE_T("[%hs]\n"), m);
}
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), m);
}
return 0;
}
static int test4 (void)
{
const qse_wchar_t unistr[] =
{
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",*/
0xB108,
L' ',
0xBB50,
0xAC00,
L' ',
0xC798,
0xB0AC,
0xC5B4,
L'?',
L'\0'
};
const qse_wchar_t* x[] =
{
L"Good morning angels",
L"",
L"Fly to the universe, kick your ass",
QSE_NULL
};
qse_mchar_t* m;
x[1] = unistr;
m = qse_wcsatombsdup (x, QSE_MMGR_GETDFL());
if (m == QSE_NULL)
{
qse_printf (QSE_T("[ERROR]\n"), m);
}
else
{
qse_printf (QSE_T("[%hs]\n"), m);
}
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), m);
return 0;
}
int main ()
{
#if defined(_WIN32)
char codepage[100];
UINT old_cp = GetConsoleOutputCP();
SetConsoleOutputCP (CP_UTF8);
/* TODO: on windows this set locale only affects those mbcs fucntions in clib.
* it doesn't support utf8 i guess find a working way. the following won't work
sprintf (codepage, ".%d", GetACP());
setlocale (LC_ALL, codepage);
*/
#else
setlocale (LC_ALL, "");
#endif
qse_printf (QSE_T("--------------------------------------------------------------------------------\n"));
qse_printf (QSE_T("Set the environment LANG to a Unicode locale such as UTF-8 if you see the illegal XXXXX errors. If you see such errors in Unicode locales, this program might be buggy. It is normal to see such messages in non-Unicode locales as it uses Unicode data\n"));
qse_printf (QSE_T("--------------------------------------------------------------------------------\n"));
R (test1);
R (test2);
R (test3);
R (test4);
#if defined(_WIN32)
SetConsoleOutputCP (old_cp);
#endif
return 0;
}