From 93ea3e255a12324281ae50799ace3fddd3a1166a Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Thu, 16 Apr 2009 08:11:42 +0000 Subject: [PATCH] resumed working on sed --- qse/include/qse/utl/sed.h | 21 +++++++++--- qse/lib/utl/sed.c | 71 +++++++++++++++++++++++++++++++++++---- qse/test/utl/sed01.c | 8 ++++- 3 files changed, 89 insertions(+), 11 deletions(-) diff --git a/qse/include/qse/utl/sed.h b/qse/include/qse/utl/sed.h index 8e1e37cc..5db90b53 100644 --- a/qse/include/qse/utl/sed.h +++ b/qse/include/qse/utl/sed.h @@ -51,7 +51,8 @@ enum qse_sed_errnum_t QSE_SED_EGRNTD, /* group nested too deeply */ QSE_SED_EOCSDU, /* multiple occurrence specifiers */ QSE_SED_EOCSZE, /* occurrence specifier to s is zero */ - QSE_SED_EOCSTL /* occurrence specifier too large */ + QSE_SED_EOCSTL, /* occurrence specifier too large */ + QSE_SED_EIOUSR /* user io error */ }; enum qse_sed_option_t @@ -71,6 +72,8 @@ enum qse_sed_io_cmd_t typedef enum qse_sed_io_cmd_t qse_sed_io_cmd_t; +typedef struct qse_sed_t qse_sed_t; + typedef qse_ssize_t (*qse_sed_iof_t) ( qse_sed_t* sed, qse_sed_io_cmd_t cmd, @@ -78,7 +81,6 @@ typedef qse_ssize_t (*qse_sed_iof_t) ( 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; @@ -114,6 +116,17 @@ struct qse_sed_t int grplvl; /* temporary storage to keep track of the begining of a command group */ qse_sed_cmd_t* grpcmd[128]; + + + /* io data for execution */ + struct + { + qse_sed_iof_t f; + qse_char_t buf[2048]; + qse_size_t len; + qse_size_t pos; + qse_str_t line; + } eio; }; @@ -203,8 +216,8 @@ int qse_sed_compile ( ); int qse_sed_execute ( - qse_sed_t* sed, - qse_sed_iof_t* iof + qse_sed_t* sed, + qse_sed_iof_t iof ); #ifdef __cplusplus diff --git a/qse/lib/utl/sed.c b/qse/lib/utl/sed.c index d31358fb..e1bc8783 100644 --- a/qse/lib/utl/sed.c +++ b/qse/lib/utl/sed.c @@ -170,7 +170,8 @@ const qse_char_t* qse_sed_geterrmsg (qse_sed_t* sed) QSE_T("group nesting too deep"), QSE_T("multiple occurrence specifier"), QSE_T("occurrence specifier is zero"), - QSE_T("occurrence specifier too large") + QSE_T("occurrence specifier too large"), + QSE_T("error returned by user io handler") }; return (sed->errnum > 0 && sed->errnum < QSE_COUNTOF(errmsg))? @@ -1192,34 +1193,92 @@ int qse_sed_compile (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen) return compile_source (sed, sptr, slen); } -static int read_line (qse_sed_t* sed, qse_sed_iof_t* iof) +static int read_char (qse_sed_t* sed, qse_char_t* c) { + qse_ssize_t n; + + if (sed->eio.pos >= sed->eio.len) + { + n = sed->eio.f ( + sed, QSE_SED_IO_READ, + sed->eio.buf, QSE_COUNTOF(sed->eio.buf) + ); + + if (n <= -1) + { + sed->errnum = QSE_SED_EIOUSR; + return -1; + } + + if (n == 0) return 0; /* end of file */ + + sed->eio.len = n; + sed->eio.pos = 0; + } + + *c = sed->eio.buf[sed->eio.pos++]; + return 1; } -int qse_sed_execute (qse_sed_t* sed, qse_sed_iof_t* iof) +static int read_line (qse_sed_t* sed) +{ + qse_char_t c; + int n; + + qse_str_clear (&sed->eio.line); + + while (1) + { + n = read_char (sed, &c); + if (n == -1) return -1; + if (n == 0) + { + } + + if (c == QSE_T('\n')) + { + } + } + + return 0; +} + +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; + if (qse_str_init (&sed->eio.line, QSE_MMGR(sed), 256) == QSE_NULL) + { + sed->errnum = QSE_SED_ENOMEM; + return -1; + } + 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; + sed->errnum = QSE_SED_EIOUSR; goto done; } + sed->eio.f = iof; + sed->eio.len = 0; + sed->eio.pos = 0; + while (1) { } done: - iof (sed, QSE_SED_IO_CLOSE, QSE_NULL< 0); + iof (sed, QSE_SED_IO_CLOSE, QSE_NULL, 0); + qse_str_fini (&sed->eio.line); return ret; #if 0 + qse_sed_cmd_t* c = sed->cmd.buf; + while (c < sed->cmd.cur) { qse_printf (QSE_T(">>> %c\n"), c->type); diff --git a/qse/test/utl/sed01.c b/qse/test/utl/sed01.c index fbd40f94..0c0889f8 100644 --- a/qse/test/utl/sed01.c +++ b/qse/test/utl/sed01.c @@ -25,6 +25,12 @@ #include #include +static qse_ssize_t io ( + qse_sed_t* sed, qse_sed_io_cmd_t cmd, qse_char_t* buf, qse_size_t len) +{ + return 0; +} + int sed_main (int argc, qse_char_t* argv[]) { qse_sed_t* sed = QSE_NULL; @@ -54,7 +60,7 @@ int sed_main (int argc, qse_char_t* argv[]) goto oops; } - if (qse_sed_execute (sed/*, io*/) == -1) + if (qse_sed_execute (sed, io) == -1) { }