diff --git a/qse/cmd/awk/awk.c b/qse/cmd/awk/awk.c index 72e365b1..dd3aba59 100644 --- a/qse/cmd/awk/awk.c +++ b/qse/cmd/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c 456 2011-05-12 14:55:53Z hyunghwan.chung $ + * $Id: awk.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -405,7 +405,7 @@ struct opttab_t { QSE_T("ncmponstr"), QSE_AWK_NCMPONSTR, QSE_T("perform numeric comparsion on numeric strings") }, { QSE_T("strictnaming"), QSE_AWK_STRICTNAMING, QSE_T("enable the strict naming rule") }, { QSE_T("include"), QSE_AWK_INCLUDE, QSE_T("enable 'include'") }, - { QSE_NULL, 0 } + { QSE_NULL, 0, QSE_NULL } }; static void print_usage (QSE_FILE* out, const qse_char_t* argv0) @@ -461,7 +461,8 @@ static int comparg (int argc, qse_char_t* argv[], struct arg_t* arg) { QSE_T(":assign"), QSE_T('v') }, { QSE_T(":memory-limit"), QSE_T('m') }, - { QSE_T("help"), QSE_T('h') } + { QSE_T("help"), QSE_T('h') }, + { QSE_NULL, QSE_T('\0') } }; static qse_opt_t opt = diff --git a/qse/include/qse/awk/awk.h b/qse/include/qse/awk/awk.h index 2a9ba7f3..b4f73055 100644 --- a/qse/include/qse/awk/awk.h +++ b/qse/include/qse/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h 459 2011-05-17 14:37:51Z hyunghwan.chung $ + * $Id: awk.h 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -201,8 +201,7 @@ typedef struct qse_awk_val_real_t qse_awk_val_real_t; struct qse_awk_val_str_t { QSE_AWK_VAL_HDR; - qse_char_t* ptr; - qse_size_t len; + qse_xstr_t val; }; typedef struct qse_awk_val_str_t qse_awk_val_str_t; @@ -355,6 +354,14 @@ struct qse_awk_nde_t QSE_AWK_NDE_HDR; }; +typedef int (*qse_awk_sprintf_t) ( + qse_awk_t* awk, + qse_char_t* buf, + qse_size_t size, + const qse_char_t* fmt, + ... +); + typedef qse_real_t (*qse_awk_math1_t) ( qse_awk_t* awk, qse_real_t x @@ -366,18 +373,31 @@ typedef qse_real_t (*qse_awk_math2_t) ( qse_real_t y ); -typedef qse_real_t (*qse_awk_pow_t) ( - qse_awk_t* awk, - qse_real_t x, - qse_real_t y + +typedef void* (*qse_awk_buildrex_t) ( + qse_awk_t* awk, + const qse_char_t* ptn, + qse_size_t len ); -typedef int (*qse_awk_sprintf_t) ( - qse_awk_t* awk, - qse_char_t* buf, - qse_size_t size, - const qse_char_t* fmt, - ... +typedef int (*qse_awk_matchrex_t) ( + qse_awk_t* awk, + void* code, + int option, + const qse_char_t* str, + qse_size_t len, + const qse_char_t** mptr, + qse_size_t* mlen +); + +typedef void (*qse_awk_freerex_t) ( + qse_awk_t* awk, + void* code +); + +typedef qse_bool_t (*qse_awk_isemptyrex_t) ( + qse_awk_t* awk, + void* code ); /** @@ -564,33 +584,10 @@ struct qse_awk_prm_t struct { /* TODO: accept regular expression handling functions */ - void* (*build) ( - qse_awk_t* awk, - const qse_char_t* ptn, - qse_size_t len, - int* errnum - ); - - int (*match) ( - qse_awk_t* awk, - void* code, - int option, - const qse_char_t* str, - qse_size_t len, - const qse_char_t** mptr, - qse_size_t* mlen, - int* errnum - ); - - void (*free) ( - qse_awk_t* awk, - void* code - ); - - qse_bool_t (*isempty) ( - qse_awk_t* awk, - void* code - ); + qse_awk_buildrex_t build; + qse_awk_matchrex_t match; + qse_awk_freerex_t free; + qse_awk_isemptyrex_t isempty; } rex; #endif }; @@ -723,11 +720,11 @@ enum qse_awk_option_t QSE_AWK_NEWLINE = (1 << 5), /** - * strips off leading and trailing spaces when splitting a record - * into fields with a regular expression. + * remove empty fields when splitting a record if FS is a regular + * expression and the match is all spaces. * * @code - * BEGIN { FS="[:[:space:]]+"; } + * BEGIN { FS="[[:space:]]+"; } * { * print "NF=" NF; * for (i = 0; i < NF; i++) print i " [" $(i+1) "]"; @@ -735,6 +732,17 @@ enum qse_awk_option_t * @endcode * " a b c " is split to [a], [b], [c] if #QSE_AWK_STRIPRECSPC is on. * Otherwise, it is split to [], [a], [b], [c], []. + * + * @code + * BEGIN { + * n=split(" o my god ", x, /[ o]+/); + * for (i=1;i<=n;i++) print "[" x[i] "]"; + * } + * @endcode + * The above example splits the string to [], [my], [g], [d] + * if #QSE_AWK_STRIPRECSPC is on. Otherwise, it results in + * [], [my], [g], [d], []. Note that the first empty field is not + * removed as the field separator is not all spaces. (space + 'o'). */ QSE_AWK_STRIPRECSPC = (1 << 6), diff --git a/qse/include/qse/cmn/rex.h b/qse/include/qse/cmn/rex.h index eb723a7c..b8e94c83 100644 --- a/qse/include/qse/cmn/rex.h +++ b/qse/include/qse/cmn/rex.h @@ -1,5 +1,5 @@ /* - * $Id: rex.h 441 2011-04-22 14:28:43Z hyunghwan.chung $ + * $Id: rex.h 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -264,9 +264,9 @@ qse_rex_node_t* qse_rex_comp ( ); int qse_rex_exec ( - qse_rex_t* rex, + qse_rex_t* rex, const qse_cstr_t* str, - const qse_cstr_t* substr, + const qse_cstr_t* substr, qse_cstr_t* matstr ); @@ -285,10 +285,8 @@ int qse_matchrex ( qse_size_t depth, void* code, int option, - const qse_char_t* str, - qse_size_t len, - const qse_char_t* substr, - qse_size_t sublen, + const qse_cstr_t* str, + const qse_cstr_t* substr, qse_cstr_t* match, qse_rex_errnum_t* errnum ); diff --git a/qse/include/qse/cmn/str.h b/qse/include/qse/cmn/str.h index fa9e4f25..ed780a48 100644 --- a/qse/include/qse/cmn/str.h +++ b/qse/include/qse/cmn/str.h @@ -1,5 +1,5 @@ /* - * $Id: str.h 446 2011-04-30 15:24:38Z hyunghwan.chung $ + * $Id: str.h 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -31,17 +31,19 @@ * deal with a string pointer and length in a structure. */ -#define QSE_MBS_LEN(s) ((s)->len) /**< string length */ -#define QSE_MBS_PTR(s) ((s)->ptr) /**< string buffer pointer */ +#define QSE_MBS_XSTR(s) (&((s)->val)) /**< string pointer and length as a aggregate */ +#define QSE_MBS_LEN(s) ((s)->val.len) /**< string length */ +#define QSE_MBS_PTR(s) ((s)->val.ptr) /**< string buffer pointer */ #define QSE_MBS_CAPA(s) ((s)->capa) /**< string buffer capacity */ -#define QSE_MBS_CHAR(s,idx) ((s)->ptr[idx]) /**< character at given position */ -#define QSE_MBS_LASTCHAR(s) ((s)->ptr[(s)->len-1]) /**< last character. unsafe if length <= 0 */ +#define QSE_MBS_CHAR(s,idx) ((s)->val.ptr[idx]) /**< character at given position */ +#define QSE_MBS_LASTCHAR(s) ((s)->val.ptr[(s)->val.len-1]) /**< last character. unsafe if length <= 0 */ -#define QSE_WCS_LEN(s) ((s)->len) /**< string buffer length */ -#define QSE_WCS_PTR(s) ((s)->ptr) /**< string buffer pointer */ +#define QSE_WCS_XSTR(s) (&((s)->val)) /**< string pointer and length as an aggregate*/ +#define QSE_WCS_LEN(s) ((s)->val.len) /**< string buffer length */ +#define QSE_WCS_PTR(s) ((s)->val.ptr) /**< string buffer pointer */ #define QSE_WCS_CAPA(s) ((s)->capa) /**< string buffer capacity */ -#define QSE_WCS_CHAR(s,idx) ((s)->ptr[idx]) /**< character at given position */ -#define QSE_WCS_LASTCHAR(s) ((s)->ptr[(s)->len-1]) /**< last character. unsafe if length <= 0 */ +#define QSE_WCS_CHAR(s,idx) ((s)->val.ptr[idx]) /**< character at given position */ +#define QSE_WCS_LASTCHAR(s) ((s)->val.ptr[(s)->val.len-1]) /**< last character. unsafe if length <= 0 */ typedef struct qse_mbs_t qse_mbs_t; typedef struct qse_wcs_t qse_wcs_t; @@ -57,6 +59,7 @@ typedef qse_size_t (*qse_wcs_sizer_t) ( ); #ifdef QSE_CHAR_IS_MCHAR +# define QSE_STR_XSTR(s) ((qse_xstr_t*)QSE_MBS_XSTR(s)) # define QSE_STR_LEN(s) QSE_MBS_LEN(s) # define QSE_STR_PTR(s) QSE_MBS_PTR(s) # define QSE_STR_CAPA(s) QSE_MBS_CAPA(s) @@ -65,6 +68,7 @@ typedef qse_size_t (*qse_wcs_sizer_t) ( # define qse_str_t qse_mbs_t # define qse_str_sizer_t qse_mbs_sizer_t #else +# define QSE_STR_XSTR(s) ((qse_xstr_t*)QSE_WCS_XSTR(s)) # define QSE_STR_LEN(s) QSE_WCS_LEN(s) # define QSE_STR_PTR(s) QSE_WCS_PTR(s) # define QSE_STR_CAPA(s) QSE_WCS_CAPA(s) @@ -82,8 +86,7 @@ struct qse_mbs_t { QSE_DEFINE_COMMON_FIELDS (mbs) qse_mbs_sizer_t sizer; /**< buffer resizer function */ - qse_mchar_t* ptr; /**< buffer/string pointer */ - qse_size_t len; /**< string length */ + qse_mxstr_t val; /**< buffer/string pointer and lengh */ qse_size_t capa; /**< buffer capacity */ }; @@ -94,8 +97,7 @@ struct qse_wcs_t { QSE_DEFINE_COMMON_FIELDS (wcs) qse_wcs_sizer_t sizer; /**< buffer resizer function */ - qse_wchar_t* ptr; /**< buffer/string pointer */ - qse_size_t len; /**< string length */ + qse_wxstr_t val; /**< buffer/string pointer and lengh */ qse_size_t capa; /**< buffer capacity */ }; diff --git a/qse/lib/awk/Awk.cpp b/qse/lib/awk/Awk.cpp index 88c9cf3e..fc1e6cb3 100644 --- a/qse/lib/awk/Awk.cpp +++ b/qse/lib/awk/Awk.cpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.cpp 460 2011-05-17 14:56:54Z hyunghwan.chung $ + * $Id: Awk.cpp 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -432,8 +432,8 @@ int Awk::Value::getStr (const char_t** str, size_t* len) const { if (val->type == QSE_AWK_VAL_STR) { - p = ((qse_awk_val_str_t*)val)->ptr; - l = ((qse_awk_val_str_t*)val)->len; + p = ((qse_awk_val_str_t*)val)->val.ptr; + l = ((qse_awk_val_str_t*)val)->val.len; } else { diff --git a/qse/lib/awk/awk.h b/qse/lib/awk/awk.h index e1e5b17e..302f2649 100644 --- a/qse/lib/awk/awk.h +++ b/qse/lib/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h 441 2011-04-22 14:28:43Z hyunghwan.chung $ + * $Id: awk.h 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -296,9 +296,9 @@ struct qse_awk_rtx_t qse_size_t nflds; /* NF */ struct { - qse_char_t* ptr; - qse_size_t len; - qse_awk_val_t* val; /* $1 .. $NF */ + const qse_char_t* ptr; + qse_size_t len; + qse_awk_val_t* val; /* $1 .. $NF */ }* flds; } inrec; @@ -384,7 +384,7 @@ struct qse_awk_rtx_t #define QSE_AWK_FREEREX(awk,code) qse_freerex((awk)->mmgr,code) #define QSE_AWK_BUILDREX(awk,ptn,len,errnum) \ qse_awk_buildrex(awk,ptn,len,errnum) -#define QSE_AWK_MATCHREX(awk,code,option,str,len,substr,sublen,match,errnum) \ - qse_awk_matchrex(awk,code,option,str,len,substr,sublen,match,errnum) +#define QSE_AWK_MATCHREX(awk,code,option,str,substr,match,errnum) \ + qse_awk_matchrex(awk,code,option,str,substr,match,errnum) #endif diff --git a/qse/lib/awk/fnc.c b/qse/lib/awk/fnc.c index d8a30bc9..947146bc 100644 --- a/qse/lib/awk/fnc.c +++ b/qse/lib/awk/fnc.c @@ -1,5 +1,5 @@ /* - * $Id: fnc.c 461 2011-05-18 02:32:39Z hyunghwan.chung $ + * $Id: fnc.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -282,8 +282,8 @@ static int fnc_close (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm) if (a0->type == QSE_AWK_VAL_STR) { - name = ((qse_awk_val_str_t*)a0)->ptr; - len = ((qse_awk_val_str_t*)a0)->len; + name = ((qse_awk_val_str_t*)a0)->val.ptr; + len = ((qse_awk_val_str_t*)a0)->val.len; } else { @@ -295,8 +295,8 @@ static int fnc_close (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm) { if (a1->type == QSE_AWK_VAL_STR) { - opt = ((qse_awk_val_str_t*)a1)->ptr; - optlen = ((qse_awk_val_str_t*)a1)->len; + opt = ((qse_awk_val_str_t*)a1)->val.ptr; + optlen = ((qse_awk_val_str_t*)a1)->val.len; } else { @@ -423,8 +423,8 @@ static int fnc_fflush (qse_awk_rtx_t* run, const qse_cstr_t* fnm) a0 = qse_awk_rtx_getarg (run, 0); if (a0->type == QSE_AWK_VAL_STR) { - str0 = ((qse_awk_val_str_t*)a0)->ptr; - len0 = ((qse_awk_val_str_t*)a0)->len; + str0 = ((qse_awk_val_str_t*)a0)->val.ptr; + len0 = ((qse_awk_val_str_t*)a0)->val.len; } else { @@ -501,8 +501,8 @@ static int fnc_index (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm) if (a0->type == QSE_AWK_VAL_STR) { - str0 = ((qse_awk_val_str_t*)a0)->ptr; - len0 = ((qse_awk_val_str_t*)a0)->len; + str0 = ((qse_awk_val_str_t*)a0)->val.ptr; + len0 = ((qse_awk_val_str_t*)a0)->val.len; } else { @@ -512,8 +512,8 @@ static int fnc_index (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm) if (a1->type == QSE_AWK_VAL_STR) { - str1 = ((qse_awk_val_str_t*)a1)->ptr; - len1 = ((qse_awk_val_str_t*)a1)->len; + str1 = ((qse_awk_val_str_t*)a1)->val.ptr; + len1 = ((qse_awk_val_str_t*)a1)->val.len; } else { @@ -566,7 +566,7 @@ static int fnc_length (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm) v = qse_awk_rtx_getarg (rtx, 0); if (v->type == QSE_AWK_VAL_STR) { - len = ((qse_awk_val_str_t*)v)->len; + len = ((qse_awk_val_str_t*)v)->val.len; } else { @@ -602,8 +602,8 @@ static int fnc_substr (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm) if (a0->type == QSE_AWK_VAL_STR) { - str = ((qse_awk_val_str_t*)a0)->ptr; - len = ((qse_awk_val_str_t*)a0)->len; + str = ((qse_awk_val_str_t*)a0)->val.ptr; + len = ((qse_awk_val_str_t*)a0)->val.len; } else { @@ -658,8 +658,9 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm) { qse_size_t nargs; qse_awk_val_t* a0, * a1, * a2, * t1, * t2, ** a1_ref; - qse_char_t* str, * str_free, * p, * tok; - qse_size_t str_len, str_left, tok_len, org_len; + qse_char_t* str, * str_free, * p; + qse_size_t str_len, str_left, org_len; + qse_cstr_t tok; qse_long_t nflds; qse_char_t key[QSE_SIZEOF(qse_long_t)*8+2]; qse_size_t key_len; @@ -704,8 +705,8 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm) if (a0->type == QSE_AWK_VAL_STR) { - str = ((qse_awk_val_str_t*)a0)->ptr; - str_len = ((qse_awk_val_str_t*)a0)->len; + str = ((qse_awk_val_str_t*)a0)->val.ptr; + str_len = ((qse_awk_val_str_t*)a0)->val.len; str_free = QSE_NULL; } else @@ -727,8 +728,8 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm) } else if (t1->type == QSE_AWK_VAL_STR) { - fs_ptr = ((qse_awk_val_str_t*)t1)->ptr; - fs_len = ((qse_awk_val_str_t*)t1)->len; + fs_ptr = ((qse_awk_val_str_t*)t1)->val.ptr; + fs_len = ((qse_awk_val_str_t*)t1)->val.len; fs_free = QSE_NULL; } else @@ -766,8 +767,8 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm) { if (a2->type == QSE_AWK_VAL_STR) { - fs_ptr = ((qse_awk_val_str_t*)a2)->ptr; - fs_len = ((qse_awk_val_str_t*)a2)->len; + fs_ptr = ((qse_awk_val_str_t*)a2)->val.ptr; + fs_len = ((qse_awk_val_str_t*)a2)->val.len; fs_free = QSE_NULL; } else @@ -823,13 +824,13 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm) if (fs_len <= 1) { p = qse_awk_rtx_strxntok (run, - p, str_len, fs_ptr, fs_len, &tok, &tok_len); + p, str_len, fs_ptr, fs_len, &tok); } else { p = qse_awk_rtx_strxntokbyrex ( run, str, org_len, p, str_len, - fs_rex, &tok, &tok_len, &errnum + fs_rex, &tok, &errnum ); if (p == QSE_NULL && errnum != QSE_AWK_ENOERR) { @@ -844,16 +845,16 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm) } } - if (nflds == 0 && p == QSE_NULL && tok_len == 0) + if (nflds == 0 && p == QSE_NULL && tok.len == 0) { /* no field at all*/ break; } - QSE_ASSERT ((tok != QSE_NULL && tok_len > 0) || tok_len == 0); + QSE_ASSERT ((tok.ptr != QSE_NULL && tok.len > 0) || tok.len == 0); /* create the field string */ - t2 = qse_awk_rtx_makestrval (run, tok, tok_len); + t2 = qse_awk_rtx_makestrval (run, tok.ptr, tok.len); if (t2 == QSE_NULL) { if (str_free != QSE_NULL) @@ -927,8 +928,8 @@ static int fnc_tolower (qse_awk_rtx_t* run, const qse_cstr_t* fnm) if (a0->type == QSE_AWK_VAL_STR) { - str = ((qse_awk_val_str_t*)a0)->ptr; - len = ((qse_awk_val_str_t*)a0)->len; + str = ((qse_awk_val_str_t*)a0)->val.ptr; + len = ((qse_awk_val_str_t*)a0)->val.len; } else { @@ -964,8 +965,8 @@ static int fnc_toupper (qse_awk_rtx_t* run, const qse_cstr_t* fnm) if (a0->type == QSE_AWK_VAL_STR) { - str = ((qse_awk_val_str_t*)a0)->ptr; - len = ((qse_awk_val_str_t*)a0)->len; + str = ((qse_awk_val_str_t*)a0)->val.ptr; + len = ((qse_awk_val_str_t*)a0)->val.len; } else { @@ -991,16 +992,17 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) { qse_size_t nargs; qse_awk_val_t* a0, * a1, * a2, ** a2_ref, * v; - qse_char_t* a0_ptr, * a1_ptr, * a2_ptr, * a2_end; - qse_size_t a0_len, a1_len, a2_len; - qse_char_t* a0_ptr_free = QSE_NULL; - qse_char_t* a1_ptr_free = QSE_NULL; - qse_char_t* a2_ptr_free = QSE_NULL; + + qse_cstr_t s0, s1, s2; + const qse_char_t* s2_end; + qse_char_t* s0_free = QSE_NULL; + qse_char_t* s1_free = QSE_NULL; + qse_char_t* s2_free = QSE_NULL; + void* rex = QSE_NULL; int opt, n; - qse_cstr_t mat, pmat; - const qse_char_t* cur_ptr; - qse_size_t cur_len, i, m; + qse_cstr_t mat, pmat, cur; + qse_size_t i, m; qse_str_t new; qse_long_t sub_count; @@ -1015,9 +1017,9 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) #define FREE_A_PTRS(awk) \ do { \ - if (a2_ptr_free != QSE_NULL) QSE_AWK_FREE (awk, a2_ptr_free); \ - if (a1_ptr_free != QSE_NULL) QSE_AWK_FREE (awk, a1_ptr_free); \ - if (a0_ptr_free != QSE_NULL) QSE_AWK_FREE (awk, a0_ptr_free); \ + if (s2_free != QSE_NULL) QSE_AWK_FREE (awk, s2_free); \ + if (s1_free != QSE_NULL) QSE_AWK_FREE (awk, s1_free); \ + if (s0_free != QSE_NULL) QSE_AWK_FREE (awk, s0_free); \ } while (0) #define FREE_A0_REX(awk,rex) \ do { \ @@ -1030,41 +1032,41 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) } else if (a0->type == QSE_AWK_VAL_STR) { - a0_ptr = ((qse_awk_val_str_t*)a0)->ptr; - a0_len = ((qse_awk_val_str_t*)a0)->len; + s0.ptr = ((qse_awk_val_str_t*)a0)->val.ptr; + s0.len = ((qse_awk_val_str_t*)a0)->val.len; } else { - a0_ptr = qse_awk_rtx_valtocpldup (run, a0, &a0_len); - if (a0_ptr == QSE_NULL) + s0.ptr = qse_awk_rtx_valtocpldup (run, a0, &s0.len); + if (s0.ptr == QSE_NULL) { FREE_A_PTRS (run->awk); return -1; } - a0_ptr_free = a0_ptr; + s0_free = (qse_char_t*)s0.ptr; } if (a1->type == QSE_AWK_VAL_STR) { - a1_ptr = ((qse_awk_val_str_t*)a1)->ptr; - a1_len = ((qse_awk_val_str_t*)a1)->len; + s1.ptr = ((qse_awk_val_str_t*)a1)->val.ptr; + s1.len = ((qse_awk_val_str_t*)a1)->val.len; } else { - a1_ptr = qse_awk_rtx_valtocpldup (run, a1, &a1_len); - if (a1_ptr == QSE_NULL) + s1.ptr = qse_awk_rtx_valtocpldup (run, a1, &s1.len); + if (s1.ptr == QSE_NULL) { FREE_A_PTRS (run->awk); return -1; } - a1_ptr_free = a1_ptr; + s1_free = (qse_char_t*)s1.ptr; } if (a2 == QSE_NULL) { /* is this correct? any needs to use inrec.d0? */ - a2_ptr = QSE_STR_PTR(&run->inrec.line); - a2_len = QSE_STR_LEN(&run->inrec.line); + s2.ptr = QSE_STR_PTR(&run->inrec.line); + s2.len = QSE_STR_LEN(&run->inrec.line); } else if (((qse_awk_val_ref_t*)a2)->id == QSE_AWK_VAL_REF_POS) { @@ -1073,18 +1075,18 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) idx = (qse_size_t)((qse_awk_val_ref_t*)a2)->adr; if (idx == 0) { - a2_ptr = QSE_STR_PTR(&run->inrec.line); - a2_len = QSE_STR_LEN(&run->inrec.line); + s2.ptr = QSE_STR_PTR(&run->inrec.line); + s2.len = QSE_STR_LEN(&run->inrec.line); } else if (idx <= run->inrec.nflds) { - a2_ptr = run->inrec.flds[idx-1].ptr; - a2_len = run->inrec.flds[idx-1].len; + s2.ptr = run->inrec.flds[idx-1].ptr; + s2.len = run->inrec.flds[idx-1].len; } else { - a2_ptr = QSE_T(""); - a2_len = 0; + s2.ptr = QSE_T(""); + s2.len = 0; } } else @@ -1101,22 +1103,22 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) if ((*a2_ref)->type == QSE_AWK_VAL_STR) { - a2_ptr = ((qse_awk_val_str_t*)(*a2_ref))->ptr; - a2_len = ((qse_awk_val_str_t*)(*a2_ref))->len; + s2.ptr = ((qse_awk_val_str_t*)(*a2_ref))->val.ptr; + s2.len = ((qse_awk_val_str_t*)(*a2_ref))->val.len; } else { - a2_ptr = qse_awk_rtx_valtocpldup (run, *a2_ref, &a2_len); - if (a2_ptr == QSE_NULL) + s2.ptr = qse_awk_rtx_valtocpldup (run, *a2_ref, &s2.len); + if (s2.ptr == QSE_NULL) { FREE_A_PTRS (run->awk); return -1; } - a2_ptr_free = a2_ptr; + s2_free = (qse_char_t*)s2.ptr; } } - if (qse_str_init (&new, run->awk->mmgr, a2_len) == QSE_NULL) + if (qse_str_init (&new, run->awk->mmgr, s2.len) == QSE_NULL) { FREE_A_PTRS (run->awk); qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); @@ -1128,7 +1130,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) qse_awk_errnum_t errnum; rex = QSE_AWK_BUILDREX ( - run->awk, a0_ptr, a0_len, &errnum); + run->awk, s0.ptr, s0.len, &errnum); if (rex == QSE_NULL) { qse_str_fini (&new); @@ -1140,27 +1142,24 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) opt = (run->gbl.ignorecase)? QSE_REX_IGNORECASE: 0; - a2_end = a2_ptr + a2_len; - cur_ptr = a2_ptr; - cur_len = a2_len; + s2_end = s2.ptr + s2.len; + cur.ptr = s2.ptr; + cur.len = s2.len; sub_count = 0; pmat.ptr = QSE_NULL; pmat.len = 0; - /* perform test when cur_ptr == a2_end also because + /* perform test when cur_ptr == s2_end also because * end of string($) needs to be tested */ - while (cur_ptr <= a2_end) + while (cur.ptr <= s2_end) { qse_awk_errnum_t errnum; if (max_count == 0 || sub_count < max_count) { n = QSE_AWK_MATCHREX ( - run->awk, rex, opt, - a2_ptr, a2_len, - cur_ptr, cur_len, - &mat, &errnum + run->awk, rex, opt, &s2, &cur, &mat, &errnum ); } else n = 0; @@ -1178,7 +1177,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) { /* no more match found */ if (qse_str_ncat ( - &new, cur_ptr, cur_len) == (qse_size_t)-1) + &new, cur.ptr, cur.len) == (qse_size_t)-1) { FREE_A0_REX (run->awk, rex); qse_str_fini (&new); @@ -1199,7 +1198,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) } if (qse_str_ncat ( - &new, cur_ptr, mat.ptr - cur_ptr) == (qse_size_t)-1) + &new, cur.ptr, mat.ptr - cur.ptr) == (qse_size_t)-1) { FREE_A0_REX (run->awk, rex); qse_str_fini (&new); @@ -1208,22 +1207,22 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) return -1; } - for (i = 0; i < a1_len; i++) + for (i = 0; i < s1.len; i++) { - if ((i+1) < a1_len && - a1_ptr[i] == QSE_T('\\') && - a1_ptr[i+1] == QSE_T('&')) + if ((i+1) < s1.len && + s1.ptr[i] == QSE_T('\\') && + s1.ptr[i+1] == QSE_T('&')) { m = qse_str_ccat (&new, QSE_T('&')); i++; } - else if (a1_ptr[i] == QSE_T('&')) + else if (s1.ptr[i] == QSE_T('&')) { m = qse_str_ncat (&new, mat.ptr, mat.len); } else { - m = qse_str_ccat (&new, a1_ptr[i]); + m = qse_str_ccat (&new, s1.ptr[i]); } if (m == (qse_size_t)-1) @@ -1237,8 +1236,8 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) } sub_count++; - cur_len = cur_len - ((mat.ptr - cur_ptr) + mat.len); - cur_ptr = mat.ptr + mat.len; + cur.len = cur.len - ((mat.ptr - cur.ptr) + mat.len); + cur.ptr = mat.ptr + mat.len; pmat = mat; @@ -1247,7 +1246,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) skip_one_char: /* special treatment is needed if match length is 0 */ - m = qse_str_ncat (&new, cur_ptr, 1); + m = qse_str_ncat (&new, cur.ptr, 1); if (m == (qse_size_t)-1) { FREE_A0_REX (run->awk, rex); @@ -1257,7 +1256,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count) return -1; } - cur_ptr++; cur_len--; + cur.ptr++; cur.len--; } } @@ -1377,8 +1376,8 @@ static int fnc_match (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm) if (a0->type == QSE_AWK_VAL_STR) { - str0 = ((qse_awk_val_str_t*)a0)->ptr; - len0 = ((qse_awk_val_str_t*)a0)->len; + str0 = ((qse_awk_val_str_t*)a0)->val.ptr; + len0 = ((qse_awk_val_str_t*)a0)->val.len; } else { @@ -1396,8 +1395,8 @@ static int fnc_match (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm) if (a1->type == QSE_AWK_VAL_STR) { - str1 = ((qse_awk_val_str_t*)a1)->ptr; - len1 = ((qse_awk_val_str_t*)a1)->len; + str1 = ((qse_awk_val_str_t*)a1)->val.ptr; + len1 = ((qse_awk_val_str_t*)a1)->val.len; } else { @@ -1428,12 +1427,15 @@ static int fnc_match (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm) if (start > len0 || start <= 0) n = 0; else { + qse_cstr_t tmp; + + /*TODO: must use str0,len0?*/ + tmp.ptr = str0 + start - 1; + tmp.len = len0 - start + 1; n = QSE_AWK_MATCHREX ( rtx->awk, rex, (rtx->gbl.ignorecase? QSE_REX_IGNORECASE: 0), - str0+start-1, len0-start+1,/*TODO: must use str0,len0?*/ - str0+start-1, len0-start+1, - &mat, &errnum + &tmp, &tmp, &mat, &errnum ); } @@ -1510,8 +1512,7 @@ static int fnc_sprintf (qse_awk_rtx_t* run, const qse_cstr_t* fnm) a0 = qse_awk_rtx_getarg (run, 0); if (a0->type == QSE_AWK_VAL_STR) { - cs0.ptr = ((qse_awk_val_str_t*)a0)->ptr; - cs0.len = ((qse_awk_val_str_t*)a0)->len; + cs0 = ((qse_awk_val_str_t*)a0)->val; } else { diff --git a/qse/lib/awk/misc.c b/qse/lib/awk/misc.c index dde592ca..8f7a726d 100644 --- a/qse/lib/awk/misc.c +++ b/qse/lib/awk/misc.c @@ -1,5 +1,5 @@ /* - * $Id: misc.c 441 2011-04-22 14:28:43Z hyunghwan.chung $ + * $Id: misc.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -623,36 +623,32 @@ qse_size_t qse_awk_longtostr ( qse_char_t* qse_awk_rtx_strtok ( qse_awk_rtx_t* rtx, const qse_char_t* s, - const qse_char_t* delim, qse_char_t** tok, qse_size_t* tok_len) + const qse_char_t* delim, qse_cstr_t* tok) { return qse_awk_rtx_strxntok ( - rtx, s, qse_strlen(s), - delim, qse_strlen(delim), tok, tok_len); + rtx, s, qse_strlen(s), delim, qse_strlen(delim), tok); } qse_char_t* qse_awk_rtx_strxtok ( qse_awk_rtx_t* rtx, const qse_char_t* s, qse_size_t len, - const qse_char_t* delim, qse_char_t** tok, qse_size_t* tok_len) + const qse_char_t* delim, qse_cstr_t* tok) { return qse_awk_rtx_strxntok ( - rtx, s, len, - delim, qse_strlen(delim), tok, tok_len); + rtx, s, len, delim, qse_strlen(delim), tok); } qse_char_t* qse_awk_rtx_strntok ( qse_awk_rtx_t* rtx, const qse_char_t* s, const qse_char_t* delim, qse_size_t delim_len, - qse_char_t** tok, qse_size_t* tok_len) + qse_cstr_t* tok) { return qse_awk_rtx_strxntok ( - rtx, s, qse_strlen(s), - delim, delim_len, tok, tok_len); + rtx, s, qse_strlen(s), delim, delim_len, tok); } qse_char_t* qse_awk_rtx_strxntok ( qse_awk_rtx_t* rtx, const qse_char_t* s, qse_size_t len, - const qse_char_t* delim, qse_size_t delim_len, - qse_char_t** tok, qse_size_t* tok_len) + const qse_char_t* delim, qse_size_t delim_len, qse_cstr_t* tok) { const qse_char_t* p = s, *d; const qse_char_t* end = s + len; @@ -826,13 +822,13 @@ qse_char_t* qse_awk_rtx_strxntok ( exit_loop: if (sp == QSE_NULL) { - *tok = QSE_NULL; - *tok_len = (qse_size_t)0; + tok->ptr = QSE_NULL; + tok->len = (qse_size_t)0; } else { - *tok = (qse_char_t*)sp; - *tok_len = ep - sp + 1; + tok->ptr = sp; + tok->len = ep - sp + 1; } /* if QSE_NULL is returned, this function should not be called again */ @@ -846,29 +842,35 @@ qse_char_t* qse_awk_rtx_strxntokbyrex ( qse_awk_rtx_t* rtx, const qse_char_t* str, qse_size_t len, const qse_char_t* substr, qse_size_t sublen, - void* rex, qse_char_t** tok, qse_size_t* tok_len, + void* rex, qse_cstr_t* tok, qse_awk_errnum_t* errnum) { int n; - qse_size_t i, left = sublen; - const qse_char_t* ptr = substr; - const qse_char_t* str_ptr = substr; - qse_size_t str_len = sublen; - qse_cstr_t match; + qse_size_t i; + qse_cstr_t match, s, cursub, realsub; - while (sublen > 0) + s.ptr = str; + s.len = len; + + cursub.ptr = substr; + cursub.len = sublen; + + realsub.ptr = substr; + realsub.len = sublen; + + while (cursub.len > 0) { n = QSE_AWK_MATCHREX ( rtx->awk, rex, ((rtx->gbl.ignorecase)? QSE_REX_IGNORECASE: 0), - str, len, ptr, left, &match, errnum); + &s, &cursub, &match, errnum); if (n == -1) return QSE_NULL; if (n == 0) { /* no match has been found. * return the entire string as a token */ - *tok = (qse_char_t*)str_ptr; - *tok_len = str_len; + tok->ptr = realsub.ptr; + tok->len = realsub.len; *errnum = QSE_AWK_ENOERR; return QSE_NULL; } @@ -877,8 +879,9 @@ qse_char_t* qse_awk_rtx_strxntokbyrex ( if (match.len == 0) { - ptr++; - left--; + /* the match length is zero. */ + cursub.ptr++; + cursub.len--; } else if (rtx->awk->option & QSE_AWK_STRIPRECSPC) { @@ -891,12 +894,15 @@ qse_char_t* qse_awk_rtx_strxntokbyrex ( goto exit_loop; } - /* the match that are all spaces at the + /* the match that is all spaces at the * beginning of the input string is skipped */ - ptr += match.len; - left -= match.len; - str_ptr = substr + match.len; - str_len -= match.len; + cursub.ptr += match.len; + cursub.len -= match.len; + + /* adjust the substring by skipping the leading + * spaces and retry matching */ + realsub.ptr = substr + match.len; + realsub.len -= match.len; } else break; } @@ -904,35 +910,40 @@ qse_char_t* qse_awk_rtx_strxntokbyrex ( } exit_loop: - if (sublen == 0) + if (cursub.len <= 0) { - *tok = (qse_char_t*)str_ptr; - *tok_len = str_len; + tok->ptr = realsub.ptr; + tok->len = realsub.len; *errnum = QSE_AWK_ENOERR; return QSE_NULL; } - *tok = (qse_char_t*)str_ptr; - *tok_len = match.ptr - str_ptr; + tok->ptr = realsub.ptr; + tok->len = match.ptr - realsub.ptr; for (i = 0; i < match.len; i++) { if (!QSE_AWK_ISSPACE(rtx->awk, match.ptr[i])) { + /* the match contains a non-space character. */ *errnum = QSE_AWK_ENOERR; return (qse_char_t*)match.ptr+match.len; } } + /* the match is all spaces */ *errnum = QSE_AWK_ENOERR; - if (rtx->awk->option & QSE_AWK_STRIPRECSPC) { + /* if the match reached the last character in the input string, + * it returns QSE_NULL to terminate tokenization. */ return (match.ptr+match.len >= substr+sublen)? QSE_NULL: ((qse_char_t*)match.ptr+match.len); } else { + /* if the match went beyond the the last character in the input + * string, it returns QSE_NULL to terminate tokenization. */ return (match.ptr+match.len > substr+sublen)? QSE_NULL: ((qse_char_t*)match.ptr+match.len); } @@ -941,7 +952,7 @@ exit_loop: qse_char_t* qse_awk_rtx_strxnfld ( qse_awk_rtx_t* rtx, qse_char_t* str, qse_size_t len, qse_char_t fs, qse_char_t ec, qse_char_t lq, qse_char_t rq, - qse_char_t** tok, qse_size_t* tok_len) + qse_cstr_t* tok) { qse_char_t* p = str; qse_char_t* end = str + len; @@ -988,8 +999,8 @@ qse_char_t* qse_awk_rtx_strxnfld ( { if (c == fs) { - *tok = ts; - *tok_len = xp - ts; + tok->ptr = ts; + tok->len = xp - ts; p++; if (QSE_ISSPACE(fs)) @@ -1022,8 +1033,8 @@ qse_char_t* qse_awk_rtx_strxnfld ( *xp++ = ec; } - *tok = ts; - *tok_len = xp - ts; + tok->ptr = ts; + tok->len = xp - ts; return QSE_NULL; } @@ -1061,8 +1072,7 @@ void* qse_awk_buildrex ( int qse_awk_matchrex ( qse_awk_t* awk, void* code, int option, - const qse_char_t* str, qse_size_t len, - const qse_char_t* substr, qse_size_t sublen, + const qse_cstr_t* str, const qse_cstr_t* substr, qse_cstr_t* match, qse_awk_errnum_t* errnum) { int x; @@ -1070,7 +1080,7 @@ int qse_awk_matchrex ( x = qse_matchrex ( awk->mmgr, awk->rex.depth.max.match, - code, option, str, len, substr, sublen, match, &err); + code, option, str, substr, match, &err); if (x < 0) *errnum = QSE_AWK_REXERRTOERR(err); return x; } diff --git a/qse/lib/awk/misc.h b/qse/lib/awk/misc.h index bbcad9e8..998aa594 100644 --- a/qse/lib/awk/misc.h +++ b/qse/lib/awk/misc.h @@ -1,5 +1,5 @@ /* - * $Id: misc.h 441 2011-04-22 14:28:43Z hyunghwan.chung $ + * $Id: misc.h 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -27,21 +27,19 @@ extern "C" { qse_char_t* qse_awk_rtx_strtok ( qse_awk_rtx_t* rtx, const qse_char_t* s, - const qse_char_t* delim, qse_char_t** tok, qse_size_t* tok_len); + const qse_char_t* delim, qse_cstr_t* tok); qse_char_t* qse_awk_rtx_strxtok ( qse_awk_rtx_t* rtx, const qse_char_t* s, qse_size_t len, - const qse_char_t* delim, qse_char_t** tok, qse_size_t* tok_len); + const qse_char_t* delim, qse_cstr_t* tok); qse_char_t* qse_awk_rtx_strntok ( qse_awk_rtx_t* rtx, const qse_char_t* s, - const qse_char_t* delim, qse_size_t delim_len, - qse_char_t** tok, qse_size_t* tok_len); + const qse_char_t* delim, qse_size_t delim_len, qse_cstr_t* tok); qse_char_t* qse_awk_rtx_strxntok ( qse_awk_rtx_t* rtx, const qse_char_t* s, qse_size_t len, - const qse_char_t* delim, qse_size_t delim_len, - qse_char_t** tok, qse_size_t* tok_len); + const qse_char_t* delim, qse_size_t delim_len, qse_cstr_t* tok); qse_char_t* qse_awk_rtx_strxntokbyrex ( qse_awk_rtx_t* rtx, @@ -50,8 +48,7 @@ qse_char_t* qse_awk_rtx_strxntokbyrex ( const qse_char_t* substr, qse_size_t sublen, void* rex, - qse_char_t** tok, - qse_size_t* tok_len, + qse_cstr_t* tok, qse_awk_errnum_t* errnum ); @@ -63,8 +60,7 @@ qse_char_t* qse_awk_rtx_strxnfld ( qse_char_t lq, qse_char_t rq, qse_char_t ec, - qse_char_t** tok, - qse_size_t* tok_len + qse_cstr_t* tok ); void* qse_awk_buildrex ( @@ -76,8 +72,7 @@ void* qse_awk_buildrex ( int qse_awk_matchrex ( qse_awk_t* awk, void* code, int option, - const qse_char_t* str, qse_size_t len, - const qse_char_t* substr, qse_size_t sublen, + const qse_cstr_t* str, const qse_cstr_t* substr, qse_cstr_t* match, qse_awk_errnum_t* errnum ); diff --git a/qse/lib/awk/parse.c b/qse/lib/awk/parse.c index 792b3dab..5fd27d48 100644 --- a/qse/lib/awk/parse.c +++ b/qse/lib/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c 441 2011-04-22 14:28:43Z hyunghwan.chung $ + * $Id: parse.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -679,8 +679,10 @@ static int begin_include (qse_awk_t* awk) qse_awk_sio_arg_t* arg = QSE_NULL; qse_htb_pair_t* pair = QSE_NULL; - if (qse_strlen(awk->tok.name->ptr) != QSE_STR_LEN(awk->tok.name)) + if (qse_strlen(QSE_STR_PTR(awk->tok.name)) != QSE_STR_LEN(awk->tok.name)) { + /* a '\0' character included in the include file name. + * we don't support such a file name */ SETERR_ARG_LOC ( awk, QSE_AWK_EIONMNL, diff --git a/qse/lib/awk/rec.c b/qse/lib/awk/rec.c index 4f9a707b..7586cb37 100644 --- a/qse/lib/awk/rec.c +++ b/qse/lib/awk/rec.c @@ -1,5 +1,5 @@ /* - * $Id: rec.c 441 2011-04-22 14:28:43Z hyunghwan.chung $ + * $Id: rec.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -98,8 +98,9 @@ int qse_awk_rtx_setrec ( static int split_record (qse_awk_rtx_t* rtx) { - qse_char_t* p, * px, * tok; - qse_size_t len, tok_len, nflds; + qse_cstr_t tok; + qse_char_t* p, * px; + qse_size_t len, nflds; qse_awk_val_t* v, * fs; qse_char_t* fs_ptr, * fs_free; qse_size_t fs_len; @@ -119,8 +120,8 @@ static int split_record (qse_awk_rtx_t* rtx) } else if (fs->type == QSE_AWK_VAL_STR) { - fs_ptr = ((qse_awk_val_str_t*)fs)->ptr; - fs_len = ((qse_awk_val_str_t*)fs)->len; + fs_ptr = ((qse_awk_val_str_t*)fs)->val.ptr; + fs_len = ((qse_awk_val_str_t*)fs)->val.len; fs_free = QSE_NULL; } else @@ -169,7 +170,7 @@ static int split_record (qse_awk_rtx_t* rtx) { case 0: p = qse_awk_rtx_strxntok (rtx, - p, len, fs_ptr, fs_len, &tok, &tok_len); + p, len, fs_ptr, fs_len, &tok); break; case 1: @@ -181,7 +182,7 @@ static int split_record (qse_awk_rtx_t* rtx) QSE_STR_PTR(&rtx->inrec.line), QSE_STR_LEN(&rtx->inrec.line), p, len, - rtx->gbl.fs, &tok, &tok_len, &errnum + rtx->gbl.fs, &tok, &errnum ); if (p == QSE_NULL && errnum != QSE_AWK_ENOERR) { @@ -192,7 +193,7 @@ static int split_record (qse_awk_rtx_t* rtx) } } - if (nflds == 0 && p == QSE_NULL && tok_len == 0) + if (nflds == 0 && p == QSE_NULL && tok.len == 0) { /* there are no fields. it can just return here * as qse_awk_rtx_clrrec has been called before this */ @@ -200,7 +201,7 @@ static int split_record (qse_awk_rtx_t* rtx) return 0; } - QSE_ASSERT ((tok != QSE_NULL && tok_len > 0) || tok_len == 0); + QSE_ASSERT ((tok.ptr != QSE_NULL && tok.len > 0) || tok.len == 0); nflds++; len = QSE_STR_LEN(&rtx->inrec.line) - @@ -255,16 +256,14 @@ static int split_record (qse_awk_rtx_t* rtx) { case 0: p = qse_awk_rtx_strxntok ( - rtx, p, len, fs_ptr, fs_len, - &tok, &tok_len); + rtx, p, len, fs_ptr, fs_len, &tok); break; case 1: p = qse_awk_rtx_strxnfld ( rtx, p, len, fs_ptr[1], fs_ptr[2], - fs_ptr[3], fs_ptr[4], - &tok, &tok_len); + fs_ptr[3], fs_ptr[4], &tok); break; default: @@ -273,7 +272,7 @@ static int split_record (qse_awk_rtx_t* rtx) QSE_STR_PTR(&rtx->inrec.line), QSE_STR_LEN(&rtx->inrec.line), p, len, - rtx->gbl.fs, &tok, &tok_len, &errnum + rtx->gbl.fs, &tok, &errnum ); if (p == QSE_NULL && errnum != QSE_AWK_ENOERR) { @@ -285,7 +284,7 @@ static int split_record (qse_awk_rtx_t* rtx) } #if 1 - if (rtx->inrec.nflds == 0 && p == QSE_NULL && tok_len == 0) + if (rtx->inrec.nflds == 0 && p == QSE_NULL && tok.len == 0) { /* there are no fields. it can just return here * as qse_awk_rtx_clrrec has been called before this */ @@ -294,7 +293,7 @@ static int split_record (qse_awk_rtx_t* rtx) } #endif - QSE_ASSERT ((tok != QSE_NULL && tok_len > 0) || tok_len == 0); + QSE_ASSERT ((tok.ptr != QSE_NULL && tok.len > 0) || tok.len == 0); #if 1 if (rtx->inrec.nflds >= rtx->inrec.maxflds) @@ -327,11 +326,11 @@ static int split_record (qse_awk_rtx_t* rtx) } #endif - rtx->inrec.flds[rtx->inrec.nflds].ptr = tok; - rtx->inrec.flds[rtx->inrec.nflds].len = tok_len; + rtx->inrec.flds[rtx->inrec.nflds].ptr = tok.ptr; + rtx->inrec.flds[rtx->inrec.nflds].len = tok.len; rtx->inrec.flds[rtx->inrec.nflds].val = - qse_awk_rtx_makenstrval (rtx, tok, tok_len); + qse_awk_rtx_makenstrval (rtx, tok.ptr, tok.len); if (rtx->inrec.flds[rtx->inrec.nflds].val == QSE_NULL) { @@ -531,10 +530,11 @@ static int recomp_record_fields ( run->inrec.flds[i].ptr = QSE_STR_PTR(&run->inrec.line) + QSE_STR_LEN(&run->inrec.line); - run->inrec.flds[i].len = tmp->len; + run->inrec.flds[i].len = tmp->val.len; - if (qse_str_ncat (&run->inrec.line, - tmp->ptr, tmp->len) == (qse_size_t)-1) + if (qse_str_ncat ( + &run->inrec.line, + tmp->val.ptr, tmp->val.len) == (qse_size_t)-1) { qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); return -1; diff --git a/qse/lib/awk/rio.c b/qse/lib/awk/rio.c index 6e1ef148..436c9d57 100644 --- a/qse/lib/awk/rio.c +++ b/qse/lib/awk/rio.c @@ -1,5 +1,5 @@ /* - * $Id: rio.c 450 2011-05-03 07:48:42Z hyunghwan.chung $ + * $Id: rio.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -206,8 +206,8 @@ static QSE_INLINE int resolve_rs ( break; case QSE_AWK_VAL_STR: - rrs->ptr = ((qse_awk_val_str_t*)rs)->ptr; - rrs->len = ((qse_awk_val_str_t*)rs)->len; + rrs->ptr = ((qse_awk_val_str_t*)rs)->val.ptr; + rrs->len = ((qse_awk_val_str_t*)rs)->val.len; break; default: @@ -231,8 +231,7 @@ static QSE_INLINE int match_long_rs ( ret = QSE_AWK_MATCHREX ( run->awk, run->gbl.rs, ((run->gbl.ignorecase)? QSE_REX_IGNORECASE: 0), - QSE_STR_PTR(buf), QSE_STR_LEN(buf), - QSE_STR_PTR(buf), QSE_STR_LEN(buf), + QSE_STR_XSTR(buf), QSE_STR_XSTR(buf), &match, &errnum); if (ret <= -1) { @@ -644,8 +643,8 @@ int qse_awk_rtx_writeio_val ( if (v->type == QSE_AWK_VAL_STR) { - str = ((qse_awk_val_str_t*)v)->ptr; - len = ((qse_awk_val_str_t*)v)->len; + str = ((qse_awk_val_str_t*)v)->val.ptr; + len = ((qse_awk_val_str_t*)v)->val.len; } else { diff --git a/qse/lib/awk/run.c b/qse/lib/awk/run.c index 2adc7c4b..fcccd805 100644 --- a/qse/lib/awk/run.c +++ b/qse/lib/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c 459 2011-05-17 14:37:51Z hyunghwan.chung $ + * $Id: run.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -385,8 +385,8 @@ static int set_global ( if (val->type == QSE_AWK_VAL_STR) { - fs_ptr = ((qse_awk_val_str_t*)val)->ptr; - fs_len = ((qse_awk_val_str_t*)val)->len; + fs_ptr = ((qse_awk_val_str_t*)val)->val.ptr; + fs_len = ((qse_awk_val_str_t*)val)->val.len; } else { @@ -436,7 +436,7 @@ static int set_global ( (val->type == QSE_AWK_VAL_REAL && ((qse_awk_val_real_t*)val)->val != 0.0) || (val->type == QSE_AWK_VAL_STR && - ((qse_awk_val_str_t*)val)->len != 0))? 1: 0; + ((qse_awk_val_str_t*)val)->val.len != 0))? 1: 0; break; } @@ -540,13 +540,11 @@ static int set_global ( case QSE_AWK_GBL_RS: { - qse_char_t* rs_ptr; - qse_size_t rs_len; + qse_xstr_t rss; if (val->type == QSE_AWK_VAL_STR) { - rs_ptr = ((qse_awk_val_str_t*)val)->ptr; - rs_len = ((qse_awk_val_str_t*)val)->len; + rss = ((qse_awk_val_str_t*)val)->val; } else { @@ -561,23 +559,22 @@ static int set_global ( if (qse_awk_rtx_valtostr (rtx, val, &out) == QSE_NULL) return -1; - rs_ptr = out.u.cpldup.ptr; - rs_len = out.u.cpldup.len; + rss = out.u.cpldup; } - if (rs_len > 1) + if (rss.len > 1) { void* rex; qse_awk_errnum_t errnum; /* compile the regular expression */ rex = QSE_AWK_BUILDREX ( - rtx->awk, rs_ptr, rs_len, &errnum); + rtx->awk, rss.ptr, rss.len, &errnum); if (rex == QSE_NULL) { SETERR_COD (rtx, errnum); if (val->type != QSE_AWK_VAL_STR) - QSE_AWK_FREE (rtx->awk, rs_ptr); + QSE_AWK_FREE (rtx->awk, rss.ptr); return -1; } @@ -588,7 +585,7 @@ static int set_global ( } if (val->type != QSE_AWK_VAL_STR) - QSE_AWK_FREE (rtx->awk, rs_ptr); + QSE_AWK_FREE (rtx->awk, rss.ptr); break; } @@ -2505,8 +2502,8 @@ static int delete_indexed ( qse_htb_delete ( map, - ((qse_awk_val_str_t*)idx)->ptr, - ((qse_awk_val_str_t*)idx)->len + ((qse_awk_val_str_t*)idx)->val.ptr, + ((qse_awk_val_str_t*)idx)->val.len ); qse_awk_rtx_refdownval (rtx, idx); } @@ -3050,8 +3047,8 @@ static int run_printf (qse_awk_rtx_t* rtx, qse_awk_nde_print_t* nde) /* perform the formatted output */ if (output_formatted ( rtx, nde->out_type, dst, - ((qse_awk_val_str_t*)v)->ptr, - ((qse_awk_val_str_t*)v)->len, + ((qse_awk_val_str_t*)v)->val.ptr, + ((qse_awk_val_str_t*)v)->val.len, head->next) == -1) { if (out != QSE_NULL) QSE_AWK_FREE (rtx->awk, out); @@ -3112,8 +3109,7 @@ static qse_awk_val_t* eval_expression (qse_awk_rtx_t* rtx, qse_awk_nde_t* nde) if (v->type == QSE_AWK_VAL_REX) { - const qse_char_t* ptr; - qse_size_t len; + qse_cstr_t vs; int opt = 0; if (((qse_awk_rtx_t*)rtx)->gbl.ignorecase) @@ -3126,8 +3122,8 @@ static qse_awk_val_t* eval_expression (qse_awk_rtx_t* rtx, qse_awk_nde_t* nde) /* the record has never been read. * probably, this function has been triggered * by the statements in the BEGIN block */ - ptr = QSE_T(""); - len = 0; + vs.ptr = QSE_T(""); + vs.len = 0; } else { @@ -3137,16 +3133,15 @@ static qse_awk_val_t* eval_expression (qse_awk_rtx_t* rtx, qse_awk_nde_t* nde) "always be of the string type once it has " "been set/updated. it is nil initially."); - ptr = ((qse_awk_val_str_t*)rtx->inrec.d0)->ptr; - len = ((qse_awk_val_str_t*)rtx->inrec.d0)->len; + vs.ptr = ((qse_awk_val_str_t*)rtx->inrec.d0)->val.ptr; + vs.len = ((qse_awk_val_str_t*)rtx->inrec.d0)->val.len; } n = QSE_AWK_MATCHREX ( ((qse_awk_rtx_t*)rtx)->awk, ((qse_awk_val_rex_t*)v)->code, - opt, ptr, len, ptr, len, - QSE_NULL, &errnum); - + opt, &vs, &vs, QSE_NULL, &errnum + ); if (n <= -1) { qse_awk_rtx_refdownval (rtx, v); @@ -3575,8 +3570,7 @@ static qse_awk_val_t* do_assignment_pos ( qse_awk_val_t* v; qse_long_t lv; qse_real_t rv; - qse_char_t* str; - qse_size_t len; + qse_xstr_t str; int n; v = eval_expression (run, pos->val); @@ -3600,8 +3594,7 @@ static qse_awk_val_t* do_assignment_pos ( if (val->type == QSE_AWK_VAL_STR) { - str = ((qse_awk_val_str_t*)val)->ptr; - len = ((qse_awk_val_str_t*)val)->len; + str = ((qse_awk_val_str_t*)val)->val; } else { @@ -3614,13 +3607,12 @@ static qse_awk_val_t* do_assignment_pos ( return QSE_NULL; } - str = out.u.cpldup.ptr; - len = out.u.cpldup.len; + str = out.u.cpldup; } - n = qse_awk_rtx_setrec (run, (qse_size_t)lv, str, len); + n = qse_awk_rtx_setrec (run, (qse_size_t)lv, str.ptr, str.len); - if (val->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str); + if (val->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str.ptr); if (n == -1) return QSE_NULL; return (lv == 0)? run->inrec.d0: run->inrec.flds[lv-1].val; @@ -3996,7 +3988,7 @@ static int __cmp_nil_real ( static int __cmp_nil_str ( qse_awk_rtx_t* run, qse_awk_val_t* left, qse_awk_val_t* right) { - return (((qse_awk_val_str_t*)right)->len == 0)? 0: -1; + return (((qse_awk_val_str_t*)right)->val.len == 0)? 0: -1; } static int __cmp_int_nil ( @@ -4041,8 +4033,8 @@ static int __cmp_int_str ( n = qse_awk_rtx_strtonum ( rtx, 1, - ((qse_awk_val_str_t*)right)->ptr, - ((qse_awk_val_str_t*)right)->len, + ((qse_awk_val_str_t*)right)->val.ptr, + ((qse_awk_val_str_t*)right)->val.len, &ll, &rr ); if (n == 0) @@ -4068,8 +4060,8 @@ static int __cmp_int_str ( n = qse_strxncasecmp ( out.u.cpldup.ptr, out.u.cpldup.len, - ((qse_awk_val_str_t*)right)->ptr, - ((qse_awk_val_str_t*)right)->len + ((qse_awk_val_str_t*)right)->val.ptr, + ((qse_awk_val_str_t*)right)->val.len ); } else @@ -4077,8 +4069,8 @@ static int __cmp_int_str ( n = qse_strxncmp ( out.u.cpldup.ptr, out.u.cpldup.len, - ((qse_awk_val_str_t*)right)->ptr, - ((qse_awk_val_str_t*)right)->len + ((qse_awk_val_str_t*)right)->val.ptr, + ((qse_awk_val_str_t*)right)->val.len ); } @@ -4128,12 +4120,12 @@ static int __cmp_real_str ( rr = qse_awk_strxtoreal ( rtx->awk, - ((qse_awk_val_str_t*)right)->ptr, - ((qse_awk_val_str_t*)right)->len, + ((qse_awk_val_str_t*)right)->val.ptr, + ((qse_awk_val_str_t*)right)->val.len, &end ); - if (end == ((qse_awk_val_str_t*)right)->ptr + - ((qse_awk_val_str_t*)right)->len) + if (end == ((qse_awk_val_str_t*)right)->val.ptr + + ((qse_awk_val_str_t*)right)->val.len) { return (((qse_awk_val_real_t*)left)->val > rr)? 1: (((qse_awk_val_real_t*)left)->val < rr)? -1: 0; @@ -4149,8 +4141,8 @@ static int __cmp_real_str ( n = qse_strxncasecmp ( out.u.cpldup.ptr, out.u.cpldup.len, - ((qse_awk_val_str_t*)right)->ptr, - ((qse_awk_val_str_t*)right)->len + ((qse_awk_val_str_t*)right)->val.ptr, + ((qse_awk_val_str_t*)right)->val.len ); } else @@ -4158,8 +4150,8 @@ static int __cmp_real_str ( n = qse_strxncmp ( out.u.cpldup.ptr, out.u.cpldup.len, - ((qse_awk_val_str_t*)right)->ptr, - ((qse_awk_val_str_t*)right)->len + ((qse_awk_val_str_t*)right)->val.ptr, + ((qse_awk_val_str_t*)right)->val.len ); } @@ -4170,7 +4162,7 @@ static int __cmp_real_str ( static int __cmp_str_nil ( qse_awk_rtx_t* run, qse_awk_val_t* left, qse_awk_val_t* right) { - return (((qse_awk_val_str_t*)left)->len == 0)? 0: 1; + return (((qse_awk_val_str_t*)left)->val.len == 0)? 0: 1; } static int __cmp_str_int ( @@ -4197,8 +4189,8 @@ static int __cmp_str_str ( { /* nother are definitely a string */ return (rtx->gbl.ignorecase)? - qse_strxncasecmp (ls->ptr, ls->len, rs->ptr, rs->len): - qse_strxncmp (ls->ptr, ls->len, rs->ptr, rs->len); + qse_strxncasecmp (ls->val.ptr, ls->val.len, rs->val.ptr, rs->val.len): + qse_strxncmp (ls->val.ptr, ls->val.len, rs->val.ptr, rs->val.len); } if (ls->nstr == 1) @@ -4206,14 +4198,14 @@ static int __cmp_str_str ( qse_long_t ll; ll = qse_awk_strxtolong ( - rtx->awk, ls->ptr, ls->len, 0, QSE_NULL); + rtx->awk, ls->val.ptr, ls->val.len, 0, QSE_NULL); if (rs->nstr == 1) { qse_long_t rr; rr = qse_awk_strxtolong ( - rtx->awk, rs->ptr, rs->len, 0, QSE_NULL); + rtx->awk, rs->val.ptr, rs->val.len, 0, QSE_NULL); return (ll > rr)? 1: (ll < rr)? -1: 0; @@ -4225,7 +4217,7 @@ static int __cmp_str_str ( QSE_ASSERT (rs->nstr == 2); rr = qse_awk_strxtoreal ( - rtx->awk, rs->ptr, rs->len, QSE_NULL); + rtx->awk, rs->val.ptr, rs->val.len, QSE_NULL); return (ll > rr)? 1: (ll < rr)? -1: 0; @@ -4238,14 +4230,14 @@ static int __cmp_str_str ( QSE_ASSERT (ls->nstr == 2); ll = qse_awk_strxtoreal ( - rtx->awk, ls->ptr, ls->len, QSE_NULL); + rtx->awk, ls->val.ptr, ls->val.len, QSE_NULL); if (rs->nstr == 1) { qse_long_t rr; rr = qse_awk_strxtolong ( - rtx->awk, rs->ptr, rs->len, 0, QSE_NULL); + rtx->awk, rs->val.ptr, rs->val.len, 0, QSE_NULL); return (ll > rr)? 1: (ll < rr)? -1: 0; @@ -4257,7 +4249,7 @@ static int __cmp_str_str ( QSE_ASSERT (rs->nstr == 2); rr = qse_awk_strxtoreal ( - rtx->awk, rs->ptr, rs->len, QSE_NULL); + rtx->awk, rs->val.ptr, rs->val.len, QSE_NULL); return (ll > rr)? 1: (ll < rr)? -1: 0; @@ -4763,8 +4755,8 @@ static qse_awk_val_t* eval_binop_match0 ( { rex_code = QSE_AWK_BUILDREX ( rtx->awk, - ((qse_awk_val_str_t*)right)->ptr, - ((qse_awk_val_str_t*)right)->len, &errnum); + ((qse_awk_val_str_t*)right)->val.ptr, + ((qse_awk_val_str_t*)right)->val.len, &errnum); if (rex_code == QSE_NULL) { SETERR_LOC (rtx, errnum, rloc); @@ -4796,10 +4788,8 @@ static qse_awk_val_t* eval_binop_match0 ( n = QSE_AWK_MATCHREX ( rtx->awk, rex_code, ((rtx->gbl.ignorecase)? QSE_REX_IGNORECASE: 0), - ((qse_awk_val_str_t*)left)->ptr, - ((qse_awk_val_str_t*)left)->len, - ((qse_awk_val_str_t*)left)->ptr, - ((qse_awk_val_str_t*)left)->len, + &((qse_awk_val_str_t*)left)->val, + &((qse_awk_val_str_t*)left)->val, QSE_NULL, &errnum); if (n == -1) { @@ -4835,8 +4825,7 @@ static qse_awk_val_t* eval_binop_match0 ( n = QSE_AWK_MATCHREX ( rtx->awk, rex_code, ((rtx->gbl.ignorecase)? QSE_REX_IGNORECASE: 0), - out.u.cpldup.ptr, out.u.cpldup.len, - out.u.cpldup.ptr, out.u.cpldup.len, + &out.u.cpldup, &out.u.cpldup, QSE_NULL, &errnum); if (n == -1) { @@ -4963,7 +4952,7 @@ static qse_awk_val_t* eval_unary (qse_awk_rtx_t* run, qse_awk_nde_t* nde) if (left->type == QSE_AWK_VAL_STR) { res = qse_awk_rtx_makeintval ( - run, !(((qse_awk_val_str_t*)left)->len > 0)); + run, !(((qse_awk_val_str_t*)left)->val.len > 0)); } else { @@ -6468,7 +6457,7 @@ static int read_record (qse_awk_rtx_t* run) static int shorten_record (qse_awk_rtx_t* run, qse_size_t nflds) { qse_awk_val_t* v; - qse_char_t* ofs_free = QSE_NULL, * ofs; + qse_char_t* ofs_free = QSE_NULL, * ofs_ptr; qse_size_t ofs_len, i; qse_str_t tmp; @@ -6482,13 +6471,13 @@ static int shorten_record (qse_awk_rtx_t* run, qse_size_t nflds) if (v->type == QSE_AWK_VAL_NIL) { /* OFS not set */ - ofs = QSE_T(" "); + ofs_ptr = QSE_T(" "); ofs_len = 1; } else if (v->type == QSE_AWK_VAL_STR) { - ofs = ((qse_awk_val_str_t*)v)->ptr; - ofs_len = ((qse_awk_val_str_t*)v)->len; + ofs_ptr = ((qse_awk_val_str_t*)v)->val.ptr; + ofs_len = ((qse_awk_val_str_t*)v)->val.len; } else { @@ -6500,9 +6489,9 @@ static int shorten_record (qse_awk_rtx_t* run, qse_size_t nflds) return -1; } - ofs = out.u.cpldup.ptr; + ofs_ptr = out.u.cpldup.ptr; ofs_len = out.u.cpldup.len; - ofs_free = ofs; + ofs_free = ofs_ptr; } } @@ -6518,7 +6507,7 @@ static int shorten_record (qse_awk_rtx_t* run, qse_size_t nflds) for (i = 0; i < nflds; i++) { - if (i > 0 && qse_str_ncat(&tmp,ofs,ofs_len) == (qse_size_t)-1) + if (i > 0 && qse_str_ncat(&tmp,ofs_ptr,ofs_len) == (qse_size_t)-1) { qse_str_fini (&tmp); if (ofs_free != QSE_NULL) @@ -7204,10 +7193,10 @@ qse_char_t* qse_awk_rtx_format ( } else if (v->type == QSE_AWK_VAL_STR) { - ch_len = ((qse_awk_val_str_t*)v)->len; + ch_len = ((qse_awk_val_str_t*)v)->val.len; if (ch_len > 0) { - ch = ((qse_awk_val_str_t*)v)->ptr[0]; + ch = ((qse_awk_val_str_t*)v)->val.ptr[0]; ch_len = 1; } else ch = QSE_T('\0'); @@ -7268,7 +7257,7 @@ qse_char_t* qse_awk_rtx_format ( } else if (fmt[i] == QSE_T('s')) { - qse_char_t* str, * str_free = QSE_NULL; + qse_char_t* str_ptr, * str_free = QSE_NULL; qse_size_t str_len; qse_long_t k; qse_awk_val_t* v; @@ -7303,13 +7292,13 @@ qse_char_t* qse_awk_rtx_format ( qse_awk_rtx_refupval (rtx, v); if (v->type == QSE_AWK_VAL_NIL) { - str = QSE_T(""); + str_ptr = QSE_T(""); str_len = 0; } else if (v->type == QSE_AWK_VAL_STR) { - str = ((qse_awk_val_str_t*)v)->ptr; - str_len = ((qse_awk_val_str_t*)v)->len; + str_ptr = ((qse_awk_val_str_t*)v)->val.ptr; + str_len = ((qse_awk_val_str_t*)v)->val.len; } else { @@ -7329,9 +7318,9 @@ qse_char_t* qse_awk_rtx_format ( return QSE_NULL; } - str = out.u.cpldup.ptr; + str_ptr = out.u.cpldup.ptr; str_len = out.u.cpldup.len; - str_free = str; + str_free = str_ptr; } if (prec == -1 || prec > (qse_long_t)str_len) prec = (qse_long_t)str_len; @@ -7355,7 +7344,7 @@ qse_char_t* qse_awk_rtx_format ( for (k = 0; k < prec; k++) { - if (qse_str_ccat (out, str[k]) == -1) + if (qse_str_ccat (out, str_ptr[k]) == -1) { if (str_free != QSE_NULL) QSE_AWK_FREE (rtx->awk, str_free); diff --git a/qse/lib/awk/std.c b/qse/lib/awk/std.c index 37174913..a4bdfe4a 100644 --- a/qse/lib/awk/std.c +++ b/qse/lib/awk/std.c @@ -1,5 +1,5 @@ /* - * $Id: std.c 461 2011-05-18 02:32:39Z hyunghwan.chung $ + * $Id: std.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -1414,8 +1414,8 @@ static int fnc_system (qse_awk_rtx_t* run, const qse_cstr_t* fnm) v = qse_awk_rtx_getarg (run, 0); if (v->type == QSE_AWK_VAL_STR) { - str = ((qse_awk_val_str_t*)v)->ptr; - len = ((qse_awk_val_str_t*)v)->len; + str = ((qse_awk_val_str_t*)v)->val.ptr; + len = ((qse_awk_val_str_t*)v)->val.len; } else { diff --git a/qse/lib/awk/val.c b/qse/lib/awk/val.c index e4c6292c..e34bc295 100644 --- a/qse/lib/awk/val.c +++ b/qse/lib/awk/val.c @@ -1,5 +1,5 @@ /* - * $Id: val.c 458 2011-05-13 04:06:55Z hyunghwan.chung $ + * $Id: val.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -27,7 +27,7 @@ #define CHUNKSIZE QSE_AWK_VAL_CHUNK_SIZE static qse_awk_val_nil_t awk_nil = { QSE_AWK_VAL_NIL, 0, 0 }; -static qse_awk_val_str_t awk_zls = { QSE_AWK_VAL_STR, 0, 0, QSE_T(""), 0 }; +static qse_awk_val_str_t awk_zls = { QSE_AWK_VAL_STR, 0, 0, { QSE_T(""), 0 } }; 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; @@ -225,9 +225,9 @@ init: val->type = QSE_AWK_VAL_STR; val->ref = 0; val->nstr = 0; - val->len = len; - val->ptr = (qse_char_t*)(val + 1); - qse_strncpy (val->ptr, str, len); + val->val.len = len; + val->val.ptr = (qse_char_t*)(val + 1); + qse_strncpy (val->val.ptr, str, len); #ifdef DEBUG_VAL qse_dprintf (QSE_T("makestrval => %p\n"), val); @@ -251,8 +251,8 @@ qse_awk_val_t* qse_awk_rtx_makestrval_nodup ( val->type = QSE_AWK_VAL_STR; val->ref = 0; val->nstr = 0; - val->len = len; - val->ptr = str; + val->val.len = len; + val->val.ptr = str; return (qse_awk_val_t*)val; } @@ -295,10 +295,10 @@ init: val->type = QSE_AWK_VAL_STR; val->ref = 0; val->nstr = 0; - val->len = len1 + len2; - val->ptr = (qse_char_t*)(val + 1); - qse_strncpy (val->ptr, str1, len1); - qse_strncpy (&val->ptr[len1], str2, len2); + 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 qse_dprintf (QSE_T("makestrval2 => %p\n"), val); @@ -623,7 +623,7 @@ void qse_awk_rtx_freeval ( qse_awk_val_str_t* v = (qse_awk_val_str_t*)val; int i; - i = v->len / FEATURE_SCACHE_BLOCK_UNIT; + i = v->val.len / FEATURE_SCACHE_BLOCK_UNIT; if (i < QSE_COUNTOF(rtx->scache_count) && rtx->scache_count[i] < QSE_COUNTOF(rtx->scache[i])) { @@ -738,7 +738,7 @@ qse_bool_t qse_awk_rtx_valtobool (qse_awk_rtx_t* run, qse_awk_val_t* val) case QSE_AWK_VAL_REAL: return ((qse_awk_val_real_t*)val)->val != 0.0; case QSE_AWK_VAL_STR: - return ((qse_awk_val_str_t*)val)->len > 0; + return ((qse_awk_val_str_t*)val)->val.len > 0; case QSE_AWK_VAL_REX: /* TODO: is this correct? */ return ((qse_awk_val_rex_t*)val)->len > 0; case QSE_AWK_VAL_MAP: @@ -1129,7 +1129,7 @@ qse_char_t* qse_awk_rtx_valtostr ( case QSE_AWK_VAL_STR: { qse_awk_val_str_t* vs = (qse_awk_val_str_t*)v; - return str_to_str (rtx, vs->ptr, vs->len, out); + return str_to_str (rtx, vs->val.ptr, vs->val.len, out); } } @@ -1180,8 +1180,8 @@ int qse_awk_rtx_valtonum ( { return qse_awk_rtx_strtonum ( rtx, 0, - ((qse_awk_val_str_t*)v)->ptr, - ((qse_awk_val_str_t*)v)->len, + ((qse_awk_val_str_t*)v)->val.ptr, + ((qse_awk_val_str_t*)v)->val.len, l, r ); } diff --git a/qse/lib/cmn/opt.c b/qse/lib/cmn/opt.c index 1e434da7..8942bd33 100644 --- a/qse/lib/cmn/opt.c +++ b/qse/lib/cmn/opt.c @@ -1,5 +1,5 @@ /* - * $Id: opt.c 441 2011-04-22 14:28:43Z hyunghwan.chung $ + * $Id: opt.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -123,9 +123,10 @@ qse_cint_t qse_getopt (int argc, qse_char_t* const* argv, qse_opt_t* opt) while (*end != QSE_T('\0') && *end != QSE_T('=')) end++; - for (o = opt->lng; o->str != QSE_NULL; o++) + for (o = opt->lng; o->str; o++) { const qse_char_t* str = o->str; + if (*str == QSE_T(':')) str++; if (qse_strxcmp (opt->cur, end-opt->cur, str) != 0) continue; diff --git a/qse/lib/cmn/rex.c b/qse/lib/cmn/rex.c index 404d57ea..546dcfda 100644 --- a/qse/lib/cmn/rex.c +++ b/qse/lib/cmn/rex.c @@ -1,5 +1,5 @@ /* - * $Id: rex.c 441 2011-04-22 14:28:43Z hyunghwan.chung $ + * $Id: rex.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -2070,25 +2070,18 @@ void* qse_buildrex ( int qse_matchrex ( - qse_mmgr_t* mmgr, qse_size_t depth, - void* code, int option, - const qse_char_t* str, qse_size_t len, - const qse_char_t* substr, qse_size_t sublen, - qse_cstr_t* match, qse_rex_errnum_t* errnum) + qse_mmgr_t* mmgr, qse_size_t depth, + void* code, int option, + const qse_cstr_t* str, const qse_cstr_t* substr, + qse_cstr_t* match, qse_rex_errnum_t* errnum) { qse_rex_t rex; - qse_cstr_t s, ss; int n; qse_rex_init (&rex, mmgr, code); qse_rex_setoption (&rex, option); - s.ptr = str; - s.len = len; - ss.ptr = substr; - ss.len = sublen; - - if ((n = qse_rex_exec (&rex, &s, &ss, match)) <= -1) + if ((n = qse_rex_exec (&rex, str, substr, match)) <= -1) { *errnum = rex.errnum; qse_rex_yield (&rex); diff --git a/qse/lib/cmn/str_dynm.c b/qse/lib/cmn/str_dynm.c index 59f993d3..36d6bca3 100644 --- a/qse/lib/cmn/str_dynm.c +++ b/qse/lib/cmn/str_dynm.c @@ -1,5 +1,5 @@ /* - * $Id: str_dynm.c 443 2011-04-25 14:56:05Z hyunghwan.chung $ + * $Id: str_dynm.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -64,16 +64,16 @@ qse_mbs_t* qse_mbs_init (qse_mbs_t* str, qse_mmgr_t* mmgr, qse_size_t capa) str->mmgr = mmgr; str->sizer = QSE_NULL; - if (capa == 0) str->ptr = QSE_NULL; + if (capa == 0) str->val.ptr = QSE_NULL; else { - str->ptr = (qse_mchar_t*) QSE_MMGR_ALLOC ( + str->val.ptr = (qse_mchar_t*) QSE_MMGR_ALLOC ( mmgr, QSE_SIZEOF(qse_mchar_t) * (capa + 1)); - if (str->ptr == QSE_NULL) return QSE_NULL; - str->ptr[0] = QSE_MT('\0'); + if (str->val.ptr == QSE_NULL) return QSE_NULL; + str->val.ptr[0] = QSE_MT('\0'); } - str->len = 0; + str->val.len = 0; str->capa = capa; return str; @@ -81,7 +81,7 @@ qse_mbs_t* qse_mbs_init (qse_mbs_t* str, qse_mmgr_t* mmgr, qse_size_t capa) void qse_mbs_fini (qse_mbs_t* str) { - if (str->ptr != QSE_NULL) QSE_MMGR_FREE (str->mmgr, str->ptr); + if (str->val.ptr != QSE_NULL) QSE_MMGR_FREE (str->mmgr, str->val.ptr); } int qse_mbs_yield (qse_mbs_t* str, qse_mxstr_t* buf, qse_size_t new_capa) @@ -99,12 +99,12 @@ int qse_mbs_yield (qse_mbs_t* str, qse_mxstr_t* buf, qse_size_t new_capa) if (buf != QSE_NULL) { - buf->ptr = str->ptr; - buf->len = str->len; + buf->ptr = str->val.ptr; + buf->len = str->val.len; } - str->ptr = tmp; - str->len = 0; + str->val.ptr = tmp; + str->val.len = 0; str->capa = new_capa; return 0; @@ -131,10 +131,10 @@ qse_size_t qse_mbs_setcapa (qse_mbs_t* str, qse_size_t capa) if (capa == str->capa) return capa; - if (str->mmgr->realloc != QSE_NULL && str->ptr != QSE_NULL) + if (str->mmgr->realloc != QSE_NULL && str->val.ptr != QSE_NULL) { tmp = (qse_mchar_t*) QSE_MMGR_REALLOC ( - str->mmgr, str->ptr, + str->mmgr, str->val.ptr, QSE_SIZEOF(qse_mchar_t)*(capa+1)); if (tmp == QSE_NULL) return (qse_size_t)-1; } @@ -144,23 +144,23 @@ qse_size_t qse_mbs_setcapa (qse_mbs_t* str, qse_size_t capa) str->mmgr, QSE_SIZEOF(qse_mchar_t)*(capa+1)); if (tmp == QSE_NULL) return (qse_size_t)-1; - if (str->ptr != QSE_NULL) + if (str->val.ptr != QSE_NULL) { - qse_size_t ncopy = (str->len <= capa)? str->len: capa; - QSE_MEMCPY (tmp, str->ptr, + qse_size_t ncopy = (str->val.len <= capa)? str->val.len: capa; + QSE_MEMCPY (tmp, str->val.ptr, QSE_SIZEOF(qse_mchar_t)*(ncopy+1)); - QSE_MMGR_FREE (str->mmgr, str->ptr); + QSE_MMGR_FREE (str->mmgr, str->val.ptr); } } - if (capa < str->len) + if (capa < str->val.len) { - str->len = capa; + str->val.len = capa; tmp[capa] = QSE_MT('\0'); } str->capa = capa; - str->ptr = tmp; + str->val.ptr = tmp; return str->capa; } @@ -172,11 +172,11 @@ qse_size_t qse_mbs_getlen (qse_mbs_t* str) qse_size_t qse_mbs_setlen (qse_mbs_t* str, qse_size_t len) { - if (len == str->len) return len; - if (len < str->len) + if (len == str->val.len) return len; + if (len < str->val.len) { - str->len = len; - str->ptr[len] = QSE_MT('\0'); + str->val.len = len; + str->val.ptr[len] = QSE_MT('\0'); return len; } @@ -186,17 +186,17 @@ qse_size_t qse_mbs_setlen (qse_mbs_t* str, qse_size_t len) return (qse_size_t)-1; } - while (str->len < len) str->ptr[str->len++] = QSE_MT(' '); - return str->len; + while (str->val.len < len) str->val.ptr[str->val.len++] = QSE_MT(' '); + return str->val.len; } void qse_mbs_clear (qse_mbs_t* str) { - str->len = 0; - if (str->ptr != QSE_NULL) + str->val.len = 0; + if (str->val.ptr != QSE_NULL) { QSE_ASSERT (str->capa >= 1); - str->ptr[0] = QSE_MT('\0'); + str->val.ptr[0] = QSE_MT('\0'); } } @@ -204,18 +204,18 @@ void qse_mbs_swap (qse_mbs_t* str, qse_mbs_t* str1) { qse_mbs_t tmp; - tmp.ptr = str->ptr; - tmp.len = str->len; + tmp.val.ptr = str->val.ptr; + tmp.val.len = str->val.len; tmp.capa = str->capa; tmp.mmgr = str->mmgr; - str->ptr = str1->ptr; - str->len = str1->len; + str->val.ptr = str1->val.ptr; + str->val.len = str1->val.len; str->capa = str1->capa; str->mmgr = str1->mmgr; - str1->ptr = tmp.ptr; - str1->len = tmp.len; + str1->val.ptr = tmp.val.ptr; + str1->val.len = tmp.val.len; str1->capa = tmp.capa; str1->mmgr = tmp.mmgr; } @@ -228,7 +228,7 @@ qse_size_t qse_mbs_cpy (qse_mbs_t* str, const qse_mchar_t* s) qse_size_t qse_mbs_ncpy (qse_mbs_t* str, const qse_mchar_t* s, qse_size_t len) { - if (len > str->capa || str->ptr == QSE_NULL) + if (len > str->capa || str->val.ptr == QSE_NULL) { qse_mchar_t* buf; @@ -236,14 +236,14 @@ qse_size_t qse_mbs_ncpy (qse_mbs_t* str, const qse_mchar_t* s, qse_size_t len) str->mmgr, QSE_SIZEOF(qse_mchar_t) * (len + 1)); if (buf == QSE_NULL) return (qse_size_t)-1; - if (str->ptr != QSE_NULL) QSE_MMGR_FREE (str->mmgr, str->ptr); + if (str->val.ptr != QSE_NULL) QSE_MMGR_FREE (str->mmgr, str->val.ptr); str->capa = len; - str->ptr = buf; + str->val.ptr = buf; } - str->len = qse_mbsncpy (str->ptr, s, len); - str->ptr[str->len] = QSE_MT('\0'); - return str->len; + str->val.len = qse_mbsncpy (str->val.ptr, s, len); + str->val.ptr[str->val.len] = QSE_MT('\0'); + return str->val.len; } qse_size_t qse_mbs_cat (qse_mbs_t* str, const qse_mchar_t* s) @@ -254,13 +254,13 @@ qse_size_t qse_mbs_cat (qse_mbs_t* str, const qse_mchar_t* s) qse_size_t qse_mbs_ncat (qse_mbs_t* str, const qse_mchar_t* s, qse_size_t len) { - if (len > str->capa - str->len) + if (len > str->capa - str->val.len) { qse_size_t ncapa, mincapa; /* let the minimum capacity be as large as * to fit in the new substring */ - mincapa = str->len + len; + mincapa = str->val.len + len; if (str->sizer == QSE_NULL) { @@ -276,7 +276,7 @@ qse_size_t qse_mbs_ncat (qse_mbs_t* str, const qse_mchar_t* s, qse_size_t len) * pass the minimum capacity required as a hint */ ncapa = str->sizer (str, mincapa); /* if no change in capacity, return current length */ - if (ncapa == str->capa) return str->len; + if (ncapa == str->capa) return str->val.len; } /* change the capacity */ @@ -289,21 +289,21 @@ qse_size_t qse_mbs_ncat (qse_mbs_t* str, const qse_mchar_t* s, qse_size_t len) while (1); } - if (len > str->capa - str->len) + if (len > str->capa - str->val.len) { /* copy as many characters as the number of cells available. * if the capacity has been decreased, len is adjusted here */ - len = str->capa - str->len; + len = str->capa - str->val.len; } if (len > 0) { - QSE_MEMCPY (&str->ptr[str->len], s, len*QSE_SIZEOF(*s)); - str->len += len; - str->ptr[str->len] = QSE_MT('\0'); + QSE_MEMCPY (&str->val.ptr[str->val.len], s, len*QSE_SIZEOF(*s)); + str->val.len += len; + str->val.ptr[str->val.len] = QSE_MT('\0'); } - return str->len; + return str->val.len; } qse_size_t qse_mbs_ccat (qse_mbs_t* str, qse_mchar_t c) @@ -322,49 +322,49 @@ qse_size_t qse_mbs_nccat (qse_mbs_t* str, qse_mchar_t c, qse_size_t len) len--; } - return str->len; + return str->val.len; } qse_size_t qse_mbs_del (qse_mbs_t* str, qse_size_t index, qse_size_t size) { - if (str->ptr != QSE_NULL && index < str->len && size > 0) + if (str->val.ptr != QSE_NULL && index < str->val.len && size > 0) { qse_size_t nidx = index + size; - if (nidx >= str->len) + if (nidx >= str->val.len) { - str->ptr[index] = QSE_MT('\0'); - str->len = index; + str->val.ptr[index] = QSE_MT('\0'); + str->val.len = index; } else { qse_mbsncpy ( - &str->ptr[index], &str->ptr[nidx], - str->len - nidx); - str->len -= size; + &str->val.ptr[index], &str->val.ptr[nidx], + str->val.len - nidx); + str->val.len -= size; } } - return str->len; + return str->val.len; } qse_size_t qse_mbs_trm (qse_mbs_t* str) { - if (str->ptr != QSE_NULL) + if (str->val.ptr != QSE_NULL) { - str->len = qse_mbsxtrm (str->ptr, str->len); + str->val.len = qse_mbsxtrm (str->val.ptr, str->val.len); } - return str->len; + return str->val.len; } qse_size_t qse_mbs_pac (qse_mbs_t* str) { - if (str->ptr != QSE_NULL) + if (str->val.ptr != QSE_NULL) { - str->len = qse_mbsxpac (str->ptr, str->len); + str->val.len = qse_mbsxpac (str->val.ptr, str->val.len); } - return str->len; + return str->val.len; } diff --git a/qse/lib/cmn/str_dynw.c b/qse/lib/cmn/str_dynw.c index 98cb2d50..17b97228 100644 --- a/qse/lib/cmn/str_dynw.c +++ b/qse/lib/cmn/str_dynw.c @@ -1,5 +1,5 @@ /* - * $Id: str_dynw.c 443 2011-04-25 14:56:05Z hyunghwan.chung $ + * $Id: str_dynw.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -64,16 +64,16 @@ qse_wcs_t* qse_wcs_init (qse_wcs_t* str, qse_mmgr_t* mmgr, qse_size_t capa) str->mmgr = mmgr; str->sizer = QSE_NULL; - if (capa == 0) str->ptr = QSE_NULL; + if (capa == 0) str->val.ptr = QSE_NULL; else { - str->ptr = (qse_wchar_t*) QSE_MMGR_ALLOC ( + str->val.ptr = (qse_wchar_t*) QSE_MMGR_ALLOC ( mmgr, QSE_SIZEOF(qse_wchar_t) * (capa + 1)); - if (str->ptr == QSE_NULL) return QSE_NULL; - str->ptr[0] = QSE_WT('\0'); + if (str->val.ptr == QSE_NULL) return QSE_NULL; + str->val.ptr[0] = QSE_WT('\0'); } - str->len = 0; + str->val.len = 0; str->capa = capa; return str; @@ -81,7 +81,7 @@ qse_wcs_t* qse_wcs_init (qse_wcs_t* str, qse_mmgr_t* mmgr, qse_size_t capa) void qse_wcs_fini (qse_wcs_t* str) { - if (str->ptr != QSE_NULL) QSE_MMGR_FREE (str->mmgr, str->ptr); + if (str->val.ptr != QSE_NULL) QSE_MMGR_FREE (str->mmgr, str->val.ptr); } int qse_wcs_yield (qse_wcs_t* str, qse_wxstr_t* buf, qse_size_t new_capa) @@ -99,12 +99,12 @@ int qse_wcs_yield (qse_wcs_t* str, qse_wxstr_t* buf, qse_size_t new_capa) if (buf != QSE_NULL) { - buf->ptr = str->ptr; - buf->len = str->len; + buf->ptr = str->val.ptr; + buf->len = str->val.len; } - str->ptr = tmp; - str->len = 0; + str->val.ptr = tmp; + str->val.len = 0; str->capa = new_capa; return 0; @@ -131,10 +131,10 @@ qse_size_t qse_wcs_setcapa (qse_wcs_t* str, qse_size_t capa) if (capa == str->capa) return capa; - if (str->mmgr->realloc != QSE_NULL && str->ptr != QSE_NULL) + if (str->mmgr->realloc != QSE_NULL && str->val.ptr != QSE_NULL) { tmp = (qse_wchar_t*) QSE_MMGR_REALLOC ( - str->mmgr, str->ptr, + str->mmgr, str->val.ptr, QSE_SIZEOF(qse_wchar_t)*(capa+1)); if (tmp == QSE_NULL) return (qse_size_t)-1; } @@ -144,23 +144,23 @@ qse_size_t qse_wcs_setcapa (qse_wcs_t* str, qse_size_t capa) str->mmgr, QSE_SIZEOF(qse_wchar_t)*(capa+1)); if (tmp == QSE_NULL) return (qse_size_t)-1; - if (str->ptr != QSE_NULL) + if (str->val.ptr != QSE_NULL) { - qse_size_t ncopy = (str->len <= capa)? str->len: capa; - QSE_MEMCPY (tmp, str->ptr, + qse_size_t ncopy = (str->val.len <= capa)? str->val.len: capa; + QSE_MEMCPY (tmp, str->val.ptr, QSE_SIZEOF(qse_wchar_t)*(ncopy+1)); - QSE_MMGR_FREE (str->mmgr, str->ptr); + QSE_MMGR_FREE (str->mmgr, str->val.ptr); } } - if (capa < str->len) + if (capa < str->val.len) { - str->len = capa; + str->val.len = capa; tmp[capa] = QSE_WT('\0'); } str->capa = capa; - str->ptr = tmp; + str->val.ptr = tmp; return str->capa; } @@ -172,11 +172,11 @@ qse_size_t qse_wcs_getlen (qse_wcs_t* str) qse_size_t qse_wcs_setlen (qse_wcs_t* str, qse_size_t len) { - if (len == str->len) return len; - if (len < str->len) + if (len == str->val.len) return len; + if (len < str->val.len) { - str->len = len; - str->ptr[len] = QSE_WT('\0'); + str->val.len = len; + str->val.ptr[len] = QSE_WT('\0'); return len; } @@ -186,17 +186,17 @@ qse_size_t qse_wcs_setlen (qse_wcs_t* str, qse_size_t len) return (qse_size_t)-1; } - while (str->len < len) str->ptr[str->len++] = QSE_WT(' '); - return str->len; + while (str->val.len < len) str->val.ptr[str->val.len++] = QSE_WT(' '); + return str->val.len; } void qse_wcs_clear (qse_wcs_t* str) { - str->len = 0; - if (str->ptr != QSE_NULL) + str->val.len = 0; + if (str->val.ptr != QSE_NULL) { QSE_ASSERT (str->capa >= 1); - str->ptr[0] = QSE_WT('\0'); + str->val.ptr[0] = QSE_WT('\0'); } } @@ -204,18 +204,18 @@ void qse_wcs_swap (qse_wcs_t* str, qse_wcs_t* str1) { qse_wcs_t tmp; - tmp.ptr = str->ptr; - tmp.len = str->len; + tmp.val.ptr = str->val.ptr; + tmp.val.len = str->val.len; tmp.capa = str->capa; tmp.mmgr = str->mmgr; - str->ptr = str1->ptr; - str->len = str1->len; + str->val.ptr = str1->val.ptr; + str->val.len = str1->val.len; str->capa = str1->capa; str->mmgr = str1->mmgr; - str1->ptr = tmp.ptr; - str1->len = tmp.len; + str1->val.ptr = tmp.val.ptr; + str1->val.len = tmp.val.len; str1->capa = tmp.capa; str1->mmgr = tmp.mmgr; } @@ -228,7 +228,7 @@ qse_size_t qse_wcs_cpy (qse_wcs_t* str, const qse_wchar_t* s) qse_size_t qse_wcs_ncpy (qse_wcs_t* str, const qse_wchar_t* s, qse_size_t len) { - if (len > str->capa || str->ptr == QSE_NULL) + if (len > str->capa || str->val.ptr == QSE_NULL) { qse_wchar_t* buf; @@ -236,14 +236,14 @@ qse_size_t qse_wcs_ncpy (qse_wcs_t* str, const qse_wchar_t* s, qse_size_t len) str->mmgr, QSE_SIZEOF(qse_wchar_t) * (len + 1)); if (buf == QSE_NULL) return (qse_size_t)-1; - if (str->ptr != QSE_NULL) QSE_MMGR_FREE (str->mmgr, str->ptr); + if (str->val.ptr != QSE_NULL) QSE_MMGR_FREE (str->mmgr, str->val.ptr); str->capa = len; - str->ptr = buf; + str->val.ptr = buf; } - str->len = qse_wcsncpy (str->ptr, s, len); - str->ptr[str->len] = QSE_WT('\0'); - return str->len; + str->val.len = qse_wcsncpy (str->val.ptr, s, len); + str->val.ptr[str->val.len] = QSE_WT('\0'); + return str->val.len; } qse_size_t qse_wcs_cat (qse_wcs_t* str, const qse_wchar_t* s) @@ -254,13 +254,13 @@ qse_size_t qse_wcs_cat (qse_wcs_t* str, const qse_wchar_t* s) qse_size_t qse_wcs_ncat (qse_wcs_t* str, const qse_wchar_t* s, qse_size_t len) { - if (len > str->capa - str->len) + if (len > str->capa - str->val.len) { qse_size_t ncapa, mincapa; /* let the minimum capacity be as large as * to fit in the new substring */ - mincapa = str->len + len; + mincapa = str->val.len + len; if (str->sizer == QSE_NULL) { @@ -276,7 +276,7 @@ qse_size_t qse_wcs_ncat (qse_wcs_t* str, const qse_wchar_t* s, qse_size_t len) * pass the minimum capacity required as a hint */ ncapa = str->sizer (str, mincapa); /* if no change in capacity, return current length */ - if (ncapa == str->capa) return str->len; + if (ncapa == str->capa) return str->val.len; } /* change the capacity */ @@ -289,21 +289,21 @@ qse_size_t qse_wcs_ncat (qse_wcs_t* str, const qse_wchar_t* s, qse_size_t len) while (1); } - if (len > str->capa - str->len) + if (len > str->capa - str->val.len) { /* copy as many characters as the number of cells available. * if the capacity has been decreased, len is adjusted here */ - len = str->capa - str->len; + len = str->capa - str->val.len; } if (len > 0) { - QSE_MEMCPY (&str->ptr[str->len], s, len*QSE_SIZEOF(*s)); - str->len += len; - str->ptr[str->len] = QSE_WT('\0'); + QSE_MEMCPY (&str->val.ptr[str->val.len], s, len*QSE_SIZEOF(*s)); + str->val.len += len; + str->val.ptr[str->val.len] = QSE_WT('\0'); } - return str->len; + return str->val.len; } qse_size_t qse_wcs_ccat (qse_wcs_t* str, qse_wchar_t c) @@ -322,49 +322,49 @@ qse_size_t qse_wcs_nccat (qse_wcs_t* str, qse_wchar_t c, qse_size_t len) len--; } - return str->len; + return str->val.len; } qse_size_t qse_wcs_del (qse_wcs_t* str, qse_size_t index, qse_size_t size) { - if (str->ptr != QSE_NULL && index < str->len && size > 0) + if (str->val.ptr != QSE_NULL && index < str->val.len && size > 0) { qse_size_t nidx = index + size; - if (nidx >= str->len) + if (nidx >= str->val.len) { - str->ptr[index] = QSE_WT('\0'); - str->len = index; + str->val.ptr[index] = QSE_WT('\0'); + str->val.len = index; } else { qse_wcsncpy ( - &str->ptr[index], &str->ptr[nidx], - str->len - nidx); - str->len -= size; + &str->val.ptr[index], &str->val.ptr[nidx], + str->val.len - nidx); + str->val.len -= size; } } - return str->len; + return str->val.len; } qse_size_t qse_wcs_trm (qse_wcs_t* str) { - if (str->ptr != QSE_NULL) + if (str->val.ptr != QSE_NULL) { - str->len = qse_wcsxtrm (str->ptr, str->len); + str->val.len = qse_wcsxtrm (str->val.ptr, str->val.len); } - return str->len; + return str->val.len; } qse_size_t qse_wcs_pac (qse_wcs_t* str) { - if (str->ptr != QSE_NULL) + if (str->val.ptr != QSE_NULL) { - str->len = qse_wcsxpac (str->ptr, str->len); + str->val.len = qse_wcsxpac (str->val.ptr, str->val.len); } - return str->len; + return str->val.len; } diff --git a/qse/lib/scm/read.c b/qse/lib/scm/read.c index 2a27c3e7..4ff2c29b 100644 --- a/qse/lib/scm/read.c +++ b/qse/lib/scm/read.c @@ -53,8 +53,8 @@ enum tok_type_t #define TOK_IVAL(scm) (scm)->r.t.ival #define TOK_RVAL(scm) (scm)->r.t.rval #define TOK_NAME(scm) (&(scm)->r.t.name) -#define TOK_NAME_PTR(scm) TOK_NAME(scm)->ptr -#define TOK_NAME_LEN(scm) TOK_NAME(scm)->len +#define TOK_NAME_PTR(scm) QSE_STR_PTR(TOK_NAME(scm)) +#define TOK_NAME_LEN(scm) QSE_STR_LEN(TOK_NAME(scm)) #define TOK_LOC(scm) (scm)->r.t.loc #define TOK_ADD_CHAR(scm,ch) QSE_BLOCK (\ diff --git a/qse/lib/sed/sed.c b/qse/lib/sed/sed.c index cc9f0546..63f478e3 100644 --- a/qse/lib/sed/sed.c +++ b/qse/lib/sed/sed.c @@ -1,5 +1,5 @@ /* - * $Id: sed.c 441 2011-04-22 14:28:43Z hyunghwan.chung $ + * $Id: sed.c 462 2011-05-18 14:36:40Z hyunghwan.chung $ * Copyright 2006-2011 Chung, Hyung-Hwan. This file is part of QSE. @@ -1832,24 +1832,24 @@ static int do_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd) qse_cstr_t mat, pmat; int opt = 0, repl = 0, n; qse_rex_errnum_t errnum; - const qse_char_t* cur_ptr, * str_ptr, * str_end; - qse_size_t cur_len, str_len, m, i; - qse_size_t max_count, sub_count; + + qse_cstr_t str, cur; + const qse_char_t* str_end; + qse_size_t m, i, max_count, sub_count; QSE_ASSERT (cmd->type == QSE_SED_CMD_SUBSTITUTE); qse_str_clear (&sed->e.txt.subst); if (cmd->u.subst.i) opt = QSE_REX_IGNORECASE; - str_ptr = QSE_STR_PTR(&sed->e.in.line); - str_len = QSE_STR_LEN(&sed->e.in.line); + str.ptr = QSE_STR_PTR(&sed->e.in.line); + str.len = QSE_STR_LEN(&sed->e.in.line); /* TODO: support different line end convension */ - if (str_len > 0 && str_ptr[str_len-1] == QSE_T('\n')) str_len--; + if (str.len > 0 && str.ptr[str.len-1] == QSE_T('\n')) str.len--; - str_end = str_ptr + str_len; - cur_ptr = str_ptr; - cur_len = str_len; + str_end = str.ptr + str.len; + cur = str; sub_count = 0; max_count = (cmd->u.subst.g)? 0: cmd->u.subst.occ; @@ -1859,7 +1859,7 @@ static int do_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd) /* perform test when cur_ptr == str_end also because * end of string($) needs to be tested */ - while (cur_ptr <= str_end) + while (cur.ptr <= str_end) { if (max_count == 0 || sub_count < max_count) { @@ -1867,9 +1867,7 @@ static int do_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd) sed->mmgr, sed->depth.rex.match, cmd->u.subst.rex, opt, - str_ptr, str_len, - cur_ptr, cur_len, - &mat, &errnum + &str, &cur, &mat, &errnum ); } else n = 0; @@ -1885,7 +1883,7 @@ static int do_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd) /* no more match found */ if (qse_str_ncat ( &sed->e.txt.subst, - cur_ptr, cur_len) == (qse_size_t)-1) + cur.ptr, cur.len) == (qse_size_t)-1) { SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL); return -1; @@ -1906,7 +1904,7 @@ static int do_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd) { m = qse_str_ncat ( &sed->e.txt.subst, - cur_ptr, mat.ptr-cur_ptr+mat.len + cur.ptr, mat.ptr-cur.ptr+mat.len ); if (m == (qse_size_t)-1) @@ -1920,7 +1918,7 @@ static int do_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd) repl = 1; m = qse_str_ncat ( - &sed->e.txt.subst, cur_ptr, mat.ptr-cur_ptr); + &sed->e.txt.subst, cur.ptr, mat.ptr-cur.ptr); if (m == (qse_size_t)-1) { SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL); @@ -1959,8 +1957,8 @@ static int do_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd) } sub_count++; - cur_len = cur_len - ((mat.ptr - cur_ptr) + mat.len); - cur_ptr = mat.ptr + mat.len; + cur.len = cur.len - ((mat.ptr - cur.ptr) + mat.len); + cur.ptr = mat.ptr + mat.len; pmat = mat; @@ -1969,18 +1967,18 @@ static int do_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd) skip_one_char: /* special treament is need if the match length is 0 */ - m = qse_str_ncat (&sed->e.txt.subst, cur_ptr, 1); + m = qse_str_ncat (&sed->e.txt.subst, cur.ptr, 1); if (m == (qse_size_t)-1) { SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL); return -1; } - cur_ptr++; cur_len--; + cur.ptr++; cur.len--; } } - if (str_len < QSE_STR_LEN(&sed->e.in.line)) + if (str.len < QSE_STR_LEN(&sed->e.in.line)) { /* TODO: support different line ending convension */ m = qse_str_ccat (&sed->e.txt.subst, QSE_T('\n')); @@ -2034,25 +2032,22 @@ static int match_a (qse_sed_t* sed, qse_sed_cmd_t* cmd, qse_sed_adr_t* a) { int n; qse_cstr_t match; - qse_str_t* line; - qse_size_t llen; + qse_cstr_t line; qse_rex_errnum_t errnum; QSE_ASSERT (a->u.rex != QSE_NULL); - line = &sed->e.in.line; - llen = QSE_STR_LEN(line); + line.ptr = QSE_STR_PTR(&sed->e.in.line); + line.len = QSE_STR_LEN(&sed->e.in.line); - /* TODO: support different line end convension */ - if (llen > 0 && - QSE_STR_CHAR(line,llen-1) == QSE_T('\n')) llen--; + if (line.len > 0 && + line.ptr[line.len-1] == QSE_T('\n')) line.len--; n = qse_matchrex ( sed->mmgr, sed->depth.rex.match, a->u.rex, 0, - QSE_STR_PTR(line), llen, - QSE_STR_PTR(line), llen, + &line, &line, &match, &errnum); if (n <= -1) { @@ -2220,8 +2215,9 @@ static qse_sed_cmd_t* exec_cmd (qse_sed_t* sed, qse_sed_cmd_t* cmd) case QSE_SED_CMD_INSERT: n = write_str (sed, - QSE_STR_PTR(&cmd->u.text), - QSE_STR_LEN(&cmd->u.text)); + cmd->u.text.ptr, + cmd->u.text.len + ); if (n <= -1) return QSE_NULL; break; @@ -2231,8 +2227,9 @@ static qse_sed_cmd_t* exec_cmd (qse_sed_t* sed, qse_sed_cmd_t* cmd) /* change the pattern space */ n = qse_str_ncpy ( &sed->e.in.line, - QSE_STR_PTR(&cmd->u.text), - QSE_STR_LEN(&cmd->u.text)); + cmd->u.text.ptr, + cmd->u.text.len + ); if (n == (qse_size_t)-1) { SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL); diff --git a/qse/regress/awk/Makefile.in b/qse/regress/awk/Makefile.in index 508cdee5..ee7f099b 100644 --- a/qse/regress/awk/Makefile.in +++ b/qse/regress/awk/Makefile.in @@ -294,6 +294,8 @@ EXTRA_DIST = \ lang-041.awk \ lang-042.awk \ lang-043.awk \ + lang-044.awk \ + lang-045.awk \ columnate.awk \ levenshtein.awk \ levenshtein-utests.awk \ @@ -308,6 +310,7 @@ EXTRA_DIST = \ lang-036.dat \ lang-037.dat \ lang-043.dat \ + lang-044.dat \ adr.dat \ asm.dat \ cou.dat \