From 4b5372cc1a202134af132a7be19996d2331b0da6 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Thu, 17 Aug 2006 03:49:30 +0000 Subject: [PATCH] *** empty log message *** --- ase/awk/func.c | 109 +++++++++++++++++++++++++++++++++++++++---- ase/test/awk/t28.awk | 6 +++ 2 files changed, 106 insertions(+), 9 deletions(-) diff --git a/ase/awk/func.c b/ase/awk/func.c index b89902e9..411f4c99 100644 --- a/ase/awk/func.c +++ b/ase/awk/func.c @@ -1,5 +1,5 @@ /* - * $Id: func.c,v 1.18 2006-08-13 16:04:32 bacon Exp $ + * $Id: func.c,v 1.19 2006-08-17 03:49:29 bacon Exp $ */ #include @@ -24,6 +24,8 @@ static int __bfn_index (xp_awk_t* awk, void* run); static int __bfn_length (xp_awk_t* awk, void* run); static int __bfn_substr (xp_awk_t* awk, void* run); static int __bfn_split (xp_awk_t* awk, void* run); +static int __bfn_tolower (xp_awk_t* awk, void* run); +static int __bfn_toupper (xp_awk_t* awk, void* run); static int __bfn_system (xp_awk_t* awk, void* run); static int __bfn_sin (xp_awk_t* awk, void* run); @@ -31,17 +33,20 @@ static int __bfn_sin (xp_awk_t* awk, void* run); static xp_awk_bfn_t __sys_bfn[] = { /* ensure that this matches XP_AWK_BNF_XXX in func.h */ - { XP_T("close"), 5, XP_AWK_EXTIO, 1, 1, XP_NULL, __bfn_close }, + { XP_T("close"), 5, XP_AWK_EXTIO, 1, 1, XP_NULL, __bfn_close }, - { XP_T("index"), 5, 0, 2, 2, XP_NULL, __bfn_index }, - { XP_T("length"), 6, 0, 1, 1, XP_NULL, __bfn_length }, - { XP_T("substr"), 6, 0, 2, 3, XP_NULL, __bfn_substr }, - { XP_T("split"), 5, 0, 2, 3, XP_NULL, __bfn_split }, + /* string functions */ + { XP_T("index"), 5, 0, 2, 2, XP_NULL, __bfn_index }, + { XP_T("length"), 6, 0, 1, 1, XP_NULL, __bfn_length }, + { XP_T("substr"), 6, 0, 2, 3, XP_NULL, __bfn_substr }, + { XP_T("split"), 5, 0, 2, 3, XP_NULL, __bfn_split }, + { XP_T("tolower"), 7, 0, 1, 1, XP_NULL, __bfn_tolower }, + { XP_T("toupper"), 7, 0, 1, 1, XP_NULL, __bfn_toupper }, - { XP_T("system"), 6, 0, 1, 1, XP_NULL, __bfn_system }, - { XP_T("sin"), 3, 0, 1, 1, XP_NULL, __bfn_sin }, + { XP_T("system"), 6, 0, 1, 1, XP_NULL, __bfn_system }, + { XP_T("sin"), 3, 0, 1, 1, XP_NULL, __bfn_sin }, - { XP_NULL, 0, 0, 0, 0, XP_NULL, XP_NULL } + { XP_NULL, 0, 0, 0, 0, XP_NULL, XP_NULL } }; xp_awk_bfn_t* xp_awk_addbfn ( @@ -425,6 +430,92 @@ static int __bfn_split (xp_awk_t* awk, void* run) return 0; } +static int __bfn_tolower (xp_awk_t* awk, void* run) +{ + xp_size_t nargs; + xp_char_t* str; + xp_size_t len, i; + xp_awk_val_t* a0; + int errnum; + + nargs = xp_awk_getnargs (run); + xp_assert (nargs == 1); + + a0 = xp_awk_getarg (run, 0); + + if (a0->type == XP_AWK_VAL_STR) + { + str = ((xp_awk_val_str_t*)a0)->buf; + len = ((xp_awk_val_str_t*)a0)->len; + } + else + { + str = xp_awk_valtostr (a0, &errnum, xp_true, XP_NULL, &len); + if (str == XP_NULL) + { + xp_awk_seterrnum (run, errnum); + return -1; + } + } + + for (i = 0; i < len; i++) str[i] = xp_tolower(str[i]); + + a0 = xp_awk_makestrval (str, len); + if (a0 == XP_NULL) + { + if (a0->type != XP_AWK_VAL_STR) xp_free (str); + xp_awk_seterrnum (run, XP_AWK_ENOMEM); + return -1; + } + + if (a0->type != XP_AWK_VAL_STR) xp_free (str); + xp_awk_setretval (run, a0); + return 0; +} + +static int __bfn_toupper (xp_awk_t* awk, void* run) +{ + xp_size_t nargs; + xp_char_t* str; + xp_size_t len, i; + xp_awk_val_t* a0; + int errnum; + + nargs = xp_awk_getnargs (run); + xp_assert (nargs == 1); + + a0 = xp_awk_getarg (run, 0); + + if (a0->type == XP_AWK_VAL_STR) + { + str = ((xp_awk_val_str_t*)a0)->buf; + len = ((xp_awk_val_str_t*)a0)->len; + } + else + { + str = xp_awk_valtostr (a0, &errnum, xp_true, XP_NULL, &len); + if (str == XP_NULL) + { + xp_awk_seterrnum (run, errnum); + return -1; + } + } + + for (i = 0; i < len; i++) str[i] = xp_toupper(str[i]); + + a0 = xp_awk_makestrval (str, len); + if (a0 == XP_NULL) + { + if (a0->type != XP_AWK_VAL_STR) xp_free (str); + xp_awk_seterrnum (run, XP_AWK_ENOMEM); + return -1; + } + + if (a0->type != XP_AWK_VAL_STR) xp_free (str); + xp_awk_setretval (run, a0); + return 0; +} + static int __bfn_system (xp_awk_t* awk, void* run) { xp_size_t nargs; diff --git a/ase/test/awk/t28.awk b/ase/test/awk/t28.awk index 5ccfeaa9..034b6686 100644 --- a/ase/test/awk/t28.awk +++ b/ase/test/awk/t28.awk @@ -12,6 +12,12 @@ END { print substr ("abc", "3", 5); print substr ("abc", 4, 5); + /* a[1] = 20; substr (a, 3, 4); + */ + + + print tolower ("AbcDEF"); + print toupper ("AbcDEF"); }