From 47792c624b47d3ad78309d29861f33f958589515 Mon Sep 17 00:00:00 2001 From: olemorud Date: Tue, 25 Apr 2023 05:05:01 +0200 Subject: [PATCH] Update comments and error code values --- include/config.h | 37 +++++++++++++++++++------------------ include/util.h | 4 ++-- src/json_value.c | 9 ++++++--- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/include/config.h b/include/config.h index 2a5ce51..4574a07 100644 --- a/include/config.h +++ b/include/config.h @@ -5,9 +5,9 @@ /* Number of elements in a hashmap - Hashmaps are implemented as an array - of linked lists. This specifies the - length of the initial array. + Hashmaps are implemented as an array + of linked lists. This specifies the + length of the initial array. */ #define OBJ_SIZE 32 @@ -15,25 +15,26 @@ Length of surrounding context to print on parser errors. - Upon parser errors (e.g. syntax errors - in the JSON file), the program outputs - the surrounding context of the error. - E.g: - | Expected ':' at index 123 - | context: - | \n\t\t{ "foo" "bar" },\n\t - | ^ - In the above example, ERROR_CONTEXT_LEN - is set to 20. + Upon parser errors (e.g. syntax errors + in the JSON file), the program outputs + the surrounding context of the error. + E.g: + | Expected ':' at index 123 + | context: + | \n\t\t{ "foo" "bar" },\n\t + | ^ + In the above example, ERROR_CONTEXT_LEN + is set to 20. */ #define ERROR_CONTEXT_LEN 60 /* - Error codes - EARLY_EOF - End of file unexpectedly reached - UNEXPECTED_CHAR - Unexpected character encountered + Return codes on different errors: + + EARLY_EOF - End of file unexpectedly reached + UNEXPECTED_CHAR - Unexpected character encountered */ -#define EARLY_EOF 202 -#define UNEXPECTED_CHAR 200 +#define EARLY_EOF 200 +#define UNEXPECTED_CHAR 201 #endif diff --git a/include/util.h b/include/util.h index 2f8e23c..829f645 100644 --- a/include/util.h +++ b/include/util.h @@ -2,8 +2,8 @@ #ifndef _UTIL_H #define _UTIL_H -#include -#include +#include // size_t +#include // FILE* __attribute__((__noreturn__)) void err_ctx(int exit_code, FILE* fp, const char* format, ...); diff --git a/src/json_value.c b/src/json_value.c index ae7af3c..7952211 100644 --- a/src/json_value.c +++ b/src/json_value.c @@ -24,9 +24,9 @@ void print_object(obj_t obj, int cur_indent, int indent_amount); Returns a hash of the string `str`. It seems to work well because 33 is coprime to - 2^32 and 2^64 (any odd number except 1 is), + 2^32 and 2^64 (any odd number except 1 is), which probably improves the distribution (I'm - not a mathematician so I can't prove it this). + not a mathematician so I can't prove this). Multiplying a number n by 33 is the same as bitshifting by 5 and adding n, which is faster than multiplying by, say, 37. Maybe any 2^x±1 @@ -112,6 +112,9 @@ bool obj_insert(obj_t m, char* const key, struct json_value* value) return true; } +/* + Free memory allocated for json_value val +*/ void json_value_delete(struct json_value val) { switch (val.type) { @@ -140,7 +143,7 @@ void json_value_delete(struct json_value val) /* Free memory allocated for obj -TODO: recurively delete children objects + Recursively deletes children objects */ void obj_delete(obj_t* m) {