/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #pragma once #ifdef __cplusplus extern "C" { #endif #include #include "tlibc/tlibc.h" #include "tlibc/time.h" #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 enum TlibtomlError { TLIBTOML_OK, TLIBTOML_ERR_SYNTAX, TLIBTOML_ERR_UNICODE, TLIBTOML_ERR_NOT_FOUND, TLIBTOML_ERR_UNEXPECTED_TYPE, } TlibtomlError; ErrorCodePage_declare(TLIBTOML); typedef DateTime TomlDateTime; typedef struct HashMap(TomlValue) HashMap(TomlValue); typedef HashMap(TomlValue) TomlTable; typedef struct List(TomlValue) List(TomlValue); typedef List(TomlValue) TomlArray; ////////////////////////////////////////////////////////////////////////////// // // // Parser // // // ////////////////////////////////////////////////////////////////////////////// /// 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, ""); } static inline Result(TomlTable*) toml_load_file(FILE* file){ return toml_load_file_filename(file, ""); } /* TODO: implement dump functions str toml_dump_str(const TomlTable* self, TomlErr *err); void toml_dump_file(const TomlTable* self, FILE* file, TomlErr *err); */ ////////////////////////////////////////////////////////////////////////////// // // // TomlValue // // // ////////////////////////////////////////////////////////////////////////////// typedef enum TomlType { TLIBTOML_INVALID_TYPE, TLIBTOML_TABLE, TLIBTOML_ARRAY, TLIBTOML_STRING, TLIBTOML_INTEGER, TLIBTOML_FLOAT, TLIBTOML_DATETIME, TLIBTOML_BOOLEAN, } TomlType; str TomlType_toStr(TomlType t); typedef struct TomlValue { TomlType type; union { i64 i; f64 f; bool b; str* s; TomlArray* array; TomlTable* table; TomlDateTime* dt; }; } TomlValue; List_declare(TomlValue); TomlValue TomlValue_new(TomlType type); TomlValue TomlValue_new_table(void); TomlValue TomlValue_new_array(void); TomlValue TomlValue_new_integer(i64 integer); TomlValue TomlValue_new_float(f64 flt); TomlValue TomlValue_new_datetime(void); TomlValue TomlValue_new_bool(bool b); /// copies the string TomlValue TomlValue_copy_str(str s); /// doesnt copy the string 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