diff --git a/qse/cmd/cut/cut.c b/qse/cmd/cut/cut.c index fc9590f2..d16eaf15 100644 --- a/qse/cmd/cut/cut.c +++ b/qse/cmd/cut/cut.c @@ -28,7 +28,6 @@ #include #include -static qse_cut_sel_id_t g_selector_id = QSE_CUT_SEL_CHAR; static qse_char_t* g_selector = QSE_NULL; static int g_infile_start = 0; @@ -204,31 +203,27 @@ static int handle_args (int argc, qse_char_t* argv[]) return 0; case QSE_T('c'): - if (g_selector != QSE_NULL) - { - qse_fprintf (QSE_STDERR, - QSE_T("ERROR: both -c and -f specified\n")); - print_usage (QSE_STDERR, argc, argv); - return -1; - } - - g_selector = opt.arg; - g_selector_id = QSE_CUT_SEL_CHAR; - break; - case QSE_T('f'): + { + qse_char_t x[2] = QSE_T(" "); + if (g_selector != QSE_NULL) { qse_fprintf (QSE_STDERR, - QSE_T("ERROR: both -c and -f specified\n")); + QSE_T("ERROR: multiple selectors specified\n")); print_usage (QSE_STDERR, argc, argv); return -1; } - g_selector = opt.arg; - g_selector_id = QSE_CUT_SEL_FIELD; + x[0] = c; + g_selector = qse_strdup2 (x, opt.arg, QSE_MMGR_GETDFL()); + if (g_selector == QSE_NULL) + { + qse_fprintf (QSE_STDERR, QSE_T("ERROR: insufficient memory\n")); + return -1; + } break; - + } case QSE_T('d'): if (qse_strlen(opt.arg) > 1) @@ -296,7 +291,7 @@ static int handle_args (int argc, qse_char_t* argv[]) return -1; } - if (g_selector_id == QSE_CUT_SEL_CHAR && + if (g_selector[0] == QSE_T('c') && (g_din != QSE_CHAR_EOF || g_dout != QSE_CHAR_EOF || (g_option & QSE_CUT_WHITESPACE) || (g_option & QSE_CUT_FOLDDELIMS))) { @@ -314,7 +309,7 @@ static int handle_args (int argc, qse_char_t* argv[]) return -1; } - if (g_selector_id == QSE_CUT_SEL_CHAR && + if (g_selector[0] == QSE_T('f') && (g_option & QSE_CUT_DELIMONLY)) { qse_fprintf (QSE_STDERR, @@ -349,8 +344,7 @@ int cut_main (int argc, qse_char_t* argv[]) if (g_din == QSE_CHAR_EOF) g_din = QSE_T('\t'); if (g_dout == QSE_CHAR_EOF) g_dout = g_din; - if (qse_cut_comp (cut, g_selector_id, - g_selector, qse_strlen(g_selector), g_din, g_dout) == -1) + if (qse_cut_comp (cut, g_selector, qse_strlen(g_selector), g_din, g_dout) == -1) { qse_fprintf (QSE_STDERR, QSE_T("cannot compile - %s\n"), @@ -393,6 +387,7 @@ int cut_main (int argc, qse_char_t* argv[]) oops: if (cut != QSE_NULL) qse_cut_close (cut); + if (g_selector != QSE_NULL) QSE_MMGR_FREE (QSE_MMGR_GETDFL(), g_selector); return ret; } diff --git a/qse/include/qse/awk/std.h b/qse/include/qse/awk/std.h index 085168b6..4985bdaa 100644 --- a/qse/include/qse/awk/std.h +++ b/qse/include/qse/awk/std.h @@ -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. diff --git a/qse/include/qse/cmn/str.h b/qse/include/qse/cmn/str.h index ad687f5a..4ec5716e 100644 --- a/qse/include/qse/cmn/str.h +++ b/qse/include/qse/cmn/str.h @@ -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, diff --git a/qse/include/qse/cut/Cut.hpp b/qse/include/qse/cut/Cut.hpp new file mode 100644 index 00000000..499aec33 --- /dev/null +++ b/qse/include/qse/cut/Cut.hpp @@ -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 . + */ + +#ifndef _QSE_CUT_CUT_HPP_ +#define _QSE_CUT_CUT_HPP_ + +#include +#include + +/** @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 diff --git a/qse/include/qse/cut/Makefile.am b/qse/include/qse/cut/Makefile.am index 33c0d5a9..f309df63 100644 --- a/qse/include/qse/cut/Makefile.am +++ b/qse/include/qse/cut/Makefile.am @@ -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 diff --git a/qse/include/qse/cut/Makefile.in b/qse/include/qse/cut/Makefile.in index c6587360..6cd966d1 100644 --- a/qse/include/qse/cut/Makefile.in +++ b/qse/include/qse/cut/Makefile.in @@ -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: diff --git a/qse/include/qse/cut/StdCut.hpp b/qse/include/qse/cut/StdCut.hpp new file mode 100644 index 00000000..193a8c53 --- /dev/null +++ b/qse/include/qse/cut/StdCut.hpp @@ -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 . + */ + +#ifndef _QSE_CUT_STDCUT_HPP_ +#define _QSE_CUT_STDCUT_HPP_ + +#include +#include +#include + +/** @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 diff --git a/qse/include/qse/cut/cut.h b/qse/include/qse/cut/cut.h index 4179a643..027f28da 100644 --- a/qse/include/qse/cut/cut.h +++ b/qse/include/qse/cut/cut.h @@ -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 */ diff --git a/qse/include/qse/cut/std.h b/qse/include/qse/cut/std.h new file mode 100644 index 00000000..8eb45c3a --- /dev/null +++ b/qse/include/qse/cut/std.h @@ -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 . + */ + +#ifndef _QSE_CUT_STD_H_ +#define _QSE_CUT_STD_H_ + +#include + +/** @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 diff --git a/qse/include/qse/sed/Makefile.am b/qse/include/qse/sed/Makefile.am index b1ab0bc5..5db64b0e 100644 --- a/qse/include/qse/sed/Makefile.am +++ b/qse/include/qse/sed/Makefile.am @@ -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 diff --git a/qse/include/qse/sed/Makefile.in b/qse/include/qse/sed/Makefile.in index f0ef8c48..b01df71f 100644 --- a/qse/include/qse/sed/Makefile.in +++ b/qse/include/qse/sed/Makefile.in @@ -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: diff --git a/qse/include/qse/sed/Sed.hpp b/qse/include/qse/sed/Sed.hpp index 330d6406..1e3b4985 100644 --- a/qse/include/qse/sed/Sed.hpp +++ b/qse/include/qse/sed/Sed.hpp @@ -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 diff --git a/qse/include/qse/sed/std.h b/qse/include/qse/sed/std.h new file mode 100644 index 00000000..84b725d2 --- /dev/null +++ b/qse/include/qse/sed/std.h @@ -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 . + */ + +#ifndef _QSE_SED_STD_H_ +#define _QSE_SED_STD_H_ + +#include + +/** @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 diff --git a/qse/lib/awk/std.c b/qse/lib/awk/std.c index 494ed77b..be78a25d 100644 --- a/qse/lib/awk/std.c +++ b/qse/lib/awk/std.c @@ -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 #include #include /* TODO: remove dependency on qse_vsprintf */ +#include "../cmn/mem.h" #include #include diff --git a/qse/lib/cmn/str_bas.c b/qse/lib/cmn/str_bas.c index 01f1f1a3..37b1735e 100644 --- a/qse/lib/cmn/str_bas.c +++ b/qse/lib/cmn/str_bas.c @@ -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) { diff --git a/qse/lib/cut/Cut.cpp b/qse/lib/cut/Cut.cpp new file mode 100644 index 00000000..6e4c5be2 --- /dev/null +++ b/qse/lib/cut/Cut.cpp @@ -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 . + */ + +#include +#include +#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) +///////////////////////////////// + diff --git a/qse/lib/cut/Makefile.am b/qse/lib/cut/Makefile.am index e29821a2..f37c9507 100644 --- a/qse/lib/cut/Makefile.am +++ b/qse/lib/cut/Makefile.am @@ -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 diff --git a/qse/lib/cut/Makefile.in b/qse/lib/cut/Makefile.in index 0677a1e3..1e1a51a7 100644 --- a/qse/lib/cut/Makefile.in +++ b/qse/lib/cut/Makefile.in @@ -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: diff --git a/qse/lib/cut/StdCut.cpp b/qse/lib/cut/StdCut.cpp new file mode 100644 index 00000000..2e2f5919 --- /dev/null +++ b/qse/lib/cut/StdCut.cpp @@ -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 . + */ + +#include +#include +#include +#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) +///////////////////////////////// diff --git a/qse/lib/cut/cut.c b/qse/lib/cut/cut.c index c17b9e9d..5eef1ffc 100644 --- a/qse/lib/cut/cut.c +++ b/qse/lib/cut/cut.c @@ -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, diff --git a/qse/lib/cut/cut.h b/qse/lib/cut/cut.h index 1b26cd0e..96c37625 100644 --- a/qse/lib/cut/cut.h +++ b/qse/lib/cut/cut.h @@ -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]; diff --git a/qse/lib/cut/std.c b/qse/lib/cut/std.c new file mode 100644 index 00000000..7729f1cd --- /dev/null +++ b/qse/lib/cut/std.c @@ -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 . + */ + +#include "cut.h" +#include +#include +#include +#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); +} diff --git a/qse/lib/sed/Makefile.am b/qse/lib/sed/Makefile.am index 1692ec70..00ab23c4 100644 --- a/qse/lib/sed/Makefile.am +++ b/qse/lib/sed/Makefile.am @@ -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 diff --git a/qse/lib/sed/Makefile.in b/qse/lib/sed/Makefile.in index 7437e13c..c6d30da7 100644 --- a/qse/lib/sed/Makefile.in +++ b/qse/lib/sed/Makefile.in @@ -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 $@ $< diff --git a/qse/lib/sed/StdSed.cpp b/qse/lib/sed/StdSed.cpp index c9733058..5d15f504 100644 --- a/qse/lib/sed/StdSed.cpp +++ b/qse/lib/sed/StdSed.cpp @@ -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(""); } } diff --git a/qse/lib/sed/std.c b/qse/lib/sed/std.c new file mode 100644 index 00000000..9bc5a912 --- /dev/null +++ b/qse/lib/sed/std.c @@ -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 . + */ + +#include "sed.h" +#include +#include +#include +#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); +} diff --git a/qse/samples/awk/awk01.c b/qse/samples/awk/awk01.c index fddfb564..79308c63 100644 --- a/qse/samples/awk/awk01.c +++ b/qse/samples/awk/awk01.c @@ -1,5 +1,5 @@ /* - * $Id: awk01.c 316 2009-12-14 12:50:11Z hyunghwan.chung $ + * $Id: awk01.c 320 2009-12-21 12:29:52Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. This file is part of QSE. @@ -18,7 +18,6 @@ License along with QSE. If not, see . */ -#include #include #include diff --git a/qse/samples/sed/Makefile.am b/qse/samples/sed/Makefile.am index db115db7..525eeb73 100644 --- a/qse/samples/sed/Makefile.am +++ b/qse/samples/sed/Makefile.am @@ -1,6 +1,6 @@ AM_CPPFLAGS = -I$(top_srcdir)/include -bin_PROGRAMS = +bin_PROGRAMS = sed01 LDFLAGS = -L../../lib/cmn -L../../lib/sed LDADD = -lqsesed -lqsecmn $(LIBM) @@ -9,6 +9,9 @@ if ENABLE_CXX CXXLIB = -lqsesedxx -lqsecmnxx +sed01_SOURCES = sed01.c +sed01_LDADD = $(LDADD) + bin_PROGRAMS += sed02 sed03 sed02_SOURCES = sed02.cpp diff --git a/qse/samples/sed/Makefile.in b/qse/samples/sed/Makefile.in index 64ff685c..8d57cc48 100644 --- a/qse/samples/sed/Makefile.in +++ b/qse/samples/sed/Makefile.in @@ -34,7 +34,7 @@ PRE_UNINSTALL = : POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ -bin_PROGRAMS = $(am__EXEEXT_1) +bin_PROGRAMS = sed01$(EXEEXT) $(am__EXEEXT_1) @ENABLE_CXX_TRUE@am__append_1 = sed02 sed03 subdir = samples/sed DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in @@ -53,11 +53,15 @@ CONFIG_CLEAN_VPATH_FILES = @ENABLE_CXX_TRUE@am__EXEEXT_1 = sed02$(EXEEXT) sed03$(EXEEXT) am__installdirs = "$(DESTDIR)$(bindir)" PROGRAMS = $(bin_PROGRAMS) +am__sed01_SOURCES_DIST = sed01.c +@ENABLE_CXX_TRUE@am_sed01_OBJECTS = sed01.$(OBJEXT) +sed01_OBJECTS = $(am_sed01_OBJECTS) +am__DEPENDENCIES_1 = +am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1) +@ENABLE_CXX_TRUE@sed01_DEPENDENCIES = $(am__DEPENDENCIES_2) am__sed02_SOURCES_DIST = sed02.cpp @ENABLE_CXX_TRUE@am_sed02_OBJECTS = sed02.$(OBJEXT) sed02_OBJECTS = $(am_sed02_OBJECTS) -am__DEPENDENCIES_1 = -am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1) @ENABLE_CXX_TRUE@sed02_DEPENDENCIES = $(am__DEPENDENCIES_1) \ @ENABLE_CXX_TRUE@ $(am__DEPENDENCIES_2) am__sed03_SOURCES_DIST = sed03.cpp @@ -69,6 +73,15 @@ DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include/qse depcomp = $(SHELL) $(top_srcdir)/ac/au/depcomp am__depfiles_maybe = depfiles am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ + --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +CCLD = $(CC) +LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ + --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \ + $(LDFLAGS) -o $@ CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) LTCXXCOMPILE = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ @@ -78,8 +91,9 @@ CXXLD = $(CXX) CXXLINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ --mode=link $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) \ $(LDFLAGS) -o $@ -SOURCES = $(sed02_SOURCES) $(sed03_SOURCES) -DIST_SOURCES = $(am__sed02_SOURCES_DIST) $(am__sed03_SOURCES_DIST) +SOURCES = $(sed01_SOURCES) $(sed02_SOURCES) $(sed03_SOURCES) +DIST_SOURCES = $(am__sed01_SOURCES_DIST) $(am__sed02_SOURCES_DIST) \ + $(am__sed03_SOURCES_DIST) ETAGS = etags CTAGS = ctags DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) @@ -225,6 +239,8 @@ top_srcdir = @top_srcdir@ AM_CPPFLAGS = -I$(top_srcdir)/include LDADD = -lqsesed -lqsecmn $(LIBM) @ENABLE_CXX_TRUE@CXXLIB = -lqsesedxx -lqsecmnxx +@ENABLE_CXX_TRUE@sed01_SOURCES = sed01.c +@ENABLE_CXX_TRUE@sed01_LDADD = $(LDADD) @ENABLE_CXX_TRUE@sed02_SOURCES = sed02.cpp @ENABLE_CXX_TRUE@sed02_LDADD = $(CXXLIB) $(LDADD) @ENABLE_CXX_TRUE@sed03_SOURCES = sed03.cpp @@ -232,7 +248,7 @@ LDADD = -lqsesed -lqsecmn $(LIBM) all: all-am .SUFFIXES: -.SUFFIXES: .cpp .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 +322,9 @@ clean-binPROGRAMS: list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \ echo " rm -f" $$list; \ rm -f $$list +sed01$(EXEEXT): $(sed01_OBJECTS) $(sed01_DEPENDENCIES) + @rm -f sed01$(EXEEXT) + $(LINK) $(sed01_OBJECTS) $(sed01_LDADD) $(LIBS) sed02$(EXEEXT): $(sed02_OBJECTS) $(sed02_DEPENDENCIES) @rm -f sed02$(EXEEXT) $(CXXLINK) $(sed02_OBJECTS) $(sed02_LDADD) $(LIBS) @@ -319,9 +338,31 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sed01.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sed02.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sed03.Po@am__quote@ +.c.o: +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(COMPILE) -c $< + +.c.obj: +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` +@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(COMPILE) -c `$(CYGPATH_W) '$<'` + +.c.lo: +@am__fastdepCC_TRUE@ $(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@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 diff --git a/qse/samples/sed/sed01.c b/qse/samples/sed/sed01.c new file mode 100644 index 00000000..8bb3a974 --- /dev/null +++ b/qse/samples/sed/sed01.c @@ -0,0 +1,70 @@ +/* + * $Id: sed01.c 316 2009-12-14 12:50:11Z 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 . + */ + +#include +#include +#include + +int sed_main (int argc, qse_char_t* argv[]) +{ + qse_sed_t* sed = QSE_NULL; + qse_char_t* infile; + qse_char_t* outfile; + int ret = -1; + + if (argc < 2 || argc > 4) + { + qse_fprintf (QSE_STDERR, QSE_T("USAGE: %s command-string [input-file [output-file]]\n"), argv[0]); + return -1; + } + + sed = qse_sed_openstd (0); + if (sed == QSE_NULL) + { + qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open sed\n")); + goto oops; + } + + if (qse_sed_compstd (sed, argv[1]) <= -1) + { + qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_sed_geterrmsg(sed)); + goto oops; + } + + infile = (argc >= 3)? argv[2]: QSE_NULL; + outfile = (argc >= 4)? argv[3]: QSE_NULL; + + if (qse_sed_execstd (sed, infile, outfile) <= -1) + { + qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_sed_geterrmsg(sed)); + goto oops; + } + +oops: + if (sed != QSE_NULL) qse_sed_close (sed); + return ret; +} + + +int qse_main (int argc, qse_achar_t* argv[]) +{ + return qse_runmain (argc, argv, sed_main); +} +