qse/ase/awk/val.c

724 lines
16 KiB
C
Raw Normal View History

2006-03-04 15:54:37 +00:00
/*
2006-10-06 03:37:40 +00:00
* $Id: val.c,v 1.69 2006-10-06 03:33:43 bacon Exp $
2006-03-04 15:54:37 +00:00
*/
2006-03-31 16:35:37 +00:00
#include <xp/awk/awk_i.h>
2006-03-04 15:54:37 +00:00
2006-09-27 14:14:47 +00:00
static xp_char_t* __str_to_str (
xp_awk_run_t* run, const xp_char_t* str, xp_size_t str_len,
xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len);
static xp_char_t* __val_int_to_str (
xp_awk_run_t* run, xp_awk_val_int_t* v,
xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len);
static xp_char_t* __val_real_to_str (
xp_awk_run_t* run, xp_awk_val_real_t* v,
xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len);
2006-03-22 16:05:50 +00:00
static xp_awk_val_nil_t __awk_nil = { XP_AWK_VAL_NIL, 0 };
2006-03-07 15:55:14 +00:00
xp_awk_val_t* xp_awk_val_nil = (xp_awk_val_t*)&__awk_nil;
2006-07-10 04:51:38 +00:00
static xp_awk_val_str_t __awk_zls = { XP_AWK_VAL_STR, 0, XP_T(""), 0 };
xp_awk_val_t* xp_awk_val_zls = (xp_awk_val_t*)&__awk_zls;
2006-03-22 16:05:50 +00:00
static xp_awk_val_int_t __awk_int[] =
{
{ XP_AWK_VAL_INT, 0, -1 },
{ XP_AWK_VAL_INT, 0, 0 },
{ XP_AWK_VAL_INT, 0, 1 },
{ XP_AWK_VAL_INT, 0, 2 },
{ XP_AWK_VAL_INT, 0, 3 },
{ XP_AWK_VAL_INT, 0, 4 },
{ XP_AWK_VAL_INT, 0, 5 },
{ XP_AWK_VAL_INT, 0, 6 },
{ XP_AWK_VAL_INT, 0, 7 },
{ XP_AWK_VAL_INT, 0, 8 },
{ XP_AWK_VAL_INT, 0, 9 },
};
2006-07-12 07:25:15 +00:00
xp_awk_val_t* xp_awk_val_zero = (xp_awk_val_t*)&__awk_int[1];
xp_awk_val_t* xp_awk_val_one = (xp_awk_val_t*)&__awk_int[2];
2006-04-21 17:24:31 +00:00
xp_awk_val_t* xp_awk_makeintval (xp_awk_run_t* run, xp_long_t v)
2006-03-04 15:54:37 +00:00
{
xp_awk_val_int_t* val;
2006-03-22 16:05:50 +00:00
if (v >= __awk_int[0].val &&
v <= __awk_int[xp_countof(__awk_int)-1].val)
{
return (xp_awk_val_t*)&__awk_int[v-__awk_int[0].val];
}
2006-04-21 17:24:31 +00:00
if (run->icache_count > 0)
2006-03-27 11:43:17 +00:00
{
2006-04-21 17:24:31 +00:00
val = run->icache[--run->icache_count];
2006-03-27 11:43:17 +00:00
}
else
{
2006-08-31 15:39:14 +00:00
val = (xp_awk_val_int_t*) XP_AWK_MALLOC (
run->awk, xp_sizeof(xp_awk_val_int_t));
2006-03-27 11:43:17 +00:00
if (val == XP_NULL) return XP_NULL;
}
2006-03-04 15:54:37 +00:00
2006-03-05 17:07:33 +00:00
val->type = XP_AWK_VAL_INT;
2006-03-26 16:36:30 +00:00
val->ref = 0;
2006-03-04 15:54:37 +00:00
val->val = v;
2006-09-27 14:14:47 +00:00
val->nde = XP_NULL;
2006-03-05 17:07:33 +00:00
2006-07-06 15:54:41 +00:00
/*xp_printf (XP_T("makeintval => %p\n"), val);*/
2006-03-05 17:07:33 +00:00
return (xp_awk_val_t*)val;
}
2006-04-21 17:24:31 +00:00
xp_awk_val_t* xp_awk_makerealval (xp_awk_run_t* run, xp_real_t v)
2006-03-05 17:07:33 +00:00
{
xp_awk_val_real_t* val;
2006-04-21 17:24:31 +00:00
if (run->rcache_count > 0)
2006-03-28 16:33:09 +00:00
{
2006-04-21 17:24:31 +00:00
val = run->rcache[--run->rcache_count];
2006-03-28 16:33:09 +00:00
}
else
{
2006-08-31 15:39:14 +00:00
val = (xp_awk_val_real_t*) XP_AWK_MALLOC (
run->awk, xp_sizeof(xp_awk_val_real_t));
2006-03-28 16:33:09 +00:00
if (val == XP_NULL) return XP_NULL;
}
2006-03-05 17:07:33 +00:00
val->type = XP_AWK_VAL_REAL;
2006-03-26 16:36:30 +00:00
val->ref = 0;
2006-03-05 17:07:33 +00:00
val->val = v;
2006-09-27 14:14:47 +00:00
val->nde = XP_NULL;
2006-03-05 17:07:33 +00:00
2006-07-06 15:54:41 +00:00
/*xp_printf (XP_T("makerealval => %p\n"), val);*/
2006-03-05 17:07:33 +00:00
return (xp_awk_val_t*)val;
}
2006-08-31 15:22:40 +00:00
xp_awk_val_t* xp_awk_makestrval0 (xp_awk_run_t* run, const xp_char_t* str)
2006-06-26 15:09:28 +00:00
{
2006-09-01 06:23:58 +00:00
return xp_awk_makestrval (run, str, xp_awk_strlen(str));
2006-06-26 15:09:28 +00:00
}
2006-08-31 15:22:40 +00:00
xp_awk_val_t* xp_awk_makestrval (
xp_awk_run_t* run, const xp_char_t* str, xp_size_t len)
2006-03-05 17:07:33 +00:00
{
xp_awk_val_str_t* val;
2006-08-31 15:39:14 +00:00
val = (xp_awk_val_str_t*) XP_AWK_MALLOC (
run->awk, xp_sizeof(xp_awk_val_str_t));
2006-03-05 17:07:33 +00:00
if (val == XP_NULL) return XP_NULL;
val->type = XP_AWK_VAL_STR;
2006-03-26 16:36:30 +00:00
val->ref = 0;
2006-03-05 17:07:33 +00:00
val->len = len;
2006-09-01 03:44:51 +00:00
val->buf = xp_awk_strxdup (run->awk, str, len);
2006-04-17 16:12:02 +00:00
if (val->buf == XP_NULL)
{
2006-08-31 15:39:14 +00:00
XP_AWK_FREE (run->awk, val);
2006-03-05 17:07:33 +00:00
return XP_NULL;
}
2006-07-06 15:54:41 +00:00
/*xp_printf (XP_T("makestrval => %p\n"), val);*/
2006-03-04 15:54:37 +00:00
return (xp_awk_val_t*)val;
}
2006-03-05 17:07:33 +00:00
2006-04-06 16:25:37 +00:00
xp_awk_val_t* xp_awk_makestrval2 (
2006-08-31 15:22:40 +00:00
xp_awk_run_t* run,
2006-04-06 16:25:37 +00:00
const xp_char_t* str1, xp_size_t len1,
const xp_char_t* str2, xp_size_t len2)
{
xp_awk_val_str_t* val;
2006-08-31 15:39:14 +00:00
val = (xp_awk_val_str_t*) XP_AWK_MALLOC (
run->awk, xp_sizeof(xp_awk_val_str_t));
2006-04-06 16:25:37 +00:00
if (val == XP_NULL) return XP_NULL;
val->type = XP_AWK_VAL_STR;
val->ref = 0;
val->len = len1 + len2;
2006-09-01 03:44:51 +00:00
val->buf = xp_awk_strxdup2 (run->awk, str1, len1, str2, len2);
2006-04-17 16:12:02 +00:00
if (val->buf == XP_NULL)
{
2006-08-31 15:39:14 +00:00
XP_AWK_FREE (run->awk, val);
2006-04-17 16:12:02 +00:00
return XP_NULL;
}
2006-07-06 15:54:41 +00:00
/*xp_printf (XP_T("makestrval2 => %p\n"), val);*/
2006-04-17 16:12:02 +00:00
return (xp_awk_val_t*)val;
}
2006-07-26 15:00:01 +00:00
xp_awk_val_t* xp_awk_makerexval (
2006-08-31 15:22:40 +00:00
xp_awk_run_t* run, const xp_char_t* buf, xp_size_t len, void* code)
2006-04-24 11:26:00 +00:00
{
xp_awk_val_rex_t* val;
2006-08-31 15:39:14 +00:00
val = (xp_awk_val_rex_t*) XP_AWK_MALLOC (
run->awk, xp_sizeof(xp_awk_val_rex_t));
2006-04-24 11:26:00 +00:00
if (val == XP_NULL) return XP_NULL;
2006-07-25 16:41:40 +00:00
val->type = XP_AWK_VAL_REX;
2006-04-24 11:26:00 +00:00
val->ref = 0;
val->len = len;
2006-09-01 03:44:51 +00:00
val->buf = xp_awk_strxdup (run->awk, buf, len);
2006-04-24 11:26:00 +00:00
if (val->buf == XP_NULL)
{
2006-08-31 15:39:14 +00:00
XP_AWK_FREE (run->awk, val);
2006-04-24 11:26:00 +00:00
return XP_NULL;
}
2006-08-31 15:39:14 +00:00
val->code = XP_AWK_MALLOC (run->awk, XP_AWK_REX_LEN(code));
2006-07-26 15:00:01 +00:00
if (val->code == XP_NULL)
{
2006-08-31 15:39:14 +00:00
XP_AWK_FREE (run->awk, val->buf);
XP_AWK_FREE (run->awk, val);
2006-07-26 15:00:01 +00:00
return XP_NULL;
}
2006-09-25 06:17:19 +00:00
XP_AWK_MEMCPY (run->awk, val->code, code, XP_AWK_REX_LEN(code));
2006-04-24 11:26:00 +00:00
return (xp_awk_val_t*)val;
}
2006-04-21 17:24:31 +00:00
static void __free_map_val (void* run, void* v)
2006-04-17 16:12:02 +00:00
{
2006-04-18 10:28:03 +00:00
/*
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("refdown in map free..."));
2006-04-18 10:28:03 +00:00
xp_awk_printval (v);
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("\n"));
2006-04-18 10:28:03 +00:00
*/
2006-04-21 17:24:31 +00:00
xp_awk_refdownval (run, v);
2006-04-17 16:12:02 +00:00
}
2006-04-21 17:24:31 +00:00
xp_awk_val_t* xp_awk_makemapval (xp_awk_run_t* run)
2006-04-17 16:12:02 +00:00
{
xp_awk_val_map_t* val;
2006-08-31 15:39:14 +00:00
val = (xp_awk_val_map_t*) XP_AWK_MALLOC (
run->awk, xp_sizeof(xp_awk_val_map_t));
2006-04-17 16:12:02 +00:00
if (val == XP_NULL) return XP_NULL;
val->type = XP_AWK_VAL_MAP;
val->ref = 0;
2006-09-01 03:44:51 +00:00
val->map = xp_awk_map_open (
XP_NULL, run, 256, __free_map_val, run->awk);
2006-04-17 16:12:02 +00:00
if (val->map == XP_NULL)
{
2006-08-31 15:39:14 +00:00
XP_AWK_FREE (run->awk, val);
2006-04-06 16:25:37 +00:00
return XP_NULL;
}
return (xp_awk_val_t*)val;
}
2006-08-20 15:49:48 +00:00
xp_awk_val_t* xp_awk_makerefval (xp_awk_run_t* run, int id, xp_awk_val_t** adr)
{
xp_awk_val_ref_t* val;
2006-08-21 02:53:42 +00:00
if (run->fcache_count > 0)
{
val = run->fcache[--run->fcache_count];
}
else
{
2006-08-31 15:39:14 +00:00
val = (xp_awk_val_ref_t*) XP_AWK_MALLOC (
run->awk, xp_sizeof(xp_awk_val_ref_t));
2006-08-21 02:53:42 +00:00
if (val == XP_NULL) return XP_NULL;
}
2006-08-20 15:49:48 +00:00
val->type = XP_AWK_VAL_REF;
val->ref = 0;
val->id = id;
val->adr = adr;
return (xp_awk_val_t*)val;
}
2006-04-24 11:26:00 +00:00
2006-03-26 16:36:30 +00:00
xp_bool_t xp_awk_isbuiltinval (xp_awk_val_t* val)
{
2006-07-10 04:51:38 +00:00
return val == XP_NULL ||
2006-07-12 17:04:02 +00:00
val == xp_awk_val_nil || val == xp_awk_val_zls ||
val == xp_awk_val_zero || val == xp_awk_val_one ||
2006-03-26 16:36:30 +00:00
(val >= (xp_awk_val_t*)&__awk_int[0] &&
val <= (xp_awk_val_t*)&__awk_int[xp_countof(__awk_int)-1]);
}
2006-04-21 17:24:31 +00:00
void xp_awk_freeval (xp_awk_run_t* run, xp_awk_val_t* val, xp_bool_t cache)
2006-03-04 15:54:37 +00:00
{
2006-03-26 16:36:30 +00:00
if (xp_awk_isbuiltinval(val)) return;
2006-03-22 16:05:50 +00:00
2006-05-13 16:33:07 +00:00
/*xp_printf (XP_T("freeing [cache=%d] ... "), cache);
2006-04-18 10:28:03 +00:00
xp_awk_printval (val);
2006-05-13 16:33:07 +00:00
xp_printf (XP_T("\n"));*/
2006-08-20 15:49:48 +00:00
if (val->type == XP_AWK_VAL_NIL)
2006-03-04 15:54:37 +00:00
{
2006-08-31 15:39:14 +00:00
XP_AWK_FREE (run->awk, val);
2006-08-20 15:49:48 +00:00
}
else if (val->type == XP_AWK_VAL_INT)
{
2006-04-20 16:17:01 +00:00
if (cache == xp_true &&
2006-04-21 17:24:31 +00:00
run->icache_count < xp_countof(run->icache))
2006-03-27 11:43:17 +00:00
{
2006-04-21 17:24:31 +00:00
run->icache[run->icache_count++] =
2006-03-27 11:43:17 +00:00
(xp_awk_val_int_t*)val;
}
2006-08-31 15:39:14 +00:00
else XP_AWK_FREE (run->awk, val);
2006-08-20 15:49:48 +00:00
}
else if (val->type == XP_AWK_VAL_REAL)
{
2006-04-20 16:17:01 +00:00
if (cache == xp_true &&
2006-04-21 17:24:31 +00:00
run->rcache_count < xp_countof(run->rcache))
2006-03-28 16:33:09 +00:00
{
2006-04-21 17:24:31 +00:00
run->rcache[run->rcache_count++] =
2006-03-28 16:33:09 +00:00
(xp_awk_val_real_t*)val;
}
2006-08-31 15:39:14 +00:00
else XP_AWK_FREE (run->awk, val);
2006-08-20 15:49:48 +00:00
}
else if (val->type == XP_AWK_VAL_STR)
{
2006-08-31 15:39:14 +00:00
XP_AWK_FREE (run->awk, ((xp_awk_val_str_t*)val)->buf);
XP_AWK_FREE (run->awk, val);
2006-08-20 15:49:48 +00:00
}
else if (val->type == XP_AWK_VAL_REX)
{
2006-08-31 15:39:14 +00:00
XP_AWK_FREE (run->awk, ((xp_awk_val_rex_t*)val)->buf);
2006-09-01 03:44:51 +00:00
xp_awk_freerex (run->awk, ((xp_awk_val_rex_t*)val)->code);
2006-08-31 15:39:14 +00:00
XP_AWK_FREE (run->awk, val);
2006-08-20 15:49:48 +00:00
}
else if (val->type == XP_AWK_VAL_MAP)
{
2006-04-17 16:12:02 +00:00
xp_awk_map_close (((xp_awk_val_map_t*)val)->map);
2006-08-31 15:39:14 +00:00
XP_AWK_FREE (run->awk, val);
2006-03-04 15:54:37 +00:00
}
2006-08-20 15:49:48 +00:00
else if (val->type == XP_AWK_VAL_REF)
{
2006-08-21 02:53:42 +00:00
if (cache == xp_true &&
run->fcache_count < xp_countof(run->fcache))
{
run->fcache[run->fcache_count++] =
(xp_awk_val_ref_t*)val;
}
2006-08-31 15:39:14 +00:00
else XP_AWK_FREE (run->awk, val);
2006-08-20 15:49:48 +00:00
}
else
{
xp_assert (!"should never happen - invalid value type");
}
2006-03-04 15:54:37 +00:00
}
2006-03-23 13:26:04 +00:00
void xp_awk_refupval (xp_awk_val_t* val)
{
2006-03-26 16:36:30 +00:00
if (xp_awk_isbuiltinval(val)) return;
2006-03-23 15:36:20 +00:00
/*
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("ref up "));
2006-03-23 15:36:20 +00:00
xp_awk_printval (val);
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("\n"));
2006-03-23 15:36:20 +00:00
*/
2006-03-23 13:26:04 +00:00
val->ref++;
}
2006-04-21 17:24:31 +00:00
void xp_awk_refdownval (xp_awk_run_t* run, xp_awk_val_t* val)
2006-03-23 13:26:04 +00:00
{
2006-03-26 16:36:30 +00:00
if (xp_awk_isbuiltinval(val)) return;
2006-03-23 15:36:20 +00:00
/*
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("%p, %p, %p\n"), xp_awk_val_nil, &__awk_nil, val);
xp_printf (XP_T("ref down [count=>%d]\n"), (int)val->ref);
2006-03-23 15:36:20 +00:00
xp_awk_printval (val);
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("\n"));
2006-03-23 15:36:20 +00:00
*/
2006-03-23 13:26:04 +00:00
xp_assert (val->ref > 0);
val->ref--;
2006-03-26 16:36:30 +00:00
if (val->ref <= 0)
{
2006-07-06 15:54:41 +00:00
/*
2006-07-06 13:57:32 +00:00
xp_printf (XP_T("**FREEING ["));
2006-03-26 16:36:30 +00:00
xp_awk_printval (val);
2006-07-06 13:57:32 +00:00
xp_printf (XP_T("]\n"));
2006-07-06 15:54:41 +00:00
*/
2006-04-21 17:24:31 +00:00
xp_awk_freeval(run, val, xp_true);
2006-03-26 16:36:30 +00:00
}
}
2006-04-21 17:24:31 +00:00
void xp_awk_refdownval_nofree (xp_awk_run_t* run, xp_awk_val_t* val)
2006-03-26 16:36:30 +00:00
{
if (xp_awk_isbuiltinval(val)) return;
xp_assert (val->ref > 0);
val->ref--;
2006-03-23 13:26:04 +00:00
}
2006-06-21 11:45:26 +00:00
xp_bool_t xp_awk_valtobool (xp_awk_val_t* val)
2006-03-15 15:34:59 +00:00
{
if (val == XP_NULL) return xp_false;
switch (val->type)
{
case XP_AWK_VAL_NIL:
return xp_false;
case XP_AWK_VAL_INT:
2006-04-07 04:23:11 +00:00
return ((xp_awk_val_int_t*)val)->val != 0;
2006-03-15 15:34:59 +00:00
case XP_AWK_VAL_REAL:
2006-04-07 04:23:11 +00:00
return ((xp_awk_val_real_t*)val)->val != 0.0;
2006-03-15 15:34:59 +00:00
case XP_AWK_VAL_STR:
2006-04-07 04:23:11 +00:00
return ((xp_awk_val_str_t*)val)->len > 0;
2006-07-25 16:41:40 +00:00
case XP_AWK_VAL_REX: /* TODO: is this correct? */
return ((xp_awk_val_rex_t*)val)->len > 0;
2006-04-17 16:12:02 +00:00
case XP_AWK_VAL_MAP:
return xp_false; /* TODO: is this correct? */
2006-08-20 15:49:48 +00:00
case XP_AWK_VAL_REF:
return xp_false; /* TODO: is this correct? */
2006-03-15 15:34:59 +00:00
}
2006-04-17 16:12:02 +00:00
xp_assert (!"should never happen - invalid value type");
2006-03-22 16:05:50 +00:00
return xp_false;
2006-03-15 15:34:59 +00:00
}
2006-07-13 03:10:35 +00:00
xp_char_t* xp_awk_valtostr (
2006-08-31 15:39:14 +00:00
xp_awk_run_t* run, xp_awk_val_t* v,
2006-08-31 16:00:20 +00:00
xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len)
2006-06-21 11:45:26 +00:00
{
2006-06-28 08:56:59 +00:00
if (v->type == XP_AWK_VAL_NIL)
{
2006-09-27 14:14:47 +00:00
return __str_to_str (run, XP_T(""), 0, clear_buf, buf, len);
}
2006-07-13 03:10:35 +00:00
2006-09-27 14:14:47 +00:00
if (v->type == XP_AWK_VAL_INT)
{
xp_awk_val_int_t* vi = (xp_awk_val_int_t*)v;
2006-10-05 14:20:57 +00:00
/*
2006-09-28 14:21:45 +00:00
if (vi->nde != XP_NULL && vi->nde->str != XP_NULL)
2006-09-27 14:14:47 +00:00
{
return __str_to_str (
run, vi->nde->str, vi->nde->len,
clear_buf, buf, len);
2006-06-28 08:56:59 +00:00
}
else
{
2006-10-05 14:20:57 +00:00
*/
2006-09-27 14:14:47 +00:00
return __val_int_to_str (run, vi, clear_buf, buf, len);
2006-10-05 14:20:57 +00:00
/*}*/
2006-09-27 14:14:47 +00:00
}
2006-08-03 05:05:48 +00:00
2006-09-27 14:14:47 +00:00
if (v->type == XP_AWK_VAL_REAL)
{
xp_awk_val_real_t* vr = (xp_awk_val_real_t*)v;
2006-10-05 14:24:38 +00:00
/*
2006-09-28 14:21:45 +00:00
if (vr->nde != XP_NULL && vr->nde->str != XP_NULL)
2006-09-27 14:14:47 +00:00
{
return __str_to_str (
run, vr->nde->str, vr->nde->len,
clear_buf, buf, len);
}
else
2006-10-05 14:24:38 +00:00
{*/
2006-09-27 14:14:47 +00:00
return __val_real_to_str (run, vr, clear_buf, buf, len);
2006-10-05 14:24:38 +00:00
/*}*/
2006-06-28 08:56:59 +00:00
}
2006-09-27 14:14:47 +00:00
if (v->type == XP_AWK_VAL_STR)
{
xp_awk_val_str_t* vs = (xp_awk_val_str_t*)v;
return __str_to_str (
run, vs->buf, vs->len, clear_buf, buf, len);
}
/* TODO: process more value types */
xp_printf (XP_T("*** ERROR: WRONG VALUE TYPE [%d] in xp_awk_valtostr v=> %p***\n"), v->type, v);
run->errnum = XP_AWK_EVALTYPE;
return XP_NULL;
}
static xp_char_t* __str_to_str (
xp_awk_run_t* run, const xp_char_t* str, xp_size_t str_len,
xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len)
{
if (buf == XP_NULL)
2006-06-21 11:45:26 +00:00
{
xp_char_t* tmp;
2006-09-27 14:14:47 +00:00
tmp = xp_awk_strxdup (run->awk, str, str_len);
if (tmp == XP_NULL)
{
run->errnum = XP_AWK_ENOMEM;
return XP_NULL;
}
if (len != XP_NULL) *len = str_len;
return tmp;
}
else
{
xp_size_t n;
2006-06-21 11:45:26 +00:00
2006-09-27 14:14:47 +00:00
if (clear_buf) xp_awk_str_clear (buf);
n = xp_awk_str_ncat (buf, str, str_len);
if (n == (xp_size_t)-1)
2006-06-21 11:45:26 +00:00
{
2006-09-27 14:14:47 +00:00
run->errnum = XP_AWK_ENOMEM;
return XP_NULL;
2006-06-21 11:45:26 +00:00
}
2006-09-27 14:14:47 +00:00
if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf);
return XP_AWK_STR_BUF(buf);
}
}
static xp_char_t* __val_int_to_str (
xp_awk_run_t* run, xp_awk_val_int_t* v,
xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len)
{
xp_char_t* tmp;
xp_long_t t;
xp_size_t l = 0;
2006-06-21 11:45:26 +00:00
2006-09-27 14:14:47 +00:00
t = v->val;
if (t == 0)
{
/* handle zero */
2006-06-21 11:45:26 +00:00
if (buf == XP_NULL)
{
2006-08-31 15:39:14 +00:00
tmp = XP_AWK_MALLOC (
2006-09-27 14:14:47 +00:00
run->awk, 2 * xp_sizeof(xp_char_t));
2006-06-21 11:45:26 +00:00
if (tmp == XP_NULL)
{
2006-08-31 15:39:14 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-06-21 11:45:26 +00:00
return XP_NULL;
}
2006-09-27 14:14:47 +00:00
tmp[0] = XP_T('0');
tmp[1] = XP_T('\0');
if (len != XP_NULL) *len = 1;
return tmp;
2006-06-21 11:45:26 +00:00
}
else
{
2006-08-31 16:00:20 +00:00
if (clear_buf) xp_awk_str_clear (buf);
2006-09-27 14:14:47 +00:00
if (xp_awk_str_cat (buf, XP_T("0")) == (xp_size_t)-1)
2006-06-21 11:45:26 +00:00
{
2006-08-31 15:39:14 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-06-21 11:45:26 +00:00
return XP_NULL;
}
2006-09-27 14:14:47 +00:00
if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf);
return XP_AWK_STR_BUF(buf);
2006-06-21 11:45:26 +00:00
}
2006-09-27 14:14:47 +00:00
}
2006-06-21 11:45:26 +00:00
2006-09-27 14:14:47 +00:00
/* non-zero values */
if (t < 0) { t = -t; l++; }
while (t > 0) { l++; t /= 10; }
2006-06-21 11:45:26 +00:00
2006-09-27 14:14:47 +00:00
if (buf == XP_NULL)
{
tmp = XP_AWK_MALLOC (
run->awk, (l + 1) * xp_sizeof(xp_char_t));
if (tmp == XP_NULL)
2006-06-21 11:45:26 +00:00
{
2006-09-27 14:14:47 +00:00
run->errnum = XP_AWK_ENOMEM;
return XP_NULL;
2006-06-21 11:45:26 +00:00
}
2006-09-27 14:14:47 +00:00
tmp[l] = XP_T('\0');
if (len != XP_NULL) *len = l;
}
else
{
/* clear the buffer */
if (clear_buf) xp_awk_str_clear (buf);
tmp = XP_AWK_STR_BUF(buf) + XP_AWK_STR_LEN(buf);
2006-06-21 11:45:26 +00:00
2006-09-27 14:14:47 +00:00
/* extend the buffer */
if (xp_awk_str_nccat (
buf, XP_T(' '), l) == (xp_size_t)-1)
2006-08-03 05:05:48 +00:00
{
2006-09-27 14:14:47 +00:00
run->errnum = XP_AWK_ENOMEM;
return XP_NULL;
2006-08-03 05:05:48 +00:00
}
2006-06-21 11:45:26 +00:00
}
2006-09-27 14:14:47 +00:00
t = v->val;
if (t < 0) t = -t;
while (t > 0)
2006-07-17 04:17:40 +00:00
{
2006-09-27 14:14:47 +00:00
tmp[--l] = (xp_char_t)(t % 10) + XP_T('0');
t /= 10;
}
2006-07-17 06:21:39 +00:00
2006-09-27 14:14:47 +00:00
if (v->val < 0) tmp[--l] = XP_T('-');
2006-08-03 05:05:48 +00:00
2006-09-27 14:14:47 +00:00
if (buf != XP_NULL)
{
tmp = XP_AWK_STR_BUF(buf);
if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf);
}
2006-07-17 06:21:39 +00:00
2006-09-27 14:14:47 +00:00
return tmp;
}
2006-07-17 06:21:39 +00:00
2006-09-27 14:14:47 +00:00
static xp_char_t* __val_real_to_str (
xp_awk_run_t* run, xp_awk_val_real_t* v,
xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len)
{
/* TODO: change the code */
xp_char_t tbuf[256], * tmp;
2006-10-04 14:52:12 +00:00
/* TODO: need to use awk's own version of sprintf so that it would have
* problems with handling long double or double... */
/*
2006-09-27 14:14:47 +00:00
#if (XP_SIZEOF_LONG_DOUBLE != 0)
run->awk->syscas->sprintf (
2006-10-04 14:52:12 +00:00
tbuf, xp_countof(tbuf), run->global.ofmt.ptr, (long double)v->val);
2006-09-27 14:14:47 +00:00
#elif (XP_SIZEOF_DOUBLE != 0)
2006-10-04 14:52:12 +00:00
*/
/* TODO: does it need to check if a null character is included in ofmt??? */
2006-09-27 14:14:47 +00:00
run->awk->syscas->sprintf (
2006-10-04 14:52:12 +00:00
tbuf, xp_countof(tbuf),
run->global.ofmt.ptr, (double)v->val);
/*
2006-09-27 14:14:47 +00:00
#else
#error unsupported floating-point data type
#endif
2006-10-04 14:52:12 +00:00
*/
2006-07-17 04:17:40 +00:00
2006-09-27 14:14:47 +00:00
if (buf == XP_NULL)
2006-06-21 11:45:26 +00:00
{
2006-09-27 14:14:47 +00:00
tmp = xp_awk_strdup (run->awk, tbuf);
if (tmp == XP_NULL)
2006-06-21 11:45:26 +00:00
{
2006-09-27 14:14:47 +00:00
run->errnum = XP_AWK_ENOMEM;
return XP_NULL;
2006-06-21 11:45:26 +00:00
}
2006-09-27 14:14:47 +00:00
if (len != XP_NULL) *len = xp_awk_strlen(tmp);
}
else
{
if (clear_buf) xp_awk_str_clear (buf);
2006-08-03 05:05:48 +00:00
2006-09-27 14:14:47 +00:00
if (xp_awk_str_cat (buf, tbuf) == (xp_size_t)-1)
{
run->errnum = XP_AWK_ENOMEM;
return XP_NULL;
2006-06-21 11:45:26 +00:00
}
2006-09-27 14:14:47 +00:00
tmp = XP_AWK_STR_BUF(buf);
if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf);
2006-06-21 11:45:26 +00:00
}
2006-09-27 14:14:47 +00:00
return tmp;
2006-06-21 11:45:26 +00:00
}
2006-09-01 06:23:58 +00:00
int xp_awk_valtonum (
xp_awk_run_t* run, xp_awk_val_t* v, xp_long_t* l, xp_real_t* r)
2006-07-17 04:17:40 +00:00
{
if (v->type == XP_AWK_VAL_NIL)
{
*l = 0;
return 0;
}
if (v->type == XP_AWK_VAL_INT)
{
*l = ((xp_awk_val_int_t*)v)->val;
return 0; /* long */
}
if (v->type == XP_AWK_VAL_REAL)
{
*r = ((xp_awk_val_real_t*)v)->val;
return 1; /* real */
}
if (v->type == XP_AWK_VAL_STR)
{
const xp_char_t* endptr;
2006-10-05 14:20:57 +00:00
*l = xp_awk_strxtolong (run->awk,
((xp_awk_val_str_t*)v)->buf,
((xp_awk_val_str_t*)v)->len, 0, &endptr);
2006-07-17 04:17:40 +00:00
if (*endptr == XP_T('.') ||
*endptr == XP_T('E') ||
*endptr == XP_T('e'))
{
2006-10-06 03:37:40 +00:00
*r = xp_awk_strxtoreal (run->awk,
((xp_awk_val_str_t*)v)->buf,
((xp_awk_val_str_t*)v)->len, XP_NULL);
/* TODO: need to check if it is a valid number using endptr for strxtoreal? */
2006-07-17 04:17:40 +00:00
return 1; /* real */
}
return 0; /* long */
}
2006-07-17 06:21:39 +00:00
xp_printf (XP_T("*** ERROR: WRONG VALUE TYPE [%d] in xp_awk_valtonum v=> %p***\n"), v->type, v);
2006-09-01 06:23:58 +00:00
run->errnum = XP_AWK_EVALTYPE;
2006-07-17 04:17:40 +00:00
return -1; /* error */
}
2006-04-30 17:12:51 +00:00
static int __print_pair (xp_awk_pair_t* pair, void* arg)
2006-04-20 05:44:29 +00:00
{
2006-05-06 12:52:36 +00:00
xp_printf (XP_T(" %s=>"), pair->key);
2006-04-20 05:44:29 +00:00
xp_awk_printval (pair->val);
2006-05-06 12:52:36 +00:00
xp_printf (XP_T(" "));
2006-04-20 05:44:29 +00:00
return 0;
}
2006-03-04 15:54:37 +00:00
void xp_awk_printval (xp_awk_val_t* val)
{
2006-04-14 10:56:42 +00:00
/* TODO: better value printing...................... */
2006-03-04 15:54:37 +00:00
switch (val->type)
{
2006-03-07 15:55:14 +00:00
case XP_AWK_VAL_NIL:
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("nil"));
2006-03-07 15:55:14 +00:00
break;
2006-03-04 15:54:37 +00:00
case XP_AWK_VAL_INT:
2006-03-15 15:34:59 +00:00
#if defined(__LCC__)
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("%lld"),
2006-03-15 15:34:59 +00:00
(long long)((xp_awk_val_int_t*)val)->val);
#elif defined(__BORLANDC__) || defined(_MSC_VER)
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("%I64d"),
2006-03-14 16:40:00 +00:00
(__int64)((xp_awk_nde_int_t*)val)->val);
2006-04-14 10:56:42 +00:00
#elif defined(vax) || defined(__vax) || defined(_SCO_DS)
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("%ld"),
2006-03-15 15:34:59 +00:00
(long)((xp_awk_val_int_t*)val)->val);
2006-03-14 16:40:00 +00:00
#else
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("%lld"),
2006-03-04 15:54:37 +00:00
(long long)((xp_awk_val_int_t*)val)->val);
2006-03-14 16:40:00 +00:00
#endif
2006-03-04 15:54:37 +00:00
break;
case XP_AWK_VAL_REAL:
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("%Lf"),
2006-03-04 15:54:37 +00:00
(long double)((xp_awk_val_real_t*)val)->val);
break;
case XP_AWK_VAL_STR:
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("%s"), ((xp_awk_val_str_t*)val)->buf);
2006-03-04 15:54:37 +00:00
break;
2006-04-24 11:26:00 +00:00
case XP_AWK_VAL_REX:
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("REX[%s]"), ((xp_awk_val_rex_t*)val)->buf);
2006-04-24 11:26:00 +00:00
break;
2006-04-17 16:12:02 +00:00
case XP_AWK_VAL_MAP:
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("MAP["));
2006-04-30 17:12:51 +00:00
xp_awk_map_walk (((xp_awk_val_map_t*)val)->map, __print_pair, XP_NULL);
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("]"));
2006-04-17 16:12:02 +00:00
break;
2006-08-20 15:49:48 +00:00
case XP_AWK_VAL_REF:
xp_printf (XP_T("REF[id=%d,val="), ((xp_awk_val_ref_t*)val)->id);
xp_awk_printval (*((xp_awk_val_ref_t*)val)->adr);
xp_printf (XP_T("]"));
break;
2006-04-17 16:12:02 +00:00
2006-03-04 15:54:37 +00:00
default:
2006-04-16 04:31:38 +00:00
xp_assert (!"should never happen - invalid value type");
2006-05-06 12:52:36 +00:00
xp_printf (XP_T("**** INTERNAL ERROR - INVALID VALUE TYPE ****\n"));
2006-03-04 15:54:37 +00:00
}
}