diff --git a/qse/cmd/http/httpd.c b/qse/cmd/http/httpd.c index 25ba43e0..f6682a9f 100644 --- a/qse/cmd/http/httpd.c +++ b/qse/cmd/http/httpd.c @@ -1237,7 +1237,7 @@ static int load_server_config (qse_httpd_t* httpd, qse_httpd_server_t* server, q qse_xli_pair_t* host; qse_char_t buf[32]; - qse_sprintf (buf, QSE_COUNTOF(buf), QSE_T("host[%d]"), i); + qse_strxfmt (buf, QSE_COUNTOF(buf), QSE_T("host[%d]"), i); host = qse_xli_findpair (httpd_xtn->xli, list, buf); if (!host) break; @@ -1254,7 +1254,7 @@ static int load_server_config (qse_httpd_t* httpd, qse_httpd_server_t* server, q j--; - qse_sprintf (buf, QSE_COUNTOF(buf), QSE_T("location[%d]"), j); + qse_strxfmt (buf, QSE_COUNTOF(buf), QSE_T("location[%d]"), j); loc = qse_xli_findpair (httpd_xtn->xli, (qse_xli_list_t*)host->val, buf); if (!loc) break; @@ -1605,7 +1605,7 @@ static int load_config (qse_httpd_t* httpd) for (i = 0; ; i++) { qse_char_t buf[32]; - qse_sprintf (buf, QSE_COUNTOF(buf), QSE_T("server[%d]"), i); + qse_strxfmt (buf, QSE_COUNTOF(buf), QSE_T("server[%d]"), i); pair = qse_xli_findpair (httpd_xtn->xli, QSE_NULL, buf); if (pair == QSE_NULL) break; @@ -1669,7 +1669,7 @@ static void reconf_server (qse_httpd_t* httpd, qse_httpd_server_t* server) if (httpd_xtn->xli) { qse_char_t buf[32]; - qse_sprintf (buf, QSE_COUNTOF(buf), QSE_T("server[%d]"), server_xtn->num); + qse_strxfmt (buf, QSE_COUNTOF(buf), QSE_T("server[%d]"), server_xtn->num); pair = qse_xli_findpair (httpd_xtn->xli, QSE_NULL, buf); if (pair && pair->val->type == QSE_XLI_LIST) diff --git a/qse/include/qse/cmn/sio.h b/qse/include/qse/cmn/sio.h index 394f32a5..1ae6c2ef 100644 --- a/qse/include/qse/cmn/sio.h +++ b/qse/include/qse/cmn/sio.h @@ -319,12 +319,6 @@ QSE_EXPORT qse_ssize_t qse_sio_putwcsf ( ... ); -QSE_EXPORT qse_ssize_t qse_sio_putstrf ( - qse_sio_t* sio, - const qse_char_t* fmt, - ... -); - QSE_EXPORT qse_ssize_t qse_sio_putmbsvf ( qse_sio_t* sio, const qse_mchar_t* fmt, @@ -337,20 +331,18 @@ QSE_EXPORT qse_ssize_t qse_sio_putwcsvf ( va_list ap ); -QSE_EXPORT qse_ssize_t qse_sio_putstrvf ( - qse_sio_t* sio, - const qse_char_t* fmt, - va_list ap -); - #if defined(QSE_CHAR_IS_MCHAR) # define qse_sio_putc(sio,c) qse_sio_putmb(sio,c) # define qse_sio_putstr(sio,str) qse_sio_putmbs(sio,str) # define qse_sio_putstrn(sio,str,size) qse_sio_putmbsn(sio,str,size) +# define qse_sio_putstrf qse_sio_putmbsf +# define qse_sio_putstrvf(sio,fmt,ap) qse_sio_putmbsvf(sio,fmt,ap) #else # define qse_sio_putc(sio,c) qse_sio_putwc(sio,c) # define qse_sio_putstr(sio,str) qse_sio_putwcs(sio,str) # define qse_sio_putstrn(sio,str,size) qse_sio_putwcsn(sio,str,size) +# define qse_sio_putstrf qse_sio_putwcsf +# define qse_sio_putstrvf(sio,fmt,ap) qse_sio_putwcsvf(sio,fmt,ap) #endif /** @@ -393,11 +385,6 @@ QSE_EXPORT qse_ssize_t qse_putwcsf ( ... ); -QSE_EXPORT qse_ssize_t qse_putstrf ( - const qse_char_t* fmt, - ... -); - QSE_EXPORT qse_ssize_t qse_putmbsvf ( const qse_mchar_t* fmt, va_list ap @@ -408,11 +395,38 @@ QSE_EXPORT qse_ssize_t qse_putwcsvf ( va_list ap ); -QSE_EXPORT qse_ssize_t qse_putstrvf ( - const qse_char_t* fmt, - va_list ap +QSE_EXPORT qse_ssize_t qse_errputmbsf ( + const qse_mchar_t* fmt, + ... ); +QSE_EXPORT qse_ssize_t qse_errputwcsf ( + const qse_wchar_t* fmt, + ... +); + +QSE_EXPORT qse_ssize_t qse_errputmbsvf ( + const qse_mchar_t* fmt, + va_list ap +); + +QSE_EXPORT qse_ssize_t qse_errputwcsvf ( + const qse_wchar_t* fmt, + va_list ap +); + +#if defined(QSE_CHAR_IS_MCHAR) +# define qse_putstrf qse_putmbsf +# define qse_putstrvf(fmt,ap) qse_putmbsvf(fmt,ap) +# define qse_errputstrf qse_errputmbsf +# define qse_errputstrvf(fmt,ap) qse_errputmbsvf(fmt,ap) +#else +# define qse_putstrf qse_putwcsf +# define qse_putstrvf(fmt,ap) qse_putwcsvf(fmt,ap) +# define qse_errputstrf qse_errputwcsf +# define qse_errputstrvf(fmt,ap) qse_errputwcsvf(fmt,ap) +#endif + #ifdef __cplusplus } #endif diff --git a/qse/include/qse/cmn/str.h b/qse/include/qse/cmn/str.h index c41a696e..36e8a577 100644 --- a/qse/include/qse/cmn/str.h +++ b/qse/include/qse/cmn/str.h @@ -712,18 +712,14 @@ QSE_EXPORT qse_size_t qse_wcsxfmt ( ... ); -QSE_EXPORT qse_size_t qse_strfmt ( - qse_char_t* buf, - const qse_char_t* fmt, - ... -); -QSE_EXPORT qse_size_t qse_strxfmt ( - qse_char_t* buf, - qse_size_t bsz, - const qse_char_t* fmt, - ... -); +#if defined(QSE_CHAR_IS_MCHAR) +# define qse_strfmt qse_mbsfmt +# define qse_strxfmt qse_mbsxfmt +#else +# define qse_strfmt qse_wcsfmt +# define qse_strxfmt qse_wcsxfmt +#endif /** * The qse_mbsxsubst() function expands \a fmt into a buffer \a buf of the size diff --git a/qse/lib/awk/mod-sys.c b/qse/lib/awk/mod-sys.c index 8596e476..00261360 100644 --- a/qse/lib/awk/mod-sys.c +++ b/qse/lib/awk/mod-sys.c @@ -43,7 +43,6 @@ #endif #include /* getenv */ -#include /* qse_sprintf */ static int fnc_fork (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) { @@ -552,7 +551,7 @@ static int fnc_getnwifcfg (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) md[4].key.ptr = QSE_T("ethw"); md[4].key.len = 4; md[4].type = QSE_AWK_VAL_MAP_DATA_STR; - qse_sprintf (ethw, QSE_COUNTOF(ethw), QSE_T("%02X:%02X:%02X:%02X:%02X:%02X"), + qse_strxfmt (ethw, QSE_COUNTOF(ethw), QSE_T("%02X:%02X:%02X:%02X:%02X:%02X"), cfg.ethw[0], cfg.ethw[1], cfg.ethw[2], cfg.ethw[3], cfg.ethw[4], cfg.ethw[5]); md[4].vptr = ethw; diff --git a/qse/lib/awk/run.c b/qse/lib/awk/run.c index a669998d..a331a71e 100644 --- a/qse/lib/awk/run.c +++ b/qse/lib/awk/run.c @@ -22,7 +22,7 @@ #include #ifdef DEBUG_RUN -#include +#include #endif #define PRINT_IOERR -99 @@ -1889,7 +1889,7 @@ static int run_block0 (qse_awk_rtx_t* rtx, qse_awk_nde_blk_t* nde) nlcls = nde->nlcls; #ifdef DEBUG_RUN - qse_dprintf ( + qse_errputstrf ( QSE_T("securing space for local variables nlcls = %d\n"), (int)nlcls); #endif @@ -1911,7 +1911,7 @@ static int run_block0 (qse_awk_rtx_t* rtx, qse_awk_nde_blk_t* nde) } #ifdef DEBUG_RUN - qse_dprintf (QSE_T("executing block statements\n")); + qse_errputstrf (QSE_T("executing block statements\n")); #endif while (p != QSE_NULL && rtx->exit_level == EXIT_NONE) @@ -1926,7 +1926,7 @@ static int run_block0 (qse_awk_rtx_t* rtx, qse_awk_nde_blk_t* nde) /* pop off local variables */ #ifdef DEBUG_RUN - qse_dprintf (QSE_T("popping off local variables\n")); + qse_errputstrf (QSE_T("popping off local variables\n")); #endif nlcls = nde->nlcls; while (nlcls > 0) @@ -3759,7 +3759,7 @@ retry: if (str == QSE_NULL) return QSE_NULL; #ifdef DEBUG_RUN - qse_dprintf (QSE_T("**** index str=>%s, map->ref=%d, map->type=%d\n"), + qse_errputstrf (QSE_T("**** index str=>%s, map->ref=%d, map->type=%d\n"), str, (int)map->ref, (int)map->type); #endif @@ -5708,7 +5708,7 @@ static qse_awk_val_t* __eval_call ( saved_stack_top = run->stack_top; #ifdef DEBUG_RUN - qse_dprintf (QSE_T("setting up function stack frame top=%ld base=%ld\n"), + qse_errputstrf (QSE_T("setting up function stack frame top=%ld base=%ld\n"), (long)run->stack_top, (long)run->stack_base); #endif if (__raw_push(run,(void*)run->stack_base) == -1) @@ -5775,7 +5775,7 @@ static qse_awk_val_t* __eval_call ( RTX_STACK_NARGS(run) = (void*)nargs; #ifdef DEBUG_RUN - qse_dprintf (QSE_T("running function body\n")); + qse_errputstrf (QSE_T("running function body\n")); #endif if (fun != QSE_NULL) @@ -5825,7 +5825,7 @@ static qse_awk_val_t* __eval_call ( /* refdown args in the run.stack */ nargs = (qse_size_t)RTX_STACK_NARGS(run); #ifdef DEBUG_RUN - qse_dprintf (QSE_T("block run complete nargs = %d\n"), (int)nargs); + qse_errputstrf (QSE_T("block run complete nargs = %d\n"), (int)nargs); #endif for (i = 0; i < nargs; i++) @@ -5834,7 +5834,7 @@ static qse_awk_val_t* __eval_call ( } #ifdef DEBUG_RUN - qse_dprintf (QSE_T("got return value\n")); + qse_errputstrf (QSE_T("got return value\n")); #endif v = RTX_STACK_RETVAL(run); @@ -5889,7 +5889,7 @@ static qse_awk_val_t* __eval_call ( if (run->exit_level == EXIT_FUNCTION) run->exit_level = EXIT_NONE; #ifdef DEBUG_RUN - qse_dprintf (QSE_T("returning from function top=%ld, base=%ld\n"), + qse_errputstrf (QSE_T("returning from function top=%ld, base=%ld\n"), (long)run->stack_top, (long)run->stack_base); #endif return (n == -1)? QSE_NULL: v; @@ -6583,7 +6583,7 @@ read_again: } #ifdef DEBUG_RUN - qse_dprintf (QSE_T("record len = %d str=[%.*s]\n"), + qse_errputstrf (QSE_T("record len = %d str=[%.*s]\n"), (int)QSE_STR_LEN(buf), (int)QSE_STR_LEN(buf), QSE_STR_PTR(buf)); #endif if (n == 0) diff --git a/qse/lib/awk/val.c b/qse/lib/awk/val.c index 602e8671..0984829f 100644 --- a/qse/lib/awk/val.c +++ b/qse/lib/awk/val.c @@ -22,7 +22,7 @@ #include #ifdef DEBUG_VAL -#include +#include #endif #define CHUNKSIZE QSE_AWK_VAL_CHUNK_SIZE @@ -140,7 +140,7 @@ qse_awk_val_t* qse_awk_rtx_makeintval (qse_awk_rtx_t* rtx, qse_long_t v) val->nde = QSE_NULL; #ifdef DEBUG_VAL - qse_dprintf (QSE_T("makeintval => %ld [%p]\n"), (long)v, val); + qse_errputstrf (QSE_T("makeintval => %ld [%p]\n"), (long)v, val); #endif return (qse_awk_val_t*)val; } @@ -196,7 +196,7 @@ qse_awk_val_t* qse_awk_rtx_makefltval (qse_awk_rtx_t* rtx, qse_flt_t v) val->nde = QSE_NULL; #ifdef DEBUG_VAL - qse_dprintf (QSE_T("makefltval => %Lf [%p]\n"), (double)v, val); + qse_errputstrf (QSE_T("makefltval => %Lf [%p]\n"), (double)v, val); #endif return (qse_awk_val_t*)val; } @@ -342,7 +342,7 @@ init: qse_strncpy (val->val.ptr, str->ptr, str->len); #ifdef DEBUG_VAL - qse_dprintf (QSE_T("makestrval => %p\n"), val); + qse_errputstrf (QSE_T("makestrval => %p\n"), val); #endif return (qse_awk_val_t*)val; } @@ -402,7 +402,7 @@ init: qse_strncpy (&val->val.ptr[len1], str2, len2); #ifdef DEBUG_VAL - qse_dprintf (QSE_T("makestrval2 => %p\n"), val); + qse_errputstrf (QSE_T("makestrval2 => %p\n"), val); #endif return (qse_awk_val_t*)val; } @@ -477,9 +477,9 @@ static void free_mapval (qse_htb_t* map, void* dptr, qse_size_t dlen) qse_awk_rtx_t* rtx = *(qse_awk_rtx_t**)QSE_XTN(map); #ifdef DEBUG_VAL - qse_dprintf (QSE_T("refdown in map free...")); + qse_errputstrf (QSE_T("refdown in map free...")); qse_awk_dprintval (rtx, dptr); - qse_dprintf (QSE_T("\n")); + qse_errputstrf (QSE_T("\n")); #endif qse_awk_rtx_refdownval (rtx, dptr); @@ -489,9 +489,9 @@ static void same_mapval (qse_htb_t* map, void* dptr, qse_size_t dlen) { qse_awk_rtx_t* run = *(qse_awk_rtx_t**)QSE_XTN(map); #ifdef DEBUG_VAL - qse_dprintf (QSE_T("refdown nofree in map free...")); + qse_errputstrf (QSE_T("refdown nofree in map free...")); qse_awk_dprintval (run, dptr); - qse_dprintf (QSE_T("\n")); + qse_errputstrf (QSE_T("\n")); #endif qse_awk_rtx_refdownval_nofree (run, dptr); } @@ -765,9 +765,9 @@ void qse_awk_rtx_freeval ( if (IS_STATICVAL(val)) return; #ifdef DEBUG_VAL - qse_dprintf (QSE_T("freeing [cache=%d] ... "), cache); + qse_errputstrf (QSE_T("freeing [cache=%d] ... "), cache); qse_awk_dprintval (rtx, val); - qse_dprintf (QSE_T("\n")); + qse_errputstrf (QSE_T("\n")); #endif switch (val->type) @@ -857,9 +857,9 @@ void qse_awk_rtx_refupval (qse_awk_rtx_t* rtx, qse_awk_val_t* val) if (IS_STATICVAL(val)) return; #ifdef DEBUG_VAL - qse_dprintf (QSE_T("ref up [ptr=%p] [count=%d] "), val, (int)val->ref); + qse_errputstrf (QSE_T("ref up [ptr=%p] [count=%d] "), val, (int)val->ref); qse_awk_dprintval (rtx, val); - qse_dprintf (QSE_T("\n")); + qse_errputstrf (QSE_T("\n")); #endif val->ref++; @@ -870,9 +870,9 @@ void qse_awk_rtx_refdownval (qse_awk_rtx_t* rtx, qse_awk_val_t* val) if (IS_STATICVAL(val)) return; #ifdef DEBUG_VAL - qse_dprintf (QSE_T("ref down [ptr=%p] [count=%d]\n"), val, (int)val->ref); + qse_errputstrf (QSE_T("ref down [ptr=%p] [count=%d]\n"), val, (int)val->ref); qse_awk_dprintval (rtx, val); - qse_dprintf (QSE_T("\n")); + qse_errputstrf (QSE_T("\n")); #endif QSE_ASSERTX (val->ref > 0, @@ -1402,7 +1402,7 @@ int qse_awk_rtx_valtostr ( #ifdef DEBUG_VAL - qse_dprintf ( + qse_errputstrf ( QSE_T(">>WRONG VALUE TYPE [%d] in qse_awk_rtx_valtostr\n"), v->type ); @@ -1569,7 +1569,7 @@ int qse_awk_rtx_valtonum ( } #ifdef DEBUG_VAL - qse_dprintf ( + qse_errputstrf ( QSE_T(">>WRONG VALUE TYPE [%d] in qse_awk_rtx_valtonum()\n"), v->type ); @@ -1669,7 +1669,7 @@ qse_long_t qse_awk_rtx_hashval (qse_awk_rtx_t* rtx, qse_awk_val_t* v) default: #ifdef DEBUG_VAL - qse_dprintf ( + qse_errputstrf ( QSE_T(">>WRONG VALUE TYPE [%d] in qse_awk_rtx_hashval()\n"), v->type ); diff --git a/qse/lib/cmn/fmt-out.c b/qse/lib/cmn/fmt-out.c index 01ed0009..3d63c792 100644 --- a/qse/lib/cmn/fmt-out.c +++ b/qse/lib/cmn/fmt-out.c @@ -116,11 +116,11 @@ static const qse_wchar_t* w_hex2ascii = #undef ochar_t #undef T #undef OT +#undef CONV_MAX #undef toupper #undef hex2ascii #undef sprintn -#undef put_char -#undef put_ochar +#undef fmtout_t #undef fmtout #define char_t qse_mchar_t @@ -128,10 +128,10 @@ static const qse_wchar_t* w_hex2ascii = #define ochar_t qse_wchar_t #define T(x) QSE_MT(x) #define OT(x) QSE_WT(x) +#define CONV_MAX QSE_MBLEN_MAX #define toupper QSE_TOUPPER #define sprintn m_sprintn -#define put_char put_mchar -#define put_ochar put_wchar +#define fmtout_t qse_mfmtout_t #define fmtout qse_mfmtout #define hex2ascii(hex) (m_hex2ascii[hex]) @@ -145,11 +145,11 @@ static const qse_wchar_t* w_hex2ascii = #undef ochar_t #undef T #undef OT +#undef CONV_MAX #undef toupper #undef hex2ascii #undef sprintn -#undef put_char -#undef put_ochar +#undef fmtout_t #undef fmtout #define char_t qse_wchar_t @@ -157,10 +157,10 @@ static const qse_wchar_t* w_hex2ascii = #define ochar_t qse_mchar_t #define T(x) QSE_WT(x) #define OT(x) QSE_MT(x) +#define CONV_MAX 1 #define toupper QSE_TOWUPPER #define sprintn w_sprintn -#define put_char put_wchar -#define put_ochar put_mchar +#define fmtout_t qse_wfmtout_t #define fmtout qse_wfmtout #define hex2ascii(hex) (w_hex2ascii[hex]) diff --git a/qse/lib/cmn/fmt-out.h b/qse/lib/cmn/fmt-out.h index a1c91822..5f0b7e8a 100644 --- a/qse/lib/cmn/fmt-out.h +++ b/qse/lib/cmn/fmt-out.h @@ -84,21 +84,17 @@ static char_t* sprintn (char_t* nbuf, qse_uintmax_t num, int base, int *lenp, in /* NOTE: data output is aborted if the data limit is reached or * I/O error occurs */ +#undef PUT_CHAR + #define PUT_CHAR(c) do { \ int xx; \ if (data->count >= data->limit) goto done; \ - if ((xx = data->put_char (c, data->ctx)) <= -1) goto oops; \ + if ((xx = data->put (c, data->ctx)) <= -1) goto oops; \ + if (xx == 0) goto done; \ data->count += xx; \ } while (0) -#define PUT_OCHAR(c) do { \ - int xx; \ - if (data->count >= data->limit) goto done; \ - if ((xx = data->put_ochar (c, data->ctx)) <= -1) goto oops; \ - data->count += xx; \ -} while (0) - -int fmtout (const char_t* fmt, qse_fmtout_t* data, va_list ap) +int fmtout (const char_t* fmt, fmtout_t* data, va_list ap) { char_t nbuf[MAXNBUF]; const char_t* p, * percent; @@ -106,12 +102,11 @@ int fmtout (const char_t* fmt, qse_fmtout_t* data, va_list ap) uchar_t ch; char_t ach, padc, * sp; ochar_t oach, * osp; + qse_size_t oslen, slen; int lm_flag, lm_dflag, flagc, numlen; qse_uintmax_t num = 0; int stop = 0; - data->count = 0; - struct { qse_mchar_t sbuf[32]; @@ -126,6 +121,8 @@ int fmtout (const char_t* fmt, qse_fmtout_t* data, va_list ap) qse_size_t capa; } fltout; + data->count = 0; + fltfmt.ptr = fltfmt.sbuf; fltfmt.capa = QSE_COUNTOF(fltfmt.sbuf) - 1; @@ -389,13 +386,36 @@ reswitch: uppercase_c: oach = QSE_SIZEOF(ochar_t) < QSE_SIZEOF(int)? va_arg(ap, int): va_arg(ap, ochar_t); + oslen = 1; + if (data->conv (&oach, &oslen, QSE_NULL, &slen, data->ctx) <= -1) + { + /* conversion error */ + goto oops; + } + /* precision 0 doesn't kill the letter */ - width--; + width -= slen; if (!(flagc & FLAGC_LEFTADJ) && width > 0) { while (width--) PUT_CHAR (padc); } - PUT_OCHAR (oach); + + { + char_t conv_buf[CONV_MAX]; + qse_size_t i, conv_len; + + oslen = 1; + conv_len = QSE_COUNTOF(conv_buf); + + /* this must not fail since the dry-run above was successful */ + data->conv (&oach, &oslen, conv_buf, &conv_len, data->ctx); + + for (i = 0; i < conv_len; i++) + { + PUT_CHAR (conv_buf[i]); + } + } + if ((flagc & FLAGC_LEFTADJ) && width > 0) { while (width--) PUT_CHAR (padc); @@ -414,13 +434,11 @@ reswitch: print_lowercase_s: if (flagc & FLAGC_DOT) { - for (n = 0; n < precision && sp[n]; n++) continue; + for (n = 0; n < precision && sp[n]; n++); } else { - char_t* p = sp; - while (*p) p++; - n = p - sp; + for (n = 0; sp[n]; n++); } width -= n; @@ -445,24 +463,57 @@ reswitch: osp = va_arg (ap, ochar_t*); if (osp == QSE_NULL) osp = OT("(null)"); - if (flagc & FLAGC_DOT) + + /* get the length */ + for (oslen = 0; osp[oslen]; oslen++); + + if (data->conv (osp, &oslen, QSE_NULL, &slen, data->ctx) <= -1) { - for (n = 0; n < precision && osp[n]; n++) continue; - } - else - { - ochar_t* p = osp; - while (*p) p++; - n = p - osp; + /* conversion error */ + goto oops; } + /* slen hold the length after conversion */ + n = slen; + if ((flagc & FLAGC_DOT) && precision < slen) n = precision; width -= n; if (!(flagc & FLAGC_LEFTADJ) && width > 0) { while (width--) PUT_CHAR (padc); } - while (n--) PUT_OCHAR(*osp++); + + { + char_t conv_buf[CONV_MAX]; + qse_size_t i, conv_len, src_len, tot_len = 0; + while (n > 0) + { + QSE_ASSERT (oslen > tot_len); + + #if CONV_MAX == 1 + src_len = oslen - tot_len; + #else + src_len = 1; + #endif + conv_len = QSE_COUNTOF(conv_buf); + + /* this must not fail since the dry-run above was successful */ + data->conv (&osp[tot_len], &src_len, conv_buf, &conv_len, data->ctx); + tot_len += src_len; + + /* stop outputting if a converted character can't be printed + * in its entirety (limited by precision). but this is not an error */ + if (n < conv_len) break; + + for (i = 0; i < conv_len; i++) + { + PUT_CHAR (conv_buf[i]); + } + + n -= conv_len; + } + } + if ((flagc & FLAGC_LEFTADJ) && width > 0) { while (width--) PUT_CHAR (padc); @@ -754,5 +805,4 @@ oops: return (qse_ssize_t)-1; } #undef PUT_CHAR -#undef PUT_OCHAR diff --git a/qse/lib/cmn/fmt.h b/qse/lib/cmn/fmt.h index 48254441..8945958a 100644 --- a/qse/lib/cmn/fmt.h +++ b/qse/lib/cmn/fmt.h @@ -45,19 +45,72 @@ struct qse_fmtout_t typedef struct qse_fmtout_t qse_fmtout_t; + + +typedef int (*qse_mfmtout_put_t) ( + qse_mchar_t c, + void* ctx +); + +/* convert a wide string to a multi-byte string */ +typedef int (*qse_mfmtout_conv_t) ( + const qse_wchar_t* wcs, + qse_size_t* wcslen, + qse_mchar_t* mbs, + qse_size_t* mbslen, + void* ctx +); + +struct qse_mfmtout_t +{ + qse_size_t count; /* out */ + qse_size_t limit; /* in */ + void* ctx; /* in */ + qse_mfmtout_put_t put; /* in */ + qse_mfmtout_conv_t conv; /* in */ +}; + +typedef struct qse_mfmtout_t qse_mfmtout_t; + +typedef int (*qse_wfmtout_put_t) ( + qse_wchar_t c, + void* ctx +); + +/* convert a multi-byte string to a wide string */ +typedef int (*qse_wfmtout_conv_t) ( + const qse_mchar_t* mbs, + qse_size_t* mbslen, + qse_wchar_t* wcs, + qse_size_t* wcslen, + void* ctx +); + +struct qse_wfmtout_t +{ + qse_size_t count; /* out */ + qse_size_t limit; /* in */ + void* ctx; /* in */ + qse_wfmtout_put_t put; /* in */ + qse_wfmtout_conv_t conv; /* in */ +}; + + +typedef struct qse_wfmtout_t qse_wfmtout_t; + #ifdef __cplusplus extern "C" { #endif int qse_mfmtout ( const qse_mchar_t* fmt, - qse_fmtout_t* data, + qse_mfmtout_t* data, va_list ap ); int qse_wfmtout ( const qse_wchar_t* fmt, - qse_fmtout_t* data, + qse_wfmtout_t* data, va_list ap ); diff --git a/qse/lib/cmn/sio.c b/qse/lib/cmn/sio.c index d520f44d..77931f47 100644 --- a/qse/lib/cmn/sio.c +++ b/qse/lib/cmn/sio.c @@ -19,6 +19,7 @@ */ #include +#include #include "mem.h" #include "fmt.h" @@ -636,26 +637,40 @@ qse_ssize_t qse_sio_putwcsn ( return n; } -static int put_wchar (qse_wchar_t c, void* arg) +static int put_wchar (qse_wchar_t c, void* ctx) { - return qse_sio_putwc ((qse_sio_t*)arg, c); + return qse_sio_putwc ((qse_sio_t*)ctx, c); } -static int put_mchar (qse_mchar_t c, void* arg) +static int put_mchar (qse_mchar_t c, void* ctx) { - return qse_sio_putmb ((qse_sio_t*)arg, c); + return qse_sio_putmb ((qse_sio_t*)ctx, c); +} + +static int wcs_to_mbs ( + const qse_wchar_t* wcs, qse_size_t* wcslen, + qse_mchar_t* mbs, qse_size_t* mbslen, void* ctx) +{ + return qse_wcsntombsnwithcmgr (wcs, wcslen, mbs, mbslen, qse_sio_getcmgr ((qse_sio_t*)ctx)); +} + +static int mbs_to_wcs ( + const qse_mchar_t* mbs, qse_size_t* mbslen, + qse_wchar_t* wcs, qse_size_t* wcslen, void* ctx) +{ + return qse_mbsntowcsnwithcmgr (mbs, mbslen, wcs, wcslen, qse_sio_getcmgr ((qse_sio_t*)ctx)); } qse_ssize_t qse_sio_putmbsf (qse_sio_t* sio, const qse_mchar_t* fmt, ...) { va_list ap; qse_ssize_t x; - qse_fmtout_t fo; + qse_mfmtout_t fo; fo.limit = QSE_TYPE_MAX(qse_ssize_t); fo.ctx = sio; - fo.put_mchar = put_mchar; - fo.put_wchar = put_wchar; + fo.put = put_mchar; + fo.conv = wcs_to_mbs; va_start (ap, fmt); x = qse_mfmtout (fmt, &fo, ap); @@ -668,12 +683,12 @@ qse_ssize_t qse_sio_putwcsf (qse_sio_t* sio, const qse_wchar_t* fmt, ...) { va_list ap; int x; - qse_fmtout_t fo; + qse_wfmtout_t fo; fo.limit = QSE_TYPE_MAX(qse_ssize_t); fo.ctx = sio; - fo.put_mchar = put_mchar; - fo.put_wchar = put_wchar; + fo.put = put_wchar; + fo.conv = mbs_to_wcs; va_start (ap, fmt); x = qse_wfmtout (fmt, &fo, ap); @@ -682,60 +697,30 @@ qse_ssize_t qse_sio_putwcsf (qse_sio_t* sio, const qse_wchar_t* fmt, ...) return (x <= -1)? -1: fo.count; } -qse_ssize_t qse_sio_putstrf (qse_sio_t* sio, const qse_char_t* fmt, ...) -{ - va_list ap; - int x; - qse_fmtout_t fo; - - fo.limit = QSE_TYPE_MAX(qse_ssize_t); - fo.ctx = sio; - fo.put_mchar = put_mchar; - fo.put_wchar = put_wchar; - - va_start (ap, fmt); - x = qse_fmtout (fmt, &fo, ap); - va_end (ap); - - return (x <= -1)? -1: fo.count; -} - qse_ssize_t qse_sio_putmbsvf (qse_sio_t* sio, const qse_mchar_t* fmt, va_list ap) { - qse_fmtout_t fo; + qse_mfmtout_t fo; fo.limit = QSE_TYPE_MAX(qse_ssize_t); fo.ctx = sio; - fo.put_mchar = put_mchar; - fo.put_wchar = put_wchar; + fo.put = put_mchar; + fo.conv = wcs_to_mbs; return (qse_mfmtout (fmt, &fo, ap) <= -1)? -1: fo.count; } qse_ssize_t qse_sio_putwcsvf (qse_sio_t* sio, const qse_wchar_t* fmt, va_list ap) { - qse_fmtout_t fo; + qse_wfmtout_t fo; fo.limit = QSE_TYPE_MAX(qse_ssize_t); fo.ctx = sio; - fo.put_mchar = put_mchar; - fo.put_wchar = put_wchar; + fo.put = put_wchar; + fo.conv = mbs_to_wcs; return (qse_wfmtout (fmt, &fo, ap) <= -1)? -1: fo.count; } -qse_ssize_t qse_sio_putstrvf (qse_sio_t* sio, const qse_char_t* fmt, va_list ap) -{ - qse_fmtout_t fo; - - fo.limit = QSE_TYPE_MAX(qse_ssize_t); - fo.ctx = sio; - fo.put_mchar = put_mchar; - fo.put_wchar = put_wchar; - - return (qse_fmtout (fmt, &fo, ap) <= -1)? -1: fo.count; -} - int qse_sio_getpos (qse_sio_t* sio, qse_sio_pos_t* pos) { qse_fio_off_t off; @@ -875,12 +860,12 @@ qse_ssize_t qse_putmbsf (const qse_mchar_t* fmt, ...) { va_list ap; int x; - qse_fmtout_t fo; + qse_mfmtout_t fo; fo.limit = QSE_TYPE_MAX(qse_ssize_t); fo.ctx = sio_stdout; - fo.put_mchar = put_mchar; - fo.put_wchar = put_wchar; + fo.put = put_mchar; + fo.conv = wcs_to_mbs; va_start (ap, fmt); x = qse_mfmtout (fmt, &fo, ap); @@ -893,12 +878,12 @@ qse_ssize_t qse_putwcsf (const qse_wchar_t* fmt, ...) { va_list ap; int x; - qse_fmtout_t fo; + qse_wfmtout_t fo; fo.limit = QSE_TYPE_MAX(qse_ssize_t); fo.ctx = sio_stdout; - fo.put_mchar = put_mchar; - fo.put_wchar = put_wchar; + fo.put = put_wchar; + fo.conv = mbs_to_wcs; va_start (ap, fmt); x = qse_wfmtout (fmt, &fo, ap); @@ -907,56 +892,86 @@ qse_ssize_t qse_putwcsf (const qse_wchar_t* fmt, ...) return (x <= -1)? -1: fo.count; } -qse_ssize_t qse_putstrf (const qse_char_t* fmt, ...) -{ - va_list ap; - int x; - qse_fmtout_t fo; - - fo.limit = QSE_TYPE_MAX(qse_ssize_t); - fo.ctx = sio_stdout; - fo.put_mchar = put_mchar; - fo.put_wchar = put_wchar; - - va_start (ap, fmt); - x = qse_fmtout (fmt, &fo, ap); - va_end (ap); - - return (x <= -1)? -1: fo.count; -} - qse_ssize_t qse_putmbsvf (const qse_mchar_t* fmt, va_list ap) { - qse_fmtout_t fo; + qse_mfmtout_t fo; fo.limit = QSE_TYPE_MAX(qse_ssize_t); fo.ctx = sio_stdout; - fo.put_mchar = put_mchar; - fo.put_wchar = put_wchar; + fo.put = put_mchar; + fo.conv = wcs_to_mbs; return (qse_mfmtout (fmt, &fo, ap) <= -1)? -1: fo.count; } qse_ssize_t qse_putwcsvf (const qse_wchar_t* fmt, va_list ap) { - qse_fmtout_t fo; + qse_wfmtout_t fo; fo.limit = QSE_TYPE_MAX(qse_ssize_t); fo.ctx = sio_stdout; - fo.put_mchar = put_mchar; - fo.put_wchar = put_wchar; + fo.put = put_wchar; + fo.conv = mbs_to_wcs; return (qse_wfmtout (fmt, &fo, ap) <= -1)? -1: fo.count; } -qse_ssize_t qse_putstrvf (const qse_char_t* fmt, va_list ap) +qse_ssize_t qse_errputmbsf (const qse_mchar_t* fmt, ...) { - qse_fmtout_t fo; + va_list ap; + int x; + qse_mfmtout_t fo; fo.limit = QSE_TYPE_MAX(qse_ssize_t); - fo.ctx = sio_stdout; - fo.put_mchar = put_mchar; - fo.put_wchar = put_wchar; + fo.ctx = sio_stderr; + fo.put = put_mchar; + fo.conv = wcs_to_mbs; - return (qse_fmtout (fmt, &fo, ap) <= -1)? -1: fo.count; + va_start (ap, fmt); + x = qse_mfmtout (fmt, &fo, ap); + va_end (ap); + + return (x <= -1)? -1: fo.count; +} + +qse_ssize_t qse_errputwcsf (const qse_wchar_t* fmt, ...) +{ + va_list ap; + int x; + qse_wfmtout_t fo; + + fo.limit = QSE_TYPE_MAX(qse_ssize_t); + fo.ctx = sio_stderr; + fo.put = put_wchar; + fo.conv = mbs_to_wcs; + + va_start (ap, fmt); + x = qse_wfmtout (fmt, &fo, ap); + va_end (ap); + + return (x <= -1)? -1: fo.count; +} + +qse_ssize_t qse_errputmbsvf (const qse_mchar_t* fmt, va_list ap) +{ + qse_mfmtout_t fo; + + fo.limit = QSE_TYPE_MAX(qse_ssize_t); + fo.ctx = sio_stderr; + fo.put = put_mchar; + fo.conv = wcs_to_mbs; + + return (qse_mfmtout (fmt, &fo, ap) <= -1)? -1: fo.count; +} + +qse_ssize_t qse_errputwcsvf (const qse_wchar_t* fmt, va_list ap) +{ + qse_wfmtout_t fo; + + fo.limit = QSE_TYPE_MAX(qse_ssize_t); + fo.ctx = sio_stderr; + fo.put = put_wchar; + fo.conv = mbs_to_wcs; + + return (qse_wfmtout (fmt, &fo, ap) <= -1)? -1: fo.count; } diff --git a/qse/lib/cmn/str-fmt.c b/qse/lib/cmn/str-fmt.c index 4034a984..b2bf7321 100644 --- a/qse/lib/cmn/str-fmt.c +++ b/qse/lib/cmn/str-fmt.c @@ -19,49 +19,62 @@ */ #include +#include #include "fmt.h" -struct wbuf_t -{ - qse_wchar_t* ptr; - qse_size_t len; - qse_size_t capa; -}; - struct mbuf_t { qse_mchar_t* ptr; qse_size_t len; qse_size_t capa; }; - -typedef struct wbuf_t wbuf_t; typedef struct mbuf_t mbuf_t; -static int put_wchar_to_wbuf (qse_wchar_t c, void* arg) +struct wbuf_t { - qse_fmtout_t* fo = (qse_fmtout_t*)arg; - wbuf_t* buf = (wbuf_t*)fo->ctx; - if (buf->len < buf->capa) buf->ptr[buf->len++] = c; - return 1; + qse_wchar_t* ptr; + qse_size_t len; + qse_size_t capa; +}; +typedef struct wbuf_t wbuf_t; + + +static int put_mchar (qse_mchar_t c, void* ctx) +{ + mbuf_t* buf = (mbuf_t*)ctx; + if (buf->len < buf->capa) + { + buf->ptr[buf->len++] = c; + return 1; + } + + return 0; /* stop. but no error */ } -static int put_mchar_to_mbuf (qse_mchar_t c, void* arg) +static int put_wchar (qse_wchar_t c, void* ctx) { - qse_fmtout_t* fo = (qse_fmtout_t*)arg; - mbuf_t* buf = (mbuf_t*)fo->ctx; - if (buf->len < buf->capa) buf->ptr[buf->len++] = c; - return 1; + wbuf_t* buf = (wbuf_t*)ctx; + if (buf->len < buf->capa) + { + buf->ptr[buf->len++] = c; + return 1; + } + + return 0; /* stop. but no error */ } -static int put_wchar_to_mbuf (qse_wchar_t c, void* arg) +static int wcs_to_mbs ( + const qse_wchar_t* wcs, qse_size_t* wcslen, + qse_mchar_t* mbs, qse_size_t* mbslen, void* ctx) { - return 1; + return qse_wcsntombsnwithcmgr (wcs, wcslen, mbs, mbslen, qse_getdflcmgr()); } -static int put_mchar_to_wbuf (qse_mchar_t c, void* arg) +static int mbs_to_wcs ( + const qse_mchar_t* mbs, qse_size_t* mbslen, + qse_wchar_t* wcs, qse_size_t* wcslen, void* ctx) { - return 1; + return qse_mbsntowcsnwithcmgr (mbs, mbslen, wcs, wcslen, qse_getdflcmgr()); } /* ----------------------------------- */ @@ -69,18 +82,20 @@ static int put_mchar_to_wbuf (qse_mchar_t c, void* arg) #undef T #undef char_t #undef buf_t +#undef fmtout_t #undef fmtout -#undef output_char -#undef output_ochar +#undef put_char +#undef conv_char #undef strfmt #undef strxfmt #define T(x) QSE_MT(x) #define char_t qse_mchar_t #define buf_t mbuf_t +#define fmtout_t qse_mfmtout_t #define fmtout qse_mfmtout -#define output_mchar put_mchar_to_mbuf -#define output_wchar put_wchar_to_mbuf +#define put_char put_mchar +#define conv_char wcs_to_mbs #define strfmt qse_mbsfmt #define strxfmt qse_mbsxfmt #include "str-fmt.h" @@ -90,47 +105,21 @@ static int put_mchar_to_wbuf (qse_mchar_t c, void* arg) #undef T #undef char_t #undef buf_t +#undef fmtout_t #undef fmtout -#undef output_mchar -#undef output_wchar +#undef put_char +#undef conv_char #undef strfmt #undef strxfmt #define T(x) QSE_WT(x) #define char_t qse_wchar_t #define buf_t wbuf_t +#define fmtout_t qse_wfmtout_t #define fmtout qse_wfmtout -#define output_mchar put_mchar_to_wbuf -#define output_wchar put_wchar_to_wbuf -#define strfmtx qse_wcsfmt -#define strxfmtx qse_wcsxfmt +#define put_char put_wchar +#define conv_char mbs_to_wcs +#define strfmt qse_wcsfmt +#define strxfmt qse_wcsxfmt #include "str-fmt.h" - -/* ----------------------------------- */ - -#undef T -#undef char_t -#undef buf_t -#undef fmtout -#undef output_mchar -#undef output_wchar -#undef strfmt -#undef strxfmt - -#define T(x) QSE_T(x) -#define char_t qse_char_t -#if defined(QSE_CHAR_IS_MCHAR) -# define buf_t mbuf_t -# define output_mchar put_mchar_to_mbuf -# define output_wchar put_wchar_to_mbuf -# define fmtout qse_mfmtout -#else -# define buf_t wbuf_t -# define output_mchar put_mchar_to_wbuf -# define output_wchar put_wchar_to_wbuf -# define fmtout qse_wfmtout -#endif -#define strfmt qse_strfmt -#define strxfmt qse_strxfmt -#include "str-fmt.h" diff --git a/qse/lib/cmn/str-fmt.h b/qse/lib/cmn/str-fmt.h index 9517b396..8d7d523d 100644 --- a/qse/lib/cmn/str-fmt.h +++ b/qse/lib/cmn/str-fmt.h @@ -23,7 +23,7 @@ qse_size_t strfmt (char_t* buf, const char_t* fmt, ...) { buf_t b; va_list ap; - qse_fmtout_t fo; + fmtout_t fo; b.ptr = buf; b.len = 0; @@ -31,8 +31,8 @@ qse_size_t strfmt (char_t* buf, const char_t* fmt, ...) fo.limit = QSE_TYPE_MAX(qse_size_t) - 1; fo.ctx = &b; - fo.put_mchar = output_mchar; - fo.put_wchar = output_wchar; + fo.put = put_char; + fo.conv = conv_char; /* no error must be returned by fmtout since * the callback function never fails. */ @@ -42,14 +42,15 @@ qse_size_t strfmt (char_t* buf, const char_t* fmt, ...) b.ptr[b.len] = T('\0'); - return fo.count; + /*return fo.count;*/ + return b.len; } qse_size_t strxfmt (char_t* buf, qse_size_t len, const char_t* fmt, ...) { buf_t b; va_list ap; - qse_fmtout_t fo; + fmtout_t fo; b.ptr = buf; b.len = 0; @@ -63,8 +64,8 @@ qse_size_t strxfmt (char_t* buf, qse_size_t len, const char_t* fmt, ...) fo.limit = QSE_TYPE_MAX(qse_size_t) - 1; fo.ctx = &b; - fo.put_mchar = output_mchar; - fo.put_wchar = output_wchar; + fo.put = put_char; + fo.conv = conv_char; /* no error must be returned by fmtout since * the callback function never fails. */ @@ -74,6 +75,7 @@ qse_size_t strxfmt (char_t* buf, qse_size_t len, const char_t* fmt, ...) if (len > 0) b.ptr[b.len] = T('\0'); - return fo.count; + /*return fo.count;*/ + return b.len; }