From 247d291da698c924516eaf77dfb9e1feb86e188c Mon Sep 17 00:00:00 2001 From: Timerix Date: Mon, 10 Nov 2025 01:15:18 +0500 Subject: [PATCH] fixed some warnings --- .vscode/launch.json | 4 +- include/tlibtoml/toml.h | 28 ++-- src/toml.c | 346 +++++++++++++++++++--------------------- tests/main.c | 4 +- 4 files changed, 179 insertions(+), 203 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 34083dd..20644dc 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,8 +5,8 @@ "name": "gdb_debug", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/bin/tlibtoml", - "windows": { "program": "${workspaceFolder}/bin/tlibtoml.exe" }, + "program": "${workspaceFolder}/bin/test", + "windows": { "program": "${workspaceFolder}/bin/test.exe" }, "preLaunchTask": "build_exec_dbg", "stopAtEntry": false, "cwd": "${workspaceFolder}/bin", diff --git a/include/tlibtoml/toml.h b/include/tlibtoml/toml.h index 9d769a7..7d46119 100644 --- a/include/tlibtoml/toml.h +++ b/include/tlibtoml/toml.h @@ -8,8 +8,9 @@ extern "C" { #endif -#include "tlibc/std.h" #include +#include "tlibc/std.h" +#include "tlibc/string/str.h" #define false 0 #define true 1 @@ -30,7 +31,7 @@ typedef struct { } TomlErr; typedef struct { - char* str; + char* s; u64 len; u64 _capacity; } TomlString; @@ -96,20 +97,20 @@ void* toml_malloc(u64 size); void* toml_realloc(void* p, u64 size); void toml_free(void* p); -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, ...); +char* toml_strdup(cstr s); +char* toml_strndup(cstr s, u64 n); +i32 toml_vasprintf(char** s, cstr format, va_list args); +i32 toml_asprintf(char** s, cstr format, ...) ATTRIBUTE_CHECK_FORMAT_PRINTF(2, 3); const TomlErr* toml_err(void); void toml_err_clear(void); TomlString* toml_string_new(void); -TomlString* toml_string_from_str(cstr str); -TomlString* toml_string_from_nstr(cstr str, u64 len); +TomlString* toml_string_from_str(cstr s); +TomlString* toml_string_from_nstr(cstr s, 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); +void toml_string_append_str(TomlString* self, cstr s); +void toml_string_append_nstr(TomlString* self, cstr s, 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); @@ -148,12 +149,11 @@ 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(i32 boolean); -TomlValue* toml_value_from_str(cstr str); -TomlValue* toml_value_from_nstr(cstr str, u64 len); +TomlValue* toml_value_from_str(cstr s); void toml_value_free(TomlValue* self); -TomlTable* toml_load_str(cstr str); -TomlTable* toml_load_nstr(cstr str, u64 len); +TomlTable* toml_load_str(cstr s); +TomlTable* toml_load_nstr(cstr s, u64 len); TomlTable* toml_load_file(FILE* file); TomlTable* toml_load_filename(cstr filename); diff --git a/src/toml.c b/src/toml.c index a673fa9..5a67682 100644 --- a/src/toml.c +++ b/src/toml.c @@ -9,6 +9,7 @@ #include #include #include "tlibtoml/toml.h" +#include "tlibc/string/StringBuilder.h" 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); if (new == 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); if (result == NULL) return NULL; 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; @@ -99,18 +100,18 @@ i32 toml_vasprintf(char** str, cstr format, va_list args) return size; } - *str = toml_malloc((u64)size + 1); - if (*str == NULL) + *s = toml_malloc((u64)size + 1); + if (*s == NULL) 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_start(args, format); - i32 size = toml_vasprintf(str, format, args); + i32 size = toml_vasprintf(s, format, args); va_end(args); 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, ...) { 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* self = toml_malloc(sizeof(TomlString)); - self->str = NULL; + self->s = NULL; self->len = 0; self->_capacity = 0; return self; } -TomlString* toml_string_from_str(cstr str) +TomlString* toml_string_from_str(cstr s) { TomlString* self = toml_string_new(); - toml_string_append_str(self, str); + toml_string_append_str(self, s); 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(); - toml_string_append_nstr(self, str, len); + toml_string_append_nstr(self, s, len); 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) { 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->s = toml_realloc(self->s, 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) { toml_string_expand_if_necessary(self, 1); - self->str[self->len] = ch; - self->str[self->len + 1] = 0; + self->s[self->len] = ch; + self->s[self->len + 1] = 0; 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); - memcpy(&self->str[self->len], str, len + 1); + memcpy(self->s + self->len, s, len + 1); 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); - memcpy(&self->str[self->len], str, len); + memcpy(self->s + self->len, s, len); self->len += len; - self->str[self->len] = 0; + self->s[self->len] = 0; } void toml_string_free(TomlString* self) { if (self != NULL) { - free(self->str); + free(self->s); free(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) @@ -244,12 +248,12 @@ i32 toml_string_equals(const TomlString* self, const TomlString* other) return false; } - if (self->str == other->str) { + if (self->s == other->s) { return true; } for (u64 i = 0; i < self->len; i++) { - if (self->str[i] != other->str[i]) { + if (self->s[i] != other->s[i]) { 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) { - TomlString str = {(char* )key, key_len, 0}; - return toml_table_get_by_string(self, &str); + TomlString s = {(char* )key, key_len, 0}; + return toml_table_get_by_string(self, &s); } 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) { - TomlString* str = toml_string_from_nstr(key, key_len); - toml_table_set_by_string(self, str, value); + TomlString* s = toml_string_from_nstr(key, key_len); + toml_table_set_by_string(self, s, value); } void toml_table_set(TomlTable* self, cstr key, TomlValue* value) @@ -489,18 +493,10 @@ TomlValue* toml_value_new(TomlType type) return self; } -TomlValue* toml_value_from_str(cstr str) +TomlValue* toml_value_from_str(cstr s) { 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(cstr str, u64 len) -{ - TomlValue* self = toml_malloc(sizeof(TomlValue)); - self->value.string = toml_string_from_nstr(str, len); + self->value.string = toml_string_from_str(s); self->type = TOML_STRING; return self; } @@ -582,12 +578,12 @@ typedef struct _TomlParser { char* filename; } TomlParser; -TomlParser *toml_parser_new(cstr str, u64 len) +TomlParser *toml_parser_new(cstr s, u64 len) { TomlParser* self = toml_malloc(sizeof(TomlParser)); - self->begin = str; - self->end = str + len; - self->ptr = str; + self->begin = s; + self->end = s + len; + self->ptr = s; self->lineno = 1; self->colno = 1; self->filename = NULL; @@ -624,11 +620,11 @@ void toml_next_n(TomlParser* self, i32 n) TomlString* toml_parse_bare_key(TomlParser* self) { - cstr str = self->ptr; + cstr s = self->ptr; u64 len = 0; while (self->ptr < self->end) { - char ch =* self->ptr; + char ch = *self->ptr; if (!(isalnum(ch) || ch == '_' || ch == '-')) break; @@ -637,7 +633,7 @@ TomlString* toml_parse_bare_key(TomlParser* 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) @@ -659,7 +655,7 @@ i32 toml_encode_unicode_scalar(TomlString* result, TomlParser *parser, i32 n) if (parser->ptr + n > parser->end) { 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; } @@ -670,7 +666,7 @@ i32 toml_encode_unicode_scalar(TomlString* result, TomlParser *parser, i32 n) toml_move_next(parser); } else { 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; } } @@ -678,7 +674,7 @@ i32 toml_encode_unicode_scalar(TomlString* result, TomlParser *parser, i32 n) if ((scalar >= 0xd800 && scalar <= 0xdfff) || (scalar >= 0xfffe && scalar <= 0xffff)) { 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; } @@ -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", - parser->filename, parser->lineno, parser->colno); + parser->filename, parser->lineno, parser->colno); return TOML_ERR_UNICODE; } @@ -738,12 +734,12 @@ TomlString* toml_parse_basic_string(TomlParser* self) TomlString* result = toml_string_new(); while (self->ptr < self->end &&* self->ptr != '\"' &&* self->ptr != '\n') { - char ch1 =* self->ptr; + 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, '\"'); @@ -763,9 +759,6 @@ TomlString* toml_parse_basic_string(TomlParser* self) } else if (ch2 == 'r') { toml_string_append_char(result, '\r'); toml_move_next(self); - } else if (ch2 == '"') { - toml_string_append_char(result, '\"'); - toml_move_next(self); } else if (ch2 == '\\') { toml_string_append_char(result, '\\'); toml_move_next(self); @@ -782,8 +775,10 @@ TomlString* toml_parse_basic_string(TomlParser* self) return NULL; } } 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); + return NULL; } } else { 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') { 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); + return NULL; } 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') { 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); return NULL; } @@ -825,36 +821,28 @@ TomlString* toml_parse_literal_string(TomlParser* self) TomlValue* toml_parse_basic_string_value(TomlParser* self) { - TomlValue* value = NULL; - TomlString* string = toml_parse_basic_string(self); if (string == NULL) return NULL; - value = toml_value_new(TOML_STRING); + TomlValue* value = toml_value_new(TOML_STRING); value->value.string = string; - return value; } TomlValue* toml_parse_literal_string_value(TomlParser* self) { - TomlValue* value = NULL; - TomlString* string = toml_parse_literal_string(self); if (string == NULL) return NULL; - value = toml_value_new(TOML_STRING); + TomlValue* value = toml_value_new(TOML_STRING); value->value.string = string; - return value; } TomlValue* toml_parse_multi_line_basic_string(TomlParser* self) { - TomlValue* value = NULL; - TomlString* result = toml_string_new(); 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) { - 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, '\"'); @@ -889,9 +877,6 @@ TomlValue* toml_parse_multi_line_basic_string(TomlParser* self) } else if (ch2 == 'r') { toml_string_append_char(result, '\r'); toml_move_next(self); - } else if (ch2 == '"') { - toml_string_append_char(result, '\"'); - toml_move_next(self); } else if (ch2 == '\\') { toml_string_append_char(result, '\\'); toml_move_next(self); @@ -912,8 +897,8 @@ TomlValue* toml_parse_multi_line_basic_string(TomlParser* self) toml_move_next(self); } while (self->ptr + 3 <= self->end && isspace(*self->ptr)); } else { - toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid escape charactor", - self->filename, self->lineno, self->colno); + toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid escape character", + self->filename, self->lineno, self->colno); toml_string_free(result); 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) { 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); return NULL; } toml_next_n(self, 3); - value = toml_value_new(TOML_STRING); + TomlValue* value = toml_value_new(TOML_STRING); value->value.string = result; - return value; } TomlValue* toml_parse_multi_line_literal_string(TomlParser* self) { - TomlValue* value = NULL; - TomlString* result = toml_string_new(); 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) { 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); return NULL; } toml_next_n(self, 3); - value = toml_value_new(TOML_STRING); + TomlValue* value = toml_value_new(TOML_STRING); value->value.string = result; return value; } -TomlValue* toml_parse_datetime(cstr str, u64 len) +TomlValue* toml_parse_datetime(cstr s, u64 len) { - (void)str; + (void)s; (void)len; return toml_value_new(TOML_DATETIME); } TomlValue* toml_parse_int_or_float_or_time(TomlParser* self) { - TomlString* str = NULL; TomlValue* result = NULL; char type = 'i'; 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. // 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'; has_exp = true; } - toml_string_append_char(str,* self->ptr); + toml_string_append_char(s,* self->ptr); } else { break; } @@ -1033,73 +1014,74 @@ TomlValue* toml_parse_int_or_float_or_time(TomlParser* self) type = 'f'; } - toml_string_append_char(str,* self->ptr); + toml_string_append_char(s,* self->ptr); } else if (*self->ptr == '.') { if (type == 'i') { type = 'f'; - toml_string_append_char(str,* self->ptr); + toml_string_append_char(s,* self->ptr); } else { toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid float", - self->filename, self->lineno, self->colno); - toml_string_free(str); + self->filename, self->lineno, self->colno); + toml_string_free(s); return NULL; } } else if (*self->ptr == '_') { if (type == 't') { toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid datetime", - self->filename, self->lineno, self->colno); - toml_string_free(str); + self->filename, self->lineno, self->colno); + toml_string_free(s); return NULL; } if (!isalnum(last_char)) { toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid integer or float", - self->filename, self->lineno, self->colno); - toml_string_free(str); + self->filename, self->lineno, self->colno); + toml_string_free(s); return NULL; } } else if (*self->ptr == '-') { type = 't'; - toml_string_append_char(str,* self->ptr); + toml_string_append_char(s,* self->ptr); } else { break; } - last_char =* self->ptr; + last_char = *self->ptr; toml_move_next(self); } if (last_char == '_') { toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid integer or float or datetime", - self->filename, self->lineno, self->colno); - toml_string_free(str); + self->filename, self->lineno, self->colno); + toml_string_free(s); return NULL; } if (type == 'i') { char* end = NULL; - i64 n = strtoll(str->str, &end, base); - if (end < str->str + str->len) { + i64 n = strtoll(s->s, &end, base); + if (end < s->s + s->len) { toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid integer", - self->filename, self->lineno, self->colno); - toml_string_free(str); + self->filename, self->lineno, self->colno); + toml_string_free(s); return NULL; } result = toml_value_new_integer(n); } else if (type == 'f') { 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"); + f64 n = strtod(s->s, &end); + if (end < s->s + s->len) { + toml_err_set(TOML_ERR_SYNTAX, "%s:%d:%d: invalid float", + self->filename, self->lineno, self->colno); goto cleanup; } result = toml_value_new_float(n); } else if (type == 't') { - result = toml_parse_datetime(str->str, str->len); + result = toml_parse_datetime(s->s, s->len); } cleanup: - toml_string_free(str); + toml_string_free(s); return result; } @@ -1126,8 +1108,7 @@ TomlValue* toml_parse_inline_table(TomlParser* self); TomlValue* toml_parse_value(TomlParser* self) { TomlValue* value = NULL; - - char ch =* self->ptr; + char ch = *self->ptr; if (strncmp(self->ptr, "\"\"\"", 3) == 0) { toml_next_n(self, 3); @@ -1153,7 +1134,8 @@ TomlValue* toml_parse_value(TomlParser* self) value = toml_parse_inline_table(self); } else { 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; @@ -1167,10 +1149,10 @@ TomlErrCode toml_parse_key_value(TomlParser* self, TomlTable* table) 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; @@ -1194,43 +1176,45 @@ 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; } else { 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; } - 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) { - 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; } if (ch != '=') { 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; } 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) { - 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; } @@ -1264,7 +1248,7 @@ TomlErrCode toml_parse_key_value(TomlParser* self, TomlTable* table) toml_move_next(self); } else { 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_string_free(key); return TOML_ERR_SYNTAX; @@ -1339,19 +1323,16 @@ end: TomlValue* toml_parse_inline_table(TomlParser* self) { - TomlValue* table = NULL; - - table = toml_value_new_table(); + TomlValue* table = toml_value_new_table(); while (self->ptr < self->end) { TomlString* key = NULL; TomlValue* value = NULL; - char ch; - ch =* self->ptr; + char 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 == '_') { @@ -1372,37 +1353,39 @@ TomlValue* toml_parse_inline_table(TomlParser* self) break; } else { 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; } - 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) { - 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; } if (ch != '=') { 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; } 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) { - 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; } @@ -1484,13 +1467,14 @@ TomlTable* toml_walk_table_path(TomlParser *parser, TomlTable* table, } else { if (t->type != TOML_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; } - TomlValue* new_table = toml_value_new_table(); + new_table = toml_value_new_table(); toml_array_append(t->value.array, new_table); real_table = new_table->value.table; + new_table = NULL; } } else { for (u64 i = 0; i < key_path->len; i++) { @@ -1579,7 +1563,8 @@ TomlErrCode toml_parse_table(TomlParser* self, TomlTable* table) goto cleanup; } else { 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); @@ -1600,7 +1585,7 @@ TomlErrCode toml_parse_table(TomlParser* self, TomlTable* table) if (key_path->len == 0) { 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; } @@ -1611,7 +1596,7 @@ TomlErrCode toml_parse_table(TomlParser* self, TomlTable* table) 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); + self->filename, self->lineno, self->colno); goto cleanup; } @@ -1632,22 +1617,20 @@ cleanup: TomlTable* toml_parse(TomlParser* self) { - TomlTable* table = NULL; - - table = toml_table_new(); + TomlTable* 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 == '[') { @@ -1660,7 +1643,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); @@ -1670,61 +1653,55 @@ TomlTable* toml_parse(TomlParser* self) return table; } -TomlTable* toml_load_nstr_filename(cstr str, u64 len, +TomlTable* toml_load_nstr_filename(cstr s, u64 len, cstr filename) { - TomlParser *parser = NULL; - TomlTable* table = NULL; - - parser = toml_parser_new(str, len); + TomlParser *parser = toml_parser_new(s, len); parser->filename = toml_strdup(filename); - table = toml_parse(parser); + TomlTable* table = toml_parse(parser); toml_parser_free(parser); 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, ""); + return toml_load_nstr_filename(s, len, ""); } -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* table = NULL; - TomlString* str = NULL; + TomlString* s = toml_string_new(); + toml_string_expand_if_necessary(s, 4095); - str = toml_string_new(); - - toml_string_expand_if_necessary(str, 4095); - - u64 count; - u64 bytes_to_read; + u64 count = 0; + u64 remaining_capacity = 0; do { - bytes_to_read = str->_capacity - str->len - 1; - - count = fread(&str->str[str->len], 1, bytes_to_read, file); + remaining_capacity = s->_capacity - s->len; + count = fread(s->s + s->len, 1, remaining_capacity, 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; } - str->len += count; + s->len += count; - if (str->len + 1 >= str->_capacity) { - toml_string_expand_if_necessary(str, str->_capacity * 2); + if (s->len + 1 >= s->_capacity) { + 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; @@ -1733,7 +1710,7 @@ error: table = NULL; cleanup: - toml_string_free(str); + toml_string_free(s); return table; } @@ -1745,11 +1722,10 @@ TomlTable* toml_load_file(FILE* file) TomlTable* toml_load_filename(cstr filename) { TomlTable* table = NULL; - FILE* f = NULL; - - f = fopen(filename, "r"); + FILE* f = fopen(filename, "r"); 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; } diff --git a/tests/main.c b/tests/main.c index 0ef838a..6de2549 100644 --- a/tests/main.c +++ b/tests/main.c @@ -34,7 +34,7 @@ void print_value(const TomlValue* value) print_array(value->value.array); break; case TOML_STRING: - printf("\"%s\"", value->value.string->str); + printf("\"%s\"", value->value.string->s); break; case TOML_INTEGER: printf("%" PRId64, value->value.integer); @@ -53,7 +53,7 @@ void print_value(const TomlValue* value) void print_keyval(const TomlKeyValue *keyval) { - printf("\"%s\": ", keyval->key->str); + printf("\"%s\": ", keyval->key->s); print_value(keyval->value); }