From d973b4b8eaaa5e7f1d10f9f02069c6d7bde25dcc Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Sun, 3 Oct 2021 21:31:59 +0100 Subject: Add testing code and fix a few bugs --- cudl.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'cudl.c') diff --git a/cudl.c b/cudl.c index 02e7b1d..fc75bb6 100644 --- a/cudl.c +++ b/cudl.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "cudl.h" #define STRIP_WHITESPACE(text) while (isspace(*text)) text++ @@ -59,7 +60,7 @@ void cudl_deinit_value(struct cudl_value value) { free(value.data.array.values); break; case CUDL_TAG_NULL: - case default: + default: break; } } @@ -67,19 +68,19 @@ void cudl_deinit_value(struct cudl_value value) { /* Parse a value from input and store it in value. * Return the number of bytes consumed. * Input must end with a null byte */ -size_t parse_value(char *input, struct cudl_value *value); +static size_t parse_value(char *input, struct cudl_value *value); static size_t parse_bool_or_null(char *input, struct cudl_value *value) { - if (strncmp(input, "null", 4)) { + if (strncmp(input, "null", 4) == 0) { value->tag = CUDL_TAG_NULL; return 4; } - if (strncmp(input, "true", 4)) { + if (strncmp(input, "true", 4) == 0) { value->tag = CUDL_TAG_BOOL; value->data.boolean = 1; return 4; } - if (strncmp(input, "false", 5)) { + if (strncmp(input, "false", 5) == 0) { value->tag = CUDL_TAG_BOOL; value->data.boolean = 0; return 5; @@ -158,8 +159,8 @@ static size_t parse_value(char *input, struct cudl_value *value) { } void cudl_parse_from_file(FILE *file, struct cudl_value *value) { - char *input; - if ((input = fread_all(file)) == NULL) { + char *input, *original_input; + if ((original_input = input = fread_all(file)) == NULL) { if (ferror(file)) cudl_err = CUDL_ERR_READING; else @@ -169,7 +170,7 @@ void cudl_parse_from_file(FILE *file, struct cudl_value *value) { input += cudl_parse(input, value); if (*input != '\0') cudl_deinit_value(*value); - free(input); + free(original_input); } size_t cudl_parse(char *input, struct cudl_value *value) { -- cgit v1.2.3