more code to sed

This commit is contained in:
hyung-hwan 2009-04-10 23:00:00 +00:00
parent f46c63cddb
commit 8a46445be8
3 changed files with 60 additions and 30 deletions

View File

@ -61,6 +61,23 @@ enum qse_sed_option_t
QSE_SED_ENSURENL = (1 << 2) /* ensure NL at the text end */ 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_t qse_sed_t;
typedef struct qse_sed_cmd_t qse_sed_cmd_t; /* command */ typedef struct qse_sed_cmd_t qse_sed_cmd_t; /* command */
typedef enum qse_sed_errnum_t qse_sed_errnum_t; typedef enum qse_sed_errnum_t qse_sed_errnum_t;
@ -186,7 +203,8 @@ int qse_sed_compile (
); );
int qse_sed_execute ( int qse_sed_execute (
qse_sed_t* sed qse_sed_t* sed,
qse_sed_iof_t* iof
); );
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -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. 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; return QSE_NULL;
} }
static int map_put ( pair_t* qse_map_upsert (
map_t* map, void* kptr, size_t klen, map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen)
void* vptr, size_t vlen, pair_t** px)
{ {
pair_t* pair, * p, * prev, * next; pair_t* pair, * p, * prev, * next;
size_t hc; size_t hc;
@ -429,7 +428,7 @@ static int map_put (
if (map->comper (map, KPTR(pair), KLEN(pair), kptr, klen) == 0) if (map->comper (map, KPTR(pair), KLEN(pair), kptr, klen) == 0)
{ {
p = change_pair_val (map, pair, vptr, vlen); 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) if (p != pair)
{ {
/* the pair has been reallocated. relink it */ /* the pair has been reallocated. relink it */
@ -438,8 +437,7 @@ static int map_put (
NEXT(p) = next; NEXT(p) = next;
} }
if (px != QSE_NULL) *px = p; return p; /* value changed for the existing key */
return 0; /* value changed for the existing key */
} }
prev = pair; prev = pair;
@ -457,26 +455,13 @@ static int map_put (
QSE_ASSERT (pair == QSE_NULL); QSE_ASSERT (pair == QSE_NULL);
pair = alloc_pair (map, kptr, klen, vptr, vlen); 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]; NEXT(pair) = map->bucket[hc];
map->bucket[hc] = pair; map->bucket[hc] = pair;
map->size++; map->size++;
if (px != QSE_NULL) *px = pair; return pair; /* new key added */
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;
} }
pair_t* qse_map_insert (map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen) pair_t* qse_map_insert (map_t* map, void* kptr, size_t klen, void* vptr, size_t vlen)

View File

@ -23,11 +23,6 @@
/* TODO: delete stdio.h */ /* TODO: delete stdio.h */
#include <qse/utl/stdio.h> #include <qse/utl/stdio.h>
/* 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_IMPLEMENT_COMMON_FUNCTIONS (sed)
qse_sed_t* qse_sed_open (qse_mmgr_t* mmgr, qse_size_t xtn) 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); 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_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) while (c < sed->cmd.cur)
{ {
qse_printf (QSE_T(">>> %c\n"), c->type); qse_printf (QSE_T(">>> %c\n"), c->type);
switch (c->type)
{
case QSE_CMD_
}
c++; c++;
} }
return 0; return 0;
#endif
} }