From 2cbe0d468186c4a838b8f4bba3a60f1bf05f1287 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Thu, 26 May 2011 08:00:19 +0000 Subject: [PATCH] enhanced val_int_to_str(). changed the return type of qse_awk_valtostr() --- qse/include/qse/awk/awk.h | 39 +++--- qse/lib/awk/Awk.cpp | 5 +- qse/lib/awk/StdAwk.cpp | 4 +- qse/lib/awk/rec.c | 4 +- qse/lib/awk/rio.c | 4 +- qse/lib/awk/run.c | 115 ++++++++---------- qse/lib/awk/std.c | 4 +- qse/lib/awk/val.c | 247 +++++++++++++------------------------- qse/samples/awk/awk04.c | 4 +- 9 files changed, 166 insertions(+), 260 deletions(-) diff --git a/qse/include/qse/awk/awk.h b/qse/include/qse/awk/awk.h index efb1ec0f..6f49dfed 100644 --- a/qse/include/qse/awk/awk.h +++ b/qse/include/qse/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h 479 2011-05-24 15:14:58Z hyunghwan.chung $ + * $Id: awk.h 480 2011-05-25 14:00:19Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -1059,7 +1059,7 @@ enum qse_awk_val_type_t * following values: * * - #QSE_AWK_RTX_VALTOSTR_CPL - * - #QSE_AWK_RTX_VALTOSTR_CPLCP + * - #QSE_AWK_RTX_VALTOSTR_CPLCPY * - #QSE_AWK_RTX_VALTOSTR_CPLDUP * - #QSE_AWK_RTX_VALTOSTR_STRP * - #QSE_AWK_RTX_VALTOSTR_STRPCAT @@ -1070,8 +1070,8 @@ enum qse_awk_rtx_valtostr_type_t { /** use u.cpl of #qse_awk_rtx_valtostr_out_t */ QSE_AWK_RTX_VALTOSTR_CPL = 0x00, - /** use u.cplcp of #qse_awk_rtx_valtostr_out_t */ - QSE_AWK_RTX_VALTOSTR_CPLCP = 0x01, + /** use u.cplcpy of #qse_awk_rtx_valtostr_out_t */ + QSE_AWK_RTX_VALTOSTR_CPLCPY = 0x01, /** use u.cpldup of #qse_awk_rtx_valtostr_out_t */ QSE_AWK_RTX_VALTOSTR_CPLDUP = 0x02, /** use u.strp of #qse_awk_rtx_valtostr_out_t */ @@ -1092,8 +1092,8 @@ struct qse_awk_rtx_valtostr_out_t union { - qse_xstr_t cpl; - qse_xstr_t cplcp; + qse_cstr_t cpl; + qse_xstr_t cplcpy; qse_xstr_t cpldup; /* need to free cpldup.ptr */ qse_str_t* strp; qse_str_t* strpcat; @@ -2079,7 +2079,7 @@ qse_bool_t qse_awk_rtx_valtobool ( * The type field is one of the following qse_awk_rtx_valtostr_type_t values: * * - #QSE_AWK_RTX_VALTOSTR_CPL - * - #QSE_AWK_RTX_VALTOSTR_CPLCP + * - #QSE_AWK_RTX_VALTOSTR_CPLCPY * - #QSE_AWK_RTX_VALTOSTR_CPLDUP * - #QSE_AWK_RTX_VALTOSTR_STRP * - #QSE_AWK_RTX_VALTOSTR_STRPCAT @@ -2091,19 +2091,19 @@ qse_bool_t qse_awk_rtx_valtobool ( * You should initialize or free other fields before and after the call * depending on the type field as shown below: * - * If you have a static buffer, use #QSE_AWK_RTX_VALTOSTR_CPLCP. + * If you have a static buffer, use #QSE_AWK_RTX_VALTOSTR_CPLCPY. * the resulting string is copied to the buffer. * @code * qse_awk_rtx_valtostr_out_t out; * qse_char_t buf[100]; - * out.type = QSE_AWK_RTX_VALTOSTR_CPLCP; - * out.u.cplcp.ptr = buf; - * out.u.cplcp.len = QSE_COUNTOF(buf); - * if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops; - * qse_printf (QSE_T("%.*s\n"), (int)out.u.cplcp.len, out.u.cplcp.ptr); + * out.type = QSE_AWK_RTX_VALTOSTR_CPLCPY; + * out.u.cplcpy.ptr = buf; + * out.u.cplcpy.len = QSE_COUNTOF(buf); + * if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) goto oops; + * qse_printf (QSE_T("%.*s\n"), (int)out.u.cplcpy.len, out.u.cplcpy.ptr); * @endcode * - * #QSE_AWK_RTX_VALTOSTR_CPL is different from #QSE_AWK_RTX_VALTOSTR_CPLCP + * #QSE_AWK_RTX_VALTOSTR_CPL is different from #QSE_AWK_RTX_VALTOSTR_CPLCPY * in that it doesn't copy the string to the buffer if the type of the value * is #QSE_AWK_VAL_STR. It copies the resulting string to the buffer if * the value type is not #QSE_AWK_VAL_STR. @@ -2113,7 +2113,7 @@ qse_bool_t qse_awk_rtx_valtobool ( * out.type = QSE_AWK_RTX_VALTOSTR_CPL; * out.u.cpl.ptr = buf; * out.u.cpl.len = QSE_COUNTOF(buf); - * if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops; + * if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) goto oops; * qse_printf (QSE_T("%.*s\n"), (int)out.u.cpl.len, out.u.cpl.ptr); * @endcode * @@ -2123,7 +2123,7 @@ qse_bool_t qse_awk_rtx_valtobool ( * @code * qse_awk_rtx_valtostr_out_t out; * out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - * if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops; + * if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) goto oops; * qse_printf (QSE_T("%.*s\n"), (int)out.u.cpldup.len, out.u.cpldup.ptr); * qse_awk_rtx_free (rtx, out.u.cpldup.ptr); * @endcode @@ -2136,7 +2136,7 @@ qse_bool_t qse_awk_rtx_valtobool ( * qse_str_init (&str, qse_awk_rtx_getmmgr(rtx), 100); * out.type = QSE_AWK_RTX_VALTOSTR_STRP; * out.u.strp = str; - * if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) goto oops; + * if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) goto oops; * qse_printf (QSE_T("%.*s\n"), * (int)QSE_STR_LEN(out.u.strp), QSE_STR_PTR(out.u.strp)); * qse_str_fini (&str); @@ -2151,10 +2151,9 @@ qse_bool_t qse_awk_rtx_valtobool ( * #QSE_AWK_VAL_STR, you may access its string pointer and length directly * instead of calling this function. * - * @return character pointer to a string converted on success, - * #QSE_NULL on failure + * @return 0 on success, -1 on failure */ -qse_char_t* qse_awk_rtx_valtostr ( +int qse_awk_rtx_valtostr ( qse_awk_rtx_t* rtx, /**< runtime context */ const qse_awk_val_t* val, /**< value to convert */ qse_awk_rtx_valtostr_out_t* out /**< output buffer */ diff --git a/qse/lib/awk/Awk.cpp b/qse/lib/awk/Awk.cpp index 10aaf1b4..46f684e4 100644 --- a/qse/lib/awk/Awk.cpp +++ b/qse/lib/awk/Awk.cpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.cpp 477 2011-05-24 04:22:40Z hyunghwan.chung $ + * $Id: Awk.cpp 480 2011-05-25 14:00:19Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -441,8 +441,7 @@ int Awk::Value::getStr (const char_t** str, size_t* len) const { qse_awk_rtx_valtostr_out_t out; out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr ( - run->rtx, val, &out) == QSE_NULL) + if (qse_awk_rtx_valtostr (run->rtx, val, &out) <= -1) { run->awk->retrieveError (run); return -1; diff --git a/qse/lib/awk/StdAwk.cpp b/qse/lib/awk/StdAwk.cpp index d47868a7..49d90de1 100644 --- a/qse/lib/awk/StdAwk.cpp +++ b/qse/lib/awk/StdAwk.cpp @@ -1,5 +1,5 @@ /* - * $Id: StdAwk.cpp 461 2011-05-18 02:32:39Z hyunghwan.chung $ + * $Id: StdAwk.cpp 480 2011-05-25 14:00:19Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -376,7 +376,7 @@ int StdAwk::open_console_in (Console& io) QSE_ASSERT (v != QSE_NULL); out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) return -1; + if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) return -1; if (out.u.cpldup.len == 0) { diff --git a/qse/lib/awk/rec.c b/qse/lib/awk/rec.c index 7586cb37..dfc9958b 100644 --- a/qse/lib/awk/rec.c +++ b/qse/lib/awk/rec.c @@ -1,5 +1,5 @@ /* - * $Id: rec.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ + * $Id: rec.c 480 2011-05-25 14:00:19Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -129,7 +129,7 @@ static int split_record (qse_awk_rtx_t* rtx) qse_awk_rtx_valtostr_out_t out; out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, fs, &out) == QSE_NULL) return -1; + if (qse_awk_rtx_valtostr (rtx, fs, &out) <= -1) return -1; fs_ptr = out.u.cpldup.ptr; fs_len = out.u.cpldup.len; diff --git a/qse/lib/awk/rio.c b/qse/lib/awk/rio.c index 0b311704..739d80db 100644 --- a/qse/lib/awk/rio.c +++ b/qse/lib/awk/rio.c @@ -1,5 +1,5 @@ /* - * $Id: rio.c 464 2011-05-19 03:33:28Z hyunghwan.chung $ + * $Id: rio.c 480 2011-05-25 14:00:19Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -652,7 +652,7 @@ int qse_awk_rtx_writeio_val ( out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP | QSE_AWK_RTX_VALTOSTR_PRINT; - if (qse_awk_rtx_valtostr (run, v, &out) == QSE_NULL) return -1; + if (qse_awk_rtx_valtostr (run, v, &out) <= -1) return -1; str = out.u.cpldup.ptr; len = out.u.cpldup.len; diff --git a/qse/lib/awk/run.c b/qse/lib/awk/run.c index 634a61db..e227bef2 100644 --- a/qse/lib/awk/run.c +++ b/qse/lib/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c 479 2011-05-24 15:14:58Z hyunghwan.chung $ + * $Id: run.c 480 2011-05-25 14:00:19Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -343,7 +343,7 @@ static int set_global ( qse_awk_rtx_valtostr_out_t out; out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, val, &out) == QSE_NULL) + if (qse_awk_rtx_valtostr (rtx, val, &out) <= -1) return -1; for (i = 0; i < out.u.cpldup.len; i++) @@ -397,7 +397,7 @@ static int set_global ( QSE_ASSERT (val->type != QSE_AWK_VAL_REX); out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, val, &out) == QSE_NULL) return -1; + if (qse_awk_rtx_valtostr (rtx, val, &out) <= -1) return -1; fs_ptr = out.u.cpldup.ptr; fs_len = out.u.cpldup.len; } @@ -485,8 +485,7 @@ static int set_global ( qse_awk_rtx_valtostr_out_t out; out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, val, &out) == QSE_NULL) - return -1; + if (qse_awk_rtx_valtostr (rtx, val, &out) <= -1) return -1; for (i = 0; i < out.u.cpldup.len; i++) { @@ -511,8 +510,7 @@ static int set_global ( qse_awk_rtx_valtostr_out_t out; out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, val, &out) == QSE_NULL) - return -1; + if (qse_awk_rtx_valtostr (rtx, val, &out) <= -1) return -1; if (rtx->gbl.ofs.ptr != QSE_NULL) QSE_AWK_FREE (rtx->awk, rtx->gbl.ofs.ptr); @@ -527,8 +525,7 @@ static int set_global ( qse_awk_rtx_valtostr_out_t out; out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, val, &out) == QSE_NULL) - return -1; + if (qse_awk_rtx_valtostr (rtx, val, &out) <= -1) return -1; if (rtx->gbl.ors.ptr != QSE_NULL) QSE_AWK_FREE (rtx->awk, rtx->gbl.ors.ptr); @@ -556,8 +553,7 @@ static int set_global ( QSE_ASSERT (val->type != QSE_AWK_VAL_REX); out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, val, &out) == QSE_NULL) - return -1; + if (qse_awk_rtx_valtostr (rtx, val, &out) <= -1) return -1; rss = out.u.cpldup; } @@ -595,8 +591,7 @@ static int set_global ( qse_awk_rtx_valtostr_out_t out; out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, val, &out) == QSE_NULL) - return -1; + if (qse_awk_rtx_valtostr (rtx, val, &out) <= -1) return -1; if (rtx->gbl.subsep.ptr != QSE_NULL) QSE_AWK_FREE (rtx->awk, rtx->gbl.subsep.ptr); @@ -2529,39 +2524,38 @@ static int delete_indexed ( else { /* delete x[20] */ - - qse_char_t* key; - qse_size_t keylen; qse_char_t buf[IDXBUFSIZE]; qse_awk_rtx_valtostr_out_t out; /* try with a fixed-size buffer */ - out.type = QSE_AWK_RTX_VALTOSTR_CPLCP; - out.u.cplcp.ptr = buf; - out.u.cplcp.len = QSE_COUNTOF(buf); - key = qse_awk_rtx_valtostr (rtx, idx, &out); - if (key == QSE_NULL) + out.type = QSE_AWK_RTX_VALTOSTR_CPLCPY; + out.u.cplcpy.ptr = buf; + out.u.cplcpy.len = QSE_COUNTOF(buf); + if (qse_awk_rtx_valtostr (rtx, idx, &out) <= -1) { + int n; + /* retry it in dynamic mode */ out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - key = qse_awk_rtx_valtostr (rtx, idx, &out); + n = qse_awk_rtx_valtostr (rtx, idx, &out); + qse_awk_rtx_refdownval (rtx, idx); + if (n <= -1) + { + /* change the error line */ + ADJERR_LOC (rtx, &var->loc); + return -1; + } + else + { + qse_htb_delete (map, out.u.cpldup.ptr, out.u.cpldup.len); + QSE_AWK_FREE (rtx->awk, out.u.cpldup.ptr); + } } - - qse_awk_rtx_refdownval (rtx, idx); - - if (key == QSE_NULL) + else { - /* change the error line */ - ADJERR_LOC (rtx, &var->loc); - return -1; + qse_awk_rtx_refdownval (rtx, idx); + qse_htb_delete (map, out.u.cplcpy.ptr, out.u.cplcpy.len); } - - keylen = (out.type == QSE_AWK_RTX_VALTOSTR_CPLCP)? - out.u.cplcp.len: out.u.cpldup.len; - - qse_htb_delete (map, key, keylen); - - if (key != buf) QSE_AWK_FREE (rtx->awk, key); } return 0; @@ -2825,7 +2819,7 @@ static int run_print (qse_awk_rtx_t* rtx, qse_awk_nde_print_t* nde) qse_awk_rtx_refupval (rtx, v); vsout.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, v, &vsout) == QSE_NULL) + if (qse_awk_rtx_valtostr (rtx, v, &vsout) <= -1) { qse_awk_rtx_refdownval (rtx, v); ADJERR_LOC (rtx, &nde->loc); @@ -2986,7 +2980,7 @@ static int run_printf (qse_awk_rtx_t* rtx, qse_awk_nde_print_t* nde) qse_awk_rtx_refupval (rtx, v); vsout.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, v, &vsout) == QSE_NULL) + if (qse_awk_rtx_valtostr (rtx, v, &vsout) <= -1) { qse_awk_rtx_refdownval (rtx, v); ADJERR_LOC (rtx, &nde->loc); @@ -3620,7 +3614,7 @@ static qse_awk_val_t* do_assignment_pos ( qse_awk_rtx_valtostr_out_t out; out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (run, val, &out) == QSE_NULL) + if (qse_awk_rtx_valtostr (run, val, &out) <= -1) { ADJERR_LOC (run, &pos->loc); return QSE_NULL; @@ -4071,8 +4065,7 @@ static int __cmp_int_str ( } out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, left, &out) == QSE_NULL) - return CMP_ERROR; + if (qse_awk_rtx_valtostr (rtx, left, &out) <= -1) return CMP_ERROR; if (rtx->gbl.ignorecase) { @@ -4152,8 +4145,7 @@ static int __cmp_real_str ( } out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, left, &out) == QSE_NULL) - return CMP_ERROR; + if (qse_awk_rtx_valtostr (rtx, left, &out) <= -1) return CMP_ERROR; if (rtx->gbl.ignorecase) { @@ -4733,13 +4725,10 @@ static qse_awk_val_t* eval_binop_concat ( qse_awk_rtx_valtostr_out_t lout, rout; lout.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (run, left, &lout) == QSE_NULL) - { - return QSE_NULL; - } + if (qse_awk_rtx_valtostr (run, left, &lout) <= -1) return QSE_NULL; rout.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (run, right, &rout) == QSE_NULL) + if (qse_awk_rtx_valtostr (run, right, &rout) <= -1) { QSE_AWK_FREE (run->awk, lout.u.cpldup.ptr); return QSE_NULL; @@ -4787,8 +4776,7 @@ static qse_awk_val_t* eval_binop_match0 ( qse_awk_rtx_valtostr_out_t out; out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, right, &out) == QSE_NULL) - return QSE_NULL; + if (qse_awk_rtx_valtostr (rtx, right, &out) <= -1) return QSE_NULL; rex_code = QSE_AWK_BUILDREX ( rtx->awk, out.u.cpldup.ptr, out.u.cpldup.len, &errnum); @@ -4834,7 +4822,7 @@ static qse_awk_val_t* eval_binop_match0 ( qse_awk_rtx_valtostr_out_t out; out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, left, &out) == QSE_NULL) + if (qse_awk_rtx_valtostr (rtx, left, &out) <= -1) { if (right->type != QSE_AWK_VAL_REX) QSE_AWK_FREEREX (rtx->awk, rex_code); @@ -6305,7 +6293,7 @@ static qse_awk_val_t* eval_getline (qse_awk_rtx_t* run, qse_awk_nde_t* nde) qse_awk_rtx_refupval (run, v); out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (run, v, &out) == QSE_NULL) + if (qse_awk_rtx_valtostr (run, v, &out) <= -1) { qse_awk_rtx_refdownval (run, v); return QSE_NULL; @@ -6505,10 +6493,7 @@ static int shorten_record (qse_awk_rtx_t* run, qse_size_t nflds) qse_awk_rtx_valtostr_out_t out; out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (run, v, &out) == QSE_NULL) - { - return -1; - } + if (qse_awk_rtx_valtostr (run, v, &out) <= -1) return -1; ofs_ptr = out.u.cpldup.ptr; ofs_len = out.u.cpldup.len; @@ -6601,14 +6586,14 @@ static qse_char_t* idxnde_to_str ( if (buf != QSE_NULL) { /* try with a fixed-size buffer if given */ - out.type = QSE_AWK_RTX_VALTOSTR_CPLCP; - out.u.cplcp.ptr = buf; - out.u.cplcp.len = *len; + out.type = QSE_AWK_RTX_VALTOSTR_CPLCPY; + out.u.cplcpy.ptr = buf; + out.u.cplcpy.len = *len; - if (qse_awk_rtx_valtostr (run, idx, &out) != QSE_NULL) + if (qse_awk_rtx_valtostr (run, idx, &out) >= 0) { - str = out.u.cplcp.ptr; - *len = out.u.cplcp.len; + str = out.u.cplcpy.ptr; + *len = out.u.cplcpy.len; QSE_ASSERT (str == buf); } } @@ -6618,7 +6603,7 @@ static qse_char_t* idxnde_to_str ( /* if no fixed-size buffer was given or the fixed-size * conversion failed, switch to the dynamic mode */ out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (run, idx, &out) == QSE_NULL) + if (qse_awk_rtx_valtostr (run, idx, &out) <= -1) { qse_awk_rtx_refdownval (run, idx); ADJERR_LOC (run, &nde->loc); @@ -6669,7 +6654,7 @@ static qse_char_t* idxnde_to_str ( return QSE_NULL; } - if (qse_awk_rtx_valtostr (run, idx, &out) == QSE_NULL) + if (qse_awk_rtx_valtostr (run, idx, &out) <= -1) { qse_awk_rtx_refdownval (run, idx); qse_str_fini (&idxstr); @@ -7333,7 +7318,7 @@ qse_char_t* qse_awk_rtx_format ( } out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) + if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) { qse_awk_rtx_refdownval (rtx, v); return QSE_NULL; diff --git a/qse/lib/awk/std.c b/qse/lib/awk/std.c index a4bdfe4a..453d0014 100644 --- a/qse/lib/awk/std.c +++ b/qse/lib/awk/std.c @@ -1,5 +1,5 @@ /* - * $Id: std.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ + * $Id: std.c 480 2011-05-25 14:00:19Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -991,7 +991,7 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod) QSE_ASSERT (v != QSE_NULL); out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) return -1; + if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) return -1; if (out.u.cpldup.len == 0) { diff --git a/qse/lib/awk/val.c b/qse/lib/awk/val.c index e84d05de..342c2241 100644 --- a/qse/lib/awk/val.c +++ b/qse/lib/awk/val.c @@ -1,5 +1,5 @@ /* - * $Id: val.c 479 2011-05-24 15:14:58Z hyunghwan.chung $ + * $Id: val.c 480 2011-05-25 14:00:19Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -753,7 +753,7 @@ qse_bool_t qse_awk_rtx_valtobool (qse_awk_rtx_t* run, const qse_awk_val_t* val) return QSE_FALSE; } -static qse_char_t* str_to_str ( +static int str_to_str ( qse_awk_rtx_t* rtx, const qse_char_t* str, qse_size_t str_len, qse_awk_rtx_valtostr_out_t* out) { @@ -765,21 +765,21 @@ static qse_char_t* str_to_str ( { out->u.cpl.len = str_len; out->u.cpl.ptr = str; - return out->u.cpl.ptr; + return 0; } - case QSE_AWK_RTX_VALTOSTR_CPLCP: + case QSE_AWK_RTX_VALTOSTR_CPLCPY: { - if (str_len >= out->u.cplcp.len) + if (str_len >= out->u.cplcpy.len) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); - out->u.cplcp.len = str_len + 1; - return QSE_NULL; + out->u.cplcpy.len = str_len + 1; + return -1; } - out->u.cplcp.len = - qse_strncpy (out->u.cplcp.ptr, str, str_len); - return out->u.cplcp.ptr; + out->u.cplcpy.len = + qse_strncpy (out->u.cplcpy.ptr, str, str_len); + return 0; } case QSE_AWK_RTX_VALTOSTR_CPLDUP: @@ -790,12 +790,12 @@ static qse_char_t* str_to_str ( if (tmp == QSE_NULL) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; + return -1; } out->u.cpldup.ptr = tmp; out->u.cpldup.len = str_len; - return tmp; + return 0; } case QSE_AWK_RTX_VALTOSTR_STRP: @@ -807,10 +807,10 @@ static qse_char_t* str_to_str ( if (n == (qse_size_t)-1) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; + return -1; } - return QSE_STR_PTR(out->u.strp); + return 0; } case QSE_AWK_RTX_VALTOSTR_STRPCAT: @@ -821,18 +821,18 @@ static qse_char_t* str_to_str ( if (n == (qse_size_t)-1) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; + return -1; } - return QSE_STR_PTR(out->u.strpcat); + return 0; } } qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); - return QSE_NULL; + return -1; } -static qse_char_t* val_int_to_str ( +static int val_int_to_str ( qse_awk_rtx_t* rtx, const qse_awk_val_int_t* v, qse_awk_rtx_valtostr_out_t* out) { @@ -842,96 +842,36 @@ static qse_char_t* val_int_to_str ( int type = out->type & ~QSE_AWK_RTX_VALTOSTR_PRINT; t = v->val; - if (t == 0) + if (t == 0) rlen++; + else { - /* handle zero */ - switch (type) - { - case QSE_AWK_RTX_VALTOSTR_CPL: - /* CPL and CPLCP behave the same for int_t. - * i just fall through assuming that cplcp - * and cpl are the same type. the following - * assertion at least ensure that they have - * the same size. */ - QSE_ASSERT (QSE_SIZEOF(out->u.cpl) == QSE_SIZEOF(out->u.cplcp)); - case QSE_AWK_RTX_VALTOSTR_CPLCP: - if (out->u.cplcp.len <= 1) - { - qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); - /* store the buffer size needed */ - out->u.cplcp.len = 2; - return QSE_NULL; - } - - out->u.cplcp.len = 1; /* actual length */ - out->u.cplcp.ptr[0] = QSE_T('0'); - out->u.cplcp.ptr[1] = QSE_T('\0'); - return out->u.cplcp.ptr; - - case QSE_AWK_RTX_VALTOSTR_CPLDUP: - tmp = QSE_AWK_ALLOC ( - rtx->awk, 2 * QSE_SIZEOF(qse_char_t)); - if (tmp == QSE_NULL) - { - qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; - } - - tmp[0] = QSE_T('0'); - tmp[1] = QSE_T('\0'); - - out->u.cpldup.ptr = tmp; - out->u.cpldup.len = 1; /* actual length */ - return out->u.cpldup.ptr; - - case QSE_AWK_RTX_VALTOSTR_STRP: - qse_str_clear (out->u.strp); - if (qse_str_ccat (out->u.strp, QSE_T('0')) == (qse_size_t)-1) - { - qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; - } - return QSE_STR_PTR(out->u.strp); - - case QSE_AWK_RTX_VALTOSTR_STRPCAT: - if (qse_str_ccat (out->u.strpcat, QSE_T('0')) == (qse_size_t)-1) - { - qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; - } - return QSE_STR_PTR(out->u.strpcat); - } - - qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); - return QSE_NULL; + /* non-zero values */ + if (t < 0) { t = -t; rlen++; } + while (t > 0) { rlen++; t /= 10; } } - /* non-zero values */ - if (t < 0) { t = -t; rlen++; } - while (t > 0) { rlen++; t /= 10; } - switch (type) { case QSE_AWK_RTX_VALTOSTR_CPL: /* CPL and CPLCP behave the same for int_t. - * i just fall through assuming that cplcp + * i just fall through assuming that cplcpy * and cpl are the same type. the following * assertion at least ensure that they have * the same size. */ - QSE_ASSERT (QSE_SIZEOF(out->u.cpl) == QSE_SIZEOF(out->u.cplcp)); + QSE_ASSERT (QSE_SIZEOF(out->u.cpl) == QSE_SIZEOF(out->u.cplcpy)); - case QSE_AWK_RTX_VALTOSTR_CPLCP: - if (rlen >= out->u.cplcp.len) + case QSE_AWK_RTX_VALTOSTR_CPLCPY: + if (rlen >= out->u.cplcpy.len) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); /* store the buffer size needed */ - out->u.cplcp.len = rlen + 1; - return QSE_NULL; + out->u.cplcpy.len = rlen + 1; + return -1; } - tmp = out->u.cplcp.ptr; + tmp = out->u.cplcpy.ptr; tmp[rlen] = QSE_T('\0'); - out->u.cplcp.len = rlen; + out->u.cplcpy.len = rlen; break; case QSE_AWK_RTX_VALTOSTR_CPLDUP: @@ -940,7 +880,7 @@ static qse_char_t* val_int_to_str ( if (tmp == QSE_NULL) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; + return -1; } tmp[rlen] = QSE_T('\0'); @@ -963,7 +903,7 @@ static qse_char_t* val_int_to_str ( if (n == (qse_size_t)-1) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; + return -1; } break; } @@ -980,42 +920,40 @@ static qse_char_t* val_int_to_str ( if (n == (qse_size_t)-1) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; + return -1; } break; } } t = v->val; - if (t < 0) t = -t; - - /* fill in the buffer with digits */ - while (t > 0) + if (t == 0) tmp[0] = QSE_T('0'); + else { - tmp[--rlen] = (qse_char_t)(t % 10) + QSE_T('0'); - t /= 10; + if (t < 0) t = -t; + + /* fill in the buffer with digits */ + while (t > 0) + { + tmp[--rlen] = (qse_char_t)(t % 10) + QSE_T('0'); + t /= 10; + } + + /* insert the negative sign if necessary */ + if (v->val < 0) tmp[--rlen] = QSE_T('-'); } - /* insert the negative sign if necessary */ - if (v->val < 0) tmp[--rlen] = QSE_T('-'); - - if (type == QSE_AWK_RTX_VALTOSTR_STRPCAT) - { - /* for concatenation type, change tmp to - * point to the buffer */ - tmp = QSE_STR_PTR(out->u.strpcat); - } - - return tmp; + return 0; } -static qse_char_t* val_real_to_str ( +static int val_real_to_str ( qse_awk_rtx_t* rtx, const qse_awk_val_real_t* v, qse_awk_rtx_valtostr_out_t* out) { qse_char_t* tmp; qse_size_t tmp_len; qse_str_t buf, fbu; + int buf_inited = 0, fbu_inited = 0; int type = out->type & ~QSE_AWK_RTX_VALTOSTR_PRINT; if (out->type & QSE_AWK_RTX_VALTOSTR_PRINT) @@ -1032,63 +970,52 @@ static qse_char_t* val_real_to_str ( if (qse_str_init (&buf, rtx->awk->mmgr, 256) == QSE_NULL) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; + return -1; } + buf_inited = 1; if (qse_str_init (&fbu, rtx->awk->mmgr, 256) == QSE_NULL) { qse_str_fini (&buf); qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; + return -1; } + fbu_inited = 1; tmp = qse_awk_rtx_format (rtx, &buf, &fbu, tmp, tmp_len, (qse_size_t)-1, (qse_awk_nde_t*)v, &tmp_len); - if (tmp == QSE_NULL) - { - qse_str_fini (&fbu); - qse_str_fini (&buf); - return QSE_NULL; - } + if (tmp == QSE_NULL) goto oops; switch (type) { case QSE_AWK_RTX_VALTOSTR_CPL: /* CPL and CPLCP behave the same for real_t. - * i just fall through assuming that cplcp + * i just fall through assuming that cplcpy * and cpl are the same type. the following * assertion at least ensure that they have * the same size. */ - QSE_ASSERT (QSE_SIZEOF(out->u.cpl) == QSE_SIZEOF(out->u.cplcp)); + QSE_ASSERT (QSE_SIZEOF(out->u.cpl) == QSE_SIZEOF(out->u.cplcpy)); - case QSE_AWK_RTX_VALTOSTR_CPLCP: - if (out->u.cplcp.len <= tmp_len) + case QSE_AWK_RTX_VALTOSTR_CPLCPY: + if (out->u.cplcpy.len <= tmp_len) { qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); /* store the buffer size required */ - out->u.cplcp.len = tmp_len + 1; - qse_str_close (&fbu); - qse_str_close (&buf); - return QSE_NULL; + out->u.cplcpy.len = tmp_len + 1; + goto oops; } - qse_strncpy (out->u.cplcp.ptr, tmp, tmp_len); - out->u.cplcp.len = tmp_len; - tmp = out->u.cplcp.ptr; - - qse_str_fini (&fbu); - qse_str_fini (&buf); - return tmp; + qse_strncpy (out->u.cplcpy.ptr, tmp, tmp_len); + out->u.cplcpy.len = tmp_len; + break; case QSE_AWK_RTX_VALTOSTR_CPLDUP: - qse_str_fini (&fbu); - + { qse_str_yield (&buf, QSE_NULL, 0); - qse_str_fini (&buf); - out->u.cpldup.ptr = tmp; out->u.cpldup.len = tmp_len; - return tmp; + break; + } case QSE_AWK_RTX_VALTOSTR_STRP: { @@ -1099,17 +1026,10 @@ static qse_char_t* val_real_to_str ( n = qse_str_ncat (out->u.strp, tmp, tmp_len); if (n == (qse_size_t)-1) { - qse_str_fini (&fbu); - qse_str_fini (&buf); qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; + goto oops; } - - tmp = QSE_STR_PTR(out->u.strp); - - qse_str_fini (&fbu); - qse_str_fini (&buf); - return tmp; + break; } case QSE_AWK_RTX_VALTOSTR_STRPCAT: @@ -1119,27 +1039,30 @@ static qse_char_t* val_real_to_str ( n = qse_str_ncat (out->u.strpcat, tmp, tmp_len); if (n == (qse_size_t)-1) { - qse_str_fini (&fbu); - qse_str_fini (&buf); qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); - return QSE_NULL; + goto oops; } + break; + } - tmp = QSE_STR_PTR(out->u.strpcat); - - qse_str_fini (&fbu); - qse_str_fini (&buf); - return tmp; + default: + { + qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); + goto oops; } } qse_str_fini (&fbu); qse_str_fini (&buf); - qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); - return QSE_NULL; + return 0; + +oops: + if (fbu_inited) qse_str_fini (&fbu); + if (buf_inited) qse_str_fini (&buf); + return -1; } -qse_char_t* qse_awk_rtx_valtostr ( +int qse_awk_rtx_valtostr ( qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_awk_rtx_valtostr_out_t* out) { @@ -1176,7 +1099,7 @@ qse_char_t* qse_awk_rtx_valtostr ( #endif qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTYPE, QSE_NULL); - return QSE_NULL; + return -1; } qse_char_t* qse_awk_rtx_valtocpldup ( @@ -1185,7 +1108,7 @@ qse_char_t* qse_awk_rtx_valtocpldup ( qse_awk_rtx_valtostr_out_t out; out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP; - if (qse_awk_rtx_valtostr (rtx, v, &out) == QSE_NULL) return QSE_NULL; + if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) return QSE_NULL; *len = out.u.cpldup.len; return out.u.cpldup.ptr; diff --git a/qse/samples/awk/awk04.c b/qse/samples/awk/awk04.c index 6f02a7da..e73f29df 100644 --- a/qse/samples/awk/awk04.c +++ b/qse/samples/awk/awk04.c @@ -1,5 +1,5 @@ /* - * $Id: awk04.c 479 2011-05-24 15:14:58Z hyunghwan.chung $ + * $Id: awk04.c 480 2011-05-25 14:00:19Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -149,7 +149,7 @@ int main () out.type = QSE_AWK_RTX_VALTOSTR_CPL; out.u.cpl.ptr = numbuf; /* used if the value is not a string */ out.u.cpl.len = QSE_COUNTOF(numbuf); - if (qse_awk_rtx_valtostr (rtx, rtv, &out) == QSE_NULL) + if (qse_awk_rtx_valtostr (rtx, rtv, &out) <= -1) { qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"), qse_awk_rtx_geterrmsg(rtx));