diff --git a/qse/include/qse/cmn/Makefile.in b/qse/include/qse/cmn/Makefile.in index e9e22884..7269b96a 100644 --- a/qse/include/qse/cmn/Makefile.in +++ b/qse/include/qse/cmn/Makefile.in @@ -52,8 +52,8 @@ CONFIG_CLEAN_VPATH_FILES = SOURCES = DIST_SOURCES = am__pkginclude_HEADERS_DIST = alg.h chr.h dll.h env.h fio.h fma.h \ - fmt.h fs.h gdl.h htb.h lda.h main.h map.h mem.h oht.h opt.h \ - path.h pio.h pma.h rbt.h rex.h sio.h sll.h stdio.h str.h \ + fmt.h fs.h gdl.h htb.h lda.h main.h map.h mbwc.h mem.h oht.h \ + opt.h path.h pio.h pma.h rbt.h rex.h sio.h sll.h stdio.h str.h \ time.h tio.h tre.h utf8.h xma.h Mmgr.hpp StdMmgr.hpp Mmged.hpp am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; am__vpath_adj = case $$p in \ @@ -224,9 +224,9 @@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ pkginclude_HEADERS = alg.h chr.h dll.h env.h fio.h fma.h fmt.h fs.h \ - gdl.h htb.h lda.h main.h map.h mem.h oht.h opt.h path.h pio.h \ - pma.h rbt.h rex.h sio.h sll.h stdio.h str.h time.h tio.h tre.h \ - utf8.h xma.h $(am__append_1) + gdl.h htb.h lda.h main.h map.h mbwc.h mem.h oht.h opt.h path.h \ + pio.h pma.h rbt.h rex.h sio.h sll.h stdio.h str.h time.h tio.h \ + tre.h utf8.h xma.h $(am__append_1) all: all-am .SUFFIXES: diff --git a/qse/lib/cmn/mbwc-chr.c b/qse/lib/cmn/mbwc-chr.c index eec69447..85d819b6 100644 --- a/qse/lib/cmn/mbwc-chr.c +++ b/qse/lib/cmn/mbwc-chr.c @@ -42,11 +42,7 @@ qse_size_t qse_mbrlen ( const qse_mchar_t* mb, qse_size_t mbl, qse_mbstate_t* state) { -#if defined(_WIN32) - /* TODO: provide an option to use windows api */ - return qse_utf8len (mb, mbl); - -#elif defined(HAVE_MBRLEN) +#if defined(HAVE_MBRLEN) size_t n; n = mbrlen (mb, mbl, (mbstate_t*)state); @@ -72,17 +68,7 @@ qse_size_t qse_mbrtowc ( const qse_mchar_t* mb, qse_size_t mbl, qse_wchar_t* wc, qse_mbstate_t* state) { -#if defined(_WIN32) -/* - int n; - - n = MultiByteToWideChar (CP_ACP, MB_ERR_INVALID_CHARS, mb, mbl, wc, 1); - if (n == 0) return 0; - return mbl; -*/ - return qse_utf8touc (mb, mbl, wc); - -#elif defined(HAVE_MBRTOWC) +#if defined(HAVE_MBRTOWC) size_t n; n = mbrtowc (wc, mb, mbl, (mbstate_t*)state); @@ -104,10 +90,7 @@ qse_size_t qse_wcrtomb ( qse_wchar_t wc, qse_mchar_t* mb, qse_size_t mbl, qse_mbstate_t* state) { -#if defined(_WIN32) - return qse_uctoutf8 (wc, mb, mbl); - -#elif defined(HAVE_WCRTOMB) +#if defined(HAVE_WCRTOMB) size_t n; if (mbl < QSE_MBLEN_MAX) diff --git a/qse/lib/cmn/mbwc.c b/qse/lib/cmn/mbwc.c index 9b6411d1..6888c11d 100644 --- a/qse/lib/cmn/mbwc.c +++ b/qse/lib/cmn/mbwc.c @@ -22,13 +22,19 @@ #include -static qse_cmgr_t builtin_cmgr = +static qse_cmgr_t utf8_cmgr = { qse_utf8touc, qse_uctoutf8 }; -static qse_cmgr_t* dfl_cmgr = &builtin_cmgr; +static qse_cmgr_t locale_cmgr = +{ + qse_mbtowc, + qse_wctomb +}; + +static qse_cmgr_t* dfl_cmgr = &locale_cmgr; qse_cmgr_t* qse_getdflcmgr (void) { @@ -37,11 +43,9 @@ qse_cmgr_t* qse_getdflcmgr (void) void qse_setdflcmgr (qse_cmgr_t* cmgr) { - dfl_cmgr = (cmgr? cmgr: &builtin_cmgr); + dfl_cmgr = (cmgr? cmgr: &locale_cmgr); } - - /* string conversion function using default character conversion manager */ int qse_mbstowcs ( diff --git a/qse/samples/cmn/Makefile.am b/qse/samples/cmn/Makefile.am index c84c435a..6f053ca1 100644 --- a/qse/samples/cmn/Makefile.am +++ b/qse/samples/cmn/Makefile.am @@ -8,9 +8,10 @@ AM_CPPFLAGS = \ bin_PROGRAMS = \ xma fma pma chr \ - str01 str02 str03 \ + str01 \ + mbwc01 mbwc02 \ sll dll lda oht htb rbt fio01 fio02 pio \ - sio01 sio02 \ + sio01 sio02 sio03 \ time main main2 rex01 env path01 tre01 \ fmt01 fmt02 fs01 @@ -22,8 +23,8 @@ fma_SOURCES = fma.c pma_SOURCES = pma.c chr_SOURCES = chr.c str01_SOURCES = str01.c -str02_SOURCES = str02.c -str03_SOURCES = str03.c +mbwc01_SOURCES = mbwc01.c +mbwc02_SOURCES = mbwc02.c sll_SOURCES = sll.c dll_SOURCES = dll.c lda_SOURCES = lda.c @@ -35,6 +36,7 @@ fio02_SOURCES = fio02.c pio_SOURCES = pio.c sio01_SOURCES = sio01.c sio02_SOURCES = sio02.c +sio03_SOURCES = sio03.c time_SOURCES = time.c main_SOURCES = main.c main2_SOURCES = main2.c diff --git a/qse/samples/cmn/Makefile.in b/qse/samples/cmn/Makefile.in index 96a0d88a..dfadd000 100644 --- a/qse/samples/cmn/Makefile.in +++ b/qse/samples/cmn/Makefile.in @@ -35,12 +35,13 @@ POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ bin_PROGRAMS = xma$(EXEEXT) fma$(EXEEXT) pma$(EXEEXT) chr$(EXEEXT) \ - str01$(EXEEXT) str02$(EXEEXT) str03$(EXEEXT) sll$(EXEEXT) \ + str01$(EXEEXT) mbwc01$(EXEEXT) mbwc02$(EXEEXT) sll$(EXEEXT) \ dll$(EXEEXT) lda$(EXEEXT) oht$(EXEEXT) htb$(EXEEXT) \ rbt$(EXEEXT) fio01$(EXEEXT) fio02$(EXEEXT) pio$(EXEEXT) \ - sio01$(EXEEXT) sio02$(EXEEXT) time$(EXEEXT) main$(EXEEXT) \ - main2$(EXEEXT) rex01$(EXEEXT) env$(EXEEXT) path01$(EXEEXT) \ - tre01$(EXEEXT) fmt01$(EXEEXT) fmt02$(EXEEXT) fs01$(EXEEXT) + sio01$(EXEEXT) sio02$(EXEEXT) sio03$(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 @@ -109,6 +110,14 @@ am_main2_OBJECTS = main2.$(OBJEXT) main2_OBJECTS = $(am_main2_OBJECTS) main2_LDADD = $(LDADD) main2_DEPENDENCIES = +am_mbwc01_OBJECTS = mbwc01.$(OBJEXT) +mbwc01_OBJECTS = $(am_mbwc01_OBJECTS) +mbwc01_LDADD = $(LDADD) +mbwc01_DEPENDENCIES = +am_mbwc02_OBJECTS = mbwc02.$(OBJEXT) +mbwc02_OBJECTS = $(am_mbwc02_OBJECTS) +mbwc02_LDADD = $(LDADD) +mbwc02_DEPENDENCIES = am_oht_OBJECTS = oht.$(OBJEXT) oht_OBJECTS = $(am_oht_OBJECTS) oht_LDADD = $(LDADD) @@ -141,6 +150,10 @@ am_sio02_OBJECTS = sio02.$(OBJEXT) sio02_OBJECTS = $(am_sio02_OBJECTS) sio02_LDADD = $(LDADD) sio02_DEPENDENCIES = +am_sio03_OBJECTS = sio03.$(OBJEXT) +sio03_OBJECTS = $(am_sio03_OBJECTS) +sio03_LDADD = $(LDADD) +sio03_DEPENDENCIES = am_sll_OBJECTS = sll.$(OBJEXT) sll_OBJECTS = $(am_sll_OBJECTS) sll_LDADD = $(LDADD) @@ -149,14 +162,6 @@ 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) @@ -186,20 +191,20 @@ 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) $(sio01_SOURCES) \ - $(sio02_SOURCES) $(sll_SOURCES) $(str01_SOURCES) \ - $(str02_SOURCES) $(str03_SOURCES) $(time_SOURCES) \ - $(tre01_SOURCES) $(xma_SOURCES) + $(mbwc01_SOURCES) $(mbwc02_SOURCES) $(oht_SOURCES) \ + $(path01_SOURCES) $(pio_SOURCES) $(pma_SOURCES) $(rbt_SOURCES) \ + $(rex01_SOURCES) $(sio01_SOURCES) $(sio02_SOURCES) \ + $(sio03_SOURCES) $(sll_SOURCES) $(str01_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) $(sio01_SOURCES) \ - $(sio02_SOURCES) $(sll_SOURCES) $(str01_SOURCES) \ - $(str02_SOURCES) $(str03_SOURCES) $(time_SOURCES) \ - $(tre01_SOURCES) $(xma_SOURCES) + $(mbwc01_SOURCES) $(mbwc02_SOURCES) $(oht_SOURCES) \ + $(path01_SOURCES) $(pio_SOURCES) $(pma_SOURCES) $(rbt_SOURCES) \ + $(rex01_SOURCES) $(sio01_SOURCES) $(sio02_SOURCES) \ + $(sio03_SOURCES) $(sll_SOURCES) $(str01_SOURCES) \ + $(time_SOURCES) $(tre01_SOURCES) $(xma_SOURCES) ETAGS = etags CTAGS = ctags DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) @@ -356,8 +361,8 @@ fma_SOURCES = fma.c pma_SOURCES = pma.c chr_SOURCES = chr.c str01_SOURCES = str01.c -str02_SOURCES = str02.c -str03_SOURCES = str03.c +mbwc01_SOURCES = mbwc01.c +mbwc02_SOURCES = mbwc02.c sll_SOURCES = sll.c dll_SOURCES = dll.c lda_SOURCES = lda.c @@ -369,6 +374,7 @@ fio02_SOURCES = fio02.c pio_SOURCES = pio.c sio01_SOURCES = sio01.c sio02_SOURCES = sio02.c +sio03_SOURCES = sio03.c time_SOURCES = time.c main_SOURCES = main.c main2_SOURCES = main2.c @@ -495,6 +501,12 @@ main$(EXEEXT): $(main_OBJECTS) $(main_DEPENDENCIES) main2$(EXEEXT): $(main2_OBJECTS) $(main2_DEPENDENCIES) @rm -f main2$(EXEEXT) $(LINK) $(main2_OBJECTS) $(main2_LDADD) $(LIBS) +mbwc01$(EXEEXT): $(mbwc01_OBJECTS) $(mbwc01_DEPENDENCIES) + @rm -f mbwc01$(EXEEXT) + $(LINK) $(mbwc01_OBJECTS) $(mbwc01_LDADD) $(LIBS) +mbwc02$(EXEEXT): $(mbwc02_OBJECTS) $(mbwc02_DEPENDENCIES) + @rm -f mbwc02$(EXEEXT) + $(LINK) $(mbwc02_OBJECTS) $(mbwc02_LDADD) $(LIBS) oht$(EXEEXT): $(oht_OBJECTS) $(oht_DEPENDENCIES) @rm -f oht$(EXEEXT) $(LINK) $(oht_OBJECTS) $(oht_LDADD) $(LIBS) @@ -519,18 +531,15 @@ sio01$(EXEEXT): $(sio01_OBJECTS) $(sio01_DEPENDENCIES) sio02$(EXEEXT): $(sio02_OBJECTS) $(sio02_DEPENDENCIES) @rm -f sio02$(EXEEXT) $(LINK) $(sio02_OBJECTS) $(sio02_LDADD) $(LIBS) +sio03$(EXEEXT): $(sio03_OBJECTS) $(sio03_DEPENDENCIES) + @rm -f sio03$(EXEEXT) + $(LINK) $(sio03_OBJECTS) $(sio03_LDADD) $(LIBS) sll$(EXEEXT): $(sll_OBJECTS) $(sll_DEPENDENCIES) @rm -f sll$(EXEEXT) $(LINK) $(sll_OBJECTS) $(sll_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) @@ -560,6 +569,8 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lda.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main2.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mbwc01.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mbwc02.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/oht.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/path01.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pio.Po@am__quote@ @@ -568,10 +579,9 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rex01.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sio01.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sio02.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sio03.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sll.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@ diff --git a/qse/samples/cmn/str02.c b/qse/samples/cmn/mbwc01.c similarity index 99% rename from qse/samples/cmn/str02.c rename to qse/samples/cmn/mbwc01.c index ae0f86f0..e2ca2973 100644 --- a/qse/samples/cmn/str02.c +++ b/qse/samples/cmn/mbwc01.c @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/qse/samples/cmn/str03.c b/qse/samples/cmn/mbwc02.c similarity index 99% rename from qse/samples/cmn/str03.c rename to qse/samples/cmn/mbwc02.c index 990f287e..9d5a3d6b 100644 --- a/qse/samples/cmn/str03.c +++ b/qse/samples/cmn/mbwc02.c @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/qse/samples/cmn/sio01.c b/qse/samples/cmn/sio01.c deleted file mode 100644 index 56c52bd3..00000000 --- a/qse/samples/cmn/sio01.c +++ /dev/null @@ -1,169 +0,0 @@ -#include -#include -#include -#include -#include - -#if defined(_WIN32) -# include -#endif - -static qse_sio_t* g_out; - -#define R(f) \ - do { \ - qse_sio_putstr (g_out,QSE_T("== ")); \ - qse_sio_putstr (g_out,QSE_T(#f)); \ - qse_sio_putstr (g_out,QSE_T(" ==\n")); \ - qse_sio_flush (g_out); \ - if (f() == -1) return -1; \ - } while (0) - -static int test1 (void) -{ - qse_sio_t* sio; - int i; - - 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"Fly to the universe, kick you ass", - L"", - L"Fly to the universe, kick you ass", - L"Fly to the universe, kick you ass", - L"" - }; - - x[0] = unistr; - x[2] = unistr; - x[5] = unistr; - sio = qse_sio_open (QSE_MMGR_GETDFL(), 0, QSE_T("sio.txt"), - QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE); - - if (sio == QSE_NULL) - { - qse_printf (QSE_T("cannot open file\n")); - return -1; - } - - for (i = 0; i < QSE_COUNTOF(x); i++) - { - qse_sio_putstr (g_out, QSE_T("Written [")); - qse_sio_putwcs (g_out, x[i]); - qse_sio_putstr (g_out, QSE_T("]\n")); - - qse_sio_putwcs (sio, x[i]); - qse_sio_putc (sio, QSE_T('\n')); - } - - qse_sio_close (sio); - - return 0; -} - -static int test2 (void) -{ - qse_ssize_t n; - qse_wchar_t buf[20]; - qse_sio_t* in, * out; - - in = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR); - out = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); - - qse_sio_putstr (out, QSE_T("Type something here:\n")); - while (1) - { - n = qse_sio_getwcs (in, buf, QSE_COUNTOF(buf)); - if (n == 0) break; - if (n <= -1) - { - qse_char_t buf[32]; - qse_fmtintmax (buf, QSE_COUNTOF(buf), qse_sio_geterrnum(in), 10, -1, QSE_T('\0'), QSE_NULL); - qse_sio_putstr (out, QSE_T("ERROR .... ")); - qse_sio_putstr (out, buf); - qse_sio_putstr (out, QSE_T("\n")); - break; - } - - qse_sio_putwcs (out, buf); - } - - qse_sio_close (out); - qse_sio_close (in); - return 0; -} - -static int test3 (void) -{ - qse_ssize_t n; - qse_mchar_t buf[20]; - qse_sio_t* in, * out; - - in = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR); - out = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); - - qse_sio_putstr (out, QSE_T("Type something here:\n")); - while (1) - { - n = qse_sio_getmbs (in, buf, QSE_COUNTOF(buf)); - if (n == 0) break; - if (n < 0) - { - qse_char_t buf[32]; - qse_fmtintmax (buf, QSE_COUNTOF(buf), qse_sio_geterrnum(in), 10, -1, QSE_T('\0'), QSE_NULL); - qse_sio_putstr (out, QSE_T("error .... ")); - qse_sio_putstr (out, buf); - qse_sio_putstr (out, QSE_T("\n")); - break; - } - - qse_sio_putmbs (out, buf); - } - - qse_sio_close (out); - qse_sio_close (in); - return 0; -} - - -int main () -{ -#if 0 -#if defined(_WIN32) - UINT old_cp = GetConsoleOutputCP(); - SetConsoleOutputCP (CP_UTF8); -#endif -#endif - - setlocale (LC_ALL, ""); - - g_out = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); - - R (test1); - R (test2); - R (test3); - - qse_sio_close (g_out); - -#if 0 -#if defined(_WIN32) - SetConsoleOutputCP (old_cp); -#endif -#endif - return 0; -} diff --git a/qse/samples/cmn/sio02.c b/qse/samples/cmn/sio02.c index b34a3741..56c52bd3 100644 --- a/qse/samples/cmn/sio02.c +++ b/qse/samples/cmn/sio02.c @@ -1,24 +1,31 @@ #include #include +#include #include - #include #if defined(_WIN32) # include #endif +static qse_sio_t* g_out; + #define R(f) \ do { \ - qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \ + qse_sio_putstr (g_out,QSE_T("== ")); \ + qse_sio_putstr (g_out,QSE_T(#f)); \ + qse_sio_putstr (g_out,QSE_T(" ==\n")); \ + qse_sio_flush (g_out); \ if (f() == -1) return -1; \ } while (0) static int test1 (void) { - const qse_wchar_t unistr[] = + qse_sio_t* sio; + int i; + + const qse_wchar_t unistr[] = { - /* ugly hack for old compilers that don't support \u */ /*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4!",*/ 0xB108, L' ', @@ -30,128 +37,133 @@ static int test1 (void) 0xC5B4, L'!', L'\0' - }; + }; - const qse_wchar_t unistr2[] = - { - /* this include an illegal unicode character. - * a strict converter should return an error so a question mark - * should be printed for such a character */ - 0xFFFF53C0u, - 0xFFFF4912u, - 0xBA00u, - 0xFFFF1234u, - L'\0' - }; const qse_wchar_t* x[] = { L"", + L"Fly to the universe, kick you ass", L"", - L"", - L"Fly to the universe, kick your ass" - }; - int i; - qse_sio_t* sio; + L"Fly to the universe, kick you ass", + L"Fly to the universe, kick you ass", + L"" + }; - x[1] = unistr; - x[2] = unistr2; + x[0] = unistr; + x[2] = unistr; + x[5] = unistr; + sio = qse_sio_open (QSE_MMGR_GETDFL(), 0, QSE_T("sio.txt"), + QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE); - sio = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_NOAUTOFLUSH); - if (sio == QSE_NULL) return -1; + if (sio == QSE_NULL) + { + qse_printf (QSE_T("cannot open file\n")); + return -1; + } for (i = 0; i < QSE_COUNTOF(x); i++) { + qse_sio_putstr (g_out, QSE_T("Written [")); + qse_sio_putwcs (g_out, x[i]); + qse_sio_putstr (g_out, QSE_T("]\n")); + qse_sio_putwcs (sio, x[i]); - qse_sio_putwc (sio, QSE_WT('\n')); + qse_sio_putc (sio, QSE_T('\n')); } qse_sio_close (sio); + return 0; } static int test2 (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") - }; - int i; - qse_sio_t* sio; - - sio = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_NOAUTOFLUSH); - if (sio == QSE_NULL) return -1; - - for (i = 0; i < QSE_COUNTOF(x); i++) + qse_ssize_t n; + qse_wchar_t buf[20]; + qse_sio_t* in, * out; + + in = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR); + out = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); + + qse_sio_putstr (out, QSE_T("Type something here:\n")); + while (1) { - qse_sio_putmbs (sio, x[i]); - qse_sio_putmb (sio, QSE_MT('\n')); + n = qse_sio_getwcs (in, buf, QSE_COUNTOF(buf)); + if (n == 0) break; + if (n <= -1) + { + qse_char_t buf[32]; + qse_fmtintmax (buf, QSE_COUNTOF(buf), qse_sio_geterrnum(in), 10, -1, QSE_T('\0'), QSE_NULL); + qse_sio_putstr (out, QSE_T("ERROR .... ")); + qse_sio_putstr (out, buf); + qse_sio_putstr (out, QSE_T("\n")); + break; + } + + qse_sio_putwcs (out, buf); } - qse_sio_close (sio); + qse_sio_close (out); + qse_sio_close (in); return 0; } static int test3 (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") - }; - int i; - qse_sio_t* sio; + qse_ssize_t n; + qse_mchar_t buf[20]; + qse_sio_t* in, * out; + + in = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR); + out = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); - sio = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_NOAUTOFLUSH); - if (sio == QSE_NULL) return -1; - - for (i = 0; i < QSE_COUNTOF(x); i++) + qse_sio_putstr (out, QSE_T("Type something here:\n")); + while (1) { - qse_sio_putmbsn (sio, x[i], qse_mbslen(x[i])); - qse_sio_putmb (sio, QSE_MT('\n')); + n = qse_sio_getmbs (in, buf, QSE_COUNTOF(buf)); + if (n == 0) break; + if (n < 0) + { + qse_char_t buf[32]; + qse_fmtintmax (buf, QSE_COUNTOF(buf), qse_sio_geterrnum(in), 10, -1, QSE_T('\0'), QSE_NULL); + qse_sio_putstr (out, QSE_T("error .... ")); + qse_sio_putstr (out, buf); + qse_sio_putstr (out, QSE_T("\n")); + break; + } + + qse_sio_putmbs (out, buf); } - qse_sio_close (sio); + qse_sio_close (out); + qse_sio_close (in); return 0; } int main () { +#if 0 #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 #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")); + setlocale (LC_ALL, ""); + + g_out = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); R (test1); R (test2); R (test3); + qse_sio_close (g_out); + +#if 0 #if defined(_WIN32) SetConsoleOutputCP (old_cp); +#endif #endif return 0; } diff --git a/qse/samples/cmn/sio03.c b/qse/samples/cmn/sio03.c new file mode 100644 index 00000000..b34a3741 --- /dev/null +++ b/qse/samples/cmn/sio03.c @@ -0,0 +1,157 @@ +#include +#include +#include + +#include + +#if defined(_WIN32) +# include +#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[] = + { + /* ugly hack for old compilers that don't support \u */ + /*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4!",*/ + 0xB108, + L' ', + 0xBB50, + 0xAC00, + L' ', + 0xC798, + 0xB0AC, + 0xC5B4, + L'!', + L'\0' + }; + + const qse_wchar_t unistr2[] = + { + /* this include an illegal unicode character. + * a strict converter should return an error so a question mark + * should be printed for such a character */ + 0xFFFF53C0u, + 0xFFFF4912u, + 0xBA00u, + 0xFFFF1234u, + L'\0' + }; + const qse_wchar_t* x[] = + { + L"", + L"", + L"", + L"Fly to the universe, kick your ass" + }; + int i; + qse_sio_t* sio; + + x[1] = unistr; + x[2] = unistr2; + + sio = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_NOAUTOFLUSH); + if (sio == QSE_NULL) return -1; + + for (i = 0; i < QSE_COUNTOF(x); i++) + { + qse_sio_putwcs (sio, x[i]); + qse_sio_putwc (sio, QSE_WT('\n')); + } + + qse_sio_close (sio); + return 0; +} + +static int test2 (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") + }; + int i; + qse_sio_t* sio; + + sio = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_NOAUTOFLUSH); + if (sio == QSE_NULL) return -1; + + for (i = 0; i < QSE_COUNTOF(x); i++) + { + qse_sio_putmbs (sio, x[i]); + qse_sio_putmb (sio, QSE_MT('\n')); + } + + qse_sio_close (sio); + return 0; +} + +static int test3 (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") + }; + int i; + qse_sio_t* sio; + + sio = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_NOAUTOFLUSH); + if (sio == QSE_NULL) return -1; + + for (i = 0; i < QSE_COUNTOF(x); i++) + { + qse_sio_putmbsn (sio, x[i], qse_mbslen(x[i])); + qse_sio_putmb (sio, QSE_MT('\n')); + } + + qse_sio_close (sio); + 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); + +#if defined(_WIN32) + SetConsoleOutputCP (old_cp); +#endif + return 0; +}