Compare commits
2 Commits
7a0570b0b9
...
bdcc838bb8
| Author | SHA1 | Date | |
|---|---|---|---|
| bdcc838bb8 | |||
| 3e21766514 |
@ -27,11 +27,10 @@ void tlibtoml_deinit();
|
||||
|
||||
typedef enum TlibtomlError {
|
||||
TLIBTOML_OK,
|
||||
TLIBTOML_ERR,
|
||||
TLIBTOML_ERR_OS,
|
||||
TLIBTOML_ERR_NOMEM,
|
||||
TLIBTOML_ERR_SYNTAX,
|
||||
TLIBTOML_ERR_UNICODE
|
||||
TLIBTOML_ERR_UNICODE,
|
||||
TLIBTOML_ERR_NOT_FOUND,
|
||||
TLIBTOML_ERR_UNEXPECTED_TYPE,
|
||||
} TlibtomlError;
|
||||
ErrorCodePage_declare(TLIBTOML);
|
||||
|
||||
@ -49,10 +48,25 @@ typedef List(TomlValue) TomlArray;
|
||||
// //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Result(TomlTable*) toml_load_str(str s);
|
||||
Result(TomlTable*) toml_load_file(FILE* file);
|
||||
/// opens file
|
||||
Result(TomlTable*) toml_load_filename(cstr filename);
|
||||
|
||||
/// @param filename to use in error messages
|
||||
Result(TomlTable*) toml_load_str_filename(str s, cstr filename);
|
||||
|
||||
/// loads whole file in memory
|
||||
/// @param filename to use in error messages
|
||||
Result(TomlTable*) toml_load_file_filename(FILE* file, cstr filename);
|
||||
|
||||
static inline Result(TomlTable*) toml_load_str(str s){
|
||||
return toml_load_str_filename(s, "<string>");
|
||||
}
|
||||
|
||||
static inline Result(TomlTable*) toml_load_file(FILE* file){
|
||||
return toml_load_file_filename(file, "<stream>");
|
||||
}
|
||||
|
||||
|
||||
/* TODO: implement dump functions
|
||||
str toml_dump_str(const TomlTable* self, TomlErr *err);
|
||||
void toml_dump_file(const TomlTable* self, FILE* file, TomlErr *err);
|
||||
@ -76,6 +90,8 @@ typedef enum TomlType {
|
||||
TLIBTOML_BOOLEAN,
|
||||
} TomlType;
|
||||
|
||||
str TomlType_toStr(TomlType t);
|
||||
|
||||
typedef struct TomlValue {
|
||||
TomlType type;
|
||||
union {
|
||||
|
||||
@ -19,12 +19,31 @@ void TomlTable_free(TomlTable* self)
|
||||
free(self);
|
||||
}
|
||||
|
||||
|
||||
#define try_assert_value_found(VAL, KEY) \
|
||||
if(VAL == NULL){ \
|
||||
Return RESULT_ERROR_CODE_FMT( \
|
||||
TLIBTOML, TLIBTOML_ERR_NOT_FOUND, \
|
||||
"can't find '"FMT_str"'", \
|
||||
str_expand(KEY)); \
|
||||
} \
|
||||
|
||||
#define try_assert_value_type(VAL, KEY, TYPE) \
|
||||
if((VAL)->type != TYPE){ \
|
||||
str t_expected_s = TomlType_toStr(TYPE); \
|
||||
str t_got_s = TomlType_toStr((VAL)->type); \
|
||||
Return RESULT_ERROR_CODE_FMT( \
|
||||
TLIBTOML, TLIBTOML_ERR_UNEXPECTED_TYPE, \
|
||||
"expected '"FMT_str"' of type '"FMT_str"', but got '"FMT_str"'", \
|
||||
str_expand(KEY), str_expand(t_expected_s), str_expand(t_got_s)); \
|
||||
} \
|
||||
|
||||
Result(TomlTable*) TomlTable_get_table(const TomlTable* self, str key)
|
||||
{
|
||||
Deferral(1);
|
||||
TomlValue* v = TomlTable_get(self, key);
|
||||
try_assert(v != NULL);
|
||||
try_assert(v->type == TLIBTOML_TABLE);
|
||||
try_assert_value_found(v, key);
|
||||
try_assert_value_type(v, key, TLIBTOML_TABLE);
|
||||
Return RESULT_VALUE(p, v->value.table);
|
||||
}
|
||||
|
||||
@ -32,8 +51,8 @@ Result(TomlArray*) TomlTable_get_array(const TomlTable* self, str key)
|
||||
{
|
||||
Deferral(1);
|
||||
TomlValue* v = TomlTable_get(self, key);
|
||||
try_assert(v != NULL);
|
||||
try_assert(v->type == TLIBTOML_ARRAY);
|
||||
try_assert_value_found(v, key);
|
||||
try_assert_value_type(v, key, TLIBTOML_ARRAY);
|
||||
Return RESULT_VALUE(p, v->value.array);
|
||||
}
|
||||
|
||||
@ -41,8 +60,8 @@ Result(str*) TomlTable_get_str(const TomlTable* self, str key)
|
||||
{
|
||||
Deferral(1);
|
||||
TomlValue* v = TomlTable_get(self, key);
|
||||
try_assert(v != NULL);
|
||||
try_assert(v->type == TLIBTOML_STRING);
|
||||
try_assert_value_found(v, key);
|
||||
try_assert_value_type(v, key, TLIBTOML_STRING);
|
||||
Return RESULT_VALUE(p, v->value.s);
|
||||
}
|
||||
|
||||
@ -50,8 +69,8 @@ Result(i64) TomlTable_get_integer(const TomlTable* self, str key)
|
||||
{
|
||||
Deferral(1);
|
||||
TomlValue* v = TomlTable_get(self, key);
|
||||
try_assert(v != NULL);
|
||||
try_assert(v->type == TLIBTOML_INTEGER);
|
||||
try_assert_value_found(v, key);
|
||||
try_assert_value_type(v, key, TLIBTOML_INTEGER);
|
||||
Return RESULT_VALUE(i, v->value.i);
|
||||
}
|
||||
|
||||
@ -59,8 +78,8 @@ Result(f64) TomlTable_get_float(const TomlTable* self, str key)
|
||||
{
|
||||
Deferral(1);
|
||||
TomlValue* v = TomlTable_get(self, key);
|
||||
try_assert(v != NULL);
|
||||
try_assert(v->type == TLIBTOML_FLOAT);
|
||||
try_assert_value_found(v, key);
|
||||
try_assert_value_type(v, key, TLIBTOML_FLOAT);
|
||||
Return RESULT_VALUE(f, v->value.f);
|
||||
}
|
||||
|
||||
@ -68,8 +87,8 @@ Result(TomlDateTime*) TomlTable_get_datetime(const TomlTable* self, str key)
|
||||
{
|
||||
Deferral(1);
|
||||
TomlValue* v = TomlTable_get(self, key);
|
||||
try_assert(v != NULL);
|
||||
try_assert(v->type == TLIBTOML_DATETIME);
|
||||
try_assert_value_found(v, key);
|
||||
try_assert_value_type(v, key, TLIBTOML_DATETIME);
|
||||
Return RESULT_VALUE(p, v->value.dt);
|
||||
}
|
||||
|
||||
@ -77,7 +96,7 @@ Result(bool) TomlTable_get_bool(const TomlTable* self, str key)
|
||||
{
|
||||
Deferral(1);
|
||||
TomlValue* v = TomlTable_get(self, key);
|
||||
try_assert(v != NULL);
|
||||
try_assert(v->type == TLIBTOML_BOOLEAN);
|
||||
try_assert_value_found(v, key);
|
||||
try_assert_value_type(v, key, TLIBTOML_BOOLEAN);
|
||||
Return RESULT_VALUE(i, v->value.b);
|
||||
}
|
||||
|
||||
@ -4,6 +4,25 @@
|
||||
|
||||
#include "toml_internal.h"
|
||||
|
||||
Array_declare(str);
|
||||
|
||||
static Array(str) _TomlType_str_array = ARRAY(str, {
|
||||
STR("INVALID_TYPE"),
|
||||
STR("TABLE"),
|
||||
STR("ARRAY"),
|
||||
STR("STRING"),
|
||||
STR("INTEGER"),
|
||||
STR("FLOAT"),
|
||||
STR("DATETIME"),
|
||||
STR("BOOLEAN"),
|
||||
});
|
||||
|
||||
str TomlType_toStr(TomlType t){
|
||||
if((u32)t >= _TomlType_str_array.len)
|
||||
return STR("!! ERROR: INVALID TomlType !!");
|
||||
return _TomlType_str_array.data[t];
|
||||
}
|
||||
|
||||
TomlValue TomlValue_new(TomlType type)
|
||||
{
|
||||
TomlValue value = {0};
|
||||
|
||||
@ -30,16 +30,6 @@ Result(TomlTable*) toml_load_file_filename(FILE* file, cstr filename)
|
||||
Return RESULT_VALUE(p, table);
|
||||
}
|
||||
|
||||
Result(TomlTable*) toml_load_str(str s)
|
||||
{
|
||||
return toml_load_str_filename(s, "<string>");
|
||||
}
|
||||
|
||||
Result(TomlTable*) toml_load_file(FILE* file)
|
||||
{
|
||||
return toml_load_file_filename(file, "<stream>");
|
||||
}
|
||||
|
||||
Result(TomlTable*) toml_load_filename(cstr filename)
|
||||
{
|
||||
Deferral(1);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user