From b010320498d7e8f66d1366529ac1ead14722b1d5 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 26 May 2009 07:39:18 +0000 Subject: [PATCH] added code to the Sed class --- qse/include/qse/Mmgr.hpp | 15 ++++++++-- qse/include/qse/Types.hpp | 52 ++++++++++++++++++++++++++++++++++ qse/include/qse/awk/Awk.hpp | 19 +------------ qse/include/qse/sed/Sed.hpp | 20 +++++++++++-- qse/lib/sed/Sed.cpp | 56 +++++++++++++++++++++++++++++++++++-- 5 files changed, 136 insertions(+), 26 deletions(-) create mode 100644 qse/include/qse/Types.hpp diff --git a/qse/include/qse/Mmgr.hpp b/qse/include/qse/Mmgr.hpp index f29c7f44..6640a9a0 100644 --- a/qse/include/qse/Mmgr.hpp +++ b/qse/include/qse/Mmgr.hpp @@ -19,8 +19,7 @@ #ifndef _QSE_MMGR_HPP_ #define _QSE_MMGR_HPP_ -#include -#include +#include ///////////////////////////////// QSE_BEGIN_NAMESPACE(QSE) @@ -33,9 +32,16 @@ QSE_BEGIN_NAMESPACE(QSE) * write code in more object-oriented fashion. An inheriting class should * implement three pure virtual functions. */ -class Mmgr: public qse_mmgr_t +class Mmgr: public Types, public qse_mmgr_t { public: + /** defines an alias type to qse_mmgr_t */ + typedef qse_mmgr_t mmgr_t; + + /** + * The Mmgr() function builds a memory manager composed of bridge + * functions connecting itself with it. + */ Mmgr () throw () { this->alloc = alloc_mem; @@ -44,6 +50,9 @@ public: this->data = this; } + /** + * The ~Mmgr() function finalizes a memory manager. + */ virtual ~Mmgr () {} protected: diff --git a/qse/include/qse/Types.hpp b/qse/include/qse/Types.hpp new file mode 100644 index 00000000..3669396a --- /dev/null +++ b/qse/include/qse/Types.hpp @@ -0,0 +1,52 @@ +/* + * $Id: Sed.hpp 127 2009-05-07 13:15:04Z baconevi $ + * + Copyright 2006-2009 Chung, Hyung-Hwan. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#ifndef _QSE_TYPES_HPP_ +#define _QSE_TYPES_HPP_ + +#include +#include + +///////////////////////////////// +QSE_BEGIN_NAMESPACE(QSE) +///////////////////////////////// + +class Types +{ +public: + /** boolean data type */ + typedef qse_bool_t bool_t; + /** data type that can hold any character */ + typedef qse_char_t char_t; + /** data type that can hold any character or an end-of-file value */ + typedef qse_cint_t cint_t; + /** represents an unsigned integer number of the same size as void* */ + typedef qse_size_t size_t; + /** signed version of size_t */ + typedef qse_ssize_t ssize_t; + /** represents an integer */ + typedef qse_long_t long_t; + /** represents a floating-point number */ + typedef qse_real_t real_t; +}; + +///////////////////////////////// +QSE_END_NAMESPACE(QSE) +///////////////////////////////// + +#endif diff --git a/qse/include/qse/awk/Awk.hpp b/qse/include/qse/awk/Awk.hpp index e3427e88..fe2b9a4b 100644 --- a/qse/include/qse/awk/Awk.hpp +++ b/qse/include/qse/awk/Awk.hpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.hpp 148 2009-05-20 10:44:47Z hyunghwan.chung $ + * $Id: Awk.hpp 156 2009-05-25 13:39:18Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. @@ -35,27 +35,10 @@ QSE_BEGIN_NAMESPACE(QSE) class Awk: public Mmgr { public: - /** Boolean data type */ - typedef qse_bool_t bool_t; - /** Data type that can hold any character */ - typedef qse_char_t char_t; - /** Data type that can hold any character or an end-of-file value */ - typedef qse_cint_t cint_t; - /** Represents an unsigned integer number of the same size as void* */ - typedef qse_size_t size_t; - /** Signed version of size_t */ - typedef qse_ssize_t ssize_t; - /** Represents an integer */ - typedef qse_long_t long_t; - /** Represents a floating-point number */ - typedef qse_real_t real_t; - /** Represents the internal hash table */ typedef qse_map_t map_t; /** Represents a key/value pair */ typedef qse_map_pair_t pair_t; - typedef qse_mmgr_t mmgr_t; - /** Represents an internal awk value */ typedef qse_awk_val_t val_t; diff --git a/qse/include/qse/sed/Sed.hpp b/qse/include/qse/sed/Sed.hpp index 58bc6bbb..19032ac5 100644 --- a/qse/include/qse/sed/Sed.hpp +++ b/qse/include/qse/sed/Sed.hpp @@ -32,15 +32,31 @@ QSE_BEGIN_NAMESPACE(QSE) class Sed: public Mmgr { public: + typedef qse_sed_t sed_t; + typedef qse_sed_io_cmd_t sed_io_cmd_t; + typedef qse_sed_io_arg_t sed_io_arg_t; + Sed () throw (): sed (QSE_NULL) {} int open () throw (); void close () throw (); - int compile () throw (); + int compile (const char_t* sptr, size_t slen) throw (); int execute () throw (); protected: - qse_sed_t* sed; + sed_t* sed; + + virtual int openIn (const char_t* path) = 0; + virtual int closeIn () = 0; + virtual ssize_t readIn (char_t* buf, size_t len) = 0; + + virtual int openOut (const char_t* path) = 0; + virtual int closeOut () = 0; + virtual ssize_t writeOut (const char_t* buf, size_t len) = 0; + +private: + static int xin (sed_t* s, sed_io_cmd_t cmd, sed_io_arg_t* arg); + static int xout (sed_t* s, sed_io_cmd_t cmd, sed_io_arg_t* arg); }; ///////////////////////////////// diff --git a/qse/lib/sed/Sed.cpp b/qse/lib/sed/Sed.cpp index d1dc58d8..c59fec56 100644 --- a/qse/lib/sed/Sed.cpp +++ b/qse/lib/sed/Sed.cpp @@ -48,14 +48,64 @@ void Sed::close () throw() } } -int Sed::compile () throw () +int Sed::compile (const char_t* sptr, size_t slen) throw () { - return 0; + QSE_ASSERT (sed != QSE_NULL); + return qse_sed_comp (sed, sptr, slen); } int Sed::execute () throw () { - return 0; + QSE_ASSERT (sed != QSE_NULL); + return qse_sed_exec (sed, xin, xout); +} + +int Sed::xin (sed_t* s, sed_io_cmd_t cmd, sed_io_arg_t* arg) +{ + Sed* sed = *(Sed**)QSE_XTN(s); + + try + { + switch (cmd) + { + case QSE_SED_IO_OPEN: + return sed->openIn (arg->open.path); + case QSE_SED_IO_CLOSE: + return sed->closeIn (); + case QSE_SED_IO_READ: + return sed->readIn (arg->read.buf, arg->read.len); + default: + return -1; + } + } + catch (...) + { + return -1; + } +} + +int Sed::xout (sed_t* s, sed_io_cmd_t cmd, sed_io_arg_t* arg) +{ + Sed* sed = *(Sed**)QSE_XTN(s); + + try + { + switch (cmd) + { + case QSE_SED_IO_OPEN: + return sed->openOut (arg->open.path); + case QSE_SED_IO_CLOSE: + return sed->closeOut (); + case QSE_SED_IO_READ: + return sed->writeOut (arg->write.data, arg->write.len); + default: + return -1; + } + } + catch (...) + { + return -1; + } } /////////////////////////////////