qse/ase/awk/val.c

713 lines
16 KiB
C
Raw Normal View History

2006-03-04 15:54:37 +00:00
/*
2006-10-22 12:39:30 +00:00
* $Id: val.c,v 1.75 2006-10-22 12:39:30 bacon Exp $
2006-03-04 15:54:37 +00:00
*/
2006-10-22 11:34:53 +00:00
#include <sse/awk/awk_i.h>
2006-03-04 15:54:37 +00:00
2006-10-22 11:34:53 +00:00
static sse_char_t* __str_to_str (
sse_awk_run_t* run, const sse_char_t* str, sse_size_t str_len,
int opt, sse_awk_str_t* buf, sse_size_t* len);
static sse_char_t* __val_int_to_str (
sse_awk_run_t* run, sse_awk_val_int_t* v,
int opt, sse_awk_str_t* buf, sse_size_t* len);
static sse_char_t* __val_real_to_str (
sse_awk_run_t* run, sse_awk_val_real_t* v,
int opt, sse_awk_str_t* buf, sse_size_t* len);
2006-09-27 14:14:47 +00:00
2006-10-22 11:34:53 +00:00
static sse_awk_val_nil_t __awk_nil = { SSE_AWK_VAL_NIL, 0 };
sse_awk_val_t* sse_awk_val_nil = (sse_awk_val_t*)&__awk_nil;
2006-03-07 15:55:14 +00:00
2006-10-22 11:34:53 +00:00
static sse_awk_val_str_t __awk_zls = { SSE_AWK_VAL_STR, 0, SSE_T(""), 0 };
sse_awk_val_t* sse_awk_val_zls = (sse_awk_val_t*)&__awk_zls;
2006-07-10 04:51:38 +00:00
2006-10-22 11:34:53 +00:00
static sse_awk_val_int_t __awk_int[] =
2006-03-22 16:05:50 +00:00
{
2006-10-22 11:34:53 +00:00
{ SSE_AWK_VAL_INT, 0, -1 },
{ SSE_AWK_VAL_INT, 0, 0 },
{ SSE_AWK_VAL_INT, 0, 1 },
{ SSE_AWK_VAL_INT, 0, 2 },
{ SSE_AWK_VAL_INT, 0, 3 },
{ SSE_AWK_VAL_INT, 0, 4 },
{ SSE_AWK_VAL_INT, 0, 5 },
{ SSE_AWK_VAL_INT, 0, 6 },
{ SSE_AWK_VAL_INT, 0, 7 },
{ SSE_AWK_VAL_INT, 0, 8 },
{ SSE_AWK_VAL_INT, 0, 9 },
2006-03-22 16:05:50 +00:00
};
2006-10-22 11:34:53 +00:00
sse_awk_val_t* sse_awk_val_zero = (sse_awk_val_t*)&__awk_int[1];
sse_awk_val_t* sse_awk_val_one = (sse_awk_val_t*)&__awk_int[2];
2006-07-12 07:25:15 +00:00
2006-10-22 11:34:53 +00:00
sse_awk_val_t* sse_awk_makeintval (sse_awk_run_t* run, sse_long_t v)
2006-03-04 15:54:37 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_val_int_t* val;
2006-03-04 15:54:37 +00:00
2006-03-22 16:05:50 +00:00
if (v >= __awk_int[0].val &&
2006-10-22 11:34:53 +00:00
v <= __awk_int[sse_countof(__awk_int)-1].val)
2006-03-22 16:05:50 +00:00
{
2006-10-22 11:34:53 +00:00
return (sse_awk_val_t*)&__awk_int[v-__awk_int[0].val];
2006-03-22 16:05:50 +00:00
}
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-10-22 11:34:53 +00:00
val = (sse_awk_val_int_t*) SSE_AWK_MALLOC (
run->awk, sse_sizeof(sse_awk_val_int_t));
if (val == SSE_NULL) return SSE_NULL;
2006-03-27 11:43:17 +00:00
}
2006-03-04 15:54:37 +00:00
2006-10-22 11:34:53 +00:00
val->type = SSE_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-10-22 11:34:53 +00:00
val->nde = SSE_NULL;
2006-03-05 17:07:33 +00:00
2006-10-22 12:39:30 +00:00
/*xp_printf (SSE_T("makeintval => %p\n"), val);*/
2006-10-22 11:34:53 +00:00
return (sse_awk_val_t*)val;
2006-03-05 17:07:33 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_val_t* sse_awk_makerealval (sse_awk_run_t* run, sse_real_t v)
2006-03-05 17:07:33 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_val_real_t* val;
2006-03-05 17:07:33 +00:00
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-10-22 11:34:53 +00:00
val = (sse_awk_val_real_t*) SSE_AWK_MALLOC (
run->awk, sse_sizeof(sse_awk_val_real_t));
if (val == SSE_NULL) return SSE_NULL;
2006-03-28 16:33:09 +00:00
}
2006-03-05 17:07:33 +00:00
2006-10-22 11:34:53 +00:00
val->type = SSE_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-10-22 11:34:53 +00:00
val->nde = SSE_NULL;
2006-03-05 17:07:33 +00:00
2006-10-22 12:39:30 +00:00
/*xp_printf (SSE_T("makerealval => %p\n"), val);*/
2006-10-22 11:34:53 +00:00
return (sse_awk_val_t*)val;
2006-03-05 17:07:33 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_val_t* sse_awk_makestrval0 (sse_awk_run_t* run, const sse_char_t* str)
2006-06-26 15:09:28 +00:00
{
2006-10-22 11:34:53 +00:00
return sse_awk_makestrval (run, str, sse_awk_strlen(str));
2006-06-26 15:09:28 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_val_t* sse_awk_makestrval (
sse_awk_run_t* run, const sse_char_t* str, sse_size_t len)
2006-03-05 17:07:33 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_val_str_t* val;
2006-03-05 17:07:33 +00:00
2006-10-22 11:34:53 +00:00
val = (sse_awk_val_str_t*) SSE_AWK_MALLOC (
run->awk, sse_sizeof(sse_awk_val_str_t));
if (val == SSE_NULL) return SSE_NULL;
2006-03-05 17:07:33 +00:00
2006-10-22 11:34:53 +00:00
val->type = SSE_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-10-22 11:34:53 +00:00
val->buf = sse_awk_strxdup (run->awk, str, len);
if (val->buf == SSE_NULL)
2006-04-17 16:12:02 +00:00
{
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE (run->awk, val);
return SSE_NULL;
2006-03-05 17:07:33 +00:00
}
2006-10-22 12:39:30 +00:00
/*xp_printf (SSE_T("makestrval => %p\n"), val);*/
2006-10-22 11:34:53 +00:00
return (sse_awk_val_t*)val;
2006-03-04 15:54:37 +00:00
}
2006-03-05 17:07:33 +00:00
2006-10-22 11:34:53 +00:00
sse_awk_val_t* sse_awk_makestrval2 (
sse_awk_run_t* run,
const sse_char_t* str1, sse_size_t len1,
const sse_char_t* str2, sse_size_t len2)
2006-04-06 16:25:37 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_val_str_t* val;
2006-04-06 16:25:37 +00:00
2006-10-22 11:34:53 +00:00
val = (sse_awk_val_str_t*) SSE_AWK_MALLOC (
run->awk, sse_sizeof(sse_awk_val_str_t));
if (val == SSE_NULL) return SSE_NULL;
2006-04-06 16:25:37 +00:00
2006-10-22 11:34:53 +00:00
val->type = SSE_AWK_VAL_STR;
2006-04-06 16:25:37 +00:00
val->ref = 0;
val->len = len1 + len2;
2006-10-22 11:34:53 +00:00
val->buf = sse_awk_strxdup2 (run->awk, str1, len1, str2, len2);
if (val->buf == SSE_NULL)
2006-04-17 16:12:02 +00:00
{
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE (run->awk, val);
return SSE_NULL;
2006-04-17 16:12:02 +00:00
}
2006-10-22 12:39:30 +00:00
/*xp_printf (SSE_T("makestrval2 => %p\n"), val);*/
2006-10-22 11:34:53 +00:00
return (sse_awk_val_t*)val;
2006-04-17 16:12:02 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_val_t* sse_awk_makerexval (
sse_awk_run_t* run, const sse_char_t* buf, sse_size_t len, void* code)
2006-04-24 11:26:00 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_val_rex_t* val;
2006-04-24 11:26:00 +00:00
2006-10-22 11:34:53 +00:00
val = (sse_awk_val_rex_t*) SSE_AWK_MALLOC (
run->awk, sse_sizeof(sse_awk_val_rex_t));
if (val == SSE_NULL) return SSE_NULL;
2006-04-24 11:26:00 +00:00
2006-10-22 11:34:53 +00:00
val->type = SSE_AWK_VAL_REX;
2006-04-24 11:26:00 +00:00
val->ref = 0;
val->len = len;
2006-10-22 11:34:53 +00:00
val->buf = sse_awk_strxdup (run->awk, buf, len);
if (val->buf == SSE_NULL)
2006-04-24 11:26:00 +00:00
{
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE (run->awk, val);
return SSE_NULL;
2006-04-24 11:26:00 +00:00
}
2006-10-22 11:34:53 +00:00
val->code = SSE_AWK_MALLOC (run->awk, SSE_AWK_REX_LEN(code));
if (val->code == SSE_NULL)
2006-07-26 15:00:01 +00:00
{
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE (run->awk, val->buf);
SSE_AWK_FREE (run->awk, val);
return SSE_NULL;
2006-07-26 15:00:01 +00:00
}
2006-10-22 11:34:53 +00:00
SSE_AWK_MEMCPY (run->awk, val->code, code, SSE_AWK_REX_LEN(code));
return (sse_awk_val_t*)val;
2006-04-24 11:26:00 +00:00
}
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-10-22 12:39:30 +00:00
xp_printf (SSE_T("refdown in map free..."));
2006-10-22 11:34:53 +00:00
sse_awk_printval (v);
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("\n"));
2006-04-18 10:28:03 +00:00
*/
2006-10-22 11:34:53 +00:00
sse_awk_refdownval (run, v);
2006-04-17 16:12:02 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_val_t* sse_awk_makemapval (sse_awk_run_t* run)
2006-04-17 16:12:02 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_val_map_t* val;
2006-04-17 16:12:02 +00:00
2006-10-22 11:34:53 +00:00
val = (sse_awk_val_map_t*) SSE_AWK_MALLOC (
run->awk, sse_sizeof(sse_awk_val_map_t));
if (val == SSE_NULL) return SSE_NULL;
2006-04-17 16:12:02 +00:00
2006-10-22 11:34:53 +00:00
val->type = SSE_AWK_VAL_MAP;
2006-04-17 16:12:02 +00:00
val->ref = 0;
2006-10-22 11:34:53 +00:00
val->map = sse_awk_map_open (
SSE_NULL, run, 256, __free_map_val, run->awk);
if (val->map == SSE_NULL)
2006-04-17 16:12:02 +00:00
{
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE (run->awk, val);
return SSE_NULL;
2006-04-06 16:25:37 +00:00
}
2006-10-22 11:34:53 +00:00
return (sse_awk_val_t*)val;
2006-04-06 16:25:37 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_val_t* sse_awk_makerefval (sse_awk_run_t* run, int id, sse_awk_val_t** adr)
2006-08-20 15:49:48 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_val_ref_t* val;
2006-08-20 15:49:48 +00:00
2006-08-21 02:53:42 +00:00
if (run->fcache_count > 0)
{
val = run->fcache[--run->fcache_count];
}
else
{
2006-10-22 11:34:53 +00:00
val = (sse_awk_val_ref_t*) SSE_AWK_MALLOC (
run->awk, sse_sizeof(sse_awk_val_ref_t));
if (val == SSE_NULL) return SSE_NULL;
2006-08-21 02:53:42 +00:00
}
2006-08-20 15:49:48 +00:00
2006-10-22 11:34:53 +00:00
val->type = SSE_AWK_VAL_REF;
2006-08-20 15:49:48 +00:00
val->ref = 0;
val->id = id;
val->adr = adr;
2006-10-22 11:34:53 +00:00
return (sse_awk_val_t*)val;
2006-08-20 15:49:48 +00:00
}
2006-04-24 11:26:00 +00:00
2006-10-22 11:34:53 +00:00
sse_bool_t sse_awk_isbuiltinval (sse_awk_val_t* val)
2006-03-26 16:36:30 +00:00
{
2006-10-22 11:34:53 +00:00
return val == SSE_NULL ||
val == sse_awk_val_nil || val == sse_awk_val_zls ||
val == sse_awk_val_zero || val == sse_awk_val_one ||
(val >= (sse_awk_val_t*)&__awk_int[0] &&
val <= (sse_awk_val_t*)&__awk_int[sse_countof(__awk_int)-1]);
2006-03-26 16:36:30 +00:00
}
2006-10-22 11:34:53 +00:00
void sse_awk_freeval (sse_awk_run_t* run, sse_awk_val_t* val, sse_bool_t cache)
2006-03-04 15:54:37 +00:00
{
2006-10-22 11:34:53 +00:00
if (sse_awk_isbuiltinval(val)) return;
2006-03-22 16:05:50 +00:00
2006-10-22 12:39:30 +00:00
/*xp_printf (SSE_T("freeing [cache=%d] ... "), cache);
2006-10-22 11:34:53 +00:00
sse_awk_printval (val);
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("\n"));*/
2006-10-22 11:34:53 +00:00
if (val->type == SSE_AWK_VAL_NIL)
2006-03-04 15:54:37 +00:00
{
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE (run->awk, val);
2006-08-20 15:49:48 +00:00
}
2006-10-22 11:34:53 +00:00
else if (val->type == SSE_AWK_VAL_INT)
2006-08-20 15:49:48 +00:00
{
2006-10-22 11:34:53 +00:00
if (cache == sse_true &&
run->icache_count < sse_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-10-22 11:34:53 +00:00
(sse_awk_val_int_t*)val;
2006-03-27 11:43:17 +00:00
}
2006-10-22 11:34:53 +00:00
else SSE_AWK_FREE (run->awk, val);
2006-08-20 15:49:48 +00:00
}
2006-10-22 11:34:53 +00:00
else if (val->type == SSE_AWK_VAL_REAL)
2006-08-20 15:49:48 +00:00
{
2006-10-22 11:34:53 +00:00
if (cache == sse_true &&
run->rcache_count < sse_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-10-22 11:34:53 +00:00
(sse_awk_val_real_t*)val;
2006-03-28 16:33:09 +00:00
}
2006-10-22 11:34:53 +00:00
else SSE_AWK_FREE (run->awk, val);
2006-08-20 15:49:48 +00:00
}
2006-10-22 11:34:53 +00:00
else if (val->type == SSE_AWK_VAL_STR)
2006-08-20 15:49:48 +00:00
{
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE (run->awk, ((sse_awk_val_str_t*)val)->buf);
SSE_AWK_FREE (run->awk, val);
2006-08-20 15:49:48 +00:00
}
2006-10-22 11:34:53 +00:00
else if (val->type == SSE_AWK_VAL_REX)
2006-08-20 15:49:48 +00:00
{
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE (run->awk, ((sse_awk_val_rex_t*)val)->buf);
sse_awk_freerex (run->awk, ((sse_awk_val_rex_t*)val)->code);
SSE_AWK_FREE (run->awk, val);
2006-08-20 15:49:48 +00:00
}
2006-10-22 11:34:53 +00:00
else if (val->type == SSE_AWK_VAL_MAP)
2006-08-20 15:49:48 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_map_close (((sse_awk_val_map_t*)val)->map);
SSE_AWK_FREE (run->awk, val);
2006-03-04 15:54:37 +00:00
}
2006-10-22 11:34:53 +00:00
else if (val->type == SSE_AWK_VAL_REF)
2006-08-20 15:49:48 +00:00
{
2006-10-22 11:34:53 +00:00
if (cache == sse_true &&
run->fcache_count < sse_countof(run->fcache))
2006-08-21 02:53:42 +00:00
{
run->fcache[run->fcache_count++] =
2006-10-22 11:34:53 +00:00
(sse_awk_val_ref_t*)val;
2006-08-21 02:53:42 +00:00
}
2006-10-22 11:34:53 +00:00
else SSE_AWK_FREE (run->awk, val);
2006-08-20 15:49:48 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
sse_awk_assert (run->awk,
2006-10-12 04:17:58 +00:00
!"should never happen - invalid value type");
2006-08-20 15:49:48 +00:00
}
2006-03-04 15:54:37 +00:00
}
2006-10-22 11:34:53 +00:00
void sse_awk_refupval (sse_awk_val_t* val)
2006-03-23 13:26:04 +00:00
{
2006-10-22 11:34:53 +00:00
if (sse_awk_isbuiltinval(val)) return;
2006-03-23 15:36:20 +00:00
/*
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("ref up "));
2006-10-22 11:34:53 +00:00
sse_awk_printval (val);
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("\n"));
2006-03-23 15:36:20 +00:00
*/
2006-03-23 13:26:04 +00:00
val->ref++;
}
2006-10-22 11:34:53 +00:00
void sse_awk_refdownval (sse_awk_run_t* run, sse_awk_val_t* val)
2006-03-23 13:26:04 +00:00
{
2006-10-22 11:34:53 +00:00
if (sse_awk_isbuiltinval(val)) return;
2006-03-23 15:36:20 +00:00
/*
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("%p, %p, %p\n"), sse_awk_val_nil, &__awk_nil, val);
xp_printf (SSE_T("ref down [count=>%d]\n"), (int)val->ref);
2006-10-22 11:34:53 +00:00
sse_awk_printval (val);
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("\n"));
2006-03-23 15:36:20 +00:00
*/
2006-03-23 13:26:04 +00:00
2006-10-22 11:34:53 +00:00
sse_awk_assert (run->awk, val->ref > 0);
2006-03-23 13:26:04 +00:00
val->ref--;
2006-03-26 16:36:30 +00:00
if (val->ref <= 0)
{
2006-07-06 15:54:41 +00:00
/*
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("**FREEING ["));
2006-10-22 11:34:53 +00:00
sse_awk_printval (val);
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("]\n"));
2006-07-06 15:54:41 +00:00
*/
2006-10-22 11:34:53 +00:00
sse_awk_freeval(run, val, sse_true);
2006-03-26 16:36:30 +00:00
}
}
2006-10-22 11:34:53 +00:00
void sse_awk_refdownval_nofree (sse_awk_run_t* run, sse_awk_val_t* val)
2006-03-26 16:36:30 +00:00
{
2006-10-22 11:34:53 +00:00
if (sse_awk_isbuiltinval(val)) return;
2006-03-26 16:36:30 +00:00
2006-10-22 11:34:53 +00:00
sse_awk_assert (run->awk, val->ref > 0);
2006-03-26 16:36:30 +00:00
val->ref--;
2006-03-23 13:26:04 +00:00
}
2006-10-22 11:34:53 +00:00
sse_bool_t sse_awk_valtobool (sse_awk_run_t* run, sse_awk_val_t* val)
2006-03-15 15:34:59 +00:00
{
2006-10-22 11:34:53 +00:00
if (val == SSE_NULL) return sse_false;
2006-03-15 15:34:59 +00:00
switch (val->type)
{
2006-10-22 11:34:53 +00:00
case SSE_AWK_VAL_NIL:
return sse_false;
case SSE_AWK_VAL_INT:
return ((sse_awk_val_int_t*)val)->val != 0;
case SSE_AWK_VAL_REAL:
return ((sse_awk_val_real_t*)val)->val != 0.0;
case SSE_AWK_VAL_STR:
return ((sse_awk_val_str_t*)val)->len > 0;
case SSE_AWK_VAL_REX: /* TODO: is this correct? */
return ((sse_awk_val_rex_t*)val)->len > 0;
case SSE_AWK_VAL_MAP:
return sse_false; /* TODO: is this correct? */
case SSE_AWK_VAL_REF:
return sse_false; /* TODO: is this correct? */
}
sse_awk_assert (run->awk, !"should never happen - invalid value type");
return sse_false;
2006-03-15 15:34:59 +00:00
}
2006-10-22 11:34:53 +00:00
sse_char_t* sse_awk_valtostr (
sse_awk_run_t* run, sse_awk_val_t* v,
int opt, sse_awk_str_t* buf, sse_size_t* len)
2006-06-21 11:45:26 +00:00
{
2006-10-22 11:34:53 +00:00
if (v->type == SSE_AWK_VAL_NIL)
2006-06-28 08:56:59 +00:00
{
2006-10-22 11:34:53 +00:00
return __str_to_str (run, SSE_T(""), 0, opt, buf, len);
2006-09-27 14:14:47 +00:00
}
2006-07-13 03:10:35 +00:00
2006-10-22 11:34:53 +00:00
if (v->type == SSE_AWK_VAL_INT)
2006-09-27 14:14:47 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_val_int_t* vi = (sse_awk_val_int_t*)v;
2006-09-27 14:14:47 +00:00
2006-10-05 14:20:57 +00:00
/*
2006-10-22 11:34:53 +00:00
if (vi->nde != SSE_NULL && vi->nde->str != SSE_NULL)
2006-09-27 14:14:47 +00:00
{
return __str_to_str (
run, vi->nde->str, vi->nde->len,
2006-10-11 15:02:26 +00:00
opt, buf, len);
2006-06-28 08:56:59 +00:00
}
else
{
2006-10-05 14:20:57 +00:00
*/
2006-10-11 15:02:26 +00:00
return __val_int_to_str (run, vi, opt, 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-10-22 11:34:53 +00:00
if (v->type == SSE_AWK_VAL_REAL)
2006-09-27 14:14:47 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_val_real_t* vr = (sse_awk_val_real_t*)v;
2006-09-27 14:14:47 +00:00
2006-10-05 14:24:38 +00:00
/*
2006-10-22 11:34:53 +00:00
if (vr->nde != SSE_NULL && vr->nde->str != SSE_NULL)
2006-09-27 14:14:47 +00:00
{
return __str_to_str (
run, vr->nde->str, vr->nde->len,
2006-10-11 15:02:26 +00:00
opt, buf, len);
2006-09-27 14:14:47 +00:00
}
else
2006-10-05 14:24:38 +00:00
{*/
2006-10-11 15:02:26 +00:00
return __val_real_to_str (run, vr, opt, buf, len);
2006-10-05 14:24:38 +00:00
/*}*/
2006-06-28 08:56:59 +00:00
}
2006-10-22 11:34:53 +00:00
if (v->type == SSE_AWK_VAL_STR)
2006-09-27 14:14:47 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_val_str_t* vs = (sse_awk_val_str_t*)v;
2006-09-27 14:14:47 +00:00
return __str_to_str (
2006-10-11 15:02:26 +00:00
run, vs->buf, vs->len, opt, buf, len);
2006-09-27 14:14:47 +00:00
}
/* TODO: process more value types */
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("*** ERROR: WRONG VALUE TYPE [%d] in sse_awk_valtostr v=> %p***\n"), v->type, v);
2006-10-22 11:34:53 +00:00
run->errnum = SSE_AWK_EVALTYPE;
return SSE_NULL;
2006-09-27 14:14:47 +00:00
}
2006-10-22 11:34:53 +00:00
static sse_char_t* __str_to_str (
sse_awk_run_t* run, const sse_char_t* str, sse_size_t str_len,
int opt, sse_awk_str_t* buf, sse_size_t* len)
2006-09-27 14:14:47 +00:00
{
2006-10-22 11:34:53 +00:00
if (buf == SSE_NULL)
2006-06-21 11:45:26 +00:00
{
2006-10-22 11:34:53 +00:00
sse_char_t* tmp;
tmp = sse_awk_strxdup (run->awk, str, str_len);
if (tmp == SSE_NULL)
2006-09-27 14:14:47 +00:00
{
2006-10-22 11:34:53 +00:00
run->errnum = SSE_AWK_ENOMEM;
return SSE_NULL;
2006-09-27 14:14:47 +00:00
}
2006-10-22 11:34:53 +00:00
if (len != SSE_NULL) *len = str_len;
2006-09-27 14:14:47 +00:00
return tmp;
}
else
{
2006-10-22 11:34:53 +00:00
sse_size_t n;
2006-06-21 11:45:26 +00:00
2006-10-22 11:34:53 +00:00
if (opt & SSE_AWK_VALTOSTR_CLEAR) sse_awk_str_clear (buf);
n = sse_awk_str_ncat (buf, str, str_len);
if (n == (sse_size_t)-1)
2006-06-21 11:45:26 +00:00
{
2006-10-22 11:34:53 +00:00
run->errnum = SSE_AWK_ENOMEM;
return SSE_NULL;
2006-06-21 11:45:26 +00:00
}
2006-10-22 11:34:53 +00:00
if (len != SSE_NULL) *len = SSE_AWK_STR_LEN(buf);
return SSE_AWK_STR_BUF(buf);
2006-09-27 14:14:47 +00:00
}
}
2006-10-22 11:34:53 +00:00
static sse_char_t* __val_int_to_str (
sse_awk_run_t* run, sse_awk_val_int_t* v,
int opt, sse_awk_str_t* buf, sse_size_t* len)
2006-09-27 14:14:47 +00:00
{
2006-10-22 11:34:53 +00:00
sse_char_t* tmp;
sse_long_t t;
sse_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-10-22 11:34:53 +00:00
if (buf == SSE_NULL)
2006-06-21 11:45:26 +00:00
{
2006-10-22 11:34:53 +00:00
tmp = SSE_AWK_MALLOC (
run->awk, 2 * sse_sizeof(sse_char_t));
if (tmp == SSE_NULL)
2006-06-21 11:45:26 +00:00
{
2006-10-22 11:34:53 +00:00
run->errnum = SSE_AWK_ENOMEM;
return SSE_NULL;
2006-06-21 11:45:26 +00:00
}
2006-10-22 11:34:53 +00:00
tmp[0] = SSE_T('0');
tmp[1] = SSE_T('\0');
if (len != SSE_NULL) *len = 1;
2006-09-27 14:14:47 +00:00
return tmp;
2006-06-21 11:45:26 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
if (opt & SSE_AWK_VALTOSTR_CLEAR) sse_awk_str_clear (buf);
if (sse_awk_str_cat (buf, SSE_T("0")) == (sse_size_t)-1)
2006-06-21 11:45:26 +00:00
{
2006-10-22 11:34:53 +00:00
run->errnum = SSE_AWK_ENOMEM;
return SSE_NULL;
2006-06-21 11:45:26 +00:00
}
2006-09-27 14:14:47 +00:00
2006-10-22 11:34:53 +00:00
if (len != SSE_NULL) *len = SSE_AWK_STR_LEN(buf);
return SSE_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-10-22 11:34:53 +00:00
if (buf == SSE_NULL)
2006-09-27 14:14:47 +00:00
{
2006-10-22 11:34:53 +00:00
tmp = SSE_AWK_MALLOC (
run->awk, (l + 1) * sse_sizeof(sse_char_t));
if (tmp == SSE_NULL)
2006-06-21 11:45:26 +00:00
{
2006-10-22 11:34:53 +00:00
run->errnum = SSE_AWK_ENOMEM;
return SSE_NULL;
2006-06-21 11:45:26 +00:00
}
2006-10-22 11:34:53 +00:00
tmp[l] = SSE_T('\0');
if (len != SSE_NULL) *len = l;
2006-09-27 14:14:47 +00:00
}
else
{
/* clear the buffer */
2006-10-22 11:34:53 +00:00
if (opt & SSE_AWK_VALTOSTR_CLEAR) sse_awk_str_clear (buf);
2006-09-27 14:14:47 +00:00
2006-10-22 11:34:53 +00:00
tmp = SSE_AWK_STR_BUF(buf) + SSE_AWK_STR_LEN(buf);
2006-06-21 11:45:26 +00:00
2006-09-27 14:14:47 +00:00
/* extend the buffer */
2006-10-22 11:34:53 +00:00
if (sse_awk_str_nccat (
buf, SSE_T(' '), l) == (sse_size_t)-1)
2006-08-03 05:05:48 +00:00
{
2006-10-22 11:34:53 +00:00
run->errnum = SSE_AWK_ENOMEM;
return SSE_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-10-22 11:34:53 +00:00
tmp[--l] = (sse_char_t)(t % 10) + SSE_T('0');
2006-09-27 14:14:47 +00:00
t /= 10;
}
2006-07-17 06:21:39 +00:00
2006-10-22 11:34:53 +00:00
if (v->val < 0) tmp[--l] = SSE_T('-');
2006-08-03 05:05:48 +00:00
2006-10-22 11:34:53 +00:00
if (buf != SSE_NULL)
2006-09-27 14:14:47 +00:00
{
2006-10-22 11:34:53 +00:00
tmp = SSE_AWK_STR_BUF(buf);
if (len != SSE_NULL) *len = SSE_AWK_STR_LEN(buf);
2006-09-27 14:14:47 +00:00
}
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-10-22 11:34:53 +00:00
static sse_char_t* __val_real_to_str (
sse_awk_run_t* run, sse_awk_val_real_t* v,
int opt, sse_awk_str_t* buf, sse_size_t* len)
2006-09-27 14:14:47 +00:00
{
/* TODO: change the code */
2006-10-22 11:34:53 +00:00
sse_char_t tbuf[256], * tmp;
2006-09-27 14:14:47 +00:00
2006-10-22 11:34:53 +00:00
tmp = (opt & SSE_AWK_VALTOSTR_PRINT)?
2006-10-11 15:02:26 +00:00
run->global.ofmt.ptr: run->global.convfmt.ptr;
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-10-11 15:02:26 +00:00
/* TODO: does it need to check if a null character is included in convfmt??? */
2006-10-22 11:34:53 +00:00
run->awk->syscas.sprintf (tbuf, sse_countof(tbuf), tmp, (double)v->val);
if (buf == SSE_NULL)
2006-06-21 11:45:26 +00:00
{
2006-10-22 11:34:53 +00:00
tmp = sse_awk_strdup (run->awk, tbuf);
if (tmp == SSE_NULL)
2006-06-21 11:45:26 +00:00
{
2006-10-22 11:34:53 +00:00
run->errnum = SSE_AWK_ENOMEM;
return SSE_NULL;
2006-06-21 11:45:26 +00:00
}
2006-10-22 11:34:53 +00:00
if (len != SSE_NULL) *len = sse_awk_strlen(tmp);
2006-09-27 14:14:47 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
if (opt & SSE_AWK_VALTOSTR_CLEAR) sse_awk_str_clear (buf);
2006-08-03 05:05:48 +00:00
2006-10-22 11:34:53 +00:00
if (sse_awk_str_cat (buf, tbuf) == (sse_size_t)-1)
2006-09-27 14:14:47 +00:00
{
2006-10-22 11:34:53 +00:00
run->errnum = SSE_AWK_ENOMEM;
return SSE_NULL;
2006-06-21 11:45:26 +00:00
}
2006-10-22 11:34:53 +00:00
tmp = SSE_AWK_STR_BUF(buf);
if (len != SSE_NULL) *len = SSE_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-10-22 11:34:53 +00:00
int sse_awk_valtonum (
sse_awk_run_t* run, sse_awk_val_t* v, sse_long_t* l, sse_real_t* r)
2006-07-17 04:17:40 +00:00
{
2006-10-22 11:34:53 +00:00
if (v->type == SSE_AWK_VAL_NIL)
2006-07-17 04:17:40 +00:00
{
*l = 0;
return 0;
}
2006-10-22 11:34:53 +00:00
if (v->type == SSE_AWK_VAL_INT)
2006-07-17 04:17:40 +00:00
{
2006-10-22 11:34:53 +00:00
*l = ((sse_awk_val_int_t*)v)->val;
2006-07-17 04:17:40 +00:00
return 0; /* long */
}
2006-10-22 11:34:53 +00:00
if (v->type == SSE_AWK_VAL_REAL)
2006-07-17 04:17:40 +00:00
{
2006-10-22 11:34:53 +00:00
*r = ((sse_awk_val_real_t*)v)->val;
2006-07-17 04:17:40 +00:00
return 1; /* real */
}
2006-10-22 11:34:53 +00:00
if (v->type == SSE_AWK_VAL_STR)
2006-07-17 04:17:40 +00:00
{
2006-10-22 11:34:53 +00:00
const sse_char_t* endptr;
2006-07-17 04:17:40 +00:00
2006-10-22 11:34:53 +00:00
*l = sse_awk_strxtolong (run->awk,
((sse_awk_val_str_t*)v)->buf,
((sse_awk_val_str_t*)v)->len, 0, &endptr);
if (*endptr == SSE_T('.') ||
*endptr == SSE_T('E') ||
*endptr == SSE_T('e'))
2006-07-17 04:17:40 +00:00
{
2006-10-22 11:34:53 +00:00
*r = sse_awk_strxtoreal (run->awk,
((sse_awk_val_str_t*)v)->buf,
((sse_awk_val_str_t*)v)->len, SSE_NULL);
2006-10-06 03:37:40 +00:00
/* 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 */
}
2006-10-08 05:46:41 +00:00
/* TODO: do should i handle strings ending with invalid number characters like "123xx" or "dkdkdkd"? */
2006-07-17 04:17:40 +00:00
return 0; /* long */
}
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("*** ERROR: WRONG VALUE TYPE [%d] in sse_awk_valtonum v=> %p***\n"), v->type, v);
2006-10-22 11:34:53 +00:00
run->errnum = SSE_AWK_EVALTYPE;
2006-07-17 04:17:40 +00:00
return -1; /* error */
}
2006-10-22 11:34:53 +00:00
static int __print_pair (sse_awk_pair_t* pair, void* arg)
2006-04-20 05:44:29 +00:00
{
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T(" %s=>"), pair->key);
2006-10-22 11:34:53 +00:00
sse_awk_printval (pair->val);
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T(" "));
2006-04-20 05:44:29 +00:00
return 0;
}
2006-10-22 11:34:53 +00:00
void sse_awk_printval (sse_awk_val_t* val)
2006-03-04 15:54:37 +00:00
{
2006-04-14 10:56:42 +00:00
/* TODO: better value printing...................... */
2006-03-04 15:54:37 +00:00
switch (val->type)
{
2006-10-22 11:34:53 +00:00
case SSE_AWK_VAL_NIL:
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("nil"));
2006-03-07 15:55:14 +00:00
break;
2006-10-22 11:34:53 +00:00
case SSE_AWK_VAL_INT:
2006-03-15 15:34:59 +00:00
#if defined(__LCC__)
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("%lld"),
2006-10-22 11:34:53 +00:00
(long long)((sse_awk_val_int_t*)val)->val);
2006-03-15 15:34:59 +00:00
#elif defined(__BORLANDC__) || defined(_MSC_VER)
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("%I64d"),
2006-10-22 11:34:53 +00:00
(__int64)((sse_awk_nde_int_t*)val)->val);
2006-04-14 10:56:42 +00:00
#elif defined(vax) || defined(__vax) || defined(_SCO_DS)
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("%ld"),
2006-10-22 11:34:53 +00:00
(long)((sse_awk_val_int_t*)val)->val);
2006-03-14 16:40:00 +00:00
#else
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("%lld"),
2006-10-22 11:34:53 +00:00
(long long)((sse_awk_val_int_t*)val)->val);
2006-03-14 16:40:00 +00:00
#endif
2006-03-04 15:54:37 +00:00
break;
2006-10-22 11:34:53 +00:00
case SSE_AWK_VAL_REAL:
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("%Lf"),
2006-10-22 11:34:53 +00:00
(long double)((sse_awk_val_real_t*)val)->val);
2006-03-04 15:54:37 +00:00
break;
2006-10-22 11:34:53 +00:00
case SSE_AWK_VAL_STR:
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("%s"), ((sse_awk_val_str_t*)val)->buf);
2006-03-04 15:54:37 +00:00
break;
2006-10-22 11:34:53 +00:00
case SSE_AWK_VAL_REX:
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("REX[%s]"), ((sse_awk_val_rex_t*)val)->buf);
2006-04-24 11:26:00 +00:00
break;
2006-10-22 11:34:53 +00:00
case SSE_AWK_VAL_MAP:
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("MAP["));
2006-10-22 11:34:53 +00:00
sse_awk_map_walk (((sse_awk_val_map_t*)val)->map, __print_pair, SSE_NULL);
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("]"));
2006-04-17 16:12:02 +00:00
break;
2006-08-20 15:49:48 +00:00
2006-10-22 11:34:53 +00:00
case SSE_AWK_VAL_REF:
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("REF[id=%d,val="), ((sse_awk_val_ref_t*)val)->id);
2006-10-22 11:34:53 +00:00
sse_awk_printval (*((sse_awk_val_ref_t*)val)->adr);
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("]"));
2006-08-20 15:49:48 +00:00
break;
2006-04-17 16:12:02 +00:00
2006-03-04 15:54:37 +00:00
default:
2006-10-22 12:39:30 +00:00
xp_printf (SSE_T("**** INTERNAL ERROR - INVALID VALUE TYPE ****\n"));
2006-03-04 15:54:37 +00:00
}
}