From a7bc78414e72973dfd00eeedd7f4761a0da83eab Mon Sep 17 00:00:00 2001 From: John Kerl Date: Wed, 16 Sep 2015 09:13:34 -0400 Subject: [PATCH] multi-char IRS/IFS/IPS iterate --- c/input/line_readers.c | 17 ++++++++++------- c/input/lrec_reader_stdio_dkvp.c | 6 ++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/c/input/line_readers.c b/c/input/line_readers.c index 03312f0f0..971b8eaa7 100644 --- a/c/input/line_readers.c +++ b/c/input/line_readers.c @@ -61,6 +61,8 @@ char* mlr_get_cline2(FILE* fp, char irs) { } // ---------------------------------------------------------------- +// 0 1 2 3 +// a b c 0 char* mlr_get_sline(FILE* fp, char* irs, int irslen) { size_t linecap = INITIAL_SIZE; char* restrict line = mlr_malloc_or_die(INITIAL_SIZE); @@ -77,21 +79,22 @@ char* mlr_get_sline(FILE* fp, char* irs, int irslen) { p = line; } c = getc_unlocked(fp); - if (c == irslast) { + if (c == EOF) { + if (p == line) + eof = TRUE; + *p = 0; + break; + } else if (c == irslast) { // Example: delim="abc". last='c'. Already have read "ab" into line. p-line=2. // Now reading 'c'. - // xxx make a memeq + // xxx make a memneq if (((p-line) >= irslenm1) && !strncmp(p-irslenm1, irs, irslenm1)) { + p -= irslenm1; *p = 0; break; } else { *(p++) = c; } - } else if (c == EOF) { - if (p == line) - eof = TRUE; - *p = 0; - break; } else { *(p++) = c; } diff --git a/c/input/lrec_reader_stdio_dkvp.c b/c/input/lrec_reader_stdio_dkvp.c index f40b46023..be0b77a2b 100644 --- a/c/input/lrec_reader_stdio_dkvp.c +++ b/c/input/lrec_reader_stdio_dkvp.c @@ -20,10 +20,12 @@ typedef struct _lrec_reader_stdio_dkvp_state_t { char ifs; char ips; int irslen; + int ifslen; + int ipslen; int allow_repeat_ifs; } lrec_reader_stdio_dkvp_state_t; -// xxx line-reader: getline or getsline +// xxx line-reader: getcline or getsline // xxx line-parser: single-char or multi-char // xxx UTx2x2 @@ -39,8 +41,8 @@ static lrec_t* lrec_reader_stdio_dkvp_process_single_irs(void* pvstate, void* pv } static lrec_t* lrec_reader_stdio_dkvp_process_multi_irs(void* pvstate, void* pvhandle, context_t* pctx) { - FILE* input_stream = pvhandle; lrec_reader_stdio_dkvp_state_t* pstate = pvstate; + FILE* input_stream = pvhandle; char* line = mlr_get_sline(input_stream, pstate->irs, pstate->irslen); if (line == NULL) return NULL;