diff --git a/include/tlibtoml/toml.h b/include/tlibtoml/toml.h index 0b913ed..9d769a7 100644 --- a/include/tlibtoml/toml.h +++ b/include/tlibtoml/toml.h @@ -11,18 +11,8 @@ extern "C" { #include "tlibc/std.h" #include -#if defined(__cplusplus) && __cplusplus >= 201103L -#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 +#define false 0 +#define true 1 typedef enum { TOML_OK, @@ -36,28 +26,28 @@ typedef enum { typedef struct { TomlErrCode code; char* message; - int _is_literal; + i32 _is_literal; } TomlErr; typedef struct { char* str; - size_t len; - size_t _capacity; + u64 len; + u64 _capacity; } TomlString; typedef struct _TomlValue TomlValue; typedef struct { TomlValue** elements; - size_t len; - size_t _capacity; + u64 len; + u64 _capacity; } TomlArray; typedef struct _TomlKeyValue TomlKeyValue; typedef struct { - size_t _capacity; - size_t len; + u64 _capacity; + u64 len; TomlKeyValue* _keyvals; } TomlTable; @@ -82,14 +72,10 @@ struct _TomlValue { TomlTable* table; TomlArray* array; TomlString* string; -#if defined(_MSC_VER) || defined(__APPLE__) - long long integer; -#else - long integer; -#endif - double float_; + i64 integer; + f64 float_; struct tm datetime; - int boolean; + bool boolean; } value; }; @@ -99,90 +85,82 @@ struct _TomlKeyValue { }; typedef struct { - void* (*malloc)(void *context, size_t size); - void* (*realloc)(void *context, void *p, size_t size); - void (*free)(void *context, void *p); + 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_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); +void* toml_malloc(u64 size); +void* toml_realloc(void* p, u64 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, ...); +char* toml_strdup(cstr str); +char* toml_strndup(cstr str, u64 n); +i32 toml_vasprintf(char** str, cstr format, va_list args); +i32 toml_asprintf(char** str, cstr format, ...); const TomlErr* toml_err(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); +TomlString* toml_string_from_str(cstr str); +TomlString* toml_string_from_nstr(cstr str, u64 len); +void toml_string_append_char(TomlString* self, char ch); +void toml_string_append_str(TomlString* self, cstr str); +void toml_string_append_nstr(TomlString* self, cstr str, u64 len); +TomlString* toml_string_clone(const TomlString* self); +void toml_string_free(TomlString* self); +i32 toml_string_equals(const TomlString* self, const TomlString* other); TomlTable* toml_table_new(void); -void toml_table_free(TomlTable *self); +void toml_table_free(TomlTable* self); -void toml_table_set_by_string(TomlTable *self, TomlString *key, TomlValue *value); -TomlValue *toml_table_get_by_string(const TomlTable *self, const TomlString *key); -void toml_table_set(TomlTable *self, const char *key, TomlValue *value); -void toml_table_setn(TomlTable *self, const char *key, size_t key_len, TomlValue *value); -TomlValue* toml_table_get(const TomlTable *self, const char *key); -TomlTable* toml_table_get_as_table(const TomlTable *self, const char *key); -TomlArray* toml_table_get_as_array(const TomlTable *self, const char *key); -TomlString* toml_table_get_as_string(const TomlTable *self, const char *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); +void toml_table_set_by_string(TomlTable* self, TomlString* key, TomlValue* value); +TomlValue* toml_table_get_by_string(const TomlTable* self, const TomlString* key); +void toml_table_set(TomlTable* self, cstr key, TomlValue* value); +void toml_table_setn(TomlTable* self, cstr key, u64 key_len, TomlValue* value); +TomlValue* toml_table_get(const TomlTable* self, cstr key); +TomlTable* toml_table_get_as_table(const TomlTable* self, cstr key); +TomlArray* toml_table_get_as_array(const TomlTable* self, cstr key); +TomlString* toml_table_get_as_string(const TomlTable* self, cstr key); +i64 toml_table_get_as_integer(const TomlTable* self, cstr key); +f64 toml_table_get_as_float(const TomlTable* self, cstr key); +const struct tm* toml_table_get_as_datetime(const TomlTable* self, cstr key); +i32 toml_table_get_as_boolean(const TomlTable* self, cstr key); +TomlValue* toml_table_getn(const TomlTable* self, cstr key, u64 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); +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); +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); -#if defined(_MSC_VER) || defined(__APPLE__) -TomlValue *toml_value_new_integer(long long integer); -#else -TomlValue *toml_value_new_integer(long integer); -#endif -TomlValue* toml_value_new_float(double flt); +TomlValue* toml_value_new_integer(i64 integer); +TomlValue* toml_value_new_float(f64 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); +TomlValue* toml_value_new_boolean(i32 boolean); +TomlValue* toml_value_from_str(cstr str); +TomlValue* toml_value_from_nstr(cstr str, u64 len); +void toml_value_free(TomlValue* self); -TomlTable* toml_load_str(const char *str); -TomlTable* toml_load_nstr(const char *str, size_t len); -TomlTable* toml_load_file(FILE *file); -TomlTable* toml_load_filename(const char *filename); +TomlTable* toml_load_str(cstr str); +TomlTable* toml_load_nstr(cstr str, u64 len); +TomlTable* toml_load_file(FILE* file); +TomlTable* toml_load_filename(cstr filename); /* TODO: implement dump functions -char *toml_dump_str(const TomlTable *self, TomlErr *err); -TomlString *toml_dump_nstr(const TomlTable *self, TomlErr *err); -void toml_dump_file(const TomlTable *self, FILE *file, TomlErr *err); +char* toml_dump_str(const TomlTable* self, TomlErr *err); +TomlString* toml_dump_nstr(const TomlTable* self, TomlErr *err); +void toml_dump_file(const TomlTable* self, FILE* file, TomlErr *err); */ #ifdef __cplusplus diff --git a/src/toml.c b/src/toml.c index f460949..a673fa9 100644 --- a/src/toml.c +++ b/src/toml.c @@ -10,25 +10,25 @@ #include #include "tlibtoml/toml.h" -static ATTRIBUTE_THREAD_LOCAL TomlErr g_err = {TOML_OK, (char*)"", TOML_TRUE}; +static ATTRIBUTE_THREAD_LOCAL TomlErr g_err = {TOML_OK, (char*)"", true}; -static void* toml_default_malloc(void *context, size_t size) +static void* toml_default_malloc(void* context, u64 size) { (void)context; - void *p = malloc(size); + void* p = malloc(size); assert(p != NULL); return p; } -static void* toml_default_realloc(void *context, void *p, size_t size) +static void* toml_default_realloc(void* context, void* p, u64 size) { (void)context; - void *ptr = realloc(p, size); + void* ptr = realloc(p, size); assert(ptr != NULL); return ptr; } -static void toml_default_free(void *context, void *p) +static void toml_default_free(void* context, void* p) { (void)context; free(p); @@ -40,45 +40,45 @@ static TomlAllocFuncs g_default_alloc_funcs = { &toml_default_free }; -static void *g_alloc_context = NULL; +static void* g_alloc_context = NULL; static const TomlAllocFuncs* g_alloc_funcs = &g_default_alloc_funcs; -void toml_set_allocator(void *context, const TomlAllocFuncs *funcs) +void toml_set_allocator(void* context, const TomlAllocFuncs *funcs) { g_alloc_context = context; g_alloc_funcs = funcs; } -void* toml_malloc(size_t size) +void* toml_malloc(u64 size) { return g_alloc_funcs->malloc(g_alloc_context, size); } -void* toml_realloc(void *p, size_t size) +void* toml_realloc(void* p, u64 size) { return g_alloc_funcs->realloc(g_alloc_context, p, size); } -void toml_free(void *p) +void toml_free(void* p) { if (p != NULL) { g_alloc_funcs->free(g_alloc_context, p); } } -char* toml_strdup(const char *str) +char* toml_strdup(cstr str) { - size_t len = strlen(str) + 1; - void *new = toml_malloc(len); + u64 len = strlen(str) + 1; + void* new = toml_malloc(len); if (new == NULL) return NULL; return memcpy(new, str, len); } -char *toml_strndup(const char *str, size_t n) +char* toml_strndup(cstr str, u64 n) { - char *result = toml_malloc(n + 1); + char* result = toml_malloc(n + 1); if (result == NULL) return NULL; @@ -86,31 +86,31 @@ char *toml_strndup(const char *str, size_t n) return memcpy(result, str, n); } -int toml_vasprintf(char **str, const char *format, va_list args) +i32 toml_vasprintf(char** str, cstr format, va_list args) { - int size = 0; + i32 size = 0; va_list args_copy; va_copy(args_copy, args); - size = vsnprintf(NULL, (size_t)size, format, args_copy); + size = vsnprintf(NULL, (u64)size, format, args_copy); va_end(args_copy); if (size < 0) { return size; } - *str = toml_malloc((size_t)size + 1); + *str = toml_malloc((u64)size + 1); if (*str == NULL) return -1; return vsprintf(*str, format, args); } -int toml_asprintf(char **str, const char *format, ...) +i32 toml_asprintf(char** str, cstr format, ...) { va_list args; va_start(args, format); - int size = toml_vasprintf(str, format, args); + i32 size = toml_vasprintf(str, format, args); va_end(args); return size; } @@ -128,84 +128,76 @@ void toml_err_clear(void) } g_err.code = TOML_OK; g_err.message = (char*)""; - g_err._is_literal = TOML_TRUE; + g_err._is_literal = true; } } -static inline void toml_err_set(TomlErrCode code, const char *format, ...) +static inline void toml_err_set(TomlErrCode code, cstr format, ...) { assert(g_err.code == TOML_OK); va_list args; va_start(args, format); g_err.code = code; toml_vasprintf(&g_err.message, format, args); - g_err._is_literal = TOML_FALSE; + g_err._is_literal = false; va_end(args); } -static inline void toml_err_set_literal(TomlErrCode code, const char *message) +static inline void toml_err_set_literal(TomlErrCode code, cstr message) { assert(g_err.code == TOML_OK); g_err.code = code; - g_err.message = (char *)message; - g_err._is_literal = TOML_TRUE; + g_err.message = (char* )message; + g_err._is_literal = true; } -static inline size_t toml_roundup_pow_of_two_size_t(size_t x) +static inline u64 toml_roundup_pow_of_two_u64(u64 v) { - size_t v = x; v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; -#if SIZE_MAX == 0xffff - v |= v >> 8; -#elif SIZE_MAX == 0xffffffff - v |= v >> 8; - v |= v >> 16; -#elif SIZE_MAX == 0xffffffffffffffffll v |= v >> 8; v |= v >> 16; v |= v >> 32; -#endif v++; return v; } -TomlString *toml_string_new(void) +TomlString* toml_string_new(void) { - TomlString *self = toml_malloc(sizeof(TomlString)); + TomlString* self = toml_malloc(sizeof(TomlString)); self->str = NULL; self->len = 0; self->_capacity = 0; return self; } -TomlString *toml_string_from_str(const char *str) +TomlString* toml_string_from_str(cstr str) { - TomlString *self = toml_string_new(); + TomlString* self = toml_string_new(); toml_string_append_str(self, str); return self; } -TomlString *toml_string_from_nstr(const char *str, size_t len) +TomlString* toml_string_from_nstr(cstr str, u64 len) { - TomlString *self = toml_string_new(); + TomlString* self = toml_string_new(); toml_string_append_nstr(self, str, len); return self; } -static inline void toml_string_expand_if_necessary(TomlString *self, size_t len_to_add) +static inline void toml_string_expand_if_necessary(TomlString* self, u64 len_to_add) { if (self->len + len_to_add + 1 > self->_capacity) { - size_t new_capacity = toml_roundup_pow_of_two_size_t(self->len + len_to_add + 1); + u64 new_capacity = toml_roundup_pow_of_two_u64(self->len + len_to_add + 1); new_capacity = new_capacity >= 8 ? new_capacity : 8; self->str = toml_realloc(self->str, new_capacity); self->_capacity = new_capacity; } } -void toml_string_append_char(TomlString *self, char ch) +void toml_string_append_char(TomlString* self, char ch) { toml_string_expand_if_necessary(self, 1); self->str[self->len] = ch; @@ -213,15 +205,15 @@ void toml_string_append_char(TomlString *self, char ch) self->len++; } -void toml_string_append_str(TomlString *self, const char *str) +void toml_string_append_str(TomlString* self, cstr str) { - size_t len = strlen(str); + u64 len = strlen(str); toml_string_expand_if_necessary(self, len); memcpy(&self->str[self->len], str, len + 1); self->len += len; } -void toml_string_append_nstr(TomlString *self, const char *str, size_t len) +void toml_string_append_nstr(TomlString* self, cstr str, u64 len) { toml_string_expand_if_necessary(self, len); memcpy(&self->str[self->len], str, len); @@ -229,7 +221,7 @@ void toml_string_append_nstr(TomlString *self, const char *str, size_t len) self->str[self->len] = 0; } -void toml_string_free(TomlString *self) +void toml_string_free(TomlString* self) { if (self != NULL) { free(self->str); @@ -237,47 +229,47 @@ void toml_string_free(TomlString *self) } } -TomlString *toml_string_clone(const TomlString *self) +TomlString* toml_string_clone(const TomlString* self) { return toml_string_from_nstr(self->str, self->len); } -int toml_string_equals(const TomlString *self, const TomlString *other) +i32 toml_string_equals(const TomlString* self, const TomlString* other) { if (self == other) { - return TOML_TRUE; + return true; } if (self->len != other->len) { - return TOML_FALSE; + return false; } if (self->str == other->str) { - return TOML_TRUE; + return true; } - for (size_t i = 0; i < self->len; i++) { + for (u64 i = 0; i < self->len; i++) { if (self->str[i] != other->str[i]) { - return TOML_FALSE; + return false; } } - return TOML_TRUE; + return true; } -TomlTable *toml_table_new(void) +TomlTable* toml_table_new(void) { - TomlTable *self = toml_malloc(sizeof(TomlTable)); + TomlTable* self = toml_malloc(sizeof(TomlTable)); self->_capacity = 0; self->_keyvals = NULL; self->len = 0; return self; } -void toml_table_free(TomlTable *self) +void toml_table_free(TomlTable* self) { if (self != NULL) { - for (size_t i = 0; i < self->len; i++) { + for (u64 i = 0; i < self->len; i++) { toml_string_free(self->_keyvals[i].key); toml_value_free(self->_keyvals[i].value); } @@ -286,20 +278,20 @@ void toml_table_free(TomlTable *self) } } -void toml_table_expand_if_necessary(TomlTable *self) +void toml_table_expand_if_necessary(TomlTable* self) { if (self->len + 1 > self->_capacity) { - size_t new_capacity = self->_capacity > 0 ? self->_capacity * 2 : 8; - void *p = toml_realloc(self->_keyvals, sizeof(TomlKeyValue) * new_capacity); + u64 new_capacity = self->_capacity > 0 ? self->_capacity * 2 : 8; + void* p = toml_realloc(self->_keyvals, sizeof(TomlKeyValue) * new_capacity); self->_keyvals = p; self->_capacity = new_capacity; } } -void toml_table_set_by_string(TomlTable *self, TomlString *key, TomlValue *value) +void toml_table_set_by_string(TomlTable* self, TomlString* key, TomlValue* value) { - TomlValue **slot = NULL; - for (size_t i = 0; i < self->len; i++) { + TomlValue** slot = NULL; + for (u64 i = 0; i < self->len; i++) { if (toml_string_equals(self->_keyvals[i].key, key)) { slot = &self->_keyvals[i].value; } @@ -315,10 +307,10 @@ void toml_table_set_by_string(TomlTable *self, TomlString *key, TomlValue *value } } -TomlValue *toml_table_get_by_string(const TomlTable *self, const TomlString *key) +TomlValue* toml_table_get_by_string(const TomlTable* self, const TomlString* key) { - TomlValue *value = NULL; - for (size_t i = 0; i < self->len; i++) { + TomlValue* value = NULL; + for (u64 i = 0; i < self->len; i++) { if (toml_string_equals(self->_keyvals[i].key, key)) { value = self->_keyvals[i].value; } @@ -326,105 +318,101 @@ TomlValue *toml_table_get_by_string(const TomlTable *self, const TomlString *key return value; } -TomlValue *toml_table_getn(const TomlTable *self, const char *key, size_t key_len) +TomlValue* toml_table_getn(const TomlTable* self, cstr key, u64 key_len) { - TomlString str = {(char *)key, key_len, 0}; + TomlString str = {(char* )key, key_len, 0}; return toml_table_get_by_string(self, &str); } -TomlValue *toml_table_get(const TomlTable *self, const char *key) +TomlValue* toml_table_get(const TomlTable* self, cstr key) { return toml_table_getn(self, key, strlen(key)); } -TomlTable* toml_table_get_as_table(const TomlTable *self, const char *key) +TomlTable* toml_table_get_as_table(const TomlTable* self, cstr key) { - TomlValue *v = toml_table_get(self, key); + TomlValue* v = toml_table_get(self, key); assert(v != NULL); assert(v->type == TOML_TABLE); return v->value.table; } -TomlArray* toml_table_get_as_array(const TomlTable *self, const char *key) +TomlArray* toml_table_get_as_array(const TomlTable* self, cstr key) { - TomlValue *v = toml_table_get(self, key); + TomlValue* v = toml_table_get(self, key); assert(v != NULL); assert(v->type == TOML_ARRAY); return v->value.array; } -TomlString* toml_table_get_as_string(const TomlTable *self, const char *key) +TomlString* toml_table_get_as_string(const TomlTable* self, cstr key) { - TomlValue *v = toml_table_get(self, key); + TomlValue* v = toml_table_get(self, key); assert(v != NULL); assert(v->type == TOML_STRING); return v->value.string; } -#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 +i64 toml_table_get_as_integer(const TomlTable* self, cstr key) { - TomlValue *v = toml_table_get(self, key); + TomlValue* v = toml_table_get(self, key); assert(v != NULL); assert(v->type == TOML_INTEGER); return v->value.integer; } -double toml_table_get_as_float(const TomlTable *self, const char *key) +f64 toml_table_get_as_float(const TomlTable* self, cstr key) { - TomlValue *v = toml_table_get(self, key); + TomlValue* v = toml_table_get(self, key); assert(v != NULL); assert(v->type == TOML_FLOAT); return v->value.float_; } -const struct tm* toml_table_get_as_datetime(const TomlTable *self, const char *key) +const struct tm* toml_table_get_as_datetime(const TomlTable* self, cstr key) { - TomlValue *v = toml_table_get(self, key); + TomlValue* v = toml_table_get(self, key); assert(v != NULL); assert(v->type == TOML_DATETIME); return &v->value.datetime; } -int toml_table_get_as_boolean(const TomlTable *self, const char *key) +i32 toml_table_get_as_boolean(const TomlTable* self, cstr key) { - TomlValue *v = toml_table_get(self, key); + TomlValue* v = toml_table_get(self, key); assert(v != NULL); assert(v->type == TOML_BOOLEAN); return v->value.boolean; } -void toml_table_setn(TomlTable *self, const char *key, size_t key_len, TomlValue *value) +void toml_table_setn(TomlTable* self, cstr key, u64 key_len, TomlValue* value) { - TomlString *str = toml_string_from_nstr(key, key_len); + TomlString* str = toml_string_from_nstr(key, key_len); toml_table_set_by_string(self, str, value); } -void toml_table_set(TomlTable *self, const char *key, TomlValue *value) +void toml_table_set(TomlTable* self, cstr key, TomlValue* value) { toml_table_setn(self, key, strlen(key), value); } -TomlTableIter toml_table_iter_new(TomlTable *table) +TomlTableIter toml_table_iter_new(TomlTable* table) { TomlTableIter self = { table, table->_keyvals }; return self; } -TomlKeyValue* toml_table_iter_get(TomlTableIter *self) +TomlKeyValue* toml_table_iter_get(TomlTableIter* self) { return self->_keyval; } -int toml_table_iter_has_next(TomlTableIter *self) +i32 toml_table_iter_has_next(TomlTableIter* self) { return self->_keyval != NULL; } -void toml_table_iter_next(TomlTableIter *self) +void toml_table_iter_next(TomlTableIter* self) { if (self->_keyval < self->_table->_keyvals + self->_table->len) { self->_keyval++; @@ -437,17 +425,17 @@ void toml_table_iter_next(TomlTableIter *self) TomlArray *toml_array_new(void) { - TomlArray *self = toml_malloc(sizeof(TomlArray)); + TomlArray* self = toml_malloc(sizeof(TomlArray)); self->elements = NULL; self->len = 0; self->_capacity = 0; return self; } -void toml_array_free(TomlArray *self) +void toml_array_free(TomlArray* self) { if (self != NULL) { - for (size_t i = 0; i < self->len; i++) { + for (u64 i = 0; i < self->len; i++) { toml_value_free(self->elements[i]); } free(self->elements); @@ -455,25 +443,25 @@ void toml_array_free(TomlArray *self) } } -void toml_array_expand_if_necessary(TomlArray *self) +void toml_array_expand_if_necessary(TomlArray* self) { if (self->len + 1 > self->_capacity) { - size_t new_capacity = self->_capacity > 0 ? self->_capacity * 2 : 8; - void *p = toml_realloc(self->elements, sizeof(TomlValue *) * new_capacity); + u64 new_capacity = self->_capacity > 0 ? self->_capacity * 2 : 8; + void* p = toml_realloc(self->elements, sizeof(TomlValue* ) * new_capacity); self->elements = p; self->_capacity = new_capacity; } } -void toml_array_append(TomlArray *self, TomlValue *value) +void toml_array_append(TomlArray* self, TomlValue* value) { toml_array_expand_if_necessary(self); self->elements[self->len++] = value; } -TomlValue *toml_value_new(TomlType type) +TomlValue* toml_value_new(TomlType type) { - TomlValue *self = toml_malloc(sizeof(TomlValue)); + TomlValue* self = toml_malloc(sizeof(TomlValue)); self->type = type; switch (type) { case TOML_TABLE: @@ -492,7 +480,7 @@ TomlValue *toml_value_new(TomlType type) self->value.float_ = 0.0; break; case TOML_BOOLEAN: - self->value.boolean = TOML_FALSE; + self->value.boolean = false; break; case TOML_DATETIME: memset(&self->value.datetime, 0, sizeof(struct tm)); @@ -501,72 +489,68 @@ TomlValue *toml_value_new(TomlType type) return self; } -TomlValue *toml_value_from_str(const char *str) +TomlValue* toml_value_from_str(cstr str) { - TomlValue *self = toml_malloc(sizeof(TomlValue)); + TomlValue* self = toml_malloc(sizeof(TomlValue)); self->value.string = toml_string_from_str(str); self->type = TOML_STRING; return self; } -TomlValue *toml_value_from_nstr(const char *str, size_t len) +TomlValue* toml_value_from_nstr(cstr str, u64 len) { - TomlValue *self = toml_malloc(sizeof(TomlValue)); + TomlValue* self = toml_malloc(sizeof(TomlValue)); self->value.string = toml_string_from_nstr(str, len); self->type = TOML_STRING; return self; } -TomlValue *toml_value_new_table(void) +TomlValue* toml_value_new_table(void) { - TomlValue *self = toml_malloc(sizeof(TomlValue)); + TomlValue* self = toml_malloc(sizeof(TomlValue)); self->value.table = toml_table_new(); self->type = TOML_TABLE; return self; } -TomlValue *toml_value_new_array(void) +TomlValue* toml_value_new_array(void) { - TomlValue *self = toml_malloc(sizeof(TomlValue)); + TomlValue* self = toml_malloc(sizeof(TomlValue)); self->value.array = toml_array_new(); self->type = TOML_ARRAY; return self; } -#if defined(_MSC_VER) || defined(__APPLE__) -TomlValue *toml_value_new_integer(long long integer) -#else -TomlValue *toml_value_new_integer(long integer) -#endif +TomlValue* toml_value_new_integer(i64 integer) { - TomlValue *self = toml_malloc(sizeof(TomlValue)); + TomlValue* self = toml_malloc(sizeof(TomlValue)); self->value.integer = integer; self->type = TOML_INTEGER; return self; } -TomlValue *toml_value_new_float(double float_) +TomlValue* toml_value_new_float(f64 float_) { - TomlValue *self = toml_malloc(sizeof(TomlValue)); + TomlValue* self = toml_malloc(sizeof(TomlValue)); self->value.float_ = float_; self->type = TOML_FLOAT; return self; } -TomlValue *toml_value_new_datetime(void) +TomlValue* toml_value_new_datetime(void) { return toml_value_new(TOML_DATETIME); } -TomlValue *toml_value_new_boolean(int boolean) +TomlValue* toml_value_new_boolean(i32 boolean) { - TomlValue *self = toml_malloc(sizeof(TomlValue)); + TomlValue* self = toml_malloc(sizeof(TomlValue)); self->value.boolean = boolean; self->type = TOML_BOOLEAN; return self; } -void toml_value_free(TomlValue *self) +void toml_value_free(TomlValue* self) { if (self != NULL) { switch (self->type) { @@ -590,17 +574,17 @@ void toml_value_free(TomlValue *self) } typedef struct _TomlParser { - const char* begin; - const char* end; - const char* ptr; - int lineno; - int colno; - char* filename; + cstr begin; + cstr end; + cstr ptr; + i32 lineno; + i32 colno; + char* filename; } TomlParser; -TomlParser *toml_parser_new(const char *str, size_t len) +TomlParser *toml_parser_new(cstr str, u64 len) { - TomlParser *self = toml_malloc(sizeof(TomlParser)); + TomlParser* self = toml_malloc(sizeof(TomlParser)); self->begin = str; self->end = str + len; self->ptr = str; @@ -610,7 +594,7 @@ TomlParser *toml_parser_new(const char *str, size_t len) return self; } -void toml_parser_free(TomlParser *self) +void toml_parser_free(TomlParser* self) { if (self != NULL) { free(self->filename); @@ -618,7 +602,7 @@ void toml_parser_free(TomlParser *self) } } -void toml_move_next(TomlParser *self) +void toml_move_next(TomlParser* self) { if (self->ptr < self->end) { if (*self->ptr == '\n') { @@ -631,20 +615,20 @@ void toml_move_next(TomlParser *self) } } -void toml_next_n(TomlParser *self, int n) +void toml_next_n(TomlParser* self, i32 n) { - for (int i = 0; i < n; i++) { + for (i32 i = 0; i < n; i++) { toml_move_next(self); } } -TomlString* toml_parse_bare_key(TomlParser *self) +TomlString* toml_parse_bare_key(TomlParser* self) { - const char *str = self->ptr; - size_t len = 0; + cstr str = self->ptr; + u64 len = 0; while (self->ptr < self->end) { - char ch = *self->ptr; + char ch =* self->ptr; if (!(isalnum(ch) || ch == '_' || ch == '-')) break; @@ -669,9 +653,9 @@ char toml_hex_char_to_int(char ch) return 0; } -int toml_encode_unicode_scalar(TomlString *result, TomlParser *parser, int n) +i32 toml_encode_unicode_scalar(TomlString* result, TomlParser *parser, i32 n) { - unsigned int scalar = 0; + u32 scalar = 0; if (parser->ptr + n > parser->end) { toml_err_set(TOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar", @@ -679,10 +663,10 @@ int toml_encode_unicode_scalar(TomlString *result, TomlParser *parser, int n) return TOML_ERR_UNICODE; } - for (int i = 0; i < n; i++) { + for (i32 i = 0; i < n; i++) { char ch = *parser->ptr; if (isxdigit(ch)) { - scalar = scalar * 16 + (unsigned int)toml_hex_char_to_int(ch); + scalar = scalar * 16 + (u32)toml_hex_char_to_int(ch); toml_move_next(parser); } else { toml_err_set(TOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar", @@ -749,17 +733,17 @@ int toml_encode_unicode_scalar(TomlString *result, TomlParser *parser, int n) return TOML_ERR_UNICODE; } -TomlString* toml_parse_basic_string(TomlParser *self) +TomlString* toml_parse_basic_string(TomlParser* self) { - TomlString *result = toml_string_new(); + TomlString* result = toml_string_new(); - while (self->ptr < self->end && *self->ptr != '\"' && *self->ptr != '\n') { - char ch1 = *self->ptr; + while (self->ptr < self->end &&* self->ptr != '\"' &&* self->ptr != '\n') { + char ch1 =* self->ptr; if (ch1 == '\\') { if (self->ptr >= self->end) break; toml_move_next(self); - char ch2 = *self->ptr; + char ch2 =* self->ptr; if (ch2 == '\"') { toml_string_append_char(result, '\"'); @@ -807,7 +791,7 @@ TomlString* toml_parse_basic_string(TomlParser *self) } } - if (self->ptr >= self->end || *self->ptr != '\"' || *self->ptr == '\n') { + if (self->ptr >= self->end ||* self->ptr != '\"' ||* self->ptr == '\n') { toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated basic string", self->filename, self->lineno, self->colno); toml_string_free(result); @@ -818,16 +802,16 @@ TomlString* toml_parse_basic_string(TomlParser *self) return result; } -TomlString* toml_parse_literal_string(TomlParser *self) +TomlString* toml_parse_literal_string(TomlParser* self) { - TomlString *result = toml_string_new(); + TomlString* result = toml_string_new(); - while (self->ptr < self->end && *self->ptr != '\'' && *self->ptr != '\n') { - toml_string_append_char(result, *self->ptr); + while (self->ptr < self->end &&* self->ptr != '\'' &&* self->ptr != '\n') { + toml_string_append_char(result,* self->ptr); toml_move_next(self); } - if (self->ptr >= self->end || *self->ptr != '\'' || *self->ptr == '\n') { + if (self->ptr >= self->end ||* self->ptr != '\'' ||* self->ptr == '\n') { toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated literal string", self->filename, self->lineno, self->colno); toml_string_free(result); @@ -839,11 +823,11 @@ TomlString* toml_parse_literal_string(TomlParser *self) return result; } -TomlValue* toml_parse_basic_string_value(TomlParser *self) +TomlValue* toml_parse_basic_string_value(TomlParser* self) { - TomlValue *value = NULL; + TomlValue* value = NULL; - TomlString *string = toml_parse_basic_string(self); + TomlString* string = toml_parse_basic_string(self); if (string == NULL) return NULL; @@ -853,11 +837,11 @@ TomlValue* toml_parse_basic_string_value(TomlParser *self) return value; } -TomlValue *toml_parse_literal_string_value(TomlParser *self) +TomlValue* toml_parse_literal_string_value(TomlParser* self) { - TomlValue *value = NULL; + TomlValue* value = NULL; - TomlString *string = toml_parse_literal_string(self); + TomlString* string = toml_parse_literal_string(self); if (string == NULL) return NULL; @@ -867,25 +851,25 @@ TomlValue *toml_parse_literal_string_value(TomlParser *self) return value; } -TomlValue* toml_parse_multi_line_basic_string(TomlParser *self) +TomlValue* toml_parse_multi_line_basic_string(TomlParser* self) { - TomlValue *value = NULL; + TomlValue* value = NULL; - TomlString *result = toml_string_new(); + TomlString* result = toml_string_new(); if (*self->ptr == '\n') { toml_move_next(self); } while (self->ptr + 3 <= self->end && strncmp(self->ptr, "\"\"\"", 3) != 0) { - char ch1 = *self->ptr; + char ch1 =* self->ptr; if (ch1 == '\\') { if (self->ptr + 3 > self->end || strncmp(self->ptr, "\"\"\"", 3) == 0) break; toml_move_next(self); - char ch2 = *self->ptr; + char ch2 =* self->ptr; if (ch2 == '\"') { toml_string_append_char(result, '\"'); @@ -954,18 +938,18 @@ TomlValue* toml_parse_multi_line_basic_string(TomlParser *self) return value; } -TomlValue* toml_parse_multi_line_literal_string(TomlParser *self) +TomlValue* toml_parse_multi_line_literal_string(TomlParser* self) { - TomlValue *value = NULL; + TomlValue* value = NULL; - TomlString *result = toml_string_new(); + TomlString* result = toml_string_new(); if (*self->ptr == '\n') { toml_move_next(self); } while (self->ptr + 3 <= self->end && strncmp(self->ptr, "\'\'\'", 3) != 0) { - toml_string_append_char(result, *self->ptr); + toml_string_append_char(result,* self->ptr); toml_move_next(self); } @@ -984,20 +968,20 @@ TomlValue* toml_parse_multi_line_literal_string(TomlParser *self) return value; } -TomlValue* toml_parse_datetime(const char *str, size_t len) +TomlValue* toml_parse_datetime(cstr str, u64 len) { (void)str; (void)len; return toml_value_new(TOML_DATETIME); } -TomlValue* toml_parse_int_or_float_or_time(TomlParser *self) +TomlValue* toml_parse_int_or_float_or_time(TomlParser* self) { - TomlString *str = NULL; - TomlValue *result = NULL; + TomlString* str = NULL; + TomlValue* result = NULL; char type = 'i'; - int base = 10; + i32 base = 10; str = toml_string_new(); @@ -1032,28 +1016,28 @@ TomlValue* toml_parse_int_or_float_or_time(TomlParser *self) } char last_char = 0; - int has_exp = TOML_FALSE; + i32 has_exp = false; while (self->ptr < self->end) { - if (*self->ptr == '+' || *self->ptr == '-') { + if (*self->ptr == '+' ||* self->ptr == '-') { if (last_char == 0 || ((last_char == 'e' || last_char == 'E') && !has_exp)) { if (last_char != 0) { type = 'f'; - has_exp = TOML_TRUE; + has_exp = true; } - toml_string_append_char(str, *self->ptr); + toml_string_append_char(str,* self->ptr); } else { break; } } else if (isalnum(*self->ptr)) { - if ((*self->ptr == 'e' || *self->ptr == 'E') && base == 10) { + if ((*self->ptr == 'e' ||* self->ptr == 'E') && base == 10) { type = 'f'; } - toml_string_append_char(str, *self->ptr); + toml_string_append_char(str,* self->ptr); } else if (*self->ptr == '.') { if (type == 'i') { type = 'f'; - toml_string_append_char(str, *self->ptr); + toml_string_append_char(str,* self->ptr); } else { toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid float", self->filename, self->lineno, self->colno); @@ -1076,12 +1060,12 @@ TomlValue* toml_parse_int_or_float_or_time(TomlParser *self) } } else if (*self->ptr == '-') { type = 't'; - toml_string_append_char(str, *self->ptr); + toml_string_append_char(str,* self->ptr); } else { break; } - last_char = *self->ptr; + last_char =* self->ptr; toml_move_next(self); } @@ -1093,8 +1077,8 @@ TomlValue* toml_parse_int_or_float_or_time(TomlParser *self) } if (type == 'i') { - char *end = NULL; - long long n = strtoll(str->str, &end, base); + char* end = NULL; + i64 n = strtoll(str->str, &end, base); if (end < str->str + str->len) { toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid integer", self->filename, self->lineno, self->colno); @@ -1103,8 +1087,8 @@ TomlValue* toml_parse_int_or_float_or_time(TomlParser *self) } result = toml_value_new_integer(n); } else if (type == 'f') { - char *end = NULL; - double n = strtod(str->str, &end); + char* end = NULL; + f64 n = strtod(str->str, &end); if (end < str->str + str->len) { toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid float"); goto cleanup; @@ -1119,31 +1103,31 @@ cleanup: return result; } -TomlValue* toml_parse_bool(TomlParser *self) +TomlValue* toml_parse_bool(TomlParser* self) { if (self->ptr + 4 <= self->end && strncmp(self->ptr, "true", 4) == 0 && (self->ptr + 4 == self->end || isspace(*(self->ptr + 4)) || *(self->ptr + 4) == ',' || *(self->ptr + 4) == ']' || *(self->ptr + 4) == '}')) { toml_next_n(self, 4); - return toml_value_new_boolean(TOML_TRUE); + return toml_value_new_boolean(true); } if (self->ptr + 5 <= self->end && strncmp(self->ptr, "false", 5) == 0 && (self->ptr + 5 == self->end || isspace(*(self->ptr + 5)) || *(self->ptr + 5) == ',' || *(self->ptr + 5) == ']' || *(self->ptr + 5) == '}')) { toml_next_n(self, 5); - return toml_value_new_boolean(TOML_FALSE); + return toml_value_new_boolean(false); } return NULL; } -TomlValue* toml_parse_array(TomlParser *self); -TomlValue* toml_parse_inline_table(TomlParser *self); +TomlValue* toml_parse_array(TomlParser* self); +TomlValue* toml_parse_inline_table(TomlParser* self); -TomlValue* toml_parse_value(TomlParser *self) +TomlValue* toml_parse_value(TomlParser* self) { - TomlValue *value = NULL; + TomlValue* value = NULL; - char ch = *self->ptr; + char ch =* self->ptr; if (strncmp(self->ptr, "\"\"\"", 3) == 0) { toml_next_n(self, 3); @@ -1175,18 +1159,18 @@ TomlValue* toml_parse_value(TomlParser *self) return value; } -TomlErrCode toml_parse_key_value(TomlParser *self, TomlTable *table) +TomlErrCode toml_parse_key_value(TomlParser* self, TomlTable* table) { - TomlString *key = NULL; - TomlValue *value = NULL; + TomlString* key = NULL; + TomlValue* value = NULL; while (self->ptr < self->end) { char ch; - ch = *self->ptr; + ch =* self->ptr; while (self->ptr < self->end && isspace(ch)) { toml_move_next(self); - ch = *self->ptr; + ch =* self->ptr; } if (self->ptr == self->end) break; @@ -1210,7 +1194,7 @@ TomlErrCode toml_parse_key_value(TomlParser *self, TomlTable *table) } else if (ch == '#') { do { toml_move_next(self); - ch = *self->ptr; + ch =* self->ptr; } while (self->ptr < self->end && ch != '\n'); toml_move_next(self); continue; @@ -1220,10 +1204,10 @@ TomlErrCode toml_parse_key_value(TomlParser *self, TomlTable *table) return TOML_ERR_SYNTAX; } - ch = *self->ptr; + ch =* self->ptr; while (self->ptr < self->end && (ch == ' ' || ch == '\t')) { toml_move_next(self); - ch = *self->ptr; + ch =* self->ptr; } if (self->ptr == self->end) { @@ -1239,10 +1223,10 @@ TomlErrCode toml_parse_key_value(TomlParser *self, TomlTable *table) toml_move_next(self); - ch = *self->ptr; + ch =* self->ptr; while (self->ptr < self->end && (ch == ' ' || ch == '\t')) { toml_move_next(self); - ch = *self->ptr; + ch =* self->ptr; } if (self->ptr == self->end) { @@ -1259,7 +1243,7 @@ TomlErrCode toml_parse_key_value(TomlParser *self, TomlTable *table) key = NULL; value = NULL; - while (self->ptr < self->end && (*self->ptr == ' ' || *self->ptr == '\t')) { + while (self->ptr < self->end && (*self->ptr == ' ' ||* self->ptr == '\t')) { toml_move_next(self); } @@ -1269,7 +1253,7 @@ TomlErrCode toml_parse_key_value(TomlParser *self, TomlTable *table) if (*self->ptr == '#') { do { toml_move_next(self); - } while (self->ptr < self->end && *self->ptr != '\n'); + } while (self->ptr < self->end &&* self->ptr != '\n'); } if (*self->ptr == '\r') { @@ -1290,10 +1274,10 @@ TomlErrCode toml_parse_key_value(TomlParser *self, TomlTable *table) return TOML_OK; } -TomlValue* toml_parse_array(TomlParser *self) +TomlValue* toml_parse_array(TomlParser* self) { - TomlValue *array = NULL; - TomlValue *value = NULL; + TomlValue* array = NULL; + TomlValue* value = NULL; array = toml_value_new_array(); @@ -1305,7 +1289,7 @@ TomlValue* toml_parse_array(TomlParser *self) } else if (*self->ptr == '#') { do { toml_move_next(self); - } while (self->ptr < self->end && *self->ptr != '\n'); + } while (self->ptr < self->end &&* self->ptr != '\n'); toml_move_next(self); } else if (*self->ptr == '\n') { toml_move_next(self); @@ -1330,13 +1314,13 @@ TomlValue* toml_parse_array(TomlParser *self) } else if (*self->ptr == '#') { do { toml_move_next(self); - } while (self->ptr < self->end && *self->ptr != '\n'); + } while (self->ptr < self->end &&* self->ptr != '\n'); } else { break; } } - if (self->ptr < self->end && *self->ptr == ',') { + if (self->ptr < self->end &&* self->ptr == ',') { toml_move_next(self); } } @@ -1353,21 +1337,21 @@ end: return array; } -TomlValue* toml_parse_inline_table(TomlParser *self) +TomlValue* toml_parse_inline_table(TomlParser* self) { - TomlValue *table = NULL; + TomlValue* table = NULL; table = toml_value_new_table(); while (self->ptr < self->end) { - TomlString *key = NULL; - TomlValue *value = NULL; + TomlString* key = NULL; + TomlValue* value = NULL; char ch; - ch = *self->ptr; + ch =* self->ptr; while (self->ptr < self->end && (ch == ' ' || ch == '\t')) { toml_move_next(self); - ch = *self->ptr; + ch =* self->ptr; } if (isalnum(ch) || ch == '_') { @@ -1392,10 +1376,10 @@ TomlValue* toml_parse_inline_table(TomlParser *self) goto error; } - ch = *self->ptr; + ch =* self->ptr; while (self->ptr < self->end && (ch == ' ' || ch == '\t')) { toml_move_next(self); - ch = *self->ptr; + ch =* self->ptr; } if (self->ptr == self->end) { @@ -1411,10 +1395,10 @@ TomlValue* toml_parse_inline_table(TomlParser *self) toml_move_next(self); - ch = *self->ptr; + ch =* self->ptr; while (self->ptr < self->end && (ch == ' ' || ch == '\t')) { toml_move_next(self); - ch = *self->ptr; + ch =* self->ptr; } if (self->ptr == self->end) { @@ -1428,7 +1412,7 @@ TomlValue* toml_parse_inline_table(TomlParser *self) toml_table_set_by_string(table->value.table, key, value); - while (self->ptr < self->end && (*self->ptr == ' ' || *self->ptr == '\t')) { + while (self->ptr < self->end && (*self->ptr == ' ' ||* self->ptr == '\t')) { toml_move_next(self); } @@ -1450,20 +1434,20 @@ end: return table; } -TomlTable* toml_walk_table_path(TomlParser *parser, TomlTable *table, - TomlArray *key_path, int is_array, - int create_if_not_exist) +TomlTable* toml_walk_table_path(TomlParser *parser, TomlTable* table, + TomlArray *key_path, i32 is_array, + i32 create_if_not_exist) { - TomlTable *real_table = table; - TomlValue *new_table = NULL; - TomlValue *array = NULL; - TomlString *part_copy = NULL; + TomlTable* real_table = table; + TomlValue* new_table = NULL; + TomlValue* array = NULL; + TomlString* part_copy = NULL; if (is_array) { - size_t i = 0; + u64 i = 0; for (; i < key_path->len - 1; i++) { - TomlString *part = key_path->elements[i]->value.string; - TomlValue *t = toml_table_get_by_string(real_table, part); + TomlString* part = key_path->elements[i]->value.string; + TomlValue* t = toml_table_get_by_string(real_table, part); if (t == NULL) { if (create_if_not_exist) { new_table = toml_value_new_table(); @@ -1481,8 +1465,8 @@ TomlTable* toml_walk_table_path(TomlParser *parser, TomlTable *table, } } - TomlString *part = key_path->elements[i]->value.string; - TomlValue *t = toml_table_get_by_string(real_table, part); + TomlString* part = key_path->elements[i]->value.string; + TomlValue* t = toml_table_get_by_string(real_table, part); if (t == NULL) { if (create_if_not_exist) { array = toml_value_new_array(); @@ -1504,14 +1488,14 @@ TomlTable* toml_walk_table_path(TomlParser *parser, TomlTable *table, goto error; } - TomlValue *new_table = toml_value_new_table(); + TomlValue* new_table = toml_value_new_table(); toml_array_append(t->value.array, new_table); real_table = new_table->value.table; } } else { - for (size_t i = 0; i < key_path->len; i++) { - TomlString *part = key_path->elements[i]->value.string; - TomlValue *t = toml_table_get_by_string(real_table, part); + for (u64 i = 0; i < key_path->len; i++) { + TomlString* part = key_path->elements[i]->value.string; + TomlValue* t = toml_table_get_by_string(real_table, part); if (t == NULL) { if (create_if_not_exist) { new_table = toml_value_new_table(); @@ -1546,25 +1530,25 @@ end: return real_table; } -TomlErrCode toml_parse_table(TomlParser *self, TomlTable *table) +TomlErrCode toml_parse_table(TomlParser* self, TomlTable* table) { TomlArray *key_path = NULL; - int is_array = TOML_FALSE; - TomlTable *real_table = table; + i32 is_array = false; + TomlTable* real_table = table; TomlErrCode rc = 0; key_path = toml_array_new(); - if (self->ptr < self->end && *self->ptr == '[') { - is_array = TOML_TRUE; + if (self->ptr < self->end &&* self->ptr == '[') { + is_array = true; toml_move_next(self); } while (1) { - if (*self->ptr == ' ' || *self->ptr == '\t') { + if (*self->ptr == ' ' ||* self->ptr == '\t') { do { toml_move_next(self); - } while (*self->ptr < *self->end && (*self->ptr == ' ' || *self->ptr == '\t')); + } while (*self->ptr <* self->end && (*self->ptr == ' ' ||* self->ptr == '\t')); } else if (*self->ptr == ']') { if (is_array) { if (self->ptr + 2 <= self->end && strncmp(self->ptr, "]]", 2) == 0) { @@ -1576,10 +1560,10 @@ TomlErrCode toml_parse_table(TomlParser *self, TomlTable *table) break; } } else { - TomlString *key_part = NULL; - TomlValue *key_part_value = NULL; + TomlString* key_part = NULL; + TomlValue* key_part_value = NULL; - if (isalnum(*self->ptr) || *self->ptr == '_') { + if (isalnum(*self->ptr) ||* self->ptr == '_') { key_part = toml_parse_bare_key(self); if (key_part == NULL) goto cleanup; @@ -1604,11 +1588,11 @@ TomlErrCode toml_parse_table(TomlParser *self, TomlTable *table) toml_array_append(key_path, key_part_value); while (self->ptr < self->end && - (*self->ptr == ' ' || *self->ptr == '\t')) { + (*self->ptr == ' ' ||* self->ptr == '\t')) { toml_move_next(self); } - if (self->ptr < self->end && *self->ptr == '.') { + if (self->ptr < self->end &&* self->ptr == '.') { toml_move_next(self); } } @@ -1621,17 +1605,17 @@ TomlErrCode toml_parse_table(TomlParser *self, TomlTable *table) } while (self->ptr < self->end && - (*self->ptr == ' ' || *self->ptr == '\t' || *self->ptr == '\r')) { + (*self->ptr == ' ' ||* self->ptr == '\t' ||* self->ptr == '\r')) { toml_move_next(self); } - if (self->ptr < self->end && *self->ptr != '\n') { + if (self->ptr < self->end &&* self->ptr != '\n') { toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: new line expected", self->filename, self->lineno, self->colno); goto cleanup; } - real_table = toml_walk_table_path(self, table, key_path, is_array, TOML_TRUE); + real_table = toml_walk_table_path(self, table, key_path, is_array, true); if (real_table == NULL) goto error; @@ -1646,24 +1630,24 @@ cleanup: return rc; } -TomlTable* toml_parse(TomlParser *self) +TomlTable* toml_parse(TomlParser* self) { - TomlTable *table = NULL; + TomlTable* table = NULL; table = toml_table_new(); while (self->ptr < self->end) { - char ch = *self->ptr; + char ch =* self->ptr; while (self->ptr < self->end && isspace(ch)) { toml_move_next(self); - ch = *self->ptr; + ch =* self->ptr; } if (ch == '#') { do { toml_move_next(self); - ch = *self->ptr; + ch =* self->ptr; } while (self->ptr < self->end && ch != '\n'); toml_move_next(self); } else if (ch == '[') { @@ -1676,7 +1660,7 @@ TomlTable* toml_parse(TomlParser *self) } else if (ch == ' ' || ch == '\t' || ch == '\r') { do { toml_move_next(self); - ch = *self->ptr; + ch =* self->ptr; } while (ch == ' ' || ch == '\t' || ch == '\r'); } else if (ch == '\n') { toml_move_next(self); @@ -1686,11 +1670,11 @@ TomlTable* toml_parse(TomlParser *self) return table; } -TomlTable* toml_load_nstr_filename(const char *str, size_t len, - const char *filename) +TomlTable* toml_load_nstr_filename(cstr str, u64 len, + cstr filename) { TomlParser *parser = NULL; - TomlTable *table = NULL; + TomlTable* table = NULL; parser = toml_parser_new(str, len); parser->filename = toml_strdup(filename); @@ -1701,27 +1685,27 @@ TomlTable* toml_load_nstr_filename(const char *str, size_t len, return table; } -TomlTable* toml_load_nstr(const char *str, size_t len) +TomlTable* toml_load_nstr(cstr str, u64 len) { return toml_load_nstr_filename(str, len, ""); } -TomlTable* toml_load_str(const char *str) +TomlTable* toml_load_str(cstr str) { return toml_load_nstr(str, sizeof(str)); } -TomlTable* toml_load_file_filename(FILE *file, const char *filename) +TomlTable* toml_load_file_filename(FILE* file, cstr filename) { - TomlTable *table = NULL; - TomlString *str = NULL; + TomlTable* table = NULL; + TomlString* str = NULL; str = toml_string_new(); toml_string_expand_if_necessary(str, 4095); - size_t count; - size_t bytes_to_read; + u64 count; + u64 bytes_to_read; do { bytes_to_read = str->_capacity - str->len - 1; @@ -1753,15 +1737,15 @@ cleanup: return table; } -TomlTable* toml_load_file(FILE *file) +TomlTable* toml_load_file(FILE* file) { return toml_load_file_filename(file, ""); } -TomlTable* toml_load_filename(const char *filename) +TomlTable* toml_load_filename(cstr filename) { - TomlTable *table = NULL; - FILE *f = NULL; + TomlTable* table = NULL; + FILE* f = NULL; f = fopen(filename, "r"); if (f == NULL) { diff --git a/tests/main.c b/tests/main.c index 4e089c0..0ef838a 100644 --- a/tests/main.c +++ b/tests/main.c @@ -9,13 +9,13 @@ #define PROJECT_SOURCE_DIR ".." #endif -void print_table(const TomlTable *table); -void print_value(const TomlValue *value); +void print_table(const TomlTable* table); +void print_value(const TomlValue* value); void print_array(const TomlArray *array) { printf("["); - for (size_t i = 0; i < array->len; i++) { + for (u64 i = 0; i < array->len; i++) { if (i > 0) { printf(", "); } @@ -24,7 +24,7 @@ void print_array(const TomlArray *array) printf("]"); } -void print_value(const TomlValue *value) +void print_value(const TomlValue* value) { switch (value->type) { case TOML_TABLE: @@ -57,12 +57,12 @@ void print_keyval(const TomlKeyValue *keyval) print_value(keyval->value); } -void print_table(const TomlTable *table) +void print_table(const TomlTable* table) { - TomlTableIter it = toml_table_iter_new((TomlTable *)table); + TomlTableIter it = toml_table_iter_new((TomlTable* )table); printf("{"); - size_t i = 0; + u64 i = 0; while (toml_table_iter_has_next(&it)) { TomlKeyValue *keyval = toml_table_iter_get(&it); @@ -77,10 +77,10 @@ void print_table(const TomlTable *table) printf("}"); } -int test_run(const char *filename) +i32 test_run(cstr filename) { - TomlTable *table = NULL; - int rc = 0; + TomlTable* table = NULL; + i32 rc = 0; table = toml_load_filename(filename); if (table == NULL) @@ -94,15 +94,15 @@ cleanup: if (toml_err()->code != TOML_OK) { fprintf(stderr, "%s\n", toml_err()->message); - rc = (int)toml_err()->code; + rc = (i32)toml_err()->code; } toml_err_clear(); return rc; } -int main(void) +i32 main(void) { - static const char *const filenames[] = { + static cstr const filenames[] = { /* should parse */ PROJECT_SOURCE_DIR "/tests/key-values.toml", PROJECT_SOURCE_DIR "/tests/complex-structure.toml", @@ -117,12 +117,12 @@ int main(void) PROJECT_SOURCE_DIR "/tests/hard_example_unicode.toml" }; - int total_tests = sizeof(filenames) / sizeof(char *); - int num_passed = 0; - int num_failed = 0; + i32 total_tests = sizeof(filenames) / sizeof(char* ); + i32 num_passed = 0; + i32 num_failed = 0; - for (int i = 0; i < total_tests; i++) { - int rc = test_run(filenames[i]); + for (i32 i = 0; i < total_tests; i++) { + i32 rc = test_run(filenames[i]); if (rc == 0) { printf("test %d success\n", i); num_passed++;