diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2021-10-01 21:02:48 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2021-10-01 21:02:48 +0100 |
commit | affb5ee5b9fac8a88daa766960602802e35484b8 (patch) | |
tree | 7697bd96540fce85a8a573b4b01bba5ef6fb6392 /cudl.c | |
parent | 6a05583e37cd8db8a35179e6cfac0c97c435a377 (diff) | |
download | cudl-affb5ee5b9fac8a88daa766960602802e35484b8.tar |
Add reading from a file
Diffstat (limited to 'cudl.c')
-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) { |