Compare commits

..

No commits in common. "52870920c172b5c2b55a4b0086de8f2e47065dfb" and "a5aff56fb1ab08ceecac9756b71136a70ca35857" have entirely different histories.

5 changed files with 1005 additions and 560 deletions

4
.vscode/launch.json vendored
View File

@ -5,8 +5,8 @@
"name": "gdb_debug", "name": "gdb_debug",
"type": "cppdbg", "type": "cppdbg",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/bin/test", "program": "${workspaceFolder}/bin/tlibtoml",
"windows": { "program": "${workspaceFolder}/bin/test.exe" }, "windows": { "program": "${workspaceFolder}/bin/tlibtoml.exe" },
"preLaunchTask": "build_exec_dbg", "preLaunchTask": "build_exec_dbg",
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceFolder}/bin", "cwd": "${workspaceFolder}/bin",

View File

@ -9,7 +9,7 @@ if [[ "$TASK" = *_dbg ]]; then
else else
dep_build_target="build_static_lib" dep_build_target="build_static_lib"
fi fi
DEP_PRE_BUILD_COMMAND="" DEP_PRE_BUILD_COMMAND="setup_user_config"
DEP_BUILD_COMMAND="cbuild $dep_build_target" DEP_BUILD_COMMAND="cbuild $dep_build_target"
DEP_POST_BUILD_COMMAND="" DEP_POST_BUILD_COMMAND=""
DEP_CLEAN_COMMAND="cbuild clean" DEP_CLEAN_COMMAND="cbuild clean"

View File

@ -8,13 +8,21 @@
extern "C" { extern "C" {
#endif #endif
#include <time.h>
#include "tlibc/std.h" #include "tlibc/std.h"
#include "tlibc/string/str.h" #include <time.h>
#include "tlibc/collections/HashMap.h"
typedef struct tm TomlDateTime; #if defined(__cplusplus) && __cplusplus >= 201103L
typedef HashMap(TomlValue) TomlTable; #define ATTRIBUTE_THREAD_LOCAL thread_local
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define ATTRIBUTE_THREAD_LOCAL _Thread_local
#elif defined(_MSC_VER)
#define ATTRIBUTE_THREAD_LOCAL __declspec(thread)
#else
#define ATTRIBUTE_THREAD_LOCAL __thread
#endif
#define TOML_FALSE 0
#define TOML_TRUE 1
typedef enum { typedef enum {
TOML_OK, TOML_OK,
@ -27,20 +35,38 @@ typedef enum {
typedef struct { typedef struct {
TomlErrCode code; TomlErrCode code;
NULLABLE(char*) message; char* message;
int _is_literal;
} TomlErr; } TomlErr;
typedef struct TomlValue TomlValue; typedef struct {
char* str;
size_t len;
size_t _capacity;
} TomlString;
typedef struct _TomlValue TomlValue;
typedef struct { typedef struct {
TomlValue* elements; TomlValue** elements;
u64 len; size_t len;
u64 _capacity; size_t _capacity;
} TomlArray; } TomlArray;
typedef struct _TomlKeyValue TomlKeyValue;
typedef struct {
size_t _capacity;
size_t len;
TomlKeyValue* _keyvals;
} TomlTable;
typedef struct {
TomlTable* _table;
TomlKeyValue* _keyval;
} TomlTableIter;
typedef enum { typedef enum {
TOML_INVALID_TYPE,
TOML_TABLE, TOML_TABLE,
TOML_ARRAY, TOML_ARRAY,
TOML_STRING, TOML_STRING,
@ -50,58 +76,113 @@ typedef enum {
TOML_BOOLEAN, TOML_BOOLEAN,
} TomlType; } TomlType;
struct TomlValue { struct _TomlValue {
TomlType type; TomlType type;
union { union {
TomlTable* table; TomlTable* table;
TomlArray* array; TomlArray* array;
str s; TomlString* string;
i64 i; #if defined(_MSC_VER) || defined(__APPLE__)
f64 f; long long integer;
TomlDateTime* dt; #else
bool b; long integer;
#endif
double float_;
struct tm datetime;
int boolean;
} value; } value;
}; };
struct _TomlKeyValue {
TomlString* key;
TomlValue* value;
};
typedef struct {
void* (*malloc)(void *context, size_t size);
void* (*realloc)(void *context, void *p, size_t size);
void (*free)(void *context, void *p);
} TomlAllocFuncs;
void toml_set_allocator(void *context, const TomlAllocFuncs *funcs);
void* toml_malloc(size_t size);
void* toml_realloc(void *p, size_t size);
void toml_free(void *p);
char* toml_strdup(const char *str);
char* toml_strndup(const char *str, size_t n);
int toml_vasprintf(char **str, const char *format, va_list args);
int toml_asprintf(char **str, const char *format, ...);
const TomlErr* toml_err(void); const TomlErr* toml_err(void);
void toml_err_clear(void); void toml_err_clear(void);
TomlString* toml_string_new(void);
TomlString* toml_string_from_str(const char *str);
TomlString* toml_string_from_nstr(const char *str, size_t len);
void toml_string_append_char(TomlString *self, char ch);
void toml_string_append_str(TomlString *self, const char *str);
void toml_string_append_nstr(TomlString *self, const char *str, size_t len);
TomlString* toml_string_clone(const TomlString *self);
void toml_string_free(TomlString *self);
int toml_string_equals(const TomlString *self, const TomlString *other);
TomlTable* toml_table_new(void); TomlTable* toml_table_new(void);
void toml_table_free(TomlTable* self); void toml_table_free(TomlTable *self);
void toml_table_set(TomlTable* self, str key, TomlValue value); void toml_table_set_by_string(TomlTable *self, TomlString *key, TomlValue *value);
TomlValue* toml_table_get(const TomlTable* self, str key); TomlValue *toml_table_get_by_string(const TomlTable *self, const TomlString *key);
TomlTable* toml_table_get_table(const TomlTable* self, str key); void toml_table_set(TomlTable *self, const char *key, TomlValue *value);
TomlArray* toml_table_get_array(const TomlTable* self, str key); void toml_table_setn(TomlTable *self, const char *key, size_t key_len, TomlValue *value);
str toml_table_get_str(const TomlTable* self, str key); TomlValue* toml_table_get(const TomlTable *self, const char *key);
i64 toml_table_get_integer(const TomlTable* self, str key); TomlTable* toml_table_get_as_table(const TomlTable *self, const char *key);
f64 toml_table_get_float(const TomlTable* self, str key); TomlArray* toml_table_get_as_array(const TomlTable *self, const char *key);
TomlDateTime* toml_table_get_datetime(const TomlTable* self, str key); TomlString* toml_table_get_as_string(const TomlTable *self, const char *key);
bool toml_table_get_bool(const TomlTable* self, str key); #if defined(_MSC_VER) || defined(__APPLE__)
long long toml_table_get_as_integer(const TomlTable *self, const char *key);
#else
long toml_table_get_as_integer(const TomlTable *self, const char *key);
#endif
double toml_table_get_as_float(const TomlTable *self, const char *key);
const struct tm* toml_table_get_as_datetime(const TomlTable *self, const char *key);
int toml_table_get_as_boolean(const TomlTable *self, const char *key);
TomlValue* toml_table_getn(const TomlTable *self, const char *key, size_t key_len);
TomlTableIter toml_table_iter_new(TomlTable *table);
TomlKeyValue* toml_table_iter_get(TomlTableIter *self);
int toml_table_iter_has_next(TomlTableIter *self);
void toml_table_iter_next(TomlTableIter *self);
TomlArray* toml_array_new(void); TomlArray* toml_array_new(void);
void toml_array_free(TomlArray* self); void toml_array_free(TomlArray *self);
void toml_array_append(TomlArray* self, TomlValue value); void toml_array_append(TomlArray *self, TomlValue *value);
TomlValue TomlValue_new(TomlType type); TomlValue* toml_value_new(TomlType type);
TomlValue TomlValue_new_string(TomlType type); TomlValue* toml_value_new_string(TomlType type);
TomlValue TomlValue_new_table(void); TomlValue* toml_value_new_table(void);
TomlValue TomlValue_new_array(void); TomlValue* toml_value_new_array(void);
TomlValue TomlValue_new_integer(i64 integer); #if defined(_MSC_VER) || defined(__APPLE__)
TomlValue TomlValue_new_float(f64 flt); TomlValue *toml_value_new_integer(long long integer);
TomlValue TomlValue_new_datetime(void); #else
TomlValue TomlValue_new_bool(bool b); TomlValue *toml_value_new_integer(long integer);
TomlValue TomlValue_from_str(str s); #endif
void TomlValue_destroy(TomlValue* self); TomlValue* toml_value_new_float(double flt);
TomlValue* toml_value_new_datetime(void);
TomlValue* toml_value_new_boolean(int boolean);
TomlValue* toml_value_from_str(const char *str);
TomlValue* toml_value_from_nstr(const char *str, size_t len);
void toml_value_free(TomlValue *self);
TomlTable* toml_load_str(str s); TomlTable* toml_load_str(const char *str);
TomlTable* toml_load_file(FILE* file); TomlTable* toml_load_nstr(const char *str, size_t len);
TomlTable* toml_load_filename(cstr filename); TomlTable* toml_load_file(FILE *file);
TomlTable* toml_load_filename(const char *filename);
/* TODO: implement dump functions /* TODO: implement dump functions
str toml_dump_str(const TomlTable* self, TomlErr *err); char *toml_dump_str(const TomlTable *self, TomlErr *err);
void toml_dump_file(const TomlTable* self, FILE* file, TomlErr *err); TomlString *toml_dump_nstr(const TomlTable *self, TomlErr *err);
void toml_dump_file(const TomlTable *self, FILE *file, TomlErr *err);
*/ */
#ifdef __cplusplus #ifdef __cplusplus

1323
src/toml.c

File diff suppressed because it is too large Load Diff

View File

@ -3,34 +3,30 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <inttypes.h> #include <inttypes.h>
#include <assert.h>
#include "tlibtoml/toml.h" #include "tlibtoml/toml.h"
#ifndef PROJECT_SOURCE_DIR #ifndef PROJECT_SOURCE_DIR
#define PROJECT_SOURCE_DIR ".." #define PROJECT_SOURCE_DIR ".."
#endif #endif
void print_table(const TomlTable* table); void print_table(const TomlTable *table);
void print_value(const TomlValue* value); void print_value(const TomlValue *value);
void print_array(const TomlArray *array) void print_array(const TomlArray *array)
{ {
printf("["); printf("[");
for (u64 i = 0; i < array->len; i++) { for (size_t i = 0; i < array->len; i++) {
if (i > 0) { if (i > 0) {
printf(", "); printf(", ");
} }
print_value(&array->elements[i]); print_value(array->elements[i]);
} }
printf("]"); printf("]");
} }
void print_value(const TomlValue* value) void print_value(const TomlValue *value)
{ {
switch (value->type) { switch (value->type) {
default:
assert(false && "invalid type");
break;
case TOML_TABLE: case TOML_TABLE:
print_table(value->value.table); print_table(value->value.table);
break; break;
@ -38,50 +34,53 @@ void print_value(const TomlValue* value)
print_array(value->value.array); print_array(value->value.array);
break; break;
case TOML_STRING: case TOML_STRING:
printf("\"%s\"", value->value.s.data); printf("\"%s\"", value->value.string->str);
break; break;
case TOML_INTEGER: case TOML_INTEGER:
printf("%" PRId64, value->value.i); printf("%" PRId64, value->value.integer);
break; break;
case TOML_FLOAT: case TOML_FLOAT:
printf("%f", value->value.f); printf("%f", value->value.float_);
break; break;
case TOML_DATETIME: case TOML_DATETIME:
printf("(datetime)"); printf("(datetime)");
break; break;
case TOML_BOOLEAN: case TOML_BOOLEAN:
printf("%s", value->value.b ? "true" : "false"); printf("%s", value->value.boolean ? "true" : "false");
break; break;
} }
} }
void print_keyval(HashMapKeyValue* keyval) void print_keyval(const TomlKeyValue *keyval)
{ {
printf("\"%s\": ", keyval->key.data); printf("\"%s\": ", keyval->key->str);
print_value(keyval->value); print_value(keyval->value);
} }
void print_table(const TomlTable* table) void print_table(const TomlTable *table)
{ {
HashMapIter it = HashMapIter_create(table); TomlTableIter it = toml_table_iter_new((TomlTable *)table);
printf("{"); printf("{");
u64 i = 0; size_t i = 0;
HashMapKeyValue keyval; while (toml_table_iter_has_next(&it)) {
while (HashMapIter_moveNext(&it)) { TomlKeyValue *keyval = toml_table_iter_get(&it);
assert(HashMapIter_getCurrent(&it, &keyval));
if (i > 0) if (i > 0) {
printf(", "); printf(", ");
print_keyval(&keyval); }
print_keyval(keyval);
toml_table_iter_next(&it);
i++; i++;
} }
printf("}"); printf("}");
} }
i32 test_run(cstr filename) int test_run(const char *filename)
{ {
TomlTable* table = NULL; TomlTable *table = NULL;
i32 rc = 0; int rc = 0;
table = toml_load_filename(filename); table = toml_load_filename(filename);
if (table == NULL) if (table == NULL)
@ -95,15 +94,15 @@ cleanup:
if (toml_err()->code != TOML_OK) { if (toml_err()->code != TOML_OK) {
fprintf(stderr, "%s\n", toml_err()->message); fprintf(stderr, "%s\n", toml_err()->message);
rc = (i32)toml_err()->code; rc = (int)toml_err()->code;
} }
toml_err_clear(); toml_err_clear();
return rc; return rc;
} }
i32 main(void) int main(void)
{ {
static cstr const filenames[] = { static const char *const filenames[] = {
/* should parse */ /* should parse */
PROJECT_SOURCE_DIR "/tests/key-values.toml", PROJECT_SOURCE_DIR "/tests/key-values.toml",
PROJECT_SOURCE_DIR "/tests/complex-structure.toml", PROJECT_SOURCE_DIR "/tests/complex-structure.toml",
@ -118,12 +117,12 @@ i32 main(void)
PROJECT_SOURCE_DIR "/tests/hard_example_unicode.toml" PROJECT_SOURCE_DIR "/tests/hard_example_unicode.toml"
}; };
i32 total_tests = sizeof(filenames) / sizeof(char*); int total_tests = sizeof(filenames) / sizeof(char *);
i32 num_passed = 0; int num_passed = 0;
i32 num_failed = 0; int num_failed = 0;
for (i32 i = 0; i < total_tests; i++) { for (int i = 0; i < total_tests; i++) {
i32 rc = test_run(filenames[i]); int rc = test_run(filenames[i]);
if (rc == 0) { if (rc == 0) {
printf("test %d success\n", i); printf("test %d success\n", i);
num_passed++; num_passed++;