From 9fb84cf71346ddc8419eb75bb5528556b9645218 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 23 May 2009 19:21:10 +0000 Subject: [PATCH] touched up code --- qse/include/qse/Mmgr.hpp | 2 +- qse/lib/sed/sed.c | 101 +++++++++++++++++++++++---------------- qse/lib/sed/sed.h | 2 + qse/test/sed/Makefile.in | 15 +++--- qse/test/sed/sed01.c | 5 +- 5 files changed, 75 insertions(+), 50 deletions(-) diff --git a/qse/include/qse/Mmgr.hpp b/qse/include/qse/Mmgr.hpp index 8374fa8f..f29c7f44 100644 --- a/qse/include/qse/Mmgr.hpp +++ b/qse/include/qse/Mmgr.hpp @@ -59,7 +59,7 @@ protected: /** * The reallocMem() function resizes a chunk of memory previously * allocated with the allocMem() function. When resized, the contents - * of a surviving memory chunk is left untouched. If it fails to + * of the surviving part of a memory chunk is preserved. If it fails to * resize memory, it should return QSE_NULL. */ virtual void* reallocMem ( diff --git a/qse/lib/sed/sed.c b/qse/lib/sed/sed.c index 6c3f8a29..e5328e13 100644 --- a/qse/lib/sed/sed.c +++ b/qse/lib/sed/sed.c @@ -170,14 +170,6 @@ int qse_sed_getoption (qse_sed_t* sed) return sed->option; } -/* get the current charanter of the source code */ -#define CURSC(sed) \ - (((sed)->src.cur < (sed)->src.end)? (*(sed)->src.cur): QSE_CHAR_EOF) -/* advance the current pointer of the source code */ -#define ADVSCP(sed) ((sed)->src.cur++) -#define NXTSC(sed) \ - (((++(sed)->src.cur) < (sed)->src.end)? (*(sed)->src.cur): QSE_CHAR_EOF) - /* check if c is a space character */ #define IS_SPACE(c) (c == QSE_T(' ') || c == QSE_T('\t')) #define IS_LINTERM(c) (c == QSE_T('\n') || c == QSE_T('\r')) @@ -188,6 +180,22 @@ int qse_sed_getoption (qse_sed_t* sed) (c == QSE_CHAR_EOF || c == QSE_T('#') || \ c == QSE_T(';') || IS_LINTERM(c)) +#define CURSC(sed) ((sed)->src.cc) +#define NXTSC(sed) getnextsc(sed) + +static qse_cint_t getnextsc (qse_sed_t* sed) +{ + if (++sed->src.cur < sed->src.end) + { + sed->src.cc = *(sed)->src.cur; + /* TODO: support different line end convension */ + if (sed->src.cc == QSE_T('\n')) sed->src.lnum++; + } + else sed->src.cc = QSE_CHAR_EOF; + + return sed->src.cc; +} + static void free_address (qse_sed_t* sed, qse_sed_cmd_t* cmd) { if (cmd->a2.type == QSE_SED_ADR_REX) @@ -262,7 +270,10 @@ static void* compile_rex (qse_sed_t* sed, qse_char_t rxend) c = NXTSC (sed); if (c == QSE_CHAR_EOF || c == QSE_T('\n')) { - SETERR1 (sed, QSE_SED_EREXIC, 0, + qse_size_t lnum = sed->src.lnum; + if (c == QSE_T('\n')) lnum--; + SETERR1 ( + sed, QSE_SED_EREXIC, lnum, QSE_STR_PTR(&sed->rexbuf), QSE_STR_LEN(&sed->rexbuf) ); @@ -273,11 +284,13 @@ static void* compile_rex (qse_sed_t* sed, qse_char_t rxend) if (c == QSE_T('\\')) { - ADVSCP (sed); - c = CURSC (sed); + c = NXTSC (sed); if (c == QSE_CHAR_EOF || c == QSE_T('\n')) { - SETERR1 (sed, QSE_SED_EREXIC, 0, + qse_size_t lnum = sed->src.lnum; + if (c == QSE_T('\n')) lnum--; + SETERR1 ( + sed, QSE_SED_EREXIC, lnum, QSE_STR_PTR(&sed->rexbuf), QSE_STR_LEN(&sed->rexbuf) ); @@ -304,7 +317,8 @@ static void* compile_rex (qse_sed_t* sed, qse_char_t rxend) ); if (code == QSE_NULL) { - SETERR1 (sed, QSE_SED_EREXBL, 0, + SETERR1 ( + sed, QSE_SED_EREXBL, sed->src.lnum, QSE_STR_PTR(&sed->rexbuf), QSE_STR_LEN(&sed->rexbuf) ); @@ -322,14 +336,14 @@ static qse_sed_adr_t* get_address (qse_sed_t* sed, qse_sed_adr_t* a) if (c == QSE_T('$')) { a->type = QSE_SED_ADR_DOL; - ADVSCP (sed); + NXTSC (sed); } else if (c == QSE_T('/')) { a->u.rex = compile_rex (sed, c); if (a->u.rex == QSE_NULL) return QSE_NULL; a->type = QSE_SED_ADR_REX; - ADVSCP (sed); + NXTSC (sed); } else if (c >= QSE_T('0') && c <= QSE_T('9')) { @@ -337,7 +351,7 @@ static qse_sed_adr_t* get_address (qse_sed_t* sed, qse_sed_adr_t* a) do { lno = lno * 10 + c - QSE_T('0'); - ADVSCP (sed); + NXTSC (sed); } while ((c = CURSC(sed)) >= QSE_T('0') && c <= QSE_T('9')); @@ -352,14 +366,16 @@ static qse_sed_adr_t* get_address (qse_sed_t* sed, qse_sed_adr_t* a) c = NXTSC (sed); if (c == QSE_CHAR_EOF || c == QSE_T('\n')) { - SETERR1 (sed, QSE_SED_EREXIC, 0, QSE_T(""), 0); + qse_size_t lnum = sed->src.lnum; + if (c == QSE_T('\n')) lnum--; + SETERR1 (sed, QSE_SED_EREXIC, lnum, QSE_T(""), 0); return QSE_NULL; } a->u.rex = compile_rex (sed, c); if (a->u.rex == QSE_NULL) return QSE_NULL; a->type = QSE_SED_ADR_REX; - ADVSCP (sed); + NXTSC (sed); } else { @@ -424,7 +440,7 @@ do { \ if (c == QSE_T('\n')) { - ADVSCP (sed); + NXTSC (sed); if (nl) goto done; break; } @@ -506,7 +522,7 @@ static int get_label (qse_sed_t* sed, qse_sed_cmd_t* cmd) /* the label can be followed by a command on the same line without * a semicolon as in ':label p'. */ - if (c != QSE_T('#') && c != QSE_CHAR_EOF) ADVSCP (sed); + if (c != QSE_T('#') && c != QSE_CHAR_EOF) NXTSC (sed); qse_str_close (t); return 0; @@ -530,7 +546,7 @@ static int terminate_command (qse_sed_t* sed) /* if the target is terminated by #, it should let the caller * to skip the comment e.txt. so don't read in the next character */ - if (c != QSE_T('#') && c != QSE_CHAR_EOF) ADVSCP (sed); + if (c != QSE_T('#') && c != QSE_CHAR_EOF) NXTSC (sed); return 0; } @@ -796,7 +812,7 @@ static int get_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd) } else if (c == QSE_T('w')) { - ADVSCP (sed); + NXTSC (sed); if (get_file (sed, &cmd->u.subst.file) <= -1) return -1; break; } @@ -818,7 +834,8 @@ static int get_subst (qse_sed_t* sed, qse_sed_cmd_t* cmd) ); if (cmd->u.subst.rex == QSE_NULL) { - SETERR1 (sed, QSE_SED_EREXBL, 0, + SETERR1 ( + sed, QSE_SED_EREXBL, sed->src.lnum, QSE_STR_PTR(t[0]), QSE_STR_LEN(t[0]) ); @@ -940,7 +957,7 @@ static int get_transet (qse_sed_t* sed, qse_sed_cmd_t* cmd) goto oops; } - ADVSCP (sed); + NXTSC (sed); if (terminate_command (sed) <= -1) goto oops; qse_str_yield (t, &cmd->u.transet, 0); @@ -964,12 +981,12 @@ restart: default: { qse_char_t cc = c; - SETERR1 (sed, QSE_SED_ECMDNR, 0, &cc, 1); + SETERR1 (sed, QSE_SED_ECMDNR, sed->src.lnum, &cc, 1); return -1; } case QSE_CHAR_EOF: - SETERR0 (sed, QSE_SED_ECMDMS, 0); + SETERR0 (sed, QSE_SED_ECMDMS, sed->src.lnum); return -1; case QSE_T(':'): @@ -982,7 +999,7 @@ restart: return -1; } - ADVSCP (sed); + NXTSC (sed); if (get_label (sed, cmd) <= -1) return -1; c = CURSC (sed); @@ -1006,7 +1023,7 @@ restart: } sed->grp.cmd[sed->grp.level++] = cmd; - ADVSCP (sed); + NXTSC (sed); break; case QSE_T('}'): @@ -1018,7 +1035,7 @@ restart: } sed->grp.cmd[--sed->grp.level]->u.branch.target = cmd; - ADVSCP (sed); + NXTSC (sed); return 0; case QSE_T('q'): @@ -1030,7 +1047,7 @@ restart: return -1; } - ADVSCP (sed); + NXTSC (sed); if (terminate_command (sed) <= -1) return -1; break; @@ -1066,7 +1083,7 @@ restart: return -1; } - ADVSCP (sed); /* skip the new line */ + NXTSC (sed); /* skip the new line */ /* get_text() starts from the next line */ if (get_text (sed, cmd) <= -1) return -1; @@ -1097,14 +1114,14 @@ restart: case QSE_T('n'): case QSE_T('N'): cmd->type = c; - ADVSCP (sed); + NXTSC (sed); if (terminate_command (sed) <= -1) return -1; break; case QSE_T('b'): case QSE_T('t'): cmd->type = c; - ADVSCP (sed); + NXTSC (sed); if (get_branch_target (sed, cmd) <= -1) return -1; break; @@ -1113,19 +1130,19 @@ restart: case QSE_T('w'): case QSE_T('W'): cmd->type = c; - ADVSCP (sed); + NXTSC (sed); if (get_file (sed, &cmd->u.file) <= -1) return -1; break; case QSE_T('s'): cmd->type = c; - ADVSCP (sed); + NXTSC (sed); if (get_subst (sed, cmd) <= -1) return -1; break; case QSE_T('y'): cmd->type = c; - ADVSCP (sed); + NXTSC (sed); if (get_transet (sed, cmd) <= -1) return -1; break; } @@ -1140,9 +1157,11 @@ static int compile_source ( qse_sed_cmd_t* cmd = sed->cmd.cur; /* store the source code pointers */ - sed->src.ptr = ptr; - sed->src.end = ptr + len; - sed->src.cur = ptr; + sed->src.ptr = ptr; + sed->src.end = ptr + len; + sed->src.cur = ptr; + sed->src.lnum = 1; + sed->src.cc = (len > 0)? (*ptr): QSE_CHAR_EOF; /* * # comment @@ -1162,7 +1181,7 @@ static int compile_source ( if (c == QSE_T('#')) { do c = NXTSC (sed); while (!IS_LINTERM(c)); - ADVSCP (sed); + NXTSC (sed); continue; } @@ -1172,7 +1191,7 @@ static int compile_source ( if (c == QSE_T(';')) { /* semicolon without a address-command pair */ - ADVSCP (sed); + NXTSC (sed); continue; } diff --git a/qse/lib/sed/sed.h b/qse/lib/sed/sed.h index 86fd2f8e..1957cbb9 100644 --- a/qse/lib/sed/sed.h +++ b/qse/lib/sed/sed.h @@ -42,6 +42,8 @@ struct qse_sed_t /** source text pointers */ struct { + qse_size_t lnum; /**< line number */ + qse_cint_t cc; /**< last character read */ const qse_char_t* ptr; /**< beginning of the source text */ const qse_char_t* end; /**< end of the source text */ const qse_char_t* cur; /**< current source text pointer */ diff --git a/qse/test/sed/Makefile.in b/qse/test/sed/Makefile.in index 8214100d..9841f1ad 100644 --- a/qse/test/sed/Makefile.in +++ b/qse/test/sed/Makefile.in @@ -32,7 +32,8 @@ PRE_UNINSTALL = : POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ -bin_PROGRAMS = sed01$(EXEEXT) sed02$(EXEEXT) +bin_PROGRAMS = sed01$(EXEEXT) $(am__EXEEXT_1) +@ENABLE_CXX_TRUE@am__append_1 = sed02 subdir = test/sed DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 @@ -42,6 +43,7 @@ am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ mkinstalldirs = $(install_sh) -d CONFIG_HEADER = $(top_builddir)/include/qse/config.h CONFIG_CLEAN_FILES = +@ENABLE_CXX_TRUE@am__EXEEXT_1 = sed02$(EXEEXT) am__installdirs = "$(DESTDIR)$(bindir)" binPROGRAMS_INSTALL = $(INSTALL_PROGRAM) PROGRAMS = $(bin_PROGRAMS) @@ -50,10 +52,11 @@ sed01_OBJECTS = $(am_sed01_OBJECTS) sed01_LDADD = $(LDADD) am__DEPENDENCIES_1 = sed01_DEPENDENCIES = $(am__DEPENDENCIES_1) -am_sed02_OBJECTS = sed02.$(OBJEXT) +am__sed02_SOURCES_DIST = sed02.cpp +@ENABLE_CXX_TRUE@am_sed02_OBJECTS = sed02.$(OBJEXT) sed02_OBJECTS = $(am_sed02_OBJECTS) am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1) -sed02_DEPENDENCIES = $(am__DEPENDENCIES_2) +@ENABLE_CXX_TRUE@sed02_DEPENDENCIES = $(am__DEPENDENCIES_2) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include/qse depcomp = $(SHELL) $(top_srcdir)/autoconf/depcomp am__depfiles_maybe = depfiles @@ -76,7 +79,7 @@ CXXLINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ --mode=link $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) \ $(LDFLAGS) -o $@ SOURCES = $(sed01_SOURCES) $(sed02_SOURCES) -DIST_SOURCES = $(sed01_SOURCES) $(sed02_SOURCES) +DIST_SOURCES = $(sed01_SOURCES) $(am__sed02_SOURCES_DIST) ETAGS = etags CTAGS = ctags DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) @@ -208,8 +211,8 @@ top_srcdir = @top_srcdir@ AM_CPPFLAGS = -I$(top_srcdir)/include LDADD = -lqsesed -lqseutl -lqsecmn $(LIBM) sed01_SOURCES = sed01.c -sed02_SOURCES = sed02.cpp -sed02_LDADD = -lqsesed++ $(LDADD) +@ENABLE_CXX_TRUE@sed02_SOURCES = sed02.cpp +@ENABLE_CXX_TRUE@sed02_LDADD = -lqsesed++ $(LDADD) all: all-am .SUFFIXES: diff --git a/qse/test/sed/sed01.c b/qse/test/sed/sed01.c index a437bcd2..57670a83 100644 --- a/qse/test/sed/sed01.c +++ b/qse/test/sed/sed01.c @@ -118,8 +118,9 @@ int sed_main (int argc, qse_char_t* argv[]) if (qse_sed_comp (sed, argv[1], qse_strlen(argv[1])) == -1) { qse_fprintf (QSE_STDERR, - QSE_T("cannot compile - %s\n"), - qse_sed_geterrmsg(sed) + QSE_T("cannot compile - %s at line %lu\n"), + qse_sed_geterrmsg(sed), + (unsigned long)qse_sed_geterrlin(sed) ); goto oops; }