replaced TomlArray with tlibc List

This commit is contained in:
2025-11-26 17:02:38 +05:00
parent 2082d56c25
commit 281a65f0d1
8 changed files with 89 additions and 114 deletions

View File

@@ -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