qse/qse/lib/awk/val.c

1264 lines
29 KiB
C
Raw Normal View History

/*
* $Id: val.c 480 2011-05-25 14:00:19Z hyunghwan.chung $
*
2011-04-23 08:28:43 +00:00
Copyright 2006-2011 Chung, Hyung-Hwan.
2009-09-16 04:01:02 +00:00
This file is part of QSE.
2008-12-27 04:35:14 +00:00
2009-09-16 04:01:02 +00:00
QSE is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
2008-12-27 04:35:14 +00:00
2009-09-16 04:01:02 +00:00
QSE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
2008-12-27 04:35:14 +00:00
2009-09-16 04:01:02 +00:00
You should have received a copy of the GNU Lesser General Public
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
*/
2008-08-21 03:17:25 +00:00
#include "awk.h"
#ifdef DEBUG_VAL
2009-06-04 15:50:32 +00:00
#include <qse/cmn/stdio.h>
#endif
#define CHUNKSIZE QSE_AWK_VAL_CHUNK_SIZE
2008-03-23 00:47:27 +00:00
static qse_awk_val_nil_t awk_nil = { QSE_AWK_VAL_NIL, 0, 0 };
2011-05-19 08:36:40 +00:00
static qse_awk_val_str_t awk_zls = { QSE_AWK_VAL_STR, 0, 0, { QSE_T(""), 0 } };
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;
2008-12-21 21:35:07 +00:00
static qse_awk_val_int_t awk_int[] =
{
{ QSE_AWK_VAL_INT, 0, 0, -1, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 0, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 1, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 2, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 3, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 4, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 5, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 6, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 7, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 8, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 9, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 10, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 11, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 12, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 13, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 14, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 15, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 16, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 17, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 18, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 19, QSE_NULL },
{ QSE_AWK_VAL_INT, 0, 0, 20, QSE_NULL }
};
2008-12-21 21:35:07 +00:00
qse_awk_val_t* qse_awk_val_negone = (qse_awk_val_t*)&awk_int[0];
qse_awk_val_t* qse_awk_val_zero = (qse_awk_val_t*)&awk_int[1];
qse_awk_val_t* qse_awk_val_one = (qse_awk_val_t*)&awk_int[2];
2009-08-21 05:28:03 +00:00
qse_awk_val_t* qse_awk_rtx_makeintval (qse_awk_rtx_t* rtx, qse_long_t v)
{
2008-12-21 21:35:07 +00:00
qse_awk_val_int_t* val;
if (v >= awk_int[0].val &&
2008-12-21 21:35:07 +00:00
v <= awk_int[QSE_COUNTOF(awk_int)-1].val)
{
2008-12-21 21:35:07 +00:00
return (qse_awk_val_t*)&awk_int[v-awk_int[0].val];
}
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;
2008-12-21 21:35:07 +00:00
val->type = QSE_AWK_VAL_INT;
val->ref = 0;
val->nstr = 0;
val->val = v;
2008-12-21 21:35:07 +00:00
val->nde = QSE_NULL;
#ifdef DEBUG_VAL
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("makeintval => %ld [%p]\n"), (long)v, val);
#endif
2008-12-21 21:35:07 +00:00
return (qse_awk_val_t*)val;
}
2009-08-21 05:28:03 +00:00
qse_awk_val_t* qse_awk_rtx_makerealval (qse_awk_rtx_t* rtx, qse_real_t v)
{
2008-12-21 21:35:07 +00:00
qse_awk_val_real_t* val;
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;
2009-01-18 01:48:21 +00:00
/*qse_awk_val_real_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)+
QSE_SIZEOF(qse_awk_val_real_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
/*
2008-12-21 21:35:07 +00:00
x = (qse_awk_val_real_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_real_t*)&x[i+1];
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++)
2008-12-21 21:35:07 +00:00
c->slot[i].nde = (qse_awk_nde_real_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.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;
rtx->vmgr.rfree = (qse_awk_val_real_t*)val->nde;
2008-12-21 21:35:07 +00:00
val->type = QSE_AWK_VAL_REAL;
val->ref = 0;
val->nstr = 0;
val->val = v;
2008-12-21 21:35:07 +00:00
val->nde = QSE_NULL;
#ifdef DEBUG_VAL
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("makerealval => %Lf [%p]\n"), (double)v, val);
#endif
2008-12-21 21:35:07 +00:00
return (qse_awk_val_t*)val;
}
2009-02-24 08:03:28 +00:00
qse_awk_val_t* qse_awk_rtx_makestrval0 (
2009-08-21 05:28:03 +00:00
qse_awk_rtx_t* rtx, const qse_char_t* str)
{
2009-08-21 05:28:03 +00:00
return qse_awk_rtx_makestrval (rtx, str, qse_strlen(str));
}
qse_awk_val_t* qse_awk_rtx_makestrval (
2009-08-21 05:28:03 +00:00
qse_awk_rtx_t* rtx, const qse_char_t* str, qse_size_t len)
{
2009-09-19 22:28:49 +00:00
qse_awk_val_str_t* val = QSE_NULL;
2008-12-21 21:35:07 +00:00
qse_size_t rlen = len;
2009-09-19 22:28:49 +00:00
#ifdef ENABLE_FEATURE_SCACHE
qse_size_t 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
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)
{
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;
}
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
2008-12-21 21:35:07 +00:00
val->type = QSE_AWK_VAL_STR;
val->ref = 0;
val->nstr = 0;
2011-05-19 08:36:40 +00:00
val->val.len = len;
val->val.ptr = (qse_char_t*)(val + 1);
qse_strncpy (val->val.ptr, str, len);
#ifdef DEBUG_VAL
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("makestrval => %p\n"), val);
#endif
2008-12-21 21:35:07 +00:00
return (qse_awk_val_t*)val;
}
qse_awk_val_t* qse_awk_rtx_makestrval_nodup (
2009-08-21 05:28:03 +00:00
qse_awk_rtx_t* rtx, qse_char_t* str, qse_size_t len)
{
2008-12-21 21:35:07 +00:00
qse_awk_val_str_t* val;
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, QSE_SIZEOF(qse_awk_val_str_t));
2008-12-21 21:35:07 +00:00
if (val == QSE_NULL)
{
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-12-21 21:35:07 +00:00
val->type = QSE_AWK_VAL_STR;
val->ref = 0;
val->nstr = 0;
2011-05-19 08:36:40 +00:00
val->val.len = len;
val->val.ptr = str;
2008-12-21 21:35:07 +00:00
return (qse_awk_val_t*)val;
}
qse_awk_val_t* qse_awk_rtx_makestrval2 (
2009-08-21 05:28:03 +00:00
qse_awk_rtx_t* rtx,
2008-12-21 21:35:07 +00:00
const qse_char_t* str1, qse_size_t len1,
const qse_char_t* str2, qse_size_t len2)
{
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
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)
{
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;
}
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
2008-12-21 21:35:07 +00:00
val->type = QSE_AWK_VAL_STR;
val->ref = 0;
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);
#ifdef DEBUG_VAL
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("makestrval2 => %p\n"), val);
#endif
2008-12-21 21:35:07 +00:00
return (qse_awk_val_t*)val;
}
2009-06-21 06:47:34 +00:00
qse_awk_val_t* qse_awk_rtx_makenstrval (
2009-08-21 05:28:03 +00:00
qse_awk_rtx_t* rtx, const qse_char_t* str, qse_size_t len)
2009-06-21 06:47:34 +00:00
{
int x;
qse_awk_val_t* v;
qse_long_t l;
qse_real_t r;
2009-08-21 05:28:03 +00:00
x = qse_awk_rtx_strtonum (rtx, 1, str, len, &l, &r);
v = qse_awk_rtx_makestrval (rtx, str, len);
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;
}
qse_awk_val_t* qse_awk_rtx_makerexval (
2009-08-21 05:28:03 +00:00
qse_awk_rtx_t* rtx, const qse_char_t* buf, qse_size_t len, void* code)
{
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.
*/
totsz = QSE_SIZEOF(*val) + (QSE_SIZEOF(*buf) * (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;
}
2008-12-21 21:35:07 +00:00
val->type = QSE_AWK_VAL_REX;
val->ref = 0;
val->nstr = 0;
val->len = len;
2009-02-17 02:11:31 +00:00
val->ptr = (qse_char_t*)(val + 1);
qse_strncpy (val->ptr, buf, len);
val->code = code;
2008-02-29 00:50:10 +00:00
2008-12-21 21:35:07 +00:00
return (qse_awk_val_t*)val;
}
static void free_mapval (qse_htb_t* map, void* dptr, qse_size_t dlen)
{
2009-09-19 22:28:49 +00:00
qse_awk_rtx_t* rtx = *(qse_awk_rtx_t**)QSE_XTN(map);
#ifdef DEBUG_VAL
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("refdown in map free..."));
2009-09-19 22:28:49 +00:00
qse_awk_dprintval (rtx, dptr);
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("\n"));
#endif
2009-09-19 22:28:49 +00:00
qse_awk_rtx_refdownval (rtx, dptr);
}
static void same_mapval (qse_htb_t* map, void* dptr, qse_size_t dlen)
{
2009-02-17 02:11:31 +00:00
qse_awk_rtx_t* run = *(qse_awk_rtx_t**)QSE_XTN(map);
#ifdef DEBUG_VAL
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("refdown nofree in map free..."));
qse_awk_dprintval (run, dptr);
qse_dprintf (QSE_T("\n"));
#endif
qse_awk_rtx_refdownval_nofree (run, dptr);
}
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)
{
static qse_htb_mancbs_t mancbs =
{
/* 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,
QSE_HTB_SIZER_DEFAULT,
QSE_HTB_HASHER_DEFAULT
};
qse_awk_val_map_t* val;
/* 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)
{
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;
}
2008-12-21 21:35:07 +00:00
val->type = QSE_AWK_VAL_MAP;
val->ref = 0;
val->nstr = 0;
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)
{
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;
}
*/
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) +
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)
{
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-12-21 21:35:07 +00:00
val->type = QSE_AWK_VAL_MAP;
val->ref = 0;
val->nstr = 0;
val->map = (qse_htb_t*)(val + 1);
val->map = qse_htb_init (
val->map, rtx->awk->mmgr, 256, 70, QSE_SIZEOF(qse_char_t), 1
);
2008-12-21 21:35:07 +00:00
if (val->map == QSE_NULL)
{
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;
}
2009-08-21 05:28:03 +00:00
*(qse_awk_rtx_t**)QSE_XTN(val->map) = rtx;
qse_htb_setmancbs (val->map, &mancbs);
/* END CHECK */
2008-12-21 21:35:07 +00:00
return (qse_awk_val_t*)val;
}
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)
{
QSE_ASSERT (map->type == QSE_AWK_VAL_MAP);
#if 0
if (map->type != QSE_AWK_VAL_MAP)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOTIDX, QSE_NULL);
return QSE_NULL;
}
#endif
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)
{
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)
{
qse_htb_pair_t* pair;
QSE_ASSERT (map->type == QSE_AWK_VAL_MAP);
#if 0
if (map->type != QSE_AWK_VAL_MAP)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOTIDX, QSE_NULL);
return QSE_NULL;
}
#endif
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);
}
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)
{
QSE_ASSERT (map->type == QSE_AWK_VAL_MAP);
itr->pair = qse_htb_getfirstpair (
((qse_awk_val_map_t*)map)->map, &itr->buckno);
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)
{
QSE_ASSERT (map->type == QSE_AWK_VAL_MAP);
itr->pair = qse_htb_getnextpair (
((qse_awk_val_map_t*)map)->map, itr->pair, &itr->buckno);
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)
{
2008-12-21 21:35:07 +00:00
qse_awk_val_ref_t* val;
2009-08-21 05:28:03 +00:00
if (rtx->fcache_count > 0)
{
2009-08-21 05:28:03 +00:00
val = rtx->fcache[--rtx->fcache_count];
}
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)
{
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-12-21 21:35:07 +00:00
val->type = QSE_AWK_VAL_REF;
val->ref = 0;
val->nstr = 0;
val->id = id;
val->adr = adr;
2008-12-21 21:35:07 +00:00
return (qse_awk_val_t*)val;
}
2008-01-16 23:08:17 +00:00
#define IS_STATICVAL(val) \
2008-12-21 21:35:07 +00:00
((val) == QSE_NULL || \
(val) == qse_awk_val_nil || \
(val) == qse_awk_val_zls || \
(val) == qse_awk_val_zero || \
(val) == qse_awk_val_one || \
((val) >= (qse_awk_val_t*)&awk_int[0] && \
(val) <= (qse_awk_val_t*)&awk_int[QSE_COUNTOF(awk_int)-1]))
2009-02-17 02:11:31 +00:00
qse_bool_t qse_awk_rtx_isstaticval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
{
2008-01-16 23:08:17 +00:00
return IS_STATICVAL(val);
}
2009-09-19 22:28:49 +00:00
void qse_awk_rtx_freeval (
qse_awk_rtx_t* rtx, qse_awk_val_t* val, qse_bool_t cache)
{
2008-01-16 23:08:17 +00:00
if (IS_STATICVAL(val)) return;
#ifdef DEBUG_VAL
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("freeing [cache=%d] ... "), cache);
qse_awk_dprintval (rtx, val);
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("\n"));
#endif
2008-12-21 21:35:07 +00:00
if (val->type == QSE_AWK_VAL_NIL)
{
QSE_AWK_FREE (rtx->awk, val);
}
2008-12-21 21:35:07 +00:00
else if (val->type == QSE_AWK_VAL_INT)
{
2009-09-19 22:28:49 +00:00
((qse_awk_val_int_t*)val)->nde =
(qse_awk_nde_int_t*)rtx->vmgr.ifree;
rtx->vmgr.ifree = (qse_awk_val_int_t*)val;
}
2008-12-21 21:35:07 +00:00
else if (val->type == QSE_AWK_VAL_REAL)
{
2009-09-19 22:28:49 +00:00
((qse_awk_val_real_t*)val)->nde =
(qse_awk_nde_real_t*)rtx->vmgr.rfree;
rtx->vmgr.rfree = (qse_awk_val_real_t*)val;
}
2008-12-21 21:35:07 +00:00
else if (val->type == QSE_AWK_VAL_STR)
{
2009-09-19 22:28:49 +00:00
#ifdef ENABLE_FEATURE_SCACHE
2008-01-16 03:01:47 +00:00
if (cache)
{
2008-12-21 21:35:07 +00:00
qse_awk_val_str_t* v = (qse_awk_val_str_t*)val;
2009-09-19 22:28:49 +00:00
int i;
2011-05-19 08:36:40 +00:00
i = v->val.len / FEATURE_SCACHE_BLOCK_UNIT;
2009-09-19 22:28:49 +00:00
if (i < QSE_COUNTOF(rtx->scache_count) &&
rtx->scache_count[i] < QSE_COUNTOF(rtx->scache[i]))
2008-01-16 03:01:47 +00:00
{
2009-09-19 22:28:49 +00:00
rtx->scache[i][rtx->scache_count[i]++] = v;
v->nstr = 0;
2008-01-16 03:01:47 +00:00
}
else QSE_AWK_FREE (rtx->awk, val);
2008-01-16 03:01:47 +00:00
}
2009-09-19 22:28:49 +00:00
else
#endif
QSE_AWK_FREE (rtx->awk, val);
}
2008-12-21 21:35:07 +00:00
else if (val->type == 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);
*/
QSE_AWK_FREE (rtx->awk, val);
}
2008-12-21 21:35:07 +00:00
else if (val->type == QSE_AWK_VAL_MAP)
{
qse_htb_fini (((qse_awk_val_map_t*)val)->map);
QSE_AWK_FREE (rtx->awk, val);
}
2008-12-21 21:35:07 +00:00
else if (val->type == QSE_AWK_VAL_REF)
{
if (cache && rtx->fcache_count < QSE_COUNTOF(rtx->fcache))
{
rtx->fcache[rtx->fcache_count++] =
2008-12-21 21:35:07 +00:00
(qse_awk_val_ref_t*)val;
}
else QSE_AWK_FREE (rtx->awk, val);
}
else
{
2008-12-21 21:35:07 +00:00
QSE_ASSERTX (
!"should never happen - invalid value type",
2008-12-21 21:35:07 +00:00
"the type of a value should be one of QSE_AWK_VAL_XXX's defined in awk.h");
}
}
void qse_awk_rtx_refupval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
{
2008-01-16 23:08:17 +00:00
if (IS_STATICVAL(val)) return;
#ifdef DEBUG_VAL
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("ref up [ptr=%p] [count=%d] "), val, (int)val->ref);
qse_awk_dprintval (rtx, val);
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("\n"));
#endif
val->ref++;
}
void qse_awk_rtx_refdownval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
{
2008-01-16 23:08:17 +00:00
if (IS_STATICVAL(val)) return;
#ifdef DEBUG_VAL
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("ref down [ptr=%p] [count=%d]\n"), val, (int)val->ref);
qse_awk_dprintval (rtx, val);
2008-12-21 21:35:07 +00:00
qse_dprintf (QSE_T("\n"));
#endif
2008-12-21 21:35:07 +00:00
QSE_ASSERTX (val->ref > 0,
2009-08-21 05:28:03 +00:00
"the reference count of a value should be greater than zero for it"
" to be decremented. check the source code for any bugs");
val->ref--;
if (val->ref <= 0)
{
qse_awk_rtx_freeval(rtx, val, QSE_TRUE);
}
}
void qse_awk_rtx_refdownval_nofree (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
{
2008-01-16 23:08:17 +00:00
if (IS_STATICVAL(val)) return;
2008-12-21 21:35:07 +00:00
QSE_ASSERTX (val->ref > 0,
2009-08-21 05:28:03 +00:00
"the reference count of a value should be greater than zero for it"
" to be decremented. check the source code for any bugs");
val->ref--;
}
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)
2008-03-23 00:47:27 +00:00
{
2008-12-21 21:35:07 +00:00
qse_awk_val_chunk_t* next = chunk->next;
QSE_AWK_FREE (rtx->awk, chunk);
2008-03-23 00:47:27 +00:00
chunk = next;
}
}
2011-05-25 09:14:58 +00:00
qse_bool_t qse_awk_rtx_valtobool (qse_awk_rtx_t* run, const qse_awk_val_t* val)
{
2008-12-21 21:35:07 +00:00
if (val == QSE_NULL) return QSE_FALSE;
switch (val->type)
{
2008-12-21 21:35:07 +00:00
case QSE_AWK_VAL_NIL:
return QSE_FALSE;
case QSE_AWK_VAL_INT:
return ((qse_awk_val_int_t*)val)->val != 0;
case QSE_AWK_VAL_REAL:
return ((qse_awk_val_real_t*)val)->val != 0.0;
case QSE_AWK_VAL_STR:
2011-05-19 08:36:40 +00:00
return ((qse_awk_val_str_t*)val)->val.len > 0;
2008-12-21 21:35:07 +00:00
case QSE_AWK_VAL_REX: /* TODO: is this correct? */
return ((qse_awk_val_rex_t*)val)->len > 0;
case QSE_AWK_VAL_MAP:
return QSE_FALSE; /* TODO: is this correct? */
case QSE_AWK_VAL_REF:
return QSE_FALSE; /* TODO: is this correct? */
}
2008-12-21 21:35:07 +00:00
QSE_ASSERTX (
!"should never happen - invalid value type",
2008-12-21 21:35:07 +00:00
"the type of a value should be one of QSE_AWK_VAL_XXX's defined in awk.h");
return QSE_FALSE;
}
static int str_to_str (
2009-02-28 20:18:00 +00:00
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
{
int type = out->type & ~QSE_AWK_RTX_VALTOSTR_PRINT;
switch (type)
2009-02-28 20:18:00 +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;
out->u.cpl.ptr = str;
return 0;
2011-05-25 09:14:58 +00:00
}
case QSE_AWK_RTX_VALTOSTR_CPLCPY:
2011-05-25 09:14:58 +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);
out->u.cplcpy.len = str_len + 1;
return -1;
2009-02-28 20:18:00 +00:00
}
out->u.cplcpy.len =
qse_strncpy (out->u.cplcpy.ptr, str, str_len);
return 0;
2009-02-28 20:18:00 +00:00
}
case QSE_AWK_RTX_VALTOSTR_CPLDUP:
2009-02-28 20:18:00 +00:00
{
qse_char_t* tmp;
2009-02-28 20:18:00 +00:00
tmp = QSE_AWK_STRXDUP (rtx->awk, str, str_len);
if (tmp == QSE_NULL)
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);
return -1;
2009-02-28 20:18:00 +00:00
}
out->u.cpldup.ptr = tmp;
out->u.cpldup.len = str_len;
return 0;
2009-02-28 20:18:00 +00:00
}
case QSE_AWK_RTX_VALTOSTR_STRP:
2009-02-28 20:18:00 +00:00
{
qse_size_t n;
qse_str_clear (out->u.strp);
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);
return -1;
2009-02-28 20:18:00 +00:00
}
return 0;
2009-02-28 20:18:00 +00:00
}
case QSE_AWK_RTX_VALTOSTR_STRPCAT:
2009-02-28 20:18:00 +00:00
{
qse_size_t n;
n = qse_str_ncat (out->u.strpcat, str, str_len);
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);
return -1;
2009-02-28 20:18:00 +00:00
}
return 0;
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);
return -1;
2009-02-28 20:18:00 +00:00
}
static int val_int_to_str (
2011-05-25 09:14:58 +00:00
qse_awk_rtx_t* rtx, const qse_awk_val_int_t* v,
qse_awk_rtx_valtostr_out_t* out)
{
2008-12-21 21:35:07 +00:00
qse_char_t* tmp;
qse_long_t t;
qse_size_t rlen = 0;
int type = out->type & ~QSE_AWK_RTX_VALTOSTR_PRINT;
t = v->val;
if (t == 0) rlen++;
else
{
/* non-zero values */
if (t < 0) { t = -t; rlen++; }
while (t > 0) { rlen++; t /= 10; }
}
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.
* 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. */
QSE_ASSERT (QSE_SIZEOF(out->u.cpl) == QSE_SIZEOF(out->u.cplcpy));
2011-05-25 09:14:58 +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);
/* store the buffer size needed */
out->u.cplcpy.len = rlen + 1;
return -1;
2008-01-16 21:46:08 +00:00
}
tmp = out->u.cplcpy.ptr;
tmp[rlen] = QSE_T('\0');
out->u.cplcpy.len = rlen;
break;
case QSE_AWK_RTX_VALTOSTR_CPLDUP:
tmp = QSE_AWK_ALLOC (
rtx->awk, (rlen + 1) * QSE_SIZEOF(qse_char_t));
if (tmp == QSE_NULL)
{
2009-08-17 02:08:58 +00:00
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return -1;
}
tmp[rlen] = QSE_T('\0');
out->u.cpldup.ptr = tmp;
out->u.cpldup.len = rlen;
break;
case QSE_AWK_RTX_VALTOSTR_STRP:
{
qse_size_t n;
qse_str_clear (out->u.strp);
QSE_ASSERT (QSE_STR_LEN(out->u.strp) == 0);
2008-01-16 21:46:08 +00:00
/* point to the beginning of the buffer */
tmp = QSE_STR_PTR(out->u.strp);
/* extend the buffer */
n = qse_str_nccat (out->u.strp, QSE_T(' '), rlen);
if (n == (qse_size_t)-1)
{
2009-08-17 02:08:58 +00:00
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return -1;
}
break;
2008-01-16 21:46:08 +00:00
}
case QSE_AWK_RTX_VALTOSTR_STRPCAT:
{
qse_size_t n;
/* point to the insertion point */
tmp = QSE_STR_PTR(out->u.strpcat) + QSE_STR_LEN(out->u.strpcat);
/* extend the buffer */
n = qse_str_nccat (out->u.strpcat, QSE_T(' '), rlen);
if (n == (qse_size_t)-1)
{
2009-08-17 02:08:58 +00:00
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return -1;
}
break;
}
}
t = v->val;
if (t == 0) tmp[0] = QSE_T('0');
else
{
if (t < 0) t = -t;
/* fill in the buffer with digits */
while (t > 0)
{
tmp[--rlen] = (qse_char_t)(t % 10) + QSE_T('0');
t /= 10;
}
/* insert the negative sign if necessary */
if (v->val < 0) tmp[--rlen] = QSE_T('-');
}
return 0;
}
static int val_real_to_str (
2011-05-25 09:14:58 +00:00
qse_awk_rtx_t* rtx, const qse_awk_val_real_t* v,
qse_awk_rtx_valtostr_out_t* out)
{
2008-12-21 21:35:07 +00:00
qse_char_t* tmp;
qse_size_t tmp_len;
qse_str_t buf, fbu;
int buf_inited = 0, fbu_inited = 0;
int type = out->type & ~QSE_AWK_RTX_VALTOSTR_PRINT;
if (out->type & QSE_AWK_RTX_VALTOSTR_PRINT)
{
tmp = rtx->gbl.ofmt.ptr;
tmp_len = rtx->gbl.ofmt.len;
}
else
{
tmp = rtx->gbl.convfmt.ptr;
tmp_len = rtx->gbl.convfmt.len;
}
if (qse_str_init (&buf, rtx->awk->mmgr, 256) == QSE_NULL)
{
2009-08-17 02:08:58 +00:00
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return -1;
}
buf_inited = 1;
if (qse_str_init (&fbu, rtx->awk->mmgr, 256) == QSE_NULL)
{
qse_str_fini (&buf);
2009-08-17 02:08:58 +00:00
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return -1;
}
fbu_inited = 1;
tmp = qse_awk_rtx_format (rtx, &buf, &fbu, tmp, tmp_len,
2008-12-21 21:35:07 +00:00
(qse_size_t)-1, (qse_awk_nde_t*)v, &tmp_len);
if (tmp == QSE_NULL) goto oops;
switch (type)
{
case QSE_AWK_RTX_VALTOSTR_CPL:
2011-05-25 09:14:58 +00:00
/* CPL and CPLCP behave the same for real_t.
* 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. */
QSE_ASSERT (QSE_SIZEOF(out->u.cpl) == QSE_SIZEOF(out->u.cplcpy));
2011-05-25 09:14:58 +00:00
case QSE_AWK_RTX_VALTOSTR_CPLCPY:
if (out->u.cplcpy.len <= tmp_len)
{
2009-08-17 02:08:58 +00:00
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
/* store the buffer size required */
out->u.cplcpy.len = tmp_len + 1;
goto oops;
}
2008-09-05 04:58:08 +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
case QSE_AWK_RTX_VALTOSTR_CPLDUP:
{
qse_str_yield (&buf, QSE_NULL, 0);
out->u.cpldup.ptr = tmp;
out->u.cpldup.len = tmp_len;
break;
}
case QSE_AWK_RTX_VALTOSTR_STRP:
{
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);
goto oops;
}
break;
}
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);
goto oops;
}
break;
}
default:
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
goto oops;
}
}
qse_str_fini (&fbu);
qse_str_fini (&buf);
return 0;
oops:
if (fbu_inited) qse_str_fini (&fbu);
if (buf_inited) qse_str_fini (&buf);
return -1;
}
int qse_awk_rtx_valtostr (
2011-05-25 09:14:58 +00:00
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
{
switch (v->type)
{
case QSE_AWK_VAL_NIL:
2011-05-25 09:14:58 +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
{
2009-02-28 20:18:00 +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
case QSE_AWK_VAL_REAL:
2011-05-25 09:14:58 +00:00
{
2009-02-28 20:18:00 +00:00
return val_real_to_str (
rtx, (qse_awk_val_real_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;
2011-05-19 08:36:40 +00:00
return str_to_str (rtx, vs->val.ptr, vs->val.len, out);
2009-02-28 20:18:00 +00:00
}
}
#ifdef DEBUG_VAL
qse_dprintf (
QSE_T("ERROR: WRONG VALUE TYPE [%d] in qse_awk_rtx_valtostr\n"),
v->type);
#endif
2009-08-17 02:08:58 +00:00
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTYPE, QSE_NULL);
return -1;
2009-02-28 20:18:00 +00:00
}
qse_char_t* qse_awk_rtx_valtocpldup (
2011-05-25 09:14:58 +00:00
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_size_t* len)
{
qse_awk_rtx_valtostr_out_t out;
out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP;
if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) return QSE_NULL;
*len = out.u.cpldup.len;
return out.u.cpldup.ptr;
}
int qse_awk_rtx_valtonum (
2011-05-25 09:14:58 +00:00
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_long_t* l, qse_real_t* r)
{
2008-12-21 21:35:07 +00:00
if (v->type == QSE_AWK_VAL_NIL)
{
*l = 0;
return 0;
}
2008-12-21 21:35:07 +00:00
if (v->type == QSE_AWK_VAL_INT)
{
2008-12-21 21:35:07 +00:00
*l = ((qse_awk_val_int_t*)v)->val;
return 0; /* long */
}
2008-12-21 21:35:07 +00:00
if (v->type == QSE_AWK_VAL_REAL)
{
2008-12-21 21:35:07 +00:00
*r = ((qse_awk_val_real_t*)v)->val;
return 1; /* real */
}
2008-12-21 21:35:07 +00:00
if (v->type == QSE_AWK_VAL_STR)
{
return qse_awk_rtx_strtonum (
rtx, 0,
2011-05-19 08:36:40 +00:00
((qse_awk_val_str_t*)v)->val.ptr,
((qse_awk_val_str_t*)v)->val.len,
l, r
);
}
#ifdef DEBUG_VAL
2008-12-21 21:35:07 +00:00
qse_dprintf (
QSE_T("ERROR: WRONG VALUE TYPE [%d] in qse_awk_rtx_valtonum\n"),
v->type);
#endif
2009-08-17 02:08:58 +00:00
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EVALTYPE, QSE_NULL);
return -1; /* error */
}
int qse_awk_rtx_strtonum (
qse_awk_rtx_t* rtx, int strict,
const qse_char_t* ptr, qse_size_t len,
2008-12-21 21:35:07 +00:00
qse_long_t* l, qse_real_t* r)
{
2008-12-21 21:35:07 +00:00
const qse_char_t* endptr;
*l = qse_awk_strxtolong (rtx->awk, ptr, len, 0, &endptr);
if (endptr < ptr + len &&
(*endptr == QSE_T('.') ||
*endptr == QSE_T('E') ||
*endptr == QSE_T('e')))
{
*r = qse_awk_strxtoreal (rtx->awk, ptr, len, &endptr);
if (strict && endptr < ptr + len) return -1;
return 1; /* real */
}
if (strict && endptr < ptr + len) return -1;
return 0; /* long */
}
2008-12-11 04:19:59 +00:00
#if 0
2008-07-21 06:42:39 +00:00
#define DPRINTF run->awk->prmfns->dprintf
2008-08-19 05:21:48 +00:00
#define DCUSTOM run->awk->prmfns->data
static qse_htb_walk_t print_pair (
qse_htb_t* map, qse_htb_pair_t* pair, void* arg)
{
qse_awk_rtx_t* run = (qse_awk_rtx_t*)arg;
2009-02-17 02:11:31 +00:00
QSE_ASSERT (run == *(qse_awk_rtx_t**)QSE_XTN(map));
2008-12-21 21:35:07 +00:00
DPRINTF (DCUSTOM, QSE_T(" %.*s=>"),
(int)QSE_HTB_KLEN(pair), QSE_HTB_KPTR(pair));
qse_awk_dprintval ((qse_awk_rtx_t*)arg, QSE_HTB_VPTR(pair));
2008-12-21 21:35:07 +00:00
DPRINTF (DCUSTOM, QSE_T(" "));
2008-03-23 06:09:35 +00:00
return QSE_HTB_WALK_FORWARD;
}
void qse_awk_dprintval (qse_awk_rtx_t* run, qse_awk_val_t* val)
{
/* TODO: better value printing ... */
switch (val->type)
{
2008-12-21 21:35:07 +00:00
case QSE_AWK_VAL_NIL:
DPRINTF (DCUSTOM, QSE_T("nil"));
break;
2008-12-21 21:35:07 +00:00
case QSE_AWK_VAL_INT:
#if QSE_SIZEOF_LONG_LONG > 0
DPRINTF (DCUSTOM, QSE_T("%lld"),
(long long)((qse_awk_val_int_t*)val)->val);
#elif QSE_SIZEOF___INT64 > 0
DPRINTF (DCUSTOM, QSE_T("%I64d"),
(__int64)((qse_awk_val_int_t*)val)->val);
#elif QSE_SIZEOF_LONG > 0
DPRINTF (DCUSTOM, QSE_T("%ld"),
(long)((qse_awk_val_int_t*)val)->val);
#elif QSE_SIZEOF_INT > 0
DPRINTF (DCUSTOM, QSE_T("%d"),
(int)((qse_awk_val_int_t*)val)->val);
#else
#error unsupported size
#endif
break;
2008-12-21 21:35:07 +00:00
case QSE_AWK_VAL_REAL:
#if defined(__MINGW32__)
2008-12-21 21:35:07 +00:00
DPRINTF (DCUSTOM, QSE_T("%Lf"),
(double)((qse_awk_val_real_t*)val)->val);
#else
2008-12-21 21:35:07 +00:00
DPRINTF (DCUSTOM, QSE_T("%Lf"),
(long double)((qse_awk_val_real_t*)val)->val);
#endif
break;
2008-12-21 21:35:07 +00:00
case QSE_AWK_VAL_STR:
DPRINTF (DCUSTOM, QSE_T("%s"), ((qse_awk_val_str_t*)val)->ptr);
break;
2008-12-21 21:35:07 +00:00
case QSE_AWK_VAL_REX:
DPRINTF (DCUSTOM, QSE_T("REX[%s]"), ((qse_awk_val_rex_t*)val)->ptr);
break;
2008-12-21 21:35:07 +00:00
case QSE_AWK_VAL_MAP:
DPRINTF (DCUSTOM, QSE_T("MAP["));
qse_htb_walk (((qse_awk_val_map_t*)val)->map, print_pair, run);
2008-12-21 21:35:07 +00:00
DPRINTF (DCUSTOM, QSE_T("]"));
break;
2008-12-21 21:35:07 +00:00
case QSE_AWK_VAL_REF:
DPRINTF (DCUSTOM, QSE_T("REF[id=%d,val="), ((qse_awk_val_ref_t*)val)->id);
qse_awk_dprintval (run, *((qse_awk_val_ref_t*)val)->adr);
DPRINTF (DCUSTOM, QSE_T("]"));
break;
default:
2008-12-21 21:35:07 +00:00
DPRINTF (DCUSTOM, QSE_T("**** INTERNAL ERROR - INVALID VALUE TYPE ****\n"));
}
}
2008-03-23 00:47:27 +00:00
2008-12-11 04:19:59 +00:00
#endif