added qse_sed_getspace(), qse_sed_allocmem(), qse_sed_reallocmem(), qse_sed_callocmem(), qse_sed_freemem()

This commit is contained in:
2013-01-13 09:04:54 +00:00
parent 2337ac67b1
commit 92e4134b7d
24 changed files with 566 additions and 247 deletions

View File

@ -857,7 +857,7 @@ int StdAwk::open_console_in (Console& io)
qse_char_t ibuf[128];
qse_size_t ibuflen;
qse_awk_val_t* v;
qse_awk_rtx_valtostr_out_t out;
qse_xstr_t as;
nextfile:
file = this->runarg.ptr[this->runarg_index].ptr;
@ -928,29 +928,29 @@ int StdAwk::open_console_in (Console& io)
v = (qse_awk_val_t*)QSE_HTB_VPTR(pair);
QSE_ASSERT (v != QSE_NULL);
out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP;
if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) return -1;
as.ptr = qse_awk_rtx_valtostrdup (rtx, v, &as.len);
if (as.ptr == QSE_NULL) return -1;
if (out.u.cpldup.len == 0)
if (as.len == 0)
{
/* the name is empty */
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
qse_awk_rtx_freemem (rtx, as.ptr);
this->runarg_index++;
goto nextfile;
}
if (qse_strlen(out.u.cpldup.ptr) < out.u.cpldup.len)
if (qse_strlen(as.ptr) < as.len)
{
/* the name contains one or more '\0' */
cstr_t arg;
arg.ptr = out.u.cpldup.ptr;
arg.len = qse_strlen (arg.ptr);
arg.ptr = as.ptr;
arg.len = qse_strlen (as.ptr);
((Run*)io)->setError (QSE_AWK_EIONMNL, &arg);
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
qse_awk_rtx_freemem (rtx, as.ptr);
return -1;
}
file = out.u.cpldup.ptr;
file = as.ptr;
if (file[0] == QSE_T('-') && file[1] == QSE_T('\0'))
sio = open_sio_std (QSE_NULL, io, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
@ -958,19 +958,18 @@ int StdAwk::open_console_in (Console& io)
sio = open_sio (QSE_NULL, io, file, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL)
{
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
qse_awk_rtx_freemem (rtx, as.ptr);
return -1;
}
if (qse_awk_rtx_setfilename (
rtx, file, qse_strlen(file)) == -1)
if (qse_awk_rtx_setfilename (rtx, file, qse_strlen(file)) <= -1)
{
qse_sio_close (sio);
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
qse_awk_rtx_freemem (rtx, as.ptr);
return -1;
}
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
qse_awk_rtx_freemem (rtx, as.ptr);
if (this->console_cmgr)
qse_sio_setcmgr (sio, this->console_cmgr);

View File

@ -669,7 +669,7 @@ static int open_parsestd (qse_awk_t* awk, xtn_t* xtn, qse_size_t index)
(psin->u.file.path[0] == QSE_T('-') &&
psin->u.file.path[1] == QSE_T('\0')))
{
/* special file name '-' */
/* no path name or - -> stdin */
qse_sio_t* tmp;
tmp = open_sio_std (awk, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
@ -955,11 +955,12 @@ static qse_ssize_t sf_out (
switch (xtn->s.out.x->type)
{
case QSE_AWK_PARSESTD_FILE:
if (xtn->s.out.x->u.file.path == QSE_NULL ||
if (xtn->s.out.x->u.file.path == QSE_NULL ||
(xtn->s.out.x->u.file.path[0] == QSE_T('-') &&
xtn->s.out.x->u.file.path[1] == QSE_T('\0')))
{
/* special file name '-' */
/* no path name or - -> stdout */
xtn->s.out.u.file.sio = open_sio_std (
awk, QSE_SIO_STDOUT,
QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR
@ -1510,7 +1511,7 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod)
qse_char_t ibuf[128];
qse_size_t ibuflen;
qse_awk_val_t* v;
qse_awk_rtx_valtostr_out_t out;
qse_xstr_t as;
nextfile:
file = rxtn->c.in.files[rxtn->c.in.index];
@ -1568,35 +1569,34 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod)
v = QSE_HTB_VPTR(pair);
QSE_ASSERT (v != QSE_NULL);
out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP;
if (qse_awk_rtx_valtostr (rtx, v, &out) <= -1) return -1;
as.ptr = qse_awk_rtx_valtostrdup (rtx, v, &as.len);
if (as.ptr == QSE_NULL) return -1;
if (out.u.cpldup.len == 0)
if (as.len == 0)
{
/* the name is empty */
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
qse_awk_rtx_freemem (rtx, as.ptr);
rxtn->c.in.index++;
goto nextfile;
}
if (qse_strlen(out.u.cpldup.ptr) < out.u.cpldup.len)
if (qse_strlen(as.ptr) < as.len)
{
/* the name contains one or more '\0' */
qse_cstr_t errarg;
errarg.ptr = out.u.cpldup.ptr;
errarg.ptr = as.ptr;
/* use this length not to contains '\0'
* in an error message */
errarg.len = qse_strlen(out.u.cpldup.ptr);
errarg.len = qse_strlen(as.ptr);
qse_awk_rtx_seterrnum (
rtx, QSE_AWK_EIONMNL, &errarg);
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIONMNL, &errarg);
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
qse_awk_rtx_freemem (rtx, as.ptr);
return -1;
}
file = out.u.cpldup.ptr;
file = as.ptr;
sio = (file[0] == QSE_T('-') && file[1] == QSE_T('\0'))?
open_sio_std_rtx (rtx, QSE_SIO_STDIN,
@ -1605,7 +1605,7 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod)
QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL)
{
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
qse_awk_rtx_freemem (rtx, as.ptr);
return -1;
}
@ -1615,11 +1615,11 @@ static int open_rio_console (qse_awk_rtx_t* rtx, qse_awk_rio_arg_t* riod)
rtx, file, qse_strlen(file)) <= -1)
{
qse_sio_close (sio);
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
qse_awk_rtx_freemem (rtx, as.ptr);
return -1;
}
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
qse_awk_rtx_freemem (rtx, as.ptr);
riod->handle = sio;
/* increment the counter of files successfully opened */

View File

@ -78,17 +78,17 @@ int StdSed::FileStream::open (Data& io)
if (ioname == QSE_NULL)
{
//
// a normal console is indicated by a null name
// a normal console is indicated by a null name or a dash
//
if (io.getMode() == READ)
{
sio = (infile == QSE_NULL)?
sio = (infile == QSE_NULL || (infile[0] == QSE_T('-') && infile[1] == QSE_T('\0')))?
open_sio_std (io, QSE_SIO_STDIN, oflags):
open_sio (io, infile, oflags);
}
else
{
sio = (outfile == QSE_NULL)?
sio = (outfile == QSE_NULL || (outfile[0] == QSE_T('-') && outfile[1] == QSE_T('\0')))?
open_sio_std (io, QSE_SIO_STDOUT, oflags):
open_sio (io, outfile, oflags);
}

View File

@ -504,14 +504,9 @@ static int add_command_block (qse_sed_t* sed)
{
qse_sed_cmd_blk_t* b;
b = (qse_sed_cmd_blk_t*) QSE_MMGR_ALLOC (sed->mmgr, QSE_SIZEOF(*b));
if (b == QSE_NULL)
{
SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL);
return -1;
}
b = (qse_sed_cmd_blk_t*) qse_sed_callocmem (sed, QSE_SIZEOF(*b));
if (b == QSE_NULL) return -1;
QSE_MEMSET (b, 0, QSE_SIZEOF(*b));
b->next = QSE_NULL;
b->len = 0;
@ -1513,14 +1508,9 @@ static int add_cut_selector_block (qse_sed_t* sed, qse_sed_cmd_t* cmd)
{
qse_sed_cut_sel_t* b;
b = (qse_sed_cut_sel_t*) QSE_MMGR_ALLOC (sed->mmgr, QSE_SIZEOF(*b));
if (b == QSE_NULL)
{
SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL);
return -1;
}
b = (qse_sed_cut_sel_t*) qse_sed_callocmem (sed, QSE_SIZEOF(*b));
if (b == QSE_NULL) return -1;
QSE_MEMSET (b, 0, QSE_SIZEOF(*b));
b->next = QSE_NULL;
b->len = 0;
@ -3021,22 +3011,14 @@ static int split_into_fields_for_cut (
if (sed->e.cutf.flds == sed->e.cutf.sflds)
{
tmp = QSE_MMGR_ALLOC (sed->mmgr, QSE_SIZEOF(*tmp) * nsz);
if (tmp == QSE_NULL)
{
SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL);
return -1;
}
tmp = qse_sed_allocmem (sed, QSE_SIZEOF(*tmp) * nsz);
if (tmp == QSE_NULL) return -1;
QSE_MEMCPY (tmp, sed->e.cutf.flds, QSE_SIZEOF(*tmp) * sed->e.cutf.cflds);
}
else
{
tmp = QSE_MMGR_REALLOC (sed->mmgr, sed->e.cutf.flds, QSE_SIZEOF(*tmp) * nsz);
if (tmp == QSE_NULL)
{
SETERR0 (sed, QSE_SED_ENOMEM, QSE_NULL);
return -1;
}
tmp = qse_sed_reallocmem (sed, sed->e.cutf.flds, QSE_SIZEOF(*tmp) * nsz);
if (tmp == QSE_NULL) return -1;
}
sed->e.cutf.flds = tmp;
@ -4131,6 +4113,35 @@ void qse_sed_pushecb (qse_sed_t* sed, qse_sed_ecb_t* ecb)
sed->ecb = ecb;
}
void* qse_sed_allocmem (qse_sed_t* sed, qse_size_t size)
{
void* ptr = QSE_MMGR_ALLOC (sed->mmgr, size);
if (ptr == QSE_NULL)
qse_sed_seterrnum (sed, QSE_SED_ENOMEM, QSE_NULL);
return ptr;
}
void* qse_sed_callocmem (qse_sed_t* sed, qse_size_t size)
{
void* ptr = QSE_MMGR_ALLOC (sed->mmgr, size);
if (ptr) QSE_MEMSET (ptr, 0, size);
else qse_sed_seterrnum (sed, QSE_SED_ENOMEM, QSE_NULL);
return ptr;
}
void* qse_sed_reallocmem (qse_sed_t* sed, void* ptr, qse_size_t size)
{
void* nptr = QSE_MMGR_REALLOC (sed->mmgr, ptr, size);
if (nptr == QSE_NULL) qse_sed_seterrnum (sed, QSE_SED_ENOMEM, QSE_NULL);
return nptr;
}
void qse_sed_freemem (qse_sed_t* sed, void* ptr)
{
QSE_MMGR_FREE (sed->mmgr, ptr);
}
#ifdef QSE_ENABLE_SEDTRACER
qse_sed_exec_tracer_t qse_sed_getexectracer (qse_sed_t* sed)
{
@ -4143,3 +4154,17 @@ void qse_sed_setexectracer (qse_sed_t* sed, qse_sed_exec_tracer_t tracer)
}
#endif
void qse_sed_getspace (qse_sed_t* sed, qse_sed_space_t space, qse_cstr_t* str)
{
switch (space)
{
case QSE_SED_SPACE_HOLD:
str->ptr = QSE_STR_PTR(&sed->e.txt.hold);
str->len = QSE_STR_LEN(&sed->e.txt.hold);
break;
case QSE_SED_SPACE_PATTERN:
str->ptr = QSE_STR_PTR(&sed->e.in.line);
str->len = QSE_STR_LEN(&sed->e.in.line);
break;
}
}

View File

@ -174,10 +174,10 @@ struct qse_sed_t
/** data needed for input streams */
struct
{
qse_sed_io_impl_t fun; /**< an input handler */
qse_sed_io_impl_t fun; /**< input handler */
qse_sed_io_arg_t arg; /**< input handling data */
qse_char_t xbuf[1]; /**< a read-ahead buffer */
qse_char_t xbuf[1]; /**< read-ahead buffer */
int xbuf_len; /**< data length in the buffer */
qse_char_t buf[2048]; /**< input buffer */
@ -199,6 +199,7 @@ struct qse_sed_t
qse_sed_app_t* tail;
} d;
} append;
/** text buffers */
struct
{

View File

@ -216,9 +216,22 @@ static int open_input_stream (
case QSE_SED_IOSTD_FILE:
{
qse_sio_t* sio;
sio = (io->u.file.path == QSE_NULL)?
open_sio_std (sed, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR):
open_sio_file (sed, io->u.file.path, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
if (io->u.file.path == QSE_NULL ||
(io->u.file.path[0] == QSE_T('-') &&
io->u.file.path[1] == QSE_T('\0')))
{
sio = open_sio_std (
sed, QSE_SIO_STDIN,
QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR
);
}
else
{
sio = open_sio_file (
sed, io->u.file.path,
QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
}
if (sio == QSE_NULL) return -1;
if (io->u.file.cmgr) qse_sio_setcmgr (sio, io->u.file.cmgr);
arg->handle = sio;
@ -284,7 +297,9 @@ static int open_output_stream (qse_sed_t* sed, qse_sed_io_arg_t* arg, qse_sed_io
case QSE_SED_IOSTD_FILE:
{
qse_sio_t* sio;
if (io->u.file.path == QSE_NULL)
if (io->u.file.path == QSE_NULL ||
(io->u.file.path[0] == QSE_T('-') &&
io->u.file.path[1] == QSE_T('\0')))
{
sio = open_sio_std (
sed, QSE_SIO_STDOUT,
@ -512,6 +527,7 @@ static qse_ssize_t x_in (
/* no file specified. console stream */
if (xtn->e.in.ptr == QSE_NULL)
{
/* QSE_NULL passed into qse_sed_exec() for input */
sio = open_sio_std (
sed, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
if (sio == QSE_NULL) return -1;
@ -609,8 +625,11 @@ static qse_ssize_t x_out (
{
if (arg->path == QSE_NULL)
{
/* main data stream */
if (xtn->e.out.ptr == QSE_NULL)
{
/* QSE_NULL passed into qse_sed_execstd() for output */
sio = open_sio_std (
sed, QSE_SIO_STDOUT,
QSE_SIO_WRITE |
@ -628,6 +647,7 @@ static qse_ssize_t x_out (
}
else
{
sio = open_sio_file (
sed, arg->path,
QSE_SIO_WRITE |
@ -785,7 +805,7 @@ int qse_sed_execstd (
QSE_MEMSET (&xtn->e, 0, QSE_SIZEOF(xtn->e));
xtn->e.in.ptr = in;
xtn->e.in.cur = in;
xtn->e.out.ptr= out;
xtn->e.out.ptr = out;
n = qse_sed_exec (sed, x_in, x_out);