Added function to only parse n bytes from input

This commit is contained in:
Sean Anderson
2016-06-29 14:17:46 -04:00
parent f6d7d87b8b
commit 4156d72642
2 changed files with 34 additions and 0 deletions

33
mpc.c
View File

@@ -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);

1
mpc.h
View File

@@ -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);