2007-05-02 01:07:00 +00:00
|
|
|
/*
|
2012-08-16 03:47:55 +00:00
|
|
|
* $Id$
|
2007-05-02 01:07:00 +00:00
|
|
|
*
|
2014-11-19 14:42:24 +00:00
|
|
|
Copyright (c) 2006-2014 Chung, Hyung-Hwan. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2007-05-02 01:07:00 +00:00
|
|
|
*/
|
|
|
|
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "awk-prv.h"
|
2012-10-29 14:41:39 +00:00
|
|
|
#include <qse/cmn/mbwc.h>
|
2019-03-05 08:21:01 +00:00
|
|
|
#include <qse/hash.h>
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_VAL
|
2013-10-28 09:40:25 +00:00
|
|
|
#include <qse/cmn/sio.h>
|
2007-05-02 01:07:00 +00:00
|
|
|
#endif
|
|
|
|
|
2009-02-27 04:56:12 +00:00
|
|
|
#define CHUNKSIZE QSE_AWK_VAL_CHUNK_SIZE
|
2008-03-23 00:47:27 +00:00
|
|
|
|
2012-11-05 05:20:12 +00:00
|
|
|
static qse_awk_val_nil_t awk_nil = { QSE_AWK_VAL_NIL, 0, 1, 0 };
|
2013-08-23 15:19:29 +00:00
|
|
|
static qse_awk_val_str_t awk_zls = { QSE_AWK_VAL_STR, 0, 1, 0, { QSE_T(""), 0 } };
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_val_t* qse_awk_val_nil = (qse_awk_val_t*)&awk_nil;
|
|
|
|
qse_awk_val_t* qse_awk_val_zls = (qse_awk_val_t*)&awk_zls;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-12-01 13:13:13 +00:00
|
|
|
qse_awk_val_t* qse_getawknilval (void)
|
|
|
|
{
|
|
|
|
return (qse_awk_val_t*)&awk_nil;
|
|
|
|
}
|
|
|
|
|
2012-11-29 14:03:59 +00:00
|
|
|
int qse_awk_rtx_isnilval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
|
|
|
{
|
2014-10-22 17:05:45 +00:00
|
|
|
return val->v_type == QSE_AWK_VAL_NIL;
|
2012-11-29 14:03:59 +00:00
|
|
|
}
|
|
|
|
|
2012-10-23 17:08:56 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makenilval (qse_awk_rtx_t* rtx)
|
|
|
|
{
|
2012-10-29 14:41:39 +00:00
|
|
|
return (qse_awk_val_t*)&awk_nil;
|
2012-10-23 17:08:56 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makeintval (qse_awk_rtx_t* rtx, qse_awk_int_t v)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_val_int_t* val;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2018-11-15 06:27:42 +00:00
|
|
|
if (QSE_AWK_IN_QUICKINT_RANGE(v)) return QSE_AWK_QUICKINT_TO_VTR(v);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-08-21 05:28:03 +00:00
|
|
|
if (rtx->vmgr.ifree == QSE_NULL)
|
2008-01-16 08:03:41 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_val_ichunk_t* c;
|
2009-01-18 01:48:21 +00:00
|
|
|
/*qse_awk_val_int_t* x;*/
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t i;
|
2008-01-16 08:03:41 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
/* use qse_awk_val_ichunk structure to avoid
|
2008-03-23 00:47:27 +00:00
|
|
|
* any alignment issues on platforms requiring
|
|
|
|
* aligned memory access - using the code commented out
|
|
|
|
* will cause a fault on such a platform */
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
/* c = QSE_AWK_ALLOC (run->awk,
|
|
|
|
QSE_SIZEOF(qse_awk_val_chunk_t)+
|
|
|
|
QSE_SIZEOF(qse_awk_val_int_t)*CHUNKSIZE); */
|
2009-08-21 05:28:03 +00:00
|
|
|
c = QSE_AWK_ALLOC (rtx->awk, QSE_SIZEOF(qse_awk_val_ichunk_t));
|
2008-12-21 21:35:07 +00:00
|
|
|
if (c == QSE_NULL)
|
2008-01-16 08:03:41 +00:00
|
|
|
{
|
2009-08-21 05:28:03 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2008-01-16 08:03:41 +00:00
|
|
|
}
|
|
|
|
|
2009-08-21 05:28:03 +00:00
|
|
|
c->next = rtx->vmgr.ichunk;
|
2008-03-23 00:47:27 +00:00
|
|
|
/*run->vmgr.ichunk = c;*/
|
2009-08-21 05:28:03 +00:00
|
|
|
rtx->vmgr.ichunk = (qse_awk_val_chunk_t*)c;
|
2008-01-16 21:46:08 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
/*x = (qse_awk_val_int_t*)(c + 1);
|
2008-01-16 21:46:08 +00:00
|
|
|
for (i = 0; i < CHUNKSIZE-1; i++)
|
2008-12-21 21:35:07 +00:00
|
|
|
x[i].nde = (qse_awk_nde_int_t*)&x[i+1];
|
|
|
|
x[i].nde = QSE_NULL;
|
2008-01-16 08:03:41 +00:00
|
|
|
|
2008-01-16 21:46:08 +00:00
|
|
|
run->vmgr.ifree = x;
|
2008-03-23 00:47:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CHUNKSIZE-1; i++)
|
2008-12-21 21:35:07 +00:00
|
|
|
c->slot[i].nde = (qse_awk_nde_int_t*)&c->slot[i+1];
|
|
|
|
c->slot[i].nde = QSE_NULL;
|
2008-03-23 00:47:27 +00:00
|
|
|
|
2009-08-21 05:28:03 +00:00
|
|
|
rtx->vmgr.ifree = &c->slot[0];
|
2008-01-16 08:03:41 +00:00
|
|
|
}
|
2008-03-23 00:47:27 +00:00
|
|
|
|
2009-08-21 05:28:03 +00:00
|
|
|
val = rtx->vmgr.ifree;
|
|
|
|
rtx->vmgr.ifree = (qse_awk_val_int_t*)val->nde;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-10-22 17:05:45 +00:00
|
|
|
val->v_type = QSE_AWK_VAL_INT;
|
2007-05-02 01:07:00 +00:00
|
|
|
val->ref = 0;
|
2012-11-05 05:20:12 +00:00
|
|
|
val->stat = 0;
|
2009-06-15 07:22:00 +00:00
|
|
|
val->nstr = 0;
|
2014-10-22 17:05:45 +00:00
|
|
|
val->i_val = v;
|
2008-12-21 21:35:07 +00:00
|
|
|
val->nde = QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_VAL
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T("makeintval => %jd [%p]\n"), (qse_intmax_t)v, val);
|
2007-05-02 01:07:00 +00:00
|
|
|
#endif
|
2008-12-21 21:35:07 +00:00
|
|
|
return (qse_awk_val_t*)val;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2013-11-04 02:33:52 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makefltval (qse_awk_rtx_t* rtx, qse_awk_flt_t v)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-11-22 05:03:31 +00:00
|
|
|
qse_awk_val_flt_t* val;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-08-21 05:28:03 +00:00
|
|
|
if (rtx->vmgr.rfree == QSE_NULL)
|
2008-01-16 21:46:08 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_val_rchunk_t* c;
|
2011-11-22 05:03:31 +00:00
|
|
|
/*qse_awk_val_flt_t* x;*/
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t i;
|
|
|
|
|
|
|
|
/* c = QSE_AWK_ALLOC (run->awk,
|
|
|
|
QSE_SIZEOF(qse_awk_val_chunk_t)+
|
2011-11-22 05:03:31 +00:00
|
|
|
QSE_SIZEOF(qse_awk_val_flt_t)*CHUNKSIZE); */
|
2009-08-21 05:28:03 +00:00
|
|
|
c = QSE_AWK_ALLOC (rtx->awk, QSE_SIZEOF(qse_awk_val_rchunk_t));
|
2008-12-21 21:35:07 +00:00
|
|
|
if (c == QSE_NULL)
|
2008-01-16 21:46:08 +00:00
|
|
|
{
|
2009-08-21 05:28:03 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2008-01-16 21:46:08 +00:00
|
|
|
}
|
|
|
|
|
2009-08-21 05:28:03 +00:00
|
|
|
c->next = rtx->vmgr.rchunk;
|
2008-03-23 00:47:27 +00:00
|
|
|
/*run->vmgr.rchunk = c;*/
|
2009-08-21 05:28:03 +00:00
|
|
|
rtx->vmgr.rchunk = (qse_awk_val_chunk_t*)c;
|
2008-01-16 21:46:08 +00:00
|
|
|
|
2008-03-23 00:47:27 +00:00
|
|
|
/*
|
2011-11-22 05:03:31 +00:00
|
|
|
x = (qse_awk_val_flt_t*)(c + 1);
|
2008-01-16 21:46:08 +00:00
|
|
|
for (i = 0; i < CHUNKSIZE-1; i++)
|
2011-11-22 05:03:31 +00:00
|
|
|
x[i].nde = (qse_awk_nde_flt_t*)&x[i+1];
|
2008-12-21 21:35:07 +00:00
|
|
|
x[i].nde = QSE_NULL;
|
2008-01-16 21:46:08 +00:00
|
|
|
|
|
|
|
run->vmgr.rfree = x;
|
2008-03-23 00:47:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CHUNKSIZE-1; i++)
|
2011-11-22 05:03:31 +00:00
|
|
|
c->slot[i].nde = (qse_awk_nde_flt_t*)&c->slot[i+1];
|
2008-12-21 21:35:07 +00:00
|
|
|
c->slot[i].nde = QSE_NULL;
|
2008-03-23 00:47:27 +00:00
|
|
|
|
2009-08-21 05:28:03 +00:00
|
|
|
rtx->vmgr.rfree = &c->slot[0];
|
2008-01-16 21:46:08 +00:00
|
|
|
}
|
2008-03-23 00:47:27 +00:00
|
|
|
|
2009-08-21 05:28:03 +00:00
|
|
|
val = rtx->vmgr.rfree;
|
2011-11-22 05:03:31 +00:00
|
|
|
rtx->vmgr.rfree = (qse_awk_val_flt_t*)val->nde;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-10-22 17:05:45 +00:00
|
|
|
val->v_type = QSE_AWK_VAL_FLT;
|
2007-05-02 01:07:00 +00:00
|
|
|
val->ref = 0;
|
2012-11-05 05:20:12 +00:00
|
|
|
val->stat = 0;
|
2009-06-15 07:22:00 +00:00
|
|
|
val->nstr = 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
val->val = v;
|
2008-12-21 21:35:07 +00:00
|
|
|
val->nde = QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_VAL
|
2013-10-28 09:40:25 +00:00
|
|
|
qse_errputstrf (QSE_T("makefltval => %Lf [%p]\n"), (double)v, val);
|
2007-05-02 01:07:00 +00:00
|
|
|
#endif
|
2008-12-21 21:35:07 +00:00
|
|
|
return (qse_awk_val_t*)val;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-14 16:17:12 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makestrvalwithxstr (qse_awk_rtx_t* rtx, const qse_cstr_t* str)
|
2014-02-20 05:29:32 +00:00
|
|
|
{
|
|
|
|
qse_awk_val_str_t* val = QSE_NULL;
|
|
|
|
qse_size_t rlen = str->len;
|
|
|
|
|
|
|
|
#ifdef ENABLE_FEATURE_SCACHE
|
|
|
|
qse_size_t i;
|
|
|
|
|
|
|
|
i = rlen / FEATURE_SCACHE_BLOCK_UNIT;
|
|
|
|
if (i < QSE_COUNTOF(rtx->scache_count))
|
|
|
|
{
|
|
|
|
rlen = (i + 1) * FEATURE_SCACHE_BLOCK_UNIT - 1;
|
|
|
|
if (rtx->scache_count[i] > 0)
|
|
|
|
{
|
|
|
|
val = rtx->scache[i][--rtx->scache_count[i]];
|
|
|
|
goto init;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-04-14 16:17:12 +00:00
|
|
|
val = (qse_awk_val_str_t*)QSE_AWK_ALLOC(rtx->awk, QSE_SIZEOF(qse_awk_val_str_t) + (rlen + 1) * QSE_SIZEOF(qse_char_t));
|
2014-02-20 05:29:32 +00:00
|
|
|
if (val == QSE_NULL)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_FEATURE_SCACHE
|
|
|
|
init:
|
|
|
|
#endif
|
2014-10-22 17:05:45 +00:00
|
|
|
val->v_type = QSE_AWK_VAL_STR;
|
2014-02-20 05:29:32 +00:00
|
|
|
val->ref = 0;
|
|
|
|
val->stat = 0;
|
|
|
|
val->nstr = 0;
|
|
|
|
val->val.len = str->len;
|
|
|
|
val->val.ptr = (qse_char_t*)(val + 1);
|
|
|
|
qse_strncpy (val->val.ptr, str->ptr, str->len);
|
|
|
|
|
|
|
|
#ifdef DEBUG_VAL
|
|
|
|
qse_errputstrf (QSE_T("makestrval => %p\n"), val);
|
|
|
|
#endif
|
|
|
|
return (qse_awk_val_t*)val;
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:17:12 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makestrvalwithmbs (qse_awk_rtx_t* rtx, const qse_mchar_t* mbs)
|
2012-10-29 14:41:39 +00:00
|
|
|
{
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
2019-04-14 16:17:12 +00:00
|
|
|
return qse_awk_rtx_makestrval(rtx, mbs, qse_mbslen(mbs));
|
2012-10-29 14:41:39 +00:00
|
|
|
#else
|
|
|
|
qse_awk_val_t* v;
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_wcstr_t tmp;
|
2012-10-29 14:41:39 +00:00
|
|
|
|
2019-04-23 15:32:23 +00:00
|
|
|
tmp.ptr = qse_mbstowcsalldupwithcmgr(mbs, &tmp.len, rtx->awk->mmgr, rtx->awk->cmgr);
|
2012-10-29 14:41:39 +00:00
|
|
|
if (tmp.ptr == QSE_NULL)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
|
2014-02-20 05:29:32 +00:00
|
|
|
v = qse_awk_rtx_makestrvalwithxstr (rtx, &tmp);
|
2012-10-29 14:41:39 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, tmp.ptr);
|
|
|
|
return v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:17:12 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makestrvalwithwcs (qse_awk_rtx_t* rtx, const qse_wchar_t* wcs)
|
2012-10-29 14:41:39 +00:00
|
|
|
{
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
qse_awk_val_t* v;
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_mcstr_t tmp;
|
2012-10-29 14:41:39 +00:00
|
|
|
|
2019-04-23 15:32:23 +00:00
|
|
|
tmp.ptr = qse_wcstombsdupwithcmgr(wcs, &tmp.len, rtx->awk->mmgr, rtx->awk->cmgr);
|
2012-10-29 14:41:39 +00:00
|
|
|
if (tmp.ptr == QSE_NULL)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
|
2014-02-20 05:29:32 +00:00
|
|
|
v = qse_awk_rtx_makestrvalwithxstr (rtx, &tmp);
|
2012-10-29 14:41:39 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, tmp.ptr);
|
|
|
|
return v;
|
|
|
|
#else
|
|
|
|
return qse_awk_rtx_makestrval (rtx, wcs, qse_wcslen(wcs));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:17:12 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makestrvalwithstr (qse_awk_rtx_t* rtx, const qse_char_t* str)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-21 05:28:03 +00:00
|
|
|
return qse_awk_rtx_makestrval (rtx, str, qse_strlen(str));
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-14 16:17:12 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makestrvalwithmxstr (qse_awk_rtx_t* rtx, const qse_mcstr_t* mxstr)
|
2012-10-29 14:41:39 +00:00
|
|
|
{
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
2019-04-14 16:17:12 +00:00
|
|
|
return qse_awk_rtx_makestrvalwithxstr(rtx, mxstr);
|
2012-10-29 14:41:39 +00:00
|
|
|
#else
|
|
|
|
qse_awk_val_t* v;
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_wcstr_t tmp;
|
2012-10-29 14:41:39 +00:00
|
|
|
qse_size_t mbslen;
|
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
mbslen = mxstr->len;
|
2019-04-23 15:32:23 +00:00
|
|
|
tmp.ptr = qse_mbsntowcsalldupwithcmgr(mxstr->ptr, &mbslen, &tmp.len, rtx->awk->mmgr, rtx->awk->cmgr);
|
2012-10-29 14:41:39 +00:00
|
|
|
if (tmp.ptr == QSE_NULL)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:17:12 +00:00
|
|
|
v = qse_awk_rtx_makestrvalwithxstr(rtx, &tmp);
|
2012-10-29 14:41:39 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, tmp.ptr);
|
|
|
|
return v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:17:12 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makestrvalwithwxstr (qse_awk_rtx_t* rtx, const qse_wcstr_t* wxstr)
|
2012-10-29 14:41:39 +00:00
|
|
|
{
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
qse_awk_val_t* v;
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_mcstr_t tmp;
|
2012-10-29 14:41:39 +00:00
|
|
|
qse_size_t wcslen;
|
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
wcslen = wxstr->len;
|
2019-04-23 15:32:23 +00:00
|
|
|
tmp.ptr = qse_wcsntombsdupwithcmgr(wxstr->ptr, &wcslen, &tmp.len, rtx->awk->mmgr, rtx->awk->cmgr);
|
2012-10-29 14:41:39 +00:00
|
|
|
if (tmp.ptr == QSE_NULL)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:17:12 +00:00
|
|
|
v = qse_awk_rtx_makestrvalwithxstr(rtx, &tmp);
|
2012-10-29 14:41:39 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, tmp.ptr);
|
|
|
|
return v;
|
|
|
|
#else
|
2019-04-14 16:17:12 +00:00
|
|
|
return qse_awk_rtx_makestrvalwithxstr(rtx, wxstr);
|
2012-10-29 14:41:39 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:17:12 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makestrval (qse_awk_rtx_t* rtx, const qse_char_t* str, qse_size_t len)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t xstr;
|
2014-07-02 12:28:58 +00:00
|
|
|
xstr.ptr = (qse_char_t*)str;
|
|
|
|
xstr.len = len;
|
2019-04-14 16:17:12 +00:00
|
|
|
return qse_awk_rtx_makestrvalwithxstr(rtx, &xstr);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-14 16:17:12 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makestrval2 (qse_awk_rtx_t* rtx, const qse_char_t* str1, qse_size_t len1, const qse_char_t* str2, qse_size_t len2)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_val_str_t* val;
|
|
|
|
qse_size_t rlen = len1 + len2;
|
2008-01-16 03:01:47 +00:00
|
|
|
|
2009-09-19 22:28:49 +00:00
|
|
|
#ifdef ENABLE_FEATURE_SCACHE
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = rlen / FEATURE_SCACHE_BLOCK_UNIT;
|
|
|
|
if (i < QSE_COUNTOF(rtx->scache_count))
|
2008-01-16 03:01:47 +00:00
|
|
|
{
|
2009-09-19 22:28:49 +00:00
|
|
|
rlen = (i + 1) * FEATURE_SCACHE_BLOCK_UNIT - 1;
|
|
|
|
if (rtx->scache_count[i] > 0)
|
2008-01-16 03:01:47 +00:00
|
|
|
{
|
2009-09-19 22:28:49 +00:00
|
|
|
val = rtx->scache[i][--rtx->scache_count[i]];
|
2008-01-16 03:01:47 +00:00
|
|
|
goto init;
|
|
|
|
}
|
|
|
|
}
|
2009-09-19 22:28:49 +00:00
|
|
|
#endif
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
val = (qse_awk_val_str_t*) QSE_AWK_ALLOC (
|
2009-08-21 05:28:03 +00:00
|
|
|
rtx->awk,
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_SIZEOF(qse_awk_val_str_t) +
|
|
|
|
(rlen+1)*QSE_SIZEOF(qse_char_t));
|
|
|
|
if (val == QSE_NULL)
|
2007-11-03 23:35:00 +00:00
|
|
|
{
|
2009-08-21 05:28:03 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2007-11-03 23:35:00 +00:00
|
|
|
}
|
|
|
|
|
2009-09-19 22:28:49 +00:00
|
|
|
#ifdef ENABLE_FEATURE_SCACHE
|
2008-01-16 03:01:47 +00:00
|
|
|
init:
|
2009-09-19 22:28:49 +00:00
|
|
|
#endif
|
2014-10-22 17:05:45 +00:00
|
|
|
val->v_type = QSE_AWK_VAL_STR;
|
2007-11-03 23:35:00 +00:00
|
|
|
val->ref = 0;
|
2012-11-05 05:20:12 +00:00
|
|
|
val->stat = 0;
|
2009-06-15 07:22:00 +00:00
|
|
|
val->nstr = 0;
|
2011-05-19 08:36:40 +00:00
|
|
|
val->val.len = len1 + len2;
|
|
|
|
val->val.ptr = (qse_char_t*)(val + 1);
|
|
|
|
qse_strncpy (val->val.ptr, str1, len1);
|
|
|
|
qse_strncpy (&val->val.ptr[len1], str2, len2);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_VAL
|
2013-10-28 09:40:25 +00:00
|
|
|
qse_errputstrf (QSE_T("makestrval2 => %p\n"), val);
|
2007-05-02 01:07:00 +00:00
|
|
|
#endif
|
2008-12-21 21:35:07 +00:00
|
|
|
return (qse_awk_val_t*)val;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 08:46:23 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makenstrvalwithstr (qse_awk_rtx_t* rtx, const qse_char_t* str)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t xstr;
|
2014-07-02 12:28:58 +00:00
|
|
|
xstr.ptr = (qse_char_t*)str;
|
|
|
|
xstr.len = qse_strlen(str);
|
|
|
|
return qse_awk_rtx_makenstrvalwithxstr (rtx, &xstr);
|
2013-05-29 08:46:23 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makenstrvalwithxstr (qse_awk_rtx_t* rtx, const qse_cstr_t* str)
|
2009-06-21 06:47:34 +00:00
|
|
|
{
|
|
|
|
int x;
|
|
|
|
qse_awk_val_t* v;
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_t l;
|
2013-11-04 02:33:52 +00:00
|
|
|
qse_awk_flt_t r;
|
2009-06-21 06:47:34 +00:00
|
|
|
|
2018-09-26 05:41:37 +00:00
|
|
|
x = qse_awk_rtx_strtonum(rtx, QSE_AWK_RTX_STRTONUM_MAKE_OPTION(1, 0), str->ptr, str->len, &l, &r);
|
|
|
|
v = qse_awk_rtx_makestrvalwithxstr(rtx, str);
|
2009-06-21 06:47:34 +00:00
|
|
|
|
|
|
|
if (v == QSE_NULL) return QSE_NULL;
|
|
|
|
|
|
|
|
if (x >= 0)
|
|
|
|
{
|
|
|
|
/* set the numeric string flag if a string
|
|
|
|
* can be converted to a number */
|
|
|
|
QSE_ASSERT (x == 0 || x == 1);
|
|
|
|
v->nstr = x + 1; /* long -> 1, real -> 2 */
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-04-18 08:42:54 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makembsval (qse_awk_rtx_t* rtx, const qse_mchar_t* ptr, qse_size_t len)
|
2019-04-14 16:17:12 +00:00
|
|
|
{
|
2019-04-18 08:42:54 +00:00
|
|
|
qse_awk_val_mbs_t* val = QSE_NULL;
|
|
|
|
qse_size_t xsz = len * QSE_SIZEOF(*ptr);
|
2019-04-14 16:17:12 +00:00
|
|
|
|
2019-04-18 08:42:54 +00:00
|
|
|
val = (qse_awk_val_mbs_t*)QSE_AWK_ALLOC(rtx->awk, QSE_SIZEOF(qse_awk_val_mbs_t) + xsz + QSE_SIZEOF(*ptr));
|
2019-04-14 16:17:12 +00:00
|
|
|
if (val == QSE_NULL)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-18 08:42:54 +00:00
|
|
|
val->v_type = QSE_AWK_VAL_MBS;
|
2019-04-14 16:17:12 +00:00
|
|
|
val->ref = 0;
|
|
|
|
val->stat = 0;
|
|
|
|
val->nstr = 0;
|
|
|
|
val->val.len = len;
|
2019-04-18 08:42:54 +00:00
|
|
|
val->val.ptr = (qse_mchar_t*)(val + 1);
|
|
|
|
QSE_MEMCPY (val->val.ptr, ptr, xsz);
|
|
|
|
val->val.ptr[len] = QSE_MT('\0');
|
2019-04-14 16:17:12 +00:00
|
|
|
|
|
|
|
return (qse_awk_val_t*)val;
|
|
|
|
}
|
|
|
|
|
2019-04-22 07:44:57 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makembsvalwithmxstr (qse_awk_rtx_t* rtx, const qse_mcstr_t* mxstr)
|
|
|
|
{
|
|
|
|
return qse_awk_rtx_makembsval(rtx, mxstr->ptr, mxstr->len);
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:17:12 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makerexval (qse_awk_rtx_t* rtx, const qse_cstr_t* str, void* code[2])
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_val_rex_t* val;
|
2009-02-17 02:11:31 +00:00
|
|
|
qse_size_t totsz;
|
|
|
|
|
|
|
|
/* the regular expression value holds:
|
2009-08-21 05:28:03 +00:00
|
|
|
* - header
|
|
|
|
* - a raw string plus with added a terminating '\0'
|
2009-02-17 02:11:31 +00:00
|
|
|
* the total size is just large enough for all these.
|
|
|
|
*/
|
2012-11-01 15:03:02 +00:00
|
|
|
totsz = QSE_SIZEOF(*val) + (QSE_SIZEOF(*str->ptr) * (str->len + 1));
|
2009-08-21 05:28:03 +00:00
|
|
|
val = (qse_awk_val_rex_t*) QSE_AWK_ALLOC (rtx->awk, totsz);
|
2009-02-17 02:11:31 +00:00
|
|
|
if (val == QSE_NULL)
|
|
|
|
{
|
2009-08-21 05:28:03 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2009-02-17 02:11:31 +00:00
|
|
|
return QSE_NULL;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-10-22 17:05:45 +00:00
|
|
|
val->v_type = QSE_AWK_VAL_REX;
|
2007-05-02 01:07:00 +00:00
|
|
|
val->ref = 0;
|
2012-11-05 05:20:12 +00:00
|
|
|
val->stat = 0;
|
2009-06-15 07:22:00 +00:00
|
|
|
val->nstr = 0;
|
2012-11-01 15:03:02 +00:00
|
|
|
val->str.len = str->len;
|
2009-02-17 02:11:31 +00:00
|
|
|
|
2012-11-01 15:03:02 +00:00
|
|
|
val->str.ptr = (qse_char_t*)(val + 1);
|
|
|
|
qse_strncpy (val->str.ptr, str->ptr, str->len);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2013-08-23 15:19:29 +00:00
|
|
|
val->code[0] = code[0];
|
|
|
|
val->code[1] = code[1];
|
2008-02-29 00:50:10 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
return (qse_awk_val_t*)val;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
static void free_mapval (qse_htb_t* map, void* dptr, qse_size_t dlen)
|
2008-09-25 02:00:50 +00:00
|
|
|
{
|
2009-09-19 22:28:49 +00:00
|
|
|
qse_awk_rtx_t* rtx = *(qse_awk_rtx_t**)QSE_XTN(map);
|
2008-09-25 02:00:50 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_VAL
|
2013-10-28 09:40:25 +00:00
|
|
|
qse_errputstrf (QSE_T("refdown in map free..."));
|
2009-09-19 22:28:49 +00:00
|
|
|
qse_awk_dprintval (rtx, dptr);
|
2013-10-28 09:40:25 +00:00
|
|
|
qse_errputstrf (QSE_T("\n"));
|
2008-09-25 02:00:50 +00:00
|
|
|
#endif
|
|
|
|
|
2009-09-19 22:28:49 +00:00
|
|
|
qse_awk_rtx_refdownval (rtx, dptr);
|
2008-09-25 02:00:50 +00:00
|
|
|
}
|
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
static void same_mapval (qse_htb_t* map, void* dptr, qse_size_t dlen)
|
2008-09-25 02:00:50 +00:00
|
|
|
{
|
2009-02-17 02:11:31 +00:00
|
|
|
qse_awk_rtx_t* run = *(qse_awk_rtx_t**)QSE_XTN(map);
|
2008-09-25 02:00:50 +00:00
|
|
|
#ifdef DEBUG_VAL
|
2013-10-28 09:40:25 +00:00
|
|
|
qse_errputstrf (QSE_T("refdown nofree in map free..."));
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_dprintval (run, dptr);
|
2013-10-28 09:40:25 +00:00
|
|
|
qse_errputstrf (QSE_T("\n"));
|
2008-09-25 02:00:50 +00:00
|
|
|
#endif
|
2009-02-01 03:59:46 +00:00
|
|
|
qse_awk_rtx_refdownval_nofree (run, dptr);
|
2008-09-25 02:00:50 +00:00
|
|
|
}
|
2007-12-07 19:57:10 +00:00
|
|
|
|
2009-08-21 05:28:03 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makemapval (qse_awk_rtx_t* rtx)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2013-03-11 16:34:41 +00:00
|
|
|
static qse_htb_style_t style =
|
2010-10-28 06:54:37 +00:00
|
|
|
{
|
|
|
|
/* the key is copied inline into a pair and is freed when the pair
|
|
|
|
* is destroyed. not setting copier for a value means that the pointer
|
|
|
|
* to the data allocated somewhere else is remembered in a pair. but
|
|
|
|
* freeing the actual value is handled by free_mapval and same_mapval */
|
|
|
|
{
|
|
|
|
QSE_HTB_COPIER_INLINE,
|
|
|
|
QSE_HTB_COPIER_DEFAULT
|
|
|
|
},
|
|
|
|
{
|
|
|
|
QSE_HTB_FREEER_DEFAULT,
|
|
|
|
free_mapval
|
|
|
|
},
|
|
|
|
QSE_HTB_COMPER_DEFAULT,
|
|
|
|
same_mapval,
|
2010-10-30 07:54:36 +00:00
|
|
|
QSE_HTB_SIZER_DEFAULT,
|
|
|
|
QSE_HTB_HASHER_DEFAULT
|
2010-10-28 06:54:37 +00:00
|
|
|
};
|
2011-05-13 08:55:53 +00:00
|
|
|
qse_awk_val_map_t* val;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-09-25 05:07:24 +00:00
|
|
|
/* CHECK */
|
|
|
|
/*
|
2008-12-21 21:35:07 +00:00
|
|
|
val = (qse_awk_val_map_t*) QSE_AWK_ALLOC (
|
|
|
|
run->awk, QSE_SIZEOF(qse_awk_val_map_t) );
|
|
|
|
if (val == QSE_NULL)
|
2007-10-24 00:18:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2007-10-24 00:18:00 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
val->type = QSE_AWK_VAL_MAP;
|
2007-05-02 01:07:00 +00:00
|
|
|
val->ref = 0;
|
2012-11-05 05:20:12 +00:00
|
|
|
val->stat = 0;
|
2009-06-15 07:22:00 +00:00
|
|
|
val->nstr = 0;
|
2010-07-09 00:58:44 +00:00
|
|
|
val->map = qse_htb_open (
|
2008-07-20 23:53:29 +00:00
|
|
|
run, 256, 70, free_mapval, same_mapval, run->awk->mmgr);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (val->map == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_AWK_FREE (run->awk, val);
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2008-09-25 02:00:50 +00:00
|
|
|
*/
|
2008-09-25 05:07:24 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
val = (qse_awk_val_map_t*) QSE_AWK_ALLOC (
|
2009-08-21 05:28:03 +00:00
|
|
|
rtx->awk,
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_SIZEOF(qse_awk_val_map_t) +
|
2010-07-09 00:58:44 +00:00
|
|
|
QSE_SIZEOF(qse_htb_t) +
|
2009-08-21 05:28:03 +00:00
|
|
|
QSE_SIZEOF(rtx));
|
2008-12-21 21:35:07 +00:00
|
|
|
if (val == QSE_NULL)
|
2008-09-25 05:07:24 +00:00
|
|
|
{
|
2009-08-21 05:28:03 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2008-09-25 05:07:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 17:05:45 +00:00
|
|
|
val->v_type = QSE_AWK_VAL_MAP;
|
2008-09-25 05:07:24 +00:00
|
|
|
val->ref = 0;
|
2012-11-05 05:20:12 +00:00
|
|
|
val->stat = 0;
|
2009-06-15 07:22:00 +00:00
|
|
|
val->nstr = 0;
|
2010-07-09 00:58:44 +00:00
|
|
|
val->map = (qse_htb_t*)(val + 1);
|
2008-09-25 05:07:24 +00:00
|
|
|
|
2011-09-01 09:43:46 +00:00
|
|
|
if (qse_htb_init (
|
|
|
|
val->map, rtx->awk->mmgr,
|
|
|
|
256, 70, QSE_SIZEOF(qse_char_t), 1) <= -1)
|
2008-09-25 02:00:50 +00:00
|
|
|
{
|
2009-08-21 05:28:03 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, val);
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2008-09-25 02:00:50 +00:00
|
|
|
}
|
2009-08-21 05:28:03 +00:00
|
|
|
*(qse_awk_rtx_t**)QSE_XTN(val->map) = rtx;
|
2013-03-11 16:34:41 +00:00
|
|
|
qse_htb_setstyle (val->map, &style);
|
2008-09-25 02:00:50 +00:00
|
|
|
/* END CHECK */
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
return (qse_awk_val_t*)val;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 14:41:39 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makemapvalwithdata (qse_awk_rtx_t* rtx, qse_awk_val_map_data_t data[])
|
|
|
|
{
|
|
|
|
qse_awk_val_t* map, * tmp;
|
|
|
|
qse_awk_val_map_data_t* p;
|
|
|
|
|
|
|
|
map = qse_awk_rtx_makemapval (rtx);
|
|
|
|
if (map == QSE_NULL) return QSE_NULL;
|
|
|
|
|
|
|
|
for (p = data; p->key.ptr; p++)
|
|
|
|
{
|
|
|
|
switch (p->type)
|
|
|
|
{
|
|
|
|
case QSE_AWK_VAL_MAP_DATA_INT:
|
2019-04-22 07:44:57 +00:00
|
|
|
tmp = qse_awk_rtx_makeintval(rtx, *(qse_awk_int_t*)p->vptr);
|
2012-10-29 14:41:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_AWK_VAL_MAP_DATA_FLT:
|
2019-04-22 07:44:57 +00:00
|
|
|
tmp = qse_awk_rtx_makefltval(rtx, *(qse_awk_flt_t*)p->vptr);
|
2012-10-29 14:41:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_AWK_VAL_MAP_DATA_STR:
|
2019-04-22 07:44:57 +00:00
|
|
|
tmp = qse_awk_rtx_makestrvalwithstr(rtx, (qse_char_t*)p->vptr);
|
2012-10-29 14:41:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_AWK_VAL_MAP_DATA_MBS:
|
2019-04-22 07:44:57 +00:00
|
|
|
tmp = qse_awk_rtx_makestrvalwithmbs(rtx, (qse_mchar_t*)p->vptr);
|
2012-10-29 14:41:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_AWK_VAL_MAP_DATA_WCS:
|
2019-04-22 07:44:57 +00:00
|
|
|
tmp = qse_awk_rtx_makestrvalwithwcs(rtx, (qse_wchar_t*)p->vptr);
|
2012-10-29 14:41:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_AWK_VAL_MAP_DATA_XSTR:
|
|
|
|
case QSE_AWK_VAL_MAP_DATA_CSTR:
|
2019-04-22 07:44:57 +00:00
|
|
|
tmp = qse_awk_rtx_makestrvalwithxstr(rtx, (qse_cstr_t*)p->vptr);
|
2012-10-29 14:41:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_AWK_VAL_MAP_DATA_MXSTR:
|
|
|
|
case QSE_AWK_VAL_MAP_DATA_MCSTR:
|
2019-04-22 07:44:57 +00:00
|
|
|
tmp = qse_awk_rtx_makestrvalwithmxstr(rtx, (qse_mcstr_t*)p->vptr);
|
2012-10-29 14:41:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_AWK_VAL_MAP_DATA_WXSTR:
|
|
|
|
case QSE_AWK_VAL_MAP_DATA_WCSTR:
|
2019-04-22 07:44:57 +00:00
|
|
|
tmp = qse_awk_rtx_makestrvalwithwxstr(rtx, (qse_wcstr_t*)p->vptr);
|
2012-10-29 14:41:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
tmp = QSE_NULL;
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp == QSE_NULL || qse_awk_rtx_setmapvalfld (rtx, map, p->key.ptr, p->key.len, tmp) == QSE_NULL)
|
|
|
|
{
|
|
|
|
if (tmp) qse_awk_rtx_freeval (rtx, tmp, 1);
|
|
|
|
qse_awk_rtx_freeval (rtx, map, 1);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2011-05-13 08:55:53 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_setmapvalfld (
|
|
|
|
qse_awk_rtx_t* rtx, qse_awk_val_t* map,
|
2011-05-13 10:16:57 +00:00
|
|
|
const qse_char_t* kptr, qse_size_t klen, qse_awk_val_t* v)
|
2011-05-13 08:55:53 +00:00
|
|
|
{
|
2014-10-23 09:30:22 +00:00
|
|
|
QSE_ASSERT (QSE_AWK_RTX_GETVALTYPE (rtx, map) == QSE_AWK_VAL_MAP);
|
2011-05-13 08:55:53 +00:00
|
|
|
|
|
|
|
if (qse_htb_upsert (
|
|
|
|
((qse_awk_val_map_t*)map)->map,
|
2011-05-13 10:16:57 +00:00
|
|
|
(qse_char_t*)kptr, klen, v, 0) == QSE_NULL)
|
2011-05-13 08:55:53 +00:00
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the value is passed in by an external party. we can't refup()
|
|
|
|
* and refdown() the value if htb_upsert() fails. that way, the value
|
|
|
|
* can be destroyed if it was passed with the reference count of 0.
|
|
|
|
* so we increment the reference count when htb_upsert() is complete */
|
|
|
|
qse_awk_rtx_refupval (rtx, v);
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
qse_awk_val_t* qse_awk_rtx_getmapvalfld (
|
|
|
|
qse_awk_rtx_t* rtx, qse_awk_val_t* map,
|
2011-05-13 10:16:57 +00:00
|
|
|
const qse_char_t* kptr, qse_size_t klen)
|
2011-05-13 08:55:53 +00:00
|
|
|
{
|
|
|
|
qse_htb_pair_t* pair;
|
|
|
|
|
2014-10-23 09:30:22 +00:00
|
|
|
QSE_ASSERT (QSE_AWK_RTX_GETVALTYPE (rtx, map) == QSE_AWK_VAL_MAP);
|
2011-05-13 08:55:53 +00:00
|
|
|
|
|
|
|
pair = qse_htb_search (((qse_awk_val_map_t*)map)->map, kptr, klen);
|
|
|
|
if (pair == QSE_NULL)
|
|
|
|
{
|
|
|
|
/* the given key is not found in the map.
|
|
|
|
* we return NULL here as this function is called by
|
|
|
|
* a user unlike the awk statement accessing the map key.
|
|
|
|
* so you can easily determine if the key is found by
|
|
|
|
* checking the error number.
|
|
|
|
*/
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QSE_HTB_VPTR(pair);
|
|
|
|
}
|
|
|
|
|
2011-05-13 22:06:55 +00:00
|
|
|
qse_awk_val_map_itr_t* qse_awk_rtx_getfirstmapvalitr (
|
|
|
|
qse_awk_rtx_t* rtx, qse_awk_val_t* map, qse_awk_val_map_itr_t* itr)
|
|
|
|
{
|
2014-10-23 09:30:22 +00:00
|
|
|
QSE_ASSERT (QSE_AWK_RTX_GETVALTYPE (rtx, map) == QSE_AWK_VAL_MAP);
|
2014-10-22 17:05:45 +00:00
|
|
|
|
|
|
|
itr->pair = qse_htb_getfirstpair (((qse_awk_val_map_t*)map)->map, &itr->buckno);
|
2011-05-13 22:06:55 +00:00
|
|
|
return itr->pair? itr: QSE_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
qse_awk_val_map_itr_t* qse_awk_rtx_getnextmapvalitr (
|
|
|
|
qse_awk_rtx_t* rtx, qse_awk_val_t* map, qse_awk_val_map_itr_t* itr)
|
|
|
|
{
|
2014-10-23 09:30:22 +00:00
|
|
|
QSE_ASSERT (QSE_AWK_RTX_GETVALTYPE (rtx, map) == QSE_AWK_VAL_MAP);
|
2014-10-22 17:05:45 +00:00
|
|
|
|
|
|
|
itr->pair = qse_htb_getnextpair (((qse_awk_val_map_t*)map)->map, itr->pair, &itr->buckno);
|
2011-05-13 22:06:55 +00:00
|
|
|
return itr->pair? itr: QSE_NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-21 05:28:03 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makerefval (
|
|
|
|
qse_awk_rtx_t* rtx, int id, qse_awk_val_t** adr)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_val_ref_t* val;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-10-29 14:41:39 +00:00
|
|
|
if (rtx->rcache_count > 0)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2012-10-29 14:41:39 +00:00
|
|
|
val = rtx->rcache[--rtx->rcache_count];
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
val = (qse_awk_val_ref_t*) QSE_AWK_ALLOC (
|
2009-08-21 05:28:03 +00:00
|
|
|
rtx->awk, QSE_SIZEOF(qse_awk_val_ref_t));
|
2008-12-21 21:35:07 +00:00
|
|
|
if (val == QSE_NULL)
|
2007-10-24 00:18:00 +00:00
|
|
|
{
|
2009-08-21 05:28:03 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2007-10-24 00:18:00 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 17:05:45 +00:00
|
|
|
val->v_type = QSE_AWK_VAL_REF;
|
2007-05-02 01:07:00 +00:00
|
|
|
val->ref = 0;
|
2012-11-05 05:20:12 +00:00
|
|
|
val->stat = 0;
|
2009-06-15 07:22:00 +00:00
|
|
|
val->nstr = 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
val->id = id;
|
|
|
|
val->adr = adr;
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
return (qse_awk_val_t*)val;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2014-02-22 14:34:45 +00:00
|
|
|
qse_awk_val_t* qse_awk_rtx_makefunval (
|
|
|
|
qse_awk_rtx_t* rtx, const qse_awk_fun_t* fun)
|
|
|
|
{
|
|
|
|
qse_awk_val_fun_t* val;
|
|
|
|
|
|
|
|
val = (qse_awk_val_fun_t*) QSE_AWK_ALLOC (
|
|
|
|
rtx->awk, QSE_SIZEOF(qse_awk_val_fun_t));
|
|
|
|
if (val == QSE_NULL)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:05:45 +00:00
|
|
|
val->v_type = QSE_AWK_VAL_FUN;
|
2014-07-02 12:28:58 +00:00
|
|
|
val->fun = (qse_awk_fun_t*)fun;
|
2014-02-22 14:34:45 +00:00
|
|
|
|
|
|
|
return (qse_awk_val_t*)val;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
int QSE_INLINE qse_awk_rtx_isstaticval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2018-11-15 06:27:42 +00:00
|
|
|
return QSE_AWK_VTR_IS_POINTER(val) && IS_STATICVAL(val);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 09:30:22 +00:00
|
|
|
int qse_awk_rtx_getvaltype (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
|
|
|
{
|
2019-04-26 16:55:57 +00:00
|
|
|
return QSE_AWK_RTX_GETVALTYPE(rtx, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
const qse_char_t* qse_awk_rtx_getvaltypename(qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
|
|
|
{
|
|
|
|
static const qse_char_t* __val_type_name[] =
|
|
|
|
{
|
|
|
|
/* synchronize this table with enum qse_awk_val_type_t in awk.h */
|
|
|
|
QSE_T("nil"),
|
|
|
|
QSE_T("int"),
|
|
|
|
QSE_T("flt"),
|
|
|
|
QSE_T("str"),
|
|
|
|
QSE_T("mbs"),
|
|
|
|
QSE_T("map"),
|
|
|
|
QSE_T("rex"),
|
|
|
|
QSE_T("ref"),
|
|
|
|
QSE_T("fun")
|
|
|
|
};
|
|
|
|
|
|
|
|
return __val_type_name[QSE_AWK_RTX_GETVALTYPE(rtx, val)];
|
2014-10-23 09:30:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qse_awk_rtx_getintfromval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
|
|
|
{
|
2019-04-26 16:55:57 +00:00
|
|
|
return QSE_AWK_RTX_GETINTFROMVAL(rtx, val);
|
2014-10-23 09:30:22 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 17:05:45 +00:00
|
|
|
void qse_awk_rtx_freeval (qse_awk_rtx_t* rtx, qse_awk_val_t* val, int cache)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-10-22 17:05:45 +00:00
|
|
|
qse_awk_val_type_t vtype;
|
|
|
|
|
2018-11-15 06:27:42 +00:00
|
|
|
if (QSE_AWK_VTR_IS_POINTER(val))
|
2014-10-22 17:05:45 +00:00
|
|
|
{
|
2014-10-23 09:18:49 +00:00
|
|
|
if (IS_STATICVAL(val)) return;
|
2014-10-22 17:05:45 +00:00
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
#ifdef DEBUG_VAL
|
|
|
|
qse_errputstrf (QSE_T("freeing [cache=%d] ... "), cache);
|
|
|
|
qse_awk_dprintval (rtx, val);
|
|
|
|
qse_errputstrf (QSE_T("\n"));
|
|
|
|
#endif
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-10-23 09:30:22 +00:00
|
|
|
vtype = QSE_AWK_RTX_GETVALTYPE (rtx, val);
|
2014-10-23 09:18:49 +00:00
|
|
|
switch (vtype)
|
2008-01-16 03:01:47 +00:00
|
|
|
{
|
2014-10-23 09:18:49 +00:00
|
|
|
case QSE_AWK_VAL_NIL:
|
|
|
|
{
|
|
|
|
QSE_AWK_FREE (rtx->awk, val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_AWK_VAL_INT:
|
|
|
|
{
|
|
|
|
((qse_awk_val_int_t*)val)->nde = (qse_awk_nde_int_t*)rtx->vmgr.ifree;
|
|
|
|
rtx->vmgr.ifree = (qse_awk_val_int_t*)val;
|
|
|
|
break;
|
|
|
|
}
|
2011-05-26 08:42:26 +00:00
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
case QSE_AWK_VAL_FLT:
|
|
|
|
{
|
|
|
|
((qse_awk_val_flt_t*)val)->nde = (qse_awk_nde_flt_t*)rtx->vmgr.rfree;
|
|
|
|
rtx->vmgr.rfree = (qse_awk_val_flt_t*)val;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-19 22:28:49 +00:00
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
case QSE_AWK_VAL_STR:
|
2008-01-16 03:01:47 +00:00
|
|
|
{
|
2014-10-23 09:18:49 +00:00
|
|
|
#ifdef ENABLE_FEATURE_SCACHE
|
|
|
|
if (cache)
|
2011-05-26 08:42:26 +00:00
|
|
|
{
|
2014-10-23 09:18:49 +00:00
|
|
|
qse_awk_val_str_t* v = (qse_awk_val_str_t*)val;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = v->val.len / FEATURE_SCACHE_BLOCK_UNIT;
|
|
|
|
if (i < QSE_COUNTOF(rtx->scache_count) &&
|
|
|
|
rtx->scache_count[i] < QSE_COUNTOF(rtx->scache[i]))
|
|
|
|
{
|
|
|
|
rtx->scache[i][rtx->scache_count[i]++] = v;
|
|
|
|
v->nstr = 0;
|
|
|
|
}
|
|
|
|
else QSE_AWK_FREE (rtx->awk, val);
|
2011-05-26 08:42:26 +00:00
|
|
|
}
|
2014-10-23 09:18:49 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
QSE_AWK_FREE (rtx->awk, val);
|
2011-05-26 08:42:26 +00:00
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-05-26 08:42:26 +00:00
|
|
|
|
2019-04-18 08:42:54 +00:00
|
|
|
case QSE_AWK_VAL_MBS:
|
2019-04-15 15:36:23 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, val);
|
|
|
|
break;
|
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
case QSE_AWK_VAL_REX:
|
|
|
|
{
|
|
|
|
/* don't free ptr as it is inlined to val
|
|
|
|
QSE_AWK_FREE (rtx->awk, ((qse_awk_val_rex_t*)val)->ptr);
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* code is just a pointer to a regular expression stored
|
|
|
|
* in parse tree nodes. so don't free it.
|
|
|
|
qse_awk_freerex (rtx->awk, ((qse_awk_val_rex_t*)val)->code[0], ((qse_awk_val_rex_t*)val)->code[1]);
|
|
|
|
*/
|
2011-05-26 08:42:26 +00:00
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, val);
|
|
|
|
break;
|
|
|
|
}
|
2009-12-11 07:03:54 +00:00
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
case QSE_AWK_VAL_MAP:
|
|
|
|
{
|
|
|
|
qse_htb_fini (((qse_awk_val_map_t*)val)->map);
|
|
|
|
QSE_AWK_FREE (rtx->awk, val);
|
|
|
|
break;
|
|
|
|
}
|
2011-05-26 08:42:26 +00:00
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
case QSE_AWK_VAL_REF:
|
2011-05-26 08:42:26 +00:00
|
|
|
{
|
2014-10-23 09:18:49 +00:00
|
|
|
if (cache && rtx->rcache_count < QSE_COUNTOF(rtx->rcache))
|
|
|
|
{
|
|
|
|
rtx->rcache[rtx->rcache_count++] = (qse_awk_val_ref_t*)val;
|
|
|
|
}
|
|
|
|
else QSE_AWK_FREE (rtx->awk, val);
|
|
|
|
break;
|
2011-05-26 08:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-23 09:18:49 +00:00
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
void qse_awk_rtx_refupval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2018-11-15 06:27:42 +00:00
|
|
|
if (QSE_AWK_VTR_IS_POINTER(val))
|
2014-10-23 09:18:49 +00:00
|
|
|
{
|
|
|
|
if (IS_STATICVAL(val)) return;
|
|
|
|
|
|
|
|
#ifdef DEBUG_VAL
|
|
|
|
qse_errputstrf (QSE_T("ref up [ptr=%p] [count=%d] "), val, (int)val->ref);
|
|
|
|
qse_awk_dprintval (rtx, val);
|
|
|
|
qse_errputstrf (QSE_T("\n"));
|
|
|
|
#endif
|
|
|
|
val->ref++;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
void qse_awk_rtx_refdownval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2018-11-15 06:27:42 +00:00
|
|
|
if (QSE_AWK_VTR_IS_POINTER(val))
|
2014-10-23 09:18:49 +00:00
|
|
|
{
|
|
|
|
if (IS_STATICVAL(val)) return;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
#ifdef DEBUG_VAL
|
|
|
|
qse_errputstrf (QSE_T("ref down [ptr=%p] [count=%d]\n"), val, (int)val->ref);
|
|
|
|
qse_awk_dprintval (rtx, val);
|
|
|
|
qse_errputstrf (QSE_T("\n"));
|
|
|
|
#endif
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
QSE_ASSERTX (val->ref > 0,
|
|
|
|
"the reference count of a value should be greater than zero for it to be decremented. check the source code for any bugs");
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
val->ref--;
|
|
|
|
if (val->ref <= 0)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_freeval(rtx, val, 1);
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
void qse_awk_rtx_refdownval_nofree (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2018-11-15 06:27:42 +00:00
|
|
|
if (QSE_AWK_VTR_IS_POINTER(val))
|
2014-10-23 09:18:49 +00:00
|
|
|
{
|
|
|
|
if (IS_STATICVAL(val)) return;
|
|
|
|
|
|
|
|
QSE_ASSERTX (val->ref > 0,
|
|
|
|
"the reference count of a value should be greater than zero for it to be decremented. check the source code for any bugs");
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
val->ref--;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
void qse_awk_rtx_freevalchunk (qse_awk_rtx_t* rtx, qse_awk_val_chunk_t* chunk)
|
2008-03-23 00:47:27 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
while (chunk != QSE_NULL)
|
2013-08-23 15:19:29 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_val_chunk_t* next = chunk->next;
|
2009-02-01 03:59:46 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, chunk);
|
2008-03-23 00:47:27 +00:00
|
|
|
chunk = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 09:18:49 +00:00
|
|
|
static int val_ref_to_bool (qse_awk_rtx_t* rtx, const qse_awk_val_ref_t* ref)
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 15:30:37 +00:00
|
|
|
switch (ref->id)
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 15:30:37 +00:00
|
|
|
case QSE_AWK_VAL_REF_POS:
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 15:30:37 +00:00
|
|
|
qse_size_t idx;
|
|
|
|
|
|
|
|
idx = (qse_size_t)ref->adr;
|
|
|
|
if (idx == 0)
|
|
|
|
{
|
|
|
|
return QSE_STR_LEN(&rtx->inrec.line) > 0;
|
|
|
|
}
|
|
|
|
else if (idx <= rtx->inrec.nflds)
|
|
|
|
{
|
|
|
|
return rtx->inrec.flds[idx-1].len > 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* the index is greater than the number of records.
|
|
|
|
* it's an empty string. so false */
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
2013-04-16 15:30:37 +00:00
|
|
|
case QSE_AWK_VAL_REF_GBL:
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 15:30:37 +00:00
|
|
|
qse_size_t idx;
|
|
|
|
idx = (qse_size_t)ref->adr;
|
2019-04-15 08:43:57 +00:00
|
|
|
return qse_awk_rtx_valtobool(rtx, RTX_STACK_GBL (rtx, idx));
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
2013-04-16 15:30:37 +00:00
|
|
|
|
|
|
|
default:
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 15:30:37 +00:00
|
|
|
qse_awk_val_t** xref = (qse_awk_val_t**)ref->adr;
|
2012-12-22 14:01:41 +00:00
|
|
|
|
2013-04-16 15:30:37 +00:00
|
|
|
/* A reference value is not able to point to another
|
|
|
|
* refernce value for the way values are represented
|
|
|
|
* in QSEAWK */
|
2014-10-23 09:30:22 +00:00
|
|
|
QSE_ASSERT (QSE_AWK_RTX_GETVALTYPE (rtx, *xref)!= QSE_AWK_VAL_REF);
|
2012-12-22 14:01:41 +00:00
|
|
|
|
2013-04-16 15:30:37 +00:00
|
|
|
/* make a recursive call back to the caller */
|
2019-04-15 08:43:57 +00:00
|
|
|
return qse_awk_rtx_valtobool(rtx, *xref);
|
2013-04-16 15:30:37 +00:00
|
|
|
}
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int qse_awk_rtx_valtobool (qse_awk_rtx_t* rtx, const qse_awk_val_t* val)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-10-22 17:05:45 +00:00
|
|
|
qse_awk_val_type_t vtype;
|
|
|
|
|
2012-10-22 09:36:15 +00:00
|
|
|
if (val == QSE_NULL) return 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-10-23 09:30:22 +00:00
|
|
|
vtype = QSE_AWK_RTX_GETVALTYPE (rtx, val);
|
2014-10-22 17:05:45 +00:00
|
|
|
switch (vtype)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_AWK_VAL_NIL:
|
2012-10-22 09:36:15 +00:00
|
|
|
return 0;
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_AWK_VAL_INT:
|
2014-10-23 09:30:22 +00:00
|
|
|
return QSE_AWK_RTX_GETINTFROMVAL (rtx, val) != 0;
|
2011-11-22 05:03:31 +00:00
|
|
|
case QSE_AWK_VAL_FLT:
|
|
|
|
return ((qse_awk_val_flt_t*)val)->val != 0.0;
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_AWK_VAL_STR:
|
2011-05-19 08:36:40 +00:00
|
|
|
return ((qse_awk_val_str_t*)val)->val.len > 0;
|
2019-04-18 08:42:54 +00:00
|
|
|
case QSE_AWK_VAL_MBS:
|
|
|
|
return ((qse_awk_val_mbs_t*)val)->val.len > 0;
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_AWK_VAL_REX: /* TODO: is this correct? */
|
2012-11-01 15:03:02 +00:00
|
|
|
return ((qse_awk_val_rex_t*)val)->str.len > 0;
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_AWK_VAL_MAP:
|
2014-10-22 17:05:45 +00:00
|
|
|
/* true if the map size is greater than 0. false if not */
|
|
|
|
return QSE_HTB_SIZE(((qse_awk_val_map_t*)val)->map) > 0;
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_AWK_VAL_REF:
|
2012-12-22 14:01:41 +00:00
|
|
|
return val_ref_to_bool (rtx, (qse_awk_val_ref_t*)val);
|
2011-05-26 08:42:26 +00:00
|
|
|
}
|
2011-05-26 08:50:19 +00:00
|
|
|
|
|
|
|
QSE_ASSERTX (
|
|
|
|
!"should never happen - invalid value type",
|
2016-04-29 03:55:42 +00:00
|
|
|
"the type of a value should be one of QSE_AWK_VAL_XXX's defined in awk-prv.h");
|
2012-10-22 09:36:15 +00:00
|
|
|
return 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 08:43:57 +00:00
|
|
|
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)
|
2009-02-28 20:18:00 +00:00
|
|
|
{
|
2009-03-01 02:44:21 +00:00
|
|
|
int type = out->type & ~QSE_AWK_RTX_VALTOSTR_PRINT;
|
|
|
|
|
|
|
|
switch (type)
|
2009-02-28 20:18:00 +00:00
|
|
|
{
|
2009-03-01 02:44:21 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_CPL:
|
2009-02-28 20:18:00 +00:00
|
|
|
{
|
2011-05-25 09:14:58 +00:00
|
|
|
out->u.cpl.len = str_len;
|
2014-07-02 12:28:58 +00:00
|
|
|
out->u.cpl.ptr = (qse_char_t*)str;
|
2011-05-26 08:00:19 +00:00
|
|
|
return 0;
|
2011-05-25 09:14:58 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 08:00:19 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_CPLCPY:
|
2011-05-25 09:14:58 +00:00
|
|
|
{
|
2011-05-26 08:00:19 +00:00
|
|
|
if (str_len >= out->u.cplcpy.len)
|
2009-02-28 20:18:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
|
2019-04-17 16:33:30 +00:00
|
|
|
/*out->u.cplcpy.len = str_len + 1;*/ /* set the required length */
|
2011-05-26 08:00:19 +00:00
|
|
|
return -1;
|
2009-02-28 20:18:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 08:43:57 +00:00
|
|
|
out->u.cplcpy.len = qse_strncpy(out->u.cplcpy.ptr, str, str_len);
|
2011-05-26 08:00:19 +00:00
|
|
|
return 0;
|
2009-02-28 20:18:00 +00:00
|
|
|
}
|
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_CPLDUP:
|
2009-02-28 20:18:00 +00:00
|
|
|
{
|
2009-03-01 02:44:21 +00:00
|
|
|
qse_char_t* tmp;
|
2009-02-28 20:18:00 +00:00
|
|
|
|
2019-04-15 08:43:57 +00:00
|
|
|
tmp = QSE_AWK_STRXDUP(rtx->awk, str, str_len);
|
|
|
|
if (!tmp)
|
2009-02-28 20:18:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-26 08:00:19 +00:00
|
|
|
return -1;
|
2009-02-28 20:18:00 +00:00
|
|
|
}
|
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
out->u.cpldup.ptr = tmp;
|
|
|
|
out->u.cpldup.len = str_len;
|
2011-05-26 08:00:19 +00:00
|
|
|
return 0;
|
2009-02-28 20:18:00 +00:00
|
|
|
}
|
2019-04-15 08:43:57 +00:00
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_STRP:
|
2009-02-28 20:18:00 +00:00
|
|
|
{
|
|
|
|
qse_size_t n;
|
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
qse_str_clear (out->u.strp);
|
2019-04-15 08:43:57 +00:00
|
|
|
n = qse_str_ncat(out->u.strp, str, str_len);
|
2009-02-28 20:18:00 +00:00
|
|
|
if (n == (qse_size_t)-1)
|
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-26 08:00:19 +00:00
|
|
|
return -1;
|
2009-02-28 20:18:00 +00:00
|
|
|
}
|
2011-05-26 08:00:19 +00:00
|
|
|
return 0;
|
2009-02-28 20:18:00 +00:00
|
|
|
}
|
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_STRPCAT:
|
2009-02-28 20:18:00 +00:00
|
|
|
{
|
2009-03-01 02:44:21 +00:00
|
|
|
qse_size_t n;
|
|
|
|
|
2019-04-15 08:43:57 +00:00
|
|
|
n = qse_str_ncat(out->u.strpcat, str, str_len);
|
2009-03-01 02:44:21 +00:00
|
|
|
if (n == (qse_size_t)-1)
|
2009-02-28 20:18:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-26 08:00:19 +00:00
|
|
|
return -1;
|
2009-02-28 20:18:00 +00:00
|
|
|
}
|
2011-05-26 08:00:19 +00:00
|
|
|
return 0;
|
2009-02-28 20:18:00 +00:00
|
|
|
}
|
2011-05-26 08:42:26 +00:00
|
|
|
}
|
2011-05-26 08:50:19 +00:00
|
|
|
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
|
|
|
|
return -1;
|
2009-02-28 20:18:00 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-04-15 08:43:57 +00:00
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
# define mbs_to_str(rtx,str,str_len,out) str_to_str(rtx,str,str_len,out)
|
|
|
|
#else
|
|
|
|
static int mbs_to_str (qse_awk_rtx_t* rtx, const qse_mchar_t* str, qse_size_t str_len, qse_awk_rtx_valtostr_out_t* out)
|
|
|
|
{
|
|
|
|
int type = out->type & ~QSE_AWK_RTX_VALTOSTR_PRINT;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case QSE_AWK_RTX_VALTOSTR_CPL:
|
|
|
|
/* conversion is required. i can't simply return it. let CPL
|
|
|
|
* behave like CPLCPY. fall thru */
|
|
|
|
case QSE_AWK_RTX_VALTOSTR_CPLCPY:
|
|
|
|
{
|
2019-04-15 15:36:23 +00:00
|
|
|
qse_size_t mbslen, wcslen;
|
|
|
|
|
|
|
|
mbslen = str_len;
|
|
|
|
wcslen = out->u.cplcpy.len;
|
2019-04-18 08:42:54 +00:00
|
|
|
if (qse_mbsntowcsnallwithcmgr(str, &mbslen, out->u.cplcpy.ptr, &wcslen, qse_findcmgrbyid(QSE_CMGR_MB8)) <= -1 || wcslen >= out->u.cplcpy.len)
|
2019-04-15 08:43:57 +00:00
|
|
|
{
|
2019-04-15 15:36:23 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL); /* TODO: change error code */
|
2019-04-15 08:43:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-04-17 16:33:30 +00:00
|
|
|
out->u.cplcpy.ptr[wcslen] = QSE_T('\0');
|
2019-04-15 15:36:23 +00:00
|
|
|
out->u.cplcpy.len = wcslen;
|
2019-04-17 16:33:30 +00:00
|
|
|
|
2019-04-15 08:43:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_AWK_RTX_VALTOSTR_CPLDUP:
|
|
|
|
{
|
|
|
|
qse_char_t* tmp;
|
2019-04-15 15:36:23 +00:00
|
|
|
qse_size_t mbslen, wcslen;
|
2019-04-15 08:43:57 +00:00
|
|
|
|
2019-04-15 15:36:23 +00:00
|
|
|
mbslen = str_len;
|
2019-04-18 08:42:54 +00:00
|
|
|
tmp = qse_mbsntowcsalldupwithcmgr(str, &mbslen, &wcslen, rtx->awk->mmgr, qse_findcmgrbyid(QSE_CMGR_MB8));
|
2019-04-15 08:43:57 +00:00
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
out->u.cpldup.ptr = tmp;
|
2019-04-15 15:36:23 +00:00
|
|
|
out->u.cpldup.len = wcslen;
|
2019-04-15 08:43:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_AWK_RTX_VALTOSTR_STRP:
|
|
|
|
{
|
|
|
|
qse_size_t n;
|
|
|
|
|
|
|
|
qse_str_clear (out->u.strp);
|
2019-04-18 08:42:54 +00:00
|
|
|
n = qse_str_ncatmbs(out->u.strp, str, str_len, qse_findcmgrbyid(QSE_CMGR_MB8));
|
2019-04-15 08:43:57 +00:00
|
|
|
if (n == (qse_size_t)-1)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_AWK_RTX_VALTOSTR_STRPCAT:
|
|
|
|
{
|
|
|
|
qse_size_t n;
|
|
|
|
|
2019-04-18 08:42:54 +00:00
|
|
|
n = qse_str_ncatmbs(out->u.strpcat, str, str_len, qse_findcmgrbyid(QSE_CMGR_MB8));
|
2019-04-15 08:43:57 +00:00
|
|
|
if (n == (qse_size_t)-1)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_char_t* tmp;
|
|
|
|
qse_size_t rlen = 0;
|
2009-03-01 02:44:21 +00:00
|
|
|
int type = out->type & ~QSE_AWK_RTX_VALTOSTR_PRINT;
|
2014-10-23 09:30:22 +00:00
|
|
|
qse_awk_int_t orgval = QSE_AWK_RTX_GETINTFROMVAL (rtx, v);
|
2014-10-22 17:05:45 +00:00
|
|
|
qse_awk_uint_t t;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-10-22 17:05:45 +00:00
|
|
|
if (orgval == 0) rlen++;
|
2011-05-26 08:00:19 +00:00
|
|
|
else
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-26 08:00:19 +00:00
|
|
|
/* non-zero values */
|
2014-10-22 17:05:45 +00:00
|
|
|
if (orgval < 0)
|
2012-08-27 15:10:57 +00:00
|
|
|
{
|
2014-10-22 17:05:45 +00:00
|
|
|
t = orgval * -1; rlen++;
|
2012-08-27 15:10:57 +00:00
|
|
|
}
|
2014-10-22 17:05:45 +00:00
|
|
|
else t = orgval;
|
2011-05-26 08:00:19 +00:00
|
|
|
while (t > 0) { rlen++; t /= 10; }
|
2009-03-01 02:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case QSE_AWK_RTX_VALTOSTR_CPL:
|
2011-05-25 09:14:58 +00:00
|
|
|
/* CPL and CPLCP behave the same for int_t.
|
2011-05-26 08:00:19 +00:00
|
|
|
* i just fall through assuming that cplcpy
|
2011-05-25 09:14:58 +00:00
|
|
|
* and cpl are the same type. the following
|
|
|
|
* assertion at least ensure that they have
|
|
|
|
* the same size. */
|
2011-05-26 08:00:19 +00:00
|
|
|
QSE_ASSERT (QSE_SIZEOF(out->u.cpl) == QSE_SIZEOF(out->u.cplcpy));
|
2011-05-25 09:14:58 +00:00
|
|
|
|
2011-05-26 08:00:19 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_CPLCPY:
|
|
|
|
if (rlen >= out->u.cplcpy.len)
|
2008-01-16 21:46:08 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
|
2009-03-01 02:44:21 +00:00
|
|
|
/* store the buffer size needed */
|
2011-05-26 08:00:19 +00:00
|
|
|
out->u.cplcpy.len = rlen + 1;
|
|
|
|
return -1;
|
2008-01-16 21:46:08 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 08:00:19 +00:00
|
|
|
tmp = out->u.cplcpy.ptr;
|
2009-03-01 02:44:21 +00:00
|
|
|
tmp[rlen] = QSE_T('\0');
|
2011-05-26 08:00:19 +00:00
|
|
|
out->u.cplcpy.len = rlen;
|
2009-03-01 02:44:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_AWK_RTX_VALTOSTR_CPLDUP:
|
2019-04-15 08:43:57 +00:00
|
|
|
tmp = QSE_AWK_ALLOC(rtx->awk, (rlen + 1) * QSE_SIZEOF(qse_char_t));
|
|
|
|
if (!tmp)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-26 08:00:19 +00:00
|
|
|
return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
tmp[rlen] = QSE_T('\0');
|
|
|
|
out->u.cpldup.ptr = tmp;
|
|
|
|
out->u.cpldup.len = rlen;
|
|
|
|
break;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_STRP:
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-03-01 02:44:21 +00:00
|
|
|
qse_size_t n;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
qse_str_clear (out->u.strp);
|
|
|
|
QSE_ASSERT (QSE_STR_LEN(out->u.strp) == 0);
|
2008-01-16 21:46:08 +00:00
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
/* point to the beginning of the buffer */
|
|
|
|
tmp = QSE_STR_PTR(out->u.strp);
|
|
|
|
|
|
|
|
/* extend the buffer */
|
2019-04-15 15:36:23 +00:00
|
|
|
n = qse_str_nccat(out->u.strp, QSE_T(' '), rlen);
|
2009-03-01 02:44:21 +00:00
|
|
|
if (n == (qse_size_t)-1)
|
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-26 08:00:19 +00:00
|
|
|
return -1;
|
2009-03-01 02:44:21 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-01-16 21:46:08 +00:00
|
|
|
}
|
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_STRPCAT:
|
|
|
|
{
|
|
|
|
qse_size_t n;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
/* point to the insertion point */
|
|
|
|
tmp = QSE_STR_PTR(out->u.strpcat) + QSE_STR_LEN(out->u.strpcat);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
/* extend the buffer */
|
2019-04-15 08:43:57 +00:00
|
|
|
n = qse_str_nccat(out->u.strpcat, QSE_T(' '), rlen);
|
2009-03-01 02:44:21 +00:00
|
|
|
if (n == (qse_size_t)-1)
|
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-26 08:00:19 +00:00
|
|
|
return -1;
|
2009-03-01 02:44:21 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-05-26 08:42:26 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 17:05:45 +00:00
|
|
|
if (orgval == 0) tmp[0] = QSE_T('0');
|
2011-05-26 08:00:19 +00:00
|
|
|
else
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-10-22 17:05:45 +00:00
|
|
|
t = (orgval < 0)? (orgval * -1): orgval;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-26 08:00:19 +00:00
|
|
|
/* fill in the buffer with digits */
|
|
|
|
while (t > 0)
|
|
|
|
{
|
|
|
|
tmp[--rlen] = (qse_char_t)(t % 10) + QSE_T('0');
|
|
|
|
t /= 10;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-26 08:00:19 +00:00
|
|
|
/* insert the negative sign if necessary */
|
2014-10-22 17:05:45 +00:00
|
|
|
if (orgval < 0) tmp[--rlen] = QSE_T('-');
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 08:00:19 +00:00
|
|
|
return 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 08:43:57 +00:00
|
|
|
static int val_flt_to_str (qse_awk_rtx_t* rtx, const qse_awk_val_flt_t* v, qse_awk_rtx_valtostr_out_t* out)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_char_t* tmp;
|
|
|
|
qse_size_t tmp_len;
|
2009-03-01 02:44:21 +00:00
|
|
|
qse_str_t buf, fbu;
|
2011-05-26 08:00:19 +00:00
|
|
|
int buf_inited = 0, fbu_inited = 0;
|
2009-03-01 02:44:21 +00:00
|
|
|
int type = out->type & ~QSE_AWK_RTX_VALTOSTR_PRINT;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
if (out->type & QSE_AWK_RTX_VALTOSTR_PRINT)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-03-01 02:44:21 +00:00
|
|
|
tmp = rtx->gbl.ofmt.ptr;
|
|
|
|
tmp_len = rtx->gbl.ofmt.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-01 02:44:21 +00:00
|
|
|
tmp = rtx->gbl.convfmt.ptr;
|
|
|
|
tmp_len = rtx->gbl.convfmt.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 08:43:57 +00:00
|
|
|
if (qse_str_init(&buf, rtx->awk->mmgr, 256) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-26 08:00:19 +00:00
|
|
|
return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-05-26 08:00:19 +00:00
|
|
|
buf_inited = 1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-04-15 08:43:57 +00:00
|
|
|
if (qse_str_init(&fbu, rtx->awk->mmgr, 256) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-03-01 02:44:21 +00:00
|
|
|
qse_str_fini (&buf);
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-26 08:00:19 +00:00
|
|
|
return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-05-26 08:00:19 +00:00
|
|
|
fbu_inited = 1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
tmp = qse_awk_rtx_format(rtx, &buf, &fbu, tmp, tmp_len, (qse_size_t)-1, (qse_awk_nde_t*)v, &tmp_len);
|
2011-05-26 08:00:19 +00:00
|
|
|
if (tmp == QSE_NULL) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
switch (type)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-03-01 02:44:21 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_CPL:
|
2011-11-22 05:03:31 +00:00
|
|
|
/* CPL and CPLCP behave the same for flt_t.
|
2011-05-26 08:00:19 +00:00
|
|
|
* i just fall through assuming that cplcpy
|
2011-05-25 09:14:58 +00:00
|
|
|
* and cpl are the same type. the following
|
|
|
|
* assertion at least ensure that they have
|
|
|
|
* the same size. */
|
2011-05-26 08:00:19 +00:00
|
|
|
QSE_ASSERT (QSE_SIZEOF(out->u.cpl) == QSE_SIZEOF(out->u.cplcpy));
|
2011-05-25 09:14:58 +00:00
|
|
|
|
2011-05-26 08:00:19 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_CPLCPY:
|
|
|
|
if (out->u.cplcpy.len <= tmp_len)
|
2009-03-01 02:44:21 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
|
2009-03-01 02:44:21 +00:00
|
|
|
/* store the buffer size required */
|
2011-05-26 08:00:19 +00:00
|
|
|
out->u.cplcpy.len = tmp_len + 1;
|
|
|
|
goto oops;
|
2009-03-01 02:44:21 +00:00
|
|
|
}
|
2008-09-05 04:58:08 +00:00
|
|
|
|
2011-05-26 08:00:19 +00:00
|
|
|
qse_strncpy (out->u.cplcpy.ptr, tmp, tmp_len);
|
|
|
|
out->u.cplcpy.len = tmp_len;
|
|
|
|
break;
|
2008-01-16 21:46:08 +00:00
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_CPLDUP:
|
2011-05-26 08:00:19 +00:00
|
|
|
{
|
2009-03-01 02:44:21 +00:00
|
|
|
qse_str_yield (&buf, QSE_NULL, 0);
|
|
|
|
out->u.cpldup.ptr = tmp;
|
|
|
|
out->u.cpldup.len = tmp_len;
|
2011-05-26 08:00:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_STRP:
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-03-01 02:44:21 +00:00
|
|
|
qse_size_t n;
|
|
|
|
|
|
|
|
qse_str_clear (out->u.strp);
|
|
|
|
|
|
|
|
n = qse_str_ncat (out->u.strp, tmp, tmp_len);
|
|
|
|
if (n == (qse_size_t)-1)
|
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-26 08:00:19 +00:00
|
|
|
goto oops;
|
2009-03-01 02:44:21 +00:00
|
|
|
}
|
2011-05-26 08:00:19 +00:00
|
|
|
break;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
case QSE_AWK_RTX_VALTOSTR_STRPCAT:
|
|
|
|
{
|
|
|
|
qse_size_t n;
|
|
|
|
|
|
|
|
n = qse_str_ncat (out->u.strpcat, tmp, tmp_len);
|
|
|
|
if (n == (qse_size_t)-1)
|
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-26 08:00:19 +00:00
|
|
|
goto oops;
|
2009-03-01 02:44:21 +00:00
|
|
|
}
|
2011-05-26 08:00:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-26 08:00:19 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
|
|
|
|
goto oops;
|
2009-03-01 02:44:21 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-03-01 02:44:21 +00:00
|
|
|
qse_str_fini (&fbu);
|
|
|
|
qse_str_fini (&buf);
|
2011-05-26 08:00:19 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
|
|
|
if (fbu_inited) qse_str_fini (&fbu);
|
|
|
|
if (buf_inited) qse_str_fini (&buf);
|
|
|
|
return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 08:43:57 +00:00
|
|
|
static int val_ref_to_str (qse_awk_rtx_t* rtx, const qse_awk_val_ref_t* ref, qse_awk_rtx_valtostr_out_t* out)
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
switch (ref->id)
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
case QSE_AWK_VAL_REF_POS:
|
|
|
|
{
|
|
|
|
qse_size_t idx;
|
2012-12-22 14:01:41 +00:00
|
|
|
|
2013-04-16 10:09:24 +00:00
|
|
|
/* special case when the reference value is
|
|
|
|
* pointing to the positional */
|
2012-12-22 14:01:41 +00:00
|
|
|
|
2013-04-16 10:09:24 +00:00
|
|
|
idx = (qse_size_t)ref->adr;
|
|
|
|
if (idx == 0)
|
|
|
|
{
|
2019-04-17 16:33:30 +00:00
|
|
|
return str_to_str(
|
2013-04-16 10:09:24 +00:00
|
|
|
rtx,
|
|
|
|
QSE_STR_PTR(&rtx->inrec.line),
|
|
|
|
QSE_STR_LEN(&rtx->inrec.line),
|
|
|
|
out
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (idx <= rtx->inrec.nflds)
|
|
|
|
{
|
2019-04-17 16:33:30 +00:00
|
|
|
return str_to_str(
|
2013-04-16 10:09:24 +00:00
|
|
|
rtx,
|
|
|
|
rtx->inrec.flds[idx-1].ptr,
|
|
|
|
rtx->inrec.flds[idx-1].len,
|
|
|
|
out
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-17 16:33:30 +00:00
|
|
|
return str_to_str(rtx, QSE_T(""), 0, out);
|
2013-04-16 10:09:24 +00:00
|
|
|
}
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
2013-04-16 10:09:24 +00:00
|
|
|
|
|
|
|
case QSE_AWK_VAL_REF_GBL:
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_size_t idx = (qse_size_t)ref->adr;
|
|
|
|
return qse_awk_rtx_valtostr (rtx, RTX_STACK_GBL (rtx, idx), out);
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
2013-04-16 10:09:24 +00:00
|
|
|
|
|
|
|
default:
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_awk_val_t** xref = (qse_awk_val_t**)ref->adr;
|
2012-12-22 14:01:41 +00:00
|
|
|
|
2013-04-16 10:09:24 +00:00
|
|
|
/* A reference value is not able to point to another
|
|
|
|
* refernce value for the way values are represented
|
|
|
|
* in QSEAWK */
|
2014-10-23 09:30:22 +00:00
|
|
|
QSE_ASSERT (QSE_AWK_RTX_GETVALTYPE (rtx, *xref) != QSE_AWK_VAL_REF);
|
2012-12-22 14:01:41 +00:00
|
|
|
|
2013-04-16 10:09:24 +00:00
|
|
|
/* make a recursive call back to the caller */
|
|
|
|
return qse_awk_rtx_valtostr (rtx, *xref, out);
|
|
|
|
}
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:32:23 +00:00
|
|
|
int qse_awk_rtx_valtostr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_awk_rtx_valtostr_out_t* out)
|
2009-02-28 20:18:00 +00:00
|
|
|
{
|
2014-10-23 09:30:22 +00:00
|
|
|
qse_awk_val_type_t vtype = QSE_AWK_RTX_GETVALTYPE (rtx, v);
|
2014-10-22 17:05:45 +00:00
|
|
|
|
|
|
|
switch (vtype)
|
2009-02-28 20:18:00 +00:00
|
|
|
{
|
|
|
|
case QSE_AWK_VAL_NIL:
|
2011-05-25 09:14:58 +00:00
|
|
|
{
|
2019-04-15 08:43:57 +00:00
|
|
|
return str_to_str(rtx, QSE_T(""), 0, out);
|
2011-05-25 09:14:58 +00:00
|
|
|
}
|
2009-02-28 20:18:00 +00:00
|
|
|
|
|
|
|
case QSE_AWK_VAL_INT:
|
2011-05-25 09:14:58 +00:00
|
|
|
{
|
2019-04-15 08:43:57 +00:00
|
|
|
return val_int_to_str(rtx, (qse_awk_val_int_t*)v, out);
|
2011-05-25 09:14:58 +00:00
|
|
|
}
|
2009-02-28 20:18:00 +00:00
|
|
|
|
2011-11-22 05:03:31 +00:00
|
|
|
case QSE_AWK_VAL_FLT:
|
2011-05-25 09:14:58 +00:00
|
|
|
{
|
2019-04-15 08:43:57 +00:00
|
|
|
return val_flt_to_str(rtx, (qse_awk_val_flt_t*)v, out);
|
2011-05-25 09:14:58 +00:00
|
|
|
}
|
2009-02-28 20:18:00 +00:00
|
|
|
|
|
|
|
case QSE_AWK_VAL_STR:
|
|
|
|
{
|
|
|
|
qse_awk_val_str_t* vs = (qse_awk_val_str_t*)v;
|
2019-04-15 08:43:57 +00:00
|
|
|
return str_to_str(rtx, vs->val.ptr, vs->val.len, out);
|
|
|
|
}
|
|
|
|
|
2019-04-18 08:42:54 +00:00
|
|
|
case QSE_AWK_VAL_MBS:
|
2019-04-15 08:43:57 +00:00
|
|
|
{
|
2019-04-18 08:42:54 +00:00
|
|
|
qse_awk_val_mbs_t* vs = (qse_awk_val_mbs_t*)v;
|
2019-04-15 08:43:57 +00:00
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
return str_to_str(rtx, vs->val.ptr, vs->val.len, out);
|
|
|
|
#else
|
|
|
|
return mbs_to_str(rtx, vs->val.ptr, vs->val.len, out);
|
|
|
|
#endif
|
2009-02-28 20:18:00 +00:00
|
|
|
}
|
2012-11-28 05:34:19 +00:00
|
|
|
|
|
|
|
case QSE_AWK_VAL_MAP:
|
|
|
|
{
|
|
|
|
if (rtx->awk->opt.trait & QSE_AWK_FLEXMAP)
|
2013-04-16 10:09:24 +00:00
|
|
|
{
|
2019-04-15 08:43:57 +00:00
|
|
|
return str_to_str(rtx, QSE_T("#MAP"), 4, out);
|
2013-04-16 10:09:24 +00:00
|
|
|
}
|
2012-11-28 05:34:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-12-22 14:01:41 +00:00
|
|
|
|
|
|
|
case QSE_AWK_VAL_REF:
|
|
|
|
{
|
2019-04-15 08:43:57 +00:00
|
|
|
return val_ref_to_str(rtx, (qse_awk_val_ref_t*)v, out);
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
2011-05-26 08:42:26 +00:00
|
|
|
}
|
2011-05-26 08:50:19 +00:00
|
|
|
|
2012-12-22 14:01:41 +00:00
|
|
|
|
2011-05-26 08:50:19 +00:00
|
|
|
#ifdef DEBUG_VAL
|
2013-10-28 09:40:25 +00:00
|
|
|
qse_errputstrf (
|
2011-05-26 08:50:19 +00:00
|
|
|
QSE_T(">>WRONG VALUE TYPE [%d] in qse_awk_rtx_valtostr\n"),
|
|
|
|
v->type
|
|
|
|
);
|
|
|
|
#endif
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTOSTR, QSE_NULL);
|
2011-05-26 08:50:19 +00:00
|
|
|
return -1;
|
2009-02-28 20:18:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 04:30:49 +00:00
|
|
|
qse_char_t* qse_awk_rtx_valtostrdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
|
2012-10-31 08:31:58 +00:00
|
|
|
{
|
|
|
|
qse_awk_rtx_valtostr_out_t out;
|
|
|
|
|
|
|
|
out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP;
|
2019-04-14 16:17:12 +00:00
|
|
|
if (qse_awk_rtx_valtostr(rtx, v, &out) <= -1) return QSE_NULL;
|
2012-10-31 08:31:58 +00:00
|
|
|
|
|
|
|
if (len) *len = out.u.cpldup.len;
|
|
|
|
return out.u.cpldup.ptr;
|
|
|
|
}
|
|
|
|
|
2019-04-15 04:30:49 +00:00
|
|
|
qse_mchar_t* qse_awk_rtx_valtombsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
|
2012-10-31 08:31:58 +00:00
|
|
|
{
|
2019-04-23 15:32:23 +00:00
|
|
|
qse_mchar_t* mbs;
|
2012-10-31 08:31:58 +00:00
|
|
|
|
2019-04-23 15:32:23 +00:00
|
|
|
if (QSE_AWK_RTX_GETVALTYPE(rtx,v) == QSE_AWK_VAL_MBS)
|
|
|
|
{
|
|
|
|
mbs = qse_mbsxdup(((qse_awk_val_mbs_t*)v)->val.ptr, ((qse_awk_val_mbs_t*)v)->val.len, rtx->awk->mmgr);
|
|
|
|
if (!mbs)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterror (rtx, QSE_AWK_ENOMEM, QSE_NULL, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
2012-10-31 08:31:58 +00:00
|
|
|
|
2019-04-23 15:32:23 +00:00
|
|
|
if (len) *len = ((qse_awk_val_mbs_t*)v)->val.len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qse_char_t* str0;
|
|
|
|
qse_size_t len0, len1;
|
2012-10-31 08:31:58 +00:00
|
|
|
|
2019-04-23 15:32:23 +00:00
|
|
|
str0 = qse_awk_rtx_getvalstr(rtx, v, &len0);
|
|
|
|
if (!str0) return -1;
|
|
|
|
|
|
|
|
mbs = qse_wcsntombsdupwithcmgr(str0, len0, &len1, rtx->awk->mmgr, rtx->awk->cmgr);
|
|
|
|
qse_awk_rtx_freevalstr (rtx, v, str0);
|
|
|
|
if (!mbs)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterror (rtx, QSE_AWK_ENOMEM, QSE_NULL, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
if (len) *len = len1;
|
|
|
|
}
|
2012-10-31 08:31:58 +00:00
|
|
|
|
|
|
|
return mbs;
|
|
|
|
}
|
|
|
|
|
2019-04-15 04:30:49 +00:00
|
|
|
qse_wchar_t* qse_awk_rtx_valtowcsdup (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
|
2009-03-01 09:27:03 +00:00
|
|
|
{
|
2012-10-31 08:31:58 +00:00
|
|
|
qse_wchar_t* wcs;
|
2019-04-23 15:32:23 +00:00
|
|
|
qse_awk_val_type_t vtype;
|
2009-03-01 09:27:03 +00:00
|
|
|
|
2019-04-23 15:32:23 +00:00
|
|
|
vtype = QSE_AWK_RTX_GETVALTYPE(rtx,v);
|
2009-03-01 09:27:03 +00:00
|
|
|
|
2019-04-23 15:32:23 +00:00
|
|
|
switch (vtype)
|
|
|
|
{
|
|
|
|
case QSE_AWK_VAL_MBS:
|
|
|
|
{
|
|
|
|
qse_size_t wcslen;
|
|
|
|
wcs = qse_mbsntowcsalldupwithcmgr(((qse_awk_val_mbs_t*)v)->val.ptr, ((qse_awk_val_mbs_t*)v)->val.len, &wcslen, rtx->awk->mmgr, rtx->awk->cmgr);
|
|
|
|
if (!wcs)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterror (rtx, QSE_AWK_ENOMEM, QSE_NULL, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
2012-10-31 08:31:58 +00:00
|
|
|
|
2019-04-23 15:32:23 +00:00
|
|
|
if (len) *len = wcslen;
|
|
|
|
break;
|
|
|
|
}
|
2012-10-31 08:31:58 +00:00
|
|
|
|
2019-04-23 15:32:23 +00:00
|
|
|
case QSE_AWK_VAL_STR:
|
|
|
|
{
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
qse_size_t wcslen;
|
|
|
|
wcs = qse_mbsntowcsalldupwithcmgr(((qse_awk_val_str_t*)v)->val.ptr, ((qse_awk_val_str_t*)v)->val.len, &wcslen, rtx->awk->mmgr, rtx->awk->cmgr);
|
|
|
|
#else
|
|
|
|
wcs = qse_strxdup(((qse_awk_val_str_t*)v)->val.ptr, ((qse_awk_val_str_t*)v)->val.len, rtx->awk->mmgr);
|
|
|
|
#endif
|
|
|
|
if (!wcs)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterror (rtx, QSE_AWK_ENOMEM, QSE_NULL, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
if (len) *len = wcslen;
|
|
|
|
#else
|
|
|
|
if (len) *len = ((qse_awk_val_str_t*)v)->val.len;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
qse_size_t duplen, wcslen;
|
|
|
|
qse_char_t* dup;
|
|
|
|
|
|
|
|
dup = qse_awk_rtx_valtostrdup(rtx, v, &duplen);
|
|
|
|
if (!dup) return QSE_NULL;
|
|
|
|
|
|
|
|
wcs = qse_mbsntowcsalldupwithcmgr(dup, duplen, &wcslen, rtx->awk->mmgr, rtx->awk->cmgr);
|
|
|
|
qse_awk_rtx_freemem (rtx, dup);
|
|
|
|
if (!wcs)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterror (rtx, QSE_AWK_ENOMEM, QSE_NULL, QSE_NULL);
|
|
|
|
return QSE_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len) *len = wcslen;
|
|
|
|
#else
|
|
|
|
qse_size_t wcslen;
|
|
|
|
wcs = qse_awk_rtx_valtostrdup(rtx, v, &wcslen);
|
|
|
|
if (!wcs) return QSE_NULL;
|
|
|
|
if (len) *len = wcslen;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wcs;
|
2009-03-01 09:27:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 04:30:49 +00:00
|
|
|
qse_char_t* qse_awk_rtx_getvalstr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
|
2014-02-19 15:24:33 +00:00
|
|
|
{
|
2014-10-23 09:30:22 +00:00
|
|
|
if (QSE_AWK_RTX_GETVALTYPE(rtx, v) == QSE_AWK_VAL_STR)
|
2014-02-19 15:24:33 +00:00
|
|
|
{
|
|
|
|
if (len) *len = ((qse_awk_val_str_t*)v)->val.len;
|
|
|
|
return ((qse_awk_val_str_t*)v)->val.ptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-22 07:44:57 +00:00
|
|
|
return qse_awk_rtx_valtostrdup(rtx, v, len);
|
2014-02-19 15:24:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 04:30:49 +00:00
|
|
|
void qse_awk_rtx_freevalstr (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_char_t* str)
|
2014-02-19 15:24:33 +00:00
|
|
|
{
|
2014-10-23 09:30:22 +00:00
|
|
|
if (QSE_AWK_RTX_GETVALTYPE(rtx, v) != QSE_AWK_VAL_STR ||
|
2014-02-19 15:24:33 +00:00
|
|
|
str != ((qse_awk_val_str_t*)v)->val.ptr)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_freemem (rtx, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 07:44:57 +00:00
|
|
|
|
|
|
|
qse_mchar_t* qse_awk_rtx_getvalmbs (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
|
|
|
|
{
|
|
|
|
if (QSE_AWK_RTX_GETVALTYPE(rtx, v) == QSE_AWK_VAL_MBS)
|
|
|
|
{
|
|
|
|
if (len) *len = ((qse_awk_val_mbs_t*)v)->val.len;
|
|
|
|
return ((qse_awk_val_mbs_t*)v)->val.ptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return qse_awk_rtx_valtombsdup(rtx, v, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void qse_awk_rtx_freevalmbs (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_mchar_t* str)
|
|
|
|
{
|
|
|
|
if (QSE_AWK_RTX_GETVALTYPE(rtx, v) != QSE_AWK_VAL_MBS ||
|
|
|
|
str != ((qse_awk_val_mbs_t*)v)->val.ptr)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_freemem (rtx, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 04:30:49 +00:00
|
|
|
static int val_ref_to_num (qse_awk_rtx_t* rtx, const qse_awk_val_ref_t* ref, qse_awk_int_t* l, qse_awk_flt_t* r)
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
switch (ref->id)
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
case QSE_AWK_VAL_REF_POS:
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_size_t idx;
|
|
|
|
|
|
|
|
idx = (qse_size_t)ref->adr;
|
|
|
|
if (idx == 0)
|
|
|
|
{
|
2019-04-15 04:30:49 +00:00
|
|
|
return qse_awk_rtx_strtonum(
|
2018-09-26 05:41:37 +00:00
|
|
|
rtx,
|
|
|
|
QSE_AWK_RTX_STRTONUM_MAKE_OPTION(0, 0),
|
2013-04-16 10:09:24 +00:00
|
|
|
QSE_STR_PTR(&rtx->inrec.line),
|
|
|
|
QSE_STR_LEN(&rtx->inrec.line),
|
|
|
|
l, r
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (idx <= rtx->inrec.nflds)
|
|
|
|
{
|
2019-04-15 04:30:49 +00:00
|
|
|
return qse_awk_rtx_strtonum(
|
2018-09-26 05:41:37 +00:00
|
|
|
rtx,
|
|
|
|
QSE_AWK_RTX_STRTONUM_MAKE_OPTION(0, 0),
|
2013-04-16 10:09:24 +00:00
|
|
|
rtx->inrec.flds[idx-1].ptr,
|
|
|
|
rtx->inrec.flds[idx-1].len,
|
|
|
|
l, r
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-15 04:30:49 +00:00
|
|
|
return qse_awk_rtx_strtonum(
|
2018-09-26 05:41:37 +00:00
|
|
|
rtx, QSE_AWK_RTX_STRTONUM_MAKE_OPTION(0, 0), QSE_T(""), 0, l, r
|
2013-04-16 10:09:24 +00:00
|
|
|
);
|
|
|
|
}
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
2013-04-16 10:09:24 +00:00
|
|
|
|
|
|
|
case QSE_AWK_VAL_REF_GBL:
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_size_t idx = (qse_size_t)ref->adr;
|
|
|
|
return qse_awk_rtx_valtonum (rtx, RTX_STACK_GBL (rtx, idx), l, r);
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
2013-04-16 10:09:24 +00:00
|
|
|
|
|
|
|
default:
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_awk_val_t** xref = (qse_awk_val_t**)ref->adr;
|
2012-12-22 14:01:41 +00:00
|
|
|
|
2013-04-16 10:09:24 +00:00
|
|
|
/* A reference value is not able to point to another
|
|
|
|
* refernce value for the way values are represented
|
|
|
|
* in QSEAWK */
|
2014-10-23 09:30:22 +00:00
|
|
|
QSE_ASSERT (QSE_AWK_RTX_GETVALTYPE (rtx, *xref) != QSE_AWK_VAL_REF);
|
2012-12-22 14:01:41 +00:00
|
|
|
|
2013-04-16 10:09:24 +00:00
|
|
|
/* make a recursive call back to the caller */
|
2019-04-15 04:30:49 +00:00
|
|
|
return qse_awk_rtx_valtonum(rtx, *xref, l, r);
|
2013-04-16 10:09:24 +00:00
|
|
|
}
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-15 04:30:49 +00:00
|
|
|
int qse_awk_rtx_valtonum (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_awk_int_t* l, qse_awk_flt_t* r)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-10-23 09:30:22 +00:00
|
|
|
qse_awk_val_type_t vtype = QSE_AWK_RTX_GETVALTYPE (rtx, v);
|
2014-10-22 17:05:45 +00:00
|
|
|
|
|
|
|
switch (vtype)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-26 08:42:26 +00:00
|
|
|
case QSE_AWK_VAL_NIL:
|
|
|
|
{
|
|
|
|
*l = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-26 08:42:26 +00:00
|
|
|
case QSE_AWK_VAL_INT:
|
|
|
|
{
|
2014-10-23 09:30:22 +00:00
|
|
|
*l = QSE_AWK_RTX_GETINTFROMVAL (rtx, v);
|
2011-05-26 08:42:26 +00:00
|
|
|
return 0; /* long */
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-11-22 05:03:31 +00:00
|
|
|
case QSE_AWK_VAL_FLT:
|
2011-05-26 08:42:26 +00:00
|
|
|
{
|
2011-11-22 05:03:31 +00:00
|
|
|
*r = ((qse_awk_val_flt_t*)v)->val;
|
2011-05-26 08:42:26 +00:00
|
|
|
return 1; /* real */
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-26 08:42:26 +00:00
|
|
|
case QSE_AWK_VAL_STR:
|
|
|
|
{
|
2019-04-22 09:03:02 +00:00
|
|
|
return qse_awk_rtx_strtonum(
|
2018-09-26 05:41:37 +00:00
|
|
|
rtx,
|
|
|
|
QSE_AWK_RTX_STRTONUM_MAKE_OPTION(0, 0),
|
2011-05-26 08:42:26 +00:00
|
|
|
((qse_awk_val_str_t*)v)->val.ptr,
|
|
|
|
((qse_awk_val_str_t*)v)->val.len,
|
|
|
|
l, r
|
|
|
|
);
|
|
|
|
}
|
2012-12-22 14:01:41 +00:00
|
|
|
|
2019-04-18 08:42:54 +00:00
|
|
|
case QSE_AWK_VAL_MBS:
|
2019-04-14 16:17:12 +00:00
|
|
|
{
|
2019-04-22 09:03:02 +00:00
|
|
|
return qse_awk_rtx_mbstonum(
|
2019-04-14 16:17:12 +00:00
|
|
|
rtx,
|
|
|
|
QSE_AWK_RTX_STRTONUM_MAKE_OPTION(0, 0),
|
2019-04-18 08:42:54 +00:00
|
|
|
((qse_awk_val_mbs_t*)v)->val.ptr,
|
|
|
|
((qse_awk_val_mbs_t*)v)->val.len,
|
2019-04-14 16:17:12 +00:00
|
|
|
l, r
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-22 01:34:37 +00:00
|
|
|
case QSE_AWK_VAL_MAP:
|
|
|
|
{
|
|
|
|
if (rtx->awk->opt.trait & QSE_AWK_FLEXMAP)
|
|
|
|
{
|
|
|
|
*l = QSE_HTB_SIZE(((qse_awk_val_map_t*)v)->map);
|
|
|
|
return 0; /* long */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-22 14:01:41 +00:00
|
|
|
case QSE_AWK_VAL_REF:
|
|
|
|
{
|
2019-04-15 04:30:49 +00:00
|
|
|
return val_ref_to_num(rtx, (qse_awk_val_ref_t*)v, l, r);
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
2011-05-26 08:42:26 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-26 08:50:19 +00:00
|
|
|
#ifdef DEBUG_VAL
|
2013-10-28 09:40:25 +00:00
|
|
|
qse_errputstrf (
|
2012-09-10 09:55:22 +00:00
|
|
|
QSE_T(">>WRONG VALUE TYPE [%d] in qse_awk_rtx_valtonum()\n"),
|
2011-05-26 08:50:19 +00:00
|
|
|
v->type
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTONUM, QSE_NULL);
|
2011-05-26 08:50:19 +00:00
|
|
|
return -1; /* error */
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 04:30:49 +00:00
|
|
|
int qse_awk_rtx_valtoint (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_awk_int_t* l)
|
2011-07-25 08:24:13 +00:00
|
|
|
{
|
|
|
|
int n;
|
2013-11-04 02:33:52 +00:00
|
|
|
qse_awk_flt_t r;
|
2011-07-25 08:24:13 +00:00
|
|
|
|
2019-04-15 04:30:49 +00:00
|
|
|
n = qse_awk_rtx_valtonum(rtx, v, l, &r);
|
2011-07-25 08:24:13 +00:00
|
|
|
if (n == 1)
|
|
|
|
{
|
2013-11-05 14:16:45 +00:00
|
|
|
*l = (qse_awk_int_t)r;
|
2011-07-25 08:24:13 +00:00
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2019-04-15 04:30:49 +00:00
|
|
|
int qse_awk_rtx_valtoflt (qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_awk_flt_t* r)
|
2011-07-25 08:24:13 +00:00
|
|
|
{
|
|
|
|
int n;
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_t l;
|
2011-07-25 08:24:13 +00:00
|
|
|
|
2019-04-15 04:30:49 +00:00
|
|
|
n = qse_awk_rtx_valtonum(rtx, v, &l, r);
|
2013-11-04 02:33:52 +00:00
|
|
|
if (n == 0) *r = (qse_awk_flt_t)l;
|
2011-07-25 08:24:13 +00:00
|
|
|
else if (n == 1) n = 0;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2019-04-15 04:30:49 +00:00
|
|
|
/* ========================================================================== */
|
|
|
|
#undef awk_rtx_strtonum
|
|
|
|
#undef awk_strxtoint
|
|
|
|
#undef awk_strxtoflt
|
|
|
|
#undef char_t
|
|
|
|
#undef AWK_ISDIGIT
|
|
|
|
#undef _T
|
|
|
|
|
|
|
|
#define awk_rtx_strtonum qse_awk_rtx_mbstonum
|
|
|
|
#define awk_strxtoint qse_awk_mbsxtoint
|
|
|
|
#define awk_strxtoflt qse_awk_mbsxtoflt
|
|
|
|
#define char_t qse_mchar_t
|
|
|
|
#define AWK_ISDIGIT QSE_AWK_ISMDIGIT
|
|
|
|
#define _T QSE_MT
|
|
|
|
#include "val-imp.h"
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
#undef awk_rtx_strtonum
|
|
|
|
#undef awk_strxtoint
|
|
|
|
#undef awk_strxtoflt
|
|
|
|
#undef char_t
|
|
|
|
#undef AWK_ISDIGIT
|
|
|
|
#undef _T
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#define awk_rtx_strtonum qse_awk_rtx_wcstonum
|
|
|
|
#define awk_strxtoint qse_awk_wcsxtoint
|
|
|
|
#define awk_strxtoflt qse_awk_wcsxtoflt
|
|
|
|
#define char_t qse_wchar_t
|
|
|
|
#define AWK_ISDIGIT QSE_AWK_ISWDIGIT
|
|
|
|
#define _T QSE_WT
|
|
|
|
#include "val-imp.h"
|
|
|
|
|
|
|
|
#undef awk_rtx_strtonum
|
|
|
|
#undef awk_strxtoint
|
|
|
|
#undef awk_strxtoflt
|
|
|
|
#undef char_t
|
|
|
|
#undef AWK_ISDIGIT
|
|
|
|
#undef _T
|
|
|
|
/* ========================================================================== */
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-03-05 08:21:01 +00:00
|
|
|
static QSE_INLINE qse_awk_uint_t hash (qse_uint8_t* ptr, qse_size_t len)
|
2012-09-10 09:55:22 +00:00
|
|
|
{
|
2019-03-05 08:21:01 +00:00
|
|
|
qse_awk_uint_t h;
|
|
|
|
QSE_HASH_BYTES (h, ptr, len);
|
2019-03-01 09:27:03 +00:00
|
|
|
return h;
|
2012-09-10 09:55:22 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_t qse_awk_rtx_hashval (qse_awk_rtx_t* rtx, qse_awk_val_t* v)
|
2012-09-10 09:55:22 +00:00
|
|
|
{
|
2014-10-23 09:30:22 +00:00
|
|
|
qse_awk_val_type_t vtype = QSE_AWK_RTX_GETVALTYPE (rtx, v);
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_t hv;
|
2012-09-10 09:55:22 +00:00
|
|
|
|
2014-10-22 17:05:45 +00:00
|
|
|
switch (vtype)
|
2012-09-10 09:55:22 +00:00
|
|
|
{
|
|
|
|
case QSE_AWK_VAL_NIL:
|
|
|
|
hv = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_AWK_VAL_INT:
|
2014-10-22 17:05:45 +00:00
|
|
|
{
|
2019-04-15 04:30:49 +00:00
|
|
|
qse_awk_int_t tmp = QSE_AWK_RTX_GETINTFROMVAL(rtx, v);
|
2012-09-10 09:55:22 +00:00
|
|
|
/*hv = ((qse_awk_val_int_t*)v)->val;*/
|
2019-03-05 08:21:01 +00:00
|
|
|
hv = (qse_awk_int_t)hash((qse_uint8_t*)&tmp, QSE_SIZEOF(tmp));
|
2012-09-10 09:55:22 +00:00
|
|
|
break;
|
2014-10-22 17:05:45 +00:00
|
|
|
}
|
2012-09-10 09:55:22 +00:00
|
|
|
|
|
|
|
case QSE_AWK_VAL_FLT:
|
2019-04-15 04:30:49 +00:00
|
|
|
{
|
|
|
|
qse_awk_val_flt_t* dv = (qse_awk_val_flt_t*)v;
|
|
|
|
hv = (qse_awk_int_t)hash((qse_uint8_t*)&dv->val, QSE_SIZEOF(dv->val));
|
2012-09-10 09:55:22 +00:00
|
|
|
break;
|
2019-04-15 04:30:49 +00:00
|
|
|
}
|
2012-09-10 09:55:22 +00:00
|
|
|
|
|
|
|
case QSE_AWK_VAL_STR:
|
2019-04-15 04:30:49 +00:00
|
|
|
{
|
|
|
|
qse_awk_val_str_t* dv = (qse_awk_val_str_t*)v;
|
|
|
|
hv = (qse_awk_int_t)hash((qse_uint8_t*)dv->val.ptr, dv->val.len * QSE_SIZEOF(*dv->val.ptr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-18 08:42:54 +00:00
|
|
|
case QSE_AWK_VAL_MBS:
|
2019-04-15 04:30:49 +00:00
|
|
|
{
|
2019-04-18 08:42:54 +00:00
|
|
|
qse_awk_val_mbs_t* dv = (qse_awk_val_mbs_t*)v;
|
2019-04-15 04:30:49 +00:00
|
|
|
hv = (qse_awk_int_t)hash((qse_uint8_t*)dv->val.ptr, dv->val.len * QSE_SIZEOF(*dv->val.ptr));
|
2012-09-10 09:55:22 +00:00
|
|
|
break;
|
2019-04-15 04:30:49 +00:00
|
|
|
}
|
2012-09-10 09:55:22 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
#ifdef DEBUG_VAL
|
2013-10-28 09:40:25 +00:00
|
|
|
qse_errputstrf (
|
2012-09-10 09:55:22 +00:00
|
|
|
QSE_T(">>WRONG VALUE TYPE [%d] in qse_awk_rtx_hashval()\n"),
|
|
|
|
v->type
|
|
|
|
);
|
|
|
|
#endif
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EHASHVAL, QSE_NULL);
|
2012-09-10 09:55:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-09-11 16:19:52 +00:00
|
|
|
/* turn off the sign bit */
|
2013-11-05 14:16:45 +00:00
|
|
|
return hv & ~(((qse_awk_uint_t)1) << ((QSE_SIZEOF(qse_awk_uint_t) * 8) - 1));
|
2012-09-10 09:55:22 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 14:01:41 +00:00
|
|
|
int qse_awk_rtx_setrefval (qse_awk_rtx_t* rtx, qse_awk_val_ref_t* ref, qse_awk_val_t* val)
|
|
|
|
{
|
2014-10-23 09:30:22 +00:00
|
|
|
qse_awk_val_type_t vtype = QSE_AWK_RTX_GETVALTYPE (rtx, val);
|
2014-10-22 17:05:45 +00:00
|
|
|
|
|
|
|
if (vtype == QSE_AWK_VAL_REX || vtype == QSE_AWK_VAL_REF)
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
|
|
|
/* though it is possible that an intrinsic function handler
|
|
|
|
* can accept a regular expression withtout evaluation when 'x'
|
|
|
|
* is specified for the parameter, this function doesn't allow
|
|
|
|
* regular expression to be set to a reference variable to
|
|
|
|
* avoid potential chaos. the nature of performing '/rex/ ~ $0'
|
2014-10-22 17:05:45 +00:00
|
|
|
* for a regular expression without the match operator
|
2012-12-22 14:01:41 +00:00
|
|
|
* makes it difficult to be implemented. */
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-16 10:09:24 +00:00
|
|
|
switch (ref->id)
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
case QSE_AWK_VAL_REF_POS:
|
2012-12-22 14:01:41 +00:00
|
|
|
{
|
2014-10-22 17:05:45 +00:00
|
|
|
switch (vtype)
|
2013-04-16 10:09:24 +00:00
|
|
|
{
|
|
|
|
case QSE_AWK_VAL_MAP:
|
|
|
|
/* a map is assigned to a positional. this is disallowed. */
|
2013-04-19 15:06:52 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EMAPTOPOS, QSE_NULL);
|
2014-10-22 17:05:45 +00:00
|
|
|
return -1;
|
2013-04-16 10:09:24 +00:00
|
|
|
|
|
|
|
case QSE_AWK_VAL_STR:
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
/* handle this separately from the default case
|
|
|
|
* for no duplication. jumping to the default case
|
|
|
|
* and callingqse_awk_rtx_valtostrdup() would also work, anyway. */
|
|
|
|
qse_awk_rtx_refupval (rtx, val);
|
2019-04-15 08:43:57 +00:00
|
|
|
x = qse_awk_rtx_setrec(rtx, (qse_size_t)ref->adr, &((qse_awk_val_str_t*)val)->val);
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_awk_rtx_refdownval (rtx, val);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2019-04-18 08:42:54 +00:00
|
|
|
case QSE_AWK_VAL_MBS:
|
2019-04-15 08:43:57 +00:00
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
{
|
|
|
|
/* same as str in the mchar mode */
|
|
|
|
int x;
|
|
|
|
qse_awk_rtx_refupval (rtx, val);
|
2019-04-18 08:42:54 +00:00
|
|
|
x = qse_awk_rtx_setrec(rtx, (qse_size_t)ref->adr, &((qse_awk_val_mbs_t*)val)->val);
|
2019-04-15 08:43:57 +00:00
|
|
|
qse_awk_rtx_refdownval (rtx, val);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
#endif
|
2019-04-15 15:36:23 +00:00
|
|
|
/* fall thru otherwise */
|
2019-04-15 04:30:49 +00:00
|
|
|
|
2013-04-16 10:09:24 +00:00
|
|
|
default:
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t str;
|
2013-04-16 10:09:24 +00:00
|
|
|
int x;
|
2019-04-15 04:30:49 +00:00
|
|
|
|
|
|
|
str.ptr = qse_awk_rtx_valtostrdup(rtx, val, &str.len);
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_awk_rtx_refupval (rtx, val);
|
2019-04-15 04:30:49 +00:00
|
|
|
x = qse_awk_rtx_setrec(rtx, (qse_size_t)ref->adr, &str);
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_awk_rtx_refdownval (rtx, val);
|
|
|
|
QSE_AWK_FREE (rtx->awk, str.ptr);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
|
|
|
|
2013-04-16 10:09:24 +00:00
|
|
|
case QSE_AWK_VAL_REF_GBL:
|
2019-04-15 04:30:49 +00:00
|
|
|
return qse_awk_rtx_setgbl(rtx, (int)ref->adr, val);
|
2013-04-16 10:09:24 +00:00
|
|
|
|
|
|
|
case QSE_AWK_VAL_REF_NAMEDIDX:
|
|
|
|
case QSE_AWK_VAL_REF_GBLIDX:
|
|
|
|
case QSE_AWK_VAL_REF_LCLIDX:
|
|
|
|
case QSE_AWK_VAL_REF_ARGIDX:
|
2014-10-22 17:05:45 +00:00
|
|
|
if (vtype == QSE_AWK_VAL_MAP)
|
2013-04-16 10:09:24 +00:00
|
|
|
{
|
|
|
|
/* an indexed variable cannot be assigned a map.
|
|
|
|
* in other cases, it falls down to the default case. */
|
2013-04-19 15:06:52 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EMAPTOIDX, QSE_NULL);
|
2014-10-22 17:05:45 +00:00
|
|
|
return -1;
|
2013-04-16 10:09:24 +00:00
|
|
|
}
|
|
|
|
/* fall through */
|
2012-12-22 14:01:41 +00:00
|
|
|
|
2013-04-16 10:09:24 +00:00
|
|
|
default:
|
2013-01-07 08:33:48 +00:00
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_awk_val_t** rref;
|
2014-10-22 17:05:45 +00:00
|
|
|
qse_awk_val_type_t rref_vtype;
|
|
|
|
|
|
|
|
rref = (qse_awk_val_t**)ref->adr; /* old value pointer */
|
2014-10-23 09:30:22 +00:00
|
|
|
rref_vtype = QSE_AWK_RTX_GETVALTYPE (rtx, *rref); /* old value type */
|
2014-10-22 17:05:45 +00:00
|
|
|
if (vtype == QSE_AWK_VAL_MAP)
|
2013-04-16 10:09:24 +00:00
|
|
|
{
|
|
|
|
/* new value: map, old value: nil or map => ok */
|
2014-10-22 17:05:45 +00:00
|
|
|
if (rref_vtype != QSE_AWK_VAL_NIL && rref_vtype != QSE_AWK_VAL_MAP)
|
2013-04-16 10:09:24 +00:00
|
|
|
{
|
|
|
|
if (!(rtx->awk->opt.trait & QSE_AWK_FLEXMAP))
|
|
|
|
{
|
|
|
|
/* cannot change a scalar value to a map */
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ESCALARTOMAP, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* new value: scalar, old value: nil or scalar => ok */
|
2014-10-22 17:05:45 +00:00
|
|
|
if (rref_vtype == QSE_AWK_VAL_MAP)
|
2013-04-16 10:09:24 +00:00
|
|
|
{
|
|
|
|
if (!(rtx->awk->opt.trait & QSE_AWK_FLEXMAP))
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EMAPTOSCALAR, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*rref != val)
|
|
|
|
{
|
|
|
|
/* if the new value is not the same as the old value */
|
|
|
|
qse_awk_rtx_refdownval (rtx, *rref);
|
|
|
|
*rref = val;
|
|
|
|
qse_awk_rtx_refupval (rtx, *rref);
|
|
|
|
}
|
|
|
|
return 0;
|
2013-01-07 08:33:48 +00:00
|
|
|
}
|
2012-12-22 14:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-11 04:19:59 +00:00
|
|
|
#if 0
|
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
#define qse_errputstrf qse_errputstrf
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
static qse_htb_walk_t print_pair (
|
|
|
|
qse_htb_t* map, qse_htb_pair_t* pair, void* arg)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-01-31 04:31:40 +00:00
|
|
|
qse_awk_rtx_t* run = (qse_awk_rtx_t*)arg;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-02-17 02:11:31 +00:00
|
|
|
QSE_ASSERT (run == *(qse_awk_rtx_t**)QSE_XTN(map));
|
2008-09-25 02:00:50 +00:00
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T(" %.*s=>"),
|
2010-07-09 00:58:44 +00:00
|
|
|
(int)QSE_HTB_KLEN(pair), QSE_HTB_KPTR(pair));
|
|
|
|
qse_awk_dprintval ((qse_awk_rtx_t*)arg, QSE_HTB_VPTR(pair));
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T(" "));
|
2008-03-23 06:09:35 +00:00
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
return QSE_HTB_WALK_FORWARD;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-01-31 04:31:40 +00:00
|
|
|
void qse_awk_dprintval (qse_awk_rtx_t* run, qse_awk_val_t* val)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* TODO: better value printing ... */
|
|
|
|
|
|
|
|
switch (val->type)
|
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_AWK_VAL_NIL:
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T("nil"));
|
2007-05-02 01:07:00 +00:00
|
|
|
break;
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_AWK_VAL_INT:
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T("%jd"),
|
|
|
|
(qse_intmax_t)((qse_awk_val_int_t*)val)->val);
|
2007-05-02 01:07:00 +00:00
|
|
|
break;
|
|
|
|
|
2011-11-22 05:03:31 +00:00
|
|
|
case QSE_AWK_VAL_FLT:
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T("%jf"),
|
|
|
|
(qse_fltmax_t)((qse_awk_val_flt_t*)val)->val);
|
2007-05-02 01:07:00 +00:00
|
|
|
break;
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_AWK_VAL_STR:
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T("%s"), ((qse_awk_val_str_t*)val)->ptr);
|
2007-05-02 01:07:00 +00:00
|
|
|
break;
|
|
|
|
|
2019-04-18 08:42:54 +00:00
|
|
|
case QSE_AWK_VAL_MBS:
|
|
|
|
qse_errputstrf (QSE_T("%hs"), ((qse_awk_val_mbs_t*)val)->ptr);
|
|
|
|
break;
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_AWK_VAL_REX:
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T("REX[%s]"), ((qse_awk_val_rex_t*)val)->ptr);
|
2007-05-02 01:07:00 +00:00
|
|
|
break;
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_AWK_VAL_MAP:
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T("MAP["));
|
2010-07-09 00:58:44 +00:00
|
|
|
qse_htb_walk (((qse_awk_val_map_t*)val)->map, print_pair, run);
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T("]"));
|
2007-05-02 01:07:00 +00:00
|
|
|
break;
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_AWK_VAL_REF:
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T("REF[id=%d,val="), ((qse_awk_val_ref_t*)val)->id);
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_dprintval (run, *((qse_awk_val_ref_t*)val)->adr);
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T("]"));
|
2007-05-02 01:07:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_errputstrf (QSE_T("**** INTERNAL ERROR - INVALID VALUE TYPE ****\n"));
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
2008-03-23 00:47:27 +00:00
|
|
|
|
2008-12-11 04:19:59 +00:00
|
|
|
#endif
|