- Changed the way Mmgr is used. A subclass inheriting Mmged is instantiated with a pointer to Mmgr which used to be the parent class.
- Separated the I/O stream handler from the Sed class and abstracted it into Sed::IOStream. - Implemented StdSed::StdStream.
This commit is contained in:
parent
232c0cc1c4
commit
de7082d0d0
@ -23,7 +23,7 @@ Embedding an interpreter typically involves the following steps.
|
||||
- open a new interpreter
|
||||
- parse in a source script
|
||||
- open a new runtime context
|
||||
- execute pattern-action blocks or call a function
|
||||
- execute BEGIN,pattern-action,END blocks or call a function
|
||||
- close the runtime context
|
||||
- close the interpter
|
||||
|
||||
@ -62,7 +62,8 @@ int main ()
|
||||
);
|
||||
if (!rtx) FAIL (qse_awk_geterrmsg(awk));
|
||||
|
||||
retv = qse_awk_rtx_loop (rtx); /* exeucte pattern-action blocks */
|
||||
/* exeucte BEGIN,pattern-action,END blocks */
|
||||
retv = qse_awk_rtx_loop (rtx);
|
||||
if (!retv) FAIL (qse_awk_rtx_geterrmsg(rtx));
|
||||
|
||||
qse_awk_rtx_refdownval (rtx, retv); /* destroy the return value */
|
||||
@ -75,11 +76,11 @@ oops:
|
||||
}
|
||||
@endcode
|
||||
|
||||
Things can get simpler when you use C++ API as the C++ API supports a single
|
||||
runtime context for each interpreter.
|
||||
Things can get simpler when you use C++ API. Note that the C++ API supports
|
||||
just a single runtime context for each interpreter.
|
||||
|
||||
@code
|
||||
/* c++ -o hello hello.cpp -lqsecmn -lqseawk -lqseawk++ */
|
||||
/* c++ -o hello hello.cpp -lqsecmn -lqseawk -lqseawkxx */
|
||||
#include <qse/awk/StdAwk.hpp>
|
||||
#include <iostream>
|
||||
|
||||
@ -110,7 +111,7 @@ int main (int argc, char* argv[])
|
||||
MyAwk::SourceString in(QSE_T("BEGIN { print \"hello, world\" }"));
|
||||
if (awk.parse (in, MyAwk::Source::NONE) == QSE_NULL) FAIL (awk);
|
||||
|
||||
// execute the BEGIN, pattern-action, END blocks.
|
||||
// execute BEGIN, pattern-action, END blocks.
|
||||
MyAwk::Value r;
|
||||
if (awk.loop (&r) <= -1) FAIL (awk);
|
||||
|
||||
|
@ -93,8 +93,8 @@ organized to dedicated modules. See relevant subpages for more information
|
||||
on each module.
|
||||
|
||||
- @subpage cmn "Common Functions"
|
||||
- @subpage sed "SED Stream Editor"
|
||||
- @subpage cut "CUT Text Cutter"
|
||||
- @subpage awk "AWK Interpreter"
|
||||
- @subpage cut "CUT Text Cutter"
|
||||
- @subpage sed "SED Stream Editor"
|
||||
|
||||
*/
|
||||
|
@ -13,7 +13,7 @@ pattern space, manipulates the pattern space by applying a set of editing
|
||||
commands, and writes the pattern space to an output stream. Typically, the
|
||||
input and output streams are a console or a file.
|
||||
|
||||
@b QSE provides an embeddable stream editor that supports most of
|
||||
@b QSESED is an embeddable stream editor that supports most of
|
||||
the conventional sed commands and implements input and output streams as a
|
||||
callback function:
|
||||
|
||||
@ -21,6 +21,62 @@ callback function:
|
||||
- QSE::Sed - C++ class that wraps #qse_sed_t
|
||||
- QSE::StdSed - C++ child class of QSE::Sed that implements standard input and output streams
|
||||
|
||||
@code
|
||||
#include <qse/sed/StdSed.hpp>
|
||||
#include <qse/cmn/main.h>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef QSE_CHAR_IS_MCHAR
|
||||
# define xcout std::cout
|
||||
#else
|
||||
# define xcout std::wcout
|
||||
#endif
|
||||
|
||||
int sed_main (int argc, qse_char_t* argv[])
|
||||
{
|
||||
if (argc < 2 || argc > 4)
|
||||
{
|
||||
xcout << QSE_T("USAGE: ") << argv[0] <<
|
||||
QSE_T(" command-string [input-file [output-file]]") << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
QSE::StdSed sed;
|
||||
|
||||
if (sed.open () == -1)
|
||||
{
|
||||
xcout << QSE_T("ERR: cannot open - ") << sed.getErrorMessage() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sed.compile (argv[1]) == -1)
|
||||
{
|
||||
xcout << QSE_T("ERR: cannot compile - ") << sed.getErrorMessage() << std::endl;
|
||||
sed.close ();
|
||||
return -1;
|
||||
}
|
||||
|
||||
qse_char_t* infile = (argc >= 3)? argv[2]: QSE_NULL;
|
||||
qse_char_t* outfile = (argc >= 4)? argv[3]: QSE_NULL;
|
||||
QSE::StdSed::StdStream stream (infile, outfile);
|
||||
|
||||
if (sed.execute (stream) == -1)
|
||||
{
|
||||
xcout << QSE_T("ERR: cannot execute - ") << sed.getErrorMessage() << std::endl;
|
||||
sed.close ();
|
||||
return -1;
|
||||
}
|
||||
|
||||
sed.close ();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int qse_main (int argc, qse_achar_t* argv[])
|
||||
{
|
||||
return qse_runmain (argc, argv, sed_main);
|
||||
}
|
||||
@endcode
|
||||
|
||||
@section sed_command COMMAND
|
||||
|
||||
A sed command is composed of:
|
||||
|
@ -6,7 +6,7 @@ pkgincludedir = $(includedir)/qse
|
||||
pkginclude_HEADERS = config.h conf_msw.h conf_vms.h types.h macros.h pack1.h unpack.h
|
||||
|
||||
if ENABLE_CXX
|
||||
pkginclude_HEADERS += Types.hpp Mmgr.hpp
|
||||
pkginclude_HEADERS += Types.hpp
|
||||
endif
|
||||
|
||||
install-data-hook:
|
||||
|
@ -33,7 +33,7 @@ PRE_UNINSTALL = :
|
||||
POST_UNINSTALL = :
|
||||
build_triplet = @build@
|
||||
host_triplet = @host@
|
||||
@ENABLE_CXX_TRUE@am__append_1 = Types.hpp Mmgr.hpp
|
||||
@ENABLE_CXX_TRUE@am__append_1 = Types.hpp
|
||||
subdir = include/qse
|
||||
DIST_COMMON = $(am__pkginclude_HEADERS_DIST) $(srcdir)/Makefile.am \
|
||||
$(srcdir)/Makefile.in $(srcdir)/config.h.in
|
||||
@ -59,7 +59,7 @@ RECURSIVE_TARGETS = all-recursive check-recursive dvi-recursive \
|
||||
installcheck-recursive installdirs-recursive pdf-recursive \
|
||||
ps-recursive uninstall-recursive
|
||||
am__pkginclude_HEADERS_DIST = config.h conf_msw.h conf_vms.h types.h \
|
||||
macros.h pack1.h unpack.h Types.hpp Mmgr.hpp
|
||||
macros.h pack1.h unpack.h Types.hpp
|
||||
am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
|
||||
am__vpath_adj = case $$p in \
|
||||
$(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \
|
||||
|
@ -1,119 +0,0 @@
|
||||
/*
|
||||
* $Id: Sed.hpp 127 2009-05-07 13:15:04Z baconevi $
|
||||
*
|
||||
Copyright 2006-2009 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/>.
|
||||
*/
|
||||
|
||||
#ifndef _QSE_MMGR_HPP_
|
||||
#define _QSE_MMGR_HPP_
|
||||
|
||||
#include <qse/Types.hpp>
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
/**
|
||||
* The Mmgr class defines a memory manager interface that can be inherited
|
||||
* by a class in need of a memory manager as defined in the primitive
|
||||
* qse_mmgr_t type. Using the class over the primitive type enables you to
|
||||
* write code in more object-oriented fashion. An inheriting class should
|
||||
* implement three pure virtual functions.
|
||||
*/
|
||||
class Mmgr: public Types, public qse_mmgr_t
|
||||
{
|
||||
public:
|
||||
/** defines an alias type to qse_mmgr_t */
|
||||
typedef qse_mmgr_t mmgr_t;
|
||||
|
||||
/**
|
||||
* The Mmgr() function builds a memory manager composed of bridge
|
||||
* functions connecting itself with it.
|
||||
*/
|
||||
Mmgr ()
|
||||
{
|
||||
this->alloc = alloc_mem;
|
||||
this->realloc = realloc_mem;
|
||||
this->free = free_mem;
|
||||
this->udd = this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The ~Mmgr() function finalizes a memory manager.
|
||||
*/
|
||||
virtual ~Mmgr () {}
|
||||
|
||||
protected:
|
||||
/**
|
||||
* The allocMem() function allocates a chunk of memory of the
|
||||
* size \a n and return the pointer to the beginning of the chunk.
|
||||
* If it fails to allocate memory, it should return QSE_NULL.
|
||||
*/
|
||||
virtual void* allocMem (
|
||||
qse_size_t n /**< the size of allocate in bytes */
|
||||
) = 0;
|
||||
|
||||
/**
|
||||
* The reallocMem() function resizes a chunk of memory previously
|
||||
* allocated with the allocMem() function. When resized, the contents
|
||||
* of the surviving part of a memory chunk is preserved. If it fails to
|
||||
* resize memory, it should return QSE_NULL.
|
||||
*/
|
||||
virtual void* reallocMem (
|
||||
void* ptr, /**< a pointer to a memory chunk to resize */
|
||||
qse_size_t n /**< new size in bytes */
|
||||
) = 0;
|
||||
|
||||
/**
|
||||
* The freeMem() function frees a chunk of memory allocated with
|
||||
* the allocMem() function or resized with the reallocMem() function.
|
||||
*/
|
||||
virtual void freeMem (
|
||||
void* ptr /**< a pointer to a memory chunk to free */
|
||||
) = 0;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* a bridge function from the qse_mmgr_t type the allocMem() function.
|
||||
*/
|
||||
static void* alloc_mem (void* udd, qse_size_t n)
|
||||
{
|
||||
return ((Mmgr*)udd)->allocMem (n);
|
||||
}
|
||||
|
||||
/**
|
||||
* a bridge function from the qse_mmgr_t type the reallocMem() function.
|
||||
*/
|
||||
static void* realloc_mem (void* udd, void* ptr, qse_size_t n)
|
||||
{
|
||||
return ((Mmgr*)udd)->reallocMem (ptr, n);
|
||||
}
|
||||
|
||||
/**
|
||||
* a bridge function from the qse_mmgr_t type the freeMem() function.
|
||||
*/
|
||||
static void free_mem (void* udd, void* ptr)
|
||||
{
|
||||
return ((Mmgr*)udd)->freeMem (ptr);
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_END_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
#endif
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: Awk.hpp 288 2009-09-15 14:03:15Z hyunghwan.chung $
|
||||
* $Id: Awk.hpp 318 2009-12-18 12:34:42Z hyunghwan.chung $
|
||||
*
|
||||
Copyright 2006-2009 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
@ -24,7 +24,7 @@
|
||||
#include <qse/awk/awk.h>
|
||||
#include <qse/cmn/map.h>
|
||||
#include <qse/cmn/chr.h>
|
||||
#include <qse/Mmgr.hpp>
|
||||
#include <qse/cmn/Mmged.hpp>
|
||||
#include <stdarg.h>
|
||||
|
||||
/// @file
|
||||
@ -39,7 +39,7 @@ QSE_BEGIN_NAMESPACE(QSE)
|
||||
/// The Awk class implements an AWK interpreter by wrapping around
|
||||
/// #qse_awk_t and #qse_awk_rtx_t.
|
||||
///
|
||||
class Awk: public Mmgr
|
||||
class Awk: public Mmged
|
||||
{
|
||||
public:
|
||||
typedef qse_map_t map_t;
|
||||
@ -180,7 +180,7 @@ public:
|
||||
/// The Data class encapsulates information passed in and out
|
||||
/// for source script I/O.
|
||||
///
|
||||
class Data: protected sio_arg_t
|
||||
class Data
|
||||
{
|
||||
public:
|
||||
friend class Awk;
|
||||
@ -202,12 +202,12 @@ public:
|
||||
return arg->name;
|
||||
}
|
||||
|
||||
const void* getHandle () const
|
||||
void* getHandle () const
|
||||
{
|
||||
return arg->handle;
|
||||
}
|
||||
|
||||
void setHandle (void* handle)
|
||||
void setHandle (void* handle)
|
||||
{
|
||||
arg->handle = handle;
|
||||
}
|
||||
@ -234,7 +234,7 @@ public:
|
||||
virtual int open (Data& io) = 0;
|
||||
virtual int close (Data& io) = 0;
|
||||
virtual ssize_t read (Data& io, char_t* buf, size_t len) = 0;
|
||||
virtual ssize_t write (Data& io, char_t* buf, size_t len) = 0;
|
||||
virtual ssize_t write (Data& io, const char_t* buf, size_t len) = 0;
|
||||
|
||||
///
|
||||
/// The NONE object indicates no source.
|
||||
@ -253,7 +253,7 @@ protected:
|
||||
int open (Data& io) { return -1; }
|
||||
int close (Data& io) { return 0; }
|
||||
ssize_t read (Data& io, char_t* buf, size_t len) { return 0; }
|
||||
ssize_t write (Data& io, char_t* buf, size_t len) { return 0; }
|
||||
ssize_t write (Data& io, const char_t* buf, size_t len) { return 0; }
|
||||
};
|
||||
|
||||
public:
|
||||
@ -774,8 +774,14 @@ public:
|
||||
/// @{
|
||||
///
|
||||
|
||||
/// Constructor
|
||||
Awk ();
|
||||
/// The Awk() function creates an interpreter without fully
|
||||
/// initializing it. You must call open() for full initialization
|
||||
/// before calling other functions.
|
||||
Awk (Mmgr* mmgr);
|
||||
|
||||
/// The ~Awk() function destroys an interpreter. Make sure to have
|
||||
/// called close() for finalization before the destructor is executed.
|
||||
virtual ~Awk () {}
|
||||
|
||||
///
|
||||
/// The open() function initializes an interpreter.
|
||||
@ -785,7 +791,7 @@ public:
|
||||
int open ();
|
||||
|
||||
///
|
||||
/// The cloase() function closes the interpreter.
|
||||
/// The close() function closes the interpreter.
|
||||
///
|
||||
void close ();
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: StdAwk.hpp 287 2009-09-15 10:01:02Z hyunghwan.chung $
|
||||
* $Id: StdAwk.hpp 318 2009-12-18 12:34:42Z hyunghwan.chung $
|
||||
*
|
||||
Copyright 2006-2009 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
@ -22,6 +22,7 @@
|
||||
#define _QSE_AWK_STDAWK_HPP_
|
||||
|
||||
#include <qse/awk/Awk.hpp>
|
||||
#include <qse/cmn/StdMmgr.hpp>
|
||||
|
||||
/// @file
|
||||
/// Standard AWK Interpreter
|
||||
@ -65,7 +66,7 @@ public:
|
||||
int open (Data& io);
|
||||
int close (Data& io);
|
||||
ssize_t read (Data& io, char_t* buf, size_t len);
|
||||
ssize_t write (Data& io, char_t* buf, size_t len);
|
||||
ssize_t write (Data& io, const char_t* buf, size_t len);
|
||||
|
||||
protected:
|
||||
const char_t* name;
|
||||
@ -84,13 +85,15 @@ public:
|
||||
int open (Data& io);
|
||||
int close (Data& io);
|
||||
ssize_t read (Data& io, char_t* buf, size_t len);
|
||||
ssize_t write (Data& io, char_t* buf, size_t len);
|
||||
ssize_t write (Data& io, const char_t* buf, size_t len);
|
||||
|
||||
protected:
|
||||
const char_t* str;
|
||||
const char_t* ptr;
|
||||
};
|
||||
|
||||
StdAwk (Mmgr* mmgr = &StdMmgr::DFL): Awk (mmgr) {}
|
||||
|
||||
int open ();
|
||||
void close ();
|
||||
|
||||
|
@ -5,7 +5,7 @@ pkginclude_HEADERS = \
|
||||
rex.h sll.h dll.h opt.h tio.h \
|
||||
fio.h pio.h sio.h time.h misc.h main.h stdio.h
|
||||
|
||||
#if ENABLE_CXX
|
||||
#pkginclude_HEADERS +=
|
||||
#endif
|
||||
if ENABLE_CXX
|
||||
pkginclude_HEADERS += Mmgr.hpp StdMmgr.hpp Mmged.hpp
|
||||
endif
|
||||
|
||||
|
@ -33,8 +33,9 @@ PRE_UNINSTALL = :
|
||||
POST_UNINSTALL = :
|
||||
build_triplet = @build@
|
||||
host_triplet = @host@
|
||||
@ENABLE_CXX_TRUE@am__append_1 = Mmgr.hpp StdMmgr.hpp Mmged.hpp
|
||||
subdir = include/qse/cmn
|
||||
DIST_COMMON = $(pkginclude_HEADERS) $(srcdir)/Makefile.am \
|
||||
DIST_COMMON = $(am__pkginclude_HEADERS_DIST) $(srcdir)/Makefile.am \
|
||||
$(srcdir)/Makefile.in
|
||||
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
|
||||
am__aclocal_m4_deps = $(top_srcdir)/ac/m4/libtool.m4 \
|
||||
@ -50,6 +51,9 @@ CONFIG_CLEAN_FILES =
|
||||
CONFIG_CLEAN_VPATH_FILES =
|
||||
SOURCES =
|
||||
DIST_SOURCES =
|
||||
am__pkginclude_HEADERS_DIST = mem.h chr.h str.h lda.h map.h rex.h \
|
||||
sll.h dll.h opt.h tio.h fio.h pio.h sio.h time.h misc.h main.h \
|
||||
stdio.h Mmgr.hpp StdMmgr.hpp Mmged.hpp
|
||||
am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
|
||||
am__vpath_adj = case $$p in \
|
||||
$(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \
|
||||
@ -216,11 +220,9 @@ target_alias = @target_alias@
|
||||
top_build_prefix = @top_build_prefix@
|
||||
top_builddir = @top_builddir@
|
||||
top_srcdir = @top_srcdir@
|
||||
pkginclude_HEADERS = \
|
||||
mem.h chr.h str.h lda.h map.h \
|
||||
rex.h sll.h dll.h opt.h tio.h \
|
||||
fio.h pio.h sio.h time.h misc.h main.h stdio.h
|
||||
|
||||
pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h \
|
||||
opt.h tio.h fio.h pio.h sio.h time.h misc.h main.h stdio.h \
|
||||
$(am__append_1)
|
||||
all: all-am
|
||||
|
||||
.SUFFIXES:
|
||||
@ -477,10 +479,6 @@ uninstall-am: uninstall-pkgincludeHEADERS
|
||||
tags uninstall uninstall-am uninstall-pkgincludeHEADERS
|
||||
|
||||
|
||||
#if ENABLE_CXX
|
||||
#pkginclude_HEADERS +=
|
||||
#endif
|
||||
|
||||
# Tell versions [3.59,3.63) of GNU make to not export all variables.
|
||||
# Otherwise a system limit (for SysV at least) may be exceeded.
|
||||
.NOEXPORT:
|
||||
|
54
qse/include/qse/cmn/Mmged.hpp
Normal file
54
qse/include/qse/cmn/Mmged.hpp
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* $Id: Sed.hpp 127 2009-05-07 13:15:04Z baconevi $
|
||||
*
|
||||
Copyright 2006-2009 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/>.
|
||||
*/
|
||||
|
||||
#ifndef _QSE_CMN_MMGED_HPP_
|
||||
#define _QSE_CMN_MMGED_HPP_
|
||||
|
||||
#include <qse/Types.hpp>
|
||||
#include <qse/cmn/Mmgr.hpp>
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
///
|
||||
/// The Mmged class defines a memory manager interface to be inherited by
|
||||
/// a subclass that uses a memory manager.
|
||||
///
|
||||
|
||||
class Mmged: public Types
|
||||
{
|
||||
public:
|
||||
Mmged (Mmgr* mmgr): mmgr (mmgr) {}
|
||||
|
||||
///
|
||||
/// The getMmgr() function returns the memory manager associated.
|
||||
///
|
||||
Mmgr* getMmgr () const { return mmgr; }
|
||||
|
||||
protected:
|
||||
Mmgr* mmgr;
|
||||
};
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_END_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
#endif
|
110
qse/include/qse/cmn/Mmgr.hpp
Normal file
110
qse/include/qse/cmn/Mmgr.hpp
Normal file
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* $Id: Sed.hpp 127 2009-05-07 13:15:04Z baconevi $
|
||||
*
|
||||
Copyright 2006-2009 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/>.
|
||||
*/
|
||||
|
||||
#ifndef _QSE_CMN_MMGR_HPP_
|
||||
#define _QSE_CMN_MMGR_HPP_
|
||||
|
||||
#include <qse/Types.hpp>
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
///
|
||||
/// The Mmgr class defines a memory manager interface that can be inherited
|
||||
/// by a class in need of a memory manager as defined in the primitive
|
||||
/// #qse_mmgr_t type. Using the class over the primitive type enables you to
|
||||
/// write code in more object-oriented fashion. An inheriting class should
|
||||
/// implement three pure virtual functions.
|
||||
///
|
||||
class Mmgr: public Types, public qse_mmgr_t
|
||||
{
|
||||
public:
|
||||
/// defines an alias type to #qse_mmgr_t
|
||||
typedef qse_mmgr_t mmgr_t;
|
||||
|
||||
///
|
||||
/// The Mmgr() function builds a memory manager composed of bridge
|
||||
/// functions connecting itself with it.
|
||||
///
|
||||
Mmgr ()
|
||||
{
|
||||
this->alloc = alloc_mem;
|
||||
this->realloc = realloc_mem;
|
||||
this->free = free_mem;
|
||||
this->udd = this;
|
||||
}
|
||||
|
||||
///
|
||||
/// The ~Mmgr() function finalizes a memory manager.
|
||||
///
|
||||
virtual ~Mmgr () {}
|
||||
|
||||
protected:
|
||||
///
|
||||
/// The allocMem() function allocates a chunk of memory of the
|
||||
/// size @a n and return the pointer to the beginning of the chunk.
|
||||
/// If it fails to allocate memory, it should return QSE_NULL.
|
||||
///
|
||||
virtual void* allocMem (
|
||||
size_t n ///< size of memory chunk to allocate in bytes
|
||||
) = 0;
|
||||
|
||||
///
|
||||
/// The reallocMem() function resizes a chunk of memory previously
|
||||
/// allocated with the allocMem() function. When resized, the contents
|
||||
/// of the surviving part of a memory chunk is preserved. If it fails to
|
||||
/// resize memory, it should return QSE_NULL.
|
||||
///
|
||||
virtual void* reallocMem (
|
||||
void* ptr, ///< pointer to memory chunk to resize
|
||||
size_t n ///< new size in bytes
|
||||
) = 0;
|
||||
|
||||
///
|
||||
/// The freeMem() function frees a chunk of memory allocated with
|
||||
/// the allocMem() function or resized with the reallocMem() function.
|
||||
///
|
||||
virtual void freeMem (
|
||||
void* ptr ///< pointer to memory chunk to free
|
||||
) = 0;
|
||||
|
||||
protected:
|
||||
///
|
||||
/// bridge function from the #qse_mmgr_t type the allocMem() function.
|
||||
///
|
||||
static void* alloc_mem (void* udd, size_t n);
|
||||
|
||||
///
|
||||
/// bridge function from the #qse_mmgr_t type the reallocMem() function.
|
||||
///
|
||||
static void* realloc_mem (void* udd, void* ptr, size_t n);
|
||||
|
||||
///
|
||||
/// bridge function from the #qse_mmgr_t type the freeMem() function.
|
||||
///
|
||||
static void free_mem (void* udd, void* ptr);
|
||||
};
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_END_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
#endif
|
45
qse/include/qse/cmn/StdMmgr.hpp
Normal file
45
qse/include/qse/cmn/StdMmgr.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* $Id: Sed.hpp 127 2009-05-07 13:15:04Z baconevi $
|
||||
*
|
||||
Copyright 2006-2009 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/>.
|
||||
*/
|
||||
|
||||
#ifndef _QSE_CMN_STDMMGR_HPP_
|
||||
#define _QSE_CMN_STDMMGR_HPP_
|
||||
|
||||
#include <qse/cmn/Mmgr.hpp>
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
class StdMmgr: public Mmgr
|
||||
{
|
||||
public:
|
||||
void* allocMem (size_t n);
|
||||
void* reallocMem (void* ptr, size_t n);
|
||||
virtual void freeMem (void* ptr);
|
||||
|
||||
public:
|
||||
static StdMmgr DFL;
|
||||
};
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_END_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
#endif
|
@ -30,10 +30,18 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* The #qse_main macro defines a main function wrapper for an underlying
|
||||
* platform. Combined with the qse_runmain() function, it provides a consistant
|
||||
* view to the main function.
|
||||
* @def qse_main
|
||||
* The qse_main macro defines a main function wrapper for an underlying
|
||||
* platform. It is defined to @b main or @b wmain depending on the choice of
|
||||
* the default character type #qse_char_t. Combined with the qse_runmain()
|
||||
* function, it provides a consistant view to the main function.
|
||||
*
|
||||
* @typedef qse_achar_t
|
||||
* The qse_achar_t type defines a character type for the second parameter to
|
||||
* #qse_main. It is defined to #qse_mchar_t or #qse_wchar_t depending on the
|
||||
* choice of the default character type #qse_char_t.
|
||||
*/
|
||||
|
||||
#if defined(_WIN32) && !defined(__MINGW32__)
|
||||
# if defined(QSE_CHAR_IS_MCHAR)
|
||||
# define qse_main main
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: sio.h 287 2009-09-15 10:01:02Z hyunghwan.chung $
|
||||
* $Id: sio.h 318 2009-12-18 12:34:42Z hyunghwan.chung $
|
||||
*
|
||||
Copyright 2006-2009 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
@ -21,6 +21,10 @@
|
||||
#ifndef _QSE_CMN_SIO_H_
|
||||
#define _QSE_CMN_SIO_H_
|
||||
|
||||
/** @file
|
||||
* This file defines a simple stream I/O interface.
|
||||
*/
|
||||
|
||||
#include <qse/types.h>
|
||||
#include <qse/macros.h>
|
||||
#include <qse/cmn/fio.h>
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: Sed.hpp 287 2009-09-15 10:01:02Z hyunghwan.chung $
|
||||
* $Id: Sed.hpp 318 2009-12-18 12:34:42Z hyunghwan.chung $
|
||||
*
|
||||
Copyright 2006-2009 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
@ -21,7 +21,7 @@
|
||||
#ifndef _QSE_SED_SED_HPP_
|
||||
#define _QSE_SED_SED_HPP_
|
||||
|
||||
#include <qse/Mmgr.hpp>
|
||||
#include <qse/cmn/Mmged.hpp>
|
||||
#include <qse/sed/sed.h>
|
||||
|
||||
/** @file
|
||||
@ -35,7 +35,7 @@ QSE_BEGIN_NAMESPACE(QSE)
|
||||
/**
|
||||
* The Sed class implements a stream editor by wrapping around #qse_sed_t.
|
||||
*/
|
||||
class Sed: public Mmgr
|
||||
class Sed: public Mmged
|
||||
{
|
||||
public:
|
||||
/// The sed_t type redefines a stream editor type
|
||||
@ -55,10 +55,84 @@ public:
|
||||
/// The depth_t type redefines an depth IDs
|
||||
typedef qse_sed_depth_t depth_t;
|
||||
|
||||
///
|
||||
/// The IOStream class is a base class for I/O operation during
|
||||
/// execution.
|
||||
///
|
||||
class IOStream: public Types
|
||||
{
|
||||
public:
|
||||
enum Mode
|
||||
{
|
||||
READ, ///< open for read
|
||||
WRITE ///< open for write
|
||||
};
|
||||
|
||||
class Data
|
||||
{
|
||||
public:
|
||||
friend class Sed;
|
||||
|
||||
protected:
|
||||
Data (Sed* sed, Mode mode, io_arg_t* arg):
|
||||
sed (sed), mode (mode), arg (arg)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
Mode getMode() const
|
||||
{
|
||||
return mode;
|
||||
}
|
||||
|
||||
void* getHandle () const
|
||||
{
|
||||
return arg->handle;
|
||||
}
|
||||
|
||||
void setHandle (void* handle)
|
||||
{
|
||||
arg->handle = handle;
|
||||
}
|
||||
|
||||
const char_t* getName () const
|
||||
{
|
||||
return arg->path;
|
||||
}
|
||||
|
||||
operator Sed* () const
|
||||
{
|
||||
return sed;
|
||||
}
|
||||
|
||||
operator sed_t* () const
|
||||
{
|
||||
return sed->sed;
|
||||
}
|
||||
|
||||
protected:
|
||||
Sed* sed;
|
||||
Mode mode;
|
||||
io_arg_t* arg;
|
||||
};
|
||||
|
||||
IOStream () {}
|
||||
virtual ~IOStream () {}
|
||||
|
||||
virtual int open (Data& io) = 0;
|
||||
virtual int close (Data& io) = 0;
|
||||
virtual ssize_t read (Data& io, char_t* buf, size_t len) = 0;
|
||||
virtual ssize_t write (Data& io, const char_t* buf, size_t len) = 0;
|
||||
|
||||
private:
|
||||
IOStream (const IOStream&);
|
||||
IOStream& operator= (const IOStream&);
|
||||
};
|
||||
|
||||
///
|
||||
/// The Sed() function creates an uninitialized stream editor.
|
||||
///
|
||||
Sed (): sed (QSE_NULL), dflerrstr (QSE_NULL) {}
|
||||
Sed (Mmgr* mmgr): Mmged (mmgr), sed (QSE_NULL), dflerrstr (QSE_NULL) {}
|
||||
|
||||
///
|
||||
/// The ~Sed() function destroys a stream editor.
|
||||
@ -67,7 +141,7 @@ public:
|
||||
/// a stream editor is destroyed if it has been initialized
|
||||
/// with open().
|
||||
///
|
||||
~Sed () {}
|
||||
virtual ~Sed () {}
|
||||
|
||||
///
|
||||
/// The open() function initializes a stream editor and makes it
|
||||
@ -105,7 +179,7 @@ public:
|
||||
/// streams defined through I/O handlers
|
||||
/// @return 0 on success, -1 on failure
|
||||
///
|
||||
int execute ();
|
||||
int execute (IOStream& iostream);
|
||||
|
||||
///
|
||||
/// The getOption() function gets the current options.
|
||||
@ -184,210 +258,11 @@ public:
|
||||
size_t num ///< a line number
|
||||
);
|
||||
|
||||
///
|
||||
/// The getMmgr() function returns the memory manager associated.
|
||||
///
|
||||
|
||||
protected:
|
||||
///
|
||||
/// The IOBase class is a base class for I/O operations. It wraps around
|
||||
/// the primitive #io_arg_t type and exposes relevant information to
|
||||
/// an I/O handler.
|
||||
///
|
||||
class IOBase
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* The Mode enumerator defines I/O operation modes.
|
||||
*/
|
||||
enum Mode
|
||||
{
|
||||
READ, ///< open a stream for reading
|
||||
WRITE ///< open a stream for writing
|
||||
};
|
||||
|
||||
protected:
|
||||
IOBase (io_arg_t* arg, Mode mode):
|
||||
arg(arg), mode (mode) {}
|
||||
|
||||
public:
|
||||
///
|
||||
/// The getHandle() function gets an I/O handle set with the
|
||||
/// setHandle() function. Once set, it is maintained until
|
||||
/// an assoicated I/O handler closes it or changes it with
|
||||
/// another call to setHandle().
|
||||
///
|
||||
const void* getHandle () const
|
||||
{
|
||||
return arg->handle;
|
||||
}
|
||||
|
||||
///
|
||||
/// The setHandle() function sets an I/O handle and is typically
|
||||
/// called in stream opening functions such as openConsole()
|
||||
/// and openFile(). You can get the handle with getHandle()
|
||||
// as needed.
|
||||
///
|
||||
void setHandle (void* handle)
|
||||
{
|
||||
arg->handle = handle;
|
||||
}
|
||||
|
||||
///
|
||||
/// The getMode() function gets the I/O mode requested.
|
||||
/// A stream opening function can inspect the mode requested and
|
||||
/// open a stream properly
|
||||
///
|
||||
Mode getMode ()
|
||||
{
|
||||
return this->mode;
|
||||
}
|
||||
|
||||
protected:
|
||||
Sed* sed;
|
||||
io_arg_t* arg;
|
||||
Mode mode;
|
||||
};
|
||||
|
||||
///
|
||||
/// The Console class inherits the IOBase class and provides
|
||||
/// functionality for console I/O operations.
|
||||
///
|
||||
class Console: public IOBase
|
||||
{
|
||||
protected:
|
||||
friend class Sed;
|
||||
Console (io_arg_t* arg, Mode mode):
|
||||
IOBase (arg, mode) {}
|
||||
};
|
||||
|
||||
///
|
||||
/// The File class inherits the IOBase class and provides functionality
|
||||
/// for file I/O operations.
|
||||
///
|
||||
class File: public IOBase
|
||||
{
|
||||
protected:
|
||||
friend class Sed;
|
||||
File (io_arg_t* arg, Mode mode):
|
||||
IOBase (arg, mode) {}
|
||||
|
||||
public:
|
||||
///
|
||||
/// The getName() function gets the file path requested.
|
||||
/// You can call this function from the openFile() function
|
||||
/// to determine a file to open.
|
||||
///
|
||||
const char_t* getName () const
|
||||
{
|
||||
return arg->path;
|
||||
}
|
||||
};
|
||||
|
||||
///
|
||||
/// The openConsole() function should be implemented by a subclass
|
||||
/// to open a console. It can get the mode requested by invoking
|
||||
/// the Console::getMode() function over the console object @a io.
|
||||
///
|
||||
/// When it comes to the meaning of the return value, 0 may look
|
||||
/// a bit tricky. Easygoers can just return 1 on success and never
|
||||
/// return 0 from openConsole().
|
||||
/// - If 0 is returned for a Console::READ console, the execute()
|
||||
/// function returns success after having calle closeConsole() as it
|
||||
/// has opened a console but has reached EOF.
|
||||
/// - If 0 is returned for a Console::WRITE console and there are any
|
||||
/// following writeConsole() requests, the execute() function
|
||||
/// returns failure after having called closeConsole() as it cannot
|
||||
/// write further on EOF.
|
||||
///
|
||||
/// @return -1 on failure, 1 on success, 0 on success but reached EOF.
|
||||
///
|
||||
virtual int openConsole (
|
||||
Console& io ///< a console object
|
||||
) = 0;
|
||||
|
||||
///
|
||||
/// The closeConsole() function should be implemented by a subclass
|
||||
/// to close a console.
|
||||
///
|
||||
virtual int closeConsole (
|
||||
Console& io ///< a console object
|
||||
) = 0;
|
||||
|
||||
///
|
||||
/// The readConsole() function should be implemented by a subclass
|
||||
/// to read from a console. It should fill the memory area pointed to
|
||||
/// by @a buf, but at most \a len characters.
|
||||
/// @return the number of characters read on success,
|
||||
/// 0 on EOF, -1 on failure
|
||||
///
|
||||
virtual ssize_t readConsole (
|
||||
Console& io, ///< a console object
|
||||
char_t* buf, ///< a buffer pointer
|
||||
size_t len ///< the size of a buffer
|
||||
) = 0;
|
||||
|
||||
///
|
||||
/// The writeConsole() function should be implemented by a subclass
|
||||
/// to write to a console. It should write up to @a len characters
|
||||
/// from the memory are pointed to by @a data.
|
||||
/// @return the number of characters written on success
|
||||
/// 0 on EOF, -1 on failure
|
||||
/// @note The number of characters written may be less than @a len.
|
||||
/// But the return value 0 causes execute() to fail as
|
||||
/// writeConsole() is called when there are data to write and
|
||||
/// it has indicated EOF.
|
||||
///
|
||||
virtual ssize_t writeConsole (
|
||||
Console& io, ///< a console object
|
||||
const char_t* data, ///< a pointer to data to write
|
||||
size_t len ///< the length of data
|
||||
) = 0;
|
||||
|
||||
///
|
||||
/// The openFile() function should be implemented by a subclass
|
||||
/// to open a file. It can get the mode requested by invoking
|
||||
/// the File::getMode() function over the file object @a io.
|
||||
/// @return -1 on failure, 1 on success, 0 on success but reached EOF.
|
||||
///
|
||||
virtual int openFile (
|
||||
File& io ///< a file object
|
||||
) = 0;
|
||||
|
||||
///
|
||||
/// The closeFile() function should be implemented by a subclass
|
||||
/// to close a file.
|
||||
///
|
||||
virtual int closeFile (
|
||||
File& io ///< a file object
|
||||
) = 0;
|
||||
|
||||
///
|
||||
/// The readFile() function should be implemented by a subclass
|
||||
/// to read from a file. It should fill the memory area pointed to
|
||||
/// by @a buf, but at most \a len characters.
|
||||
/// @return the number of characters read on success,
|
||||
/// 0 on EOF, -1 on failure
|
||||
///
|
||||
virtual ssize_t readFile (
|
||||
File& io, ///< a file object
|
||||
char_t* buf, ///< a buffer pointer
|
||||
size_t len ///< the size of a buffer
|
||||
) = 0;
|
||||
|
||||
///
|
||||
/// The writeFile() function should be implemented by a subclass
|
||||
/// to write to a file. It should write up to @a len characters
|
||||
/// from the memory are pointed to by @a data.
|
||||
/// @return the number of characters written on success
|
||||
/// 0 on EOF, -1 on failure
|
||||
/// @note The number of characters written may be less than @a len.
|
||||
/// But the return value 0 causes execute() to fail as
|
||||
/// writeFile() is called when there are data to write and
|
||||
/// it has indicated EOF.
|
||||
///
|
||||
virtual ssize_t writeFile (
|
||||
File& io, ///< a file object
|
||||
const char_t* data, ///< a pointer to data to write
|
||||
size_t len ///< the length of data
|
||||
) = 0;
|
||||
|
||||
///
|
||||
/// The getErrorString() function returns an error formatting string
|
||||
/// for the error number @a num. A subclass wishing to customize
|
||||
@ -402,6 +277,9 @@ protected:
|
||||
sed_t* sed;
|
||||
/// default error formatting string getter
|
||||
errstr_t dflerrstr;
|
||||
/// I/O stream to read data from and write output to.
|
||||
IOStream* iostream;
|
||||
|
||||
|
||||
private:
|
||||
static ssize_t xin (
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: StdSed.hpp 287 2009-09-15 10:01:02Z hyunghwan.chung $
|
||||
* $Id: StdSed.hpp 318 2009-12-18 12:34:42Z hyunghwan.chung $
|
||||
*
|
||||
Copyright 2006-2009 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
@ -22,6 +22,7 @@
|
||||
#define _QSE_SED_STDSED_HPP_
|
||||
|
||||
#include <qse/sed/Sed.hpp>
|
||||
#include <qse/cmn/StdMmgr.hpp>
|
||||
|
||||
/** @file
|
||||
* Standard Stream Editor
|
||||
@ -32,32 +33,40 @@ QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
/**
|
||||
* The StdSed class inherits the Sed class and implements the standard
|
||||
* I/O handlers and memory manager for easier use.
|
||||
* The StdSed class inherits the Sed class, implements a standard
|
||||
* I/O stream class, and sets the default memory manager.
|
||||
*
|
||||
*/
|
||||
class StdSed: public Sed
|
||||
{
|
||||
protected:
|
||||
void* allocMem (qse_size_t n);
|
||||
void* reallocMem (void* ptr, qse_size_t n);
|
||||
void freeMem (void* ptr);
|
||||
public:
|
||||
StdSed (Mmgr* mmgr = &StdMmgr::DFL): Sed (mmgr) {}
|
||||
|
||||
int openConsole (Console& io);
|
||||
int closeConsole (Console& io);
|
||||
ssize_t readConsole (Console& io, char_t* buf, size_t len);
|
||||
ssize_t writeConsole (Console& io, const char_t* data, size_t len);
|
||||
class StdStream: public IOStream
|
||||
{
|
||||
public:
|
||||
StdStream (const char_t* infile = QSE_NULL,
|
||||
const char_t* outfile = QSE_NULL):
|
||||
infile(infile), outfile(outfile)
|
||||
{
|
||||
}
|
||||
|
||||
int openFile (File& io);
|
||||
int closeFile (File& io);
|
||||
ssize_t readFile (File& io, char_t* buf, size_t len);
|
||||
ssize_t writeFile (File& io, const char_t* data, size_t len);
|
||||
int open (Data& io);
|
||||
int close (Data& io);
|
||||
ssize_t read (Data& io, char_t* buf, size_t len);
|
||||
ssize_t write (Data& io, const char_t* buf, size_t len);
|
||||
|
||||
protected:
|
||||
const char_t* infile;
|
||||
const char_t* outfile;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* @example sed02.cpp
|
||||
* The example shows how to use the QSE::StdSed class to write a simple stream
|
||||
* editor that reads from a standard input and writes to a standard output.
|
||||
* editor that reads from a standard input or a file and writes to a standard
|
||||
* output or a file.
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: Awk.cpp 288 2009-09-15 14:03:15Z hyunghwan.chung $
|
||||
* $Id: Awk.cpp 318 2009-12-18 12:34:42Z hyunghwan.chung $
|
||||
*
|
||||
Copyright 2006-2009 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
@ -1003,7 +1003,8 @@ int Awk::Run::getGlobal (int id, Value& g) const
|
||||
// Awk
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
Awk::Awk () : awk (QSE_NULL), functionMap (QSE_NULL), runctx (this)
|
||||
Awk::Awk (Mmgr* mmgr):
|
||||
Mmged (mmgr), awk (QSE_NULL), functionMap (QSE_NULL), runctx (this)
|
||||
|
||||
{
|
||||
QSE_MEMSET (&errinf, 0, QSE_SIZEOF(errinf));
|
||||
@ -1112,7 +1113,7 @@ int Awk::open ()
|
||||
prm.pow = pow;
|
||||
prm.sprintf = sprintf;
|
||||
|
||||
awk = qse_awk_open ((qse_mmgr_t*)this, QSE_SIZEOF(xtn_t), &prm);
|
||||
awk = qse_awk_open (this->getMmgr(), QSE_SIZEOF(xtn_t), &prm);
|
||||
if (awk == QSE_NULL)
|
||||
{
|
||||
setError (QSE_AWK_ENOMEM);
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: StdAwk.cpp 287 2009-09-15 10:01:02Z hyunghwan.chung $
|
||||
* $Id: StdAwk.cpp 318 2009-12-18 12:34:42Z hyunghwan.chung $
|
||||
*
|
||||
Copyright 2006-2009 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
@ -305,7 +305,7 @@ int StdAwk::openPipe (Pipe& io)
|
||||
}
|
||||
|
||||
pio = qse_pio_open (
|
||||
(qse_mmgr_t*)this,
|
||||
this->getMmgr(),
|
||||
0,
|
||||
io.getName(),
|
||||
flags
|
||||
@ -376,7 +376,7 @@ int StdAwk::openFile (File& io)
|
||||
}
|
||||
|
||||
fio = qse_fio_open (
|
||||
(qse_mmgr_t*)this,
|
||||
this->getMmgr(),
|
||||
0,
|
||||
io.getName(),
|
||||
flags,
|
||||
@ -801,6 +801,8 @@ int StdAwk::SourceFile::open (Data& io)
|
||||
|
||||
if (ioname == QSE_NULL)
|
||||
{
|
||||
// open the main source file.
|
||||
|
||||
if (name[0] == QSE_T('-') && name[1] == QSE_T('\0'))
|
||||
{
|
||||
sio = (io.getMode() == READ)? qse_sio_in: qse_sio_out;
|
||||
@ -836,6 +838,8 @@ int StdAwk::SourceFile::open (Data& io)
|
||||
}
|
||||
else
|
||||
{
|
||||
// open an included file
|
||||
|
||||
const char_t* file = ioname;
|
||||
char_t fbuf[64];
|
||||
char_t* dbuf = QSE_NULL;
|
||||
@ -909,7 +913,7 @@ StdAwk::ssize_t StdAwk::SourceFile::read (Data& io, char_t* buf, size_t len)
|
||||
return qse_sio_getsn ((qse_sio_t*)io.getHandle(), buf, len);
|
||||
}
|
||||
|
||||
StdAwk::ssize_t StdAwk::SourceFile::write (Data& io, char_t* buf, size_t len)
|
||||
StdAwk::ssize_t StdAwk::SourceFile::write (Data& io, const char_t* buf, size_t len)
|
||||
{
|
||||
return qse_sio_putsn ((qse_sio_t*)io.getHandle(), buf, len);
|
||||
}
|
||||
@ -921,13 +925,14 @@ int StdAwk::SourceString::open (Data& io)
|
||||
|
||||
if (ioname == QSE_NULL)
|
||||
{
|
||||
/* SourceString does not support writing */
|
||||
// open the main source file.
|
||||
// SourceString does not support writing.
|
||||
if (io.getMode() == WRITE) return -1;
|
||||
ptr = str;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* open an included file */
|
||||
// open an included file
|
||||
sio = qse_sio_open (
|
||||
((awk_t*)io)->mmgr,
|
||||
0,
|
||||
@ -971,7 +976,7 @@ StdAwk::ssize_t StdAwk::SourceString::read (Data& io, char_t* buf, size_t len)
|
||||
}
|
||||
}
|
||||
|
||||
StdAwk::ssize_t StdAwk::SourceString::write (Data& io, char_t* buf, size_t len)
|
||||
StdAwk::ssize_t StdAwk::SourceString::write (Data& io, const char_t* buf, size_t len)
|
||||
{
|
||||
if (io.getName() == QSE_NULL)
|
||||
{
|
||||
|
@ -21,3 +21,13 @@ if WIN32
|
||||
libqsecmn_la_LIBADD = -lpsapi
|
||||
endif
|
||||
|
||||
if ENABLE_CXX
|
||||
|
||||
lib_LTLIBRARIES += libqsecmnxx.la
|
||||
libqsecmnxx_la_SOURCES = \
|
||||
Mmgr.cpp StdMmgr.cpp
|
||||
libqsecmnxx_la_LDFLAGS = -version-info 1:0:0 -no-undefined
|
||||
|
||||
endif
|
||||
|
||||
|
||||
|
@ -34,6 +34,7 @@ PRE_UNINSTALL = :
|
||||
POST_UNINSTALL = :
|
||||
build_triplet = @build@
|
||||
host_triplet = @host@
|
||||
@ENABLE_CXX_TRUE@am__append_1 = libqsecmnxx.la
|
||||
subdir = lib/cmn
|
||||
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
|
||||
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
|
||||
@ -80,6 +81,14 @@ libqsecmn_la_OBJECTS = $(am_libqsecmn_la_OBJECTS)
|
||||
libqsecmn_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
|
||||
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
|
||||
$(libqsecmn_la_LDFLAGS) $(LDFLAGS) -o $@
|
||||
libqsecmnxx_la_LIBADD =
|
||||
am__libqsecmnxx_la_SOURCES_DIST = Mmgr.cpp StdMmgr.cpp
|
||||
@ENABLE_CXX_TRUE@am_libqsecmnxx_la_OBJECTS = Mmgr.lo StdMmgr.lo
|
||||
libqsecmnxx_la_OBJECTS = $(am_libqsecmnxx_la_OBJECTS)
|
||||
libqsecmnxx_la_LINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) \
|
||||
$(LIBTOOLFLAGS) --mode=link $(CXXLD) $(AM_CXXFLAGS) \
|
||||
$(CXXFLAGS) $(libqsecmnxx_la_LDFLAGS) $(LDFLAGS) -o $@
|
||||
@ENABLE_CXX_TRUE@am_libqsecmnxx_la_rpath = -rpath $(libdir)
|
||||
DEFAULT_INCLUDES =
|
||||
depcomp = $(SHELL) $(top_srcdir)/ac/au/depcomp
|
||||
am__depfiles_maybe = depfiles
|
||||
@ -93,8 +102,18 @@ CCLD = $(CC)
|
||||
LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
|
||||
--mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \
|
||||
$(LDFLAGS) -o $@
|
||||
SOURCES = $(libqsecmn_la_SOURCES)
|
||||
DIST_SOURCES = $(libqsecmn_la_SOURCES)
|
||||
CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
|
||||
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
|
||||
LTCXXCOMPILE = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
|
||||
--mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
|
||||
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
|
||||
CXXLD = $(CXX)
|
||||
CXXLINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
|
||||
--mode=link $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) \
|
||||
$(LDFLAGS) -o $@
|
||||
SOURCES = $(libqsecmn_la_SOURCES) $(libqsecmnxx_la_SOURCES)
|
||||
DIST_SOURCES = $(libqsecmn_la_SOURCES) \
|
||||
$(am__libqsecmnxx_la_SOURCES_DIST)
|
||||
ETAGS = etags
|
||||
CTAGS = ctags
|
||||
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
|
||||
@ -239,7 +258,7 @@ top_builddir = @top_builddir@
|
||||
top_srcdir = @top_srcdir@
|
||||
AUTOMAKE_OPTIONS = nostdinc
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/include
|
||||
lib_LTLIBRARIES = libqsecmn.la
|
||||
lib_LTLIBRARIES = libqsecmn.la $(am__append_1)
|
||||
libqsecmn_la_SOURCES = \
|
||||
syscall.h mem.h \
|
||||
mem.c chr.c chr_cnv.c rex.c \
|
||||
@ -255,10 +274,14 @@ libqsecmn_la_SOURCES = \
|
||||
|
||||
libqsecmn_la_LDFLAGS = -version-info 1:0:0 -no-undefined
|
||||
@WIN32_TRUE@libqsecmn_la_LIBADD = -lpsapi
|
||||
@ENABLE_CXX_TRUE@libqsecmnxx_la_SOURCES = \
|
||||
@ENABLE_CXX_TRUE@ Mmgr.cpp StdMmgr.cpp
|
||||
|
||||
@ENABLE_CXX_TRUE@libqsecmnxx_la_LDFLAGS = -version-info 1:0:0 -no-undefined
|
||||
all: all-am
|
||||
|
||||
.SUFFIXES:
|
||||
.SUFFIXES: .c .lo .o .obj
|
||||
.SUFFIXES: .c .cpp .lo .o .obj
|
||||
$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
|
||||
@for dep in $?; do \
|
||||
case '$(am__configure_deps)' in \
|
||||
@ -322,6 +345,8 @@ clean-libLTLIBRARIES:
|
||||
done
|
||||
libqsecmn.la: $(libqsecmn_la_OBJECTS) $(libqsecmn_la_DEPENDENCIES)
|
||||
$(libqsecmn_la_LINK) -rpath $(libdir) $(libqsecmn_la_OBJECTS) $(libqsecmn_la_LIBADD) $(LIBS)
|
||||
libqsecmnxx.la: $(libqsecmnxx_la_OBJECTS) $(libqsecmnxx_la_DEPENDENCIES)
|
||||
$(libqsecmnxx_la_LINK) $(am_libqsecmnxx_la_rpath) $(libqsecmnxx_la_OBJECTS) $(libqsecmnxx_la_LIBADD) $(LIBS)
|
||||
|
||||
mostlyclean-compile:
|
||||
-rm -f *.$(OBJEXT)
|
||||
@ -329,6 +354,8 @@ mostlyclean-compile:
|
||||
distclean-compile:
|
||||
-rm -f *.tab.c
|
||||
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Mmgr.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/StdMmgr.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/assert.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chr.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chr_cnv.Plo@am__quote@
|
||||
@ -375,6 +402,27 @@ distclean-compile:
|
||||
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCC_FALSE@ $(LTCOMPILE) -c -o $@ $<
|
||||
|
||||
.cpp.o:
|
||||
@am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ $<
|
||||
|
||||
.cpp.obj:
|
||||
@am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'`
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'`
|
||||
|
||||
.cpp.lo:
|
||||
@am__fastdepCXX_TRUE@ $(LTCXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
|
||||
@am__fastdepCXX_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(LTCXXCOMPILE) -c -o $@ $<
|
||||
|
||||
mostlyclean-libtool:
|
||||
-rm -f *.lo
|
||||
|
||||
|
44
qse/lib/cmn/Mmgr.cpp
Normal file
44
qse/lib/cmn/Mmgr.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
Copyright 2006-2009 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/cmn/Mmgr.hpp>
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
void* Mmgr::alloc_mem (void* udd, size_t n)
|
||||
{
|
||||
return ((Mmgr*)udd)->allocMem (n);
|
||||
}
|
||||
|
||||
void* Mmgr::realloc_mem (void* udd, void* ptr, size_t n)
|
||||
{
|
||||
return ((Mmgr*)udd)->reallocMem (ptr, n);
|
||||
}
|
||||
|
||||
void Mmgr::free_mem (void* udd, void* ptr)
|
||||
{
|
||||
return ((Mmgr*)udd)->freeMem (ptr);
|
||||
}
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_END_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
47
qse/lib/cmn/StdMmgr.cpp
Normal file
47
qse/lib/cmn/StdMmgr.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
Copyright 2006-2009 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/cmn/StdMmgr.hpp>
|
||||
#include <stdlib.h>
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
StdMmgr StdMmgr::DFL;
|
||||
|
||||
void* StdMmgr::allocMem (size_t n)
|
||||
{
|
||||
return ::malloc (n);
|
||||
}
|
||||
|
||||
void* StdMmgr::reallocMem (void* ptr, size_t n)
|
||||
{
|
||||
return ::realloc (ptr, n);
|
||||
}
|
||||
|
||||
void StdMmgr::freeMem (void* ptr)
|
||||
{
|
||||
return ::free (ptr);
|
||||
}
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_END_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: Sed.cpp 287 2009-09-15 10:01:02Z hyunghwan.chung $
|
||||
* $Id: Sed.cpp 318 2009-12-18 12:34:42Z hyunghwan.chung $
|
||||
*
|
||||
Copyright 2006-2009 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
@ -19,6 +19,8 @@
|
||||
*/
|
||||
|
||||
#include <qse/sed/Sed.hpp>
|
||||
#include <qse/cmn/sio.h>
|
||||
#include "../cmn/mem.h"
|
||||
#include "sed.h"
|
||||
|
||||
/////////////////////////////////
|
||||
@ -27,7 +29,7 @@ QSE_BEGIN_NAMESPACE(QSE)
|
||||
|
||||
int Sed::open ()
|
||||
{
|
||||
sed = qse_sed_open (this, QSE_SIZEOF(Sed*));
|
||||
sed = qse_sed_open (this->mmgr, QSE_SIZEOF(Sed*));
|
||||
if (sed == QSE_NULL) return -1;
|
||||
*(Sed**)QSE_XTN(sed) = this;
|
||||
|
||||
@ -58,9 +60,11 @@ int Sed::compile (const char_t* sptr, size_t slen)
|
||||
return qse_sed_comp (sed, sptr, slen);
|
||||
}
|
||||
|
||||
int Sed::execute ()
|
||||
int Sed::execute (IOStream& iostream)
|
||||
{
|
||||
QSE_ASSERT (sed != QSE_NULL);
|
||||
|
||||
this->iostream = &iostream;
|
||||
return qse_sed_exec (sed, xin, xout);
|
||||
}
|
||||
|
||||
@ -132,51 +136,25 @@ Sed::ssize_t Sed::xin (
|
||||
{
|
||||
Sed* sed = *(Sed**)QSE_XTN(s);
|
||||
|
||||
if (arg->path == QSE_NULL)
|
||||
{
|
||||
Console io (arg, Console::READ);
|
||||
IOStream::Data iodata (sed, IOStream::READ, arg);
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
switch (cmd)
|
||||
{
|
||||
switch (cmd)
|
||||
{
|
||||
case QSE_SED_IO_OPEN:
|
||||
return sed->openConsole (io);
|
||||
case QSE_SED_IO_CLOSE:
|
||||
return sed->closeConsole (io);
|
||||
case QSE_SED_IO_READ:
|
||||
return sed->readConsole (io, buf, len);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return -1;
|
||||
case QSE_SED_IO_OPEN:
|
||||
return sed->iostream->open (iodata);
|
||||
case QSE_SED_IO_CLOSE:
|
||||
return sed->iostream->close (iodata);
|
||||
case QSE_SED_IO_READ:
|
||||
return sed->iostream->read (iodata, buf, len);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (...)
|
||||
{
|
||||
File io (arg, File::READ);
|
||||
|
||||
try
|
||||
{
|
||||
switch (cmd)
|
||||
{
|
||||
case QSE_SED_IO_OPEN:
|
||||
return sed->openFile (io);
|
||||
case QSE_SED_IO_CLOSE:
|
||||
return sed->closeFile (io);
|
||||
case QSE_SED_IO_READ:
|
||||
return sed->readFile (io, buf, len);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -185,51 +163,25 @@ Sed::ssize_t Sed::xout (
|
||||
{
|
||||
Sed* sed = *(Sed**)QSE_XTN(s);
|
||||
|
||||
if (arg->path == QSE_NULL)
|
||||
{
|
||||
Console io (arg, Console::WRITE);
|
||||
IOStream::Data iodata (sed, IOStream::WRITE, arg);
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
switch (cmd)
|
||||
{
|
||||
switch (cmd)
|
||||
{
|
||||
case QSE_SED_IO_OPEN:
|
||||
return sed->openConsole (io);
|
||||
case QSE_SED_IO_CLOSE:
|
||||
return sed->closeConsole (io);
|
||||
case QSE_SED_IO_WRITE:
|
||||
return sed->writeConsole (io, dat, len);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return -1;
|
||||
case QSE_SED_IO_OPEN:
|
||||
return sed->iostream->open (iodata);
|
||||
case QSE_SED_IO_CLOSE:
|
||||
return sed->iostream->close (iodata);
|
||||
case QSE_SED_IO_WRITE:
|
||||
return sed->iostream->write (iodata, dat, len);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (...)
|
||||
{
|
||||
File io (arg, File::WRITE);
|
||||
|
||||
try
|
||||
{
|
||||
switch (cmd)
|
||||
{
|
||||
case QSE_SED_IO_OPEN:
|
||||
return sed->openFile (io);
|
||||
case QSE_SED_IO_CLOSE:
|
||||
return sed->closeFile (io);
|
||||
case QSE_SED_IO_WRITE:
|
||||
return sed->writeFile (io, dat, len);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: StdSed.cpp 287 2009-09-15 10:01:02Z hyunghwan.chung $
|
||||
* $Id: StdSed.cpp 318 2009-12-18 12:34:42Z hyunghwan.chung $
|
||||
*
|
||||
Copyright 2006-2009 Chung, Hyung-Hwan.
|
||||
This file is part of QSE.
|
||||
@ -21,79 +21,150 @@
|
||||
#include <qse/sed/StdSed.hpp>
|
||||
#include <qse/cmn/fio.h>
|
||||
#include <qse/cmn/sio.h>
|
||||
#include <stdlib.h>
|
||||
#include "sed.h"
|
||||
#include "../cmn/mem.h"
|
||||
|
||||
/////////////////////////////////
|
||||
QSE_BEGIN_NAMESPACE(QSE)
|
||||
/////////////////////////////////
|
||||
|
||||
void* StdSed::allocMem (qse_size_t n)
|
||||
int StdSed::StdStream::open (Data& io)
|
||||
{
|
||||
return ::malloc (n);
|
||||
}
|
||||
qse_sio_t* sio;
|
||||
const char_t* ioname = io.getName();
|
||||
|
||||
void* StdSed::reallocMem (void* ptr, qse_size_t n)
|
||||
{
|
||||
return ::realloc (ptr, n);
|
||||
}
|
||||
if (ioname == QSE_NULL)
|
||||
{
|
||||
//
|
||||
// a normal console is indicated by a null name
|
||||
//
|
||||
|
||||
void StdSed::freeMem (void* ptr)
|
||||
{
|
||||
::free (ptr);
|
||||
}
|
||||
if (io.getMode() == READ)
|
||||
{
|
||||
if (infile == QSE_NULL) sio = qse_sio_in;
|
||||
else
|
||||
{
|
||||
sio = qse_sio_open (
|
||||
((sed_t*)io)->mmgr,
|
||||
0,
|
||||
infile,
|
||||
QSE_SIO_READ
|
||||
);
|
||||
if (sio == QSE_NULL)
|
||||
{
|
||||
// set the error message explicitly
|
||||
// as the file name is different from
|
||||
// the standard console name (NULL)
|
||||
qse_cstr_t ea;
|
||||
ea.ptr = infile;
|
||||
ea.len = qse_strlen (infile);
|
||||
((Sed*)io)->setError (
|
||||
QSE_SED_EIOFIL, &ea);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (outfile == QSE_NULL) sio = qse_sio_out;
|
||||
else
|
||||
{
|
||||
sio = qse_sio_open (
|
||||
((sed_t*)io)->mmgr,
|
||||
0,
|
||||
outfile,
|
||||
QSE_SIO_WRITE |
|
||||
QSE_SIO_CREATE |
|
||||
QSE_SIO_TRUNCATE
|
||||
);
|
||||
if (sio == QSE_NULL)
|
||||
{
|
||||
// set the error message explicitly
|
||||
// as the file name is different from
|
||||
// the standard console name (NULL)
|
||||
qse_cstr_t ea;
|
||||
ea.ptr = outfile;
|
||||
ea.len = qse_strlen (outfile);
|
||||
((Sed*)io)->setError (
|
||||
QSE_SED_EIOFIL, &ea);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// if ioname is not empty, it is a file name
|
||||
//
|
||||
|
||||
int StdSed::openConsole (Console& io)
|
||||
{
|
||||
io.setHandle ((io.getMode() == Console::READ)? qse_sio_in: qse_sio_out);
|
||||
sio = qse_sio_open (
|
||||
((sed_t*)io)->mmgr,
|
||||
0,
|
||||
ioname,
|
||||
(io.getMode() == READ?
|
||||
QSE_SIO_READ:
|
||||
(QSE_SIO_WRITE|QSE_SIO_CREATE|QSE_SIO_TRUNCATE))
|
||||
);
|
||||
|
||||
if (sio == QSE_NULL) return -1;
|
||||
}
|
||||
|
||||
io.setHandle (sio);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int StdSed::closeConsole (Console& io)
|
||||
int StdSed::StdStream::close (Data& io)
|
||||
{
|
||||
qse_sio_t* sio = (qse_sio_t*)io.getHandle();
|
||||
|
||||
qse_sio_flush (sio);
|
||||
if (sio != qse_sio_in && sio != qse_sio_out && sio != qse_sio_err)
|
||||
qse_sio_close (sio);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
StdSed::ssize_t StdSed::readConsole (Console& io, char_t* buf, size_t len)
|
||||
StdSed::ssize_t StdSed::StdStream::read (Data& io, char_t* buf, size_t len)
|
||||
{
|
||||
return qse_sio_getsn ((qse_sio_t*)io.getHandle(), buf, len);
|
||||
ssize_t n = qse_sio_getsn ((qse_sio_t*)io.getHandle(), buf, len);
|
||||
|
||||
if (n == -1)
|
||||
{
|
||||
if (io.getName() == QSE_NULL && infile != QSE_NULL)
|
||||
{
|
||||
// if writing to outfile, set the error message
|
||||
// explicitly. other cases are handled by
|
||||
// the caller in sed.c.
|
||||
qse_cstr_t ea;
|
||||
ea.ptr = infile;
|
||||
ea.len = qse_strlen (infile);
|
||||
((Sed*)io)->setError (QSE_SED_EIOFIL, &ea);
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
StdSed::ssize_t StdSed::writeConsole (Console& io, const char_t* data, size_t len)
|
||||
StdSed::ssize_t StdSed::StdStream::write (Data& io, const char_t* buf, size_t len)
|
||||
{
|
||||
return qse_sio_putsn ((qse_sio_t*)io.getHandle(), data, len);
|
||||
}
|
||||
ssize_t n = qse_sio_putsn ((qse_sio_t*)io.getHandle(), buf, len);
|
||||
|
||||
int StdSed::openFile (File& io)
|
||||
{
|
||||
int flags = (io.getMode() == File::READ)?
|
||||
QSE_FIO_READ: (QSE_FIO_WRITE|QSE_FIO_CREATE|QSE_FIO_TRUNCATE);
|
||||
if (n == -1)
|
||||
{
|
||||
if (io.getName() == QSE_NULL && outfile != QSE_NULL)
|
||||
{
|
||||
// if writing to outfile, set the error message
|
||||
// explicitly. other cases are handled by
|
||||
// the caller in sed.c.
|
||||
qse_cstr_t ea;
|
||||
ea.ptr = outfile;
|
||||
ea.len = qse_strlen (outfile);
|
||||
((Sed*)io)->setError (QSE_SED_EIOFIL, &ea);
|
||||
}
|
||||
}
|
||||
|
||||
qse_fio_t* fio = qse_fio_open (
|
||||
this, 0, io.getName(),
|
||||
flags | QSE_FIO_TEXT,
|
||||
QSE_FIO_RUSR | QSE_FIO_WUSR |
|
||||
QSE_FIO_RGRP | QSE_FIO_ROTH
|
||||
);
|
||||
if (fio == QSE_NULL) return -1;
|
||||
|
||||
io.setHandle (fio);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int StdSed::closeFile (File& io)
|
||||
{
|
||||
qse_fio_close ((qse_fio_t*)io.getHandle());
|
||||
return 0;
|
||||
}
|
||||
|
||||
StdSed::ssize_t StdSed::readFile (File& io, char_t* buf, size_t len)
|
||||
{
|
||||
return qse_fio_read ((qse_fio_t*)io.getHandle(), buf, len);
|
||||
}
|
||||
|
||||
StdSed::ssize_t StdSed::writeFile (File& io, const char_t* data, size_t len)
|
||||
{
|
||||
return qse_fio_write ((qse_fio_t*)io.getHandle(), data, len);
|
||||
return n;
|
||||
}
|
||||
|
||||
/////////////////////////////////
|
||||
|
@ -11,6 +11,9 @@ awk03_SOURCES = awk03.c
|
||||
awk04_SOURCES = awk04.c
|
||||
|
||||
if ENABLE_CXX
|
||||
|
||||
CXXLIB = -lqseawkxx -lqsecmnxx
|
||||
|
||||
bin_PROGRAMS += awk05 awk06 awk07 awk08
|
||||
|
||||
awk05_SOURCES = awk05.cpp
|
||||
@ -18,8 +21,8 @@ awk06_SOURCES = awk06.cpp
|
||||
awk07_SOURCES = awk07.cpp
|
||||
awk08_SOURCES = awk08.cpp
|
||||
|
||||
awk05_LDADD = -lqseawkxx $(LDADD)
|
||||
awk06_LDADD = -lqseawkxx $(LDADD)
|
||||
awk07_LDADD = -lqseawkxx $(LDADD)
|
||||
awk08_LDADD = -lqseawkxx $(LDADD)
|
||||
awk05_LDADD = $(CXXLIB) $(LDADD)
|
||||
awk06_LDADD = $(CXXLIB) $(LDADD)
|
||||
awk07_LDADD = $(CXXLIB) $(LDADD)
|
||||
awk08_LDADD = $(CXXLIB) $(LDADD)
|
||||
endif
|
||||
|
@ -76,19 +76,23 @@ am__awk05_SOURCES_DIST = awk05.cpp
|
||||
@ENABLE_CXX_TRUE@am_awk05_OBJECTS = awk05.$(OBJEXT)
|
||||
awk05_OBJECTS = $(am_awk05_OBJECTS)
|
||||
am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1)
|
||||
@ENABLE_CXX_TRUE@awk05_DEPENDENCIES = $(am__DEPENDENCIES_2)
|
||||
@ENABLE_CXX_TRUE@awk05_DEPENDENCIES = $(am__DEPENDENCIES_1) \
|
||||
@ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_2)
|
||||
am__awk06_SOURCES_DIST = awk06.cpp
|
||||
@ENABLE_CXX_TRUE@am_awk06_OBJECTS = awk06.$(OBJEXT)
|
||||
awk06_OBJECTS = $(am_awk06_OBJECTS)
|
||||
@ENABLE_CXX_TRUE@awk06_DEPENDENCIES = $(am__DEPENDENCIES_2)
|
||||
@ENABLE_CXX_TRUE@awk06_DEPENDENCIES = $(am__DEPENDENCIES_1) \
|
||||
@ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_2)
|
||||
am__awk07_SOURCES_DIST = awk07.cpp
|
||||
@ENABLE_CXX_TRUE@am_awk07_OBJECTS = awk07.$(OBJEXT)
|
||||
awk07_OBJECTS = $(am_awk07_OBJECTS)
|
||||
@ENABLE_CXX_TRUE@awk07_DEPENDENCIES = $(am__DEPENDENCIES_2)
|
||||
@ENABLE_CXX_TRUE@awk07_DEPENDENCIES = $(am__DEPENDENCIES_1) \
|
||||
@ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_2)
|
||||
am__awk08_SOURCES_DIST = awk08.cpp
|
||||
@ENABLE_CXX_TRUE@am_awk08_OBJECTS = awk08.$(OBJEXT)
|
||||
awk08_OBJECTS = $(am_awk08_OBJECTS)
|
||||
@ENABLE_CXX_TRUE@awk08_DEPENDENCIES = $(am__DEPENDENCIES_2)
|
||||
@ENABLE_CXX_TRUE@awk08_DEPENDENCIES = $(am__DEPENDENCIES_1) \
|
||||
@ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_2)
|
||||
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include/qse
|
||||
depcomp = $(SHELL) $(top_srcdir)/ac/au/depcomp
|
||||
am__depfiles_maybe = depfiles
|
||||
@ -266,14 +270,15 @@ awk01_SOURCES = awk01.c
|
||||
awk02_SOURCES = awk02.c
|
||||
awk03_SOURCES = awk03.c
|
||||
awk04_SOURCES = awk04.c
|
||||
@ENABLE_CXX_TRUE@CXXLIB = -lqseawkxx -lqsecmnxx
|
||||
@ENABLE_CXX_TRUE@awk05_SOURCES = awk05.cpp
|
||||
@ENABLE_CXX_TRUE@awk06_SOURCES = awk06.cpp
|
||||
@ENABLE_CXX_TRUE@awk07_SOURCES = awk07.cpp
|
||||
@ENABLE_CXX_TRUE@awk08_SOURCES = awk08.cpp
|
||||
@ENABLE_CXX_TRUE@awk05_LDADD = -lqseawkxx $(LDADD)
|
||||
@ENABLE_CXX_TRUE@awk06_LDADD = -lqseawkxx $(LDADD)
|
||||
@ENABLE_CXX_TRUE@awk07_LDADD = -lqseawkxx $(LDADD)
|
||||
@ENABLE_CXX_TRUE@awk08_LDADD = -lqseawkxx $(LDADD)
|
||||
@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)
|
||||
all: all-am
|
||||
|
||||
.SUFFIXES:
|
||||
|
@ -6,11 +6,14 @@ LDFLAGS = -L../../lib/cmn -L../../lib/sed
|
||||
LDADD = -lqsesed -lqsecmn $(LIBM)
|
||||
|
||||
if ENABLE_CXX
|
||||
|
||||
CXXLIB = -lqsesedxx -lqsecmnxx
|
||||
|
||||
bin_PROGRAMS += sed02 sed03
|
||||
|
||||
sed02_SOURCES = sed02.cpp
|
||||
sed02_LDADD = -lqsesedxx $(LDADD)
|
||||
sed02_LDADD = $(CXXLIB) $(LDADD)
|
||||
|
||||
sed03_SOURCES = sed03.cpp
|
||||
sed03_LDADD = -lqsesedxx $(LDADD)
|
||||
sed03_LDADD = $(CXXLIB) $(LDADD)
|
||||
endif
|
||||
|
@ -58,11 +58,13 @@ am__sed02_SOURCES_DIST = sed02.cpp
|
||||
sed02_OBJECTS = $(am_sed02_OBJECTS)
|
||||
am__DEPENDENCIES_1 =
|
||||
am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1)
|
||||
@ENABLE_CXX_TRUE@sed02_DEPENDENCIES = $(am__DEPENDENCIES_2)
|
||||
@ENABLE_CXX_TRUE@sed02_DEPENDENCIES = $(am__DEPENDENCIES_1) \
|
||||
@ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_2)
|
||||
am__sed03_SOURCES_DIST = sed03.cpp
|
||||
@ENABLE_CXX_TRUE@am_sed03_OBJECTS = sed03.$(OBJEXT)
|
||||
sed03_OBJECTS = $(am_sed03_OBJECTS)
|
||||
@ENABLE_CXX_TRUE@sed03_DEPENDENCIES = $(am__DEPENDENCIES_2)
|
||||
@ENABLE_CXX_TRUE@sed03_DEPENDENCIES = $(am__DEPENDENCIES_1) \
|
||||
@ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_2)
|
||||
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include/qse
|
||||
depcomp = $(SHELL) $(top_srcdir)/ac/au/depcomp
|
||||
am__depfiles_maybe = depfiles
|
||||
@ -222,10 +224,11 @@ top_builddir = @top_builddir@
|
||||
top_srcdir = @top_srcdir@
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/include
|
||||
LDADD = -lqsesed -lqsecmn $(LIBM)
|
||||
@ENABLE_CXX_TRUE@CXXLIB = -lqsesedxx -lqsecmnxx
|
||||
@ENABLE_CXX_TRUE@sed02_SOURCES = sed02.cpp
|
||||
@ENABLE_CXX_TRUE@sed02_LDADD = -lqsesedxx $(LDADD)
|
||||
@ENABLE_CXX_TRUE@sed02_LDADD = $(CXXLIB) $(LDADD)
|
||||
@ENABLE_CXX_TRUE@sed03_SOURCES = sed03.cpp
|
||||
@ENABLE_CXX_TRUE@sed03_LDADD = -lqsesedxx $(LDADD)
|
||||
@ENABLE_CXX_TRUE@sed03_LDADD = $(CXXLIB) $(LDADD)
|
||||
all: all-am
|
||||
|
||||
.SUFFIXES:
|
||||
|
@ -20,14 +20,20 @@
|
||||
|
||||
#include <qse/sed/StdSed.hpp>
|
||||
#include <qse/cmn/main.h>
|
||||
#include <qse/cmn/stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef QSE_CHAR_IS_MCHAR
|
||||
# define xcout std::cout
|
||||
#else
|
||||
# define xcout std::wcout
|
||||
#endif
|
||||
|
||||
int sed_main (int argc, qse_char_t* argv[])
|
||||
{
|
||||
if (argc != 2)
|
||||
if (argc < 2 || argc > 4)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR,
|
||||
QSE_T("usage: %s command-string\n"), argv[0]);
|
||||
xcout << QSE_T("USAGE: ") << argv[0] <<
|
||||
QSE_T(" command-string [input-file [output-file]]") << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -35,23 +41,24 @@ int sed_main (int argc, qse_char_t* argv[])
|
||||
|
||||
if (sed.open () == -1)
|
||||
{
|
||||
qse_printf (QSE_T("cannot open a stream editor - %s\n"),
|
||||
sed.getErrorMessage());
|
||||
xcout << QSE_T("ERR: cannot open") << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sed.compile (argv[1]) == -1)
|
||||
{
|
||||
qse_printf (QSE_T("cannot compile - %s\n"),
|
||||
sed.getErrorMessage());
|
||||
xcout << QSE_T("ERR: cannot compile - ") << sed.getErrorMessage() << std::endl;
|
||||
sed.close ();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sed.execute () == -1)
|
||||
qse_char_t* infile = (argc >= 3)? argv[2]: QSE_NULL;
|
||||
qse_char_t* outfile = (argc >= 4)? argv[3]: QSE_NULL;
|
||||
QSE::StdSed::StdStream stream (infile, outfile);
|
||||
|
||||
if (sed.execute (stream) == -1)
|
||||
{
|
||||
qse_printf (QSE_T("cannot execute - %s\n"),
|
||||
sed.getErrorMessage());
|
||||
xcout << QSE_T("ERR: cannot execute - ") << sed.getErrorMessage() << std::endl;
|
||||
sed.close ();
|
||||
return -1;
|
||||
}
|
||||
@ -60,7 +67,7 @@ int sed_main (int argc, qse_char_t* argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
int qse_main (int argc, char* argv[])
|
||||
int qse_main (int argc, qse_achar_t* argv[])
|
||||
{
|
||||
return qse_runmain (argc, argv, sed_main);
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
|
||||
#include <qse/sed/StdSed.hpp>
|
||||
#include <qse/cmn/main.h>
|
||||
#include <qse/cmn/sio.h>
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
@ -30,13 +31,111 @@
|
||||
# define xcout std::wcout
|
||||
#endif
|
||||
|
||||
typedef std::basic_string<QSE::StdSed::char_t> xstring;
|
||||
typedef QSE::StdSed StdSed; // added for doxygen cross-reference
|
||||
//
|
||||
// The StringStream class implements a data I/O stream over strings.
|
||||
//
|
||||
class StringStream: public QSE::StdSed::IOStream
|
||||
{
|
||||
public:
|
||||
StringStream (const char_t* in) { this->in.ptr = in; }
|
||||
|
||||
int open (Data& io)
|
||||
{
|
||||
const char_t* ioname = io.getName ();
|
||||
|
||||
if (ioname == QSE_NULL)
|
||||
{
|
||||
// open a main data stream
|
||||
if (io.getMode() == READ)
|
||||
{
|
||||
in.cur = in.ptr;
|
||||
io.setHandle ((void*)in.ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
out.erase ();
|
||||
io.setHandle (&out);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// open files for a r or w command
|
||||
qse_sio_t* sio;
|
||||
int mode = (io.getMode() == READ)?
|
||||
QSE_SIO_READ:
|
||||
(QSE_SIO_WRITE|QSE_SIO_CREATE|QSE_SIO_TRUNCATE);
|
||||
|
||||
sio = qse_sio_open (((QSE::Sed*)io)->getMmgr(), 0, ioname, mode);
|
||||
if (sio == QSE_NULL) return -1;
|
||||
|
||||
io.setHandle (sio);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int close (Data& io)
|
||||
{
|
||||
const void* handle = io.getHandle();
|
||||
if (handle != in.ptr && handle != &out)
|
||||
qse_sio_close ((qse_sio_t*)handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t read (Data& io, char_t* buf, size_t len)
|
||||
{
|
||||
const void* handle = io.getHandle();
|
||||
if (handle == in.ptr)
|
||||
{
|
||||
ssize_t n = qse_strxcpy (buf, len, in.cur);
|
||||
in.cur += n; return n;
|
||||
}
|
||||
else
|
||||
{
|
||||
QSE_ASSERT (handle != &out);
|
||||
return qse_sio_getsn ((qse_sio_t*)handle, buf, len);
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t write (Data& io, const char_t* buf, size_t len)
|
||||
{
|
||||
const void* handle = io.getHandle();
|
||||
|
||||
if (handle == &out)
|
||||
{
|
||||
try
|
||||
{
|
||||
out.append (buf, len);
|
||||
return len;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
((QSE::Sed*)io)->setError (QSE_SED_ENOMEM);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
QSE_ASSERT (handle != in.ptr);
|
||||
return qse_sio_putsn ((qse_sio_t*)handle, buf, len);
|
||||
}
|
||||
}
|
||||
|
||||
const char_t* getInput () const { return in.ptr; }
|
||||
const char_t* getOutput () const { return out.c_str (); }
|
||||
|
||||
protected:
|
||||
struct
|
||||
{
|
||||
const char_t* ptr;
|
||||
const char_t* cur;
|
||||
} in;
|
||||
|
||||
std::basic_string<char_t> out;
|
||||
};
|
||||
|
||||
//
|
||||
// The MySed class provides a handier interface to QSE::StdSed by
|
||||
// reimplementing console I/O functions, combining compile() and execute(),
|
||||
// and utilizing exception handling.
|
||||
// The MySed class simplifies QSE::StdSed by utilizing exception handling.
|
||||
//
|
||||
class MySed: protected QSE::StdSed
|
||||
{
|
||||
@ -45,65 +144,26 @@ public:
|
||||
class Error
|
||||
{
|
||||
public:
|
||||
Error (const char_t* msg) throw (): msg (msg) { }
|
||||
Error (const char_t* msg) throw (): msg (msg) {}
|
||||
const char_t* getMessage() const throw() { return msg; }
|
||||
protected:
|
||||
const char_t* msg;
|
||||
};
|
||||
|
||||
MySed ()
|
||||
MySed () { if (open() <= -1) throw Error (QSE_T("cannot open")); }
|
||||
~MySed () { close (); }
|
||||
|
||||
void compile (const char_t* sptr)
|
||||
{
|
||||
if (QSE::StdSed::open() == -1)
|
||||
throw Error (QSE_T("cannot open"));
|
||||
if (QSE::StdSed::compile (sptr) <= -1)
|
||||
throw Error (getErrorMessage());
|
||||
}
|
||||
|
||||
~MySed ()
|
||||
void execute (IOStream& stream)
|
||||
{
|
||||
QSE::StdSed::close ();
|
||||
if (QSE::StdSed::execute (stream) <= -1)
|
||||
throw Error (getErrorMessage());
|
||||
}
|
||||
|
||||
void run (const char_t* cmds, const char_t* in, xstring& out)
|
||||
{
|
||||
// remember an input string and an output string
|
||||
this->in = in; this->out = &out;
|
||||
|
||||
// compile source commands and execute compiled commands.
|
||||
if (QSE::StdSed::compile (cmds) <= -1 ||
|
||||
QSE::StdSed::execute () <= -1)
|
||||
{
|
||||
throw Error (QSE::StdSed::getErrorMessage());
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
// override console I/O functions to perform I/O over strings.
|
||||
|
||||
int openConsole (Console& io)
|
||||
{
|
||||
if (io.getMode() == Console::WRITE) out->clear();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int closeConsole (Console& io)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t readConsole (Console& io, char_t* buf, size_t len)
|
||||
{
|
||||
ssize_t n = qse_strxcpy (buf, len, in);
|
||||
in += n; return n;
|
||||
}
|
||||
|
||||
ssize_t writeConsole (Console& io, const char_t* buf, size_t len)
|
||||
{
|
||||
try { out->append (buf, len); return len; }
|
||||
catch (...) { QSE::Sed::setError (QSE_SED_ENOMEM); throw; }
|
||||
}
|
||||
|
||||
protected:
|
||||
const char_t* in;
|
||||
xstring* out;
|
||||
};
|
||||
|
||||
int sed_main (int argc, qse_char_t* argv[])
|
||||
@ -111,12 +171,14 @@ int sed_main (int argc, qse_char_t* argv[])
|
||||
try
|
||||
{
|
||||
MySed sed;
|
||||
xstring out;
|
||||
|
||||
sed.run (
|
||||
QSE_T("y/ABC/abc/;s/abc/def/g"),
|
||||
QSE_T("ABCDEFabcdef"), out);
|
||||
xcout << QSE_T("OUTPUT: ") << out << std::endl;
|
||||
sed.compile (QSE_T("y/ABC/abc/;s/abc/def/g"));
|
||||
|
||||
StringStream stream (QSE_T("ABCDEFabcdef"));
|
||||
sed.execute (stream);
|
||||
|
||||
xcout << QSE_T("INPUT: ") << stream.getInput() << std::endl;
|
||||
xcout << QSE_T("OUTPUT: ") << stream.getOutput() << std::endl;
|
||||
}
|
||||
catch (MySed::Error& err)
|
||||
{
|
||||
@ -127,7 +189,7 @@ int sed_main (int argc, qse_char_t* argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
int qse_main (int argc, char* argv[])
|
||||
int qse_main (int argc, qse_achar_t* argv[])
|
||||
{
|
||||
return qse_runmain (argc, argv, sed_main);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user