From 4156d72642b95e71065be07b98166b4ad4510eef Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Wed, 29 Jun 2016 14:17:46 -0400 Subject: [PATCH 1/3] Added function to only parse n bytes from input --- mpc.c | 33 +++++++++++++++++++++++++++++++++ mpc.h | 1 + 2 files changed, 34 insertions(+) diff --git a/mpc.c b/mpc.c index f9cced1..963ec9b 100644 --- a/mpc.c +++ b/mpc.c @@ -356,6 +356,31 @@ static mpc_input_t *mpc_input_new_string(const char *filename, const char *strin return i; } +static mpc_input_t *mpc_input_new_nstring(const char *filename, const char *string, size_t length) { + + mpc_input_t *i = malloc(sizeof(mpc_input_t)); + + i->filename = malloc(strlen(filename) + 1); + strcpy(i->filename, filename); + i->type = MPC_INPUT_STRING; + + i->state = mpc_state_new(); + + i->string = malloc(length + 1); + strncpy(i->string, string, length); + i->buffer = NULL; + i->file = NULL; + + i->backtrack = 1; + i->marks_num = 0; + i->marks = NULL; + i->lasts = NULL; + + i->last = '\0'; + + return i; +} + static mpc_input_t *mpc_input_new_pipe(const char *filename, FILE *pipe) { mpc_input_t *i = malloc(sizeof(mpc_input_t)); @@ -1186,6 +1211,14 @@ int mpc_parse(const char *filename, const char *string, mpc_parser_t *p, mpc_res return x; } +int mpc_nparse(const char *filename, const char *string, size_t length, mpc_parser_t *p, mpc_result_t *r) { + int x; + mpc_input_t *i = mpc_input_new_nstring(filename, string, length); + x = mpc_parse_input(i, p, r); + mpc_input_delete(i); + return x; +} + int mpc_parse_file(const char *filename, FILE *file, mpc_parser_t *p, mpc_result_t *r) { int x; mpc_input_t *i = mpc_input_new_file(filename, file); diff --git a/mpc.h b/mpc.h index 9ea77dd..d6766f5 100644 --- a/mpc.h +++ b/mpc.h @@ -60,6 +60,7 @@ struct mpc_parser_t; typedef struct mpc_parser_t mpc_parser_t; int mpc_parse(const char *filename, const char *string, mpc_parser_t *p, mpc_result_t *r); +int mpc_nparse(const char *filename, const char *string, size_t length, mpc_parser_t *p, mpc_result_t *r); int mpc_parse_file(const char *filename, FILE *file, mpc_parser_t *p, mpc_result_t *r); int mpc_parse_pipe(const char *filename, FILE *pipe, mpc_parser_t *p, mpc_result_t *r); int mpc_parse_contents(const char *filename, mpc_parser_t *p, mpc_result_t *r); From 570ba8907a1f98ac235166ea2880d46954927af8 Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Wed, 29 Jun 2016 17:31:31 -0400 Subject: [PATCH 2/3] Updated mpc_input_new_string to reflect changes in mpc --- mpc.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mpc.c b/mpc.c index 535830a..6940d0e 100644 --- a/mpc.c +++ b/mpc.c @@ -142,14 +142,19 @@ static mpc_input_t *mpc_input_new_nstring(const char *filename, const char *stri i->buffer = NULL; i->file = NULL; + i->suppress = 0; i->backtrack = 1; i->marks_num = 0; - i->marks = NULL; - i->lasts = NULL; - + i->marks_slots = MPC_INPUT_MARKS_MIN; + i->marks = malloc(sizeof(mpc_state_t) * i->marks_slots); + i->lasts = malloc(sizeof(char) * i->marks_slots); i->last = '\0'; + i->mem_index = 0; + memset(i->mem_full, 0, sizeof(char) * MPC_INPUT_MEM_NUM); + return i; + } static mpc_input_t *mpc_input_new_pipe(const char *filename, FILE *pipe) { From 43b3fdbd8a4e2de2caeafc316cd0eb45a7b1e5ee Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Mon, 4 Jul 2016 13:33:23 -0400 Subject: [PATCH 3/3] Fixed uninitialized terminator --- mpc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/mpc.c b/mpc.c index 6940d0e..be2e0cf 100644 --- a/mpc.c +++ b/mpc.c @@ -139,6 +139,7 @@ static mpc_input_t *mpc_input_new_nstring(const char *filename, const char *stri i->string = malloc(length + 1); strncpy(i->string, string, length); + i->string[length] = '\0'; i->buffer = NULL; i->file = NULL;