diff --git a/qse/include/qse/utl/sed.h b/qse/include/qse/utl/sed.h index 41af3a61..e3088dc9 100644 --- a/qse/include/qse/utl/sed.h +++ b/qse/include/qse/utl/sed.h @@ -57,6 +57,27 @@ void qse_sed_close ( ); /******/ +/****f* Text Processor/qse_sed_init + * NAME + * qse_sed_init - initialize a stream editor + * SYNOPSIS + */ +qse_sed_t* qse_sed_init ( + qse_sed_t* sed, + qse_mmgr_t* mmgr +); +/******/ + +/****f* Text Processor/qse_sed_fini + * NAME + * qse_sed_fini - finalize a stream editor + * SYNOPSIS + */ +void qse_sed_fini ( + qse_sed_t* sed +); +/******/ + int qse_sed_execute ( qse_sed_t* sed diff --git a/qse/lib/cmn/map.c b/qse/lib/cmn/map.c index 47b6593e..d136fee7 100644 --- a/qse/lib/cmn/map.c +++ b/qse/lib/cmn/map.c @@ -219,7 +219,7 @@ map_t* qse_map_open (mmgr_t* mmgr, size_t ext, size_t capa, int factor) if (mmgr == QSE_NULL) return QSE_NULL; } - map = (qse_map_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_map_t) + ext); + map = (map_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(map_t) + ext); if (map == QSE_NULL) return QSE_NULL; if (qse_map_init (map, mmgr, capa, factor) == QSE_NULL) diff --git a/qse/lib/utl/Makefile.am b/qse/lib/utl/Makefile.am index 55d42632..b09372d3 100644 --- a/qse/lib/utl/Makefile.am +++ b/qse/lib/utl/Makefile.am @@ -2,6 +2,9 @@ AM_CFLAGS = -I$(top_builddir)/include lib_LTLIBRARIES = libqseutl.la -libqseutl_la_SOURCES = assert.c http.c main.c stdio.c sed.c tgp.c +libqseutl_la_SOURCES = \ + assert.c http.c main.c stdio.c \ + sed.c tgp.c \ + sed.h libqseutl_la_LDFLAGS = -version-info 1:0:0 -no-undefined -L../cmn libqseutl_la_LIBADD = -lqsecmn diff --git a/qse/lib/utl/Makefile.in b/qse/lib/utl/Makefile.in index ee2b300e..c91c0925 100644 --- a/qse/lib/utl/Makefile.in +++ b/qse/lib/utl/Makefile.in @@ -197,7 +197,11 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ AM_CFLAGS = -I$(top_builddir)/include lib_LTLIBRARIES = libqseutl.la -libqseutl_la_SOURCES = assert.c http.c main.c stdio.c sed.c tgp.c +libqseutl_la_SOURCES = \ + assert.c http.c main.c stdio.c \ + sed.c tgp.c \ + sed.h + libqseutl_la_LDFLAGS = -version-info 1:0:0 -no-undefined -L../cmn libqseutl_la_LIBADD = -lqsecmn all: all-am diff --git a/qse/lib/utl/sed.c b/qse/lib/utl/sed.c index 981ca75b..800dca68 100644 --- a/qse/lib/utl/sed.c +++ b/qse/lib/utl/sed.c @@ -16,16 +16,89 @@ limitations under the License. */ -#include +#include "sed.h" +#include "../cmn/mem.h" QSE_IMPLEMENT_COMMON_FUNCTIONS (sed) qse_sed_t* qse_sed_open (qse_mmgr_t* mmgr, qse_size_t xtn) { - return QSE_NULL; + qse_sed_t* sed; + + if (mmgr == QSE_NULL) + { + mmgr = QSE_MMGR_GETDFL(); + + QSE_ASSERTX (mmgr != QSE_NULL, + "Set the memory manager with QSE_MMGR_SETDFL()"); + + if (mmgr == QSE_NULL) return QSE_NULL; + } + + sed = (qse_sed_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_sed_t) + xtn); + if (sed == QSE_NULL) return QSE_NULL; + + if (qse_sed_init (sed, mmgr) == QSE_NULL) + { + QSE_MMGR_FREE (sed->mmgr, sed); + return QSE_NULL; + } + + return sed; } void qse_sed_close (qse_sed_t* sed) { + qse_sed_fini (sed); + QSE_MMGR_FREE (sed->mmgr, sed); } +qse_sed_t* qse_sed_init (qse_sed_t* sed, qse_mmgr_t* mmgr) +{ + QSE_MEMSET (sed, 0, sizeof(*sed)); + sed->mmgr = mmgr; + + return sed; +} + +void qse_sed_fini (qse_sed_t* sed) +{ +} + +static const qse_char_t* address (const qse_char_t* cp, qse_sed_a_t* a) +{ + qse_char_t c; + + if ((c = *cp++) == QSE_T('$')) + a->type = QSE_SED_A_DOL; + else if (c == QSE_T('/')) + { + } + + return cp; +} + +static void compile (const qse_char_t* str) +{ + const qse_char_t* cp = str; + + while (1) + { + /* TODO: should use ISSPACE()? or is it enough to + * check for a ' ' and '\t' because the input 'str' + * is just a line + */ + while (*cp == QSE_T(' ') || *cp == QSE_T('\t')) cp++; + + if (*cp == QSE_T('\0') || *cp == QSE_T('#')) break; + + if (*cp == QSE_T(';')) + { + cp++; + continue; + } + + cp = address (cp/*, &rep->ad1*/); + } + +} diff --git a/qse/lib/utl/sed.h b/qse/lib/utl/sed.h new file mode 100644 index 00000000..d19cfd2c --- /dev/null +++ b/qse/lib/utl/sed.h @@ -0,0 +1,73 @@ +/* + * $Id$ + * + 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_LIB_UTL_SED_H_ +#define _QSE_LIB_UTL_SED_H_ + +#include + +typedef qse_int_t qse_sed_line_t; + +typedef struct qse_sed_a_t qse_sed_a_t; /* address */ +typedef struct qse_sed_c_t qse_sed_c_t; /* command */ +typedef struct qse_sed_l_t qse_sed_l_t; /* label */ + +struct qse_sed_a_t +{ + enum + { + QSE_SED_A_NONE, /* no address */ + QSE_SED_A_DOL, /* $ */ + QSE_SED_A_LINE, /* line # */ + QSE_SED_A_REX, /* regular expression */ + QSE_SED_A_LAST /* the last regular expression */ + } type; + + union + { + qse_sed_line_t line; + void* rex; + } u; +}; + +struct qse_sed_c_t +{ + qse_sed_a_t a1; /* optional start address */ + qse_sed_a_t a2; /* optional end address */ + + union + { + void* rex; /* regular expression */ + qse_char_t* text; /* added text or file name */ + qse_sed_c_t* lbl; /* destination command of branch */ + + } u; + + qse_char_t* rhs; /* right-hand side of sustitution */ +}; + +struct qse_sed_l_t +{ + qse_char_t name[9]; /* label name */ + qse_sed_c_t* chain; + qse_sed_c_t* address; /* command associated with label */ + +}; + + +#endif