From f16437d7c03352ac9d01d04c09748ca47d7cfe33 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 15 Apr 2019 15:36:23 +0000 Subject: [PATCH] writing mbs_to_str() in awk/val.c --- qse/include/qse/awk/StdAwk.hpp | 8 +++---- qse/include/qse/awk/stdawk.h | 4 ++-- qse/lib/awk/StdAwk.cpp | 20 +++++++++--------- qse/lib/awk/awk-prv.h | 1 + qse/lib/awk/run.c | 37 ++++++++++++++------------------- qse/lib/awk/std.c | 38 ++++++++++++++-------------------- qse/lib/awk/val.c | 29 +++++++++++++++++++------- qse/lib/cmn/mbwc-str.c | 9 +++----- 8 files changed, 71 insertions(+), 75 deletions(-) diff --git a/qse/include/qse/awk/StdAwk.hpp b/qse/include/qse/awk/StdAwk.hpp index d23de55b..edec1893 100644 --- a/qse/include/qse/awk/StdAwk.hpp +++ b/qse/include/qse/awk/StdAwk.hpp @@ -123,12 +123,10 @@ protected: int __build_environ (Run* run, void* envptr); // intrinsic functions - qse_cmgr_t* getcmgr (const char_t* ioname); + qse_cmgr_t* getiocmgr (const char_t* ioname); - int setioattr (Run& run, Value& ret, Value* args, size_t nargs, - const char_t* name, size_t len); - int getioattr (Run& run, Value& ret, Value* args, size_t nargs, - const char_t* name, size_t len); + int setioattr (Run& run, Value& ret, Value* args, size_t nargs, const char_t* name, size_t len); + int getioattr (Run& run, Value& ret, Value* args, size_t nargs, const char_t* name, size_t len); // pipe io handlers int openPipe (Pipe& io); diff --git a/qse/include/qse/awk/stdawk.h b/qse/include/qse/awk/stdawk.h index f4a553a6..4ff1aad1 100644 --- a/qse/include/qse/awk/stdawk.h +++ b/qse/include/qse/awk/stdawk.h @@ -179,12 +179,12 @@ QSE_EXPORT void* qse_awk_rtx_getxtnstd ( /** - * The qse_awk_rtx_getcmgrstd() function gets the current character + * The qse_awk_rtx_getiocmgrstd() function gets the current character * manager associated with a particular I/O target indicated by the name * \a ioname if #QSE_CHAR_IS_WCHAR is defined. It always returns #QSE_NULL * if #QSE_CHAR_IS_MCHAR is defined. */ -QSE_EXPORT qse_cmgr_t* qse_awk_rtx_getcmgrstd ( +QSE_EXPORT qse_cmgr_t* qse_awk_rtx_getiocmgrstd ( qse_awk_rtx_t* rtx, const qse_char_t* ioname ); diff --git a/qse/lib/awk/StdAwk.cpp b/qse/lib/awk/StdAwk.cpp index a19c247e..73bef9af 100644 --- a/qse/lib/awk/StdAwk.cpp +++ b/qse/lib/awk/StdAwk.cpp @@ -329,12 +329,12 @@ int StdAwk::make_additional_globals (Run* run) return 0; } -qse_cmgr_t* StdAwk::getcmgr (const char_t* ioname) +qse_cmgr_t* StdAwk::getiocmgr (const char_t* ioname) { QSE_ASSERT (this->cmgrtab_inited == true); #if defined(QSE_CHAR_IS_WCHAR) - ioattr_t* ioattr = get_ioattr (ioname, qse_strlen(ioname)); + ioattr_t* ioattr = get_ioattr(ioname, qse_strlen(ioname)); if (ioattr) return ioattr->cmgr; #endif return QSE_NULL; @@ -521,7 +521,7 @@ int StdAwk::open_nwio (Pipe& io, int flags, void* nwad) if (handle == QSE_NULL) return -1; #if defined(QSE_CHAR_IS_WCHAR) - qse_cmgr_t* cmgr = this->getcmgr (io.getName()); + qse_cmgr_t* cmgr = this->getiocmgr(io.getName()); if (cmgr) qse_nwio_setcmgr (handle, cmgr); #endif @@ -566,7 +566,7 @@ int StdAwk::open_pio (Pipe& io) if (pio == QSE_NULL) return -1; #if defined(QSE_CHAR_IS_WCHAR) - qse_cmgr_t* cmgr = this->getcmgr (io.getName()); + qse_cmgr_t* cmgr = this->getiocmgr(io.getName()); if (cmgr) { qse_pio_setcmgr (pio, QSE_PIO_IN, cmgr); @@ -697,10 +697,10 @@ int StdAwk::openFile (File& io) break; } - sio = qse_sio_open (this->getMmgr(), 0, io.getName(), flags); - if (sio == NULL) return -1; + sio = qse_sio_open(this->getMmgr(), 0, io.getName(), flags); + if (!sio) return -1; #if defined(QSE_CHAR_IS_WCHAR) - qse_cmgr_t* cmgr = this->getcmgr (io.getName()); + qse_cmgr_t* cmgr = this->getiocmgr(io.getName()); if (cmgr) qse_sio_setcmgr (sio, cmgr); #endif @@ -716,17 +716,17 @@ int StdAwk::closeFile (File& io) StdAwk::ssize_t StdAwk::readFile (File& io, char_t* buf, size_t len) { - return qse_sio_getstrn ((qse_sio_t*)io.getHandle(), buf, len); + return qse_sio_getstrn((qse_sio_t*)io.getHandle(), buf, len); } StdAwk::ssize_t StdAwk::writeFile (File& io, const char_t* buf, size_t len) { - return qse_sio_putstrn ((qse_sio_t*)io.getHandle(), buf, len); + return qse_sio_putstrn((qse_sio_t*)io.getHandle(), buf, len); } int StdAwk::flushFile (File& io) { - return qse_sio_flush ((qse_sio_t*)io.getHandle()); + return qse_sio_flush((qse_sio_t*)io.getHandle()); } void StdAwk::setConsoleCmgr (const qse_cmgr_t* cmgr) diff --git a/qse/lib/awk/awk-prv.h b/qse/lib/awk/awk-prv.h index afbb04a2..4a22ae93 100644 --- a/qse/lib/awk/awk-prv.h +++ b/qse/lib/awk/awk-prv.h @@ -385,6 +385,7 @@ struct qse_awk_rtx_t qse_awk_errinf_t errinf; qse_awk_t* awk; + qse_cmgr_t* cmgr; /* internal default cmgr */ qse_awk_rtx_ecb_t* ecb; }; diff --git a/qse/lib/awk/run.c b/qse/lib/awk/run.c index 37b57e24..02d747c6 100644 --- a/qse/lib/awk/run.c +++ b/qse/lib/awk/run.c @@ -825,8 +825,7 @@ static qse_rbt_walk_t fini_module (qse_rbt_t* rbt, qse_rbt_pair_t* pair, void* c return QSE_RBT_WALK_FORWARD; } -qse_awk_rtx_t* qse_awk_rtx_open ( - qse_awk_t* awk, qse_size_t xtnsize, qse_awk_rio_t* rio) +qse_awk_rtx_t* qse_awk_rtx_open (qse_awk_t* awk, qse_size_t xtnsize, qse_awk_rio_t* rio) { qse_awk_rtx_t* rtx; struct module_init_ctx_t mic; @@ -846,9 +845,8 @@ qse_awk_rtx_t* qse_awk_rtx_open ( } /* allocate the storage for the rtx object */ - rtx = (qse_awk_rtx_t*) QSE_AWK_ALLOC ( - awk, QSE_SIZEOF(qse_awk_rtx_t) + xtnsize); - if (rtx == QSE_NULL) + rtx = (qse_awk_rtx_t*)QSE_AWK_ALLOC(awk, QSE_SIZEOF(qse_awk_rtx_t) + xtnsize); + if (!rtx) { /* if it fails, the failure is reported thru * the awk object */ @@ -987,6 +985,7 @@ static int init_rtx (qse_awk_rtx_t* rtx, qse_awk_t* awk, qse_awk_rio_t* rio) }; rtx->awk = awk; + rtx->cmgr = qse_getdflcmgr(); CLRERR (rtx); @@ -1009,32 +1008,28 @@ static int init_rtx (qse_awk_rtx_t* rtx, qse_awk_t* awk, qse_awk_rio_t* rio) rtx->inrec.maxflds = 0; rtx->inrec.d0 = qse_awk_val_nil; - if (qse_str_init (&rtx->inrec.line, MMGR(rtx), DEF_BUF_CAPA) <= -1) goto oops_0; - if (qse_str_init (&rtx->inrec.linew, MMGR(rtx), DEF_BUF_CAPA) <= -1) goto oops_1; - if (qse_str_init (&rtx->inrec.lineg, MMGR(rtx), DEF_BUF_CAPA) <= -1) goto oops_2; - if (qse_str_init (&rtx->format.out, MMGR(rtx), 256) <= -1) goto oops_3; - if (qse_str_init (&rtx->format.fmt, MMGR(rtx), 256) <= -1) goto oops_4; + if (qse_str_init(&rtx->inrec.line, MMGR(rtx), DEF_BUF_CAPA) <= -1) goto oops_0; + if (qse_str_init(&rtx->inrec.linew, MMGR(rtx), DEF_BUF_CAPA) <= -1) goto oops_1; + if (qse_str_init(&rtx->inrec.lineg, MMGR(rtx), DEF_BUF_CAPA) <= -1) goto oops_2; + if (qse_str_init(&rtx->format.out, MMGR(rtx), 256) <= -1) goto oops_3; + if (qse_str_init(&rtx->format.fmt, MMGR(rtx), 256) <= -1) goto oops_4; - rtx->named = qse_htb_open (MMGR(rtx), QSE_SIZEOF(rtx), 1024, 70, QSE_SIZEOF(qse_char_t), 1); - if (rtx->named == QSE_NULL) goto oops_5; + rtx->named = qse_htb_open(MMGR(rtx), QSE_SIZEOF(rtx), 1024, 70, QSE_SIZEOF(qse_char_t), 1); + if (!rtx->named) goto oops_5; *(qse_awk_rtx_t**)QSE_XTN(rtx->named) = rtx; qse_htb_setstyle (rtx->named, &style_for_named); - rtx->format.tmp.ptr = (qse_char_t*) - QSE_AWK_ALLOC (rtx->awk, 4096*QSE_SIZEOF(qse_char_t*)); - if (rtx->format.tmp.ptr == QSE_NULL) goto oops_6; + rtx->format.tmp.ptr = (qse_char_t*)QSE_AWK_ALLOC(rtx->awk, 4096*QSE_SIZEOF(qse_char_t*)); + if (!rtx->format.tmp.ptr) goto oops_6; rtx->format.tmp.len = 4096; rtx->format.tmp.inc = 4096*2; if (rtx->awk->tree.chain_size > 0) { - rtx->pattern_range_state = (qse_byte_t*) QSE_AWK_ALLOC ( - rtx->awk, rtx->awk->tree.chain_size*QSE_SIZEOF(qse_byte_t)); - if (rtx->pattern_range_state == QSE_NULL) goto oops_7; + rtx->pattern_range_state = (qse_byte_t*)QSE_AWK_ALLOC(rtx->awk, rtx->awk->tree.chain_size * QSE_SIZEOF(qse_byte_t)); + if (!rtx->pattern_range_state) goto oops_7; - QSE_MEMSET ( - rtx->pattern_range_state, 0, - rtx->awk->tree.chain_size * QSE_SIZEOF(qse_byte_t)); + QSE_MEMSET (rtx->pattern_range_state, 0, rtx->awk->tree.chain_size * QSE_SIZEOF(qse_byte_t)); } else rtx->pattern_range_state = QSE_NULL; diff --git a/qse/lib/awk/std.c b/qse/lib/awk/std.c index 9c590763..24082386 100644 --- a/qse/lib/awk/std.c +++ b/qse/lib/awk/std.c @@ -1080,7 +1080,7 @@ static qse_ssize_t nwio_handler_open ( #if defined(QSE_CHAR_IS_WCHAR) { - qse_cmgr_t* cmgr = qse_awk_rtx_getcmgrstd (rtx, riod->name); + qse_cmgr_t* cmgr = qse_awk_rtx_getiocmgrstd(rtx, riod->name); if (cmgr) qse_nwio_setcmgr (handle, cmgr); } #endif @@ -1202,7 +1202,7 @@ static qse_ssize_t pio_handler_open ( #if defined(QSE_CHAR_IS_WCHAR) { - qse_cmgr_t* cmgr = qse_awk_rtx_getcmgrstd (rtx, riod->name); + qse_cmgr_t* cmgr = qse_awk_rtx_getiocmgrstd(rtx, riod->name); if (cmgr) { qse_pio_setcmgr (handle, QSE_PIO_IN, cmgr); @@ -1304,10 +1304,9 @@ static qse_ssize_t awk_rio_pipe ( ioattr_t* ioattr; rxtn_t* rxtn; - rxtn = (rxtn_t*) QSE_XTN (rtx); + rxtn = (rxtn_t*)QSE_XTN(rtx); - ioattr = get_ioattr ( - &rxtn->cmgrtab, riod->name, qse_strlen(riod->name)); + ioattr = get_ioattr(&rxtn->cmgrtab, riod->name, qse_strlen(riod->name)); if (ioattr) { tmout = &tmout_buf; @@ -1317,7 +1316,7 @@ static qse_ssize_t awk_rio_pipe ( tmout->a = ioattr->tmout[3]; } - return nwio_handler_open (rtx, riod, flags, &nwad, tmout); + return nwio_handler_open(rtx, riod, flags, &nwad, tmout); } } else if (riod->uflags > 0) @@ -1369,7 +1368,7 @@ static qse_ssize_t awk_rio_file ( #if defined(QSE_CHAR_IS_WCHAR) { - qse_cmgr_t* cmgr = qse_awk_rtx_getcmgrstd (rtx, riod->name); + qse_cmgr_t* cmgr = qse_awk_rtx_getiocmgrstd(rtx, riod->name); if (cmgr) qse_sio_setcmgr (handle, cmgr); } #endif @@ -2021,27 +2020,20 @@ qse_awk_rtx_t* qse_awk_rtx_openstd ( rio.file = awk_rio_file; rio.console = awk_rio_console; - rtx = qse_awk_rtx_open ( - awk, - QSE_SIZEOF(rxtn_t) + xtnsize, - &rio - ); - if (rtx == QSE_NULL) return QSE_NULL; + rtx = qse_awk_rtx_open(awk, QSE_SIZEOF(rxtn_t) + xtnsize, &rio); + if (!rtx) return QSE_NULL; rxtn = (rxtn_t*) QSE_XTN (rtx); if (rtx->awk->opt.trait & QSE_AWK_RIO) { - if (qse_htb_init ( - &rxtn->cmgrtab, awk->mmgr, 256, 70, - QSE_SIZEOF(qse_char_t), 1) <= -1) + if (qse_htb_init(&rxtn->cmgrtab, awk->mmgr, 256, 70, QSE_SIZEOF(qse_char_t), 1) <= -1) { qse_awk_rtx_close (rtx); qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL); return QSE_NULL; } - qse_htb_setstyle (&rxtn->cmgrtab, - qse_gethtbstyle(QSE_HTB_STYLE_INLINE_COPIERS)); + qse_htb_setstyle (&rxtn->cmgrtab, qse_gethtbstyle(QSE_HTB_STYLE_INLINE_COPIERS)); rxtn->cmgrtab_inited = 1; } @@ -2190,7 +2182,7 @@ static int fnc_setioattr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) x = qse_awk_rtx_strtonum(rtx, QSE_AWK_RTX_STRTONUM_MAKE_OPTION(0, 0), ptr[2], len[2], &l, &r); if (x == 0) r = (qse_awk_flt_t)l; - ioattr = find_or_make_ioattr (rtx, &rxtn->cmgrtab, ptr[0], len[0]); + ioattr = find_or_make_ioattr(rtx, &rxtn->cmgrtab, ptr[0], len[0]); if (ioattr == QSE_NULL) { ret = -1; @@ -2223,7 +2215,7 @@ static int fnc_setioattr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } else { - cmgr = qse_findcmgr (ptr[2]); + cmgr = qse_findcmgr(ptr[2]); if (cmgr == QSE_NULL) { fret = -1; @@ -2231,7 +2223,7 @@ static int fnc_setioattr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) } } - ioattr = find_or_make_ioattr (rtx, &rxtn->cmgrtab, ptr[0], len[0]); + ioattr = find_or_make_ioattr(rtx, &rxtn->cmgrtab, ptr[0], len[0]); if (ioattr == QSE_NULL) { ret = -1; @@ -2358,7 +2350,7 @@ done: return ret; } -qse_cmgr_t* qse_awk_rtx_getcmgrstd (qse_awk_rtx_t* rtx, const qse_char_t* ioname) +qse_cmgr_t* qse_awk_rtx_getiocmgrstd (qse_awk_rtx_t* rtx, const qse_char_t* ioname) { #if defined(QSE_CHAR_IS_WCHAR) rxtn_t* rxtn; @@ -2367,7 +2359,7 @@ qse_cmgr_t* qse_awk_rtx_getcmgrstd (qse_awk_rtx_t* rtx, const qse_char_t* ioname rxtn = (rxtn_t*) QSE_XTN (rtx); QSE_ASSERT (rxtn->cmgrtab_inited == 1); - ioattr = get_ioattr (&rxtn->cmgrtab, ioname, qse_strlen(ioname)); + ioattr = get_ioattr(&rxtn->cmgrtab, ioname, qse_strlen(ioname)); if (ioattr) return ioattr->cmgr; #endif return QSE_NULL; diff --git a/qse/lib/awk/val.c b/qse/lib/awk/val.c index dd36c207..ec6932c9 100644 --- a/qse/lib/awk/val.c +++ b/qse/lib/awk/val.c @@ -812,6 +812,10 @@ void qse_awk_rtx_freeval (qse_awk_rtx_t* rtx, qse_awk_val_t* val, int cache) break; } + case QSE_AWK_VAL_BYTEARR: + QSE_AWK_FREE (rtx->awk, val); + break; + case QSE_AWK_VAL_REX: { /* don't free ptr as it is inlined to val @@ -1077,22 +1081,31 @@ static int mbs_to_str (qse_awk_rtx_t* rtx, const qse_mchar_t* str, qse_size_t st * behave like CPLCPY. fall thru */ case QSE_AWK_RTX_VALTOSTR_CPLCPY: { - if (str_len >= out->u.cplcpy.len) + qse_size_t mbslen, wcslen; + + mbslen = str_len; + wcslen = out->u.cplcpy.len; + if (qse_mbsntowcsnwithcmgr(str, &mbslen, out->u.cplcpy.ptr, &wcslen, rtx->cmgr) <= -1) { - qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); - out->u.cplcpy.len = str_len + 1; /* set the required length */ + qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); /* TODO: change error code */ return -1; } - out->u.cplcpy.len = qse_strncpy(out->u.cplcpy.ptr, str, str_len); + if (mbslen < str_len) + { + /* TODO: ... */ + } + out->u.cplcpy.len = wcslen; return 0; } case QSE_AWK_RTX_VALTOSTR_CPLDUP: { qse_char_t* tmp; + qse_size_t mbslen, wcslen; - tmp = QSE_AWK_STRXDUP(rtx->awk, str, str_len); + mbslen = str_len; + tmp = qse_mbsntowcsdupwithcmgr(str, &mbslen, &wcslen, rtx->awk->mmgr, rtx->cmgr); if (!tmp) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); @@ -1100,7 +1113,7 @@ static int mbs_to_str (qse_awk_rtx_t* rtx, const qse_mchar_t* str, qse_size_t st } out->u.cpldup.ptr = tmp; - out->u.cpldup.len = str_len; + out->u.cpldup.len = wcslen; return 0; } @@ -1206,7 +1219,7 @@ static int val_int_to_str (qse_awk_rtx_t* rtx, const qse_awk_val_int_t* v, qse_a tmp = QSE_STR_PTR(out->u.strp); /* extend the buffer */ - n = qse_str_nccats(out->u.strp, QSE_T(' '), rlen); + n = qse_str_nccat(out->u.strp, QSE_T(' '), rlen); if (n == (qse_size_t)-1) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); @@ -1886,7 +1899,7 @@ int qse_awk_rtx_setrefval (qse_awk_rtx_t* rtx, qse_awk_val_ref_t* ref, qse_awk_v return x; } #endif - /* fall thru */ + /* fall thru otherwise */ default: { diff --git a/qse/lib/cmn/mbwc-str.c b/qse/lib/cmn/mbwc-str.c index ddf7fd0d..e960026c 100644 --- a/qse/lib/cmn/mbwc-str.c +++ b/qse/lib/cmn/mbwc-str.c @@ -272,20 +272,17 @@ static qse_wchar_t* mbsn_to_wcs_dup_with_cmgr ( return wcs; } -qse_wchar_t* qse_mbsntowcsdupwithcmgr ( - const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr) +qse_wchar_t* qse_mbsntowcsdupwithcmgr (const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr) { return mbsn_to_wcs_dup_with_cmgr (mbs, mbslen, wcslen, mmgr, cmgr, 0); } -qse_wchar_t* qse_mbsntowcsalldupwithcmgr ( - const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr) +qse_wchar_t* qse_mbsntowcsalldupwithcmgr (const qse_mchar_t* mbs, qse_size_t* mbslen, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr) { return mbsn_to_wcs_dup_with_cmgr (mbs, mbslen, wcslen, mmgr, cmgr, 1); } -static qse_wchar_t* mbs_to_wcs_dup_with_cmgr ( - const qse_mchar_t* mbs, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr, int all) +static qse_wchar_t* mbs_to_wcs_dup_with_cmgr (const qse_mchar_t* mbs, qse_size_t* wcslen, qse_mmgr_t* mmgr, qse_cmgr_t* cmgr, int all) { qse_size_t ml, wl; qse_wchar_t* wcs;