From 6cdaf0bda64274227a21aab9b7c46679c4748dbb Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 27 Aug 2019 08:42:13 +0000 Subject: [PATCH] work in progress. implementing qse_awk_addgblwithmbs() & qse_awk_addgblwithwcs() --- qse/lib/awk/parse.c | 94 ++++++++++++++++++++++++++++++++++++++++++++- qse/lib/awk/std.c | 6 +-- 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/qse/lib/awk/parse.c b/qse/lib/awk/parse.c index 3699fdfe..f02a899d 100644 --- a/qse/lib/awk/parse.c +++ b/qse/lib/awk/parse.c @@ -1926,7 +1926,99 @@ int qse_awk_addgbl (qse_awk_t* awk, const qse_char_t* name) return -1; } - n = add_global (awk, &ncs, QSE_NULL, 0); + n = add_global(awk, &ncs, QSE_NULL, 0); + + /* update the count of the static globals. + * the total global count has been updated inside add_global. */ + if (n >= 0) awk->tree.ngbls_base++; + + return n; +} + +int qse_awk_addgblwithwcs (qse_awk_t* awk, const qse_wchar_t* name) +{ + int n; + qse_wcstr_t ncs; + + if (awk->tree.ngbls > awk->tree.ngbls_base) + { + /* this function is not allowed after qse_awk_parse is called */ + SETERR_COD (awk, QSE_AWK_EPERM); + return -1; + } + + ncs.ptr = (qse_wchar_t*)name; + ncs.len = qse_wcslen(name);; + if (ncs.len <= 0) + { + SETERR_COD (awk, QSE_AWK_EINVAL); + return -1; + } + +#if defined(QSE_CHAR_IS_MCHAR) + { + qse_mcstr_t mbs; + + mbs.ptr = qse_wcstombsdupwithcmgr(name, &mbs.len, awk->_mmgr, awk->_cmgr); + if (!mbs.ptr) + { + SETERR_COD (awk, QSE_AWK_ENOMEM); /* TODO: it could be encoidng error too?? how to tell? */ + return -1; + } + + n = add_global(awk, &mbs, QSE_NULL, 0); + + qse_awk_freemem (awk, mbs.ptr); + } +#else + n = add_global(awk, &ncs, QSE_NULL, 0); +#endif + + /* update the count of the static globals. + * the total global count has been updated inside add_global. */ + if (n >= 0) awk->tree.ngbls_base++; + + return n; +} + +int qse_awk_addgblwithmbs (qse_awk_t* awk, const qse_mchar_t* name) +{ + int n; + qse_mcstr_t ncs; + + if (awk->tree.ngbls > awk->tree.ngbls_base) + { + /* this function is not allowed after qse_awk_parse is called */ + SETERR_COD (awk, QSE_AWK_EPERM); + return -1; + } + + ncs.ptr = (qse_mchar_t*)name; + ncs.len = qse_mbslen(name);; + if (ncs.len <= 0) + { + SETERR_COD (awk, QSE_AWK_EINVAL); + return -1; + } + +#if defined(QSE_CHAR_IS_MCHAR) + n = add_global(awk, &ncs, QSE_NULL, 0); +#else + { + qse_wcstr_t wcs; + + wcs.ptr = qse_mbstowcsdupwithcmgr(name, &wcs.len, awk->_mmgr, awk->_cmgr); + if (!wcs.ptr) + { + SETERR_COD (awk, QSE_AWK_ENOMEM); /* TODO: it could be encoidng error too?? how to tell? */ + return -1; + } + + n = add_global(awk, &wcs, QSE_NULL, 0); + + qse_awk_freemem (awk, wcs.ptr); + } +#endif /* update the count of the static globals. * the total global count has been updated inside add_global. */ diff --git a/qse/lib/awk/std.c b/qse/lib/awk/std.c index 3ffd0bfb..2d7e1154 100644 --- a/qse/lib/awk/std.c +++ b/qse/lib/awk/std.c @@ -2322,9 +2322,9 @@ static int add_globals (qse_awk_t* awk) { xtn_t* xtn = GET_XTN(awk); - xtn->gbl_argc = qse_awk_addgbl (awk, QSE_T("ARGC")); - xtn->gbl_argv = qse_awk_addgbl (awk, QSE_T("ARGV")); - xtn->gbl_environ = qse_awk_addgbl (awk, QSE_T("ENVIRON")); + xtn->gbl_argc = qse_awk_addgbl(awk, QSE_T("ARGC")); + xtn->gbl_argv = qse_awk_addgbl(awk, QSE_T("ARGV")); + xtn->gbl_environ = qse_awk_addgbl(awk, QSE_T("ENVIRON")); return (xtn->gbl_argc <= -1 || xtn->gbl_argv <= -1 ||