diff options
-rw-r--r-- | cudl.c | 28 |
1 files changed, 27 insertions, 1 deletions
@@ -30,6 +30,24 @@ struct cudl_value { #define CUDL_OK 0 #define CUDL_ERR_OUT_OF_MEMORY 1 #define CUDL_ERR_MISSING_VALUE 2 +#define CUDL_ERR_READING 3 + +static char *fread_all(FILE *file) { + size_t size; + char *buffer; + fseek(file, 0, SEEK_END); + size = ftell(file); + rewind(file); + clearerr(file); + if ((buffer = malloc(size + 1)) == NULL) + return NULL; + if (fread(buffer, 1, size, file) != size) { + free(buffer); + return NULL; + } + buffer[size] = '\0'; + return buffer; +} void cudl_debug(struct cudl_value *value) { } @@ -50,7 +68,15 @@ void cudl_deinit_value(struct cudl_value value) { } } -int cudl_parse_from_file(FILE input_file, struct cudl_value *value) { +int cudl_parse_from_file(FILE *file, struct cudl_value *value) { + char *input; + if ((input = fread_all(file)) == NULL) { + if (ferror(file)) + return CUDL_ERR_READING; + else + return CUDL_ERR_OUT_OF_MEMORY; + } + return cudl_parse(input, value); } int cudl_parse(char *input, struct cudl_value *value) { |