All checks were successful
Build / linux-build-and-test (push) Successful in 27s
102 lines
3.8 KiB
C
102 lines
3.8 KiB
C
#pragma once
|
|
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
#define IKV_V1 1u
|
|
#define IKV_V2 2u
|
|
#define IKV_CURRENT_VERSION IKV_V2
|
|
|
|
typedef struct ikv_node_t ikv_node_t;
|
|
|
|
typedef enum
|
|
{
|
|
IKV_VERSION_UNKNOWN = 0,
|
|
IKV_VERSION_1 = IKV_V1,
|
|
IKV_VERSION_2 = IKV_V2
|
|
} ikv_version_t;
|
|
|
|
typedef enum
|
|
{
|
|
IKV_NULL = 0,
|
|
IKV_STRING,
|
|
IKV_INT,
|
|
IKV_FLOAT,
|
|
IKV_BOOL,
|
|
IKV_OBJECT,
|
|
IKV_ARRAY
|
|
} ikv_type_t;
|
|
|
|
/* Node lifetime */
|
|
ikv_node_t *ikv_create_object(const char *key);
|
|
ikv_node_t *ikv_create_array(const char *key, ikv_type_t element_type);
|
|
void ikv_free(ikv_node_t *node);
|
|
|
|
/* Node metadata */
|
|
const char *ikv_node_key(const ikv_node_t *node);
|
|
ikv_type_t ikv_node_type(const ikv_node_t *node);
|
|
|
|
/* Object access */
|
|
ikv_node_t *ikv_object_get(const ikv_node_t *object_node, const char *key);
|
|
uint32_t ikv_object_size(const ikv_node_t *object_node);
|
|
|
|
/* Object mutation */
|
|
ikv_node_t *ikv_object_add_object(ikv_node_t *object_node, const char *key);
|
|
ikv_node_t *ikv_object_add_array(ikv_node_t *object_node, const char *key, ikv_type_t element_type);
|
|
void ikv_object_set_int(ikv_node_t *object_node, const char *key, int64_t value);
|
|
void ikv_object_set_float(ikv_node_t *object_node, const char *key, double value);
|
|
void ikv_object_set_bool(ikv_node_t *object_node, const char *key, bool value);
|
|
void ikv_object_set_string(ikv_node_t *object_node, const char *key, const char *value);
|
|
|
|
/* Array access */
|
|
ikv_node_t *ikv_array_get(const ikv_node_t *array_node, uint32_t index);
|
|
uint32_t ikv_array_size(const ikv_node_t *array_node);
|
|
ikv_type_t ikv_array_element_type(const ikv_node_t *array_node);
|
|
|
|
/* Array mutation */
|
|
ikv_node_t *ikv_array_add_object(ikv_node_t *array_node);
|
|
void ikv_array_add_int(ikv_node_t *array_node, int64_t value);
|
|
void ikv_array_add_float(ikv_node_t *array_node, double value);
|
|
void ikv_array_add_bool(ikv_node_t *array_node, bool value);
|
|
void ikv_array_add_string(ikv_node_t *array_node, const char *value);
|
|
|
|
/* Scalar reads */
|
|
const char *ikv_as_string(const ikv_node_t *node);
|
|
int64_t ikv_as_int(const ikv_node_t *node);
|
|
double ikv_as_float(const ikv_node_t *node);
|
|
bool ikv_as_bool(const ikv_node_t *node);
|
|
|
|
/* Version detection */
|
|
ikv_version_t ikv_detect_text_version(const char *src);
|
|
ikv_version_t ikv_detect_binary_version(const void *data, size_t size);
|
|
ikv_version_t ikv_detect_file_version(const char *path, bool binary);
|
|
|
|
/* Text I/O */
|
|
ikv_node_t *ikv_parse_string(const char *src);
|
|
ikv_node_t *ikv_parse_string_version(const char *src, ikv_version_t version);
|
|
ikv_node_t *ikv_parse_file(const char *path);
|
|
ikv_node_t *ikv_parse_file_version(const char *path, ikv_version_t version);
|
|
bool ikv_write_file(const char *path, const ikv_node_t *root);
|
|
bool ikv_write_file_version(const char *path, const ikv_node_t *root, ikv_version_t version);
|
|
|
|
/* Binary I/O */
|
|
ikv_node_t *ikvb_parse_memory(const void *data, size_t size);
|
|
ikv_node_t *ikvb_parse_memory_version(const void *data, size_t size, ikv_version_t version);
|
|
ikv_node_t *ikvb_parse_file(const char *path);
|
|
ikv_node_t *ikvb_parse_file_version(const char *path, ikv_version_t version);
|
|
bool ikvb_write_memory(const ikv_node_t *root, uint8_t **out_data, uint32_t *out_size);
|
|
bool ikvb_write_memory_version(const ikv_node_t *root, uint8_t **out_data, uint32_t *out_size, ikv_version_t version);
|
|
bool ikvb_write_file(const char *path, const ikv_node_t *root);
|
|
bool ikvb_write_file_version(const char *path, const ikv_node_t *root, ikv_version_t version);
|
|
|
|
#ifdef IKV_TESTING
|
|
void ikv_test_hooks_reset(void);
|
|
void ikv_test_fail_alloc_after(int remaining_calls);
|
|
void ikv_test_fail_fopen_after(int remaining_calls);
|
|
void ikv_test_fail_fread_after(int remaining_calls);
|
|
void ikv_test_fail_fseek_after(int remaining_calls);
|
|
void ikv_test_fail_ftell_after(int remaining_calls);
|
|
void ikv_test_fail_fclose_after(int remaining_calls);
|
|
#endif
|