diff --git a/include/tlibtoml.h b/include/tlibtoml.h index 507e81c..04258be 100644 --- a/include/tlibtoml.h +++ b/include/tlibtoml.h @@ -14,14 +14,17 @@ extern "C" { #include "tlibc/errors.h" #include "tlibc/string/str.h" #include "tlibc/collections/HashMap.h" +#include "tlibc/collections/List.h" +////////////////////////////////////////////////////////////////////////////// +// // +// tlibtoml // +// // +////////////////////////////////////////////////////////////////////////////// Result(void) tlibtoml_init(); void tlibtoml_deinit(); -typedef DateTime TomlDateTime; -typedef HashMap(TomlValue) TomlTable; - typedef enum TlibtomlError { TLIBTOML_OK, TLIBTOML_ERR, @@ -32,43 +35,17 @@ typedef enum TlibtomlError { } TlibtomlError; ErrorCodePage_declare(TLIBTOML); -typedef struct TomlValue TomlValue; -typedef struct TomlArray { - TomlValue* elements; - u64 len; - u64 _capacity; -} TomlArray; - - -typedef enum { - TLIBTOML_INVALID_TYPE, - TLIBTOML_TABLE, - TLIBTOML_ARRAY, - TLIBTOML_STRING, - TLIBTOML_INTEGER, - TLIBTOML_FLOAT, - TLIBTOML_DATETIME, - TLIBTOML_BOOLEAN, -} TomlType; - -struct TomlValue { - TomlType type; - union { - i64 i; - f64 f; - bool b; - str* s; - TomlArray* array; - TomlTable* table; - TomlDateTime* dt; - } value; -}; +typedef DateTime TomlDateTime; +typedef struct HashMap(TomlValue) HashMap(TomlValue); +typedef HashMap(TomlValue) TomlTable; +typedef struct List(TomlValue) List(TomlValue); +typedef List(TomlValue) TomlArray; ////////////////////////////////////////////////////////////////////////////// // // -// toml.c // +// Parser // // // ////////////////////////////////////////////////////////////////////////////// @@ -84,40 +61,35 @@ void toml_dump_file(const TomlTable* self, FILE* file, TomlErr *err); ////////////////////////////////////////////////////////////////////////////// // // -// TomlTable.c // +// TomlValue // // // ////////////////////////////////////////////////////////////////////////////// -TomlTable* TomlTable_new(void); -void TomlTable_free(TomlTable* self); +typedef enum TomlType { + TLIBTOML_INVALID_TYPE, + TLIBTOML_TABLE, + TLIBTOML_ARRAY, + TLIBTOML_STRING, + TLIBTOML_INTEGER, + TLIBTOML_FLOAT, + TLIBTOML_DATETIME, + TLIBTOML_BOOLEAN, +} TomlType; -void TomlTable_set(TomlTable* self, str key, TomlValue value); -NULLABLE(TomlValue*) TomlTable_get(const TomlTable* self, str key); -Result(TomlTable*) TomlTable_get_table(const TomlTable* self, str key); -Result(TomlArray*) TomlTable_get_array(const TomlTable* self, str key); -Result(str*) TomlTable_get_str(const TomlTable* self, str key); -Result(i64) TomlTable_get_integer(const TomlTable* self, str key); -Result(f64) TomlTable_get_float(const TomlTable* self, str key); -Result(bool) TomlTable_get_bool(const TomlTable* self, str key); -Result(TomlDateTime*) TomlTable_get_datetime(const TomlTable* self, str key); +typedef struct TomlValue { + TomlType type; + union { + i64 i; + f64 f; + bool b; + str* s; + TomlArray* array; + TomlTable* table; + TomlDateTime* dt; + } value; +} TomlValue; - -////////////////////////////////////////////////////////////////////////////// -// // -// TomlArray.c // -// // -////////////////////////////////////////////////////////////////////////////// - -TomlArray* TomlArray_new(void); -void TomlArray_free(TomlArray* self); -void TomlArray_append(TomlArray* self, TomlValue value); - - -////////////////////////////////////////////////////////////////////////////// -// // -// TomlValue.c // -// // -////////////////////////////////////////////////////////////////////////////// +List_declare(TomlValue); TomlValue TomlValue_new(TomlType type); TomlValue TomlValue_new_table(void); @@ -132,6 +104,46 @@ TomlValue TomlValue_copy_str(str s); TomlValue TomlValue_move_str(str s); void TomlValue_destroy(TomlValue* self); + +////////////////////////////////////////////////////////////////////////////// +// // +// TomlTable // +// // +////////////////////////////////////////////////////////////////////////////// + +TomlTable* TomlTable_new(void); +void TomlTable_free(TomlTable* self); + +static inline void TomlTable_set(TomlTable* self, str key, TomlValue value){ + HashMap_pushOrUpdate(self, key, &value); +} + +static inline TomlValue* TomlTable_get(const TomlTable* self, const str key){ + return HashMap_tryGetPtr(self, key); +} + +Result(TomlTable*) TomlTable_get_table(const TomlTable* self, str key); +Result(TomlArray*) TomlTable_get_array(const TomlTable* self, str key); +Result(str*) TomlTable_get_str(const TomlTable* self, str key); +Result(i64) TomlTable_get_integer(const TomlTable* self, str key); +Result(f64) TomlTable_get_float(const TomlTable* self, str key); +Result(bool) TomlTable_get_bool(const TomlTable* self, str key); +Result(TomlDateTime*) TomlTable_get_datetime(const TomlTable* self, str key); + + +////////////////////////////////////////////////////////////////////////////// +// // +// TomlArray // +// // +////////////////////////////////////////////////////////////////////////////// + +TomlArray* TomlArray_new(void); +void TomlArray_free(TomlArray* self); + +static inline void TomlArray_append(TomlArray* self, TomlValue value){ + List_TomlValue_push(self, value); +} + #ifdef __cplusplus } #endif diff --git a/src/TomlArray.c b/src/TomlArray.c index 4cb3ab4..1825a52 100644 --- a/src/TomlArray.c +++ b/src/TomlArray.c @@ -6,37 +6,15 @@ TomlArray* TomlArray_new(void) { - TomlArray* array = malloc(sizeof(TomlArray)); - array->elements = NULL; - array->len = 0; - array->_capacity = 0; + TomlArray* array = (TomlArray*)malloc(sizeof(TomlArray)); + *array = List_TomlValue_construct(NULL, 0, 0); return array; } void TomlArray_free(TomlArray* self) { - if (self == NULL) + if(!self) return; - - for (u64 i = 0; i < self->len; i++) { - TomlValue_destroy(&self->elements[i]); - } - free(self->elements); + List_TomlValue_destroy(self); free(self); } - -void TomlArray_expand_if_necessary(TomlArray* self) -{ - if (self->len + 1 > self->_capacity) { - u64 new_capacity = self->_capacity > 0 ? self->_capacity * 2 : 8; - void* p = realloc(self->elements, sizeof(TomlValue) * new_capacity); - self->elements = p; - self->_capacity = new_capacity; - } -} - -void TomlArray_append(TomlArray* self, TomlValue value) -{ - TomlArray_expand_if_necessary(self); - self->elements[self->len++] = value; -} diff --git a/src/TomlTable.c b/src/TomlTable.c index ba4efba..29d3d93 100644 --- a/src/TomlTable.c +++ b/src/TomlTable.c @@ -6,7 +6,7 @@ TomlTable* TomlTable_new(void) { - TomlTable* table = malloc(sizeof(TomlTable)); + TomlTable* table = (TomlTable*)malloc(sizeof(TomlTable)); HashMap_construct(table, TomlValue, (Destructor_t)TomlValue_destroy); return table; } @@ -19,19 +19,6 @@ void TomlTable_free(TomlTable* self) free(self); } -void TomlTable_set(TomlTable* self, str key, TomlValue value) -{ - assert(key.data != NULL); - HashMap_pushOrUpdate(self, key, &value); -} - -TomlValue* TomlTable_get(const TomlTable* self, const str key) -{ - assert(key.data != NULL); - TomlValue* value = HashMap_tryGetPtr(self, key); - return value; -} - Result(TomlTable*) TomlTable_get_table(const TomlTable* self, str key) { Deferral(1); diff --git a/src/TomlValue.c b/src/TomlValue.c index 6a42b1b..cef004d 100644 --- a/src/TomlValue.c +++ b/src/TomlValue.c @@ -98,7 +98,7 @@ TomlValue TomlValue_new_bool(bool b) void TomlValue_destroy(TomlValue* self) { - if (self == NULL) + if (!self) return; switch (self->type) { diff --git a/src/toml_internal.h b/src/toml_internal.h index 2483928..68752c4 100644 --- a/src/toml_internal.h +++ b/src/toml_internal.h @@ -10,8 +10,6 @@ extern "C" { #include "tlibtoml.h" #include "tlibc/string/StringBuilder.h" -#include "tlibc/collections/HashMap.h" -#include "tlibc/collections/List.h" #include #include diff --git a/src/toml_parse/toml_walk_table_path.c b/src/toml_parse/toml_walk_table_path.c index 1904457..ea251a8 100644 --- a/src/toml_parse/toml_walk_table_path.c +++ b/src/toml_parse/toml_walk_table_path.c @@ -14,7 +14,7 @@ Result(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table, if (is_array) { u64 i = 0; for (; i < key_path->len - 1; i++) { - str part = *key_path->elements[i].value.s; + str part = *key_path->data[i].value.s; TomlValue* t = TomlTable_get(real_table, part); if (t == NULL) { if (create_if_not_exist) { @@ -31,7 +31,7 @@ Result(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table, } } - str part = *key_path->elements[i].value.s; + str part = *key_path->data[i].value.s; TomlValue* t = TomlTable_get(real_table, part); if (t == NULL) { if (create_if_not_exist) { @@ -58,7 +58,7 @@ Result(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table, } } else { for (u64 i = 0; i < key_path->len; i++) { - str part = *key_path->elements[i].value.s; + str part = *key_path->data[i].value.s; TomlValue* t = TomlTable_get(real_table, part); if (t == NULL) { if (create_if_not_exist) { @@ -72,7 +72,7 @@ Result(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table, } } else { if (t->type == TLIBTOML_ARRAY) { - real_table = t->value.array->elements[t->value.array->len - 1].value.table; + real_table = t->value.array->data[t->value.array->len - 1].value.table; } else if (t->type == TLIBTOML_TABLE) { real_table = t->value.table; } diff --git a/src/toml_parser.c b/src/toml_parser.c index e3f991a..ce8c627 100644 --- a/src/toml_parser.c +++ b/src/toml_parser.c @@ -6,7 +6,7 @@ TomlParser* toml_parser_new(str s, cstr filename) { - TomlParser* parser = malloc(sizeof(TomlParser)); + TomlParser* parser = (TomlParser*)malloc(sizeof(TomlParser)); parser->begin = s.data; parser->end = s.data + s.len; parser->ptr = s.data; @@ -18,7 +18,7 @@ TomlParser* toml_parser_new(str s, cstr filename) void toml_parser_free(TomlParser* self) { - if (self == NULL) + if (!self) return; free(self); diff --git a/tests/main.c b/tests/main.c index 05bb58b..9d95eb7 100644 --- a/tests/main.c +++ b/tests/main.c @@ -20,7 +20,7 @@ void print_array(const TomlArray *array) if (i > 0) { printf(", "); } - print_value(&array->elements[i]); + print_value(&array->data[i]); } printf("]"); }