tlibtoml/include/tlibtoml.h

150 lines
5.1 KiB
C

/* 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 <time.h>
#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,
TLIBTOML_ERR_OS,
TLIBTOML_ERR_NOMEM,
TLIBTOML_ERR_SYNTAX,
TLIBTOML_ERR_UNICODE
} 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 //
// //
//////////////////////////////////////////////////////////////////////////////
Result(TomlTable*) toml_load_str(str s);
Result(TomlTable*) toml_load_file(FILE* file);
Result(TomlTable*) toml_load_filename(cstr filename);
/* 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;
typedef struct TomlValue {
TomlType type;
union {
i64 i;
f64 f;
bool b;
str* s;
TomlArray* array;
TomlTable* table;
TomlDateTime* dt;
} value;
} 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