tlibtoml/include/tlibtoml/toml.h

142 lines
3.5 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/std.h"
#include "tlibc/string/str.h"
#include "tlibc/string/StringBuilder.h"
typedef struct tm TomlDateTime;
typedef enum {
TOML_OK,
TOML_ERR,
TOML_ERR_OS,
TOML_ERR_NOMEM,
TOML_ERR_SYNTAX,
TOML_ERR_UNICODE
} TomlErrCode;
typedef struct {
TomlErrCode code;
NULLABLE(char*) message;
} TomlErr;
typedef struct _TomlValue TomlValue;
typedef struct {
TomlValue** elements;
u64 len;
u64 _capacity;
} TomlArray;
typedef struct _TomlKeyValue TomlKeyValue;
typedef struct {
u64 _capacity;
u64 len;
TomlKeyValue* _keyvals;
} TomlTable;
typedef struct {
TomlTable* _table;
TomlKeyValue* _keyval;
} TomlTableIter;
typedef enum {
TOML_TABLE,
TOML_ARRAY,
TOML_STRING,
TOML_INTEGER,
TOML_FLOAT,
TOML_DATETIME,
TOML_BOOLEAN,
} TomlType;
struct _TomlValue {
TomlType type;
union {
TomlTable* table;
TomlArray* array;
str s;
i64 i;
f64 f;
TomlDateTime dt;
bool b;
} value;
};
struct _TomlKeyValue {
str key;
TomlValue* value;
};
typedef struct {
void* (*malloc)(void* context, u64 size);
void* (*realloc)(void* context, void* p, u64 size);
void (*free)(void* context, void* p);
} TomlAllocFuncs;
void toml_set_allocator(void* context, const TomlAllocFuncs *funcs);
void* toml_malloc(u64 size);
void* toml_realloc(void* p, u64 size);
void toml_free(void* p);
const TomlErr* toml_err(void);
void toml_err_clear(void);
TomlTable* toml_table_new(void);
void toml_table_free(TomlTable* self);
void toml_table_set(TomlTable* self, str key, TomlValue* value);
TomlValue* toml_table_get(const TomlTable* self, str key);
TomlTable* toml_table_get_table(const TomlTable* self, str key);
TomlArray* toml_table_get_array(const TomlTable* self, str key);
str toml_table_get_str(const TomlTable* self, str key);
i64 toml_table_get_integer(const TomlTable* self, str key);
f64 toml_table_get_float(const TomlTable* self, str key);
const TomlDateTime* toml_table_get_datetime(const TomlTable* self, str key);
bool toml_table_get_bool(const TomlTable* self, str key);
TomlTableIter toml_table_iter_new(TomlTable* table);
TomlKeyValue* toml_table_iter_get(TomlTableIter* self);
i32 toml_table_iter_has_next(TomlTableIter* self);
void toml_table_iter_next(TomlTableIter* self);
TomlArray* toml_array_new(void);
void toml_array_free(TomlArray* self);
void toml_array_append(TomlArray* self, TomlValue* value);
TomlValue* toml_value_new(TomlType type);
TomlValue* toml_value_new_string(TomlType type);
TomlValue* toml_value_new_table(void);
TomlValue* toml_value_new_array(void);
TomlValue* toml_value_new_integer(i64 integer);
TomlValue* toml_value_new_float(f64 flt);
TomlValue* toml_value_new_datetime(void);
TomlValue* toml_value_new_bool(bool b);
TomlValue* toml_value_from_str(str s);
void toml_value_free(TomlValue* self);
TomlTable* toml_load_str(str s);
TomlTable* toml_load_file(FILE* file);
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);
*/
#ifdef __cplusplus
}
#endif