improved tokenization by rex

This commit is contained in:
hyung-hwan 2011-05-19 08:36:40 +00:00
parent 845b4ae4f0
commit c7a49615a2
22 changed files with 547 additions and 548 deletions

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. 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("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("strictnaming"), QSE_AWK_STRICTNAMING, QSE_T("enable the strict naming rule") },
{ QSE_T("include"), QSE_AWK_INCLUDE, QSE_T("enable 'include'") }, { 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) 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(":assign"), QSE_T('v') },
{ QSE_T(":memory-limit"), QSE_T('m') }, { 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 = static qse_opt_t opt =

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. 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 struct qse_awk_val_str_t
{ {
QSE_AWK_VAL_HDR; QSE_AWK_VAL_HDR;
qse_char_t* ptr; qse_xstr_t val;
qse_size_t len;
}; };
typedef struct qse_awk_val_str_t qse_awk_val_str_t; 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; 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) ( typedef qse_real_t (*qse_awk_math1_t) (
qse_awk_t* awk, qse_awk_t* awk,
qse_real_t x qse_real_t x
@ -366,18 +373,31 @@ typedef qse_real_t (*qse_awk_math2_t) (
qse_real_t y qse_real_t y
); );
typedef qse_real_t (*qse_awk_pow_t) (
qse_awk_t* awk, typedef void* (*qse_awk_buildrex_t) (
qse_real_t x, qse_awk_t* awk,
qse_real_t y const qse_char_t* ptn,
qse_size_t len
); );
typedef int (*qse_awk_sprintf_t) ( typedef int (*qse_awk_matchrex_t) (
qse_awk_t* awk, qse_awk_t* awk,
qse_char_t* buf, void* code,
qse_size_t size, int option,
const qse_char_t* fmt, 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 struct
{ {
/* TODO: accept regular expression handling functions */ /* TODO: accept regular expression handling functions */
void* (*build) ( qse_awk_buildrex_t build;
qse_awk_t* awk, qse_awk_matchrex_t match;
const qse_char_t* ptn, qse_awk_freerex_t free;
qse_size_t len, qse_awk_isemptyrex_t isempty;
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
);
} rex; } rex;
#endif #endif
}; };
@ -723,11 +720,11 @@ enum qse_awk_option_t
QSE_AWK_NEWLINE = (1 << 5), QSE_AWK_NEWLINE = (1 << 5),
/** /**
* strips off leading and trailing spaces when splitting a record * remove empty fields when splitting a record if FS is a regular
* into fields with a regular expression. * expression and the match is all spaces.
* *
* @code * @code
* BEGIN { FS="[:[:space:]]+"; } * BEGIN { FS="[[:space:]]+"; }
* { * {
* print "NF=" NF; * print "NF=" NF;
* for (i = 0; i < NF; i++) print i " [" $(i+1) "]"; * for (i = 0; i < NF; i++) print i " [" $(i+1) "]";
@ -735,6 +732,17 @@ enum qse_awk_option_t
* @endcode * @endcode
* " a b c " is split to [a], [b], [c] if #QSE_AWK_STRIPRECSPC is on. * " a b c " is split to [a], [b], [c] if #QSE_AWK_STRIPRECSPC is on.
* Otherwise, it is split to [], [a], [b], [c], []. * 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), QSE_AWK_STRIPRECSPC = (1 << 6),

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -264,9 +264,9 @@ qse_rex_node_t* qse_rex_comp (
); );
int qse_rex_exec ( int qse_rex_exec (
qse_rex_t* rex, qse_rex_t* rex,
const qse_cstr_t* str, const qse_cstr_t* str,
const qse_cstr_t* substr, const qse_cstr_t* substr,
qse_cstr_t* matstr qse_cstr_t* matstr
); );
@ -285,10 +285,8 @@ int qse_matchrex (
qse_size_t depth, qse_size_t depth,
void* code, void* code,
int option, int option,
const qse_char_t* str, const qse_cstr_t* str,
qse_size_t len, const qse_cstr_t* substr,
const qse_char_t* substr,
qse_size_t sublen,
qse_cstr_t* match, qse_cstr_t* match,
qse_rex_errnum_t* errnum qse_rex_errnum_t* errnum
); );

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -31,17 +31,19 @@
* deal with a string pointer and length in a structure. * deal with a string pointer and length in a structure.
*/ */
#define QSE_MBS_LEN(s) ((s)->len) /**< string length */ #define QSE_MBS_XSTR(s) (&((s)->val)) /**< string pointer and length as a aggregate */
#define QSE_MBS_PTR(s) ((s)->ptr) /**< string buffer pointer */ #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_CAPA(s) ((s)->capa) /**< string buffer capacity */
#define QSE_MBS_CHAR(s,idx) ((s)->ptr[idx]) /**< character at given position */ #define QSE_MBS_CHAR(s,idx) ((s)->val.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_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_XSTR(s) (&((s)->val)) /**< string pointer and length as an aggregate*/
#define QSE_WCS_PTR(s) ((s)->ptr) /**< string buffer pointer */ #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_CAPA(s) ((s)->capa) /**< string buffer capacity */
#define QSE_WCS_CHAR(s,idx) ((s)->ptr[idx]) /**< character at given position */ #define QSE_WCS_CHAR(s,idx) ((s)->val.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_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_mbs_t qse_mbs_t;
typedef struct qse_wcs_t qse_wcs_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 #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_LEN(s) QSE_MBS_LEN(s)
# define QSE_STR_PTR(s) QSE_MBS_PTR(s) # define QSE_STR_PTR(s) QSE_MBS_PTR(s)
# define QSE_STR_CAPA(s) QSE_MBS_CAPA(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_t qse_mbs_t
# define qse_str_sizer_t qse_mbs_sizer_t # define qse_str_sizer_t qse_mbs_sizer_t
#else #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_LEN(s) QSE_WCS_LEN(s)
# define QSE_STR_PTR(s) QSE_WCS_PTR(s) # define QSE_STR_PTR(s) QSE_WCS_PTR(s)
# define QSE_STR_CAPA(s) QSE_WCS_CAPA(s) # define QSE_STR_CAPA(s) QSE_WCS_CAPA(s)
@ -82,8 +86,7 @@ struct qse_mbs_t
{ {
QSE_DEFINE_COMMON_FIELDS (mbs) QSE_DEFINE_COMMON_FIELDS (mbs)
qse_mbs_sizer_t sizer; /**< buffer resizer function */ qse_mbs_sizer_t sizer; /**< buffer resizer function */
qse_mchar_t* ptr; /**< buffer/string pointer */ qse_mxstr_t val; /**< buffer/string pointer and lengh */
qse_size_t len; /**< string length */
qse_size_t capa; /**< buffer capacity */ qse_size_t capa; /**< buffer capacity */
}; };
@ -94,8 +97,7 @@ struct qse_wcs_t
{ {
QSE_DEFINE_COMMON_FIELDS (wcs) QSE_DEFINE_COMMON_FIELDS (wcs)
qse_wcs_sizer_t sizer; /**< buffer resizer function */ qse_wcs_sizer_t sizer; /**< buffer resizer function */
qse_wchar_t* ptr; /**< buffer/string pointer */ qse_wxstr_t val; /**< buffer/string pointer and lengh */
qse_size_t len; /**< string length */
qse_size_t capa; /**< buffer capacity */ qse_size_t capa; /**< buffer capacity */
}; };

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. 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) if (val->type == QSE_AWK_VAL_STR)
{ {
p = ((qse_awk_val_str_t*)val)->ptr; p = ((qse_awk_val_str_t*)val)->val.ptr;
l = ((qse_awk_val_str_t*)val)->len; l = ((qse_awk_val_str_t*)val)->val.len;
} }
else else
{ {

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -296,9 +296,9 @@ struct qse_awk_rtx_t
qse_size_t nflds; /* NF */ qse_size_t nflds; /* NF */
struct struct
{ {
qse_char_t* ptr; const qse_char_t* ptr;
qse_size_t len; qse_size_t len;
qse_awk_val_t* val; /* $1 .. $NF */ qse_awk_val_t* val; /* $1 .. $NF */
}* flds; }* flds;
} inrec; } inrec;
@ -384,7 +384,7 @@ struct qse_awk_rtx_t
#define QSE_AWK_FREEREX(awk,code) qse_freerex((awk)->mmgr,code) #define QSE_AWK_FREEREX(awk,code) qse_freerex((awk)->mmgr,code)
#define QSE_AWK_BUILDREX(awk,ptn,len,errnum) \ #define QSE_AWK_BUILDREX(awk,ptn,len,errnum) \
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) \ #define QSE_AWK_MATCHREX(awk,code,option,str,substr,match,errnum) \
qse_awk_matchrex(awk,code,option,str,len,substr,sublen,match,errnum) qse_awk_matchrex(awk,code,option,str,substr,match,errnum)
#endif #endif

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. 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) if (a0->type == QSE_AWK_VAL_STR)
{ {
name = ((qse_awk_val_str_t*)a0)->ptr; name = ((qse_awk_val_str_t*)a0)->val.ptr;
len = ((qse_awk_val_str_t*)a0)->len; len = ((qse_awk_val_str_t*)a0)->val.len;
} }
else 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) if (a1->type == QSE_AWK_VAL_STR)
{ {
opt = ((qse_awk_val_str_t*)a1)->ptr; opt = ((qse_awk_val_str_t*)a1)->val.ptr;
optlen = ((qse_awk_val_str_t*)a1)->len; optlen = ((qse_awk_val_str_t*)a1)->val.len;
} }
else 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); a0 = qse_awk_rtx_getarg (run, 0);
if (a0->type == QSE_AWK_VAL_STR) if (a0->type == QSE_AWK_VAL_STR)
{ {
str0 = ((qse_awk_val_str_t*)a0)->ptr; str0 = ((qse_awk_val_str_t*)a0)->val.ptr;
len0 = ((qse_awk_val_str_t*)a0)->len; len0 = ((qse_awk_val_str_t*)a0)->val.len;
} }
else 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) if (a0->type == QSE_AWK_VAL_STR)
{ {
str0 = ((qse_awk_val_str_t*)a0)->ptr; str0 = ((qse_awk_val_str_t*)a0)->val.ptr;
len0 = ((qse_awk_val_str_t*)a0)->len; len0 = ((qse_awk_val_str_t*)a0)->val.len;
} }
else 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) if (a1->type == QSE_AWK_VAL_STR)
{ {
str1 = ((qse_awk_val_str_t*)a1)->ptr; str1 = ((qse_awk_val_str_t*)a1)->val.ptr;
len1 = ((qse_awk_val_str_t*)a1)->len; len1 = ((qse_awk_val_str_t*)a1)->val.len;
} }
else 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); v = qse_awk_rtx_getarg (rtx, 0);
if (v->type == QSE_AWK_VAL_STR) 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 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) if (a0->type == QSE_AWK_VAL_STR)
{ {
str = ((qse_awk_val_str_t*)a0)->ptr; str = ((qse_awk_val_str_t*)a0)->val.ptr;
len = ((qse_awk_val_str_t*)a0)->len; len = ((qse_awk_val_str_t*)a0)->val.len;
} }
else else
{ {
@ -658,8 +658,9 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
{ {
qse_size_t nargs; qse_size_t nargs;
qse_awk_val_t* a0, * a1, * a2, * t1, * t2, ** a1_ref; qse_awk_val_t* a0, * a1, * a2, * t1, * t2, ** a1_ref;
qse_char_t* str, * str_free, * p, * tok; qse_char_t* str, * str_free, * p;
qse_size_t str_len, str_left, tok_len, org_len; qse_size_t str_len, str_left, org_len;
qse_cstr_t tok;
qse_long_t nflds; qse_long_t nflds;
qse_char_t key[QSE_SIZEOF(qse_long_t)*8+2]; qse_char_t key[QSE_SIZEOF(qse_long_t)*8+2];
qse_size_t key_len; 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) if (a0->type == QSE_AWK_VAL_STR)
{ {
str = ((qse_awk_val_str_t*)a0)->ptr; str = ((qse_awk_val_str_t*)a0)->val.ptr;
str_len = ((qse_awk_val_str_t*)a0)->len; str_len = ((qse_awk_val_str_t*)a0)->val.len;
str_free = QSE_NULL; str_free = QSE_NULL;
} }
else 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) else if (t1->type == QSE_AWK_VAL_STR)
{ {
fs_ptr = ((qse_awk_val_str_t*)t1)->ptr; fs_ptr = ((qse_awk_val_str_t*)t1)->val.ptr;
fs_len = ((qse_awk_val_str_t*)t1)->len; fs_len = ((qse_awk_val_str_t*)t1)->val.len;
fs_free = QSE_NULL; fs_free = QSE_NULL;
} }
else 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) if (a2->type == QSE_AWK_VAL_STR)
{ {
fs_ptr = ((qse_awk_val_str_t*)a2)->ptr; fs_ptr = ((qse_awk_val_str_t*)a2)->val.ptr;
fs_len = ((qse_awk_val_str_t*)a2)->len; fs_len = ((qse_awk_val_str_t*)a2)->val.len;
fs_free = QSE_NULL; fs_free = QSE_NULL;
} }
else else
@ -823,13 +824,13 @@ static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
if (fs_len <= 1) if (fs_len <= 1)
{ {
p = qse_awk_rtx_strxntok (run, 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 else
{ {
p = qse_awk_rtx_strxntokbyrex ( p = qse_awk_rtx_strxntokbyrex (
run, str, org_len, p, str_len, 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) 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*/ /* no field at all*/
break; 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 */ /* 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 (t2 == QSE_NULL)
{ {
if (str_free != 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) if (a0->type == QSE_AWK_VAL_STR)
{ {
str = ((qse_awk_val_str_t*)a0)->ptr; str = ((qse_awk_val_str_t*)a0)->val.ptr;
len = ((qse_awk_val_str_t*)a0)->len; len = ((qse_awk_val_str_t*)a0)->val.len;
} }
else 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) if (a0->type == QSE_AWK_VAL_STR)
{ {
str = ((qse_awk_val_str_t*)a0)->ptr; str = ((qse_awk_val_str_t*)a0)->val.ptr;
len = ((qse_awk_val_str_t*)a0)->len; len = ((qse_awk_val_str_t*)a0)->val.len;
} }
else else
{ {
@ -991,16 +992,17 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
{ {
qse_size_t nargs; qse_size_t nargs;
qse_awk_val_t* a0, * a1, * a2, ** a2_ref, * v; 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_cstr_t s0, s1, s2;
qse_char_t* a0_ptr_free = QSE_NULL; const qse_char_t* s2_end;
qse_char_t* a1_ptr_free = QSE_NULL; qse_char_t* s0_free = QSE_NULL;
qse_char_t* a2_ptr_free = QSE_NULL; qse_char_t* s1_free = QSE_NULL;
qse_char_t* s2_free = QSE_NULL;
void* rex = QSE_NULL; void* rex = QSE_NULL;
int opt, n; int opt, n;
qse_cstr_t mat, pmat; qse_cstr_t mat, pmat, cur;
const qse_char_t* cur_ptr; qse_size_t i, m;
qse_size_t cur_len, i, m;
qse_str_t new; qse_str_t new;
qse_long_t sub_count; 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) \ #define FREE_A_PTRS(awk) \
do { \ do { \
if (a2_ptr_free != QSE_NULL) QSE_AWK_FREE (awk, a2_ptr_free); \ if (s2_free != QSE_NULL) QSE_AWK_FREE (awk, s2_free); \
if (a1_ptr_free != QSE_NULL) QSE_AWK_FREE (awk, a1_ptr_free); \ if (s1_free != QSE_NULL) QSE_AWK_FREE (awk, s1_free); \
if (a0_ptr_free != QSE_NULL) QSE_AWK_FREE (awk, a0_ptr_free); \ if (s0_free != QSE_NULL) QSE_AWK_FREE (awk, s0_free); \
} while (0) } while (0)
#define FREE_A0_REX(awk,rex) \ #define FREE_A0_REX(awk,rex) \
do { \ 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) else if (a0->type == QSE_AWK_VAL_STR)
{ {
a0_ptr = ((qse_awk_val_str_t*)a0)->ptr; s0.ptr = ((qse_awk_val_str_t*)a0)->val.ptr;
a0_len = ((qse_awk_val_str_t*)a0)->len; s0.len = ((qse_awk_val_str_t*)a0)->val.len;
} }
else else
{ {
a0_ptr = qse_awk_rtx_valtocpldup (run, a0, &a0_len); s0.ptr = qse_awk_rtx_valtocpldup (run, a0, &s0.len);
if (a0_ptr == QSE_NULL) if (s0.ptr == QSE_NULL)
{ {
FREE_A_PTRS (run->awk); FREE_A_PTRS (run->awk);
return -1; return -1;
} }
a0_ptr_free = a0_ptr; s0_free = (qse_char_t*)s0.ptr;
} }
if (a1->type == QSE_AWK_VAL_STR) if (a1->type == QSE_AWK_VAL_STR)
{ {
a1_ptr = ((qse_awk_val_str_t*)a1)->ptr; s1.ptr = ((qse_awk_val_str_t*)a1)->val.ptr;
a1_len = ((qse_awk_val_str_t*)a1)->len; s1.len = ((qse_awk_val_str_t*)a1)->val.len;
} }
else else
{ {
a1_ptr = qse_awk_rtx_valtocpldup (run, a1, &a1_len); s1.ptr = qse_awk_rtx_valtocpldup (run, a1, &s1.len);
if (a1_ptr == QSE_NULL) if (s1.ptr == QSE_NULL)
{ {
FREE_A_PTRS (run->awk); FREE_A_PTRS (run->awk);
return -1; return -1;
} }
a1_ptr_free = a1_ptr; s1_free = (qse_char_t*)s1.ptr;
} }
if (a2 == QSE_NULL) if (a2 == QSE_NULL)
{ {
/* is this correct? any needs to use inrec.d0? */ /* is this correct? any needs to use inrec.d0? */
a2_ptr = QSE_STR_PTR(&run->inrec.line); s2.ptr = QSE_STR_PTR(&run->inrec.line);
a2_len = QSE_STR_LEN(&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) 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; idx = (qse_size_t)((qse_awk_val_ref_t*)a2)->adr;
if (idx == 0) if (idx == 0)
{ {
a2_ptr = QSE_STR_PTR(&run->inrec.line); s2.ptr = QSE_STR_PTR(&run->inrec.line);
a2_len = QSE_STR_LEN(&run->inrec.line); s2.len = QSE_STR_LEN(&run->inrec.line);
} }
else if (idx <= run->inrec.nflds) else if (idx <= run->inrec.nflds)
{ {
a2_ptr = run->inrec.flds[idx-1].ptr; s2.ptr = run->inrec.flds[idx-1].ptr;
a2_len = run->inrec.flds[idx-1].len; s2.len = run->inrec.flds[idx-1].len;
} }
else else
{ {
a2_ptr = QSE_T(""); s2.ptr = QSE_T("");
a2_len = 0; s2.len = 0;
} }
} }
else 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) if ((*a2_ref)->type == QSE_AWK_VAL_STR)
{ {
a2_ptr = ((qse_awk_val_str_t*)(*a2_ref))->ptr; s2.ptr = ((qse_awk_val_str_t*)(*a2_ref))->val.ptr;
a2_len = ((qse_awk_val_str_t*)(*a2_ref))->len; s2.len = ((qse_awk_val_str_t*)(*a2_ref))->val.len;
} }
else else
{ {
a2_ptr = qse_awk_rtx_valtocpldup (run, *a2_ref, &a2_len); s2.ptr = qse_awk_rtx_valtocpldup (run, *a2_ref, &s2.len);
if (a2_ptr == QSE_NULL) if (s2.ptr == QSE_NULL)
{ {
FREE_A_PTRS (run->awk); FREE_A_PTRS (run->awk);
return -1; 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); FREE_A_PTRS (run->awk);
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); 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; qse_awk_errnum_t errnum;
rex = QSE_AWK_BUILDREX ( rex = QSE_AWK_BUILDREX (
run->awk, a0_ptr, a0_len, &errnum); run->awk, s0.ptr, s0.len, &errnum);
if (rex == QSE_NULL) if (rex == QSE_NULL)
{ {
qse_str_fini (&new); 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; opt = (run->gbl.ignorecase)? QSE_REX_IGNORECASE: 0;
a2_end = a2_ptr + a2_len; s2_end = s2.ptr + s2.len;
cur_ptr = a2_ptr; cur.ptr = s2.ptr;
cur_len = a2_len; cur.len = s2.len;
sub_count = 0; sub_count = 0;
pmat.ptr = QSE_NULL; pmat.ptr = QSE_NULL;
pmat.len = 0; 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 */ * end of string($) needs to be tested */
while (cur_ptr <= a2_end) while (cur.ptr <= s2_end)
{ {
qse_awk_errnum_t errnum; qse_awk_errnum_t errnum;
if (max_count == 0 || sub_count < max_count) if (max_count == 0 || sub_count < max_count)
{ {
n = QSE_AWK_MATCHREX ( n = QSE_AWK_MATCHREX (
run->awk, rex, opt, run->awk, rex, opt, &s2, &cur, &mat, &errnum
a2_ptr, a2_len,
cur_ptr, cur_len,
&mat, &errnum
); );
} }
else n = 0; else n = 0;
@ -1178,7 +1177,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
{ {
/* no more match found */ /* no more match found */
if (qse_str_ncat ( 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); FREE_A0_REX (run->awk, rex);
qse_str_fini (&new); 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 ( 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); FREE_A0_REX (run->awk, rex);
qse_str_fini (&new); qse_str_fini (&new);
@ -1208,22 +1207,22 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
return -1; return -1;
} }
for (i = 0; i < a1_len; i++) for (i = 0; i < s1.len; i++)
{ {
if ((i+1) < a1_len && if ((i+1) < s1.len &&
a1_ptr[i] == QSE_T('\\') && s1.ptr[i] == QSE_T('\\') &&
a1_ptr[i+1] == QSE_T('&')) s1.ptr[i+1] == QSE_T('&'))
{ {
m = qse_str_ccat (&new, QSE_T('&')); m = qse_str_ccat (&new, QSE_T('&'));
i++; i++;
} }
else if (a1_ptr[i] == QSE_T('&')) else if (s1.ptr[i] == QSE_T('&'))
{ {
m = qse_str_ncat (&new, mat.ptr, mat.len); m = qse_str_ncat (&new, mat.ptr, mat.len);
} }
else else
{ {
m = qse_str_ccat (&new, a1_ptr[i]); m = qse_str_ccat (&new, s1.ptr[i]);
} }
if (m == (qse_size_t)-1) 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++; sub_count++;
cur_len = cur_len - ((mat.ptr - cur_ptr) + mat.len); cur.len = cur.len - ((mat.ptr - cur.ptr) + mat.len);
cur_ptr = mat.ptr + mat.len; cur.ptr = mat.ptr + mat.len;
pmat = mat; pmat = mat;
@ -1247,7 +1246,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
skip_one_char: skip_one_char:
/* special treatment is needed if match length is 0 */ /* 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) if (m == (qse_size_t)-1)
{ {
FREE_A0_REX (run->awk, rex); 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; 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) if (a0->type == QSE_AWK_VAL_STR)
{ {
str0 = ((qse_awk_val_str_t*)a0)->ptr; str0 = ((qse_awk_val_str_t*)a0)->val.ptr;
len0 = ((qse_awk_val_str_t*)a0)->len; len0 = ((qse_awk_val_str_t*)a0)->val.len;
} }
else 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) if (a1->type == QSE_AWK_VAL_STR)
{ {
str1 = ((qse_awk_val_str_t*)a1)->ptr; str1 = ((qse_awk_val_str_t*)a1)->val.ptr;
len1 = ((qse_awk_val_str_t*)a1)->len; len1 = ((qse_awk_val_str_t*)a1)->val.len;
} }
else 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; if (start > len0 || start <= 0) n = 0;
else else
{ {
qse_cstr_t tmp;
/*TODO: must use str0,len0?*/
tmp.ptr = str0 + start - 1;
tmp.len = len0 - start + 1;
n = QSE_AWK_MATCHREX ( n = QSE_AWK_MATCHREX (
rtx->awk, rex, rtx->awk, rex,
(rtx->gbl.ignorecase? QSE_REX_IGNORECASE: 0), (rtx->gbl.ignorecase? QSE_REX_IGNORECASE: 0),
str0+start-1, len0-start+1,/*TODO: must use str0,len0?*/ &tmp, &tmp, &mat, &errnum
str0+start-1, len0-start+1,
&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); a0 = qse_awk_rtx_getarg (run, 0);
if (a0->type == QSE_AWK_VAL_STR) if (a0->type == QSE_AWK_VAL_STR)
{ {
cs0.ptr = ((qse_awk_val_str_t*)a0)->ptr; cs0 = ((qse_awk_val_str_t*)a0)->val;
cs0.len = ((qse_awk_val_str_t*)a0)->len;
} }
else else
{ {

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -623,36 +623,32 @@ qse_size_t qse_awk_longtostr (
qse_char_t* qse_awk_rtx_strtok ( qse_char_t* qse_awk_rtx_strtok (
qse_awk_rtx_t* rtx, const qse_char_t* s, 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 ( return qse_awk_rtx_strxntok (
rtx, s, qse_strlen(s), rtx, s, qse_strlen(s), delim, qse_strlen(delim), tok);
delim, qse_strlen(delim), tok, tok_len);
} }
qse_char_t* qse_awk_rtx_strxtok ( qse_char_t* qse_awk_rtx_strxtok (
qse_awk_rtx_t* rtx, const qse_char_t* s, qse_size_t len, 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 ( return qse_awk_rtx_strxntok (
rtx, s, len, rtx, s, len, delim, qse_strlen(delim), tok);
delim, qse_strlen(delim), tok, tok_len);
} }
qse_char_t* qse_awk_rtx_strntok ( qse_char_t* qse_awk_rtx_strntok (
qse_awk_rtx_t* rtx, const qse_char_t* s, qse_awk_rtx_t* rtx, const qse_char_t* s,
const qse_char_t* delim, qse_size_t delim_len, 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 ( return qse_awk_rtx_strxntok (
rtx, s, qse_strlen(s), rtx, s, qse_strlen(s), delim, delim_len, tok);
delim, delim_len, tok, tok_len);
} }
qse_char_t* qse_awk_rtx_strxntok ( qse_char_t* qse_awk_rtx_strxntok (
qse_awk_rtx_t* rtx, const qse_char_t* s, qse_size_t len, qse_awk_rtx_t* rtx, const qse_char_t* s, qse_size_t len,
const qse_char_t* delim, qse_size_t delim_len, const qse_char_t* delim, qse_size_t delim_len, qse_cstr_t* tok)
qse_char_t** tok, qse_size_t* tok_len)
{ {
const qse_char_t* p = s, *d; const qse_char_t* p = s, *d;
const qse_char_t* end = s + len; const qse_char_t* end = s + len;
@ -826,13 +822,13 @@ qse_char_t* qse_awk_rtx_strxntok (
exit_loop: exit_loop:
if (sp == QSE_NULL) if (sp == QSE_NULL)
{ {
*tok = QSE_NULL; tok->ptr = QSE_NULL;
*tok_len = (qse_size_t)0; tok->len = (qse_size_t)0;
} }
else else
{ {
*tok = (qse_char_t*)sp; tok->ptr = sp;
*tok_len = ep - sp + 1; tok->len = ep - sp + 1;
} }
/* if QSE_NULL is returned, this function should not be called again */ /* 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, qse_awk_rtx_t* rtx,
const qse_char_t* str, qse_size_t len, const qse_char_t* str, qse_size_t len,
const qse_char_t* substr, qse_size_t sublen, 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) qse_awk_errnum_t* errnum)
{ {
int n; int n;
qse_size_t i, left = sublen; qse_size_t i;
const qse_char_t* ptr = substr; qse_cstr_t match, s, cursub, realsub;
const qse_char_t* str_ptr = substr;
qse_size_t str_len = sublen;
qse_cstr_t match;
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 ( n = QSE_AWK_MATCHREX (
rtx->awk, rex, rtx->awk, rex,
((rtx->gbl.ignorecase)? QSE_REX_IGNORECASE: 0), ((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 == -1) return QSE_NULL;
if (n == 0) if (n == 0)
{ {
/* no match has been found. /* no match has been found.
* return the entire string as a token */ * return the entire string as a token */
*tok = (qse_char_t*)str_ptr; tok->ptr = realsub.ptr;
*tok_len = str_len; tok->len = realsub.len;
*errnum = QSE_AWK_ENOERR; *errnum = QSE_AWK_ENOERR;
return QSE_NULL; return QSE_NULL;
} }
@ -877,8 +879,9 @@ qse_char_t* qse_awk_rtx_strxntokbyrex (
if (match.len == 0) if (match.len == 0)
{ {
ptr++; /* the match length is zero. */
left--; cursub.ptr++;
cursub.len--;
} }
else if (rtx->awk->option & QSE_AWK_STRIPRECSPC) else if (rtx->awk->option & QSE_AWK_STRIPRECSPC)
{ {
@ -891,12 +894,15 @@ qse_char_t* qse_awk_rtx_strxntokbyrex (
goto exit_loop; 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 */ * beginning of the input string is skipped */
ptr += match.len; cursub.ptr += match.len;
left -= match.len; cursub.len -= match.len;
str_ptr = substr + match.len;
str_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; else break;
} }
@ -904,35 +910,40 @@ qse_char_t* qse_awk_rtx_strxntokbyrex (
} }
exit_loop: exit_loop:
if (sublen == 0) if (cursub.len <= 0)
{ {
*tok = (qse_char_t*)str_ptr; tok->ptr = realsub.ptr;
*tok_len = str_len; tok->len = realsub.len;
*errnum = QSE_AWK_ENOERR; *errnum = QSE_AWK_ENOERR;
return QSE_NULL; return QSE_NULL;
} }
*tok = (qse_char_t*)str_ptr; tok->ptr = realsub.ptr;
*tok_len = match.ptr - str_ptr; tok->len = match.ptr - realsub.ptr;
for (i = 0; i < match.len; i++) for (i = 0; i < match.len; i++)
{ {
if (!QSE_AWK_ISSPACE(rtx->awk, match.ptr[i])) if (!QSE_AWK_ISSPACE(rtx->awk, match.ptr[i]))
{ {
/* the match contains a non-space character. */
*errnum = QSE_AWK_ENOERR; *errnum = QSE_AWK_ENOERR;
return (qse_char_t*)match.ptr+match.len; return (qse_char_t*)match.ptr+match.len;
} }
} }
/* the match is all spaces */
*errnum = QSE_AWK_ENOERR; *errnum = QSE_AWK_ENOERR;
if (rtx->awk->option & QSE_AWK_STRIPRECSPC) 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)? return (match.ptr+match.len >= substr+sublen)?
QSE_NULL: ((qse_char_t*)match.ptr+match.len); QSE_NULL: ((qse_char_t*)match.ptr+match.len);
} }
else 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)? return (match.ptr+match.len > substr+sublen)?
QSE_NULL: ((qse_char_t*)match.ptr+match.len); QSE_NULL: ((qse_char_t*)match.ptr+match.len);
} }
@ -941,7 +952,7 @@ exit_loop:
qse_char_t* qse_awk_rtx_strxnfld ( qse_char_t* qse_awk_rtx_strxnfld (
qse_awk_rtx_t* rtx, qse_char_t* str, qse_size_t len, 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 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* p = str;
qse_char_t* end = str + len; qse_char_t* end = str + len;
@ -988,8 +999,8 @@ qse_char_t* qse_awk_rtx_strxnfld (
{ {
if (c == fs) if (c == fs)
{ {
*tok = ts; tok->ptr = ts;
*tok_len = xp - ts; tok->len = xp - ts;
p++; p++;
if (QSE_ISSPACE(fs)) if (QSE_ISSPACE(fs))
@ -1022,8 +1033,8 @@ qse_char_t* qse_awk_rtx_strxnfld (
*xp++ = ec; *xp++ = ec;
} }
*tok = ts; tok->ptr = ts;
*tok_len = xp - ts; tok->len = xp - ts;
return QSE_NULL; return QSE_NULL;
} }
@ -1061,8 +1072,7 @@ void* qse_awk_buildrex (
int qse_awk_matchrex ( int qse_awk_matchrex (
qse_awk_t* awk, void* code, int option, qse_awk_t* awk, void* code, int option,
const qse_char_t* str, qse_size_t len, const qse_cstr_t* str, const qse_cstr_t* substr,
const qse_char_t* substr, qse_size_t sublen,
qse_cstr_t* match, qse_awk_errnum_t* errnum) qse_cstr_t* match, qse_awk_errnum_t* errnum)
{ {
int x; int x;
@ -1070,7 +1080,7 @@ int qse_awk_matchrex (
x = qse_matchrex ( x = qse_matchrex (
awk->mmgr, awk->rex.depth.max.match, 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); if (x < 0) *errnum = QSE_AWK_REXERRTOERR(err);
return x; return x;
} }

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -27,21 +27,19 @@ extern "C" {
qse_char_t* qse_awk_rtx_strtok ( qse_char_t* qse_awk_rtx_strtok (
qse_awk_rtx_t* rtx, const qse_char_t* s, 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_char_t* qse_awk_rtx_strxtok (
qse_awk_rtx_t* rtx, const qse_char_t* s, qse_size_t len, 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_char_t* qse_awk_rtx_strntok (
qse_awk_rtx_t* rtx, const qse_char_t* s, qse_awk_rtx_t* rtx, const qse_char_t* s,
const qse_char_t* delim, qse_size_t delim_len, const qse_char_t* delim, qse_size_t delim_len, qse_cstr_t* tok);
qse_char_t** tok, qse_size_t* tok_len);
qse_char_t* qse_awk_rtx_strxntok ( qse_char_t* qse_awk_rtx_strxntok (
qse_awk_rtx_t* rtx, const qse_char_t* s, qse_size_t len, qse_awk_rtx_t* rtx, const qse_char_t* s, qse_size_t len,
const qse_char_t* delim, qse_size_t delim_len, const qse_char_t* delim, qse_size_t delim_len, qse_cstr_t* tok);
qse_char_t** tok, qse_size_t* tok_len);
qse_char_t* qse_awk_rtx_strxntokbyrex ( qse_char_t* qse_awk_rtx_strxntokbyrex (
qse_awk_rtx_t* rtx, qse_awk_rtx_t* rtx,
@ -50,8 +48,7 @@ qse_char_t* qse_awk_rtx_strxntokbyrex (
const qse_char_t* substr, const qse_char_t* substr,
qse_size_t sublen, qse_size_t sublen,
void* rex, void* rex,
qse_char_t** tok, qse_cstr_t* tok,
qse_size_t* tok_len,
qse_awk_errnum_t* errnum qse_awk_errnum_t* errnum
); );
@ -63,8 +60,7 @@ qse_char_t* qse_awk_rtx_strxnfld (
qse_char_t lq, qse_char_t lq,
qse_char_t rq, qse_char_t rq,
qse_char_t ec, qse_char_t ec,
qse_char_t** tok, qse_cstr_t* tok
qse_size_t* tok_len
); );
void* qse_awk_buildrex ( void* qse_awk_buildrex (
@ -76,8 +72,7 @@ void* qse_awk_buildrex (
int qse_awk_matchrex ( int qse_awk_matchrex (
qse_awk_t* awk, void* code, int option, qse_awk_t* awk, void* code, int option,
const qse_char_t* str, qse_size_t len, const qse_cstr_t* str, const qse_cstr_t* substr,
const qse_char_t* substr, qse_size_t sublen,
qse_cstr_t* match, qse_awk_errnum_t* errnum qse_cstr_t* match, qse_awk_errnum_t* errnum
); );

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. 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_awk_sio_arg_t* arg = QSE_NULL;
qse_htb_pair_t* pair = 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 ( SETERR_ARG_LOC (
awk, awk,
QSE_AWK_EIONMNL, QSE_AWK_EIONMNL,

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -98,8 +98,9 @@ int qse_awk_rtx_setrec (
static int split_record (qse_awk_rtx_t* rtx) static int split_record (qse_awk_rtx_t* rtx)
{ {
qse_char_t* p, * px, * tok; qse_cstr_t tok;
qse_size_t len, tok_len, nflds; qse_char_t* p, * px;
qse_size_t len, nflds;
qse_awk_val_t* v, * fs; qse_awk_val_t* v, * fs;
qse_char_t* fs_ptr, * fs_free; qse_char_t* fs_ptr, * fs_free;
qse_size_t fs_len; 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) else if (fs->type == QSE_AWK_VAL_STR)
{ {
fs_ptr = ((qse_awk_val_str_t*)fs)->ptr; fs_ptr = ((qse_awk_val_str_t*)fs)->val.ptr;
fs_len = ((qse_awk_val_str_t*)fs)->len; fs_len = ((qse_awk_val_str_t*)fs)->val.len;
fs_free = QSE_NULL; fs_free = QSE_NULL;
} }
else else
@ -169,7 +170,7 @@ static int split_record (qse_awk_rtx_t* rtx)
{ {
case 0: case 0:
p = qse_awk_rtx_strxntok (rtx, p = qse_awk_rtx_strxntok (rtx,
p, len, fs_ptr, fs_len, &tok, &tok_len); p, len, fs_ptr, fs_len, &tok);
break; break;
case 1: case 1:
@ -181,7 +182,7 @@ static int split_record (qse_awk_rtx_t* rtx)
QSE_STR_PTR(&rtx->inrec.line), QSE_STR_PTR(&rtx->inrec.line),
QSE_STR_LEN(&rtx->inrec.line), QSE_STR_LEN(&rtx->inrec.line),
p, len, p, len,
rtx->gbl.fs, &tok, &tok_len, &errnum rtx->gbl.fs, &tok, &errnum
); );
if (p == QSE_NULL && errnum != QSE_AWK_ENOERR) 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 /* there are no fields. it can just return here
* as qse_awk_rtx_clrrec has been called before this */ * 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; 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++; nflds++;
len = QSE_STR_LEN(&rtx->inrec.line) - len = QSE_STR_LEN(&rtx->inrec.line) -
@ -255,16 +256,14 @@ static int split_record (qse_awk_rtx_t* rtx)
{ {
case 0: case 0:
p = qse_awk_rtx_strxntok ( p = qse_awk_rtx_strxntok (
rtx, p, len, fs_ptr, fs_len, rtx, p, len, fs_ptr, fs_len, &tok);
&tok, &tok_len);
break; break;
case 1: case 1:
p = qse_awk_rtx_strxnfld ( p = qse_awk_rtx_strxnfld (
rtx, p, len, rtx, p, len,
fs_ptr[1], fs_ptr[2], fs_ptr[1], fs_ptr[2],
fs_ptr[3], fs_ptr[4], fs_ptr[3], fs_ptr[4], &tok);
&tok, &tok_len);
break; break;
default: default:
@ -273,7 +272,7 @@ static int split_record (qse_awk_rtx_t* rtx)
QSE_STR_PTR(&rtx->inrec.line), QSE_STR_PTR(&rtx->inrec.line),
QSE_STR_LEN(&rtx->inrec.line), QSE_STR_LEN(&rtx->inrec.line),
p, len, p, len,
rtx->gbl.fs, &tok, &tok_len, &errnum rtx->gbl.fs, &tok, &errnum
); );
if (p == QSE_NULL && errnum != QSE_AWK_ENOERR) if (p == QSE_NULL && errnum != QSE_AWK_ENOERR)
{ {
@ -285,7 +284,7 @@ static int split_record (qse_awk_rtx_t* rtx)
} }
#if 1 #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 /* there are no fields. it can just return here
* as qse_awk_rtx_clrrec has been called before this */ * as qse_awk_rtx_clrrec has been called before this */
@ -294,7 +293,7 @@ static int split_record (qse_awk_rtx_t* rtx)
} }
#endif #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 1
if (rtx->inrec.nflds >= rtx->inrec.maxflds) if (rtx->inrec.nflds >= rtx->inrec.maxflds)
@ -327,11 +326,11 @@ static int split_record (qse_awk_rtx_t* rtx)
} }
#endif #endif
rtx->inrec.flds[rtx->inrec.nflds].ptr = tok; rtx->inrec.flds[rtx->inrec.nflds].ptr = tok.ptr;
rtx->inrec.flds[rtx->inrec.nflds].len = tok_len; rtx->inrec.flds[rtx->inrec.nflds].len = tok.len;
rtx->inrec.flds[rtx->inrec.nflds].val = 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) if (rtx->inrec.flds[rtx->inrec.nflds].val == QSE_NULL)
{ {
@ -531,10 +530,11 @@ static int recomp_record_fields (
run->inrec.flds[i].ptr = run->inrec.flds[i].ptr =
QSE_STR_PTR(&run->inrec.line) + QSE_STR_PTR(&run->inrec.line) +
QSE_STR_LEN(&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, if (qse_str_ncat (
tmp->ptr, tmp->len) == (qse_size_t)-1) &run->inrec.line,
tmp->val.ptr, tmp->val.len) == (qse_size_t)-1)
{ {
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return -1; return -1;

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -206,8 +206,8 @@ static QSE_INLINE int resolve_rs (
break; break;
case QSE_AWK_VAL_STR: case QSE_AWK_VAL_STR:
rrs->ptr = ((qse_awk_val_str_t*)rs)->ptr; rrs->ptr = ((qse_awk_val_str_t*)rs)->val.ptr;
rrs->len = ((qse_awk_val_str_t*)rs)->len; rrs->len = ((qse_awk_val_str_t*)rs)->val.len;
break; break;
default: default:
@ -231,8 +231,7 @@ static QSE_INLINE int match_long_rs (
ret = QSE_AWK_MATCHREX ( ret = QSE_AWK_MATCHREX (
run->awk, run->gbl.rs, run->awk, run->gbl.rs,
((run->gbl.ignorecase)? QSE_REX_IGNORECASE: 0), ((run->gbl.ignorecase)? QSE_REX_IGNORECASE: 0),
QSE_STR_PTR(buf), QSE_STR_LEN(buf), QSE_STR_XSTR(buf), QSE_STR_XSTR(buf),
QSE_STR_PTR(buf), QSE_STR_LEN(buf),
&match, &errnum); &match, &errnum);
if (ret <= -1) if (ret <= -1)
{ {
@ -644,8 +643,8 @@ int qse_awk_rtx_writeio_val (
if (v->type == QSE_AWK_VAL_STR) if (v->type == QSE_AWK_VAL_STR)
{ {
str = ((qse_awk_val_str_t*)v)->ptr; str = ((qse_awk_val_str_t*)v)->val.ptr;
len = ((qse_awk_val_str_t*)v)->len; len = ((qse_awk_val_str_t*)v)->val.len;
} }
else else
{ {

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -385,8 +385,8 @@ static int set_global (
if (val->type == QSE_AWK_VAL_STR) if (val->type == QSE_AWK_VAL_STR)
{ {
fs_ptr = ((qse_awk_val_str_t*)val)->ptr; fs_ptr = ((qse_awk_val_str_t*)val)->val.ptr;
fs_len = ((qse_awk_val_str_t*)val)->len; fs_len = ((qse_awk_val_str_t*)val)->val.len;
} }
else else
{ {
@ -436,7 +436,7 @@ static int set_global (
(val->type == QSE_AWK_VAL_REAL && (val->type == QSE_AWK_VAL_REAL &&
((qse_awk_val_real_t*)val)->val != 0.0) || ((qse_awk_val_real_t*)val)->val != 0.0) ||
(val->type == QSE_AWK_VAL_STR && (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; break;
} }
@ -540,13 +540,11 @@ static int set_global (
case QSE_AWK_GBL_RS: case QSE_AWK_GBL_RS:
{ {
qse_char_t* rs_ptr; qse_xstr_t rss;
qse_size_t rs_len;
if (val->type == QSE_AWK_VAL_STR) if (val->type == QSE_AWK_VAL_STR)
{ {
rs_ptr = ((qse_awk_val_str_t*)val)->ptr; rss = ((qse_awk_val_str_t*)val)->val;
rs_len = ((qse_awk_val_str_t*)val)->len;
} }
else else
{ {
@ -561,23 +559,22 @@ static int set_global (
if (qse_awk_rtx_valtostr (rtx, val, &out) == QSE_NULL) if (qse_awk_rtx_valtostr (rtx, val, &out) == QSE_NULL)
return -1; return -1;
rs_ptr = out.u.cpldup.ptr; rss = out.u.cpldup;
rs_len = out.u.cpldup.len;
} }
if (rs_len > 1) if (rss.len > 1)
{ {
void* rex; void* rex;
qse_awk_errnum_t errnum; qse_awk_errnum_t errnum;
/* compile the regular expression */ /* compile the regular expression */
rex = QSE_AWK_BUILDREX ( rex = QSE_AWK_BUILDREX (
rtx->awk, rs_ptr, rs_len, &errnum); rtx->awk, rss.ptr, rss.len, &errnum);
if (rex == QSE_NULL) if (rex == QSE_NULL)
{ {
SETERR_COD (rtx, errnum); SETERR_COD (rtx, errnum);
if (val->type != QSE_AWK_VAL_STR) if (val->type != QSE_AWK_VAL_STR)
QSE_AWK_FREE (rtx->awk, rs_ptr); QSE_AWK_FREE (rtx->awk, rss.ptr);
return -1; return -1;
} }
@ -588,7 +585,7 @@ static int set_global (
} }
if (val->type != QSE_AWK_VAL_STR) if (val->type != QSE_AWK_VAL_STR)
QSE_AWK_FREE (rtx->awk, rs_ptr); QSE_AWK_FREE (rtx->awk, rss.ptr);
break; break;
} }
@ -2505,8 +2502,8 @@ static int delete_indexed (
qse_htb_delete ( qse_htb_delete (
map, map,
((qse_awk_val_str_t*)idx)->ptr, ((qse_awk_val_str_t*)idx)->val.ptr,
((qse_awk_val_str_t*)idx)->len ((qse_awk_val_str_t*)idx)->val.len
); );
qse_awk_rtx_refdownval (rtx, idx); 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 */ /* perform the formatted output */
if (output_formatted ( if (output_formatted (
rtx, nde->out_type, dst, rtx, nde->out_type, dst,
((qse_awk_val_str_t*)v)->ptr, ((qse_awk_val_str_t*)v)->val.ptr,
((qse_awk_val_str_t*)v)->len, ((qse_awk_val_str_t*)v)->val.len,
head->next) == -1) head->next) == -1)
{ {
if (out != QSE_NULL) QSE_AWK_FREE (rtx->awk, out); 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) if (v->type == QSE_AWK_VAL_REX)
{ {
const qse_char_t* ptr; qse_cstr_t vs;
qse_size_t len;
int opt = 0; int opt = 0;
if (((qse_awk_rtx_t*)rtx)->gbl.ignorecase) 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. /* the record has never been read.
* probably, this function has been triggered * probably, this function has been triggered
* by the statements in the BEGIN block */ * by the statements in the BEGIN block */
ptr = QSE_T(""); vs.ptr = QSE_T("");
len = 0; vs.len = 0;
} }
else 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 " "always be of the string type once it has "
"been set/updated. it is nil initially."); "been set/updated. it is nil initially.");
ptr = ((qse_awk_val_str_t*)rtx->inrec.d0)->ptr; vs.ptr = ((qse_awk_val_str_t*)rtx->inrec.d0)->val.ptr;
len = ((qse_awk_val_str_t*)rtx->inrec.d0)->len; vs.len = ((qse_awk_val_str_t*)rtx->inrec.d0)->val.len;
} }
n = QSE_AWK_MATCHREX ( n = QSE_AWK_MATCHREX (
((qse_awk_rtx_t*)rtx)->awk, ((qse_awk_rtx_t*)rtx)->awk,
((qse_awk_val_rex_t*)v)->code, ((qse_awk_val_rex_t*)v)->code,
opt, ptr, len, ptr, len, opt, &vs, &vs, QSE_NULL, &errnum
QSE_NULL, &errnum); );
if (n <= -1) if (n <= -1)
{ {
qse_awk_rtx_refdownval (rtx, v); qse_awk_rtx_refdownval (rtx, v);
@ -3575,8 +3570,7 @@ static qse_awk_val_t* do_assignment_pos (
qse_awk_val_t* v; qse_awk_val_t* v;
qse_long_t lv; qse_long_t lv;
qse_real_t rv; qse_real_t rv;
qse_char_t* str; qse_xstr_t str;
qse_size_t len;
int n; int n;
v = eval_expression (run, pos->val); 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) if (val->type == QSE_AWK_VAL_STR)
{ {
str = ((qse_awk_val_str_t*)val)->ptr; str = ((qse_awk_val_str_t*)val)->val;
len = ((qse_awk_val_str_t*)val)->len;
} }
else else
{ {
@ -3614,13 +3607,12 @@ static qse_awk_val_t* do_assignment_pos (
return QSE_NULL; return QSE_NULL;
} }
str = out.u.cpldup.ptr; str = out.u.cpldup;
len = out.u.cpldup.len;
} }
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; if (n == -1) return QSE_NULL;
return (lv == 0)? run->inrec.d0: run->inrec.flds[lv-1].val; 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 ( static int __cmp_nil_str (
qse_awk_rtx_t* run, qse_awk_val_t* left, qse_awk_val_t* right) 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 ( static int __cmp_int_nil (
@ -4041,8 +4033,8 @@ static int __cmp_int_str (
n = qse_awk_rtx_strtonum ( n = qse_awk_rtx_strtonum (
rtx, 1, rtx, 1,
((qse_awk_val_str_t*)right)->ptr, ((qse_awk_val_str_t*)right)->val.ptr,
((qse_awk_val_str_t*)right)->len, ((qse_awk_val_str_t*)right)->val.len,
&ll, &rr &ll, &rr
); );
if (n == 0) if (n == 0)
@ -4068,8 +4060,8 @@ static int __cmp_int_str (
n = qse_strxncasecmp ( n = qse_strxncasecmp (
out.u.cpldup.ptr, out.u.cpldup.ptr,
out.u.cpldup.len, out.u.cpldup.len,
((qse_awk_val_str_t*)right)->ptr, ((qse_awk_val_str_t*)right)->val.ptr,
((qse_awk_val_str_t*)right)->len ((qse_awk_val_str_t*)right)->val.len
); );
} }
else else
@ -4077,8 +4069,8 @@ static int __cmp_int_str (
n = qse_strxncmp ( n = qse_strxncmp (
out.u.cpldup.ptr, out.u.cpldup.ptr,
out.u.cpldup.len, out.u.cpldup.len,
((qse_awk_val_str_t*)right)->ptr, ((qse_awk_val_str_t*)right)->val.ptr,
((qse_awk_val_str_t*)right)->len ((qse_awk_val_str_t*)right)->val.len
); );
} }
@ -4128,12 +4120,12 @@ static int __cmp_real_str (
rr = qse_awk_strxtoreal ( rr = qse_awk_strxtoreal (
rtx->awk, rtx->awk,
((qse_awk_val_str_t*)right)->ptr, ((qse_awk_val_str_t*)right)->val.ptr,
((qse_awk_val_str_t*)right)->len, ((qse_awk_val_str_t*)right)->val.len,
&end &end
); );
if (end == ((qse_awk_val_str_t*)right)->ptr + if (end == ((qse_awk_val_str_t*)right)->val.ptr +
((qse_awk_val_str_t*)right)->len) ((qse_awk_val_str_t*)right)->val.len)
{ {
return (((qse_awk_val_real_t*)left)->val > rr)? 1: return (((qse_awk_val_real_t*)left)->val > rr)? 1:
(((qse_awk_val_real_t*)left)->val < rr)? -1: 0; (((qse_awk_val_real_t*)left)->val < rr)? -1: 0;
@ -4149,8 +4141,8 @@ static int __cmp_real_str (
n = qse_strxncasecmp ( n = qse_strxncasecmp (
out.u.cpldup.ptr, out.u.cpldup.ptr,
out.u.cpldup.len, out.u.cpldup.len,
((qse_awk_val_str_t*)right)->ptr, ((qse_awk_val_str_t*)right)->val.ptr,
((qse_awk_val_str_t*)right)->len ((qse_awk_val_str_t*)right)->val.len
); );
} }
else else
@ -4158,8 +4150,8 @@ static int __cmp_real_str (
n = qse_strxncmp ( n = qse_strxncmp (
out.u.cpldup.ptr, out.u.cpldup.ptr,
out.u.cpldup.len, out.u.cpldup.len,
((qse_awk_val_str_t*)right)->ptr, ((qse_awk_val_str_t*)right)->val.ptr,
((qse_awk_val_str_t*)right)->len ((qse_awk_val_str_t*)right)->val.len
); );
} }
@ -4170,7 +4162,7 @@ static int __cmp_real_str (
static int __cmp_str_nil ( static int __cmp_str_nil (
qse_awk_rtx_t* run, qse_awk_val_t* left, qse_awk_val_t* right) 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 ( static int __cmp_str_int (
@ -4197,8 +4189,8 @@ static int __cmp_str_str (
{ {
/* nother are definitely a string */ /* nother are definitely a string */
return (rtx->gbl.ignorecase)? return (rtx->gbl.ignorecase)?
qse_strxncasecmp (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->ptr, ls->len, rs->ptr, rs->len); qse_strxncmp (ls->val.ptr, ls->val.len, rs->val.ptr, rs->val.len);
} }
if (ls->nstr == 1) if (ls->nstr == 1)
@ -4206,14 +4198,14 @@ static int __cmp_str_str (
qse_long_t ll; qse_long_t ll;
ll = qse_awk_strxtolong ( 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) if (rs->nstr == 1)
{ {
qse_long_t rr; qse_long_t rr;
rr = qse_awk_strxtolong ( 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: return (ll > rr)? 1:
(ll < rr)? -1: 0; (ll < rr)? -1: 0;
@ -4225,7 +4217,7 @@ static int __cmp_str_str (
QSE_ASSERT (rs->nstr == 2); QSE_ASSERT (rs->nstr == 2);
rr = qse_awk_strxtoreal ( 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: return (ll > rr)? 1:
(ll < rr)? -1: 0; (ll < rr)? -1: 0;
@ -4238,14 +4230,14 @@ static int __cmp_str_str (
QSE_ASSERT (ls->nstr == 2); QSE_ASSERT (ls->nstr == 2);
ll = qse_awk_strxtoreal ( 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) if (rs->nstr == 1)
{ {
qse_long_t rr; qse_long_t rr;
rr = qse_awk_strxtolong ( 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: return (ll > rr)? 1:
(ll < rr)? -1: 0; (ll < rr)? -1: 0;
@ -4257,7 +4249,7 @@ static int __cmp_str_str (
QSE_ASSERT (rs->nstr == 2); QSE_ASSERT (rs->nstr == 2);
rr = qse_awk_strxtoreal ( 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: return (ll > rr)? 1:
(ll < rr)? -1: 0; (ll < rr)? -1: 0;
@ -4763,8 +4755,8 @@ static qse_awk_val_t* eval_binop_match0 (
{ {
rex_code = QSE_AWK_BUILDREX ( rex_code = QSE_AWK_BUILDREX (
rtx->awk, rtx->awk,
((qse_awk_val_str_t*)right)->ptr, ((qse_awk_val_str_t*)right)->val.ptr,
((qse_awk_val_str_t*)right)->len, &errnum); ((qse_awk_val_str_t*)right)->val.len, &errnum);
if (rex_code == QSE_NULL) if (rex_code == QSE_NULL)
{ {
SETERR_LOC (rtx, errnum, rloc); SETERR_LOC (rtx, errnum, rloc);
@ -4796,10 +4788,8 @@ static qse_awk_val_t* eval_binop_match0 (
n = QSE_AWK_MATCHREX ( n = QSE_AWK_MATCHREX (
rtx->awk, rex_code, rtx->awk, rex_code,
((rtx->gbl.ignorecase)? QSE_REX_IGNORECASE: 0), ((rtx->gbl.ignorecase)? QSE_REX_IGNORECASE: 0),
((qse_awk_val_str_t*)left)->ptr, &((qse_awk_val_str_t*)left)->val,
((qse_awk_val_str_t*)left)->len, &((qse_awk_val_str_t*)left)->val,
((qse_awk_val_str_t*)left)->ptr,
((qse_awk_val_str_t*)left)->len,
QSE_NULL, &errnum); QSE_NULL, &errnum);
if (n == -1) if (n == -1)
{ {
@ -4835,8 +4825,7 @@ static qse_awk_val_t* eval_binop_match0 (
n = QSE_AWK_MATCHREX ( n = QSE_AWK_MATCHREX (
rtx->awk, rex_code, rtx->awk, rex_code,
((rtx->gbl.ignorecase)? QSE_REX_IGNORECASE: 0), ((rtx->gbl.ignorecase)? QSE_REX_IGNORECASE: 0),
out.u.cpldup.ptr, out.u.cpldup.len, &out.u.cpldup, &out.u.cpldup,
out.u.cpldup.ptr, out.u.cpldup.len,
QSE_NULL, &errnum); QSE_NULL, &errnum);
if (n == -1) 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) if (left->type == QSE_AWK_VAL_STR)
{ {
res = qse_awk_rtx_makeintval ( 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 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) static int shorten_record (qse_awk_rtx_t* run, qse_size_t nflds)
{ {
qse_awk_val_t* v; 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_size_t ofs_len, i;
qse_str_t tmp; 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) if (v->type == QSE_AWK_VAL_NIL)
{ {
/* OFS not set */ /* OFS not set */
ofs = QSE_T(" "); ofs_ptr = QSE_T(" ");
ofs_len = 1; ofs_len = 1;
} }
else if (v->type == QSE_AWK_VAL_STR) else if (v->type == QSE_AWK_VAL_STR)
{ {
ofs = ((qse_awk_val_str_t*)v)->ptr; ofs_ptr = ((qse_awk_val_str_t*)v)->val.ptr;
ofs_len = ((qse_awk_val_str_t*)v)->len; ofs_len = ((qse_awk_val_str_t*)v)->val.len;
} }
else else
{ {
@ -6500,9 +6489,9 @@ static int shorten_record (qse_awk_rtx_t* run, qse_size_t nflds)
return -1; return -1;
} }
ofs = out.u.cpldup.ptr; ofs_ptr = out.u.cpldup.ptr;
ofs_len = out.u.cpldup.len; 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++) 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); qse_str_fini (&tmp);
if (ofs_free != QSE_NULL) if (ofs_free != QSE_NULL)
@ -7204,10 +7193,10 @@ qse_char_t* qse_awk_rtx_format (
} }
else if (v->type == QSE_AWK_VAL_STR) 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) 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; ch_len = 1;
} }
else ch = QSE_T('\0'); else ch = QSE_T('\0');
@ -7268,7 +7257,7 @@ qse_char_t* qse_awk_rtx_format (
} }
else if (fmt[i] == QSE_T('s')) 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_size_t str_len;
qse_long_t k; qse_long_t k;
qse_awk_val_t* v; qse_awk_val_t* v;
@ -7303,13 +7292,13 @@ qse_char_t* qse_awk_rtx_format (
qse_awk_rtx_refupval (rtx, v); qse_awk_rtx_refupval (rtx, v);
if (v->type == QSE_AWK_VAL_NIL) if (v->type == QSE_AWK_VAL_NIL)
{ {
str = QSE_T(""); str_ptr = QSE_T("");
str_len = 0; str_len = 0;
} }
else if (v->type == QSE_AWK_VAL_STR) else if (v->type == QSE_AWK_VAL_STR)
{ {
str = ((qse_awk_val_str_t*)v)->ptr; str_ptr = ((qse_awk_val_str_t*)v)->val.ptr;
str_len = ((qse_awk_val_str_t*)v)->len; str_len = ((qse_awk_val_str_t*)v)->val.len;
} }
else else
{ {
@ -7329,9 +7318,9 @@ qse_char_t* qse_awk_rtx_format (
return QSE_NULL; return QSE_NULL;
} }
str = out.u.cpldup.ptr; str_ptr = out.u.cpldup.ptr;
str_len = out.u.cpldup.len; 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; 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++) 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) if (str_free != QSE_NULL)
QSE_AWK_FREE (rtx->awk, str_free); QSE_AWK_FREE (rtx->awk, str_free);

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. 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); v = qse_awk_rtx_getarg (run, 0);
if (v->type == QSE_AWK_VAL_STR) if (v->type == QSE_AWK_VAL_STR)
{ {
str = ((qse_awk_val_str_t*)v)->ptr; str = ((qse_awk_val_str_t*)v)->val.ptr;
len = ((qse_awk_val_str_t*)v)->len; len = ((qse_awk_val_str_t*)v)->val.len;
} }
else else
{ {

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -27,7 +27,7 @@
#define CHUNKSIZE QSE_AWK_VAL_CHUNK_SIZE #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_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_nil = (qse_awk_val_t*)&awk_nil;
qse_awk_val_t* qse_awk_val_zls = (qse_awk_val_t*)&awk_zls; 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->type = QSE_AWK_VAL_STR;
val->ref = 0; val->ref = 0;
val->nstr = 0; val->nstr = 0;
val->len = len; val->val.len = len;
val->ptr = (qse_char_t*)(val + 1); val->val.ptr = (qse_char_t*)(val + 1);
qse_strncpy (val->ptr, str, len); qse_strncpy (val->val.ptr, str, len);
#ifdef DEBUG_VAL #ifdef DEBUG_VAL
qse_dprintf (QSE_T("makestrval => %p\n"), 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->type = QSE_AWK_VAL_STR;
val->ref = 0; val->ref = 0;
val->nstr = 0; val->nstr = 0;
val->len = len; val->val.len = len;
val->ptr = str; val->val.ptr = str;
return (qse_awk_val_t*)val; return (qse_awk_val_t*)val;
} }
@ -295,10 +295,10 @@ init:
val->type = QSE_AWK_VAL_STR; val->type = QSE_AWK_VAL_STR;
val->ref = 0; val->ref = 0;
val->nstr = 0; val->nstr = 0;
val->len = len1 + len2; val->val.len = len1 + len2;
val->ptr = (qse_char_t*)(val + 1); val->val.ptr = (qse_char_t*)(val + 1);
qse_strncpy (val->ptr, str1, len1); qse_strncpy (val->val.ptr, str1, len1);
qse_strncpy (&val->ptr[len1], str2, len2); qse_strncpy (&val->val.ptr[len1], str2, len2);
#ifdef DEBUG_VAL #ifdef DEBUG_VAL
qse_dprintf (QSE_T("makestrval2 => %p\n"), 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; qse_awk_val_str_t* v = (qse_awk_val_str_t*)val;
int i; int i;
i = v->len / FEATURE_SCACHE_BLOCK_UNIT; i = v->val.len / FEATURE_SCACHE_BLOCK_UNIT;
if (i < QSE_COUNTOF(rtx->scache_count) && if (i < QSE_COUNTOF(rtx->scache_count) &&
rtx->scache_count[i] < QSE_COUNTOF(rtx->scache[i])) 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: case QSE_AWK_VAL_REAL:
return ((qse_awk_val_real_t*)val)->val != 0.0; return ((qse_awk_val_real_t*)val)->val != 0.0;
case QSE_AWK_VAL_STR: 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? */ case QSE_AWK_VAL_REX: /* TODO: is this correct? */
return ((qse_awk_val_rex_t*)val)->len > 0; return ((qse_awk_val_rex_t*)val)->len > 0;
case QSE_AWK_VAL_MAP: case QSE_AWK_VAL_MAP:
@ -1129,7 +1129,7 @@ qse_char_t* qse_awk_rtx_valtostr (
case QSE_AWK_VAL_STR: case QSE_AWK_VAL_STR:
{ {
qse_awk_val_str_t* vs = (qse_awk_val_str_t*)v; 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 ( return qse_awk_rtx_strtonum (
rtx, 0, rtx, 0,
((qse_awk_val_str_t*)v)->ptr, ((qse_awk_val_str_t*)v)->val.ptr,
((qse_awk_val_str_t*)v)->len, ((qse_awk_val_str_t*)v)->val.len,
l, r l, r
); );
} }

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. 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++; 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; const qse_char_t* str = o->str;
if (*str == QSE_T(':')) str++; if (*str == QSE_T(':')) str++;
if (qse_strxcmp (opt->cur, end-opt->cur, str) != 0) continue; if (qse_strxcmp (opt->cur, end-opt->cur, str) != 0) continue;

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -2070,25 +2070,18 @@ void* qse_buildrex (
int qse_matchrex ( int qse_matchrex (
qse_mmgr_t* mmgr, qse_size_t depth, qse_mmgr_t* mmgr, qse_size_t depth,
void* code, int option, void* code, int option,
const qse_char_t* str, qse_size_t len, const qse_cstr_t* str, const qse_cstr_t* substr,
const qse_char_t* substr, qse_size_t sublen, qse_cstr_t* match, qse_rex_errnum_t* errnum)
qse_cstr_t* match, qse_rex_errnum_t* errnum)
{ {
qse_rex_t rex; qse_rex_t rex;
qse_cstr_t s, ss;
int n; int n;
qse_rex_init (&rex, mmgr, code); qse_rex_init (&rex, mmgr, code);
qse_rex_setoption (&rex, option); qse_rex_setoption (&rex, option);
s.ptr = str; if ((n = qse_rex_exec (&rex, str, substr, match)) <= -1)
s.len = len;
ss.ptr = substr;
ss.len = sublen;
if ((n = qse_rex_exec (&rex, &s, &ss, match)) <= -1)
{ {
*errnum = rex.errnum; *errnum = rex.errnum;
qse_rex_yield (&rex); qse_rex_yield (&rex);

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. 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->mmgr = mmgr;
str->sizer = QSE_NULL; str->sizer = QSE_NULL;
if (capa == 0) str->ptr = QSE_NULL; if (capa == 0) str->val.ptr = QSE_NULL;
else 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)); mmgr, QSE_SIZEOF(qse_mchar_t) * (capa + 1));
if (str->ptr == QSE_NULL) return QSE_NULL; if (str->val.ptr == QSE_NULL) return QSE_NULL;
str->ptr[0] = QSE_MT('\0'); str->val.ptr[0] = QSE_MT('\0');
} }
str->len = 0; str->val.len = 0;
str->capa = capa; str->capa = capa;
return str; 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) 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) 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) if (buf != QSE_NULL)
{ {
buf->ptr = str->ptr; buf->ptr = str->val.ptr;
buf->len = str->len; buf->len = str->val.len;
} }
str->ptr = tmp; str->val.ptr = tmp;
str->len = 0; str->val.len = 0;
str->capa = new_capa; str->capa = new_capa;
return 0; 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 (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 ( tmp = (qse_mchar_t*) QSE_MMGR_REALLOC (
str->mmgr, str->ptr, str->mmgr, str->val.ptr,
QSE_SIZEOF(qse_mchar_t)*(capa+1)); QSE_SIZEOF(qse_mchar_t)*(capa+1));
if (tmp == QSE_NULL) return (qse_size_t)-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)); str->mmgr, QSE_SIZEOF(qse_mchar_t)*(capa+1));
if (tmp == QSE_NULL) return (qse_size_t)-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_size_t ncopy = (str->val.len <= capa)? str->val.len: capa;
QSE_MEMCPY (tmp, str->ptr, QSE_MEMCPY (tmp, str->val.ptr,
QSE_SIZEOF(qse_mchar_t)*(ncopy+1)); 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'); tmp[capa] = QSE_MT('\0');
} }
str->capa = capa; str->capa = capa;
str->ptr = tmp; str->val.ptr = tmp;
return str->capa; 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) qse_size_t qse_mbs_setlen (qse_mbs_t* str, qse_size_t len)
{ {
if (len == str->len) return len; if (len == str->val.len) return len;
if (len < str->len) if (len < str->val.len)
{ {
str->len = len; str->val.len = len;
str->ptr[len] = QSE_MT('\0'); str->val.ptr[len] = QSE_MT('\0');
return len; 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; return (qse_size_t)-1;
} }
while (str->len < len) str->ptr[str->len++] = QSE_MT(' '); while (str->val.len < len) str->val.ptr[str->val.len++] = QSE_MT(' ');
return str->len; return str->val.len;
} }
void qse_mbs_clear (qse_mbs_t* str) void qse_mbs_clear (qse_mbs_t* str)
{ {
str->len = 0; str->val.len = 0;
if (str->ptr != QSE_NULL) if (str->val.ptr != QSE_NULL)
{ {
QSE_ASSERT (str->capa >= 1); 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; qse_mbs_t tmp;
tmp.ptr = str->ptr; tmp.val.ptr = str->val.ptr;
tmp.len = str->len; tmp.val.len = str->val.len;
tmp.capa = str->capa; tmp.capa = str->capa;
tmp.mmgr = str->mmgr; tmp.mmgr = str->mmgr;
str->ptr = str1->ptr; str->val.ptr = str1->val.ptr;
str->len = str1->len; str->val.len = str1->val.len;
str->capa = str1->capa; str->capa = str1->capa;
str->mmgr = str1->mmgr; str->mmgr = str1->mmgr;
str1->ptr = tmp.ptr; str1->val.ptr = tmp.val.ptr;
str1->len = tmp.len; str1->val.len = tmp.val.len;
str1->capa = tmp.capa; str1->capa = tmp.capa;
str1->mmgr = tmp.mmgr; 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) 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; 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)); str->mmgr, QSE_SIZEOF(qse_mchar_t) * (len + 1));
if (buf == QSE_NULL) return (qse_size_t)-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->capa = len;
str->ptr = buf; str->val.ptr = buf;
} }
str->len = qse_mbsncpy (str->ptr, s, len); str->val.len = qse_mbsncpy (str->val.ptr, s, len);
str->ptr[str->len] = QSE_MT('\0'); str->val.ptr[str->val.len] = QSE_MT('\0');
return str->len; return str->val.len;
} }
qse_size_t qse_mbs_cat (qse_mbs_t* str, const qse_mchar_t* s) 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) 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; qse_size_t ncapa, mincapa;
/* let the minimum capacity be as large as /* let the minimum capacity be as large as
* to fit in the new substring */ * to fit in the new substring */
mincapa = str->len + len; mincapa = str->val.len + len;
if (str->sizer == QSE_NULL) 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 */ * pass the minimum capacity required as a hint */
ncapa = str->sizer (str, mincapa); ncapa = str->sizer (str, mincapa);
/* if no change in capacity, return current length */ /* 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 */ /* 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); 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. /* copy as many characters as the number of cells available.
* if the capacity has been decreased, len is adjusted here */ * if the capacity has been decreased, len is adjusted here */
len = str->capa - str->len; len = str->capa - str->val.len;
} }
if (len > 0) if (len > 0)
{ {
QSE_MEMCPY (&str->ptr[str->len], s, len*QSE_SIZEOF(*s)); QSE_MEMCPY (&str->val.ptr[str->val.len], s, len*QSE_SIZEOF(*s));
str->len += len; str->val.len += len;
str->ptr[str->len] = QSE_MT('\0'); 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) 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--; 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) 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; qse_size_t nidx = index + size;
if (nidx >= str->len) if (nidx >= str->val.len)
{ {
str->ptr[index] = QSE_MT('\0'); str->val.ptr[index] = QSE_MT('\0');
str->len = index; str->val.len = index;
} }
else else
{ {
qse_mbsncpy ( qse_mbsncpy (
&str->ptr[index], &str->ptr[nidx], &str->val.ptr[index], &str->val.ptr[nidx],
str->len - nidx); str->val.len - nidx);
str->len -= size; str->val.len -= size;
} }
} }
return str->len; return str->val.len;
} }
qse_size_t qse_mbs_trm (qse_mbs_t* str) 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) 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;
} }

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. 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->mmgr = mmgr;
str->sizer = QSE_NULL; str->sizer = QSE_NULL;
if (capa == 0) str->ptr = QSE_NULL; if (capa == 0) str->val.ptr = QSE_NULL;
else 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)); mmgr, QSE_SIZEOF(qse_wchar_t) * (capa + 1));
if (str->ptr == QSE_NULL) return QSE_NULL; if (str->val.ptr == QSE_NULL) return QSE_NULL;
str->ptr[0] = QSE_WT('\0'); str->val.ptr[0] = QSE_WT('\0');
} }
str->len = 0; str->val.len = 0;
str->capa = capa; str->capa = capa;
return str; 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) 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) 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) if (buf != QSE_NULL)
{ {
buf->ptr = str->ptr; buf->ptr = str->val.ptr;
buf->len = str->len; buf->len = str->val.len;
} }
str->ptr = tmp; str->val.ptr = tmp;
str->len = 0; str->val.len = 0;
str->capa = new_capa; str->capa = new_capa;
return 0; 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 (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 ( tmp = (qse_wchar_t*) QSE_MMGR_REALLOC (
str->mmgr, str->ptr, str->mmgr, str->val.ptr,
QSE_SIZEOF(qse_wchar_t)*(capa+1)); QSE_SIZEOF(qse_wchar_t)*(capa+1));
if (tmp == QSE_NULL) return (qse_size_t)-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)); str->mmgr, QSE_SIZEOF(qse_wchar_t)*(capa+1));
if (tmp == QSE_NULL) return (qse_size_t)-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_size_t ncopy = (str->val.len <= capa)? str->val.len: capa;
QSE_MEMCPY (tmp, str->ptr, QSE_MEMCPY (tmp, str->val.ptr,
QSE_SIZEOF(qse_wchar_t)*(ncopy+1)); 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'); tmp[capa] = QSE_WT('\0');
} }
str->capa = capa; str->capa = capa;
str->ptr = tmp; str->val.ptr = tmp;
return str->capa; 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) qse_size_t qse_wcs_setlen (qse_wcs_t* str, qse_size_t len)
{ {
if (len == str->len) return len; if (len == str->val.len) return len;
if (len < str->len) if (len < str->val.len)
{ {
str->len = len; str->val.len = len;
str->ptr[len] = QSE_WT('\0'); str->val.ptr[len] = QSE_WT('\0');
return len; 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; return (qse_size_t)-1;
} }
while (str->len < len) str->ptr[str->len++] = QSE_WT(' '); while (str->val.len < len) str->val.ptr[str->val.len++] = QSE_WT(' ');
return str->len; return str->val.len;
} }
void qse_wcs_clear (qse_wcs_t* str) void qse_wcs_clear (qse_wcs_t* str)
{ {
str->len = 0; str->val.len = 0;
if (str->ptr != QSE_NULL) if (str->val.ptr != QSE_NULL)
{ {
QSE_ASSERT (str->capa >= 1); 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; qse_wcs_t tmp;
tmp.ptr = str->ptr; tmp.val.ptr = str->val.ptr;
tmp.len = str->len; tmp.val.len = str->val.len;
tmp.capa = str->capa; tmp.capa = str->capa;
tmp.mmgr = str->mmgr; tmp.mmgr = str->mmgr;
str->ptr = str1->ptr; str->val.ptr = str1->val.ptr;
str->len = str1->len; str->val.len = str1->val.len;
str->capa = str1->capa; str->capa = str1->capa;
str->mmgr = str1->mmgr; str->mmgr = str1->mmgr;
str1->ptr = tmp.ptr; str1->val.ptr = tmp.val.ptr;
str1->len = tmp.len; str1->val.len = tmp.val.len;
str1->capa = tmp.capa; str1->capa = tmp.capa;
str1->mmgr = tmp.mmgr; 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) 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; 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)); str->mmgr, QSE_SIZEOF(qse_wchar_t) * (len + 1));
if (buf == QSE_NULL) return (qse_size_t)-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->capa = len;
str->ptr = buf; str->val.ptr = buf;
} }
str->len = qse_wcsncpy (str->ptr, s, len); str->val.len = qse_wcsncpy (str->val.ptr, s, len);
str->ptr[str->len] = QSE_WT('\0'); str->val.ptr[str->val.len] = QSE_WT('\0');
return str->len; return str->val.len;
} }
qse_size_t qse_wcs_cat (qse_wcs_t* str, const qse_wchar_t* s) 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) 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; qse_size_t ncapa, mincapa;
/* let the minimum capacity be as large as /* let the minimum capacity be as large as
* to fit in the new substring */ * to fit in the new substring */
mincapa = str->len + len; mincapa = str->val.len + len;
if (str->sizer == QSE_NULL) 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 */ * pass the minimum capacity required as a hint */
ncapa = str->sizer (str, mincapa); ncapa = str->sizer (str, mincapa);
/* if no change in capacity, return current length */ /* 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 */ /* 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); 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. /* copy as many characters as the number of cells available.
* if the capacity has been decreased, len is adjusted here */ * if the capacity has been decreased, len is adjusted here */
len = str->capa - str->len; len = str->capa - str->val.len;
} }
if (len > 0) if (len > 0)
{ {
QSE_MEMCPY (&str->ptr[str->len], s, len*QSE_SIZEOF(*s)); QSE_MEMCPY (&str->val.ptr[str->val.len], s, len*QSE_SIZEOF(*s));
str->len += len; str->val.len += len;
str->ptr[str->len] = QSE_WT('\0'); 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) 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--; 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) 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; qse_size_t nidx = index + size;
if (nidx >= str->len) if (nidx >= str->val.len)
{ {
str->ptr[index] = QSE_WT('\0'); str->val.ptr[index] = QSE_WT('\0');
str->len = index; str->val.len = index;
} }
else else
{ {
qse_wcsncpy ( qse_wcsncpy (
&str->ptr[index], &str->ptr[nidx], &str->val.ptr[index], &str->val.ptr[nidx],
str->len - nidx); str->val.len - nidx);
str->len -= size; str->val.len -= size;
} }
} }
return str->len; return str->val.len;
} }
qse_size_t qse_wcs_trm (qse_wcs_t* str) 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) 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;
} }

View File

@ -53,8 +53,8 @@ enum tok_type_t
#define TOK_IVAL(scm) (scm)->r.t.ival #define TOK_IVAL(scm) (scm)->r.t.ival
#define TOK_RVAL(scm) (scm)->r.t.rval #define TOK_RVAL(scm) (scm)->r.t.rval
#define TOK_NAME(scm) (&(scm)->r.t.name) #define TOK_NAME(scm) (&(scm)->r.t.name)
#define TOK_NAME_PTR(scm) TOK_NAME(scm)->ptr #define TOK_NAME_PTR(scm) QSE_STR_PTR(TOK_NAME(scm))
#define TOK_NAME_LEN(scm) TOK_NAME(scm)->len #define TOK_NAME_LEN(scm) QSE_STR_LEN(TOK_NAME(scm))
#define TOK_LOC(scm) (scm)->r.t.loc #define TOK_LOC(scm) (scm)->r.t.loc
#define TOK_ADD_CHAR(scm,ch) QSE_BLOCK (\ #define TOK_ADD_CHAR(scm,ch) QSE_BLOCK (\

View File

@ -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. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. 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; qse_cstr_t mat, pmat;
int opt = 0, repl = 0, n; int opt = 0, repl = 0, n;
qse_rex_errnum_t errnum; 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_cstr_t str, cur;
qse_size_t max_count, sub_count; const qse_char_t* str_end;
qse_size_t m, i, max_count, sub_count;
QSE_ASSERT (cmd->type == QSE_SED_CMD_SUBSTITUTE); QSE_ASSERT (cmd->type == QSE_SED_CMD_SUBSTITUTE);
qse_str_clear (&sed->e.txt.subst); qse_str_clear (&sed->e.txt.subst);
if (cmd->u.subst.i) opt = QSE_REX_IGNORECASE; if (cmd->u.subst.i) opt = QSE_REX_IGNORECASE;
str_ptr = QSE_STR_PTR(&sed->e.in.line); str.ptr = QSE_STR_PTR(&sed->e.in.line);
str_len = QSE_STR_LEN(&sed->e.in.line); str.len = QSE_STR_LEN(&sed->e.in.line);
/* TODO: support different line end convension */ /* 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; str_end = str.ptr + str.len;
cur_ptr = str_ptr; cur = str;
cur_len = str_len;
sub_count = 0; sub_count = 0;
max_count = (cmd->u.subst.g)? 0: cmd->u.subst.occ; 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 /* perform test when cur_ptr == str_end also because
* end of string($) needs to be tested */ * 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) 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->mmgr,
sed->depth.rex.match, sed->depth.rex.match,
cmd->u.subst.rex, opt, cmd->u.subst.rex, opt,
str_ptr, str_len, &str, &cur, &mat, &errnum
cur_ptr, cur_len,
&mat, &errnum
); );
} }
else n = 0; else n = 0;
@ -1885,7 +1883,7 @@ static int do_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd)
/* no more match found */ /* no more match found */
if (qse_str_ncat ( if (qse_str_ncat (
&sed->e.txt.subst, &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); SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL);
return -1; return -1;
@ -1906,7 +1904,7 @@ static int do_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd)
{ {
m = qse_str_ncat ( m = qse_str_ncat (
&sed->e.txt.subst, &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) 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; repl = 1;
m = qse_str_ncat ( 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) if (m == (qse_size_t)-1)
{ {
SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL); 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++; sub_count++;
cur_len = cur_len - ((mat.ptr - cur_ptr) + mat.len); cur.len = cur.len - ((mat.ptr - cur.ptr) + mat.len);
cur_ptr = mat.ptr + mat.len; cur.ptr = mat.ptr + mat.len;
pmat = mat; pmat = mat;
@ -1969,18 +1967,18 @@ static int do_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd)
skip_one_char: skip_one_char:
/* special treament is need if the match length is 0 */ /* 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) if (m == (qse_size_t)-1)
{ {
SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL); SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL);
return -1; 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 */ /* TODO: support different line ending convension */
m = qse_str_ccat (&sed->e.txt.subst, QSE_T('\n')); 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; int n;
qse_cstr_t match; qse_cstr_t match;
qse_str_t* line; qse_cstr_t line;
qse_size_t llen;
qse_rex_errnum_t errnum; qse_rex_errnum_t errnum;
QSE_ASSERT (a->u.rex != QSE_NULL); QSE_ASSERT (a->u.rex != QSE_NULL);
line = &sed->e.in.line; line.ptr = QSE_STR_PTR(&sed->e.in.line);
llen = QSE_STR_LEN(line); line.len = QSE_STR_LEN(&sed->e.in.line);
/* TODO: support different line end convension */ if (line.len > 0 &&
if (llen > 0 && line.ptr[line.len-1] == QSE_T('\n')) line.len--;
QSE_STR_CHAR(line,llen-1) == QSE_T('\n')) llen--;
n = qse_matchrex ( n = qse_matchrex (
sed->mmgr, sed->mmgr,
sed->depth.rex.match, sed->depth.rex.match,
a->u.rex, 0, a->u.rex, 0,
QSE_STR_PTR(line), llen, &line, &line,
QSE_STR_PTR(line), llen,
&match, &errnum); &match, &errnum);
if (n <= -1) 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: case QSE_SED_CMD_INSERT:
n = write_str (sed, n = write_str (sed,
QSE_STR_PTR(&cmd->u.text), cmd->u.text.ptr,
QSE_STR_LEN(&cmd->u.text)); cmd->u.text.len
);
if (n <= -1) return QSE_NULL; if (n <= -1) return QSE_NULL;
break; 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 */ /* change the pattern space */
n = qse_str_ncpy ( n = qse_str_ncpy (
&sed->e.in.line, &sed->e.in.line,
QSE_STR_PTR(&cmd->u.text), cmd->u.text.ptr,
QSE_STR_LEN(&cmd->u.text)); cmd->u.text.len
);
if (n == (qse_size_t)-1) if (n == (qse_size_t)-1)
{ {
SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL); SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL);

View File

@ -294,6 +294,8 @@ EXTRA_DIST = \
lang-041.awk \ lang-041.awk \
lang-042.awk \ lang-042.awk \
lang-043.awk \ lang-043.awk \
lang-044.awk \
lang-045.awk \
columnate.awk \ columnate.awk \
levenshtein.awk \ levenshtein.awk \
levenshtein-utests.awk \ levenshtein-utests.awk \
@ -308,6 +310,7 @@ EXTRA_DIST = \
lang-036.dat \ lang-036.dat \
lang-037.dat \ lang-037.dat \
lang-043.dat \ lang-043.dat \
lang-044.dat \
adr.dat \ adr.dat \
asm.dat \ asm.dat \
cou.dat \ cou.dat \