added code for the StdSed class

This commit is contained in:
hyung-hwan 2009-05-27 07:29:47 +00:00
parent 822c681691
commit 502bab8e4e
9 changed files with 217 additions and 85 deletions

View File

@ -40,19 +40,51 @@ public:
int open () throw ();
void close () throw ();
int compile (const char_t* sptr) throw ();
int compile (const char_t* sptr, size_t slen) throw ();
int execute () throw ();
class IO
{
public:
friend class Sed;
protected:
IO (sed_io_arg_t* arg): arg(arg) {}
public:
const char_t* getPath () const
{
return arg->path;
}
const void* getHandle () const
{
return arg->handle;
}
void setHandle (void* handle)
{
arg->handle = handle;
}
protected:
sed_io_arg_t* arg;
};
protected:
sed_t* sed;
virtual int openIn (const char_t* path) = 0;
virtual int closeIn () = 0;
virtual ssize_t readIn (char_t* buf, size_t len) = 0;
virtual int openInput (IO& io) = 0;
virtual int closeInput (IO& io) = 0;
virtual ssize_t readInput (IO& io, char_t* buf, size_t len) = 0;
virtual int openOut (const char_t* path) = 0;
virtual int closeOut () = 0;
virtual ssize_t writeOut (const char_t* buf, size_t len) = 0;
virtual int openOutput (IO& io) = 0;
virtual int closeOutput (IO& io) = 0;
virtual ssize_t writeOutput (
IO& io, const char_t* data, size_t len) = 0;
private:
static int xin (sed_t* s, sed_io_cmd_t cmd, sed_io_arg_t* arg);

View File

@ -27,12 +27,18 @@ QSE_BEGIN_NAMESPACE(QSE)
class StdSed: public Sed
{
public:
protected:
void* allocMem (qse_size_t n) throw ();
void* reallocMem (void* ptr, qse_size_t n) throw ();
void freeMem (void* ptr) throw ();
int openInput (IO& io);
int closeInput (IO& io);
ssize_t readInput (IO& io, char_t* buf, size_t len);
int openOutput (IO& io);
int closeOutput (IO& io);
ssize_t writeOutput (IO& io, const char_t* data, size_t len);
};
/////////////////////////////////

View File

@ -122,34 +122,27 @@ typedef enum qse_sed_io_cmd_t qse_sed_io_cmd_t;
/**
* The qse_sed_io_arg_t type defines a data structure required by an IO handler.
*/
union qse_sed_io_arg_t
struct qse_sed_io_arg_t
{
void* handle;
const qse_char_t* path;
union
{
struct
{
void* handle; /* out */
const qse_char_t* path; /* in */
} open;
qse_char_t* buf;
qse_size_t len;
} r;
struct
{
void* handle; /* in */
qse_char_t* buf; /* out */
qse_size_t len; /* in */
} read;
struct
{
void* handle; /* in */
const qse_char_t* data; /* in */
qse_size_t len; /* in */
} write;
struct
{
void* handle; /* in */
} close;
const qse_char_t* data;
qse_size_t len;
} w;
} u;
};
typedef union qse_sed_io_arg_t qse_sed_io_arg_t;
typedef struct qse_sed_io_arg_t qse_sed_io_arg_t;
/**
* The qse_sed_io_fun_t type defines an IO handler. An IO handler is called by

View File

@ -1,5 +1,5 @@
/*
* $Id: StdAwk.cpp 148 2009-05-20 10:44:47Z hyunghwan.chung $
* $Id: StdAwk.cpp 158 2009-05-26 13:29:47Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -343,16 +343,16 @@ int StdAwk::openFile (File& io)
switch (mode)
{
case Awk::File::READ:
flags = QSE_SIO_READ;
flags = QSE_FIO_READ;
break;
case Awk::File::WRITE:
flags = QSE_SIO_WRITE |
QSE_SIO_CREATE |
QSE_SIO_TRUNCATE;
flags = QSE_FIO_WRITE |
QSE_FIO_CREATE |
QSE_FIO_TRUNCATE;
break;
case Awk::File::APPEND:
flags = QSE_SIO_APPEND |
QSE_SIO_CREATE;
flags = QSE_FIO_APPEND |
QSE_FIO_CREATE;
break;
}
@ -360,7 +360,7 @@ int StdAwk::openFile (File& io)
((Awk*)io)->getMmgr(),
0,
io.getName(),
flags,
flags | QSE_FIO_TEXT,
QSE_FIO_RUSR | QSE_FIO_WUSR |
QSE_FIO_RGRP | QSE_FIO_ROTH
);

View File

@ -48,6 +48,12 @@ void Sed::close () throw()
}
}
int Sed::compile (const char_t* sptr) throw ()
{
QSE_ASSERT (sed != QSE_NULL);
return qse_sed_comp (sed, sptr, qse_strlen(sptr));
}
int Sed::compile (const char_t* sptr, size_t slen) throw ()
{
QSE_ASSERT (sed != QSE_NULL);
@ -63,17 +69,19 @@ int Sed::execute () throw ()
int Sed::xin (sed_t* s, sed_io_cmd_t cmd, sed_io_arg_t* arg)
{
Sed* sed = *(Sed**)QSE_XTN(s);
IO io (arg);
try
{
switch (cmd)
{
case QSE_SED_IO_OPEN:
return sed->openIn (arg->open.path);
return sed->openInput (io);
case QSE_SED_IO_CLOSE:
return sed->closeIn ();
return sed->closeInput (io);
case QSE_SED_IO_READ:
return sed->readIn (arg->read.buf, arg->read.len);
return sed->readInput (
io, arg->u.r.buf, arg->u.w.len);
default:
return -1;
}
@ -87,17 +95,19 @@ int Sed::xin (sed_t* s, sed_io_cmd_t cmd, sed_io_arg_t* arg)
int Sed::xout (sed_t* s, sed_io_cmd_t cmd, sed_io_arg_t* arg)
{
Sed* sed = *(Sed**)QSE_XTN(s);
IO io (arg);
try
{
switch (cmd)
{
case QSE_SED_IO_OPEN:
return sed->openOut (arg->open.path);
return sed->openOutput (io);
case QSE_SED_IO_CLOSE:
return sed->closeOut ();
return sed->closeOutput (io);
case QSE_SED_IO_READ:
return sed->writeOut (arg->write.data, arg->write.len);
return sed->writeOutput (
io, arg->u.w.data, arg->u.w.len);
default:
return -1;
}

View File

@ -17,6 +17,8 @@
*/
#include <qse/sed/StdSed.hpp>
#include <qse/cmn/fio.h>
#include <qse/cmn/sio.h>
#include <stdlib.h>
/////////////////////////////////
@ -38,7 +40,83 @@ void StdSed::freeMem (void* ptr) throw ()
::free (ptr);
}
int StdSed::openInput (IO& io)
{
int flags;
const qse_char_t* path = io.getPath ();
if (path == QSE_NULL) io.setHandle (qse_sio_in);
else
{
qse_fio_t* fio;
fio = qse_fio_open (
this, 0, path,
QSE_FIO_READ | QSE_FIO_TEXT,
QSE_FIO_RUSR | QSE_FIO_WUSR |
QSE_FIO_RGRP | QSE_FIO_ROTH
);
if (fio == NULL) return -1;
io.setHandle (fio);
}
return 1;
}
int StdSed::closeInput (IO& io)
{
if (io.getPath() != QSE_NULL)
qse_fio_close ((qse_fio_t*)io.getHandle());
return 0;
}
ssize_t StdSed::readInput (IO& io, char_t* buf, size_t len)
{
if (io.getPath() == QSE_NULL)
return qse_sio_getsn ((qse_sio_t*)io.getHandle(), buf, len);
else
return qse_fio_read ((qse_fio_t*)io.getHandle(), buf, len);
}
int StdSed::openOutput (IO& io)
{
int flags;
const qse_char_t* path = io.getPath ();
if (path == QSE_NULL) io.setHandle (qse_sio_out);
else
{
qse_fio_t* fio;
fio = qse_fio_open (
this, 0, path,
QSE_FIO_WRITE | QSE_FIO_CREATE |
QSE_FIO_TRUNCATE | QSE_FIO_TEXT,
QSE_FIO_RUSR | QSE_FIO_WUSR |
QSE_FIO_RGRP | QSE_FIO_ROTH
);
if (fio == NULL) return -1;
io.setHandle (fio);
}
return 1;
}
int StdSed::closeOutput (IO& io)
{
if (io.getPath() != QSE_NULL)
qse_fio_close ((qse_fio_t*)io.getHandle());
return 0;
}
ssize_t StdSed::writeOutput (IO& io, const char_t* data, size_t len)
{
if (io.getPath() == QSE_NULL)
return qse_sio_putsn ((qse_sio_t*)io.getHandle(), data, len);
else
return qse_fio_write ((qse_fio_t*)io.getHandle(), data, len);
}
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////

View File

@ -1295,8 +1295,8 @@ static int read_char (qse_sed_t* sed, qse_char_t* c)
if (sed->e.in.pos >= sed->e.in.len)
{
sed->errnum = QSE_SED_ENOERR;
sed->e.in.arg.read.buf = sed->e.in.buf;
sed->e.in.arg.read.len = QSE_COUNTOF(sed->e.in.buf);
sed->e.in.arg.u.r.buf = sed->e.in.buf;
sed->e.in.arg.u.r.len = QSE_COUNTOF(sed->e.in.buf);
n = sed->e.in.fun (
sed, QSE_SED_IO_READ, &sed->e.in.arg
);
@ -1337,7 +1337,7 @@ static int read_file (
qse_sed_io_arg_t arg;
qse_char_t buf[256];
arg.open.path = path;
arg.path = path;
sed->errnum = QSE_SED_ENOERR;
n = sed->e.in.fun (sed, QSE_SED_IO_OPEN, &arg);
if (n <= -1)
@ -1356,8 +1356,8 @@ static int read_file (
while (1)
{
arg.read.buf = buf;
arg.read.len = QSE_COUNTOF(buf);
arg.u.r.buf = buf;
arg.u.r.len = QSE_COUNTOF(buf);
sed->errnum = QSE_SED_ENOERR;
n = sed->e.in.fun (sed, QSE_SED_IO_READ, &arg);
@ -1459,8 +1459,8 @@ static int flush (qse_sed_t* sed)
while (sed->e.out.len > 0)
{
sed->errnum = QSE_SED_ENOERR;
sed->e.out.arg.write.data = &sed->e.out.buf[pos];
sed->e.out.arg.write.len = sed->e.out.len;
sed->e.out.arg.u.w.data = &sed->e.out.buf[pos];
sed->e.out.arg.u.w.len = sed->e.out.len;
n = sed->e.out.fun (sed, QSE_SED_IO_WRITE, &sed->e.out.arg);
if (n <= -1)
@ -1670,10 +1670,10 @@ static int write_str_to_file (
}
ap = QSE_MAP_VPTR(pair);
if (ap->open.handle == QSE_NULL)
if (ap->handle == QSE_NULL)
{
sed->errnum = QSE_SED_ENOERR;
ap->open.path = path;
ap->path = path;
n = sed->e.out.fun (sed, QSE_SED_IO_OPEN, ap);
if (n <= -1)
{
@ -1687,7 +1687,7 @@ static int write_str_to_file (
/* EOF is returned upon opening a write stream.
* it is also an error as it can't write any more */
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, ap);
ap->close.handle = QSE_NULL;
ap->handle = QSE_NULL;
SETERR1 (sed, QSE_SED_EIOFIL, cmd->lnum, path, plen);
return -1;
}
@ -1696,13 +1696,13 @@ static int write_str_to_file (
while (len > 0)
{
sed->errnum = QSE_SED_ENOERR;
ap->write.data = str;
ap->write.len = len;
ap->u.w.data = str;
ap->u.w.len = len;
n = sed->e.out.fun (sed, QSE_SED_IO_WRITE, ap);
if (n <= -1)
{
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, ap);
ap->close.handle = QSE_NULL;
ap->handle = QSE_NULL;
if (sed->errnum == QSE_SED_ENOERR)
SETERR1 (sed, QSE_SED_EIOFIL, 0, path, plen);
sed->errlin = cmd->lnum;
@ -1714,7 +1714,7 @@ static int write_str_to_file (
/* eof is returned on the write stream.
* it is also an error as it can't write any more */
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, ap);
ap->close.handle = QSE_NULL;
ap->handle = QSE_NULL;
SETERR1 (sed, QSE_SED_EIOFIL, cmd->lnum, path, plen);
return -1;
}
@ -2357,11 +2357,11 @@ static void close_outfile (qse_map_t* map, void* dptr, qse_size_t dlen)
qse_sed_io_arg_t* arg = dptr;
QSE_ASSERT (dlen == QSE_SIZEOF(*arg));
if (arg->close.handle != QSE_NULL)
if (arg->handle != QSE_NULL)
{
qse_sed_t* sed = *(qse_sed_t**)QSE_XTN(map);
sed->e.out.fun (sed, QSE_SED_IO_CLOSE, arg);
arg->close.handle = QSE_NULL;
arg->handle = QSE_NULL;
}
}
@ -2413,7 +2413,7 @@ int qse_sed_exec (qse_sed_t* sed, qse_sed_io_fun_t inf, qse_sed_io_fun_t outf)
}
sed->errnum = QSE_SED_ENOERR;
sed->e.in.arg.open.path = QSE_NULL;
sed->e.in.arg.path = QSE_NULL;
n = sed->e.in.fun (sed, QSE_SED_IO_OPEN, &sed->e.in.arg);
if (n <= -1)
{
@ -2430,7 +2430,7 @@ int qse_sed_exec (qse_sed_t* sed, qse_sed_io_fun_t inf, qse_sed_io_fun_t outf)
}
sed->errnum = QSE_SED_ENOERR;
sed->e.out.arg.open.path = QSE_NULL;
sed->e.out.arg.path = QSE_NULL;
n = sed->e.out.fun (sed, QSE_SED_IO_OPEN, &sed->e.out.arg);
if (n <= -1)
{

View File

@ -30,13 +30,13 @@ static qse_ssize_t in (
switch (cmd)
{
case QSE_SED_IO_OPEN:
if (arg->open.path == QSE_NULL ||
arg->open.path[0] == QSE_T('\0'))
if (arg->path == QSE_NULL ||
arg->path[0] == QSE_T('\0'))
{
if (instream)
{
arg->open.handle = qse_fopen (instream, QSE_T("r"));
if (arg->open.handle == QSE_NULL)
arg->handle = qse_fopen (instream, QSE_T("r"));
if (arg->handle == QSE_NULL)
{
qse_cstr_t errarg;
errarg.ptr = instream;
@ -45,26 +45,26 @@ static qse_ssize_t in (
return -1;
}
}
else arg->open.handle = QSE_STDIN;
else arg->handle = QSE_STDIN;
}
else
{
arg->open.handle = qse_fopen (arg->open.path, QSE_T("r"));
if (arg->open.handle == QSE_NULL) return -1;
arg->handle = qse_fopen (arg->path, QSE_T("r"));
if (arg->handle == QSE_NULL) return -1;
}
return 1;
case QSE_SED_IO_CLOSE:
if (arg->close.handle != QSE_STDIN)
qse_fclose (arg->close.handle);
if (arg->handle != QSE_STDIN)
qse_fclose (arg->handle);
return 0;
case QSE_SED_IO_READ:
{
qse_cint_t c;
c = qse_fgetc (arg->read.handle);
c = qse_fgetc (arg->handle);
if (c == QSE_CHAR_EOF) return 0;
arg->read.buf[0] = c;
arg->u.r.buf[0] = c;
return 1;
}
@ -79,29 +79,29 @@ static qse_ssize_t out (
switch (cmd)
{
case QSE_SED_IO_OPEN:
if (arg->open.path == QSE_NULL ||
arg->open.path[0] == QSE_T('\0'))
if (arg->path == QSE_NULL ||
arg->path[0] == QSE_T('\0'))
{
arg->open.handle = QSE_STDOUT;
arg->handle = QSE_STDOUT;
}
else
{
arg->open.handle = qse_fopen (arg->open.path, QSE_T("w"));
if (arg->open.handle == QSE_NULL) return -1;
arg->handle = qse_fopen (arg->path, QSE_T("w"));
if (arg->handle == QSE_NULL) return -1;
}
return 1;
case QSE_SED_IO_CLOSE:
if (arg->close.handle != QSE_STDOUT)
qse_fclose (arg->close.handle);
if (arg->handle != QSE_STDOUT)
qse_fclose (arg->handle);
return 0;
case QSE_SED_IO_WRITE:
{
qse_size_t i = 0;
for (i = 0; i < arg->write.len; i++)
qse_fputc (arg->write.data[i], arg->write.handle);
return arg->write.len;
for (i = 0; i < arg->u.w.len; i++)
qse_fputc (arg->u.w.data[i], arg->handle);
return arg->u.w.len;
}
default:

View File

@ -30,6 +30,19 @@ int sed_main (int argc, qse_char_t* argv[])
return -1;
}
if (sed.compile (argv[1]) == -1)
{
qse_printf (QSE_T("cannot compile\n"));
sed.close ();
}
if (sed.execute () == -1)
{
qse_printf (QSE_T("cannot execute\n"));
sed.close ();
return -1;
}
sed.close ();
return 0;
}