updated qse_sed_t a little

This commit is contained in:
hyung-hwan 2009-02-07 22:06:16 +00:00
parent 4536d7308f
commit 41b324b7a7
6 changed files with 189 additions and 73 deletions

View File

@ -39,11 +39,25 @@ typedef qse_ssize_t (*qse_tgp_io_t) (
extern "C" { extern "C" {
#endif #endif
qse_tgp_t* qse_tgp_open (qse_mmgr_t* mmgr); QSE_DEFINE_COMMON_FUNCTIONS (tgp)
void qse_tgp_close (qse_tgp_t* tgp);
void qse_tgp_setassocdata (qse_tgp_t* tgp, void* data); qse_tgp_t* qse_tgp_open (
void* qse_tgp_getassocdata (qse_tgp_t* tgp); qse_mmgr_t* mmgr,
qse_size_t xtn
);
void qse_tgp_close (
qse_tgp_t* tgp
);
qse_tgp_t* qse_tgp_init (
qse_tgp_t* tgp,
qse_mmgr_t* mmgr
);
void qse_tgp_fini (
qse_tgp_t* tgp
);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -5,6 +5,6 @@ lib_LTLIBRARIES = libqseutl.la
libqseutl_la_SOURCES = \ libqseutl_la_SOURCES = \
assert.c http.c main.c stdio.c \ assert.c http.c main.c stdio.c \
sed.c tgp.c \ sed.c tgp.c \
sed.h sed.h tgp.h
libqseutl_la_LDFLAGS = -version-info 1:0:0 -no-undefined -L../cmn libqseutl_la_LDFLAGS = -version-info 1:0:0 -no-undefined -L../cmn
libqseutl_la_LIBADD = -lqsecmn libqseutl_la_LIBADD = -lqsecmn

View File

@ -200,7 +200,7 @@ lib_LTLIBRARIES = libqseutl.la
libqseutl_la_SOURCES = \ libqseutl_la_SOURCES = \
assert.c http.c main.c stdio.c \ assert.c http.c main.c stdio.c \
sed.c tgp.c \ sed.c tgp.c \
sed.h sed.h tgp.h
libqseutl_la_LDFLAGS = -version-info 1:0:0 -no-undefined -L../cmn libqseutl_la_LDFLAGS = -version-info 1:0:0 -no-undefined -L../cmn
libqseutl_la_LIBADD = -lqsecmn libqseutl_la_LIBADD = -lqsecmn

View File

@ -65,20 +65,88 @@ void qse_sed_fini (qse_sed_t* sed)
{ {
} }
static const qse_char_t* address (const qse_char_t* cp, qse_sed_a_t* a) static void compile (qse_sed_t* sed, const qse_char_t* cp, qse_char_t seof)
{ {
qse_char_t c; qse_char_t c;
if ((c = *cp++) == QSE_T('$')) if ((c = *cp++) == seof) return QSE_NULL; /* // */
do
{
if (c == QSE_T('\0') || c == QSE_T('\n'))
{
/* premature end of text */
return QSE_NULL; /* TODO: return an error..*/
}
if (c == QSE_T('\\')
{
if (ep >= end)
{
/* too many characters */
return QSE_NULL; /* TODO: return an error..*/
}
*ep++ = c;
/* TODO: more escaped characters */
if ((c = *cp++) == QSE_T('n') c = QSE_T('n');
}
if (ep >= end)
{
/* too many characters */
return QSE_NULL; /* TODO: return an error..*/
}
*ep++ = c;
}
while ((c = *cp++) != seof);
*ep = QSE_T('\0');
regcomp (expbuf);
}
static const qse_char_t* address (
qse_sed_t* sed, 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; a->type = QSE_SED_A_DOL;
cp++;
}
else if (c == QSE_T('/')) else if (c == QSE_T('/'))
{ {
cp++;
a->type = (a->u.rex = compile(sed, c))? A_RE: A_LAST;
}
else if (c >= QSE_T('0') && c <= QSE_T('9'))
{
qse_sed_line_t lno = 0;
do
{
lno = lno * 10 + c - QSE_T('0');
cp++;
}
while ((c = *cp) >= QSE_T('0') && c <= QSE_T('9'))
/* line number 0 is illegal */
if (lno == 0) return QSE_NULL;
a->type = QSE_SED_A_LINE;
a->u.line = lno;
}
else
{
a->type = QSE_SED_A_NONE;
} }
return cp; return cp;
} }
static void compile (const qse_char_t* str) static void fcomp (const qse_char_t* str)
{ {
const qse_char_t* cp = str; const qse_char_t* cp = str;
@ -98,7 +166,7 @@ static void compile (const qse_char_t* str)
continue; continue;
} }
cp = address (cp/*, &rep->ad1*/); cp = address (sed, cp/*, &rep->ad1*/);
} }
} }

View File

@ -18,91 +18,52 @@
#include <qse/utl/tgp.h> #include <qse/utl/tgp.h>
#include "../cmn/mem.h" #include "../cmn/mem.h"
#include "tgp.h"
struct qse_tgp_t QSE_IMPLEMENT_COMMON_FUNCTIONS (tgp)
{
qse_mmgr_t mmgr;
void* assoc_data;
int errnum;
struct qse_tgp_t* qse_tgp_open (qse_mmgr_t* mmgr, qse_size_t xtn)
{
qse_tgp_io_t func;
void* arg;
} ih;
struct
{
qse_tgp_io_t func;
void* arg;
} oh;
struct
{
qse_tgp_io_t func;
void* arg;
} rh;
struct
{
qse_size_t pos;
qse_size_t len;
qse_char_t ptr[512];
} ib;
struct
{
qse_size_t len;
qse_char_t ptr[512];
} ob;
struct
{
qse_size_t len;
qse_char_t ptr[512];
} rb;
int (*read) (qse_tgp_t* tgp, qse_char_t* buf, int len);
int (*write) (qse_tgp_t* tgp, const qse_char_t* buf, int len);
int (*run) (qse_tgp_t* tgp, const qse_char_t* buf, int len);
};
qse_tgp_t* qse_tgp_open (qse_mmgr_t* mmgr)
{ {
qse_tgp_t* tgp; qse_tgp_t* tgp;
/*
if (mmgr == QSE_NULL) mmgr = QSE_GETMMGR();
if (mmgr == QSE_NULL) if (mmgr == QSE_NULL)
{ {
QSE_ASSERTX (mmgr != QSE_NULL, mmgr = QSE_MMGR_GETDFL();
"Provide the memory manager or set the global memory manager with QSE_SETMMGR()");
return QSE_NULL;
}
*/
tgp = QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(*tgp)); QSE_ASSERTX (mmgr != QSE_NULL,
"Set the memory manager with QSE_MMGR_SETDFL()");
if (mmgr == QSE_NULL) return QSE_NULL;
}
tgp = (qse_tgp_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_tgp_t) + xtn);
if (tgp == QSE_NULL) return QSE_NULL; if (tgp == QSE_NULL) return QSE_NULL;
QSE_MEMSET (tgp, 0, QSE_SIZEOF(*tgp)); if (qse_tgp_init (tgp, mmgr) == QSE_NULL)
QSE_MEMCPY (&tgp->mmgr, mmgr, QSE_SIZEOF(*mmgr)); {
QSE_MMGR_FREE (tgp->mmgr, tgp);
return QSE_NULL;
}
return tgp; return tgp;
} }
void qse_tgp_close (qse_tgp_t* tgp) void qse_tgp_close (qse_tgp_t* tgp)
{ {
QSE_MMGR_FREE (&tgp->mmgr, tgp); qse_tgp_fini (tgp);
QSE_MMGR_FREE (tgp->mmgr, tgp);
} }
void qse_tgp_setassocdata (qse_tgp_t* tgp, void* data) qse_tgp_t* qse_tgp_init (qse_tgp_t* tgp, qse_mmgr_t* mmgr)
{ {
tgp->assoc_data = data; QSE_MEMSET (tgp, 0, sizeof(*tgp));
tgp->mmgr = mmgr;
return tgp;
} }
void* qse_tgp_getassocdata (qse_tgp_t* tgp) void qse_tgp_fini (qse_tgp_t* tgp)
{ {
return tgp->assoc_data;
} }
int qse_tgp_geterrnum (qse_tgp_t* tgp) int qse_tgp_geterrnum (qse_tgp_t* tgp)

73
qse/lib/utl/tgp.h Normal file
View File

@ -0,0 +1,73 @@
/*
* $Id$
*
Copyright 2006-2009 Chung, Hyung-Hwan.
Licentgp 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_TGP_H_
#define _QSE_LIB_UTL_TGP_H_
#include <qse/utl/tgp.h>
struct qse_tgp_t
{
QSE_DEFINE_COMMON_FIELDS (tgp)
void* assoc_data;
int errnum;
struct
{
qse_tgp_io_t func;
void* arg;
} ih;
struct
{
qse_tgp_io_t func;
void* arg;
} oh;
struct
{
qse_tgp_io_t func;
void* arg;
} rh;
struct
{
qse_size_t pos;
qse_size_t len;
qse_char_t ptr[512];
} ib;
struct
{
qse_size_t len;
qse_char_t ptr[512];
} ob;
struct
{
qse_size_t len;
qse_char_t ptr[512];
} rb;
int (*read) (qse_tgp_t* tgp, qse_char_t* buf, int len);
int (*write) (qse_tgp_t* tgp, const qse_char_t* buf, int len);
int (*run) (qse_tgp_t* tgp, const qse_char_t* buf, int len);
};
#endif