replaced TomlArray with tlibc List
This commit is contained in:
parent
2082d56c25
commit
281a65f0d1
@ -14,14 +14,17 @@ extern "C" {
|
|||||||
#include "tlibc/errors.h"
|
#include "tlibc/errors.h"
|
||||||
#include "tlibc/string/str.h"
|
#include "tlibc/string/str.h"
|
||||||
#include "tlibc/collections/HashMap.h"
|
#include "tlibc/collections/HashMap.h"
|
||||||
|
#include "tlibc/collections/List.h"
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// //
|
||||||
|
// tlibtoml //
|
||||||
|
// //
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
Result(void) tlibtoml_init();
|
Result(void) tlibtoml_init();
|
||||||
void tlibtoml_deinit();
|
void tlibtoml_deinit();
|
||||||
|
|
||||||
typedef DateTime TomlDateTime;
|
|
||||||
typedef HashMap(TomlValue) TomlTable;
|
|
||||||
|
|
||||||
typedef enum TlibtomlError {
|
typedef enum TlibtomlError {
|
||||||
TLIBTOML_OK,
|
TLIBTOML_OK,
|
||||||
TLIBTOML_ERR,
|
TLIBTOML_ERR,
|
||||||
@ -32,43 +35,17 @@ typedef enum TlibtomlError {
|
|||||||
} TlibtomlError;
|
} TlibtomlError;
|
||||||
ErrorCodePage_declare(TLIBTOML);
|
ErrorCodePage_declare(TLIBTOML);
|
||||||
|
|
||||||
typedef struct TomlValue TomlValue;
|
|
||||||
|
|
||||||
typedef struct TomlArray {
|
typedef DateTime TomlDateTime;
|
||||||
TomlValue* elements;
|
typedef struct HashMap(TomlValue) HashMap(TomlValue);
|
||||||
u64 len;
|
typedef HashMap(TomlValue) TomlTable;
|
||||||
u64 _capacity;
|
typedef struct List(TomlValue) List(TomlValue);
|
||||||
} TomlArray;
|
typedef List(TomlValue) 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;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
// //
|
// //
|
||||||
// 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);
|
typedef enum TomlType {
|
||||||
void TomlTable_free(TomlTable* self);
|
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);
|
typedef struct TomlValue {
|
||||||
NULLABLE(TomlValue*) TomlTable_get(const TomlTable* self, str key);
|
TomlType type;
|
||||||
Result(TomlTable*) TomlTable_get_table(const TomlTable* self, str key);
|
union {
|
||||||
Result(TomlArray*) TomlTable_get_array(const TomlTable* self, str key);
|
i64 i;
|
||||||
Result(str*) TomlTable_get_str(const TomlTable* self, str key);
|
f64 f;
|
||||||
Result(i64) TomlTable_get_integer(const TomlTable* self, str key);
|
bool b;
|
||||||
Result(f64) TomlTable_get_float(const TomlTable* self, str key);
|
str* s;
|
||||||
Result(bool) TomlTable_get_bool(const TomlTable* self, str key);
|
TomlArray* array;
|
||||||
Result(TomlDateTime*) TomlTable_get_datetime(const TomlTable* self, str key);
|
TomlTable* table;
|
||||||
|
TomlDateTime* dt;
|
||||||
|
} value;
|
||||||
|
} TomlValue;
|
||||||
|
|
||||||
|
List_declare(TomlValue);
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
|
||||||
// //
|
|
||||||
// TomlArray.c //
|
|
||||||
// //
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
TomlArray* TomlArray_new(void);
|
|
||||||
void TomlArray_free(TomlArray* self);
|
|
||||||
void TomlArray_append(TomlArray* self, TomlValue value);
|
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
|
||||||
// //
|
|
||||||
// TomlValue.c //
|
|
||||||
// //
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
TomlValue TomlValue_new(TomlType type);
|
TomlValue TomlValue_new(TomlType type);
|
||||||
TomlValue TomlValue_new_table(void);
|
TomlValue TomlValue_new_table(void);
|
||||||
@ -132,6 +104,46 @@ TomlValue TomlValue_copy_str(str s);
|
|||||||
TomlValue TomlValue_move_str(str s);
|
TomlValue TomlValue_move_str(str s);
|
||||||
void TomlValue_destroy(TomlValue* self);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -6,37 +6,15 @@
|
|||||||
|
|
||||||
TomlArray* TomlArray_new(void)
|
TomlArray* TomlArray_new(void)
|
||||||
{
|
{
|
||||||
TomlArray* array = malloc(sizeof(TomlArray));
|
TomlArray* array = (TomlArray*)malloc(sizeof(TomlArray));
|
||||||
array->elements = NULL;
|
*array = List_TomlValue_construct(NULL, 0, 0);
|
||||||
array->len = 0;
|
|
||||||
array->_capacity = 0;
|
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TomlArray_free(TomlArray* self)
|
void TomlArray_free(TomlArray* self)
|
||||||
{
|
{
|
||||||
if (self == NULL)
|
if(!self)
|
||||||
return;
|
return;
|
||||||
|
List_TomlValue_destroy(self);
|
||||||
for (u64 i = 0; i < self->len; i++) {
|
|
||||||
TomlValue_destroy(&self->elements[i]);
|
|
||||||
}
|
|
||||||
free(self->elements);
|
|
||||||
free(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* TomlTable_new(void)
|
||||||
{
|
{
|
||||||
TomlTable* table = malloc(sizeof(TomlTable));
|
TomlTable* table = (TomlTable*)malloc(sizeof(TomlTable));
|
||||||
HashMap_construct(table, TomlValue, (Destructor_t)TomlValue_destroy);
|
HashMap_construct(table, TomlValue, (Destructor_t)TomlValue_destroy);
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
@ -19,19 +19,6 @@ void TomlTable_free(TomlTable* self)
|
|||||||
free(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)
|
Result(TomlTable*) TomlTable_get_table(const TomlTable* self, str key)
|
||||||
{
|
{
|
||||||
Deferral(1);
|
Deferral(1);
|
||||||
|
|||||||
@ -98,7 +98,7 @@ TomlValue TomlValue_new_bool(bool b)
|
|||||||
|
|
||||||
void TomlValue_destroy(TomlValue* self)
|
void TomlValue_destroy(TomlValue* self)
|
||||||
{
|
{
|
||||||
if (self == NULL)
|
if (!self)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
switch (self->type) {
|
switch (self->type) {
|
||||||
|
|||||||
@ -10,8 +10,6 @@ extern "C" {
|
|||||||
|
|
||||||
#include "tlibtoml.h"
|
#include "tlibtoml.h"
|
||||||
#include "tlibc/string/StringBuilder.h"
|
#include "tlibc/string/StringBuilder.h"
|
||||||
#include "tlibc/collections/HashMap.h"
|
|
||||||
#include "tlibc/collections/List.h"
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
|
|||||||
@ -14,7 +14,7 @@ Result(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table,
|
|||||||
if (is_array) {
|
if (is_array) {
|
||||||
u64 i = 0;
|
u64 i = 0;
|
||||||
for (; i < key_path->len - 1; i++) {
|
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);
|
TomlValue* t = TomlTable_get(real_table, part);
|
||||||
if (t == NULL) {
|
if (t == NULL) {
|
||||||
if (create_if_not_exist) {
|
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);
|
TomlValue* t = TomlTable_get(real_table, part);
|
||||||
if (t == NULL) {
|
if (t == NULL) {
|
||||||
if (create_if_not_exist) {
|
if (create_if_not_exist) {
|
||||||
@ -58,7 +58,7 @@ Result(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table,
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (u64 i = 0; i < key_path->len; i++) {
|
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);
|
TomlValue* t = TomlTable_get(real_table, part);
|
||||||
if (t == NULL) {
|
if (t == NULL) {
|
||||||
if (create_if_not_exist) {
|
if (create_if_not_exist) {
|
||||||
@ -72,7 +72,7 @@ Result(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table,
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (t->type == TLIBTOML_ARRAY) {
|
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) {
|
} else if (t->type == TLIBTOML_TABLE) {
|
||||||
real_table = t->value.table;
|
real_table = t->value.table;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
TomlParser* toml_parser_new(str s, cstr filename)
|
TomlParser* toml_parser_new(str s, cstr filename)
|
||||||
{
|
{
|
||||||
TomlParser* parser = malloc(sizeof(TomlParser));
|
TomlParser* parser = (TomlParser*)malloc(sizeof(TomlParser));
|
||||||
parser->begin = s.data;
|
parser->begin = s.data;
|
||||||
parser->end = s.data + s.len;
|
parser->end = s.data + s.len;
|
||||||
parser->ptr = s.data;
|
parser->ptr = s.data;
|
||||||
@ -18,7 +18,7 @@ TomlParser* toml_parser_new(str s, cstr filename)
|
|||||||
|
|
||||||
void toml_parser_free(TomlParser* self)
|
void toml_parser_free(TomlParser* self)
|
||||||
{
|
{
|
||||||
if (self == NULL)
|
if (!self)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
free(self);
|
free(self);
|
||||||
|
|||||||
@ -20,7 +20,7 @@ void print_array(const TomlArray *array)
|
|||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
printf(", ");
|
printf(", ");
|
||||||
}
|
}
|
||||||
print_value(&array->elements[i]);
|
print_value(&array->data[i]);
|
||||||
}
|
}
|
||||||
printf("]");
|
printf("]");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user