*** empty log message ***

This commit is contained in:
hyung-hwan 2006-08-31 16:00:20 +00:00
parent fe1fe9609e
commit c5c6d23a3b
20 changed files with 364 additions and 330 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.c,v 1.73 2006-08-31 15:09:23 bacon Exp $ * $Id: awk.c,v 1.74 2006-08-31 16:00:18 bacon Exp $
*/ */
#include <xp/awk/awk_i.h> #include <xp/awk/awk_i.h>
@ -25,7 +25,7 @@ xp_awk_t* xp_awk_open (xp_awk_syscas_t* syscas)
awk->syscas = syscas; awk->syscas = syscas;
if (xp_str_open (&awk->token.name, 128) == XP_NULL) if (xp_awk_str_open (&awk->token.name, 128, awk) == XP_NULL)
{ {
XP_AWK_FREE (awk, awk); XP_AWK_FREE (awk, awk);
return XP_NULL; return XP_NULL;
@ -35,14 +35,14 @@ xp_awk_t* xp_awk_open (xp_awk_syscas_t* syscas)
if (xp_awk_map_open ( if (xp_awk_map_open (
&awk->tree.afns, awk, 256, __free_afn) == XP_NULL) &awk->tree.afns, awk, 256, __free_afn) == XP_NULL)
{ {
xp_str_close (&awk->token.name); xp_awk_str_close (&awk->token.name);
XP_AWK_FREE (awk, awk); XP_AWK_FREE (awk, awk);
return XP_NULL; return XP_NULL;
} }
if (xp_awk_tab_open (&awk->parse.globals, awk) == XP_NULL) if (xp_awk_tab_open (&awk->parse.globals, awk) == XP_NULL)
{ {
xp_str_close (&awk->token.name); xp_awk_str_close (&awk->token.name);
xp_awk_map_close (&awk->tree.afns); xp_awk_map_close (&awk->tree.afns);
XP_AWK_FREE (awk, awk); XP_AWK_FREE (awk, awk);
return XP_NULL; return XP_NULL;
@ -50,7 +50,7 @@ xp_awk_t* xp_awk_open (xp_awk_syscas_t* syscas)
if (xp_awk_tab_open (&awk->parse.locals, awk) == XP_NULL) if (xp_awk_tab_open (&awk->parse.locals, awk) == XP_NULL)
{ {
xp_str_close (&awk->token.name); xp_awk_str_close (&awk->token.name);
xp_awk_map_close (&awk->tree.afns); xp_awk_map_close (&awk->tree.afns);
xp_awk_tab_close (&awk->parse.globals); xp_awk_tab_close (&awk->parse.globals);
XP_AWK_FREE (awk, awk); XP_AWK_FREE (awk, awk);
@ -59,7 +59,7 @@ xp_awk_t* xp_awk_open (xp_awk_syscas_t* syscas)
if (xp_awk_tab_open (&awk->parse.params, awk) == XP_NULL) if (xp_awk_tab_open (&awk->parse.params, awk) == XP_NULL)
{ {
xp_str_close (&awk->token.name); xp_awk_str_close (&awk->token.name);
xp_awk_map_close (&awk->tree.afns); xp_awk_map_close (&awk->tree.afns);
xp_awk_tab_close (&awk->parse.globals); xp_awk_tab_close (&awk->parse.globals);
xp_awk_tab_close (&awk->parse.locals); xp_awk_tab_close (&awk->parse.locals);
@ -112,7 +112,7 @@ int xp_awk_close (xp_awk_t* awk)
xp_awk_tab_close (&awk->parse.globals); xp_awk_tab_close (&awk->parse.globals);
xp_awk_tab_close (&awk->parse.locals); xp_awk_tab_close (&awk->parse.locals);
xp_awk_tab_close (&awk->parse.params); xp_awk_tab_close (&awk->parse.params);
xp_str_close (&awk->token.name); xp_awk_str_close (&awk->token.name);
/* XP_AWK_MALLOC, XP_AWK_FREE, etc can not be used /* XP_AWK_MALLOC, XP_AWK_FREE, etc can not be used
* from the next line onwards */ * from the next line onwards */

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk_i.h,v 1.50 2006-08-31 15:09:24 bacon Exp $ * $Id: awk_i.h,v 1.51 2006-08-31 16:00:18 bacon Exp $
*/ */
#ifndef _XP_AWK_AWKI_H_ #ifndef _XP_AWK_AWKI_H_
@ -13,10 +13,9 @@ typedef struct xp_awk_tree_t xp_awk_tree_t;
#ifdef XP_AWK_STAND_ALONE #ifdef XP_AWK_STAND_ALONE
#include <xp/awk/sa.h> #include <xp/awk/sa.h>
#else
#include <xp/bas/str.h>
#endif #endif
#include <xp/awk/str.h>
#include <xp/awk/rex.h> #include <xp/awk/rex.h>
#include <xp/awk/map.h> #include <xp/awk/map.h>
#include <xp/awk/val.h> #include <xp/awk/val.h>
@ -116,11 +115,11 @@ struct xp_awk_t
/* token */ /* token */
struct struct
{ {
int prev; int prev;
int type; int type;
xp_str_t name; xp_awk_str_t name;
xp_size_t line; xp_size_t line;
xp_size_t column; xp_size_t column;
} token; } token;
/* builtin functions */ /* builtin functions */
@ -175,7 +174,7 @@ struct xp_awk_run_t
xp_size_t buf_len; xp_size_t buf_len;
xp_bool_t eof; xp_bool_t eof;
xp_str_t line; xp_awk_str_t line;
xp_awk_val_t* d0; /* $0 */ xp_awk_val_t* d0; /* $0 */
xp_size_t maxflds; xp_size_t maxflds;

View File

@ -1,5 +1,5 @@
/* /*
* $Id: extio.c,v 1.41 2006-08-31 15:39:13 bacon Exp $ * $Id: extio.c,v 1.42 2006-08-31 16:00:18 bacon Exp $
*/ */
#include <xp/awk/awk_i.h> #include <xp/awk/awk_i.h>
@ -84,7 +84,7 @@ static int __writeextio (
int xp_awk_readextio ( int xp_awk_readextio (
xp_awk_run_t* run, int in_type, xp_awk_run_t* run, int in_type,
const xp_char_t* name, xp_str_t* buf) const xp_char_t* name, xp_awk_str_t* buf)
{ {
xp_awk_extio_t* p = run->extio.chain; xp_awk_extio_t* p = run->extio.chain;
xp_awk_io_t handler; xp_awk_io_t handler;
@ -175,7 +175,7 @@ int xp_awk_readextio (
} }
/* ready to read a line */ /* ready to read a line */
xp_str_clear (buf); xp_awk_str_clear (buf);
/* get the record separator */ /* get the record separator */
rs = xp_awk_getglobal (run, XP_AWK_GLOBAL_RS); rs = xp_awk_getglobal (run, XP_AWK_GLOBAL_RS);
@ -214,7 +214,7 @@ int xp_awk_readextio (
if (p->in.eof) if (p->in.eof)
{ {
if (XP_STR_LEN(buf) == 0) ret = 0; if (XP_AWK_STR_LEN(buf) == 0) ret = 0;
break; break;
} }
@ -241,7 +241,7 @@ int xp_awk_readextio (
if (n == 0) if (n == 0)
{ {
p->in.eof = xp_true; p->in.eof = xp_true;
if (XP_STR_LEN(buf) == 0) ret = 0; if (XP_AWK_STR_LEN(buf) == 0) ret = 0;
break; break;
} }
@ -263,7 +263,7 @@ int xp_awk_readextio (
/* TODO: handle different line terminator like \r\n */ /* TODO: handle different line terminator like \r\n */
if (line_len == 0 && c == XP_T('\n')) if (line_len == 0 && c == XP_T('\n'))
{ {
if (XP_STR_LEN(buf) <= 0) if (XP_AWK_STR_LEN(buf) <= 0)
{ {
/* if the record is empty when a blank /* if the record is empty when a blank
* line is encountered, the line * line is encountered, the line
@ -276,7 +276,7 @@ int xp_awk_readextio (
* it needs to snip off the line * it needs to snip off the line
* terminator of the previous line */ * terminator of the previous line */
/* TODO: handle different line terminator like \r\n */ /* TODO: handle different line terminator like \r\n */
XP_STR_LEN(buf) -= 1; XP_AWK_STR_LEN(buf) -= 1;
break; break;
} }
} }
@ -293,7 +293,7 @@ int xp_awk_readextio (
/* TODO: safematchrex */ /* TODO: safematchrex */
n = xp_awk_matchrex (run->extio.rs_rex, n = xp_awk_matchrex (run->extio.rs_rex,
XP_STR_BUF(buf), XP_STR_LEN(buf), XP_AWK_STR_BUF(buf), XP_AWK_STR_LEN(buf),
&match_ptr, &match_len, &run->errnum); &match_ptr, &match_len, &run->errnum);
if (n == -1) if (n == -1)
{ {
@ -306,15 +306,15 @@ int xp_awk_readextio (
/* the match should be found at the end of /* the match should be found at the end of
* the current buffer */ * the current buffer */
xp_assert ( xp_assert (
XP_STR_BUF(buf) + XP_STR_LEN(buf) == XP_AWK_STR_BUF(buf) + XP_AWK_STR_LEN(buf) ==
match_ptr + match_len); match_ptr + match_len);
XP_STR_LEN(buf) -= match_len; XP_AWK_STR_LEN(buf) -= match_len;
break; break;
} }
} }
if (xp_str_ccat (buf, c) == (xp_size_t)-1) if (xp_awk_str_ccat (buf, c) == (xp_size_t)-1)
{ {
run->errnum = XP_AWK_ENOMEM; run->errnum = XP_AWK_ENOMEM;
ret = -1; ret = -1;

View File

@ -1,5 +1,5 @@
/* /*
* $Id: extio.h,v 1.11 2006-08-27 15:29:21 bacon Exp $ * $Id: extio.h,v 1.12 2006-08-31 16:00:18 bacon Exp $
*/ */
#ifndef _XP_AWK_EXTIO_H_ #ifndef _XP_AWK_EXTIO_H_
@ -15,7 +15,7 @@ extern "C"
int xp_awk_readextio ( int xp_awk_readextio (
xp_awk_run_t* run, int in_type, xp_awk_run_t* run, int in_type,
const xp_char_t* name, xp_str_t* buf); const xp_char_t* name, xp_awk_str_t* buf);
int xp_awk_writeextio ( int xp_awk_writeextio (
xp_awk_run_t* run, int out_type, xp_awk_run_t* run, int out_type,

View File

@ -1,4 +1,4 @@
SRCS = awk.c err.c tree.c tab.c map.c parse.c \ SRCS = awk.c err.c tree.c str.c tab.c map.c parse.c \
run.c sa.c val.c func.c misc.c extio.c rex.c run.c sa.c val.c func.c misc.c extio.c rex.c
OBJS = $(SRCS:.c=.obj) OBJS = $(SRCS:.c=.obj)
OUT = xpawk.lib OUT = xpawk.lib

View File

@ -1,6 +1,6 @@
SRCS = \ SRCS = \
awk.c err.c tree.c tab.c map.c parse.c \ awk.c err.c tree.c str.c tab.c map.c parse.c \
run.c sa.c val.c func.c misc.c extio.c rex.c run.c sa.c val.c func.c misc.c extio.c rex.c
OBJS = $(SRCS:.c=.obj) OBJS = $(SRCS:.c=.obj)
OUT = xpawk OUT = xpawk

View File

@ -1,4 +1,4 @@
SRCS = awk.c err.c tree.c tab.c map.c parse.c \ SRCS = awk.c err.c tree.c str.c tab.c map.c parse.c \
run.c sa.c val.c func.c misc.c extio.c rex.c run.c sa.c val.c func.c misc.c extio.c rex.c
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)
OUT = libxpawk.a OUT = libxpawk.a

View File

@ -1,4 +1,4 @@
SRCS = awk.c err.c tree.c tab.c map.c parse.c \ SRCS = awk.c err.c tree.c str.c tab.c map.c parse.c \
run.c sa.c val.c func.c misc.c extio.c rex.c run.c sa.c val.c func.c misc.c extio.c rex.c
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)
OUT = libxpawk.a OUT = libxpawk.a

View File

@ -1,5 +1,5 @@
SRCS = awk.c err.c tree.c tab.c map.c parse.c run.c sa.c val.c misc.c SRCS = awk.c err.c tree.c str.c tab.c map.c parse.c run.c sa.c val.c misc.c
OBJS = awk.obj err.obj tree.obj tab.obj map.obj parse.obj run.obj sa.obj val.obj misc.obj OBJS = awk.obj err.obj tree.obj str.obj tab.obj map.obj parse.obj run.obj sa.obj val.obj misc.obj
OUT = xpawk.lib OUT = xpawk.lib
CC = lcc CC = lcc

View File

@ -1,4 +1,4 @@
SRCS = awk.c err.c tree.c tab.c map.c parse.c run.c sa.c val.c func.c misc.c extio.c SRCS = awk.c err.c tree.c str.c tab.c map.c parse.c run.c sa.c val.c func.c misc.c extio.c
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)
OUT = libxpawk.a OUT = libxpawk.a

View File

@ -1,4 +1,4 @@
SRCS = awk.c err.c tree.c tab.c map.c parse.c run.c sa.c val.c misc.c SRCS = awk.c err.c tree.c str.c tab.c map.c parse.c run.c sa.c val.c misc.c
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)
OUT = libxpawk.a OUT = libxpawk.a

View File

@ -1,4 +1,4 @@
SRCS = awk.c err.c tree.c tab.c map.c parse.c run.c sa.c val.c misc.c extio.c rex.c SRCS = awk.c err.c tree.c str.c tab.c map.c parse.c run.c sa.c val.c misc.c extio.c rex.c
OBJS = $(SRCS:.c=.obj) OBJS = $(SRCS:.c=.obj)
OUT = xpawk.lib OUT = xpawk.lib

View File

@ -1,5 +1,5 @@
/* /*
* $Id: parse.c,v 1.173 2006-08-31 14:52:12 bacon Exp $ * $Id: parse.c,v 1.174 2006-08-31 16:00:19 bacon Exp $
*/ */
#include <xp/awk/awk_i.h> #include <xp/awk/awk_i.h>
@ -308,14 +308,14 @@ static struct __bvent __bvtab[] =
#define ADD_TOKEN_CHAR(awk,c) \ #define ADD_TOKEN_CHAR(awk,c) \
do { \ do { \
if (xp_str_ccat(&(awk)->token.name,(c)) == (xp_size_t)-1) { \ if (xp_awk_str_ccat(&(awk)->token.name,(c)) == (xp_size_t)-1) { \
(awk)->errnum = XP_AWK_ENOMEM; return -1; \ (awk)->errnum = XP_AWK_ENOMEM; return -1; \
} \ } \
} while (0) } while (0)
#define ADD_TOKEN_STR(awk,str) \ #define ADD_TOKEN_STR(awk,str) \
do { \ do { \
if (xp_str_cat(&(awk)->token.name,(str)) == (xp_size_t)-1) { \ if (xp_awk_str_cat(&(awk)->token.name,(str)) == (xp_size_t)-1) { \
(awk)->errnum = XP_AWK_ENOMEM; return -1; \ (awk)->errnum = XP_AWK_ENOMEM; return -1; \
} \ } \
} while (0) } while (0)
@ -578,8 +578,8 @@ static xp_awk_nde_t* __parse_function (xp_awk_t* awk)
PANIC (awk, XP_AWK_EIDENT); PANIC (awk, XP_AWK_EIDENT);
} }
name = XP_STR_BUF(&awk->token.name); name = XP_AWK_STR_BUF(&awk->token.name);
name_len = XP_STR_LEN(&awk->token.name); name_len = XP_AWK_STR_LEN(&awk->token.name);
if (xp_awk_map_get(&awk->tree.afns, name, name_len) != XP_NULL) if (xp_awk_map_get(&awk->tree.afns, name, name_len) != XP_NULL)
{ {
/* the function is defined previously */ /* the function is defined previously */
@ -649,8 +649,8 @@ static xp_awk_nde_t* __parse_function (xp_awk_t* awk)
PANIC (awk, XP_AWK_EIDENT); PANIC (awk, XP_AWK_EIDENT);
} }
param = XP_STR_BUF(&awk->token.name); param = XP_AWK_STR_BUF(&awk->token.name);
param_len = XP_STR_LEN(&awk->token.name); param_len = XP_AWK_STR_LEN(&awk->token.name);
if (awk->option & XP_AWK_UNIQUE) if (awk->option & XP_AWK_UNIQUE)
{ {
@ -1041,8 +1041,8 @@ static xp_awk_t* __collect_globals (xp_awk_t* awk)
} }
if (__add_global (awk, if (__add_global (awk,
XP_STR_BUF(&awk->token.name), XP_AWK_STR_BUF(&awk->token.name),
XP_STR_LEN(&awk->token.name)) == XP_NULL) return XP_NULL; XP_AWK_STR_LEN(&awk->token.name)) == XP_NULL) return XP_NULL;
if (__get_token(awk) == -1) return XP_NULL; if (__get_token(awk) == -1) return XP_NULL;
@ -1074,8 +1074,8 @@ static xp_awk_t* __collect_locals (xp_awk_t* awk, xp_size_t nlocals)
PANIC (awk, XP_AWK_EIDENT); PANIC (awk, XP_AWK_EIDENT);
} }
local = XP_STR_BUF(&awk->token.name); local = XP_AWK_STR_BUF(&awk->token.name);
local_len = XP_STR_LEN(&awk->token.name); local_len = XP_AWK_STR_LEN(&awk->token.name);
/* NOTE: it is not checked againt globals names */ /* NOTE: it is not checked againt globals names */
@ -1944,11 +1944,11 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk)
nde->type = XP_AWK_NDE_INT; nde->type = XP_AWK_NDE_INT;
nde->next = XP_NULL; nde->next = XP_NULL;
nde->val = xp_awk_strtolong ( nde->val = xp_awk_strtolong (
XP_STR_BUF(&awk->token.name), 0, XP_NULL); XP_AWK_STR_BUF(&awk->token.name), 0, XP_NULL);
xp_assert ( xp_assert (
XP_STR_LEN(&awk->token.name) == XP_AWK_STR_LEN(&awk->token.name) ==
xp_strlen(XP_STR_BUF(&awk->token.name))); xp_strlen(XP_AWK_STR_BUF(&awk->token.name)));
if (__get_token(awk) == -1) if (__get_token(awk) == -1)
{ {
@ -1968,11 +1968,11 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk)
nde->type = XP_AWK_NDE_REAL; nde->type = XP_AWK_NDE_REAL;
nde->next = XP_NULL; nde->next = XP_NULL;
nde->val = xp_awk_strtoreal (XP_STR_BUF(&awk->token.name)); nde->val = xp_awk_strtoreal (XP_AWK_STR_BUF(&awk->token.name));
xp_assert ( xp_assert (
XP_STR_LEN(&awk->token.name) == XP_AWK_STR_LEN(&awk->token.name) ==
xp_strlen(XP_STR_BUF(&awk->token.name))); xp_strlen(XP_AWK_STR_BUF(&awk->token.name)));
if (__get_token(awk) == -1) if (__get_token(awk) == -1)
{ {
@ -1992,8 +1992,8 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk)
nde->type = XP_AWK_NDE_STR; nde->type = XP_AWK_NDE_STR;
nde->next = XP_NULL; nde->next = XP_NULL;
nde->len = XP_STR_LEN(&awk->token.name); nde->len = XP_AWK_STR_LEN(&awk->token.name);
nde->buf = xp_strxdup(XP_STR_BUF(&awk->token.name), nde->len); nde->buf = xp_strxdup(XP_AWK_STR_BUF(&awk->token.name), nde->len);
if (nde->buf == XP_NULL) if (nde->buf == XP_NULL)
{ {
XP_AWK_FREE (awk, nde); XP_AWK_FREE (awk, nde);
@ -2017,7 +2017,7 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk)
/* the regular expression is tokenized here because /* the regular expression is tokenized here because
* of the context-sensitivity of the slash symbol */ * of the context-sensitivity of the slash symbol */
SET_TOKEN_TYPE (awk, TOKEN_REX); SET_TOKEN_TYPE (awk, TOKEN_REX);
xp_str_clear (&awk->token.name); xp_awk_str_clear (&awk->token.name);
if (__get_rexstr (awk) == -1) return XP_NULL; if (__get_rexstr (awk) == -1) return XP_NULL;
xp_assert (MATCH(awk,TOKEN_REX)); xp_assert (MATCH(awk,TOKEN_REX));
@ -2028,10 +2028,10 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk)
nde->type = XP_AWK_NDE_REX; nde->type = XP_AWK_NDE_REX;
nde->next = XP_NULL; nde->next = XP_NULL;
nde->len = XP_STR_LEN(&awk->token.name); nde->len = XP_AWK_STR_LEN(&awk->token.name);
nde->buf = xp_strxdup ( nde->buf = xp_strxdup (
XP_STR_BUF(&awk->token.name), XP_AWK_STR_BUF(&awk->token.name),
XP_STR_LEN(&awk->token.name)); XP_AWK_STR_LEN(&awk->token.name));
if (nde->buf == XP_NULL) if (nde->buf == XP_NULL)
{ {
XP_AWK_FREE (awk, nde); XP_AWK_FREE (awk, nde);
@ -2039,8 +2039,8 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk)
} }
nde->code = xp_awk_buildrex ( nde->code = xp_awk_buildrex (
XP_STR_BUF(&awk->token.name), XP_AWK_STR_BUF(&awk->token.name),
XP_STR_LEN(&awk->token.name), XP_AWK_STR_LEN(&awk->token.name),
&errnum); &errnum);
if (nde->code == XP_NULL) if (nde->code == XP_NULL)
{ {
@ -2234,9 +2234,9 @@ static xp_awk_nde_t* __parse_primary_ident (xp_awk_t* awk)
xp_assert (MATCH(awk,TOKEN_IDENT)); xp_assert (MATCH(awk,TOKEN_IDENT));
name_dup = xp_strxdup ( name_dup = xp_strxdup (
XP_STR_BUF(&awk->token.name), XP_STR_LEN(&awk->token.name)); XP_AWK_STR_BUF(&awk->token.name), XP_AWK_STR_LEN(&awk->token.name));
if (name_dup == XP_NULL) PANIC (awk, XP_AWK_ENOMEM); if (name_dup == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
name_len = XP_STR_LEN(&awk->token.name); name_len = XP_AWK_STR_LEN(&awk->token.name);
if (__get_token(awk) == -1) if (__get_token(awk) == -1)
{ {
@ -3224,7 +3224,7 @@ static int __get_token (xp_awk_t* awk)
} }
while (n == 1); while (n == 1);
xp_str_clear (&awk->token.name); xp_awk_str_clear (&awk->token.name);
awk->token.line = awk->src.lex.line; awk->token.line = awk->src.lex.line;
awk->token.column = awk->src.lex.column; awk->token.column = awk->src.lex.column;
@ -3263,8 +3263,8 @@ static int __get_token (xp_awk_t* awk)
while (xp_isalpha(c) || c == XP_T('_') || xp_isdigit(c)); while (xp_isalpha(c) || c == XP_T('_') || xp_isdigit(c));
type = __classify_ident (awk, type = __classify_ident (awk,
XP_STR_BUF(&awk->token.name), XP_AWK_STR_BUF(&awk->token.name),
XP_STR_LEN(&awk->token.name)); XP_AWK_STR_LEN(&awk->token.name));
SET_TOKEN_TYPE (awk, type); SET_TOKEN_TYPE (awk, type);
} }
else if (c == XP_T('\"')) else if (c == XP_T('\"'))
@ -3599,7 +3599,7 @@ static int __get_token (xp_awk_t* awk)
return -1; return -1;
} }
/*xp_printf (XP_T("token -> [%s]\n"), XP_STR_BUF(&awk->token.name));*/ /*xp_printf (XP_T("token -> [%s]\n"), XP_AWK_STR_BUF(&awk->token.name));*/
return 0; return 0;
} }
@ -3607,7 +3607,7 @@ static int __get_number (xp_awk_t* awk)
{ {
xp_cint_t c; xp_cint_t c;
xp_assert (XP_STR_LEN(&awk->token.name) == 0); xp_assert (XP_AWK_STR_LEN(&awk->token.name) == 0);
SET_TOKEN_TYPE (awk, TOKEN_INT); SET_TOKEN_TYPE (awk, TOKEN_INT);
c = awk->src.lex.curc; c = awk->src.lex.curc;

View File

@ -1,5 +1,5 @@
/* /*
* $Id: run.c,v 1.189 2006-08-31 15:39:14 bacon Exp $ * $Id: run.c,v 1.190 2006-08-31 16:00:19 bacon Exp $
*/ */
#include <xp/awk/awk_i.h> #include <xp/awk/awk_i.h>
@ -506,7 +506,8 @@ static int __init_run (
run->inrec.nflds = 0; run->inrec.nflds = 0;
run->inrec.maxflds = 0; run->inrec.maxflds = 0;
run->inrec.d0 = xp_awk_val_nil; run->inrec.d0 = xp_awk_val_nil;
if (xp_str_open (&run->inrec.line, DEF_BUF_CAPA) == XP_NULL) if (xp_awk_str_open (
&run->inrec.line, DEF_BUF_CAPA, run->awk) == XP_NULL)
{ {
*errnum = XP_AWK_ENOMEM; *errnum = XP_AWK_ENOMEM;
return -1; return -1;
@ -515,7 +516,7 @@ static int __init_run (
if (xp_awk_map_open (&run->named, if (xp_awk_map_open (&run->named,
run, DEF_BUF_CAPA, __free_namedval) == XP_NULL) run, DEF_BUF_CAPA, __free_namedval) == XP_NULL)
{ {
xp_str_close (&run->inrec.line); xp_awk_str_close (&run->inrec.line);
*errnum = XP_AWK_ENOMEM; *errnum = XP_AWK_ENOMEM;
return -1; return -1;
} }
@ -525,7 +526,7 @@ static int __init_run (
if (run->pattern_range_state == XP_NULL) if (run->pattern_range_state == XP_NULL)
{ {
xp_awk_map_close (&run->named); xp_awk_map_close (&run->named);
xp_str_close (&run->inrec.line); xp_awk_str_close (&run->inrec.line);
*errnum = XP_AWK_ENOMEM; *errnum = XP_AWK_ENOMEM;
return -1; return -1;
} }
@ -564,7 +565,7 @@ static void __deinit_run (xp_awk_run_t* run)
run->inrec.flds = XP_NULL; run->inrec.flds = XP_NULL;
run->inrec.maxflds = 0; run->inrec.maxflds = 0;
} }
xp_str_close (&run->inrec.line); xp_awk_str_close (&run->inrec.line);
/* destroy run stack */ /* destroy run stack */
if (run->stack != XP_NULL) if (run->stack != XP_NULL)
@ -2243,7 +2244,7 @@ static xp_awk_val_t* __do_assignment_pos (
return XP_NULL; return XP_NULL;
} }
if (xp_str_ncpy (&run->inrec.line, str, len) == (xp_size_t)-1) if (xp_awk_str_ncpy (&run->inrec.line, str, len) == (xp_size_t)-1)
{ {
XP_AWK_FREE (run->awk, str); XP_AWK_FREE (run->awk, str);
PANIC (run, XP_AWK_ENOMEM); PANIC (run, XP_AWK_ENOMEM);
@ -2260,8 +2261,8 @@ static xp_awk_val_t* __do_assignment_pos (
{ {
v = xp_awk_makestrval ( v = xp_awk_makestrval (
run, run,
XP_STR_BUF(&run->inrec.line), XP_AWK_STR_BUF(&run->inrec.line),
XP_STR_LEN(&run->inrec.line)); XP_AWK_STR_LEN(&run->inrec.line));
if (v == XP_NULL) if (v == XP_NULL)
{ {
__clear_record (run, xp_false); __clear_record (run, xp_false);
@ -2298,8 +2299,8 @@ static xp_awk_val_t* __do_assignment_pos (
/* recompose $0 */ /* recompose $0 */
v = xp_awk_makestrval ( v = xp_awk_makestrval (
run, run,
XP_STR_BUF(&run->inrec.line), XP_AWK_STR_BUF(&run->inrec.line),
XP_STR_LEN(&run->inrec.line)); XP_AWK_STR_LEN(&run->inrec.line));
if (v == XP_NULL) if (v == XP_NULL)
{ {
__clear_record (run, xp_false); __clear_record (run, xp_false);
@ -4432,7 +4433,7 @@ static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde)
xp_awk_val_t* v, * res; xp_awk_val_t* v, * res;
xp_char_t* in = XP_NULL; xp_char_t* in = XP_NULL;
const xp_char_t* dst; const xp_char_t* dst;
xp_str_t buf; xp_awk_str_t buf;
int n; int n;
p = (xp_awk_nde_getline_t*)nde; p = (xp_awk_nde_getline_t*)nde;
@ -4489,7 +4490,7 @@ static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde)
dst = (in == XP_NULL)? XP_T(""): in; dst = (in == XP_NULL)? XP_T(""): in;
/* TODO: optimize the line buffer management */ /* TODO: optimize the line buffer management */
if (xp_str_open (&buf, DEF_BUF_CAPA) == XP_NULL) if (xp_awk_str_open (&buf, DEF_BUF_CAPA, run->awk) == XP_NULL)
{ {
if (in != XP_NULL) XP_AWK_FREE (run->awk, in); if (in != XP_NULL) XP_AWK_FREE (run->awk, in);
PANIC (run, XP_AWK_ENOMEM); PANIC (run, XP_AWK_ENOMEM);
@ -4502,7 +4503,7 @@ static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde)
{ {
if (run->errnum != XP_AWK_EIOHANDLER) if (run->errnum != XP_AWK_EIOHANDLER)
{ {
xp_str_close (&buf); xp_awk_str_close (&buf);
return XP_NULL; return XP_NULL;
} }
@ -4518,27 +4519,27 @@ static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde)
/* set $0 with the input value */ /* set $0 with the input value */
if (__clear_record (run, xp_false) == -1) if (__clear_record (run, xp_false) == -1)
{ {
xp_str_close (&buf); xp_awk_str_close (&buf);
return XP_NULL; return XP_NULL;
} }
if (__set_record (run, if (__set_record (run,
XP_STR_BUF(&buf), XP_STR_LEN(&buf)) == -1) XP_AWK_STR_BUF(&buf), XP_AWK_STR_LEN(&buf)) == -1)
{ {
xp_str_close (&buf); xp_awk_str_close (&buf);
return XP_NULL; return XP_NULL;
} }
xp_str_close (&buf); xp_awk_str_close (&buf);
} }
else else
{ {
xp_awk_val_t* v; xp_awk_val_t* v;
v = xp_awk_makestrval ( v = xp_awk_makestrval (
run, XP_STR_BUF(&buf), XP_STR_LEN(&buf)); run, XP_AWK_STR_BUF(&buf), XP_AWK_STR_LEN(&buf));
xp_str_close (&buf); xp_awk_str_close (&buf);
if (v == XP_NULL) PANIC (run, XP_AWK_ENOMEM); if (v == XP_NULL) PANIC (run, XP_AWK_ENOMEM);
xp_awk_refupval (v); xp_awk_refupval (v);
@ -4553,7 +4554,7 @@ static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde)
} }
else else
{ {
xp_str_close (&buf); xp_awk_str_close (&buf);
} }
skip_read: skip_read:
@ -4623,13 +4624,13 @@ static int __read_record (xp_awk_run_t* run)
} }
if (n == 0) if (n == 0)
{ {
xp_assert (XP_STR_LEN(&run->inrec.line) == 0); xp_assert (XP_AWK_STR_LEN(&run->inrec.line) == 0);
return 0; return 0;
} }
if (__set_record (run, if (__set_record (run,
XP_STR_BUF(&run->inrec.line), XP_AWK_STR_BUF(&run->inrec.line),
XP_STR_LEN(&run->inrec.line)) == -1) return -1; XP_AWK_STR_LEN(&run->inrec.line)) == -1) return -1;
return 1; return 1;
} }
@ -4696,8 +4697,8 @@ static int __split_record (xp_awk_run_t* run)
#endif #endif
/* scan the input record to count the fields */ /* scan the input record to count the fields */
p = XP_STR_BUF(&run->inrec.line); p = XP_AWK_STR_BUF(&run->inrec.line);
len = XP_STR_LEN(&run->inrec.line); len = XP_AWK_STR_LEN(&run->inrec.line);
nflds = 0; nflds = 0;
while (p != XP_NULL) while (p != XP_NULL)
@ -4740,8 +4741,8 @@ static int __split_record (xp_awk_run_t* run)
xp_assert ((tok != XP_NULL && tok_len > 0) || tok_len == 0); xp_assert ((tok != XP_NULL && tok_len > 0) || tok_len == 0);
nflds++; nflds++;
len = XP_STR_LEN(&run->inrec.line) - len = XP_AWK_STR_LEN(&run->inrec.line) -
(p - XP_STR_BUF(&run->inrec.line)); (p - XP_AWK_STR_BUF(&run->inrec.line));
} }
/* THIS PART IS WRONG. XP_AWK_FREE IT AFTER THE NEXT SPLIT LOOP */ /* THIS PART IS WRONG. XP_AWK_FREE IT AFTER THE NEXT SPLIT LOOP */
@ -4766,8 +4767,8 @@ static int __split_record (xp_awk_run_t* run)
} }
/* scan again and split it */ /* scan again and split it */
p = XP_STR_BUF(&run->inrec.line); p = XP_AWK_STR_BUF(&run->inrec.line);
len = XP_STR_LEN(&run->inrec.line); len = XP_AWK_STR_LEN(&run->inrec.line);
while (p != XP_NULL) while (p != XP_NULL)
{ {
@ -4786,8 +4787,8 @@ static int __split_record (xp_awk_run_t* run)
xp_awk_refupval (run->inrec.flds[run->inrec.nflds].val); xp_awk_refupval (run->inrec.flds[run->inrec.nflds].val);
run->inrec.nflds++; run->inrec.nflds++;
len = XP_STR_LEN(&run->inrec.line) - len = XP_AWK_STR_LEN(&run->inrec.line) -
(p - XP_STR_BUF(&run->inrec.line)); (p - XP_AWK_STR_BUF(&run->inrec.line));
} }
/* set the number of fields */ /* set the number of fields */
@ -4829,7 +4830,7 @@ static int __clear_record (xp_awk_run_t* run, xp_bool_t noline)
} }
xp_assert (run->inrec.nflds == 0); xp_assert (run->inrec.nflds == 0);
if (!noline) xp_str_clear (&run->inrec.line); if (!noline) xp_awk_str_clear (&run->inrec.line);
return n; return n;
} }
@ -4886,7 +4887,7 @@ static int __recomp_record_fields (
lv = lv - 1; /* adjust the value to 0-based index */ lv = lv - 1; /* adjust the value to 0-based index */
xp_str_clear (&run->inrec.line); xp_awk_str_clear (&run->inrec.line);
if (max > 1) if (max > 1)
{ {
@ -4912,7 +4913,7 @@ static int __recomp_record_fields (
{ {
if (i > 0) if (i > 0)
{ {
if (xp_str_ncat ( if (xp_awk_str_ncat (
&run->inrec.line, &run->inrec.line,
ofs, ofs_len) == (xp_size_t)-1) ofs, ofs_len) == (xp_size_t)-1)
{ {
@ -4927,11 +4928,11 @@ static int __recomp_record_fields (
xp_awk_val_t* tmp; xp_awk_val_t* tmp;
run->inrec.flds[i].ptr = run->inrec.flds[i].ptr =
XP_STR_BUF(&run->inrec.line) + XP_AWK_STR_BUF(&run->inrec.line) +
XP_STR_LEN(&run->inrec.line); XP_AWK_STR_LEN(&run->inrec.line);
run->inrec.flds[i].len = len; run->inrec.flds[i].len = len;
if (xp_str_ncat ( if (xp_awk_str_ncat (
&run->inrec.line, str, len) == (xp_size_t)-1) &run->inrec.line, str, len) == (xp_size_t)-1)
{ {
if (ofsp != XP_NULL) XP_AWK_FREE (run->awk, ofsp); if (ofsp != XP_NULL) XP_AWK_FREE (run->awk, ofsp);
@ -4956,11 +4957,11 @@ static int __recomp_record_fields (
else if (i >= nflds) else if (i >= nflds)
{ {
run->inrec.flds[i].ptr = run->inrec.flds[i].ptr =
XP_STR_BUF(&run->inrec.line) + XP_AWK_STR_BUF(&run->inrec.line) +
XP_STR_LEN(&run->inrec.line); XP_AWK_STR_LEN(&run->inrec.line);
run->inrec.flds[i].len = 0; run->inrec.flds[i].len = 0;
if (xp_str_cat ( if (xp_awk_str_cat (
&run->inrec.line, XP_T("")) == (xp_size_t)-1) &run->inrec.line, XP_T("")) == (xp_size_t)-1)
{ {
if (ofsp != XP_NULL) XP_AWK_FREE (run->awk, ofsp); if (ofsp != XP_NULL) XP_AWK_FREE (run->awk, ofsp);
@ -4983,11 +4984,11 @@ static int __recomp_record_fields (
tmp = (xp_awk_val_str_t*)run->inrec.flds[i].val; tmp = (xp_awk_val_str_t*)run->inrec.flds[i].val;
run->inrec.flds[i].ptr = run->inrec.flds[i].ptr =
XP_STR_BUF(&run->inrec.line) + XP_AWK_STR_BUF(&run->inrec.line) +
XP_STR_LEN(&run->inrec.line); XP_AWK_STR_LEN(&run->inrec.line);
run->inrec.flds[i].len = tmp->len; run->inrec.flds[i].len = tmp->len;
if (xp_str_ncat (&run->inrec.line, if (xp_awk_str_ncat (&run->inrec.line,
tmp->buf, tmp->len) == (xp_size_t)-1) tmp->buf, tmp->len) == (xp_size_t)-1)
{ {
if (ofsp != XP_NULL) XP_AWK_FREE (run->awk, ofsp); if (ofsp != XP_NULL) XP_AWK_FREE (run->awk, ofsp);
@ -5045,9 +5046,10 @@ static xp_char_t* __idxnde_to_str (
else else
{ {
/* multidimensional index */ /* multidimensional index */
xp_str_t idxstr; xp_awk_str_t idxstr;
if (xp_str_open (&idxstr, DEF_BUF_CAPA) == XP_NULL) if (xp_awk_str_open (
&idxstr, DEF_BUF_CAPA, run->awk) == XP_NULL)
{ {
PANIC (run, XP_AWK_ENOMEM); PANIC (run, XP_AWK_ENOMEM);
} }
@ -5057,18 +5059,18 @@ static xp_char_t* __idxnde_to_str (
idx = __eval_expression (run, nde); idx = __eval_expression (run, nde);
if (idx == XP_NULL) if (idx == XP_NULL)
{ {
xp_str_close (&idxstr); xp_awk_str_close (&idxstr);
return XP_NULL; return XP_NULL;
} }
xp_awk_refupval (idx); xp_awk_refupval (idx);
/* TODO: configurable index seperator, not just a comma */ /* TODO: configurable index seperator, not just a comma */
if (XP_STR_LEN(&idxstr) > 0 && if (XP_AWK_STR_LEN(&idxstr) > 0 &&
xp_str_cat (&idxstr, XP_T(",")) == (xp_size_t)-1) xp_awk_str_cat (&idxstr, XP_T(",")) == (xp_size_t)-1)
{ {
xp_awk_refdownval (run, idx); xp_awk_refdownval (run, idx);
xp_str_close (&idxstr); xp_awk_str_close (&idxstr);
PANIC (run, XP_AWK_ENOMEM); PANIC (run, XP_AWK_ENOMEM);
} }
@ -5076,7 +5078,7 @@ static xp_char_t* __idxnde_to_str (
idx, xp_false, &idxstr, XP_NULL) == XP_NULL) idx, xp_false, &idxstr, XP_NULL) == XP_NULL)
{ {
xp_awk_refdownval (run, idx); xp_awk_refdownval (run, idx);
xp_str_close (&idxstr); xp_awk_str_close (&idxstr);
return XP_NULL; return XP_NULL;
} }
@ -5084,9 +5086,9 @@ static xp_char_t* __idxnde_to_str (
nde = nde->next; nde = nde->next;
} }
str = XP_STR_BUF(&idxstr); str = XP_AWK_STR_BUF(&idxstr);
*len = XP_STR_LEN(&idxstr); *len = XP_AWK_STR_LEN(&idxstr);
xp_str_forfeit (&idxstr); xp_awk_str_forfeit (&idxstr);
} }
return str; return str;

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sa.c,v 1.31 2006-08-30 14:23:19 bacon Exp $ * $Id: sa.c,v 1.32 2006-08-31 16:00:19 bacon Exp $
*/ */
#include <xp/awk/awk_i.h> #include <xp/awk/awk_i.h>
@ -273,143 +273,14 @@ int xp_vsprintf (xp_char_t* buf, xp_size_t size, const xp_char_t* fmt, xp_va_lis
return n; return n;
} }
xp_str_t* xp_str_open (xp_str_t* str, xp_size_t capa)
{
if (str == XP_NULL)
{
str = (xp_str_t*) xp_malloc (sizeof(xp_str_t));
if (str == XP_NULL) return XP_NULL;
str->__dynamic = xp_true;
}
else str->__dynamic = xp_false;
str->buf = (xp_char_t*) xp_malloc (xp_sizeof(xp_char_t) * (capa + 1));
if (str->buf == XP_NULL)
{
if (str->__dynamic) xp_free (str);
return XP_NULL;
}
str->size = 0;
str->capa = capa;
str->buf[0] = XP_T('\0');
return str;
}
void xp_str_close (xp_str_t* str)
{
xp_free (str->buf);
if (str->__dynamic) xp_free (str);
}
void xp_str_forfeit (xp_str_t* str)
{
if (str->__dynamic) xp_free (str);
}
xp_size_t xp_str_cpy (xp_str_t* str, const xp_char_t* s)
{
/* TODO: improve it */
return xp_str_ncpy (str, s, xp_strlen(s));
}
xp_size_t xp_str_ncpy (xp_str_t* str, const xp_char_t* s, xp_size_t len)
{
xp_char_t* buf;
if (len > str->capa)
{
buf = (xp_char_t*) xp_malloc (xp_sizeof(xp_char_t) * (len + 1));
if (buf == XP_NULL) return (xp_size_t)-1;
xp_free (str->buf);
str->capa = len;
str->buf = buf;
}
str->size = xp_strncpy (str->buf, s, len);
str->buf[str->size] = XP_T('\0');
return str->size;
}
xp_size_t xp_str_cat (xp_str_t* str, const xp_char_t* s)
{
/* TODO: improve it */
return xp_str_ncat (str, s, xp_strlen(s));
}
xp_size_t xp_str_ncat (xp_str_t* str, const xp_char_t* s, xp_size_t len)
{
if (len > str->capa - str->size)
{
xp_char_t* tmp;
xp_size_t capa;
capa = str->size + len;
/* double the capa if necessary for concatenation */
if (capa < str->capa * 2) capa = str->capa * 2;
#ifndef XP_AWK_NTDDK
tmp = (xp_char_t*) xp_realloc (
str->buf, xp_sizeof(xp_char_t) * (capa + 1));
if (tmp == XP_NULL) return (xp_size_t)-1;
#else
tmp = (xp_char_t*) xp_malloc (
xp_sizeof(xp_char_t) * (capa + 1));
if (tmp == XP_NULL) return (xp_size_t)-1;
if (str->buf != XP_NULL)
{
xp_memcpy (tmp, str->buf,
xp_sizeof(xp_char_t) * (str->capa + 1));
xp_free (str->buf);
}
#endif
str->capa = capa;
str->buf = tmp;
}
str->size += xp_strncpy (&str->buf[str->size], s, len);
str->buf[str->size] = XP_T('\0');
return str->size;
}
xp_size_t xp_str_ccat (xp_str_t* str, xp_char_t c)
{
return xp_str_ncat (str, &c, 1);
}
xp_size_t xp_str_nccat (xp_str_t* str, xp_char_t c, xp_size_t len)
{
while (len > 0)
{
if (xp_str_ncat (str, &c, 1) == (xp_size_t)-1)
{
return (xp_size_t)-1;
}
len--;
}
return str->size;
}
void xp_str_clear (xp_str_t* str)
{
str->size = 0;
str->buf[0] = XP_T('\0');
}
#define MOD_SHORT 1 #define MOD_SHORT 1
#define MOD_LONG 2 #define MOD_LONG 2
#define MOD_LONGLONG 3 #define MOD_LONGLONG 3
#define ADDC(str,c) \ #define ADDC(str,c) \
do { \ do { \
if (xp_str_ccat(&str, c) == (xp_size_t)-1) { \ if (xp_awk_str_ccat(&str, c) == (xp_size_t)-1) { \
xp_str_close (&str); \ xp_awk_str_close (&str); \
return XP_NULL; \ return XP_NULL; \
} \ } \
} while (0) } while (0)
@ -418,11 +289,11 @@ static xp_char_t* __adjust_format (const xp_char_t* format)
{ {
const xp_char_t* fp = format; const xp_char_t* fp = format;
xp_char_t* tmp; xp_char_t* tmp;
xp_str_t str; xp_awk_str_t str;
xp_char_t ch; xp_char_t ch;
int modifier; int modifier;
if (xp_str_open (&str, 256) == XP_NULL) return XP_NULL; if (xp_awk_str_open (&str, 256) == XP_NULL) return XP_NULL;
while (*fp != XP_T('\0')) while (*fp != XP_T('\0'))
{ {
@ -548,8 +419,8 @@ static xp_char_t* __adjust_format (const xp_char_t* format)
else ADDC (str, ch); else ADDC (str, ch);
} }
tmp = XP_STR_BUF(&str); tmp = XP_AWK_STR_BUF(&str);
xp_str_forfeit (&str); xp_awk_str_forfeit (&str);
return tmp; return tmp;
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sa.h,v 1.34 2006-08-31 04:21:04 bacon Exp $ * $Id: sa.h,v 1.35 2006-08-31 16:00:19 bacon Exp $
*/ */
#ifndef _XP_AWK_SA_H_ #ifndef _XP_AWK_SA_H_
@ -96,22 +96,6 @@
#define xp_va_end(pvar) va_end(pvar) #define xp_va_end(pvar) va_end(pvar)
#define xp_va_arg(pvar,type) va_arg(pvar,type) #define xp_va_arg(pvar,type) va_arg(pvar,type)
#define XP_STR_LEN(x) ((x)->size)
#define XP_STR_SIZE(x) ((x)->size + 1)
#define XP_STR_CAPA(x) ((x)->capa)
#define XP_STR_BUF(x) ((x)->buf)
#define XP_STR_CHAR(x,idx) ((x)->buf[idx])
typedef struct xp_str_t xp_str_t;
struct xp_str_t
{
xp_char_t* buf;
xp_size_t size;
xp_size_t capa;
xp_bool_t __dynamic;
};
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -177,36 +161,6 @@ int xp_sprintf (
int xp_vsprintf ( int xp_vsprintf (
xp_char_t* buf, xp_size_t size, const xp_char_t* fmt, xp_va_list ap); xp_char_t* buf, xp_size_t size, const xp_char_t* fmt, xp_va_list ap);
#define xp_str_open xp_awk_str_open
xp_str_t* xp_str_open (xp_str_t* str, xp_size_t capa);
#define xp_str_close xp_awk_str_close
void xp_str_close (xp_str_t* str);
#define xp_str_forfeit xp_awk_str_forfeit
void xp_str_forfeit (xp_str_t* str);
#define xp_str_cpy xp_awk_str_cpy
xp_size_t xp_str_cpy (xp_str_t* str, const xp_char_t* s);
#define xp_str_ncpy xp_awk_str_ncpy
xp_size_t xp_str_ncpy (xp_str_t* str, const xp_char_t* s, xp_size_t len);
#define xp_str_cat xp_awk_str_cat
xp_size_t xp_str_cat (xp_str_t* str, const xp_char_t* s);
#define xp_str_ncat xp_awk_str_ncat
xp_size_t xp_str_ncat (xp_str_t* str, const xp_char_t* s, xp_size_t len);
#define xp_str_ccat xp_awk_str_ccat
xp_size_t xp_str_ccat (xp_str_t* str, xp_char_t c);
#define xp_str_nccat xp_awk_str_nccat
xp_size_t xp_str_nccat (xp_str_t* str, xp_char_t c, xp_size_t len);
#define xp_str_clear xp_awk_str_clear
void xp_str_clear (xp_str_t* str);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

148
ase/awk/str.c Normal file
View File

@ -0,0 +1,148 @@
/*
* $Id: str.c,v 1.1 2006-08-31 16:00:19 bacon Exp $
*/
#include <xp/awk/awk_i.h>
#ifndef XP_AWK_STAND_ALONE
#include <xp/bas/memory.h>
#include <xp/bas/string.h>
#include <xp/bas/assert.h>
#endif
xp_awk_str_t* xp_awk_str_open (
xp_awk_str_t* str, xp_size_t capa, xp_awk_t* awk)
{
if (str == XP_NULL)
{
str = (xp_awk_str_t*) XP_AWK_MALLOC (awk, sizeof(xp_awk_str_t));
if (str == XP_NULL) return XP_NULL;
str->__dynamic = xp_true;
}
else str->__dynamic = xp_false;
str->awk = awk;
str->buf = (xp_char_t*) XP_AWK_MALLOC (
awk, xp_sizeof(xp_char_t) * (capa + 1));
if (str->buf == XP_NULL)
{
if (str->__dynamic) XP_AWK_FREE (awk, str);
return XP_NULL;
}
str->size = 0;
str->capa = capa;
str->buf[0] = XP_T('\0');
return str;
}
void xp_awk_str_close (xp_awk_str_t* str)
{
XP_AWK_FREE (str->awk, str->buf);
if (str->__dynamic) XP_AWK_FREE (str->awk, str);
}
void xp_awk_str_forfeit (xp_awk_str_t* str)
{
if (str->__dynamic) XP_AWK_FREE (str->awk, str);
}
xp_size_t xp_awk_str_cpy (xp_awk_str_t* str, const xp_char_t* s)
{
/* TODO: improve it */
return xp_awk_str_ncpy (str, s, xp_strlen(s));
}
xp_size_t xp_awk_str_ncpy (xp_awk_str_t* str, const xp_char_t* s, xp_size_t len)
{
xp_char_t* buf;
if (len > str->capa)
{
buf = (xp_char_t*) XP_AWK_MALLOC (
str->awk, xp_sizeof(xp_char_t) * (len + 1));
if (buf == XP_NULL) return (xp_size_t)-1;
XP_AWK_FREE (str->awk, str->buf);
str->capa = len;
str->buf = buf;
}
str->size = xp_strncpy (str->buf, s, len);
str->buf[str->size] = XP_T('\0');
return str->size;
}
xp_size_t xp_awk_str_cat (xp_awk_str_t* str, const xp_char_t* s)
{
/* TODO: improve it */
return xp_awk_str_ncat (str, s, xp_strlen(s));
}
xp_size_t xp_awk_str_ncat (xp_awk_str_t* str, const xp_char_t* s, xp_size_t len)
{
if (len > str->capa - str->size)
{
xp_char_t* tmp;
xp_size_t capa;
capa = str->size + len;
/* double the capa if necessary for concatenation */
if (capa < str->capa * 2) capa = str->capa * 2;
if (str->awk->syscas->realloc != XP_NULL)
{
tmp = (xp_char_t*) XP_AWK_REALLOC (
str->awk, str->buf,
xp_sizeof(xp_char_t) * (capa + 1));
if (tmp == XP_NULL) return (xp_size_t)-1;
}
else
{
tmp = (xp_char_t*) XP_AWK_MALLOC (
str->awk, xp_sizeof(xp_char_t) * (capa + 1));
if (tmp == XP_NULL) return (xp_size_t)-1;
if (str->buf != XP_NULL)
{
xp_memcpy (tmp, str->buf,
xp_sizeof(xp_char_t) * (str->capa + 1));
XP_AWK_FREE (str->awk, str->buf);
}
}
str->capa = capa;
str->buf = tmp;
}
str->size += xp_strncpy (&str->buf[str->size], s, len);
str->buf[str->size] = XP_T('\0');
return str->size;
}
xp_size_t xp_awk_str_ccat (xp_awk_str_t* str, xp_char_t c)
{
return xp_awk_str_ncat (str, &c, 1);
}
xp_size_t xp_awk_str_nccat (xp_awk_str_t* str, xp_char_t c, xp_size_t len)
{
while (len > 0)
{
if (xp_awk_str_ncat (str, &c, 1) == (xp_size_t)-1)
{
return (xp_size_t)-1;
}
len--;
}
return str->size;
}
void xp_awk_str_clear (xp_awk_str_t* str)
{
str->size = 0;
str->buf[0] = XP_T('\0');
}

60
ase/awk/str.h Normal file
View File

@ -0,0 +1,60 @@
/*
* $Id: str.h,v 1.1 2006-08-31 16:00:20 bacon Exp $
*/
#ifndef _XP_AWK_STR_H_
#define _XP_AWK_STR_H_
#ifndef _XP_AWK_AWK_H_
#error Never include this file directly. Include <xp/awk/awk.h> instead
#endif
#define XP_AWK_STR_LEN(x) ((x)->size)
#define XP_AWK_STR_SIZE(x) ((x)->size + 1)
#define XP_AWK_STR_CAPA(x) ((x)->capa)
#define XP_AWK_STR_BUF(x) ((x)->buf)
#define XP_AWK_STR_CHAR(x,idx) ((x)->buf[idx])
typedef struct xp_awk_str_t xp_awk_str_t;
struct xp_awk_str_t
{
xp_char_t* buf;
xp_size_t size;
xp_size_t capa;
xp_awk_t* awk;
xp_bool_t __dynamic;
};
#ifdef __cplusplus
extern "C" {
#endif
xp_awk_str_t* xp_awk_str_open (
xp_awk_str_t* str, xp_size_t capa, xp_awk_t* awk);
void xp_awk_str_close (xp_awk_str_t* str);
void xp_awk_str_forfeit (xp_awk_str_t* str);
xp_size_t xp_awk_str_cpy (xp_awk_str_t* str, const xp_char_t* s);
xp_size_t xp_awk_str_ncpy (
xp_awk_str_t* str, const xp_char_t* s, xp_size_t len);
xp_size_t xp_awk_str_cat (xp_awk_str_t* str, const xp_char_t* s);
xp_size_t xp_awk_str_ncat (
xp_awk_str_t* str, const xp_char_t* s, xp_size_t len);
xp_size_t xp_awk_str_ccat (xp_awk_str_t* str, xp_char_t c);
xp_size_t xp_awk_str_nccat (xp_awk_str_t* str, xp_char_t c, xp_size_t len);
void xp_awk_str_clear (xp_awk_str_t* str);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,5 +1,5 @@
/* /*
* $Id: val.c,v 1.55 2006-08-31 15:41:11 bacon Exp $ * $Id: val.c,v 1.56 2006-08-31 16:00:20 bacon Exp $
*/ */
#include <xp/awk/awk_i.h> #include <xp/awk/awk_i.h>
@ -367,7 +367,7 @@ xp_bool_t xp_awk_valtobool (xp_awk_val_t* val)
xp_char_t* xp_awk_valtostr ( xp_char_t* xp_awk_valtostr (
xp_awk_run_t* run, xp_awk_val_t* v, xp_awk_run_t* run, xp_awk_val_t* v,
xp_bool_t clear_buf, xp_str_t* buf, xp_size_t* len) xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len)
{ {
if (v->type == XP_AWK_VAL_NIL) if (v->type == XP_AWK_VAL_NIL)
{ {
@ -386,10 +386,10 @@ xp_char_t* xp_awk_valtostr (
} }
else else
{ {
if (clear_buf) xp_str_clear (buf); if (clear_buf) xp_awk_str_clear (buf);
if (len != XP_NULL) *len = XP_STR_LEN(buf); if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf);
return XP_STR_BUF(buf); return XP_AWK_STR_BUF(buf);
} }
} }
@ -420,15 +420,15 @@ xp_char_t* xp_awk_valtostr (
} }
else else
{ {
if (clear_buf) xp_str_clear (buf); if (clear_buf) xp_awk_str_clear (buf);
if (xp_str_cat (buf, XP_T("0")) == (xp_size_t)-1) if (xp_awk_str_cat (buf, XP_T("0")) == (xp_size_t)-1)
{ {
run->errnum = XP_AWK_ENOMEM; run->errnum = XP_AWK_ENOMEM;
return XP_NULL; return XP_NULL;
} }
if (len != XP_NULL) *len = XP_STR_LEN(buf); if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf);
return XP_STR_BUF(buf); return XP_AWK_STR_BUF(buf);
} }
} }
@ -452,12 +452,12 @@ xp_char_t* xp_awk_valtostr (
else else
{ {
/* clear the buffer */ /* clear the buffer */
if (clear_buf) xp_str_clear (buf); if (clear_buf) xp_awk_str_clear (buf);
tmp = XP_STR_BUF(buf) + XP_STR_LEN(buf); tmp = XP_AWK_STR_BUF(buf) + XP_AWK_STR_LEN(buf);
/* extend the buffer */ /* extend the buffer */
if (xp_str_nccat ( if (xp_awk_str_nccat (
buf, XP_T(' '), l) == (xp_size_t)-1) buf, XP_T(' '), l) == (xp_size_t)-1)
{ {
run->errnum = XP_AWK_ENOMEM; run->errnum = XP_AWK_ENOMEM;
@ -478,8 +478,8 @@ xp_char_t* xp_awk_valtostr (
if (buf != XP_NULL) if (buf != XP_NULL)
{ {
tmp = XP_STR_BUF(buf); tmp = XP_AWK_STR_BUF(buf);
if (len != XP_NULL) *len = XP_STR_LEN(buf); if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf);
} }
return tmp; return tmp;
@ -515,16 +515,16 @@ xp_char_t* xp_awk_valtostr (
} }
else else
{ {
if (clear_buf) xp_str_clear (buf); if (clear_buf) xp_awk_str_clear (buf);
if (xp_str_cat (buf, tbuf) == (xp_size_t)-1) if (xp_awk_str_cat (buf, tbuf) == (xp_size_t)-1)
{ {
run->errnum = XP_AWK_ENOMEM; run->errnum = XP_AWK_ENOMEM;
return XP_NULL; return XP_NULL;
} }
tmp = XP_STR_BUF(buf); tmp = XP_AWK_STR_BUF(buf);
if (len != XP_NULL) *len = XP_STR_LEN(buf); if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf);
} }
return tmp; return tmp;
@ -549,9 +549,9 @@ xp_char_t* xp_awk_valtostr (
} }
else else
{ {
if (clear_buf) xp_str_clear (buf); if (clear_buf) xp_awk_str_clear (buf);
if (xp_str_ncat (buf, if (xp_awk_str_ncat (buf,
((xp_awk_val_str_t*)v)->buf, ((xp_awk_val_str_t*)v)->buf,
((xp_awk_val_str_t*)v)->len) == (xp_size_t)-1) ((xp_awk_val_str_t*)v)->len) == (xp_size_t)-1)
{ {
@ -559,8 +559,8 @@ xp_char_t* xp_awk_valtostr (
return XP_NULL; return XP_NULL;
} }
tmp = XP_STR_BUF(buf); tmp = XP_AWK_STR_BUF(buf);
if (len != XP_NULL) *len = XP_STR_LEN(buf); if (len != XP_NULL) *len = XP_AWK_STR_LEN(buf);
} }
return tmp; return tmp;

View File

@ -1,5 +1,5 @@
/* /*
* $Id: val.h,v 1.38 2006-08-31 15:39:14 bacon Exp $ * $Id: val.h,v 1.39 2006-08-31 16:00:20 bacon Exp $
*/ */
#ifndef _XP_AWK_VAL_H_ #ifndef _XP_AWK_VAL_H_
@ -150,7 +150,7 @@ void xp_awk_refdownval_nofree (xp_awk_run_t* run, xp_awk_val_t* val);
xp_bool_t xp_awk_valtobool (xp_awk_val_t* val); xp_bool_t xp_awk_valtobool (xp_awk_val_t* val);
xp_char_t* xp_awk_valtostr ( xp_char_t* xp_awk_valtostr (
xp_awk_run_t* run, xp_awk_val_t* val, xp_awk_run_t* run, xp_awk_val_t* val,
xp_bool_t clear_buf, xp_str_t* buf, xp_size_t* len); xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len);
int xp_awk_valtonum (xp_awk_val_t* v, xp_long_t* l, xp_real_t* r); int xp_awk_valtonum (xp_awk_val_t* v, xp_long_t* l, xp_real_t* r);
void xp_awk_printval (xp_awk_val_t* val); void xp_awk_printval (xp_awk_val_t* val);