- 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:
@ -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)
|
||||
{
|
||||
return ::malloc (n);
|
||||
}
|
||||
|
||||
void* StdSed::reallocMem (void* ptr, qse_size_t n)
|
||||
{
|
||||
return ::realloc (ptr, n);
|
||||
}
|
||||
|
||||
void StdSed::freeMem (void* ptr)
|
||||
{
|
||||
::free (ptr);
|
||||
}
|
||||
|
||||
int StdSed::openConsole (Console& io)
|
||||
int StdSed::StdStream::open (Data& io)
|
||||
{
|
||||
io.setHandle ((io.getMode() == Console::READ)? qse_sio_in: qse_sio_out);
|
||||
qse_sio_t* sio;
|
||||
const char_t* ioname = io.getName();
|
||||
|
||||
if (ioname == QSE_NULL)
|
||||
{
|
||||
//
|
||||
// a normal console is indicated by a null name
|
||||
//
|
||||
|
||||
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
|
||||
//
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/////////////////////////////////
|
||||
|
Reference in New Issue
Block a user