diff --git a/c/containers/slls.c b/c/containers/slls.c index 62ae3b43b..f7ba91d35 100644 --- a/c/containers/slls.c +++ b/c/containers/slls.c @@ -1,5 +1,6 @@ #include #include +#include "lib/mlr_arch.h" #include "lib/mlrutil.h" #include "containers/slls.h" @@ -100,7 +101,7 @@ slls_t* slls_from_line(char* line, char ifs, int allow_repeat_ifs) { char* sep = &seps[0]; char* walker = line; char* piece; - while ((piece = strsep(&walker, sep)) != NULL) { + while ((piece = mlr_arch_strsep(&walker, sep)) != NULL) { slls_append_no_free(plist, piece); } diff --git a/c/containers/xvfuncs.c b/c/containers/xvfuncs.c index e2e1b6fa2..8d88c4451 100644 --- a/c/containers/xvfuncs.c +++ b/c/containers/xvfuncs.c @@ -1,6 +1,7 @@ -#include "../containers/xvfuncs.h" +#include "../lib/mlr_arch.h" #include "../lib/string_builder.h" #include "../lib/free_flags.h" +#include "../containers/xvfuncs.h" // ---------------------------------------------------------------- boxed_xval_t b_xx_haskey_xfunc(boxed_xval_t* pmapval, boxed_xval_t* pkeyval) { @@ -167,7 +168,7 @@ boxed_xval_t m_ss_splitnv_xfunc(boxed_xval_t* pstringval, boxed_xval_t* psepval) int i = 1; char* walker = input; char* piece; - while ((piece = strsep(&walker, sep)) != NULL) { + while ((piece = mlr_arch_strsep(&walker, sep)) != NULL) { mv_t key = mv_from_int(i); mv_t val = mv_ref_type_infer_string_or_float_or_int(piece); mlhmmv_level_put_terminal_singly_keyed(map.pnext_level, &key, &val); @@ -193,7 +194,7 @@ boxed_xval_t m_ss_splitnvx_xfunc(boxed_xval_t* pstringval, boxed_xval_t* psepval int i = 1; char* walker = input; char* piece; - while ((piece = strsep(&walker, sep)) != NULL) { + while ((piece = mlr_arch_strsep(&walker, sep)) != NULL) { mv_t key = mv_from_int(i); // xxx do not ref here mv_t val = mv_ref_type_infer_string(piece); @@ -224,15 +225,15 @@ boxed_xval_t m_sss_splitkv_xfunc(boxed_xval_t* pstringval, boxed_xval_t* ppairse int i = 1; char* walker = input; char* piece; - while ((piece = strsep(&walker, listsep)) != NULL) { + while ((piece = mlr_arch_strsep(&walker, listsep)) != NULL) { char* pair = piece; - char* left = strsep(&pair, pairsep); + char* left = mlr_arch_strsep(&pair, pairsep); if (pair == NULL) { mv_t key = mv_from_int(i); mv_t val = mv_ref_type_infer_string_or_float_or_int(left); mlhmmv_level_put_terminal_singly_keyed(map.pnext_level, &key, &val); } else { - char* right = strsep(&pair, pairsep); + char* right = mlr_arch_strsep(&pair, pairsep); mv_t key = mv_from_string(left, NO_FREE); mv_t val = mv_ref_type_infer_string_or_float_or_int(right); mlhmmv_level_put_terminal_singly_keyed(map.pnext_level, &key, &val); @@ -263,15 +264,15 @@ boxed_xval_t m_sss_splitkvx_xfunc(boxed_xval_t* pstringval, boxed_xval_t* ppairs int i = 1; char* walker = input; char* piece; - while ((piece = strsep(&walker, listsep)) != NULL) { + while ((piece = mlr_arch_strsep(&walker, listsep)) != NULL) { char* pair = piece; - char* left = strsep(&pair, pairsep); + char* left = mlr_arch_strsep(&pair, pairsep); if (pair == NULL) { mv_t key = mv_from_int(i); mv_t val = mv_ref_type_infer_string(left); mlhmmv_level_put_terminal_singly_keyed(map.pnext_level, &key, &val); } else { - char* right = strsep(&pair, pairsep); + char* right = mlr_arch_strsep(&pair, pairsep); mv_t key = mv_from_string(left, NO_FREE); mv_t val = mv_ref_type_infer_string(right); mlhmmv_level_put_terminal_singly_keyed(map.pnext_level, &key, &val); diff --git a/c/experimental/getlines.c b/c/experimental/getlines.c index 5374614f7..c6476ef7c 100644 --- a/c/experimental/getlines.c +++ b/c/experimental/getlines.c @@ -1,6 +1,7 @@ #include #include #include "lib/mlr_globals.h" +#include "lib/mlr_arch.h" #include "lib/mlrutil.h" #include "input/file_reader_stdio.h" #include "input/file_reader_mmap.h" @@ -166,7 +167,7 @@ static char* read_line_getc_unlocked(FILE* fp, char* irs) { char* line = mlr_malloc_or_die(FIXED_LINE_LEN); char* p = line; while (TRUE) { - int i = getc_unlocked(fp); + int i = mlr_arch_getc(fp); char c = i; if (i == EOF) { if (p == line) { @@ -208,7 +209,7 @@ static int read_file_getc_unlocked_fixed_len(char* filename, int do_write) { // ================================================================ static char* read_line_getc_unlocked_psb(FILE* fp, string_builder_t* psb, char* irs) { while (TRUE) { - int c = getc_unlocked(fp); + int c = mlr_arch_getc(fp); if (c == EOF) { if (sb_is_empty(psb)) return NULL; diff --git a/c/lib/mlr_arch.c b/c/lib/mlr_arch.c index 3e02e7269..3ce672948 100644 --- a/c/lib/mlr_arch.c +++ b/c/lib/mlr_arch.c @@ -1,5 +1,6 @@ #include #include +#include #include "mlr_globals.h" #include "mlr_arch.h" @@ -22,3 +23,151 @@ int mlr_arch_unsetenv(const char *name) { return unsetenv(name); #endif } + +// ---------------------------------------------------------------- +char * mlr_arch_strsep(char **pstring, const char *delim) { + return strtok_r(*pstring, delim, pstring); +} + +//> /* Copyright 2002, Red Hat Inc. - all rights reserved */ +//> /* +//> FUNCTION +//> <>---read a line up to a specified line delimiter +//> +//> INDEX +//> getdelim +//> +//> ANSI_SYNOPSIS +//> #include +//> int getdelim(char **<[bufptr]>, size_t *<[n]>, +//> int <[delim]>, FILE *<[fp]>); +//> +//> TRAD_SYNOPSIS +//> #include +//> int getdelim(<[bufptr]>, <[n]>, <[delim]>, <[fp]>) +//> char **<[bufptr]>; +//> size_t *<[n]>; +//> int <[delim]>; +//> FILE *<[fp]>; +//> +//> DESCRIPTION +//> <> reads a file <[fp]> up to and possibly including a specified +//> delimiter <[delim]>. The line is read into a buffer pointed to +//> by <[bufptr]> and designated with size *<[n]>. If the buffer is +//> not large enough, it will be dynamically grown by <>. +//> As the buffer is grown, the pointer to the size <[n]> will be +//> updated. +//> +//> RETURNS +//> <> returns <<-1>> if no characters were successfully read; +//> otherwise, it returns the number of bytes successfully read. +//> At end of file, the result is nonzero. +//> +//> PORTABILITY +//> <> is a glibc extension. +//> +//> No supporting OS subroutines are directly required. +//> */ +//> +//> //#include <_ansi.h> +//> #include +//> #include +//> #include +//> //#include "local.h" +//> +//> #define MIN_LINE_SIZE 4 +//> #define DEFAULT_LINE_SIZE 128 +//> +//> ssize_t +//> getdelim( +//> char **bufptr, +//> size_t *n, +//> int delim, +//> FILE *fp) +//> { +//> char *buf; +//> char *ptr; +//> size_t newsize, numbytes; +//> int pos; +//> int ch; +//> int cont; +//> +//> if (fp == NULL || bufptr == NULL || n == NULL) +//> { +//> errno = EINVAL; +//> return -1; +//> } +//> +//> buf = *bufptr; +//> if (buf == NULL || *n < MIN_LINE_SIZE) +//> { +//> buf = (char *)realloc (*bufptr, DEFAULT_LINE_SIZE); +//> if (buf == NULL) +//> { +//> return -1; +//> } +//> *bufptr = buf; +//> *n = DEFAULT_LINE_SIZE; +//> } +//> +//> //CHECK_INIT (_REENT, fp); +//> +//> //_flockfile (fp); +//> +//> numbytes = *n; +//> ptr = buf; +//> +//> cont = 1; +//> +//> while (cont) +//> { +//> /* fill buffer - leaving room for nul-terminator */ +//> while (--numbytes > 0) +//> { +//> //if ((ch = getc_unlocked (fp)) == EOF) +//> if ((ch = getc(fp)) == EOF) +//> { +//> cont = 0; +//> break; +//> } +//> else +//> { +//> *ptr++ = ch; +//> if (ch == delim) +//> { +//> cont = 0; +//> break; +//> } +//> } +//> } +//> +//> if (cont) +//> { +//> /* Buffer is too small so reallocate a larger buffer. */ +//> pos = ptr - buf; +//> newsize = (*n << 1); +//> buf = realloc (buf, newsize); +//> if (buf == NULL) +//> { +//> cont = 0; +//> break; +//> } +//> +//> /* After reallocating, continue in new buffer */ +//> *bufptr = buf; +//> *n = newsize; +//> ptr = buf + pos; +//> numbytes = newsize - pos; +//> } +//> } +//> +//> //_funlockfile (fp); +//> +//> /* if no input data, return failure */ +//> if (ptr == buf) +//> return -1; +//> +//> /* otherwise, nul-terminate and return number of bytes read */ +//> *ptr = '\0'; +//> return (ssize_t)(ptr - buf); +//> } diff --git a/c/lib/mlr_arch.h b/c/lib/mlr_arch.h index 569bc5978..b9f0234a4 100644 --- a/c/lib/mlr_arch.h +++ b/c/lib/mlr_arch.h @@ -23,4 +23,7 @@ int mlr_arch_setenv(const char *name, const char *value); int mlr_arch_unsetenv(const char *name); +// ---------------------------------------------------------------- +char * mlr_arch_strsep(char **pstring, const char *delim); + #endif // MLR_ARCH_H