replaced TomlArray with tlibc List
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -98,7 +98,7 @@ TomlValue TomlValue_new_bool(bool b)
|
||||
|
||||
void TomlValue_destroy(TomlValue* self)
|
||||
{
|
||||
if (self == NULL)
|
||||
if (!self)
|
||||
return;
|
||||
|
||||
switch (self->type) {
|
||||
|
||||
@@ -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 <assert.h>
|
||||
#include <ctype.h>
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user