From 8a46445be85233559bad0cb2c7ffada60cd80570 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 10 Apr 2009 23:00:00 +0000 Subject: [PATCH] more code to sed --- qse/include/qse/utl/sed.h | 20 ++++++++++++++++++- qse/lib/cmn/map.c | 29 +++++++-------------------- qse/lib/utl/sed.c | 41 ++++++++++++++++++++++++++++++++------- 3 files changed, 60 insertions(+), 30 deletions(-) diff --git a/qse/include/qse/utl/sed.h b/qse/include/qse/utl/sed.h index d966d9b9..8e1e37cc 100644 --- a/qse/include/qse/utl/sed.h +++ b/qse/include/qse/utl/sed.h @@ -61,6 +61,23 @@ enum qse_sed_option_t QSE_SED_ENSURENL = (1 << 2) /* ensure NL at the text end */ }; +enum qse_sed_io_cmd_t +{ + QSE_SED_IO_OPEN = 0, + QSE_SED_IO_CLOSE = 1, + QSE_SED_IO_READ = 2, + QSE_SED_IO_WRITE = 3 +}; + +typedef enum qse_sed_io_cmd_t qse_sed_io_cmd_t; + +typedef qse_ssize_t (*qse_sed_iof_t) ( + qse_sed_t* sed, + qse_sed_io_cmd_t cmd, + qse_char_t* data, + qse_size_t count +); + typedef struct qse_sed_t qse_sed_t; typedef struct qse_sed_cmd_t qse_sed_cmd_t; /* command */ typedef enum qse_sed_errnum_t qse_sed_errnum_t; @@ -186,7 +203,8 @@ int qse_sed_compile ( ); int qse_sed_execute ( - qse_sed_t* sed + qse_sed_t* sed, + qse_sed_iof_t* iof ); #ifdef __cplusplus diff --git a/qse/lib/cmn/map.c b/qse/lib/cmn/map.c index 1b220da9..8273527b 100644 --- a/qse/lib/cmn/map.c +++ b/qse/lib/cmn/map.c @@ -1,5 +1,5 @@ /* - * $Id: map.c 76 2009-02-22 14:18:06Z hyunghwan.chung $ + * $Id: map.c 120 2009-04-10 05:00:00Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. @@ -411,9 +411,8 @@ pair_t* qse_map_search (map_t* map, const void* kptr, size_t klen) return QSE_NULL; } -static int map_put ( - map_t* map, void* kptr, size_t klen, - void* vptr, size_t vlen, pair_t** px) +pair_t* qse_map_upsert ( + map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen) { pair_t* pair, * p, * prev, * next; size_t hc; @@ -429,7 +428,7 @@ static int map_put ( if (map->comper (map, KPTR(pair), KLEN(pair), kptr, klen) == 0) { p = change_pair_val (map, pair, vptr, vlen); - if (p == QSE_NULL) return -1; /* change error */ + if (p == QSE_NULL) return QSE_NULL; /* change error */ if (p != pair) { /* the pair has been reallocated. relink it */ @@ -438,8 +437,7 @@ static int map_put ( NEXT(p) = next; } - if (px != QSE_NULL) *px = p; - return 0; /* value changed for the existing key */ + return p; /* value changed for the existing key */ } prev = pair; @@ -457,26 +455,13 @@ static int map_put ( QSE_ASSERT (pair == QSE_NULL); pair = alloc_pair (map, kptr, klen, vptr, vlen); - if (pair == QSE_NULL) return -1; /* error */ + if (pair == QSE_NULL) return QSE_NULL; /* error */ NEXT(pair) = map->bucket[hc]; map->bucket[hc] = pair; map->size++; - if (px != QSE_NULL) *px = pair; - return 1; /* new key added */ -} - -pair_t* qse_map_upsert ( - map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen) -{ - /* update if the key exists, otherwise insert a new pair */ - int n; - pair_t* px; - - n = map_put (map, kptr, klen, vptr, vlen, &px); - if (n < 0) return QSE_NULL; - return px; + return pair; /* new key added */ } pair_t* qse_map_insert (map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen) diff --git a/qse/lib/utl/sed.c b/qse/lib/utl/sed.c index cf7a70df..d31358fb 100644 --- a/qse/lib/utl/sed.c +++ b/qse/lib/utl/sed.c @@ -23,11 +23,6 @@ /* TODO: delete stdio.h */ #include -/* POSIX http://www.opengroup.org/onlinepubs/009695399/utilities/sed.html - * ALSO READ - http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/sed/POSIX?rev=1.4.6.1;content-type=text%2Fplain - */ - QSE_IMPLEMENT_COMMON_FUNCTIONS (sed) qse_sed_t* qse_sed_open (qse_mmgr_t* mmgr, qse_size_t xtn) @@ -1197,15 +1192,47 @@ int qse_sed_compile (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen) return compile_source (sed, sptr, slen); } -int qse_sed_execute (qse_sed_t* sed) +static int read_line (qse_sed_t* sed, qse_sed_iof_t* iof) +{ +} + +int qse_sed_execute (qse_sed_t* sed, qse_sed_iof_t* iof) { qse_sed_cmd_t* c = sed->cmd.buf; - + qse_ssize_t n; + int ret = 0; + + n = iof (sed, QSE_SED_IO_OPEN, QSE_NULL, 0); + if (n == 0) goto done; /* EOF reached upon opening a stream */ + if (n == -1) + { + ret = -1; + sed->errnum = QSE_SED_EIO; + goto done; + } + + while (1) + { + } + +done: + iof (sed, QSE_SED_IO_CLOSE, QSE_NULL< 0); + return ret; + +#if 0 while (c < sed->cmd.cur) { qse_printf (QSE_T(">>> %c\n"), c->type); + + switch (c->type) + { + case QSE_CMD_ + } + c++; } return 0; +#endif + }