added Awk::Pipe::Handler, Awk::File::Handler, Awk::Console::Handler

enhanced how to invoke I/O handler functions and objects
This commit is contained in:
hyung-hwan 2012-06-06 14:41:21 +00:00
parent c64efd8ae7
commit 4223736c14
10 changed files with 631 additions and 86 deletions

View File

@ -319,6 +319,16 @@ public:
CLOSE_WRITE = QSE_AWK_RIO_CLOSE_WRITE
};
class Handler
{
public:
virtual int open (Pipe& io) = 0;
virtual int close (Pipe& io) = 0;
virtual ssize_t read (Pipe& io, char_t* buf, size_t len) = 0;
virtual ssize_t write (Pipe& io, const char_t* buf, size_t len) = 0;
virtual int flush (Pipe& io) = 0;
};
protected:
Pipe (Run* run, rio_arg_t* riod);
@ -351,6 +361,16 @@ public:
APPEND = QSE_AWK_RIO_FILE_APPEND
};
class Handler
{
public:
virtual int open (File& io) = 0;
virtual int close (File& io) = 0;
virtual ssize_t read (File& io, char_t* buf, size_t len) = 0;
virtual ssize_t write (File& io, const char_t* buf, size_t len) = 0;
virtual int flush (File& io) = 0;
};
protected:
File (Run* run, rio_arg_t* riod);
@ -373,6 +393,17 @@ public:
WRITE = QSE_AWK_RIO_CONSOLE_WRITE
};
class Handler
{
public:
virtual int open (Console& io) = 0;
virtual int close (Console& io) = 0;
virtual ssize_t read (Console& io, char_t* buf, size_t len) = 0;
virtual ssize_t write (Console& io, const char_t* buf, size_t len) = 0;
virtual int flush (Console& io) = 0;
virtual int next (Console& io) = 0;
};
protected:
Console (Run* run, rio_arg_t* riod);
~Console ();
@ -386,6 +417,7 @@ public:
char_t* filename;
};
///
/// The Value class wraps around #qse_awk_val_t to provide a more
/// comprehensive interface.
@ -1050,50 +1082,119 @@ public:
);
/// @}
Pipe::Handler* getPipeHandler ()
{
return this->pipe_handler;
}
const Pipe::Handler* getPipeHandler () const
{
return this->pipe_handler;
}
///
/// The setPipeHandler() function registers an external pipe
/// handler object. An external pipe handler can be implemented
/// outside this class without overriding various pipe functions.
/// Note that an external pipe handler must outlive an outer
/// awk object.
///
void setPipeHandler (Pipe::Handler* handler)
{
this->pipe_handler = handler;
}
File::Handler* getFileHandler ()
{
return this->file_handler;
}
const File::Handler* getFileHandler () const
{
return this->file_handler;
}
///
/// The setFileHandler() function registers an external file
/// handler object. An external file handler can be implemented
/// outside this class without overriding various file functions.
/// Note that an external file handler must outlive an outer
/// awk object.
///
void setFileHandler (File::Handler* handler)
{
this->file_handler = handler;
}
Console::Handler* getConsoleHandler ()
{
return this->console_handler;
}
const Console::Handler* getConsoleHandler () const
{
return this->console_handler;
}
///
/// The setConsoleHandler() function registers an external console
/// handler object. An external file handler can be implemented
/// outside this class without overriding various console functions.
/// Note that an external console handler must outlive an outer
/// awk object.
///
void setConsoleHandler (Console::Handler* handler)
{
this->console_handler = handler;
}
protected:
///
/// @name Pipe I/O handlers
/// Pipe operations are achieved through the following functions.
/// Pipe operations are achieved through the following functions
/// if no external pipe handler is set with setPipeHandler().
/// @{
/// The openPipe() function is a pure virtual function that must be
/// overridden by a child class to open a pipe. It must return 1
/// on success, 0 on end of a pipe, and -1 on failure.
virtual int openPipe (Pipe& io) = 0;
virtual int openPipe (Pipe& io);
/// The closePipe() function is a pure virtual function that must be
/// overridden by a child class to close a pipe. It must return 0
/// on success and -1 on failure.
virtual int closePipe (Pipe& io) = 0;
virtual int closePipe (Pipe& io);
virtual ssize_t readPipe (Pipe& io, char_t* buf, size_t len) = 0;
virtual ssize_t writePipe (Pipe& io, const char_t* buf, size_t len) = 0;
virtual int flushPipe (Pipe& io) = 0;
virtual ssize_t readPipe (Pipe& io, char_t* buf, size_t len);
virtual ssize_t writePipe (Pipe& io, const char_t* buf, size_t len);
virtual int flushPipe (Pipe& io);
/// @}
///
/// @name File I/O handlers
/// File operations are achieved through the following functions.
/// File operations are achieved through the following functions
/// if no external file handler is set with setFileHandler().
/// @{
///
virtual int openFile (File& io) = 0;
virtual int closeFile (File& io) = 0;
virtual ssize_t readFile (File& io, char_t* buf, size_t len) = 0;
virtual ssize_t writeFile (File& io, const char_t* buf, size_t len) = 0;
virtual int flushFile (File& io) = 0;
virtual int openFile (File& io);
virtual int closeFile (File& io);
virtual ssize_t readFile (File& io, char_t* buf, size_t len);
virtual ssize_t writeFile (File& io, const char_t* buf, size_t len);
virtual int flushFile (File& io);
/// @}
///
/// @name Console I/O handlers
/// Console operations are achieved through the following functions.
/// if no external console handler is set with setConsoleHandler().
/// @{
///
virtual int openConsole (Console& io) = 0;
virtual int closeConsole (Console& io) = 0;
virtual ssize_t readConsole (Console& io, char_t* buf, size_t len) = 0;
virtual ssize_t writeConsole (Console& io, const char_t* buf, size_t len) = 0;
virtual int flushConsole (Console& io) = 0;
virtual int nextConsole (Console& io) = 0;
virtual int openConsole (Console& io);
virtual int closeConsole (Console& io);
virtual ssize_t readConsole (Console& io, char_t* buf, size_t len);
virtual ssize_t writeConsole (Console& io, const char_t* buf, size_t len);
virtual int flushConsole (Console& io);
virtual int nextConsole (Console& io);
/// @}
// primitive handlers
@ -1132,8 +1233,9 @@ protected:
static int functionHandler (rtx_t* rtx, const cstr_t* name);
static int sprintf (awk_t* data, char_t* buf, size_t size,
const char_t* fmt, ...);
static int sprintf (awk_t* data, char_t* buf, size_t size,
const char_t* fmt, ...);
static flt_t pow (awk_t* data, flt_t x, flt_t y);
static flt_t mod (awk_t* data, flt_t x, flt_t y);
static flt_t sin (awk_t* data, flt_t x);
@ -1154,8 +1256,12 @@ protected:
htb_t* functionMap;
Source* sourceReader;
Source* sourceWriter;
Source* source_reader;
Source* source_writer;
Pipe::Handler* pipe_handler;
File::Handler* file_handler;
Console::Handler* console_handler;
struct xstrs_t
{

View File

@ -40,8 +40,19 @@
/// This program shows how to add intrinsic functions.
///
/// @example awk12.cpp
/// This program shows how to override console methods to use a
/// This program shows how to override console methods to use
/// string buffers for console input and output.
///
/// @example awk13.cpp
/// This program shows how to use resetRunContext(). It is similar
/// to awk12.cpp in principle.
///
/// @example awk14.cpp
/// This program shows how to define a console handler to use
/// string buffers for console input and output. It is identical
/// to awk13.cpp except that it relies an external console handler
/// rather than overriding console methods.
///
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)

View File

@ -850,6 +850,7 @@ enum qse_awk_errnum_t
{
QSE_AWK_ENOERR, /**< no error */
QSE_AWK_EINTERN, /**< internal error */
QSE_AWK_ENOIMPL, /**< not implemented */
/* common errors */
QSE_AWK_ENOMEM, /**< insufficient memory */

View File

@ -960,7 +960,10 @@ int Awk::Run::getGlobal (int id, Value& g) const
//////////////////////////////////////////////////////////////////
Awk::Awk (Mmgr* mmgr):
Mmged (mmgr), awk (QSE_NULL), functionMap (QSE_NULL), runctx (this)
Mmged (mmgr), awk (QSE_NULL), functionMap (QSE_NULL),
source_reader (QSE_NULL), source_writer (QSE_NULL),
pipe_handler (QSE_NULL), file_handler (QSE_NULL),
console_handler (QSE_NULL), runctx (this)
{
QSE_MEMSET (&errinf, 0, QSE_SIZEOF(errinf));
@ -1162,12 +1165,12 @@ Awk::Run* Awk::parse (Source& in, Source& out)
fini_runctx ();
sourceReader = &in;
sourceWriter = (&out == &Source::NONE)? QSE_NULL: &out;
source_reader = &in;
source_writer = (&out == &Source::NONE)? QSE_NULL: &out;
qse_awk_sio_t sio;
sio.in = readSource;
sio.out = (sourceWriter == QSE_NULL)? QSE_NULL: writeSource;
sio.out = (source_writer == QSE_NULL)? QSE_NULL: writeSource;
int n = qse_awk_parse (awk, &sio);
if (n <= -1)
@ -1592,11 +1595,11 @@ Awk::ssize_t Awk::readSource (
switch (cmd)
{
case QSE_AWK_SIO_OPEN:
return xtn->awk->sourceReader->open (sdat);
return xtn->awk->source_reader->open (sdat);
case QSE_AWK_SIO_CLOSE:
return xtn->awk->sourceReader->close (sdat);
return xtn->awk->source_reader->close (sdat);
case QSE_AWK_SIO_READ:
return xtn->awk->sourceReader->read (sdat, data, count);
return xtn->awk->source_reader->read (sdat, data, count);
default:
return -1;
}
@ -1612,11 +1615,11 @@ Awk::ssize_t Awk::writeSource (
switch (cmd)
{
case QSE_AWK_SIO_OPEN:
return xtn->awk->sourceWriter->open (sdat);
return xtn->awk->source_writer->open (sdat);
case QSE_AWK_SIO_CLOSE:
return xtn->awk->sourceWriter->close (sdat);
return xtn->awk->source_writer->close (sdat);
case QSE_AWK_SIO_WRITE:
return xtn->awk->sourceWriter->write (sdat, data, count);
return xtn->awk->source_writer->write (sdat, data, count);
default:
return -1;
}
@ -1635,23 +1638,47 @@ Awk::ssize_t Awk::pipeHandler (
try
{
switch (cmd)
if (awk->pipe_handler)
{
case QSE_AWK_RIO_OPEN:
return awk->openPipe (pipe);
case QSE_AWK_RIO_CLOSE:
return awk->closePipe (pipe);
case QSE_AWK_RIO_READ:
return awk->readPipe (pipe, data, count);
case QSE_AWK_RIO_WRITE:
return awk->writePipe (pipe, data, count);
case QSE_AWK_RIO_FLUSH:
return awk->flushPipe (pipe);
default:
return -1;
switch (cmd)
{
case QSE_AWK_RIO_OPEN:
return awk->pipe_handler->open (pipe);
case QSE_AWK_RIO_CLOSE:
return awk->pipe_handler->close (pipe);
case QSE_AWK_RIO_READ:
return awk->pipe_handler->read (pipe, data, count);
case QSE_AWK_RIO_WRITE:
return awk->pipe_handler->write (pipe, data, count);
case QSE_AWK_RIO_FLUSH:
return awk->pipe_handler->flush (pipe);
default:
return -1;
}
}
else
{
switch (cmd)
{
case QSE_AWK_RIO_OPEN:
return awk->openPipe (pipe);
case QSE_AWK_RIO_CLOSE:
return awk->closePipe (pipe);
case QSE_AWK_RIO_READ:
return awk->readPipe (pipe, data, count);
case QSE_AWK_RIO_WRITE:
return awk->writePipe (pipe, data, count);
case QSE_AWK_RIO_FLUSH:
return awk->flushPipe (pipe);
default:
return -1;
}
}
}
catch (...)
@ -1673,23 +1700,47 @@ Awk::ssize_t Awk::fileHandler (
try
{
switch (cmd)
if (awk->file_handler)
{
case QSE_AWK_RIO_OPEN:
return awk->openFile (file);
case QSE_AWK_RIO_CLOSE:
return awk->closeFile (file);
switch (cmd)
{
case QSE_AWK_RIO_OPEN:
return awk->file_handler->open (file);
case QSE_AWK_RIO_CLOSE:
return awk->file_handler->close (file);
case QSE_AWK_RIO_READ:
return awk->file_handler->read (file, data, count);
case QSE_AWK_RIO_WRITE:
return awk->file_handler->write (file, data, count);
case QSE_AWK_RIO_FLUSH:
return awk->file_handler->flush (file);
default:
return -1;
}
}
else
{
switch (cmd)
{
case QSE_AWK_RIO_OPEN:
return awk->openFile (file);
case QSE_AWK_RIO_CLOSE:
return awk->closeFile (file);
case QSE_AWK_RIO_READ:
return awk->readFile (file, data, count);
case QSE_AWK_RIO_WRITE:
return awk->writeFile (file, data, count);
case QSE_AWK_RIO_READ:
return awk->readFile (file, data, count);
case QSE_AWK_RIO_WRITE:
return awk->writeFile (file, data, count);
case QSE_AWK_RIO_FLUSH:
return awk->flushFile (file);
case QSE_AWK_RIO_FLUSH:
return awk->flushFile (file);
default:
return -1;
default:
return -1;
}
}
}
catch (...)
@ -1711,25 +1762,51 @@ Awk::ssize_t Awk::consoleHandler (
try
{
switch (cmd)
if (awk->console_handler)
{
case QSE_AWK_RIO_OPEN:
return awk->openConsole (console);
case QSE_AWK_RIO_CLOSE:
return awk->closeConsole (console);
switch (cmd)
{
case QSE_AWK_RIO_OPEN:
return awk->console_handler->open (console);
case QSE_AWK_RIO_CLOSE:
return awk->console_handler->close (console);
case QSE_AWK_RIO_READ:
return awk->readConsole (console, data, count);
case QSE_AWK_RIO_WRITE:
return awk->writeConsole (console, data, count);
case QSE_AWK_RIO_READ:
return awk->console_handler->read (console, data, count);
case QSE_AWK_RIO_WRITE:
return awk->console_handler->write (console, data, count);
case QSE_AWK_RIO_FLUSH:
return awk->flushConsole (console);
case QSE_AWK_RIO_NEXT:
return awk->nextConsole (console);
case QSE_AWK_RIO_FLUSH:
return awk->console_handler->flush (console);
case QSE_AWK_RIO_NEXT:
return awk->console_handler->next (console);
default:
return -1;
default:
return -1;
}
}
else
{
switch (cmd)
{
case QSE_AWK_RIO_OPEN:
return awk->openConsole (console);
case QSE_AWK_RIO_CLOSE:
return awk->closeConsole (console);
case QSE_AWK_RIO_READ:
return awk->readConsole (console, data, count);
case QSE_AWK_RIO_WRITE:
return awk->writeConsole (console, data, count);
case QSE_AWK_RIO_FLUSH:
return awk->flushConsole (console);
case QSE_AWK_RIO_NEXT:
return awk->nextConsole (console);
default:
return -1;
}
}
}
catch (...)
@ -1738,6 +1815,102 @@ Awk::ssize_t Awk::consoleHandler (
}
}
int Awk::openPipe (Pipe& io)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
int Awk::closePipe (Pipe& io)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
Awk::ssize_t Awk::readPipe (Pipe& io, char_t* buf, size_t len)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
Awk::ssize_t Awk::writePipe (Pipe& io, const char_t* buf, size_t len)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
int Awk::flushPipe (Pipe& io)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
int Awk::openFile (File& io)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
int Awk::closeFile (File& io)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
Awk::ssize_t Awk::readFile (File& io, char_t* buf, size_t len)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
Awk::ssize_t Awk::writeFile (File& io, const char_t* buf, size_t len)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
int Awk::flushFile (File& io)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
int Awk::openConsole (Console& io)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
int Awk::closeConsole (Console& io)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
Awk::ssize_t Awk::readConsole (Console& io, char_t* buf, size_t len)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
Awk::ssize_t Awk::writeConsole (Console& io, const char_t* buf, size_t len)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
int Awk::flushConsole (Console& io)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
int Awk::nextConsole (Console& io)
{
((Run*)io)->setError (QSE_AWK_ENOIMPL);
return -1;
}
int Awk::functionHandler (rtx_t* rtx, const cstr_t* name)
{
rxtn_t* rxtn = (rxtn_t*) QSE_XTN (rtx);

View File

@ -26,6 +26,7 @@ const qse_char_t* qse_awk_dflerrstr (qse_awk_t* awk, qse_awk_errnum_t errnum)
{
QSE_T("no error"),
QSE_T("internal error that should never have happened"),
QSE_T("not implemented"),
QSE_T("insufficient memory"),
QSE_T("invalid parameter or data"),

View File

@ -26,7 +26,7 @@ if ENABLE_CXX
CXXLIB = -lqseawkxx -lqsecmnxx
bin_PROGRAMS += awk05 awk06 awk07 awk08 awk12 awk13
bin_PROGRAMS += awk05 awk06 awk07 awk08 awk12 awk13 awk14
awk05_SOURCES = awk05.cpp
awk06_SOURCES = awk06.cpp
@ -34,6 +34,7 @@ awk07_SOURCES = awk07.cpp
awk08_SOURCES = awk08.cpp
awk12_SOURCES = awk12.cpp
awk13_SOURCES = awk13.cpp
awk14_SOURCES = awk14.cpp
awk05_LDADD = $(CXXLIB) $(LDADD)
awk06_LDADD = $(CXXLIB) $(LDADD)
@ -41,4 +42,5 @@ awk07_LDADD = $(CXXLIB) $(LDADD)
awk08_LDADD = $(CXXLIB) $(LDADD)
awk12_LDADD = $(CXXLIB) $(LDADD)
awk13_LDADD = $(CXXLIB) $(LDADD)
awk14_LDADD = $(CXXLIB) $(LDADD)
endif

View File

@ -38,7 +38,7 @@ bin_PROGRAMS = awk01$(EXEEXT) awk02$(EXEEXT) awk03$(EXEEXT) \
awk04$(EXEEXT) awk09$(EXEEXT) awk10$(EXEEXT) awk11$(EXEEXT) \
$(am__EXEEXT_1)
@WIN32_TRUE@am__append_1 = $(UNICOWS_LIBS)
@ENABLE_CXX_TRUE@am__append_2 = awk05 awk06 awk07 awk08 awk12 awk13
@ENABLE_CXX_TRUE@am__append_2 = awk05 awk06 awk07 awk08 awk12 awk13 awk14
subdir = samples/awk
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
@ -55,7 +55,7 @@ CONFIG_CLEAN_FILES =
CONFIG_CLEAN_VPATH_FILES =
@ENABLE_CXX_TRUE@am__EXEEXT_1 = awk05$(EXEEXT) awk06$(EXEEXT) \
@ENABLE_CXX_TRUE@ awk07$(EXEEXT) awk08$(EXEEXT) awk12$(EXEEXT) \
@ENABLE_CXX_TRUE@ awk13$(EXEEXT)
@ENABLE_CXX_TRUE@ awk13$(EXEEXT) awk14$(EXEEXT)
am__installdirs = "$(DESTDIR)$(bindir)"
PROGRAMS = $(bin_PROGRAMS)
am_awk01_OBJECTS = awk01.$(OBJEXT)
@ -119,6 +119,11 @@ am__awk13_SOURCES_DIST = awk13.cpp
awk13_OBJECTS = $(am_awk13_OBJECTS)
@ENABLE_CXX_TRUE@awk13_DEPENDENCIES = $(am__DEPENDENCIES_1) \
@ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_3)
am__awk14_SOURCES_DIST = awk14.cpp
@ENABLE_CXX_TRUE@am_awk14_OBJECTS = awk14.$(OBJEXT)
awk14_OBJECTS = $(am_awk14_OBJECTS)
@ENABLE_CXX_TRUE@awk14_DEPENDENCIES = $(am__DEPENDENCIES_1) \
@ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_3)
DEFAULT_INCLUDES =
depcomp = $(SHELL) $(top_srcdir)/ac/depcomp
am__depfiles_maybe = depfiles
@ -145,13 +150,13 @@ SOURCES = $(awk01_SOURCES) $(awk02_SOURCES) $(awk03_SOURCES) \
$(awk04_SOURCES) $(awk05_SOURCES) $(awk06_SOURCES) \
$(awk07_SOURCES) $(awk08_SOURCES) $(awk09_SOURCES) \
$(awk10_SOURCES) $(awk11_SOURCES) $(awk12_SOURCES) \
$(awk13_SOURCES)
$(awk13_SOURCES) $(awk14_SOURCES)
DIST_SOURCES = $(awk01_SOURCES) $(awk02_SOURCES) $(awk03_SOURCES) \
$(awk04_SOURCES) $(am__awk05_SOURCES_DIST) \
$(am__awk06_SOURCES_DIST) $(am__awk07_SOURCES_DIST) \
$(am__awk08_SOURCES_DIST) $(awk09_SOURCES) $(awk10_SOURCES) \
$(awk11_SOURCES) $(am__awk12_SOURCES_DIST) \
$(am__awk13_SOURCES_DIST)
$(am__awk13_SOURCES_DIST) $(am__awk14_SOURCES_DIST)
ETAGS = etags
CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@ -322,12 +327,14 @@ awk11_SOURCES = awk11.c
@ENABLE_CXX_TRUE@awk08_SOURCES = awk08.cpp
@ENABLE_CXX_TRUE@awk12_SOURCES = awk12.cpp
@ENABLE_CXX_TRUE@awk13_SOURCES = awk13.cpp
@ENABLE_CXX_TRUE@awk14_SOURCES = awk14.cpp
@ENABLE_CXX_TRUE@awk05_LDADD = $(CXXLIB) $(LDADD)
@ENABLE_CXX_TRUE@awk06_LDADD = $(CXXLIB) $(LDADD)
@ENABLE_CXX_TRUE@awk07_LDADD = $(CXXLIB) $(LDADD)
@ENABLE_CXX_TRUE@awk08_LDADD = $(CXXLIB) $(LDADD)
@ENABLE_CXX_TRUE@awk12_LDADD = $(CXXLIB) $(LDADD)
@ENABLE_CXX_TRUE@awk13_LDADD = $(CXXLIB) $(LDADD)
@ENABLE_CXX_TRUE@awk14_LDADD = $(CXXLIB) $(LDADD)
all: all-am
.SUFFIXES:
@ -444,6 +451,9 @@ awk12$(EXEEXT): $(awk12_OBJECTS) $(awk12_DEPENDENCIES) $(EXTRA_awk12_DEPENDENCIE
awk13$(EXEEXT): $(awk13_OBJECTS) $(awk13_DEPENDENCIES) $(EXTRA_awk13_DEPENDENCIES)
@rm -f awk13$(EXEEXT)
$(CXXLINK) $(awk13_OBJECTS) $(awk13_LDADD) $(LIBS)
awk14$(EXEEXT): $(awk14_OBJECTS) $(awk14_DEPENDENCIES) $(EXTRA_awk14_DEPENDENCIES)
@rm -f awk14$(EXEEXT)
$(CXXLINK) $(awk14_OBJECTS) $(awk14_LDADD) $(LIBS)
mostlyclean-compile:
-rm -f *.$(OBJEXT)
@ -464,6 +474,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/awk11.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/awk12.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/awk13.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/awk14.Po@am__quote@
.c.o:
@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<

View File

@ -153,7 +153,7 @@ static int run_awk (MyAwk& awk)
if (x >= 0)
{
qse_printf (QSE_T("%s"), awk.getOutput()); // print the console output
qse_printf (QSE_T("-----------------------------\n"), awk.getOutput());
qse_printf (QSE_T("-----------------------------\n"));
// prepare a string to print lines with A in the fourth column
MyAwk::SourceString in2 (QSE_T("$4 == \"A\" { print $1; }"));
@ -167,7 +167,7 @@ static int run_awk (MyAwk& awk)
if (x >= 0)
{
qse_printf (QSE_T("%s"), awk.getOutput());
qse_printf (QSE_T("-----------------------------\n"), awk.getOutput());
qse_printf (QSE_T("-----------------------------\n"));
}
}

View File

@ -174,7 +174,7 @@ static int run_awk (MyAwk& awk)
if (x >= 0)
{
qse_printf (QSE_T("%s"), awk.getOutput()); // print the console output
qse_printf (QSE_T("-----------------------------\n"), awk.getOutput());
qse_printf (QSE_T("-----------------------------\n"));
awk.setInput (instr2);
@ -187,7 +187,7 @@ static int run_awk (MyAwk& awk)
if (x >= 0)
{
qse_printf (QSE_T("%s"), awk.getOutput());
qse_printf (QSE_T("-----------------------------\n"), awk.getOutput());
qse_printf (QSE_T("-----------------------------\n"));
}
}

240
qse/samples/awk/awk14.cpp Normal file
View File

@ -0,0 +1,240 @@
/*
* $Id$
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
QSE is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
QSE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
*/
#include <qse/awk/StdAwk.hpp>
#include <qse/cmn/stdio.h>
#include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h>
#include <qse/cmn/str.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#include <string>
#if defined(QSE_CHAR_IS_WCHAR)
typedef std::wstring String;
#else
typedef std::string String;
#endif
typedef QSE::StdAwk StdAwk;
typedef QSE::StdAwk::Run Run;
typedef QSE::StdAwk::Value Value;
class MyConsoleHandler: public StdAwk::Console::Handler
{
public:
// this class defines a console handler that can be
// registered into an awk object.
void setInput (const StdAwk::char_t* instr)
{
this->input = instr;
this->inptr = this->input.c_str();
this->inend = inptr + this->input.length();
}
const StdAwk::char_t* getOutput () { return this->output.c_str(); }
protected:
String input; // console input buffer
const StdAwk::char_t* inptr;
const StdAwk::char_t* inend;
String output; // console output buffer
int open (StdAwk::Console& io)
{
if (io.getMode() == StdAwk::Console::READ)
{
this->inptr = this->input.c_str();
this->inend = inptr + this->input.length();
}
else
{
this->output.clear ();
}
return 1; // return open-success
}
int close (StdAwk::Console& io)
{
return 0; // return success
}
int flush (StdAwk::Console& io)
{
// there is nothing to flush since a string buffer
// is used for a console output. just return success.
return 0;
}
int next (StdAwk::Console& io)
{
// this stripped-down awk doesn't honor the nextfile statement
// or the nextofile statement. just return failure.
return -1;
}
ssize_t read (StdAwk::Console& io, StdAwk::char_t* data, size_t size)
{
if (this->inptr >= this->inend) return 0; // EOF
size_t x = qse_strxncpy (data, size, inptr, inend - inptr);
this->inptr += x;
return x;
}
ssize_t write (StdAwk::Console& io, const StdAwk::char_t* data, size_t size)
{
try { this->output.append (data, size); }
catch (...)
{
((StdAwk::Run*)io)->setError (QSE_AWK_ENOMEM);
return -1;
}
return size;
}
};
static void print_error (
const StdAwk::loc_t& loc, const StdAwk::char_t* msg)
{
if (loc.line > 0 || loc.colm > 0)
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s at LINE %lu COLUMN %lu\n"), msg, loc.line, loc.colm);
else
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), msg);
}
static int run_awk (StdAwk& awk)
{
// sample input string
const qse_char_t* instr = QSE_T(
"aardvark 555-5553 1200/300 B\n"
"alpo-net 555-3412 2400/1200/300 A\n"
"barfly 555-7685 1200/300 A\n"
"bites 555-1675 2400/1200/300 A\n"
"camelot 555-0542 300 C\n"
"core 555-2912 1200/300 C\n"
"fooey 555-1234 2400/1200/300 B\n"
"foot 555-6699 1200/300 B\n"
"macfoo 555-6480 1200/300 A\n"
"sdace 555-3430 2400/1200/300 A\n"
"sabafoo 555-2127 1200/300 C\n");
const qse_char_t* instr2 = QSE_T(
"aardvark 555-5553 1200/300 A\n"
"alpo-net 555-3412 2400/1200/300 B\n"
"barfly 555-7685 1200/300 C\n"
"bites 555-1675 2400/1200/300 A\n"
"camelot 555-0542 300 C\n"
"core 555-2912 1200/300 B\n"
"fooey 555-1234 2400/1200/300 A\n"
"foot 555-6699 1200/300 A\n"
"macfoo 555-6480 1200/300 B\n"
"sdace 555-3430 2400/1200/300 B\n"
"sabafoo 555-2127 1200/300 A\n");
// ARGV[0]
if (awk.addArgument (QSE_T("awk14")) <= -1) return -1;
// prepare a string to print lines with A in the fourth column
StdAwk::SourceString in (QSE_T("$4 == \"A\" { print $2, $1, $3; }"));
// parse the script.
if (awk.parse (in, StdAwk::Source::NONE) == QSE_NULL) return -1;
StdAwk::Value r;
MyConsoleHandler* mch = (MyConsoleHandler*)awk.getConsoleHandler();
mch->setInput (instr); // locate the input string
int x = awk.loop (&r); // execute the BEGIN, pattern-action, END blocks.
if (x >= 0)
{
qse_printf (QSE_T("%s"), mch->getOutput()); // print the console output
qse_printf (QSE_T("-----------------------------\n"));
mch->setInput (instr2);
// reset the runtime context so that the next loop() method
// is performed over a new console stream.
if (awk.resetRunContext() == QSE_NULL) return -1;
int x = awk.loop (&r);
if (x >= 0)
{
qse_printf (QSE_T("%s"), mch->getOutput());
qse_printf (QSE_T("-----------------------------\n"));
}
}
return x;
}
static int awk_main (int argc, qse_char_t* argv[])
{
MyConsoleHandler mch;
StdAwk awk;
int ret = awk.open ();
if (ret >= 0)
{
awk.setConsoleHandler (&mch);
ret = run_awk (awk);
}
if (ret <= -1)
{
StdAwk::loc_t loc = awk.getErrorLocation();
print_error (loc, awk.getErrorMessage());
}
awk.close ();
return ret;
}
int qse_main (int argc, qse_achar_t* argv[])
{
#if defined(_WIN32)
char locale[100];
UINT codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/
qse_setdflcmgr (qse_utf8cmgr);
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
qse_setdflcmgr (qse_slmbcmgr);
}
#else
setlocale (LC_ALL, "");
qse_setdflcmgr (qse_slmbcmgr);
#endif
return qse_runmain (argc,argv,awk_main);
}