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:
2012-06-06 14:41:21 +00:00
parent c64efd8ae7
commit 4223736c14
10 changed files with 631 additions and 86 deletions

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);
}