From bd85ae398aefd7dbc908191eb83ee1ffff840ec9 Mon Sep 17 00:00:00 2001 From: John Kerl Date: Thu, 27 Aug 2015 22:55:03 -0400 Subject: [PATCH] [read-opt] experiment iterate --- c/bld.sh | 7 +++- c/experimental/getline_for_profile.c | 17 +++++++++ c/experimental/getmulti_for_profile.c | 51 +++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 c/experimental/getline_for_profile.c create mode 100644 c/experimental/getmulti_for_profile.c diff --git a/c/bld.sh b/c/bld.sh index e9614a925..7c765bc3a 100644 --- a/c/bld.sh +++ b/c/bld.sh @@ -1 +1,6 @@ -gcc -Wall -Werror -I. lib/mlrutil.c lib/string_builder.c lib/mlr_globals.c containers/slls.c input/peek_file_reader.c experimental/csv0.c +#gcc -Wall -Werror -I. lib/mlrutil.c lib/string_builder.c lib/mlr_globals.c containers/slls.c input/peek_file_reader.c experimental/csv0.c +gcc -Wall -Werror -I. -O3 lib/mlrutil.c lib/mlr_globals.c experimental/getline_for_profile.c -o get1 +gcc -Wall -Werror -I. -O3 lib/mlrutil.c lib/mlr_globals.c \ + lib/string_builder.c \ + input/peek_file_reader.c \ + experimental/getmulti_for_profile.c -o get2 diff --git a/c/experimental/getline_for_profile.c b/c/experimental/getline_for_profile.c new file mode 100644 index 000000000..ce0282dc6 --- /dev/null +++ b/c/experimental/getline_for_profile.c @@ -0,0 +1,17 @@ +#include +#include +#include "lib/mlrutil.h" +#include "input/file_reader_stdio.h" +#include "input/lrec_readers.h" + +int main(void) { + while (1) { + char* line = mlr_get_line(stdin, '\n'); + if (line == NULL) + break; + fputs(line, stdout); + fputc('\n', stdout); + free(line); + } + return 0; +} diff --git a/c/experimental/getmulti_for_profile.c b/c/experimental/getmulti_for_profile.c new file mode 100644 index 000000000..c32e45ebe --- /dev/null +++ b/c/experimental/getmulti_for_profile.c @@ -0,0 +1,51 @@ +#include +#include +#include "lib/mlr_globals.h" +#include "lib/mlrutil.h" +#include "containers/slls.h" +#include "containers/lhmslv.h" +#include "input/file_reader_stdio.h" +#include "input/lrec_readers.h" +#include "lib/string_builder.h" +#include "input/peek_file_reader.h" + +#define PEEK_BUF_LEN 32 +#define STRING_BUILDER_INIT_SIZE 1024 + +static char* mlr_get_line2(FILE* input_stream, char* irs, int irs_len, + peek_file_reader_t* pfr, string_builder_t* psb) +{ + while (TRUE) { + if (pfr_at_eof(pfr)) { + if (sb_is_empty(psb)) + return NULL; + else + return sb_finish(psb); + } else if (pfr_next_is(pfr, irs, irs_len)) { + if (!pfr_advance_past(pfr, irs)) { + fprintf(stderr, "%s: Internal coding error: IRS found and lost.\n", MLR_GLOBALS.argv0); + exit(1); + } + return sb_finish(psb); + } else { + sb_append_char(psb, pfr_read_char(pfr)); + } + } +} + +int main(void) { + FILE* input_stream = stdin; + peek_file_reader_t* pfr = pfr_alloc(input_stream, PEEK_BUF_LEN); + string_builder_t sb; + string_builder_t* psb = &sb; + sb_init(&sb, STRING_BUILDER_INIT_SIZE); + + while (1) { + char* line = mlr_get_line2(stdin, "\r\n", 2, pfr, psb); + if (line == NULL) + break; + fputs(line, stdout); + free(line); + } + return 0; +}