- added easy C API functions for sed and cut

- added a sample C program for sed
- added C++ API for cut.
This commit is contained in:
2009-12-22 06:29:52 +00:00
parent 92cbbbcec1
commit d418e651e5
30 changed files with 1705 additions and 148 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: std.c 306 2009-11-22 13:58:53Z hyunghwan.chung $
* $Id: std.c 320 2009-12-21 12:29:52Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -26,6 +26,7 @@
#include <qse/cmn/time.h>
#include <qse/cmn/misc.h>
#include <qse/cmn/stdio.h> /* TODO: remove dependency on qse_vsprintf */
#include "../cmn/mem.h"
#include <stdarg.h>
#include <stdlib.h>

View File

@ -1,5 +1,5 @@
/*
* $Id: str_bas.c 290 2009-09-19 04:28:49Z hyunghwan.chung $
* $Id: str_bas.c 320 2009-12-21 12:29:52Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -450,6 +450,13 @@ qse_char_t* qse_strdup (const qse_char_t* str, qse_mmgr_t* mmgr)
return tmp;
}
qse_char_t* qse_strdup2 (
const qse_char_t* str1, const qse_char_t* str2, qse_mmgr_t* mmgr)
{
return qse_strxdup2 (
str1, qse_strlen(str1), str2, qse_strlen(str2), mmgr);
}
qse_char_t* qse_strxdup (
const qse_char_t* str, qse_size_t len, qse_mmgr_t* mmgr)
{

175
qse/lib/cut/Cut.cpp Normal file
View File

@ -0,0 +1,175 @@
/*
* $Id: Cut.cpp 319 2009-12-19 03:06:28Z hyunghwan.chung $
*
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/cut/Cut.hpp>
#include <qse/cmn/sio.h>
#include "../cmn/mem.h"
#include "cut.h"
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
int Cut::open ()
{
cut = qse_cut_open (this->mmgr, QSE_SIZEOF(Cut*));
if (cut == QSE_NULL) return -1;
*(Cut**)QSE_XTN(cut) = this;
dflerrstr = qse_cut_geterrstr (cut);
qse_cut_seterrstr (cut, xerrstr);
return 0;
}
void Cut::close ()
{
if (cut != QSE_NULL)
{
qse_cut_close (cut);
cut = QSE_NULL;
}
}
int Cut::compile (const char_t* sptr)
{
QSE_ASSERT (cut != QSE_NULL);
return qse_cut_comp (cut, sptr, qse_strlen(sptr), delim.in, delim.out);
}
int Cut::compile (const char_t* sptr, size_t slen)
{
QSE_ASSERT (cut != QSE_NULL);
return qse_cut_comp (cut, sptr, slen, delim.in, delim.out);
}
int Cut::execute (Stream& iostream)
{
QSE_ASSERT (cut != QSE_NULL);
this->iostream = &iostream;
return qse_cut_exec (cut, xin, xout);
}
int Cut::getOption() const
{
QSE_ASSERT (cut != QSE_NULL);
return qse_cut_getoption (cut);
}
void Cut::setOption (int opt)
{
QSE_ASSERT (cut != QSE_NULL);
qse_cut_setoption (cut, opt);
}
const Cut::char_t* Cut::getErrorMessage () const
{
return (cut == QSE_NULL)? QSE_T(""): qse_cut_geterrmsg (cut);
}
Cut::errnum_t Cut::getErrorNumber () const
{
return (cut == QSE_NULL)? QSE_CUT_ENOERR: qse_cut_geterrnum (cut);
}
void Cut::setError (errnum_t err, const cstr_t* args)
{
QSE_ASSERT (cut != QSE_NULL);
qse_cut_seterror (cut, err, args);
}
Cut::ssize_t Cut::xin (
cut_t* s, io_cmd_t cmd, io_arg_t* arg, char_t* buf, size_t len)
{
Cut* cut = *(Cut**)QSE_XTN(s);
Stream::Data iodata (cut, Stream::READ, arg);
try
{
switch (cmd)
{
case QSE_CUT_IO_OPEN:
return cut->iostream->open (iodata);
case QSE_CUT_IO_CLOSE:
return cut->iostream->close (iodata);
case QSE_CUT_IO_READ:
return cut->iostream->read (iodata, buf, len);
default:
return -1;
}
}
catch (...)
{
return -1;
}
}
Cut::ssize_t Cut::xout (
cut_t* s, io_cmd_t cmd, io_arg_t* arg, char_t* dat, size_t len)
{
Cut* cut = *(Cut**)QSE_XTN(s);
Stream::Data iodata (cut, Stream::WRITE, arg);
try
{
switch (cmd)
{
case QSE_CUT_IO_OPEN:
return cut->iostream->open (iodata);
case QSE_CUT_IO_CLOSE:
return cut->iostream->close (iodata);
case QSE_CUT_IO_WRITE:
return cut->iostream->write (iodata, dat, len);
default:
return -1;
}
}
catch (...)
{
return -1;
}
}
const Cut::char_t* Cut::getErrorString (errnum_t num) const
{
QSE_ASSERT (dflerrstr != QSE_NULL);
return dflerrstr (cut, num);
}
const Cut::char_t* Cut::xerrstr (cut_t* s, errnum_t num)
{
Cut* cut = *(Cut**)QSE_XTN(s);
try
{
return cut->getErrorString (num);
}
catch (...)
{
return cut->dflerrstr (s, num);
}
}
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////

View File

@ -1,15 +1,16 @@
AUTOMAKE_OPTIONS = nostdinc
AM_CPPFLAGS = -I$(top_srcdir)/include
lib_LTLIBRARIES = libqsecut.la
libqsecut_la_SOURCES = cut.c err.c cut.h
libqsecut_la_SOURCES = cut.c err.c cut.h std.c
libqsecut_la_LDFLAGS = -version-info 1:0:0 -no-undefined -L../cmn
libqsecut_la_LIBADD = -lqsecmn
#if ENABLE_CXX
#lib_LTLIBRARIES += libqsecutxx.la
#libqsecutxx_la_SOURCES = Sed.cpp StdSed.cpp
#libqsecutxx_la_LDFLAGS = -L. -L../cmn -version-info 1:0:0 -no-undefined
#libqsecutxx_la_LIBADD = -lqsecut -lqsecmn
#endif
if ENABLE_CXX
lib_LTLIBRARIES += libqsecutxx.la
libqsecutxx_la_SOURCES = Cut.cpp StdCut.cpp
libqsecutxx_la_LDFLAGS = -L. -L../cmn -version-info 1:0:0 -no-undefined
libqsecutxx_la_LIBADD = -lqsecut -lqsecmn
endif

View File

@ -34,6 +34,7 @@ PRE_UNINSTALL = :
POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
@ENABLE_CXX_TRUE@am__append_1 = libqsecutxx.la
subdir = lib/cut
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
@ -72,12 +73,20 @@ am__base_list = \
am__installdirs = "$(DESTDIR)$(libdir)"
LTLIBRARIES = $(lib_LTLIBRARIES)
libqsecut_la_DEPENDENCIES =
am_libqsecut_la_OBJECTS = cut.lo err.lo
am_libqsecut_la_OBJECTS = cut.lo err.lo std.lo
libqsecut_la_OBJECTS = $(am_libqsecut_la_OBJECTS)
libqsecut_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
$(libqsecut_la_LDFLAGS) $(LDFLAGS) -o $@
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include/qse
libqsecutxx_la_DEPENDENCIES =
am__libqsecutxx_la_SOURCES_DIST = Cut.cpp StdCut.cpp
@ENABLE_CXX_TRUE@am_libqsecutxx_la_OBJECTS = Cut.lo StdCut.lo
libqsecutxx_la_OBJECTS = $(am_libqsecutxx_la_OBJECTS)
libqsecutxx_la_LINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CXXLD) $(AM_CXXFLAGS) \
$(CXXFLAGS) $(libqsecutxx_la_LDFLAGS) $(LDFLAGS) -o $@
@ENABLE_CXX_TRUE@am_libqsecutxx_la_rpath = -rpath $(libdir)
DEFAULT_INCLUDES =
depcomp = $(SHELL) $(top_srcdir)/ac/au/depcomp
am__depfiles_maybe = depfiles
am__mv = mv -f
@ -90,8 +99,18 @@ CCLD = $(CC)
LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
--mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \
$(LDFLAGS) -o $@
SOURCES = $(libqsecut_la_SOURCES)
DIST_SOURCES = $(libqsecut_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 = $(libqsecut_la_SOURCES) $(libqsecutxx_la_SOURCES)
DIST_SOURCES = $(libqsecut_la_SOURCES) \
$(am__libqsecutxx_la_SOURCES_DIST)
ETAGS = etags
CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@ -234,15 +253,19 @@ target_alias = @target_alias@
top_build_prefix = @top_build_prefix@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
AUTOMAKE_OPTIONS = nostdinc
AM_CPPFLAGS = -I$(top_srcdir)/include
lib_LTLIBRARIES = libqsecut.la
libqsecut_la_SOURCES = cut.c err.c cut.h
lib_LTLIBRARIES = libqsecut.la $(am__append_1)
libqsecut_la_SOURCES = cut.c err.c cut.h std.c
libqsecut_la_LDFLAGS = -version-info 1:0:0 -no-undefined -L../cmn
libqsecut_la_LIBADD = -lqsecmn
@ENABLE_CXX_TRUE@libqsecutxx_la_SOURCES = Cut.cpp StdCut.cpp
@ENABLE_CXX_TRUE@libqsecutxx_la_LDFLAGS = -L. -L../cmn -version-info 1:0:0 -no-undefined
@ENABLE_CXX_TRUE@libqsecutxx_la_LIBADD = -lqsecut -lqsecmn
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 \
@ -306,6 +329,8 @@ clean-libLTLIBRARIES:
done
libqsecut.la: $(libqsecut_la_OBJECTS) $(libqsecut_la_DEPENDENCIES)
$(libqsecut_la_LINK) -rpath $(libdir) $(libqsecut_la_OBJECTS) $(libqsecut_la_LIBADD) $(LIBS)
libqsecutxx.la: $(libqsecutxx_la_OBJECTS) $(libqsecutxx_la_DEPENDENCIES)
$(libqsecutxx_la_LINK) $(am_libqsecutxx_la_rpath) $(libqsecutxx_la_OBJECTS) $(libqsecutxx_la_LIBADD) $(LIBS)
mostlyclean-compile:
-rm -f *.$(OBJEXT)
@ -313,8 +338,11 @@ mostlyclean-compile:
distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Cut.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/StdCut.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cut.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/err.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/std.Plo@am__quote@
.c.o:
@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
@ -337,6 +365,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
@ -545,13 +594,6 @@ uninstall-am: uninstall-libLTLIBRARIES
tags uninstall uninstall-am uninstall-libLTLIBRARIES
#if ENABLE_CXX
#lib_LTLIBRARIES += libqsecutxx.la
#libqsecutxx_la_SOURCES = Sed.cpp StdSed.cpp
#libqsecutxx_la_LDFLAGS = -L. -L../cmn -version-info 1:0:0 -no-undefined
#libqsecutxx_la_LIBADD = -lqsecut -lqsecmn
#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:

264
qse/lib/cut/StdCut.cpp Normal file
View File

@ -0,0 +1,264 @@
/*
* $Id: StdCut.cpp 319 2009-12-19 03:06:28Z hyunghwan.chung $
*
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/cut/StdCut.hpp>
#include <qse/cmn/fio.h>
#include <qse/cmn/sio.h>
#include "cut.h"
#include "../cmn/mem.h"
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
int StdCut::FileStream::open (Data& io)
{
qse_sio_t* sio;
if (io.getMode() == READ)
{
if (infile == QSE_NULL) sio = qse_sio_in;
else
{
sio = qse_sio_open (
((cut_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);
((Cut*)io)->setError (
QSE_CUT_EIOFIL, &ea);
return -1;
}
}
}
else
{
if (outfile == QSE_NULL) sio = qse_sio_out;
else
{
sio = qse_sio_open (
((cut_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);
((Cut*)io)->setError (
QSE_CUT_EIOFIL, &ea);
return -1;
}
}
}
io.setHandle (sio);
return 1;
}
int StdCut::FileStream::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;
}
StdCut::ssize_t StdCut::FileStream::read (Data& io, char_t* buf, size_t len)
{
ssize_t n = qse_sio_getsn ((qse_sio_t*)io.getHandle(), buf, len);
if (n == -1)
{
if (infile != QSE_NULL)
{
// if writing to outfile, set the error message
// explicitly. other cases are handled by
// the caller in cut.c.
qse_cstr_t ea;
ea.ptr = infile;
ea.len = qse_strlen (infile);
((Cut*)io)->setError (QSE_CUT_EIOFIL, &ea);
}
}
return n;
}
StdCut::ssize_t StdCut::FileStream::write (Data& io, const char_t* buf, size_t len)
{
ssize_t n = qse_sio_putsn ((qse_sio_t*)io.getHandle(), buf, len);
if (n == -1)
{
if (outfile != QSE_NULL)
{
// if writing to outfile, set the error message
// explicitly. other cases are handled by
// the caller in cut.c.
qse_cstr_t ea;
ea.ptr = outfile;
ea.len = qse_strlen (outfile);
((Cut*)io)->setError (QSE_CUT_EIOFIL, &ea);
}
}
return n;
}
StdCut::StringStream::StringStream (const char_t* in)
{
this->in.ptr = in;
this->in.end = in + qse_strlen(in);
this->out.inited = false;
}
StdCut::StringStream::StringStream (const char_t* in, size_t len)
{
this->in.ptr = in;
this->in.end = in + len;
this->out.inited = false;
}
StdCut::StringStream::~StringStream ()
{
if (out.inited) qse_str_fini (&out.buf);
}
int StdCut::StringStream::open (Data& io)
{
// open a main data stream
if (io.getMode() == READ)
{
in.cur = in.ptr;
io.setHandle ((void*)in.ptr);
}
else
{
if (!out.inited)
{
if (qse_str_init (&out.buf, ((Cut*)io)->getMmgr(), 256) == QSE_NULL)
{
((Cut*)io)->setError (QSE_CUT_ENOMEM);
return -1;
}
out.inited = true;
}
qse_str_clear (&out.buf);
io.setHandle (&out.buf);
}
return 1;
}
int StdCut::StringStream::close (Data& io)
{
const void* handle = io.getHandle();
if (handle != in.ptr && handle != &out.buf)
qse_sio_close ((qse_sio_t*)handle);
return 0;
}
StdCut::ssize_t StdCut::StringStream::read (Data& io, char_t* buf, size_t len)
{
const void* handle = io.getHandle();
if (len == (size_t)-1) len--; // shrink buffer if too long
if (handle == in.ptr)
{
size_t n = 0;
while (in.cur < in.end && n < len)
buf[n++] = *in.cur++;
return (ssize_t)n;
}
else
{
QSE_ASSERT (handle != &out.buf);
return -1;
}
}
StdCut::ssize_t StdCut::StringStream::write (Data& io, const char_t* data, size_t len)
{
const void* handle = io.getHandle();
if (len == (qse_size_t)-1) len--; // shrink data if too long
if (handle == &out.buf)
{
if (qse_str_ncat (&out.buf, data, len) == (qse_size_t)-1)
{
((Cut*)io)->setError (QSE_CUT_ENOMEM);
return -1;
}
return len;
}
else
{
QSE_ASSERT (handle != in.ptr);
return -1;
}
}
const StdCut::char_t* StdCut::StringStream::getInput (size_t* len) const
{
if (len) *len = in.end - in.ptr;
return in.ptr;
}
const StdCut::char_t* StdCut::StringStream::getOutput (size_t* len) const
{
if (out.inited)
{
if (len) *len = QSE_STR_LEN(&out.buf);
return QSE_STR_PTR(&out.buf);
}
else
{
if (len) *len = 0;
return QSE_T("");
}
}
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////

View File

@ -163,13 +163,13 @@ void qse_cut_clear (qse_cut_t* cut)
}
int qse_cut_comp (
qse_cut_t* cut, qse_cut_sel_id_t sel,
const qse_char_t* str, qse_size_t len,
qse_cut_t* cut, const qse_char_t* str, qse_size_t len,
qse_char_t din, qse_char_t dout)
{
const qse_char_t* p = str;
const qse_char_t* xnd = str + len;
qse_cint_t c;
int sel = QSE_SED_SEL_CHAR;
#define CC(x,y) (((x) <= (y))? ((qse_cint_t)*(x)): QSE_CHAR_EOF)
#define NC(x,y) (((x) < (y))? ((qse_cint_t)*(++(x))): QSE_CHAR_EOF)
@ -200,20 +200,17 @@ int qse_cut_comp (
break;
}
if (cut->option & QSE_CUT_HYBRIDSEL)
if (c == QSE_T('c'))
{
if (c == QSE_T('c'))
{
sel = QSE_CUT_SEL_CHAR;
c = NC (p, xnd);
while (QSE_ISSPACE(c)) c = NC (p, xnd);
}
else if (c == QSE_T('f'))
{
sel = QSE_CUT_SEL_FIELD;
c = NC (p, xnd);
while (QSE_ISSPACE(c)) c = NC (p, xnd);
}
sel = QSE_SED_SEL_CHAR;
c = NC (p, xnd);
while (QSE_ISSPACE(c)) c = NC (p, xnd);
}
else if (c == QSE_T('f'))
{
sel = QSE_SED_SEL_FIELD;
c = NC (p, xnd);
while (QSE_ISSPACE(c)) c = NC (p, xnd);
}
if (QSE_ISDIGIT(c))
@ -270,7 +267,7 @@ int qse_cut_comp (
cut->sel.lb->range[cut->sel.lb->len].end = end;
cut->sel.lb->len++;
cut->sel.count++;
if (sel == QSE_CUT_SEL_FIELD) cut->sel.fcount++;
if (sel == QSE_SED_SEL_FIELD) cut->sel.fcount++;
else cut->sel.ccount++;
if (EOF(c)) break;
@ -407,11 +404,13 @@ static int write_linebreak (qse_cut_t* cut)
static int write_str (qse_cut_t* cut, const qse_char_t* str, qse_size_t len)
{
qse_size_t i;
for (i = 0; i < len; i++)
{
if (write_char (cut, str[i]) <= -1) return -1;
}
return 0;
return 0;
}
static int cut_chars (
@ -431,7 +430,8 @@ static int cut_chars (
if (end >= len) end = len - 1;
if (delim && write_char (cut, cut->sel.dout) <= -1) return -1;
if (delim && write_char (cut, cut->sel.dout) <= -1)
return -1;
if (write_str (cut, &ptr[start], end-start+1) <= -1)
return -1;
@ -450,7 +450,8 @@ static int cut_chars (
if (start >= len) start = len - 1;
if (delim && write_char (cut, cut->sel.dout) <= -1) return -1;
if (delim && write_char (cut, cut->sel.dout) <= -1)
return -1;
for (i = start; i >= end; i--)
{
@ -547,14 +548,16 @@ static int cut_fields (
if (end >= len) end = len - 1;
if (delim && write_char (cut, cut->sel.dout) <= -1) return -1;
if (delim && write_char (cut, cut->sel.dout) <= -1)
return -1;
for (i = start; i <= end; i++)
{
if (write_str (cut, cut->e.in.flds[i].ptr, cut->e.in.flds[i].len) <= -1)
return -1;
if (i < end && write_char (cut, cut->sel.dout) <= -1) return -1;
if (i < end && write_char (cut, cut->sel.dout) <= -1)
return -1;
}
return 1;
@ -571,14 +574,16 @@ static int cut_fields (
if (start >= len) start = len - 1;
if (delim && write_char (cut, cut->sel.dout) <= -1) return -1;
if (delim && write_char (cut, cut->sel.dout) <= -1)
return -1;
for (i = start; i >= end; i--)
{
if (write_str (cut, cut->e.in.flds[i].ptr, cut->e.in.flds[i].len) <= -1)
return -1;
if (i > end && write_char (cut, cut->sel.dout) <= -1) return -1;
if (i > end && write_char (cut, cut->sel.dout) <= -1)
return -1;
}
return 1;
@ -658,7 +663,7 @@ int qse_cut_exec (qse_cut_t* cut, qse_cut_io_fun_t inf, qse_cut_io_fun_t outf)
for (i = 0; i < b->len; i++)
{
if (b->range[i].id == QSE_CUT_SEL_CHAR)
if (b->range[i].id == QSE_SED_SEL_CHAR)
{
n = cut_chars (
cut,

View File

@ -31,7 +31,11 @@ struct qse_cut_sel_blk_t
qse_size_t len;
struct
{
qse_cut_sel_id_t id;
enum
{
QSE_SED_SEL_CHAR,
QSE_SED_SEL_FIELD
} id;
qse_size_t start;
qse_size_t end;
} range[128];

210
qse/lib/cut/std.c Normal file
View File

@ -0,0 +1,210 @@
/*
* $Id: std.c 306 2009-11-22 13:58:53Z 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/>.
*/
#include "cut.h"
#include <qse/cut/std.h>
#include <qse/cmn/str.h>
#include <qse/cmn/sio.h>
#include "../cmn/mem.h"
struct xtn_t
{
const qse_char_t* infile;
const qse_char_t* outfile;
};
typedef struct xtn_t xtn_t;
qse_cut_t* qse_cut_openstd (qse_size_t xtnsize)
{
qse_cut_t* cut;
xtn_t* xtn;
/* create an object */
cut = qse_cut_open (
QSE_MMGR_GETDFL(), QSE_SIZEOF(xtn_t) + xtnsize);
if (cut == QSE_NULL) return QSE_NULL;
/* initialize extension */
xtn = (xtn_t*) QSE_XTN (cut);
QSE_MEMSET (xtn, 0, QSE_SIZEOF(xtn_t));
return cut;
}
void* qse_cut_getxtnstd (qse_cut_t* cut)
{
return (void*)((xtn_t*)QSE_XTN(cut) + 1);
}
int qse_cut_compstd (qse_cut_t* cut, const qse_char_t* sptr, qse_char_t din, qse_char_t dout)
{
return qse_cut_comp (cut, sptr, qse_strlen(sptr), din, dout);
}
static qse_ssize_t xin (
qse_cut_t* cut, qse_cut_io_cmd_t cmd, qse_cut_io_arg_t* arg,
qse_char_t* buf, qse_size_t len)
{
qse_sio_t* sio;
xtn_t* xtn = (xtn_t*) QSE_XTN (cut);
switch (cmd)
{
case QSE_CUT_IO_OPEN:
{
/* main data stream */
if (xtn->infile == QSE_NULL) sio = qse_sio_in;
else
{
sio = qse_sio_open (
cut->mmgr,
0,
xtn->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 = xtn->infile;
ea.len = qse_strlen (xtn->infile);
qse_cut_seterrnum (cut,QSE_CUT_EIOFIL, &ea);
return -1;
}
}
if (sio == QSE_NULL) return -1;
arg->handle = sio;
return 1;
}
case QSE_CUT_IO_CLOSE:
{
sio = (qse_sio_t*)arg->handle;
if (sio != qse_sio_in && sio != qse_sio_out && sio != qse_sio_err)
qse_sio_close (sio);
return 0;
}
case QSE_CUT_IO_READ:
{
qse_ssize_t n = qse_sio_getsn (arg->handle, buf, len);
if (n == -1)
{
if (xtn->infile != QSE_NULL)
{
qse_cstr_t ea;
ea.ptr = xtn->infile;
ea.len = qse_strlen (xtn->infile);
qse_cut_seterrnum (cut, QSE_CUT_EIOFIL, &ea);
}
}
return n;
}
default:
return -1;
}
}
static qse_ssize_t xout (
qse_cut_t* cut, qse_cut_io_cmd_t cmd, qse_cut_io_arg_t* arg,
qse_char_t* dat, qse_size_t len)
{
qse_sio_t* sio;
xtn_t* xtn = (xtn_t*) QSE_XTN (cut);
switch (cmd)
{
case QSE_CUT_IO_OPEN:
{
if (xtn->outfile == QSE_NULL) sio = qse_sio_out;
else
{
sio = qse_sio_open (
cut->mmgr,
0,
xtn->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 = xtn->outfile;
ea.len = qse_strlen (xtn->outfile);
qse_cut_seterrnum (cut, QSE_CUT_EIOFIL, &ea);
return -1;
}
}
if (sio == QSE_NULL) return -1;
arg->handle = sio;
return 1;
}
case QSE_CUT_IO_CLOSE:
{
sio = (qse_sio_t*)arg->handle;
qse_sio_flush (sio);
if (sio != qse_sio_in && sio != qse_sio_out && sio != qse_sio_err)
qse_sio_close (sio);
return 0;
}
case QSE_CUT_IO_WRITE:
{
qse_ssize_t n = qse_sio_putsn (arg->handle, dat, len);
if (n == -1)
{
if (xtn->infile != QSE_NULL)
{
qse_cstr_t ea;
ea.ptr = xtn->infile;
ea.len = qse_strlen (xtn->infile);
qse_cut_seterrnum (cut, QSE_CUT_EIOFIL, &ea);
}
}
return n;
}
default:
return -1;
}
}
int qse_cut_execstd (qse_cut_t* cut, const qse_char_t* infile, const qse_char_t* outfile)
{
xtn_t* xtn = (xtn_t*) QSE_XTN (cut);
xtn->infile = infile;
xtn->outfile = outfile;
return qse_cut_exec (cut, xin, xout);
}

View File

@ -1,8 +1,9 @@
AUTOMAKE_OPTIONS = nostdinc
AM_CPPFLAGS = -I$(top_srcdir)/include
lib_LTLIBRARIES = libqsesed.la
libqsesed_la_SOURCES = sed.c err.c sed.h
libqsesed_la_SOURCES = sed.c err.c sed.h std.c
libqsesed_la_LDFLAGS = -version-info 1:0:0 -no-undefined -L../cmn
libqsesed_la_LIBADD = -lqsecmn

View File

@ -73,7 +73,7 @@ am__base_list = \
am__installdirs = "$(DESTDIR)$(libdir)"
LTLIBRARIES = $(lib_LTLIBRARIES)
libqsesed_la_DEPENDENCIES =
am_libqsesed_la_OBJECTS = sed.lo err.lo
am_libqsesed_la_OBJECTS = sed.lo err.lo std.lo
libqsesed_la_OBJECTS = $(am_libqsesed_la_OBJECTS)
libqsesed_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
@ -86,7 +86,7 @@ libqsesedxx_la_LINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CXXLD) $(AM_CXXFLAGS) \
$(CXXFLAGS) $(libqsesedxx_la_LDFLAGS) $(LDFLAGS) -o $@
@ENABLE_CXX_TRUE@am_libqsesedxx_la_rpath = -rpath $(libdir)
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include/qse
DEFAULT_INCLUDES =
depcomp = $(SHELL) $(top_srcdir)/ac/au/depcomp
am__depfiles_maybe = depfiles
am__mv = mv -f
@ -253,9 +253,10 @@ target_alias = @target_alias@
top_build_prefix = @top_build_prefix@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
AUTOMAKE_OPTIONS = nostdinc
AM_CPPFLAGS = -I$(top_srcdir)/include
lib_LTLIBRARIES = libqsesed.la $(am__append_1)
libqsesed_la_SOURCES = sed.c err.c sed.h
libqsesed_la_SOURCES = sed.c err.c sed.h std.c
libqsesed_la_LDFLAGS = -version-info 1:0:0 -no-undefined -L../cmn
libqsesed_la_LIBADD = -lqsecmn
@ENABLE_CXX_TRUE@libqsesedxx_la_SOURCES = Sed.cpp StdSed.cpp
@ -341,6 +342,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/StdSed.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/err.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sed.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/std.Plo@am__quote@
.c.o:
@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<

View File

@ -1,5 +1,5 @@
/*
* $Id: StdSed.cpp 319 2009-12-19 03:06:28Z hyunghwan.chung $
* $Id: StdSed.cpp 320 2009-12-21 12:29:52Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -297,9 +297,8 @@ const StdSed::char_t* StdSed::StringStream::getOutput (size_t* len) const
}
else
{
static char_t empty[] = QSE_T("");
if (len) *len = 0;
return empty;
return QSE_T("");
}
}

236
qse/lib/sed/std.c Normal file
View File

@ -0,0 +1,236 @@
/*
* $Id: std.c 306 2009-11-22 13:58:53Z 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/>.
*/
#include "sed.h"
#include <qse/sed/std.h>
#include <qse/cmn/str.h>
#include <qse/cmn/sio.h>
#include "../cmn/mem.h"
struct xtn_t
{
const qse_char_t* infile;
const qse_char_t* outfile;
};
typedef struct xtn_t xtn_t;
qse_sed_t* qse_sed_openstd (qse_size_t xtnsize)
{
qse_sed_t* sed;
xtn_t* xtn;
/* create an object */
sed = qse_sed_open (
QSE_MMGR_GETDFL(), QSE_SIZEOF(xtn_t) + xtnsize);
if (sed == QSE_NULL) return QSE_NULL;
/* initialize extension */
xtn = (xtn_t*) QSE_XTN (sed);
QSE_MEMSET (xtn, 0, QSE_SIZEOF(xtn_t));
return sed;
}
void* qse_sed_getxtnstd (qse_sed_t* sed)
{
return (void*)((xtn_t*)QSE_XTN(sed) + 1);
}
int qse_sed_compstd (qse_sed_t* sed, const qse_char_t* sptr)
{
return qse_sed_comp (sed, sptr, qse_strlen(sptr));
}
static qse_ssize_t xin (
qse_sed_t* sed, qse_sed_io_cmd_t cmd, qse_sed_io_arg_t* arg,
qse_char_t* buf, qse_size_t len)
{
qse_sio_t* sio;
xtn_t* xtn = (xtn_t*) QSE_XTN (sed);
switch (cmd)
{
case QSE_SED_IO_OPEN:
{
if (arg->path == QSE_NULL)
{
/* main data stream */
if (xtn->infile == QSE_NULL) sio = qse_sio_in;
else
{
sio = qse_sio_open (
sed->mmgr,
0,
xtn->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 = xtn->infile;
ea.len = qse_strlen (xtn->infile);
qse_sed_seterrnum (sed,QSE_SED_EIOFIL, &ea);
return -1;
}
}
}
else
{
sio = qse_sio_open (
sed->mmgr,
0,
arg->path,
QSE_SIO_READ
);
}
if (sio == QSE_NULL) return -1;
arg->handle = sio;
return 1;
}
case QSE_SED_IO_CLOSE:
{
sio = (qse_sio_t*)arg->handle;
if (sio != qse_sio_in && sio != qse_sio_out && sio != qse_sio_err)
qse_sio_close (sio);
return 0;
}
case QSE_SED_IO_READ:
{
qse_ssize_t n = qse_sio_getsn (arg->handle, buf, len);
if (n == -1)
{
if (arg->path == QSE_NULL && xtn->infile != QSE_NULL)
{
qse_cstr_t ea;
ea.ptr = xtn->infile;
ea.len = qse_strlen (xtn->infile);
qse_sed_seterrnum (sed, QSE_SED_EIOFIL, &ea);
}
}
return n;
}
default:
return -1;
}
}
static qse_ssize_t xout (
qse_sed_t* sed, qse_sed_io_cmd_t cmd, qse_sed_io_arg_t* arg,
qse_char_t* dat, qse_size_t len)
{
qse_sio_t* sio;
xtn_t* xtn = (xtn_t*) QSE_XTN (sed);
switch (cmd)
{
case QSE_SED_IO_OPEN:
{
if (arg->path == QSE_NULL)
{
if (xtn->outfile == QSE_NULL) sio = qse_sio_out;
else
{
sio = qse_sio_open (
sed->mmgr,
0,
xtn->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 = xtn->outfile;
ea.len = qse_strlen (xtn->outfile);
qse_sed_seterrnum (sed, QSE_SED_EIOFIL, &ea);
return -1;
}
}
}
else
{
sio = qse_sio_open (
sed->mmgr,
0,
arg->path,
QSE_SIO_WRITE |
QSE_SIO_CREATE |
QSE_SIO_TRUNCATE
);
}
if (sio == QSE_NULL) return -1;
arg->handle = sio;
return 1;
}
case QSE_SED_IO_CLOSE:
{
sio = (qse_sio_t*)arg->handle;
qse_sio_flush (sio);
if (sio != qse_sio_in && sio != qse_sio_out && sio != qse_sio_err)
qse_sio_close (sio);
return 0;
}
case QSE_SED_IO_WRITE:
{
qse_ssize_t n = qse_sio_putsn (arg->handle, dat, len);
if (n == -1)
{
if (arg->path == QSE_NULL && xtn->infile != QSE_NULL)
{
qse_cstr_t ea;
ea.ptr = xtn->infile;
ea.len = qse_strlen (xtn->infile);
qse_sed_seterrnum (sed, QSE_SED_EIOFIL, &ea);
}
}
return n;
}
default:
return -1;
}
}
int qse_sed_execstd (qse_sed_t* sed, const qse_char_t* infile, const qse_char_t* outfile)
{
xtn_t* xtn = (xtn_t*) QSE_XTN (sed);
xtn->infile = infile;
xtn->outfile = outfile;
return qse_sed_exec (sed, xin, xout);
}