- 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:
2009-12-19 06:34:42 +00:00
parent 232c0cc1c4
commit de7082d0d0
31 changed files with 944 additions and 630 deletions

View File

@ -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:

View File

@ -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/||"`;; \

View File

@ -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

View File

@ -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 ();

View File

@ -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 ();

View File

@ -3,9 +3,9 @@ pkgincludedir = $(includedir)/qse/cmn
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
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

View File

@ -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:

View 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

View 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

View 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

View File

@ -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

View File

@ -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>

View File

@ -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 (

View File

@ -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.
*/
/**