Compare commits

..

No commits in common. "bdcc838bb8cf55aa22d772340077266c23b83864" and "7a0570b0b92ae09cb907367a1a0aba5d66b09b69" have entirely different histories.

4 changed files with 31 additions and 75 deletions

View File

@ -27,10 +27,11 @@ void tlibtoml_deinit();
typedef enum TlibtomlError { typedef enum TlibtomlError {
TLIBTOML_OK, TLIBTOML_OK,
TLIBTOML_ERR,
TLIBTOML_ERR_OS,
TLIBTOML_ERR_NOMEM,
TLIBTOML_ERR_SYNTAX, TLIBTOML_ERR_SYNTAX,
TLIBTOML_ERR_UNICODE, TLIBTOML_ERR_UNICODE
TLIBTOML_ERR_NOT_FOUND,
TLIBTOML_ERR_UNEXPECTED_TYPE,
} TlibtomlError; } TlibtomlError;
ErrorCodePage_declare(TLIBTOML); ErrorCodePage_declare(TLIBTOML);
@ -47,26 +48,11 @@ typedef List(TomlValue) TomlArray;
// Parser // // Parser //
// // // //
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
/// opens file Result(TomlTable*) toml_load_str(str s);
Result(TomlTable*) toml_load_file(FILE* file);
Result(TomlTable*) toml_load_filename(cstr filename); 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 /* TODO: implement dump functions
str toml_dump_str(const TomlTable* self, TomlErr *err); str toml_dump_str(const TomlTable* self, TomlErr *err);
void toml_dump_file(const TomlTable* self, FILE* file, TomlErr *err); void toml_dump_file(const TomlTable* self, FILE* file, TomlErr *err);
@ -90,8 +76,6 @@ typedef enum TomlType {
TLIBTOML_BOOLEAN, TLIBTOML_BOOLEAN,
} TomlType; } TomlType;
str TomlType_toStr(TomlType t);
typedef struct TomlValue { typedef struct TomlValue {
TomlType type; TomlType type;
union { union {

View File

@ -19,31 +19,12 @@ void TomlTable_free(TomlTable* self)
free(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) Result(TomlTable*) TomlTable_get_table(const TomlTable* self, str key)
{ {
Deferral(1); Deferral(1);
TomlValue* v = TomlTable_get(self, key); TomlValue* v = TomlTable_get(self, key);
try_assert_value_found(v, key); try_assert(v != NULL);
try_assert_value_type(v, key, TLIBTOML_TABLE); try_assert(v->type == TLIBTOML_TABLE);
Return RESULT_VALUE(p, v->value.table); Return RESULT_VALUE(p, v->value.table);
} }
@ -51,8 +32,8 @@ Result(TomlArray*) TomlTable_get_array(const TomlTable* self, str key)
{ {
Deferral(1); Deferral(1);
TomlValue* v = TomlTable_get(self, key); TomlValue* v = TomlTable_get(self, key);
try_assert_value_found(v, key); try_assert(v != NULL);
try_assert_value_type(v, key, TLIBTOML_ARRAY); try_assert(v->type == TLIBTOML_ARRAY);
Return RESULT_VALUE(p, v->value.array); Return RESULT_VALUE(p, v->value.array);
} }
@ -60,8 +41,8 @@ Result(str*) TomlTable_get_str(const TomlTable* self, str key)
{ {
Deferral(1); Deferral(1);
TomlValue* v = TomlTable_get(self, key); TomlValue* v = TomlTable_get(self, key);
try_assert_value_found(v, key); try_assert(v != NULL);
try_assert_value_type(v, key, TLIBTOML_STRING); try_assert(v->type == TLIBTOML_STRING);
Return RESULT_VALUE(p, v->value.s); Return RESULT_VALUE(p, v->value.s);
} }
@ -69,8 +50,8 @@ Result(i64) TomlTable_get_integer(const TomlTable* self, str key)
{ {
Deferral(1); Deferral(1);
TomlValue* v = TomlTable_get(self, key); TomlValue* v = TomlTable_get(self, key);
try_assert_value_found(v, key); try_assert(v != NULL);
try_assert_value_type(v, key, TLIBTOML_INTEGER); try_assert(v->type == TLIBTOML_INTEGER);
Return RESULT_VALUE(i, v->value.i); Return RESULT_VALUE(i, v->value.i);
} }
@ -78,8 +59,8 @@ Result(f64) TomlTable_get_float(const TomlTable* self, str key)
{ {
Deferral(1); Deferral(1);
TomlValue* v = TomlTable_get(self, key); TomlValue* v = TomlTable_get(self, key);
try_assert_value_found(v, key); try_assert(v != NULL);
try_assert_value_type(v, key, TLIBTOML_FLOAT); try_assert(v->type == TLIBTOML_FLOAT);
Return RESULT_VALUE(f, v->value.f); Return RESULT_VALUE(f, v->value.f);
} }
@ -87,8 +68,8 @@ Result(TomlDateTime*) TomlTable_get_datetime(const TomlTable* self, str key)
{ {
Deferral(1); Deferral(1);
TomlValue* v = TomlTable_get(self, key); TomlValue* v = TomlTable_get(self, key);
try_assert_value_found(v, key); try_assert(v != NULL);
try_assert_value_type(v, key, TLIBTOML_DATETIME); try_assert(v->type == TLIBTOML_DATETIME);
Return RESULT_VALUE(p, v->value.dt); Return RESULT_VALUE(p, v->value.dt);
} }
@ -96,7 +77,7 @@ Result(bool) TomlTable_get_bool(const TomlTable* self, str key)
{ {
Deferral(1); Deferral(1);
TomlValue* v = TomlTable_get(self, key); TomlValue* v = TomlTable_get(self, key);
try_assert_value_found(v, key); try_assert(v != NULL);
try_assert_value_type(v, key, TLIBTOML_BOOLEAN); try_assert(v->type == TLIBTOML_BOOLEAN);
Return RESULT_VALUE(i, v->value.b); Return RESULT_VALUE(i, v->value.b);
} }

View File

@ -4,25 +4,6 @@
#include "toml_internal.h" #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 TomlValue_new(TomlType type)
{ {
TomlValue value = {0}; TomlValue value = {0};

View File

@ -30,6 +30,16 @@ Result(TomlTable*) toml_load_file_filename(FILE* file, cstr filename)
Return RESULT_VALUE(p, table); 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) Result(TomlTable*) toml_load_filename(cstr filename)
{ {
Deferral(1); Deferral(1);