From 8b0e522b272902afe75999892a05c07fce1a9557 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 20 Oct 2013 14:14:10 +0000 Subject: [PATCH] added some convenience routines into sio --- qse/cmd/awk/awk.c | 5 +- qse/cmd/http/httpd.c | 5 +- qse/cmd/sed/sed.c | 5 +- qse/cmd/xli/xli.c | 107 +++++++++++++++++++++----------------- qse/include/qse/cmn/sio.h | 20 +++++++ qse/lib/cmn/sio.c | 85 ++++++++++++++++++++++++++++++ qse/lib/xli/xli.c | 1 + qse/samples/cmn/sio03.c | 6 +-- qse/samples/cmn/tre01.c | 4 +- 9 files changed, 181 insertions(+), 57 deletions(-) diff --git a/qse/cmd/awk/awk.c b/qse/cmd/awk/awk.c index db34415c..1d5d24a7 100644 --- a/qse/cmd/awk/awk.c +++ b/qse/cmd/awk/awk.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -1230,7 +1231,9 @@ int qse_main (int argc, qse_achar_t* argv[]) } else { - sprintf (locale, ".%u", (unsigned int)codepage); + /* .codepage */ + qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale), + codepage, 10, -1, QSE_MT('\0'), QSE_MT(".")); setlocale (LC_ALL, locale); qse_setdflcmgrbyid (QSE_CMGR_SLMB); } diff --git a/qse/cmd/http/httpd.c b/qse/cmd/http/httpd.c index 3347617d..09c50fa6 100644 --- a/qse/cmd/http/httpd.c +++ b/qse/cmd/http/httpd.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -1950,7 +1951,9 @@ int qse_main (int argc, qse_achar_t* argv[]) } else { - sprintf (locale, ".%u", (unsigned int)codepage); + /* .codepage */ + qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale), + codepage, 10, -1, QSE_MT('\0'), QSE_MT(".")); setlocale (LC_ALL, locale); qse_setdflcmgrbyid (QSE_CMGR_SLMB); } diff --git a/qse/cmd/sed/sed.c b/qse/cmd/sed/sed.c index 0f7314ec..a3e4faec 100644 --- a/qse/cmd/sed/sed.c +++ b/qse/cmd/sed/sed.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -1031,7 +1032,9 @@ int qse_main (int argc, qse_achar_t* argv[]) } else { - sprintf (locale, ".%u", (unsigned int)codepage); + /* .codepage */ + qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale), + codepage, 10, -1, QSE_MT('\0'), QSE_MT(".")); setlocale (LC_ALL, locale); qse_setdflcmgrbyid (QSE_CMGR_SLMB); } diff --git a/qse/cmd/xli/xli.c b/qse/cmd/xli/xli.c index e055075d..074b3b77 100644 --- a/qse/cmd/xli/xli.c +++ b/qse/cmd/xli/xli.c @@ -31,6 +31,7 @@ #include #include #include +#include #include @@ -116,38 +117,38 @@ static qse_mmgr_t xma_mmgr = static void print_version (void) { - qse_printf (QSE_T("QSEXLI version %hs\n"), QSE_PACKAGE_VERSION); + qse_putstrf (QSE_T("QSEXLI version %hs\n"), QSE_PACKAGE_VERSION); } static void print_usage (QSE_FILE* out, int argc, qse_char_t* argv[]) { const qse_char_t* b = qse_basename (argv[0]); - qse_fprintf (out, QSE_T("USAGE: %s [options] -i input-file [key]\n"), b); + qse_sio_putstrf (out, QSE_T("USAGE: %s [options] -i input-file [key]\n"), b); - qse_fprintf (out, QSE_T("options as follows:\n")); - qse_fprintf (out, QSE_T(" -h/--help show this message\n")); - qse_fprintf (out, QSE_T(" --version show version\n")); - qse_fprintf (out, QSE_T(" -i file specify an input file\n")); - qse_fprintf (out, QSE_T(" -o file specify an output file\n")); - qse_fprintf (out, QSE_T(" -u disallow duplicate keys\n")); - qse_fprintf (out, QSE_T(" -a allow a key alias\n")); - qse_fprintf (out, QSE_T(" -f keep file inclusion info\n")); - qse_fprintf (out, QSE_T(" -t keep comment text\n")); - qse_fprintf (out, QSE_T(" -s allow multi-segmented strings\n")); - qse_fprintf (out, QSE_T(" -d allow a leading digit in identifiers\n")); - qse_fprintf (out, QSE_T(" -n disallow nil\n")); - qse_fprintf (out, QSE_T(" -l disallow lists\n")); - qse_fprintf (out, QSE_T(" -K allow key tags\n")); - qse_fprintf (out, QSE_T(" -S allow string tags\n")); - qse_fprintf (out, QSE_T(" -v perform validation\n")); - qse_fprintf (out, QSE_T(" -m number specify the maximum amount of memory to use in bytes\n")); + qse_sio_putstrf (out, QSE_T("options as follows:\n")); + qse_sio_putstrf (out, QSE_T(" -h/--help show this message\n")); + qse_sio_putstrf (out, QSE_T(" --version show version\n")); + qse_sio_putstrf (out, QSE_T(" -i file specify an input file\n")); + qse_sio_putstrf (out, QSE_T(" -o file specify an output file\n")); + qse_sio_putstrf (out, QSE_T(" -u disallow duplicate keys\n")); + qse_sio_putstrf (out, QSE_T(" -a allow a key alias\n")); + qse_sio_putstrf (out, QSE_T(" -f keep file inclusion info\n")); + qse_sio_putstrf (out, QSE_T(" -t keep comment text\n")); + qse_sio_putstrf (out, QSE_T(" -s allow multi-segmented strings\n")); + qse_sio_putstrf (out, QSE_T(" -d allow a leading digit in identifiers\n")); + qse_sio_putstrf (out, QSE_T(" -n disallow nil\n")); + qse_sio_putstrf (out, QSE_T(" -l disallow lists\n")); + qse_sio_putstrf (out, QSE_T(" -K allow key tags\n")); + qse_sio_putstrf (out, QSE_T(" -S allow string tags\n")); + qse_sio_putstrf (out, QSE_T(" -v perform validation\n")); + qse_sio_putstrf (out, QSE_T(" -m number specify the maximum amount of memory to use in bytes\n")); #if defined(QSE_BUILD_DEBUG) - qse_fprintf (out, QSE_T(" -X number fail the number'th memory allocation\n")); + qse_sio_putstrf (out, QSE_T(" -X number fail the number'th memory allocation\n")); #endif #if defined(QSE_CHAR_IS_WCHAR) - qse_fprintf (out, QSE_T(" --infile-encoding string specify input file encoding name\n")); - qse_fprintf (out, QSE_T(" --outfile-encoding string specify output file encoding name\n")); + qse_sio_putstrf (out, QSE_T(" --infile-encoding string specify input file encoding name\n")); + qse_sio_putstrf (out, QSE_T(" --outfile-encoding string specify output file encoding name\n")); #endif } @@ -180,23 +181,23 @@ static int handle_args (int argc, qse_char_t* argv[]) switch (c) { default: - print_usage (QSE_STDERR, argc, argv); + print_usage (qse_getstderr(), argc, argv); goto oops; case QSE_T('?'): - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (qse_getstderr(), QSE_T("ERROR: bad option - %c\n"), opt.opt ); - print_usage (QSE_STDERR, argc, argv); + print_usage (qse_getstderr(), argc, argv); goto oops; case QSE_T(':'): - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (qse_getstderr(), QSE_T("ERROR: bad parameter for %c\n"), opt.opt ); - print_usage (QSE_STDERR, argc, argv); + print_usage (qse_getstderr(), argc, argv); goto oops; case QSE_T('h'): @@ -277,7 +278,7 @@ static int handle_args (int argc, qse_char_t* argv[]) g_infile_cmgr = qse_findcmgr (opt.arg); if (g_infile_cmgr == QSE_NULL) { - qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown input file encoding - %s\n"), opt.arg); + qse_sio_putstrf (qse_getstderr(), QSE_T("ERROR: unknown input file encoding - %s\n"), opt.arg); goto oops; } } @@ -286,7 +287,7 @@ static int handle_args (int argc, qse_char_t* argv[]) g_outfile_cmgr = qse_findcmgr (opt.arg); if (g_outfile_cmgr == QSE_NULL) { - qse_fprintf (QSE_STDERR, QSE_T("ERROR: unknown output file encoding - %s\n"), opt.arg); + qse_sio_putstrf (qse_getstderr(), QSE_T("ERROR: unknown output file encoding - %s\n"), opt.arg); goto oops; } } @@ -298,7 +299,7 @@ static int handle_args (int argc, qse_char_t* argv[]) if (!g_input_file) { - print_usage (QSE_STDERR, argc, argv); + print_usage (qse_getstderr(), argc, argv); goto oops; } @@ -306,7 +307,7 @@ static int handle_args (int argc, qse_char_t* argv[]) if (opt.ind < argc) { - print_usage (QSE_STDERR, argc, argv); + print_usage (qse_getstderr(), argc, argv); goto oops; } @@ -325,7 +326,7 @@ void print_exec_error (qse_xli_t* xli) const qse_xli_loc_t* errloc = qse_xli_geterrloc(xli); if (errloc->line > 0 || errloc->colm > 0) { - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (qse_getstderr(), QSE_T("ERROR: cannot execute - %s at line %lu column %lu\n"), qse_xli_geterrmsg(xli), (unsigned long)errloc->line, @@ -334,7 +335,7 @@ void print_exec_error (qse_xli_t* xli) } else { - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (qse_getstderr(), QSE_T("ERROR: cannot execute - %s\n"), qse_xli_geterrmsg(xli) ); @@ -368,7 +369,7 @@ static int xli_main (int argc, qse_char_t* argv[]) xma_mmgr.ctx = qse_xma_open (QSE_MMGR_GETDFL(), 0, g_memlimit); if (xma_mmgr.ctx == QSE_NULL) { - qse_printf (QSE_T("ERROR: cannot open memory heap\n")); + qse_sio_putstrf (qse_getstderr(), QSE_T("ERROR: cannot open memory heap\n")); goto oops; } mmgr = &xma_mmgr; @@ -377,7 +378,7 @@ static int xli_main (int argc, qse_char_t* argv[]) xli = qse_xli_openstdwithmmgr (mmgr, 0, 0); if (xli == QSE_NULL) { - qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open stream editor\n")); + qse_sio_putstrf (qse_getstderr(), QSE_T("ERROR: cannot open stream editor\n")); goto oops; } @@ -494,7 +495,7 @@ for (i = 0; i < QSE_COUNTOF(defs); i++) qse_xli_definepair (xli, defs[i].name, & if (errloc->line > 0 || errloc->colm > 0) { - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (qse_getstderr(), QSE_T("ERROR: cannot read %s - %s at line %lu column %lu%s%s\n"), g_input_file, qse_xli_geterrmsg(xli), @@ -506,7 +507,7 @@ for (i = 0; i < QSE_COUNTOF(defs); i++) qse_xli_definepair (xli, defs[i].name, & } else { - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (qse_getstderr(), QSE_T("ERROR: cannot read %s - %s\n"), g_input_file, qse_xli_geterrmsg(xli) @@ -525,7 +526,7 @@ for (i = 0; i < QSE_COUNTOF(defs); i++) qse_xli_definepair (xli, defs[i].name, & if (qse_xli_insertpairwithstrs (xli, qse_xli_getroot(xli), QSE_NULL, QSE_T("test-key"), QSE_NULL, QSE_NULL, strs, QSE_COUNTOF(strs)) == QSE_NULL) { - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (qse_getstderr(), QSE_T("ERROR: cannot insert a string pair - %s \n"), qse_xli_geterrmsg(xli) ); @@ -538,12 +539,12 @@ for (i = 0; i < QSE_COUNTOF(defs); i++) qse_xli_definepair (xli, defs[i].name, & qse_size_t count; count = qse_xli_countpairs (xli, QSE_NULL, g_lookup_key); - qse_printf (QSE_T("COUNT: %lu\n"), (unsigned long)count); + qse_putstrf (QSE_T("COUNT: %lu\n"), (unsigned long)count); pair = qse_xli_findpair (xli, QSE_NULL, g_lookup_key); if (pair == QSE_NULL) { - qse_fprintf (QSE_STDERR, + qse_sio_putstrf (qse_getstderr(), QSE_T("ERROR: cannot find %s - %s \n"), g_lookup_key, qse_xli_geterrmsg(xli) @@ -555,15 +556,15 @@ for (i = 0; i < QSE_COUNTOF(defs); i++) qse_xli_definepair (xli, defs[i].name, & if (pair->val->type == QSE_XLI_STR) { qse_xli_str_t* str = (qse_xli_str_t*)pair->val; - qse_printf (QSE_T("[%.*s]\n"), (int)str->len, str->ptr); + qse_putstrf (QSE_T("[%.*s]\n"), (int)str->len, str->ptr); } else if (pair->val->type == QSE_XLI_NIL) { - qse_printf (QSE_T("#NIL\n")); + qse_putstrf (QSE_T("#NIL\n")); } else { - qse_printf (QSE_T("#LIST\n")); + qse_putstrf (QSE_T("#LIST\n")); } } } @@ -581,13 +582,13 @@ oops: #if defined(QSE_BUILD_DEBUG) if (g_failmalloc > 0) { - qse_fprintf (QSE_STDERR, QSE_T("\n")); - qse_fprintf (QSE_STDERR, QSE_T("-[MALLOC COUNTS]---------------------------------------\n")); - qse_fprintf (QSE_STDERR, QSE_T("ALLOC: %lu FREE: %lu: REALLOC: %lu\n"), + qse_sio_putstrf (qse_getstderr(), QSE_T("\n")); + qse_sio_putstrf (qse_getstderr(), QSE_T("-[MALLOC COUNTS]---------------------------------------\n")); + qse_sio_putstrf (qse_getstderr(), QSE_T("ALLOC: %lu FREE: %lu: REALLOC: %lu\n"), (unsigned long)debug_mmgr_alloc_count, (unsigned long)debug_mmgr_free_count, (unsigned long)debug_mmgr_realloc_count); - qse_fprintf (QSE_STDERR, QSE_T("-------------------------------------------------------\n")); + qse_sio_putstrf (qse_getstderr(), QSE_T("-------------------------------------------------------\n")); } #endif @@ -596,6 +597,8 @@ oops: int qse_main (int argc, qse_achar_t* argv[]) { + int x; + #if defined(_WIN32) char locale[100]; UINT codepage = GetConsoleOutputCP(); @@ -606,7 +609,9 @@ int qse_main (int argc, qse_achar_t* argv[]) } else { - sprintf (locale, ".%u", (unsigned int)codepage); + /* .codepage */ + qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale), + codepage, 10, -1, QSE_MT('\0'), QSE_MT(".")); setlocale (LC_ALL, locale); qse_setdflcmgrbyid (QSE_CMGR_SLMB); } @@ -615,6 +620,10 @@ int qse_main (int argc, qse_achar_t* argv[]) qse_setdflcmgrbyid (QSE_CMGR_SLMB); #endif - return qse_runmain (argc, argv, xli_main); + qse_openstdsios (); + x = qse_runmain (argc, argv, xli_main); + qse_closestdsios (); + + return x; } diff --git a/qse/include/qse/cmn/sio.h b/qse/include/qse/cmn/sio.h index 0bbbe7a8..61d82254 100644 --- a/qse/include/qse/cmn/sio.h +++ b/qse/include/qse/cmn/sio.h @@ -358,6 +358,26 @@ int qse_sio_movetoend (qse_sio_t* sio); #endif +QSE_EXPORT int qse_openstdsios (void); +QSE_EXPORT void qse_closestdsios (void); +QSE_EXPORT qse_sio_t* qse_getstdout (void); +QSE_EXPORT qse_sio_t* qse_getstderr (void); + +QSE_EXPORT qse_ssize_t qse_putmbsf ( + const qse_mchar_t* fmt, + ... +); + +QSE_EXPORT qse_ssize_t qse_putwcsf ( + const qse_wchar_t* fmt, + ... +); + +QSE_EXPORT qse_ssize_t qse_putstrf ( + const qse_char_t* fmt, + ... +); + #ifdef __cplusplus } #endif diff --git a/qse/lib/cmn/sio.c b/qse/lib/cmn/sio.c index d3f9b126..df52cf9e 100644 --- a/qse/lib/cmn/sio.c +++ b/qse/lib/cmn/sio.c @@ -718,4 +718,89 @@ static qse_ssize_t file_output ( return 0; } +static qse_sio_t* sio_stdout = QSE_NULL; +static qse_sio_t* sio_stderr = QSE_NULL; +int qse_openstdsios (void) +{ + if (sio_stdout == QSE_NULL) + { + sio_stdout = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, 0); + } + if (sio_stderr == QSE_NULL) + { + sio_stderr = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDERR, 0); + } + + if (sio_stdout == QSE_NULL || sio_stderr == QSE_NULL) + { + qse_closestdsios (); + return -1; + } + + return 0; +} + +void qse_closestdsios (void) +{ + if (sio_stderr) + { + qse_sio_close (sio_stderr); + sio_stderr = QSE_NULL; + } + if (sio_stdout) + { + qse_sio_close (sio_stdout); + sio_stdout = QSE_NULL; + } +} + +qse_sio_t* qse_getstdout (void) +{ + return sio_stdout; +} + +qse_sio_t* qse_getstderr (void) +{ + return sio_stderr; +} + +qse_ssize_t qse_putmbsf (const qse_mchar_t* fmt, ...) +{ + va_list ap; + qse_ssize_t x; + + va_start (ap, fmt); + x = qse_mxprintf (fmt, put_mchar, put_wchar, sio_stdout, ap); + va_end (ap); + + return x; +} + +qse_ssize_t qse_putwcsf (const qse_wchar_t* fmt, ...) +{ + va_list ap; + qse_ssize_t x; + + va_start (ap, fmt); + x = qse_wxprintf (fmt, put_wchar, put_mchar, sio_stdout, ap); + va_end (ap); + + return x; +} + +qse_ssize_t qse_putstrf (const qse_char_t* fmt, ...) +{ + va_list ap; + qse_ssize_t x; + + va_start (ap, fmt); +#if defined(QSE_CHAR_IS_MCHAR) + x = qse_mxprintf (fmt, put_mchar, put_wchar, sio_stdout, ap); +#else + x = qse_wxprintf (fmt, put_wchar, put_mchar, sio_stdout, ap); +#endif + va_end (ap); + + return x; +} diff --git a/qse/lib/xli/xli.c b/qse/lib/xli/xli.c index 01541c71..2a7c27af 100644 --- a/qse/lib/xli/xli.c +++ b/qse/lib/xli/xli.c @@ -980,6 +980,7 @@ qse_xli_pair_t* qse_xli_setpair (qse_xli_t* xli, const qse_char_t* fqpn, const q if (pair == QSE_NULL) { +/* TODO: honor QSE_XLI_VALIDATE.... */ /* insert a new item..... */ if (*ptr == QSE_T('\0')) { diff --git a/qse/samples/cmn/sio03.c b/qse/samples/cmn/sio03.c index 26b904e3..475b2ba1 100644 --- a/qse/samples/cmn/sio03.c +++ b/qse/samples/cmn/sio03.c @@ -143,12 +143,12 @@ int main () } else { - sprintf (locale, ".%u", (unsigned int)codepage); - setlocale (LC_ALL, locale); + sprintf (locale, ".%u", (unsigned int)codepage); + setlocale (LC_ALL, locale); qse_setdflcmgrbyid (QSE_CMGR_SLMB); } #else - setlocale (LC_ALL, ""); + setlocale (LC_ALL, ""); qse_setdflcmgrbyid (QSE_CMGR_SLMB); #endif diff --git a/qse/samples/cmn/tre01.c b/qse/samples/cmn/tre01.c index 4566c43c..c1f69926 100644 --- a/qse/samples/cmn/tre01.c +++ b/qse/samples/cmn/tre01.c @@ -27,7 +27,7 @@ static int test_main (int argc, qse_char_t* argv[]) qse_tre_init (&tre, QSE_MMGR_GETDFL()); - if (qse_tre_comp (&tre, argv[1], &nsubmat, 0 /*QSE_TRE_EXTENDED*/) <= -1) + if (qse_tre_comp (&tre, argv[1], &nsubmat, QSE_TRE_EXTENDED) <= -1) { qse_printf (QSE_T("ERROR: Cannot compile pattern [%s] - %s\n"), argv[1], qse_tre_geterrmsg(&tre)); goto oops; @@ -92,7 +92,7 @@ int qse_main (int argc, qse_achar_t* argv[]) qse_setdflcmgrbyid (QSE_CMGR_SLMB); } #else - setlocale (LC_ALL, ""); + setlocale (LC_ALL, ""); qse_setdflcmgrbyid (QSE_CMGR_SLMB); #endif return qse_runmain (argc, argv, test_main);