- 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.h 316 2009-12-14 12:50:11Z hyunghwan.chung $
* $Id: std.h 320 2009-12-21 12:29:52Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -122,7 +122,6 @@ int qse_awk_parsestd (
const qse_awk_parsestd_in_t* in,
qse_awk_parsestd_out_t* out
);
/******/
/**
* The qse_awk_rtx_openstd() function creates a standard runtime context.

View File

@ -1,5 +1,5 @@
/*
* $Id: str.h 297 2009-10-08 13:09:19Z hyunghwan.chung $
* $Id: str.h 320 2009-12-21 12:29:52Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -275,6 +275,12 @@ qse_char_t* qse_strdup (
qse_mmgr_t* mmgr
);
qse_char_t* qse_strdup2 (
const qse_char_t* str1,
const qse_char_t* str2,
qse_mmgr_t* mmgr
);
qse_char_t* qse_strxdup (
const qse_char_t* str,
qse_size_t len,

242
qse/include/qse/cut/Cut.hpp Normal file
View File

@ -0,0 +1,242 @@
/*
* $Id: Cut.hpp 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/>.
*/
#ifndef _QSE_CUT_CUT_HPP_
#define _QSE_CUT_CUT_HPP_
#include <qse/cmn/Mmged.hpp>
#include <qse/cut/cut.h>
/** @file
* Stream Editor
*/
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
/**
* The Cut class implements a stream editor by wrapping around #qse_cut_t.
*/
class Cut: public Mmged
{
public:
/// The cut_t type redefines a stream editor type
typedef qse_cut_t cut_t;
/// The errnum_t type redefines an error number type
typedef qse_cut_errnum_t errnum_t;
/// The errstr_t type redefines an error formattering string getter type
typedef qse_cut_errstr_t errstr_t;
/// The io_cmd_t type redefines an IO command type
typedef qse_cut_io_cmd_t io_cmd_t;
/// The io_arg_t type redefines an IO data type
typedef qse_cut_io_arg_t io_arg_t;
/// The option_t type redefines an option type
typedef qse_cut_option_t option_t;
///
/// The Stream class is a base class for I/O operation during
/// execution.
///
class Stream: public Types
{
public:
enum Mode
{
READ, ///< open for read
WRITE ///< open for write
};
class Data
{
public:
friend class Cut;
protected:
Data (Cut* cut, Mode mode, io_arg_t* arg):
cut (cut), mode (mode), arg (arg) {}
public:
Mode getMode() const { return mode; }
void* getHandle () const { return arg->handle; }
void setHandle (void* handle) { arg->handle = handle; }
operator Cut* () const { return cut; }
operator cut_t* () const { return cut->cut; }
protected:
Cut* cut;
Mode mode;
io_arg_t* arg;
};
Stream () {}
virtual ~Stream () {}
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:
Stream (const Stream&);
Stream& operator= (const Stream&);
};
///
/// The Cut() function creates an uninitialized stream editor.
///
Cut (Mmgr* mmgr): Mmged (mmgr), cut (QSE_NULL), dflerrstr (QSE_NULL)
{
delim.in = QSE_T(' ');
delim.out = delim.in;
}
///
/// The ~Cut() function destroys a stream editor.
/// @note The close() function is not called by this destructor.
/// To avoid resource leaks, You should call close() before
/// a stream editor is destroyed if it has been initialized
/// with open().
///
virtual ~Cut () {}
///
/// The open() function initializes a stream editor and makes it
/// ready for subsequent use.
/// @return 0 on success, -1 on failure.
///
int open ();
///
/// The close() function finalizes a stream editor.
///
void close ();
char_t getInputDelimiter () const { return delim.in; }
void setInputDelimiter (char_t delimc) { delim.in = delimc; }
char_t getOutputDelimiter () const { return delim.out; }
void setOutputDelimiter (char_t delimc) { delim.out = delimc; }
///
/// The compile() function compiles a null-terminated string pointed
/// to by @a sptr.
/// @return 0 on success, -1 on failure
///
int compile (
const char_t* sptr ///< a pointer to a null-terminated string
);
///
/// The compile() function compiles a string pointed to by @a sptr
/// and of the length @a slen.
/// @return 0 on success, -1 on failure
///
int compile (
const char_t* sptr, ///< a pointer to a string
size_t slen ///< the number of characters in the string
);
///
/// The execute() function executes compiled commands over the I/O
/// streams defined through I/O handlers
/// @return 0 on success, -1 on failure
///
int execute (Stream& iostream);
///
/// The getOption() function gets the current options.
/// @return 0 or current options ORed of #option_t enumerators.
///
int getOption () const;
///
/// The setOption() function sets options for a stream editor.
/// The option code @a opt is 0 or OR'ed of #option_t enumerators.
///
void setOption (
int opt ///< option code
);
///
/// The getErrorMessage() function gets the description of the last
/// error occurred. It returns an empty string if the stream editor
/// has not been initialized with the open() function.
///
const char_t* getErrorMessage() const;
///
/// The getErrorNumber() function gets the number of the last
/// error occurred. It returns QSE_CUT_ENOERR if the stream editor
/// has not been initialized with the open() function.
///
errnum_t getErrorNumber () const;
///
/// The setError() function sets information on an error occurred.
///
void setError (
errnum_t num, ///< error number
const cstr_t* args = QSE_NULL ///< string array for formatting
/// an error message
);
protected:
///
/// The getErrorString() function returns an error formatting string
/// for the error number @a num. A subclass wishing to customize
/// an error formatting string may override this function.
///
virtual const char_t* getErrorString (
errnum_t num ///< an error number
) const;
protected:
/// handle to a primitive cut object
cut_t* cut;
/// default error formatting string getter
errstr_t dflerrstr;
/// I/O stream to read data from and write output to.
Stream* iostream;
struct
{
char_t in;
char_t out;
} delim;
private:
static ssize_t xin (
cut_t* s, io_cmd_t cmd, io_arg_t* arg, char_t* buf, size_t len);
static ssize_t xout (
cut_t* s, io_cmd_t cmd, io_arg_t* arg, char_t* dat, size_t len);
static const char_t* xerrstr (cut_t* s, errnum_t num);
private:
Cut (const Cut&);
Cut& operator= (const Cut&);
};
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////
#endif

View File

@ -1,8 +1,7 @@
pkgincludedir= $(includedir)/qse/cut
pkginclude_HEADERS = cut.h
#if ENABLE_CXX
#pkginclude_HEADERS += Cut.hpp StdCut.hpp
#endif
pkginclude_HEADERS = cut.h std.h
if ENABLE_CXX
pkginclude_HEADERS += Cut.hpp StdCut.hpp
endif

View File

@ -33,8 +33,9 @@ PRE_UNINSTALL = :
POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
@ENABLE_CXX_TRUE@am__append_1 = Cut.hpp StdCut.hpp
subdir = include/qse/cut
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,7 @@ CONFIG_CLEAN_FILES =
CONFIG_CLEAN_VPATH_FILES =
SOURCES =
DIST_SOURCES =
am__pkginclude_HEADERS_DIST = cut.h std.h Cut.hpp StdCut.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,7 +218,7 @@ target_alias = @target_alias@
top_build_prefix = @top_build_prefix@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
pkginclude_HEADERS = cut.h
pkginclude_HEADERS = cut.h std.h $(am__append_1)
all: all-am
.SUFFIXES:
@ -473,10 +475,6 @@ uninstall-am: uninstall-pkgincludeHEADERS
tags uninstall uninstall-am uninstall-pkgincludeHEADERS
#if ENABLE_CXX
#pkginclude_HEADERS += Cut.hpp StdCut.hpp
#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,100 @@
/*
* $Id: StdCut.hpp 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/>.
*/
#ifndef _QSE_CUT_STDCUT_HPP_
#define _QSE_CUT_STDCUT_HPP_
#include <qse/cut/Cut.hpp>
#include <qse/cmn/StdMmgr.hpp>
#include <qse/cmn/str.h>
/** @file
* Standard Text Cutter
*/
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
/**
* The StdCut class inherits the Cut class, implements a standard
* I/O stream class, and sets the default memory manager.
*
*/
class StdCut: public Cut
{
public:
StdCut (Mmgr* mmgr = &StdMmgr::DFL): Cut (mmgr) {}
class FileStream: public Stream
{
public:
FileStream (const char_t* infile = QSE_NULL,
const char_t* outfile = QSE_NULL):
infile(infile), outfile(outfile)
{
}
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;
};
class StringStream: public Stream
{
public:
StringStream (const char_t* in);
StringStream (const char_t* in, size_t len);
~StringStream ();
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);
const char_t* getInput (size_t* len = QSE_NULL) const;
const char_t* getOutput (size_t* len = QSE_NULL) const;
protected:
struct
{
const char_t* ptr;
const char_t* end;
const char_t* cur;
} in;
struct
{
bool inited;
qse_str_t buf;
} out;
};
};
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////
#endif

View File

@ -48,7 +48,7 @@ enum qse_cut_errnum_t
QSE_CUT_ENOERR, /**< no error */
QSE_CUT_ENOMEM, /**< insufficient memory */
QSE_CUT_ESELNV, /**< selector not valid */
QSE_SED_EIOFIL, /**< io error with file '${0}'*/
QSE_CUT_EIOFIL, /**< io error with file '${0}'*/
QSE_CUT_EIOUSR /**< error returned by user io handler */
};
typedef enum qse_cut_errnum_t qse_cut_errnum_t;
@ -75,29 +75,21 @@ enum qse_cut_option_t
/** show delimited line only. if not set, undelimited lines are
* shown in its entirety */
QSE_CUT_DELIMONLY = (1 << 0),
/** support mixing of c and f selectors */
QSE_CUT_HYBRIDSEL = (1 << 1),
/** treat any whitespaces as an input delimiter */
QSE_CUT_WHITESPACE = (1 << 2),
/** fold adjacent delimiters */
QSE_CUT_FOLDDELIMS = (1 << 3),
/** trim leading and trailing whitespaces off the input line */
QSE_CUT_TRIMSPACE = (1 << 4),
/** normalize whitespaces in the input line */
QSE_CUT_NORMSPACE = (1 << 5)
};
typedef enum qse_cut_option_t qse_cut_option_t;
/**
* The qse_cut_sel_id_t type defines selector types.
*/
enum qse_cut_sel_id_t
{
QSE_CUT_SEL_CHAR, /**< character */
QSE_CUT_SEL_FIELD /**< field */
};
typedef enum qse_cut_sel_id_t qse_cut_sel_id_t;
/**
* The qse_cut_io_cmd_t type defines I/O command codes. The code indicates
* the action to take in an I/O handler.
@ -260,7 +252,6 @@ void qse_cut_clear (
*/
int qse_cut_comp (
qse_cut_t* cut, /**< text cutter */
qse_cut_sel_id_t sel, /**< initial selector type */
const qse_char_t* str, /**< selector pointer */
qse_size_t len, /**< selector length */
qse_char_t din, /**< input field delimiter */

78
qse/include/qse/cut/std.h Normal file
View File

@ -0,0 +1,78 @@
/*
* $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/>.
*/
#ifndef _QSE_CUT_STD_H_
#define _QSE_CUT_STD_H_
#include <qse/cut/cut.h>
/** @file
* This file defines a simple text cutter.
*/
#ifdef __cplusplus
extern "C" {
#endif
/**
* The qse_cut_openstd() function creates a text cutter.
*/
qse_cut_t* qse_cut_openstd (
qse_size_t xtnsize /**< size of extension in bytes */
);
/**
* The qse_cut_getxtnstd() gets the pointer to extension space.
* Note that you must not call qse_cut_getxtn() for a text cutter
* created with qse_cut_openstd().
*/
void* qse_cut_getxtnstd (
qse_cut_t* cut
);
/**
* The qse_cut_compstd() function compiles a null-terminated selector.
* Call qse_cut_comp() for a length delimited selector.
*/
int qse_cut_compstd (
qse_cut_t* cut,
const qse_char_t* str,
qse_char_t din,
qse_char_t dout
);
/**
* The qse_cut_execstd() function executes the compiled script
* over an input file @a infile and an output file @a outfile.
* If @infile is QSE_NULL, the standard console input is used.
* If @outfile is QSE_NULL, the standard console output is used..
*/
int qse_cut_execstd (
qse_cut_t* cut,
const qse_char_t* infile,
const qse_char_t* outfile
);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,6 +1,6 @@
pkgincludedir= $(includedir)/qse/sed
pkginclude_HEADERS = sed.h
pkginclude_HEADERS = sed.h std.h
if ENABLE_CXX
pkginclude_HEADERS += Sed.hpp StdSed.hpp

View File

@ -51,7 +51,7 @@ CONFIG_CLEAN_FILES =
CONFIG_CLEAN_VPATH_FILES =
SOURCES =
DIST_SOURCES =
am__pkginclude_HEADERS_DIST = sed.h Sed.hpp StdSed.hpp
am__pkginclude_HEADERS_DIST = sed.h std.h Sed.hpp StdSed.hpp
am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
am__vpath_adj = case $$p in \
$(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \
@ -218,7 +218,7 @@ target_alias = @target_alias@
top_build_prefix = @top_build_prefix@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
pkginclude_HEADERS = sed.h $(am__append_1)
pkginclude_HEADERS = sed.h std.h $(am__append_1)
all: all-am
.SUFFIXES:

View File

@ -1,5 +1,5 @@
/*
* $Id: Sed.hpp 319 2009-12-19 03:06:28Z hyunghwan.chung $
* $Id: Sed.hpp 320 2009-12-21 12:29:52Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
@ -62,12 +62,14 @@ public:
class Stream: public Types
{
public:
/// The Mode type defines I/O modes.
enum Mode
{
READ, ///< open for read
WRITE ///< open for write
};
/// The Data class conveys information need for I/O operations.
class Data
{
public:
@ -75,40 +77,32 @@ public:
protected:
Data (Sed* sed, Mode mode, io_arg_t* arg):
sed (sed), mode (mode), arg (arg)
{
}
sed (sed), mode (mode), arg (arg) {}
public:
Mode getMode() const
{
return mode;
}
/// The getMode() function returns the I/O mode
/// requested.
Mode getMode() const { return mode; }
void* getHandle () const
{
return arg->handle;
}
/// The getHandle() function returns the I/O handle
/// saved by setHandle().
void* getHandle () const { return arg->handle; }
void setHandle (void* handle)
{
arg->handle = handle;
}
/// The setHandle() function sets an I/O handle
/// typically in the Stream::open() function.
void setHandle (void* handle) { arg->handle = handle; }
const char_t* getName () const
{
return arg->path;
}
/// The getName() function returns an I/O name.
/// @return QSE_NULL for the main data stream,
/// file path for explicit file stream
const char_t* getName () const { return arg->path; }
operator Sed* () const
{
return sed;
}
/// The Sed* operator returns the associated Sed class.
operator Sed* () const { return sed; }
operator sed_t* () const
{
return sed->sed;
}
/// The sed_t* operator returns a pointer to the
/// underlying stream editor.
operator sed_t* () const { return sed->sed; }
protected:
Sed* sed;
@ -116,11 +110,34 @@ public:
io_arg_t* arg;
};
/// The Stream() function constructs a stream.
Stream () {}
/// The Stream() function destructs a stream.
virtual ~Stream () {}
/// The open() function should be implemented by a subclass
/// to open a stream. It can get the mode requested by calling
/// the Data::getMode() function over the I/O parameter @a io.
///
/// The return value of 0 may look a bit tricky. Easygoers
/// can just return 1 on success and never return 0 from open().
/// - If 0 is returned for the #READ mode, Sed::execute()
/// returns success after having called close() as it has
/// opened a console but has reached EOF.
/// - If 0 is returned for the #WRITE mode and there are
/// any write() calls, the Sed::execute() function returns
/// failure after having called close() as it cannot write
/// further on EOF.
///
/// @return -1 on failure, 1 on success,
/// 0 on success but reached EOF.
virtual int open (Data& io) = 0;
/// The close() function should be implemented by a subclass
/// to open a stream.
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;
@ -258,10 +275,6 @@ public:
size_t num ///< a line number
);
///
/// The getMmgr() function returns the memory manager associated.
///
protected:
///
/// The getErrorString() function returns an error formatting string

76
qse/include/qse/sed/std.h Normal file
View File

@ -0,0 +1,76 @@
/*
* $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/>.
*/
#ifndef _QSE_SED_STD_H_
#define _QSE_SED_STD_H_
#include <qse/sed/sed.h>
/** @file
* This file defines a simple stream editor.
*/
#ifdef __cplusplus
extern "C" {
#endif
/**
* The qse_sed_openstd() function creates a stream editor.
*/
qse_sed_t* qse_sed_openstd (
qse_size_t xtnsize /**< size of extension in bytes */
);
/**
* The qse_sed_getxtnstd() gets the pointer to extension space.
* Note that you must not call qse_sed_getxtn() for a stream editor
* created with qse_sed_openstd().
*/
void* qse_sed_getxtnstd (
qse_sed_t* sed
);
/**
* The qse_sed_compstd() function compiles a null-terminated sed script.
* Call qse_sed_comp() for a length delimited script.
*/
int qse_sed_compstd (
qse_sed_t* sed,
const qse_char_t* str
);
/**
* The qse_sed_execstd() function executes the compiled script
* over an input file @a infile and an output file @a outfile.
* If @infile is QSE_NULL, the standard console input is used.
* If @outfile is QSE_NULL, the standard console output is used.
*/
int qse_sed_execstd (
qse_sed_t* sed,
const qse_char_t* infile,
const qse_char_t* outfile
);
#ifdef __cplusplus
}
#endif
#endif