diff --git a/mpc.c b/mpc.c index 1801606..be2e0cf 100644 --- a/mpc.c +++ b/mpc.c @@ -127,6 +127,37 @@ 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->string[length] = '\0'; + i->buffer = NULL; + i->file = NULL; + + i->suppress = 0; + i->backtrack = 1; + i->marks_num = 0; + 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) { mpc_input_t *i = malloc(sizeof(mpc_input_t)); @@ -1223,6 +1254,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 22d4720..3f0d943 100644 --- a/mpc.h +++ b/mpc.h @@ -65,6 +65,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);