fixed some warnings

This commit is contained in:
Timerix 2025-11-10 01:15:18 +05:00
parent f889990728
commit 247d291da6
4 changed files with 179 additions and 203 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/tlibtoml", "program": "${workspaceFolder}/bin/test",
"windows": { "program": "${workspaceFolder}/bin/tlibtoml.exe" }, "windows": { "program": "${workspaceFolder}/bin/test.exe" },
"preLaunchTask": "build_exec_dbg", "preLaunchTask": "build_exec_dbg",
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceFolder}/bin", "cwd": "${workspaceFolder}/bin",

View File

@ -8,8 +8,9 @@
extern "C" { extern "C" {
#endif #endif
#include "tlibc/std.h"
#include <time.h> #include <time.h>
#include "tlibc/std.h"
#include "tlibc/string/str.h"
#define false 0 #define false 0
#define true 1 #define true 1
@ -30,7 +31,7 @@ typedef struct {
} TomlErr; } TomlErr;
typedef struct { typedef struct {
char* str; char* s;
u64 len; u64 len;
u64 _capacity; u64 _capacity;
} TomlString; } TomlString;
@ -96,20 +97,20 @@ void* toml_malloc(u64 size);
void* toml_realloc(void* p, u64 size); void* toml_realloc(void* p, u64 size);
void toml_free(void* p); void toml_free(void* p);
char* toml_strdup(cstr str); char* toml_strdup(cstr s);
char* toml_strndup(cstr str, u64 n); char* toml_strndup(cstr s, u64 n);
i32 toml_vasprintf(char** str, cstr format, va_list args); i32 toml_vasprintf(char** s, cstr format, va_list args);
i32 toml_asprintf(char** str, cstr format, ...); i32 toml_asprintf(char** s, cstr format, ...) ATTRIBUTE_CHECK_FORMAT_PRINTF(2, 3);
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_new(void);
TomlString* toml_string_from_str(cstr str); TomlString* toml_string_from_str(cstr s);
TomlString* toml_string_from_nstr(cstr str, u64 len); TomlString* toml_string_from_nstr(cstr s, u64 len);
void toml_string_append_char(TomlString* self, char ch); void toml_string_append_char(TomlString* self, char ch);
void toml_string_append_str(TomlString* self, cstr str); void toml_string_append_str(TomlString* self, cstr s);
void toml_string_append_nstr(TomlString* self, cstr str, u64 len); void toml_string_append_nstr(TomlString* self, cstr s, u64 len);
TomlString* toml_string_clone(const TomlString* self); TomlString* toml_string_clone(const TomlString* self);
void toml_string_free(TomlString* self); void toml_string_free(TomlString* self);
i32 toml_string_equals(const TomlString* self, const TomlString* other); i32 toml_string_equals(const TomlString* self, const TomlString* other);
@ -148,12 +149,11 @@ TomlValue* toml_value_new_integer(i64 integer);
TomlValue* toml_value_new_float(f64 flt); TomlValue* toml_value_new_float(f64 flt);
TomlValue* toml_value_new_datetime(void); TomlValue* toml_value_new_datetime(void);
TomlValue* toml_value_new_boolean(i32 boolean); TomlValue* toml_value_new_boolean(i32 boolean);
TomlValue* toml_value_from_str(cstr str); TomlValue* toml_value_from_str(cstr s);
TomlValue* toml_value_from_nstr(cstr str, u64 len);
void toml_value_free(TomlValue* self); void toml_value_free(TomlValue* self);
TomlTable* toml_load_str(cstr str); TomlTable* toml_load_str(cstr s);
TomlTable* toml_load_nstr(cstr str, u64 len); TomlTable* toml_load_nstr(cstr s, u64 len);
TomlTable* toml_load_file(FILE* file); TomlTable* toml_load_file(FILE* file);
TomlTable* toml_load_filename(cstr filename); TomlTable* toml_load_filename(cstr filename);

View File

@ -9,6 +9,7 @@
#include <string.h> #include <string.h>
#include <pthread.h> #include <pthread.h>
#include "tlibtoml/toml.h" #include "tlibtoml/toml.h"
#include "tlibc/string/StringBuilder.h"
static ATTRIBUTE_THREAD_LOCAL TomlErr g_err = {TOML_OK, (char*)"", true}; static ATTRIBUTE_THREAD_LOCAL TomlErr g_err = {TOML_OK, (char*)"", true};
@ -66,27 +67,27 @@ void toml_free(void* p)
} }
} }
char* toml_strdup(cstr str) char* toml_strdup(cstr s)
{ {
u64 len = strlen(str) + 1; u64 len = strlen(s) + 1;
void* new = toml_malloc(len); void* new = toml_malloc(len);
if (new == NULL) if (new == NULL)
return NULL; return NULL;
return memcpy(new, str, len); return memcpy(new, s, len);
} }
char* toml_strndup(cstr str, u64 n) char* toml_strndup(cstr s, u64 n)
{ {
char* result = toml_malloc(n + 1); char* result = toml_malloc(n + 1);
if (result == NULL) if (result == NULL)
return NULL; return NULL;
result[n] = 0; result[n] = 0;
return memcpy(result, str, n); return memcpy(result, s, n);
} }
i32 toml_vasprintf(char** str, cstr format, va_list args) i32 toml_vasprintf(char** s, cstr format, va_list args)
{ {
i32 size = 0; i32 size = 0;
@ -99,18 +100,18 @@ i32 toml_vasprintf(char** str, cstr format, va_list args)
return size; return size;
} }
*str = toml_malloc((u64)size + 1); *s = toml_malloc((u64)size + 1);
if (*str == NULL) if (*s == NULL)
return -1; return -1;
return vsprintf(*str, format, args); return vsprintf(*s, format, args);
} }
i32 toml_asprintf(char** str, cstr format, ...) i32 toml_asprintf(char** s, cstr format, ...)
{ {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
i32 size = toml_vasprintf(str, format, args); i32 size = toml_vasprintf(s, format, args);
va_end(args); va_end(args);
return size; return size;
} }
@ -132,6 +133,9 @@ void toml_err_clear(void)
} }
} }
static inline void toml_err_set(TomlErrCode code, cstr format, ...)
ATTRIBUTE_CHECK_FORMAT_PRINTF(2, 3);
static inline void toml_err_set(TomlErrCode code, cstr format, ...) static inline void toml_err_set(TomlErrCode code, cstr format, ...)
{ {
assert(g_err.code == TOML_OK); assert(g_err.code == TOML_OK);
@ -167,23 +171,23 @@ static inline u64 toml_roundup_pow_of_two_u64(u64 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->s = NULL;
self->len = 0; self->len = 0;
self->_capacity = 0; self->_capacity = 0;
return self; return self;
} }
TomlString* toml_string_from_str(cstr str) TomlString* toml_string_from_str(cstr s)
{ {
TomlString* self = toml_string_new(); TomlString* self = toml_string_new();
toml_string_append_str(self, str); toml_string_append_str(self, s);
return self; return self;
} }
TomlString* toml_string_from_nstr(cstr str, u64 len) TomlString* toml_string_from_nstr(cstr s, u64 len)
{ {
TomlString* self = toml_string_new(); TomlString* self = toml_string_new();
toml_string_append_nstr(self, str, len); toml_string_append_nstr(self, s, len);
return self; return self;
} }
@ -192,7 +196,7 @@ static inline void toml_string_expand_if_necessary(TomlString* self, u64 len_to_
if (self->len + len_to_add + 1 > self->_capacity) { if (self->len + len_to_add + 1 > self->_capacity) {
u64 new_capacity = toml_roundup_pow_of_two_u64(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; new_capacity = new_capacity >= 8 ? new_capacity : 8;
self->str = toml_realloc(self->str, new_capacity); self->s = toml_realloc(self->s, new_capacity);
self->_capacity = new_capacity; self->_capacity = new_capacity;
} }
} }
@ -200,38 +204,38 @@ static inline void toml_string_expand_if_necessary(TomlString* self, u64 len_to_
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); toml_string_expand_if_necessary(self, 1);
self->str[self->len] = ch; self->s[self->len] = ch;
self->str[self->len + 1] = 0; self->s[self->len + 1] = 0;
self->len++; self->len++;
} }
void toml_string_append_str(TomlString* self, cstr str) void toml_string_append_str(TomlString* self, cstr s)
{ {
u64 len = strlen(str); u64 len = strlen(s);
toml_string_expand_if_necessary(self, len); toml_string_expand_if_necessary(self, len);
memcpy(&self->str[self->len], str, len + 1); memcpy(self->s + self->len, s, len + 1);
self->len += len; self->len += len;
} }
void toml_string_append_nstr(TomlString* self, cstr str, u64 len) void toml_string_append_nstr(TomlString* self, cstr s, u64 len)
{ {
toml_string_expand_if_necessary(self, len); toml_string_expand_if_necessary(self, len);
memcpy(&self->str[self->len], str, len); memcpy(self->s + self->len, s, len);
self->len += len; self->len += len;
self->str[self->len] = 0; self->s[self->len] = 0;
} }
void toml_string_free(TomlString* self) void toml_string_free(TomlString* self)
{ {
if (self != NULL) { if (self != NULL) {
free(self->str); free(self->s);
free(self); free(self);
} }
} }
TomlString* toml_string_clone(const TomlString* self) TomlString* toml_string_clone(const TomlString* self)
{ {
return toml_string_from_nstr(self->str, self->len); return toml_string_from_nstr(self->s, self->len);
} }
i32 toml_string_equals(const TomlString* self, const TomlString* other) i32 toml_string_equals(const TomlString* self, const TomlString* other)
@ -244,12 +248,12 @@ i32 toml_string_equals(const TomlString* self, const TomlString* other)
return false; return false;
} }
if (self->str == other->str) { if (self->s == other->s) {
return true; return true;
} }
for (u64 i = 0; i < self->len; i++) { for (u64 i = 0; i < self->len; i++) {
if (self->str[i] != other->str[i]) { if (self->s[i] != other->s[i]) {
return false; return false;
} }
} }
@ -320,8 +324,8 @@ TomlValue* toml_table_get_by_string(const TomlTable* self, const TomlString* key
TomlValue* toml_table_getn(const TomlTable* self, cstr key, u64 key_len) TomlValue* toml_table_getn(const TomlTable* self, cstr key, u64 key_len)
{ {
TomlString str = {(char* )key, key_len, 0}; TomlString s = {(char* )key, key_len, 0};
return toml_table_get_by_string(self, &str); return toml_table_get_by_string(self, &s);
} }
TomlValue* toml_table_get(const TomlTable* self, cstr key) TomlValue* toml_table_get(const TomlTable* self, cstr key)
@ -387,8 +391,8 @@ i32 toml_table_get_as_boolean(const TomlTable* self, cstr key)
void toml_table_setn(TomlTable* self, cstr key, u64 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* s = toml_string_from_nstr(key, key_len);
toml_table_set_by_string(self, str, value); toml_table_set_by_string(self, s, value);
} }
void toml_table_set(TomlTable* self, cstr key, TomlValue* value) void toml_table_set(TomlTable* self, cstr key, TomlValue* value)
@ -489,18 +493,10 @@ TomlValue* toml_value_new(TomlType type)
return self; return self;
} }
TomlValue* toml_value_from_str(cstr str) TomlValue* toml_value_from_str(cstr s)
{ {
TomlValue* self = toml_malloc(sizeof(TomlValue)); TomlValue* self = toml_malloc(sizeof(TomlValue));
self->value.string = toml_string_from_str(str); self->value.string = toml_string_from_str(s);
self->type = TOML_STRING;
return self;
}
TomlValue* toml_value_from_nstr(cstr str, u64 len)
{
TomlValue* self = toml_malloc(sizeof(TomlValue));
self->value.string = toml_string_from_nstr(str, len);
self->type = TOML_STRING; self->type = TOML_STRING;
return self; return self;
} }
@ -582,12 +578,12 @@ typedef struct _TomlParser {
char* filename; char* filename;
} TomlParser; } TomlParser;
TomlParser *toml_parser_new(cstr str, u64 len) TomlParser *toml_parser_new(cstr s, u64 len)
{ {
TomlParser* self = toml_malloc(sizeof(TomlParser)); TomlParser* self = toml_malloc(sizeof(TomlParser));
self->begin = str; self->begin = s;
self->end = str + len; self->end = s + len;
self->ptr = str; self->ptr = s;
self->lineno = 1; self->lineno = 1;
self->colno = 1; self->colno = 1;
self->filename = NULL; self->filename = NULL;
@ -624,11 +620,11 @@ void toml_next_n(TomlParser* self, i32 n)
TomlString* toml_parse_bare_key(TomlParser* self) TomlString* toml_parse_bare_key(TomlParser* self)
{ {
cstr str = self->ptr; cstr s = self->ptr;
u64 len = 0; u64 len = 0;
while (self->ptr < self->end) { while (self->ptr < self->end) {
char ch =* self->ptr; char ch = *self->ptr;
if (!(isalnum(ch) || ch == '_' || ch == '-')) if (!(isalnum(ch) || ch == '_' || ch == '-'))
break; break;
@ -637,7 +633,7 @@ TomlString* toml_parse_bare_key(TomlParser* self)
toml_move_next(self); toml_move_next(self);
} }
return toml_string_from_nstr(str, len); return toml_string_from_nstr(s, len);
} }
char toml_hex_char_to_int(char ch) char toml_hex_char_to_int(char ch)
@ -659,7 +655,7 @@ i32 toml_encode_unicode_scalar(TomlString* result, TomlParser *parser, i32 n)
if (parser->ptr + n > parser->end) { if (parser->ptr + n > parser->end) {
toml_err_set(TOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar", toml_err_set(TOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar",
parser->filename, parser->lineno, parser->colno); parser->filename, parser->lineno, parser->colno);
return TOML_ERR_UNICODE; return TOML_ERR_UNICODE;
} }
@ -670,7 +666,7 @@ i32 toml_encode_unicode_scalar(TomlString* result, TomlParser *parser, i32 n)
toml_move_next(parser); toml_move_next(parser);
} else { } else {
toml_err_set(TOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar", toml_err_set(TOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar",
parser->filename, parser->lineno, parser->colno); parser->filename, parser->lineno, parser->colno);
return TOML_ERR_UNICODE; return TOML_ERR_UNICODE;
} }
} }
@ -678,7 +674,7 @@ i32 toml_encode_unicode_scalar(TomlString* result, TomlParser *parser, i32 n)
if ((scalar >= 0xd800 && scalar <= 0xdfff) || if ((scalar >= 0xd800 && scalar <= 0xdfff) ||
(scalar >= 0xfffe && scalar <= 0xffff)) { (scalar >= 0xfffe && scalar <= 0xffff)) {
toml_err_set(TOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar", toml_err_set(TOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar",
parser->filename, parser->lineno, parser->colno); parser->filename, parser->lineno, parser->colno);
return TOML_ERR_UNICODE; return TOML_ERR_UNICODE;
} }
@ -728,7 +724,7 @@ i32 toml_encode_unicode_scalar(TomlString* result, TomlParser *parser, i32 n)
} }
toml_err_set(TOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar", toml_err_set(TOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar",
parser->filename, parser->lineno, parser->colno); parser->filename, parser->lineno, parser->colno);
return TOML_ERR_UNICODE; return TOML_ERR_UNICODE;
} }
@ -738,12 +734,12 @@ 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') { while (self->ptr < self->end &&* self->ptr != '\"' &&* self->ptr != '\n') {
char ch1 =* self->ptr; char ch1 = *self->ptr;
if (ch1 == '\\') { if (ch1 == '\\') {
if (self->ptr >= self->end) break; if (self->ptr >= self->end) break;
toml_move_next(self); toml_move_next(self);
char ch2 =* self->ptr; char ch2 = *self->ptr;
if (ch2 == '\"') { if (ch2 == '\"') {
toml_string_append_char(result, '\"'); toml_string_append_char(result, '\"');
@ -763,9 +759,6 @@ TomlString* toml_parse_basic_string(TomlParser* self)
} else if (ch2 == 'r') { } else if (ch2 == 'r') {
toml_string_append_char(result, '\r'); toml_string_append_char(result, '\r');
toml_move_next(self); toml_move_next(self);
} else if (ch2 == '"') {
toml_string_append_char(result, '\"');
toml_move_next(self);
} else if (ch2 == '\\') { } else if (ch2 == '\\') {
toml_string_append_char(result, '\\'); toml_string_append_char(result, '\\');
toml_move_next(self); toml_move_next(self);
@ -782,8 +775,10 @@ TomlString* toml_parse_basic_string(TomlParser* self)
return NULL; return NULL;
} }
} else { } else {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid escape charactor"); toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid escape character",
self->filename, self->lineno, self->colno);
toml_string_free(result); toml_string_free(result);
return NULL;
} }
} else { } else {
toml_string_append_char(result, ch1); toml_string_append_char(result, ch1);
@ -793,8 +788,9 @@ 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", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated basic string",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
toml_string_free(result); toml_string_free(result);
return NULL;
} }
toml_move_next(self); toml_move_next(self);
@ -813,7 +809,7 @@ TomlString* toml_parse_literal_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 literal string", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated literal string",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
toml_string_free(result); toml_string_free(result);
return NULL; return NULL;
} }
@ -825,36 +821,28 @@ TomlString* toml_parse_literal_string(TomlParser* self)
TomlValue* toml_parse_basic_string_value(TomlParser* self) TomlValue* toml_parse_basic_string_value(TomlParser* self)
{ {
TomlValue* value = NULL;
TomlString* string = toml_parse_basic_string(self); TomlString* string = toml_parse_basic_string(self);
if (string == NULL) if (string == NULL)
return NULL; return NULL;
value = toml_value_new(TOML_STRING); TomlValue* value = toml_value_new(TOML_STRING);
value->value.string = string; value->value.string = string;
return value; return value;
} }
TomlValue* toml_parse_literal_string_value(TomlParser* self) TomlValue* toml_parse_literal_string_value(TomlParser* self)
{ {
TomlValue* value = NULL;
TomlString* string = toml_parse_literal_string(self); TomlString* string = toml_parse_literal_string(self);
if (string == NULL) if (string == NULL)
return NULL; return NULL;
value = toml_value_new(TOML_STRING); TomlValue* value = toml_value_new(TOML_STRING);
value->value.string = string; value->value.string = string;
return value; return value;
} }
TomlValue* toml_parse_multi_line_basic_string(TomlParser* self) TomlValue* toml_parse_multi_line_basic_string(TomlParser* self)
{ {
TomlValue* value = NULL;
TomlString* result = toml_string_new(); TomlString* result = toml_string_new();
if (*self->ptr == '\n') { if (*self->ptr == '\n') {
@ -862,14 +850,14 @@ TomlValue* toml_parse_multi_line_basic_string(TomlParser* self)
} }
while (self->ptr + 3 <= self->end && strncmp(self->ptr, "\"\"\"", 3) != 0) { while (self->ptr + 3 <= self->end && strncmp(self->ptr, "\"\"\"", 3) != 0) {
char ch1 =* self->ptr; char ch1 = *self->ptr;
if (ch1 == '\\') { if (ch1 == '\\') {
if (self->ptr + 3 > self->end || strncmp(self->ptr, "\"\"\"", 3) == 0) if (self->ptr + 3 > self->end || strncmp(self->ptr, "\"\"\"", 3) == 0)
break; break;
toml_move_next(self); toml_move_next(self);
char ch2 =* self->ptr; char ch2 = *self->ptr;
if (ch2 == '\"') { if (ch2 == '\"') {
toml_string_append_char(result, '\"'); toml_string_append_char(result, '\"');
@ -889,9 +877,6 @@ TomlValue* toml_parse_multi_line_basic_string(TomlParser* self)
} else if (ch2 == 'r') { } else if (ch2 == 'r') {
toml_string_append_char(result, '\r'); toml_string_append_char(result, '\r');
toml_move_next(self); toml_move_next(self);
} else if (ch2 == '"') {
toml_string_append_char(result, '\"');
toml_move_next(self);
} else if (ch2 == '\\') { } else if (ch2 == '\\') {
toml_string_append_char(result, '\\'); toml_string_append_char(result, '\\');
toml_move_next(self); toml_move_next(self);
@ -912,8 +897,8 @@ TomlValue* toml_parse_multi_line_basic_string(TomlParser* self)
toml_move_next(self); toml_move_next(self);
} while (self->ptr + 3 <= self->end && isspace(*self->ptr)); } while (self->ptr + 3 <= self->end && isspace(*self->ptr));
} else { } else {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid escape charactor", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid escape character",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
toml_string_free(result); toml_string_free(result);
return NULL; return NULL;
} }
@ -925,23 +910,20 @@ TomlValue* toml_parse_multi_line_basic_string(TomlParser* self)
if (self->ptr + 3 > self->end || strncmp(self->ptr, "\"\"\"", 3) != 0) { if (self->ptr + 3 > self->end || strncmp(self->ptr, "\"\"\"", 3) != 0) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated multi-line basic string", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated multi-line basic string",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
toml_string_free(result); toml_string_free(result);
return NULL; return NULL;
} }
toml_next_n(self, 3); toml_next_n(self, 3);
value = toml_value_new(TOML_STRING); TomlValue* value = toml_value_new(TOML_STRING);
value->value.string = result; value->value.string = result;
return value; return value;
} }
TomlValue* toml_parse_multi_line_literal_string(TomlParser* self) TomlValue* toml_parse_multi_line_literal_string(TomlParser* self)
{ {
TomlValue* value = NULL;
TomlString* result = toml_string_new(); TomlString* result = toml_string_new();
if (*self->ptr == '\n') { if (*self->ptr == '\n') {
@ -955,35 +937,34 @@ TomlValue* toml_parse_multi_line_literal_string(TomlParser* self)
if (self->ptr + 3 > self->end || strncmp(self->ptr, "\'\'\'", 3) != 0) { if (self->ptr + 3 > self->end || strncmp(self->ptr, "\'\'\'", 3) != 0) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated multi-line literal string", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated multi-line literal string",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
toml_string_free(result); toml_string_free(result);
return NULL; return NULL;
} }
toml_next_n(self, 3); toml_next_n(self, 3);
value = toml_value_new(TOML_STRING); TomlValue* value = toml_value_new(TOML_STRING);
value->value.string = result; value->value.string = result;
return value; return value;
} }
TomlValue* toml_parse_datetime(cstr str, u64 len) TomlValue* toml_parse_datetime(cstr s, u64 len)
{ {
(void)str; (void)s;
(void)len; (void)len;
return toml_value_new(TOML_DATETIME); 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; TomlValue* result = NULL;
char type = 'i'; char type = 'i';
i32 base = 10; i32 base = 10;
str = toml_string_new(); TomlString* s = toml_string_new();
// Determine nan and inf type as float, as we cannot determine by dot. // Determine nan and inf type as float, as we cannot determine by dot.
// But do not strip it because we will append it to the string later // But do not strip it because we will append it to the string later
@ -1024,7 +1005,7 @@ TomlValue* toml_parse_int_or_float_or_time(TomlParser* self)
type = 'f'; type = 'f';
has_exp = true; has_exp = true;
} }
toml_string_append_char(str,* self->ptr); toml_string_append_char(s,* self->ptr);
} else { } else {
break; break;
} }
@ -1033,73 +1014,74 @@ TomlValue* toml_parse_int_or_float_or_time(TomlParser* self)
type = 'f'; type = 'f';
} }
toml_string_append_char(str,* self->ptr); toml_string_append_char(s,* self->ptr);
} else if (*self->ptr == '.') { } else if (*self->ptr == '.') {
if (type == 'i') { if (type == 'i') {
type = 'f'; type = 'f';
toml_string_append_char(str,* self->ptr); toml_string_append_char(s,* self->ptr);
} else { } else {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid float", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid float",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
toml_string_free(str); toml_string_free(s);
return NULL; return NULL;
} }
} else if (*self->ptr == '_') { } else if (*self->ptr == '_') {
if (type == 't') { if (type == 't') {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid datetime", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid datetime",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
toml_string_free(str); toml_string_free(s);
return NULL; return NULL;
} }
if (!isalnum(last_char)) { if (!isalnum(last_char)) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid integer or float", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid integer or float",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
toml_string_free(str); toml_string_free(s);
return NULL; return NULL;
} }
} else if (*self->ptr == '-') { } else if (*self->ptr == '-') {
type = 't'; type = 't';
toml_string_append_char(str,* self->ptr); toml_string_append_char(s,* self->ptr);
} else { } else {
break; break;
} }
last_char =* self->ptr; last_char = *self->ptr;
toml_move_next(self); toml_move_next(self);
} }
if (last_char == '_') { if (last_char == '_') {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid integer or float or datetime", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid integer or float or datetime",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
toml_string_free(str); toml_string_free(s);
return NULL; return NULL;
} }
if (type == 'i') { if (type == 'i') {
char* end = NULL; char* end = NULL;
i64 n = strtoll(str->str, &end, base); i64 n = strtoll(s->s, &end, base);
if (end < str->str + str->len) { if (end < s->s + s->len) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid integer", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid integer",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
toml_string_free(str); toml_string_free(s);
return NULL; return NULL;
} }
result = toml_value_new_integer(n); result = toml_value_new_integer(n);
} else if (type == 'f') { } else if (type == 'f') {
char* end = NULL; char* end = NULL;
f64 n = strtod(str->str, &end); f64 n = strtod(s->s, &end);
if (end < str->str + str->len) { if (end < s->s + s->len) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid float"); toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid float",
self->filename, self->lineno, self->colno);
goto cleanup; goto cleanup;
} }
result = toml_value_new_float(n); result = toml_value_new_float(n);
} else if (type == 't') { } else if (type == 't') {
result = toml_parse_datetime(str->str, str->len); result = toml_parse_datetime(s->s, s->len);
} }
cleanup: cleanup:
toml_string_free(str); toml_string_free(s);
return result; return result;
} }
@ -1126,8 +1108,7 @@ 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) { if (strncmp(self->ptr, "\"\"\"", 3) == 0) {
toml_next_n(self, 3); toml_next_n(self, 3);
@ -1153,7 +1134,8 @@ TomlValue* toml_parse_value(TomlParser* self)
value = toml_parse_inline_table(self); value = toml_parse_inline_table(self);
} else { } else {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unexpected token", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
return NULL;
} }
return value; return value;
@ -1167,10 +1149,10 @@ TomlErrCode toml_parse_key_value(TomlParser* self, TomlTable* table)
while (self->ptr < self->end) { while (self->ptr < self->end) {
char ch; char ch;
ch =* self->ptr; ch = *self->ptr;
while (self->ptr < self->end && isspace(ch)) { while (self->ptr < self->end && isspace(ch)) {
toml_move_next(self); toml_move_next(self);
ch =* self->ptr; ch = *self->ptr;
} }
if (self->ptr == self->end) break; if (self->ptr == self->end) break;
@ -1194,43 +1176,45 @@ TomlErrCode toml_parse_key_value(TomlParser* self, TomlTable* table)
} else if (ch == '#') { } else if (ch == '#') {
do { do {
toml_move_next(self); toml_move_next(self);
ch =* self->ptr; ch = *self->ptr;
} while (self->ptr < self->end && ch != '\n'); } while (self->ptr < self->end && ch != '\n');
toml_move_next(self); toml_move_next(self);
continue; continue;
} else { } else {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unexpected token", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
return TOML_ERR_SYNTAX; return TOML_ERR_SYNTAX;
} }
ch =* self->ptr; ch = *self->ptr;
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) { while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
toml_move_next(self); toml_move_next(self);
ch =* self->ptr; ch = *self->ptr;
} }
if (self->ptr == self->end) { if (self->ptr == self->end) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair"); toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair",
self->filename, self->lineno, self->colno);
return TOML_ERR_SYNTAX; return TOML_ERR_SYNTAX;
} }
if (ch != '=') { if (ch != '=') {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unexpected token", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
return TOML_ERR_SYNTAX; return TOML_ERR_SYNTAX;
} }
toml_move_next(self); toml_move_next(self);
ch =* self->ptr; ch = *self->ptr;
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) { while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
toml_move_next(self); toml_move_next(self);
ch =* self->ptr; ch = *self->ptr;
} }
if (self->ptr == self->end) { if (self->ptr == self->end) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair"); toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair",
self->filename, self->lineno, self->colno);
return TOML_ERR_SYNTAX; return TOML_ERR_SYNTAX;
} }
@ -1264,7 +1248,7 @@ TomlErrCode toml_parse_key_value(TomlParser* self, TomlTable* table)
toml_move_next(self); toml_move_next(self);
} else { } else {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: new line expected", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: new line expected",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
toml_value_free(value); toml_value_free(value);
toml_string_free(key); toml_string_free(key);
return TOML_ERR_SYNTAX; return TOML_ERR_SYNTAX;
@ -1339,19 +1323,16 @@ end:
TomlValue* toml_parse_inline_table(TomlParser* self) TomlValue* toml_parse_inline_table(TomlParser* self)
{ {
TomlValue* table = NULL; TomlValue* table = toml_value_new_table();
table = toml_value_new_table();
while (self->ptr < self->end) { while (self->ptr < self->end) {
TomlString* key = NULL; TomlString* key = NULL;
TomlValue* value = NULL; TomlValue* value = NULL;
char ch;
ch =* self->ptr; char ch = *self->ptr;
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) { while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
toml_move_next(self); toml_move_next(self);
ch =* self->ptr; ch = *self->ptr;
} }
if (isalnum(ch) || ch == '_') { if (isalnum(ch) || ch == '_') {
@ -1372,37 +1353,39 @@ TomlValue* toml_parse_inline_table(TomlParser* self)
break; break;
} else { } else {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unexpected token", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto error; goto error;
} }
ch =* self->ptr; ch = *self->ptr;
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) { while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
toml_move_next(self); toml_move_next(self);
ch =* self->ptr; ch = *self->ptr;
} }
if (self->ptr == self->end) { if (self->ptr == self->end) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair"); toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair",
self->filename, self->lineno, self->colno);
goto error; goto error;
} }
if (ch != '=') { if (ch != '=') {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unexpected token", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto error; goto error;
} }
toml_move_next(self); toml_move_next(self);
ch =* self->ptr; ch = *self->ptr;
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) { while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
toml_move_next(self); toml_move_next(self);
ch =* self->ptr; ch = *self->ptr;
} }
if (self->ptr == self->end) { if (self->ptr == self->end) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair"); toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair",
self->filename, self->lineno, self->colno);
goto error; goto error;
} }
@ -1484,13 +1467,14 @@ TomlTable* toml_walk_table_path(TomlParser *parser, TomlTable* table,
} else { } else {
if (t->type != TOML_ARRAY) { if (t->type != TOML_ARRAY) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: this key was not an array", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: this key was not an array",
parser->filename, parser->lineno, parser->colno); parser->filename, parser->lineno, parser->colno);
goto error; goto error;
} }
TomlValue* new_table = toml_value_new_table(); new_table = toml_value_new_table();
toml_array_append(t->value.array, new_table); toml_array_append(t->value.array, new_table);
real_table = new_table->value.table; real_table = new_table->value.table;
new_table = NULL;
} }
} else { } else {
for (u64 i = 0; i < key_path->len; i++) { for (u64 i = 0; i < key_path->len; i++) {
@ -1579,7 +1563,8 @@ TomlErrCode toml_parse_table(TomlParser* self, TomlTable* table)
goto cleanup; goto cleanup;
} else { } else {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unexpected token", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto cleanup;
} }
key_part_value = toml_value_new(TOML_STRING); key_part_value = toml_value_new(TOML_STRING);
@ -1600,7 +1585,7 @@ TomlErrCode toml_parse_table(TomlParser* self, TomlTable* table)
if (key_path->len == 0) { if (key_path->len == 0) {
toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: empty table name", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: empty table name",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto cleanup; goto cleanup;
} }
@ -1611,7 +1596,7 @@ TomlErrCode toml_parse_table(TomlParser* self, TomlTable* table)
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", toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: new line expected",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto cleanup; goto cleanup;
} }
@ -1632,22 +1617,20 @@ cleanup:
TomlTable* toml_parse(TomlParser* self) TomlTable* toml_parse(TomlParser* self)
{ {
TomlTable* table = NULL; TomlTable* table = toml_table_new();
table = toml_table_new();
while (self->ptr < self->end) { while (self->ptr < self->end) {
char ch =* self->ptr; char ch = *self->ptr;
while (self->ptr < self->end && isspace(ch)) { while (self->ptr < self->end && isspace(ch)) {
toml_move_next(self); toml_move_next(self);
ch =* self->ptr; ch = *self->ptr;
} }
if (ch == '#') { if (ch == '#') {
do { do {
toml_move_next(self); toml_move_next(self);
ch =* self->ptr; ch = *self->ptr;
} while (self->ptr < self->end && ch != '\n'); } while (self->ptr < self->end && ch != '\n');
toml_move_next(self); toml_move_next(self);
} else if (ch == '[') { } else if (ch == '[') {
@ -1660,7 +1643,7 @@ TomlTable* toml_parse(TomlParser* self)
} else if (ch == ' ' || ch == '\t' || ch == '\r') { } else if (ch == ' ' || ch == '\t' || ch == '\r') {
do { do {
toml_move_next(self); toml_move_next(self);
ch =* self->ptr; ch = *self->ptr;
} while (ch == ' ' || ch == '\t' || ch == '\r'); } while (ch == ' ' || ch == '\t' || ch == '\r');
} else if (ch == '\n') { } else if (ch == '\n') {
toml_move_next(self); toml_move_next(self);
@ -1670,61 +1653,55 @@ TomlTable* toml_parse(TomlParser* self)
return table; return table;
} }
TomlTable* toml_load_nstr_filename(cstr str, u64 len, TomlTable* toml_load_nstr_filename(cstr s, u64 len,
cstr filename) cstr filename)
{ {
TomlParser *parser = NULL; TomlParser *parser = toml_parser_new(s, len);
TomlTable* table = NULL;
parser = toml_parser_new(str, len);
parser->filename = toml_strdup(filename); parser->filename = toml_strdup(filename);
table = toml_parse(parser); TomlTable* table = toml_parse(parser);
toml_parser_free(parser); toml_parser_free(parser);
return table; return table;
} }
TomlTable* toml_load_nstr(cstr str, u64 len) TomlTable* toml_load_nstr(cstr s, u64 len)
{ {
return toml_load_nstr_filename(str, len, "<string>"); return toml_load_nstr_filename(s, len, "<string>");
} }
TomlTable* toml_load_str(cstr str) TomlTable* toml_load_str(cstr s)
{ {
return toml_load_nstr(str, sizeof(str)); return toml_load_nstr(s, sizeof(s));
} }
TomlTable* toml_load_file_filename(FILE* file, cstr filename) TomlTable* toml_load_file_filename(FILE* file, cstr filename)
{ {
TomlTable* table = NULL; TomlTable* table = NULL;
TomlString* str = NULL; TomlString* s = toml_string_new();
toml_string_expand_if_necessary(s, 4095);
str = toml_string_new(); u64 count = 0;
u64 remaining_capacity = 0;
toml_string_expand_if_necessary(str, 4095);
u64 count;
u64 bytes_to_read;
do { do {
bytes_to_read = str->_capacity - str->len - 1; remaining_capacity = s->_capacity - s->len;
count = fread(s->s + s->len, 1, remaining_capacity, file);
count = fread(&str->str[str->len], 1, bytes_to_read, file);
if (ferror(file)) { if (ferror(file)) {
toml_err_set(TOML_ERR_OS, "Error when reading %s [errno %d: %s]", filename, errno, strerror(errno)); toml_err_set(TOML_ERR_OS, "Error when reading %s [errno %d: %s]",
filename, errno, strerror(errno));
goto error; goto error;
} }
str->len += count; s->len += count;
if (str->len + 1 >= str->_capacity) { if (s->len + 1 >= s->_capacity) {
toml_string_expand_if_necessary(str, str->_capacity * 2); toml_string_expand_if_necessary(s, s->_capacity * 2);
} }
} while (count == bytes_to_read); } while (count == remaining_capacity);
str->str[str->len] = 0; s->s[s->len] = 0;
table = toml_load_nstr_filename(str->str, str->len, filename); table = toml_load_nstr_filename(s->s, s->len, filename);
goto cleanup; goto cleanup;
@ -1733,7 +1710,7 @@ error:
table = NULL; table = NULL;
cleanup: cleanup:
toml_string_free(str); toml_string_free(s);
return table; return table;
} }
@ -1745,11 +1722,10 @@ TomlTable* toml_load_file(FILE* file)
TomlTable* toml_load_filename(cstr filename) TomlTable* toml_load_filename(cstr filename)
{ {
TomlTable* table = NULL; TomlTable* table = NULL;
FILE* f = NULL; FILE* f = fopen(filename, "r");
f = fopen(filename, "r");
if (f == NULL) { if (f == NULL) {
toml_err_set(TOML_ERR_OS, "Cannot open file %s [errno %d: %s]", filename, errno, strerror(errno)); toml_err_set(TOML_ERR_OS, "Cannot open file %s [errno %d: %s]",
filename, errno, strerror(errno));
goto error; goto error;
} }

View File

@ -34,7 +34,7 @@ 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.string->str); printf("\"%s\"", value->value.string->s);
break; break;
case TOML_INTEGER: case TOML_INTEGER:
printf("%" PRId64, value->value.integer); printf("%" PRId64, value->value.integer);
@ -53,7 +53,7 @@ void print_value(const TomlValue* value)
void print_keyval(const TomlKeyValue *keyval) void print_keyval(const TomlKeyValue *keyval)
{ {
printf("\"%s\": ", keyval->key->str); printf("\"%s\": ", keyval->key->s);
print_value(keyval->value); print_value(keyval->value);
} }