replaced char* function arguments with str

This commit is contained in:
Timerix 2025-11-10 02:16:02 +05:00
parent 247d291da6
commit c83ee4078e
3 changed files with 63 additions and 175 deletions

View File

@ -12,8 +12,7 @@ extern "C" {
#include "tlibc/std.h" #include "tlibc/std.h"
#include "tlibc/string/str.h" #include "tlibc/string/str.h"
#define false 0 typedef struct tm TomlDateTime;
#define true 1
typedef enum { typedef enum {
TOML_OK, TOML_OK,
@ -26,8 +25,7 @@ typedef enum {
typedef struct { typedef struct {
TomlErrCode code; TomlErrCode code;
char* message; NULLABLE(char*) message;
i32 _is_literal;
} TomlErr; } TomlErr;
typedef struct { typedef struct {
@ -75,7 +73,7 @@ struct _TomlValue {
TomlString* string; TomlString* string;
i64 integer; i64 integer;
f64 float_; f64 float_;
struct tm datetime; TomlDateTime datetime;
bool boolean; bool boolean;
} value; } value;
}; };
@ -97,20 +95,13 @@ 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 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); 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 s); TomlString* toml_string_from_str(str s);
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 s); void toml_string_append_str(TomlString* self, str s);
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);
@ -120,17 +111,15 @@ void toml_table_free(TomlTable* self);
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* toml_table_get_by_string(const TomlTable* self, const TomlString* key); 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_set(TomlTable* self, str key, TomlValue* value);
void toml_table_setn(TomlTable* self, cstr key, u64 key_len, TomlValue* value); TomlValue* toml_table_get(const TomlTable* self, str key);
TomlValue* toml_table_get(const TomlTable* self, cstr key); TomlTable* toml_table_get_table(const TomlTable* self, str key);
TomlTable* toml_table_get_as_table(const TomlTable* self, cstr key); TomlArray* toml_table_get_array(const TomlTable* self, str key);
TomlArray* toml_table_get_as_array(const TomlTable* self, cstr key); TomlString* toml_table_get_string(const TomlTable* self, str key);
TomlString* toml_table_get_as_string(const TomlTable* self, cstr key); i64 toml_table_get_integer(const TomlTable* self, str key);
i64 toml_table_get_as_integer(const TomlTable* self, cstr key); f64 toml_table_get_float(const TomlTable* self, str key);
f64 toml_table_get_as_float(const TomlTable* self, cstr key); const TomlDateTime* toml_table_get_datetime(const TomlTable* self, str key);
const struct tm* toml_table_get_as_datetime(const TomlTable* self, cstr key); i32 toml_table_get_boolean(const TomlTable* self, str 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); TomlTableIter toml_table_iter_new(TomlTable* table);
TomlKeyValue* toml_table_iter_get(TomlTableIter* self); TomlKeyValue* toml_table_iter_get(TomlTableIter* self);
@ -149,17 +138,15 @@ 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 s); TomlValue* toml_value_from_str(str s);
void toml_value_free(TomlValue* self); void toml_value_free(TomlValue* self);
TomlTable* toml_load_str(cstr s); TomlTable* toml_load_str(str s);
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);
/* TODO: implement dump functions /* TODO: implement dump functions
char* toml_dump_str(const TomlTable* self, TomlErr *err); TomlString* 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); void toml_dump_file(const TomlTable* self, FILE* file, TomlErr *err);
*/ */

View File

@ -4,20 +4,16 @@
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include <pthread.h>
#include "tlibtoml/toml.h" #include "tlibtoml/toml.h"
#include "tlibc/errors.h"
#include "tlibc/string/StringBuilder.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, NULL };
static void* toml_default_malloc(void* context, u64 size) static void* toml_default_malloc(void* context, u64 size)
{ {
(void)context; (void)context;
void* p = malloc(size); void* p = malloc(size);
assert(p != NULL);
return p; return p;
} }
@ -25,7 +21,6 @@ static void* toml_default_realloc(void* context, void* p, u64 size)
{ {
(void)context; (void)context;
void* ptr = realloc(p, size); void* ptr = realloc(p, size);
assert(ptr != NULL);
return ptr; return ptr;
} }
@ -67,55 +62,6 @@ void toml_free(void* p)
} }
} }
char* toml_strdup(cstr s)
{
u64 len = strlen(s) + 1;
void* new = toml_malloc(len);
if (new == NULL)
return NULL;
return memcpy(new, s, len);
}
char* toml_strndup(cstr s, u64 n)
{
char* result = toml_malloc(n + 1);
if (result == NULL)
return NULL;
result[n] = 0;
return memcpy(result, s, n);
}
i32 toml_vasprintf(char** s, cstr format, va_list args)
{
i32 size = 0;
va_list args_copy;
va_copy(args_copy, args);
size = vsnprintf(NULL, (u64)size, format, args_copy);
va_end(args_copy);
if (size < 0) {
return size;
}
*s = toml_malloc((u64)size + 1);
if (*s == NULL)
return -1;
return vsprintf(*s, format, args);
}
i32 toml_asprintf(char** s, cstr format, ...)
{
va_list args;
va_start(args, format);
i32 size = toml_vasprintf(s, format, args);
va_end(args);
return size;
}
const TomlErr* toml_err(void) const TomlErr* toml_err(void)
{ {
return &g_err; return &g_err;
@ -123,14 +69,8 @@ const TomlErr* toml_err(void)
void toml_err_clear(void) void toml_err_clear(void)
{ {
if (g_err.code != TOML_OK) {
if (!g_err._is_literal) {
toml_free(g_err.message); toml_free(g_err.message);
}
g_err.code = TOML_OK; g_err.code = TOML_OK;
g_err.message = (char*)"";
g_err._is_literal = true;
}
} }
static inline void toml_err_set(TomlErrCode code, cstr format, ...) static inline void toml_err_set(TomlErrCode code, cstr format, ...)
@ -138,23 +78,15 @@ static inline void toml_err_set(TomlErrCode code, cstr format, ...)
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); if(g_err.code != TOML_OK)
toml_err_clear();
va_list args; va_list args;
va_start(args, format); va_start(args, format);
g_err.code = code; g_err.code = code;
toml_vasprintf(&g_err.message, format, args); g_err.message = vsprintf_malloc(format, args);
g_err._is_literal = false;
va_end(args); va_end(args);
} }
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 = true;
}
static inline u64 toml_roundup_pow_of_two_u64(u64 v) static inline u64 toml_roundup_pow_of_two_u64(u64 v)
{ {
v--; v--;
@ -177,20 +109,13 @@ TomlString* toml_string_new(void)
return self; return self;
} }
TomlString* toml_string_from_str(cstr s) TomlString* toml_string_from_str(str s)
{ {
TomlString* self = toml_string_new(); TomlString* self = toml_string_new();
toml_string_append_str(self, s); toml_string_append_str(self, s);
return self; return self;
} }
TomlString* toml_string_from_nstr(cstr s, u64 len)
{
TomlString* self = toml_string_new();
toml_string_append_nstr(self, s, len);
return self;
}
static inline void toml_string_expand_if_necessary(TomlString* self, u64 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) { if (self->len + len_to_add + 1 > self->_capacity) {
@ -209,19 +134,11 @@ void toml_string_append_char(TomlString* self, char ch)
self->len++; self->len++;
} }
void toml_string_append_str(TomlString* self, cstr s) void toml_string_append_str(TomlString* self, str s)
{ {
u64 len = strlen(s); toml_string_expand_if_necessary(self, s.size);
toml_string_expand_if_necessary(self, len); memcpy(self->s + self->len, s.data, s.size);
memcpy(self->s + self->len, s, len + 1); self->len += s.size;
self->len += len;
}
void toml_string_append_nstr(TomlString* self, cstr s, u64 len)
{
toml_string_expand_if_necessary(self, len);
memcpy(self->s + self->len, s, len);
self->len += len;
self->s[self->len] = 0; self->s[self->len] = 0;
} }
@ -235,7 +152,7 @@ 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->s, self->len); return toml_string_from_str(str_construct(self->s, self->len, true));
} }
i32 toml_string_equals(const TomlString* self, const TomlString* other) i32 toml_string_equals(const TomlString* self, const TomlString* other)
@ -322,18 +239,13 @@ TomlValue* toml_table_get_by_string(const TomlTable* self, const TomlString* key
return value; return value;
} }
TomlValue* toml_table_getn(const TomlTable* self, cstr key, u64 key_len) TomlValue* toml_table_get(const TomlTable* self, str key)
{ {
TomlString s = {(char* )key, key_len, 0}; TomlString s = {(char*)key.data, key.size, 0};
return toml_table_get_by_string(self, &s); return toml_table_get_by_string(self, &s);
} }
TomlValue* toml_table_get(const TomlTable* self, cstr key) TomlTable* toml_table_get_table(const TomlTable* self, str key)
{
return toml_table_getn(self, key, strlen(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 != NULL);
@ -341,7 +253,7 @@ TomlTable* toml_table_get_as_table(const TomlTable* self, cstr key)
return v->value.table; return v->value.table;
} }
TomlArray* toml_table_get_as_array(const TomlTable* self, cstr key) TomlArray* toml_table_get_array(const TomlTable* self, str key)
{ {
TomlValue* v = toml_table_get(self, key); TomlValue* v = toml_table_get(self, key);
assert(v != NULL); assert(v != NULL);
@ -349,7 +261,7 @@ TomlArray* toml_table_get_as_array(const TomlTable* self, cstr key)
return v->value.array; return v->value.array;
} }
TomlString* toml_table_get_as_string(const TomlTable* self, cstr key) TomlString* toml_table_get_string(const TomlTable* self, str key)
{ {
TomlValue* v = toml_table_get(self, key); TomlValue* v = toml_table_get(self, key);
assert(v != NULL); assert(v != NULL);
@ -357,7 +269,7 @@ TomlString* toml_table_get_as_string(const TomlTable* self, cstr key)
return v->value.string; return v->value.string;
} }
i64 toml_table_get_as_integer(const TomlTable* self, cstr key) i64 toml_table_get_integer(const TomlTable* self, str key)
{ {
TomlValue* v = toml_table_get(self, key); TomlValue* v = toml_table_get(self, key);
assert(v != NULL); assert(v != NULL);
@ -365,7 +277,7 @@ i64 toml_table_get_as_integer(const TomlTable* self, cstr key)
return v->value.integer; return v->value.integer;
} }
f64 toml_table_get_as_float(const TomlTable* self, cstr key) f64 toml_table_get_float(const TomlTable* self, str key)
{ {
TomlValue* v = toml_table_get(self, key); TomlValue* v = toml_table_get(self, key);
assert(v != NULL); assert(v != NULL);
@ -373,7 +285,7 @@ f64 toml_table_get_as_float(const TomlTable* self, cstr key)
return v->value.float_; return v->value.float_;
} }
const struct tm* toml_table_get_as_datetime(const TomlTable* self, cstr key) const TomlDateTime* toml_table_get_datetime(const TomlTable* self, str key)
{ {
TomlValue* v = toml_table_get(self, key); TomlValue* v = toml_table_get(self, key);
assert(v != NULL); assert(v != NULL);
@ -381,7 +293,7 @@ const struct tm* toml_table_get_as_datetime(const TomlTable* self, cstr key)
return &v->value.datetime; return &v->value.datetime;
} }
i32 toml_table_get_as_boolean(const TomlTable* self, cstr key) i32 toml_table_get_boolean(const TomlTable* self, str key)
{ {
TomlValue* v = toml_table_get(self, key); TomlValue* v = toml_table_get(self, key);
assert(v != NULL); assert(v != NULL);
@ -389,17 +301,12 @@ i32 toml_table_get_as_boolean(const TomlTable* self, cstr key)
return v->value.boolean; return v->value.boolean;
} }
void toml_table_setn(TomlTable* self, cstr key, u64 key_len, TomlValue* value) void toml_table_set(TomlTable* self, str key, TomlValue* value)
{ {
TomlString* s = toml_string_from_nstr(key, key_len); TomlString* s = toml_string_from_str(key);
toml_table_set_by_string(self, s, value); toml_table_set_by_string(self, s, 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 }; TomlTableIter self = { table, table->_keyvals };
@ -451,7 +358,7 @@ void toml_array_expand_if_necessary(TomlArray* self)
{ {
if (self->len + 1 > self->_capacity) { if (self->len + 1 > self->_capacity) {
u64 new_capacity = self->_capacity > 0 ? self->_capacity * 2 : 8; u64 new_capacity = self->_capacity > 0 ? self->_capacity * 2 : 8;
void* p = toml_realloc(self->elements, sizeof(TomlValue* ) * new_capacity); void* p = toml_realloc(self->elements, sizeof(TomlValue*) * new_capacity);
self->elements = p; self->elements = p;
self->_capacity = new_capacity; self->_capacity = new_capacity;
} }
@ -487,13 +394,13 @@ TomlValue* toml_value_new(TomlType type)
self->value.boolean = false; self->value.boolean = false;
break; break;
case TOML_DATETIME: case TOML_DATETIME:
memset(&self->value.datetime, 0, sizeof(struct tm)); memset(&self->value.datetime, 0, sizeof(TomlDateTime));
break; break;
} }
return self; return self;
} }
TomlValue* toml_value_from_str(cstr s) TomlValue* toml_value_from_str(str s)
{ {
TomlValue* self = toml_malloc(sizeof(TomlValue)); TomlValue* self = toml_malloc(sizeof(TomlValue));
self->value.string = toml_string_from_str(s); self->value.string = toml_string_from_str(s);
@ -560,7 +467,7 @@ void toml_value_free(TomlValue* self)
toml_array_free(self->value.array); toml_array_free(self->value.array);
break; break;
case TOML_DATETIME: case TOML_DATETIME:
memset(&self->value.datetime, 0, sizeof(struct tm)); memset(&self->value.datetime, 0, sizeof(TomlDateTime));
break; break;
default: default:
break; break;
@ -578,12 +485,12 @@ typedef struct _TomlParser {
char* filename; char* filename;
} TomlParser; } TomlParser;
TomlParser *toml_parser_new(cstr s, u64 len) TomlParser* toml_parser_new(str s)
{ {
TomlParser* self = toml_malloc(sizeof(TomlParser)); TomlParser* self = toml_malloc(sizeof(TomlParser));
self->begin = s; self->begin = s.data;
self->end = s + len; self->end = s.data + s.size;
self->ptr = s; self->ptr = s.data;
self->lineno = 1; self->lineno = 1;
self->colno = 1; self->colno = 1;
self->filename = NULL; self->filename = NULL;
@ -633,7 +540,7 @@ TomlString* toml_parse_bare_key(TomlParser* self)
toml_move_next(self); toml_move_next(self);
} }
return toml_string_from_nstr(s, len); return toml_string_from_str(str_construct((char*)s, len, false));
} }
char toml_hex_char_to_int(char ch) char toml_hex_char_to_int(char ch)
@ -649,7 +556,7 @@ char toml_hex_char_to_int(char ch)
return 0; return 0;
} }
i32 toml_encode_unicode_scalar(TomlString* result, TomlParser *parser, i32 n) i32 toml_encode_unicode_scalar(TomlString* result, TomlParser* parser, i32 n)
{ {
u32 scalar = 0; u32 scalar = 0;
@ -950,10 +857,10 @@ TomlValue* toml_parse_multi_line_literal_string(TomlParser* self)
return value; return value;
} }
TomlValue* toml_parse_datetime(cstr s, u64 len) TomlValue* toml_parse_datetime(str s)
{ {
(void)s; (void)s;
(void)len; //TODO: parse datetime
return toml_value_new(TOML_DATETIME); return toml_value_new(TOML_DATETIME);
} }
@ -1077,7 +984,7 @@ TomlValue* toml_parse_int_or_float_or_time(TomlParser* self)
} }
result = toml_value_new_float(n); result = toml_value_new_float(n);
} else if (type == 't') { } else if (type == 't') {
result = toml_parse_datetime(s->s, s->len); result = toml_parse_datetime(str_construct(s->s, s->len, false));
} }
cleanup: cleanup:
@ -1417,7 +1324,7 @@ end:
return table; return table;
} }
TomlTable* toml_walk_table_path(TomlParser *parser, TomlTable* table, TomlTable* toml_walk_table_path(TomlParser* parser, TomlTable* table,
TomlArray *key_path, i32 is_array, TomlArray *key_path, i32 is_array,
i32 create_if_not_exist) i32 create_if_not_exist)
{ {
@ -1653,11 +1560,10 @@ TomlTable* toml_parse(TomlParser* self)
return table; return table;
} }
TomlTable* toml_load_nstr_filename(cstr s, u64 len, TomlTable* toml_load_str_filename(str s, cstr filename)
cstr filename)
{ {
TomlParser *parser = toml_parser_new(s, len); TomlParser* parser = toml_parser_new(s);
parser->filename = toml_strdup(filename); parser->filename = str_copy(str_from_cstr(filename)).data;
TomlTable* table = toml_parse(parser); TomlTable* table = toml_parse(parser);
@ -1665,14 +1571,9 @@ TomlTable* toml_load_nstr_filename(cstr s, u64 len,
return table; return table;
} }
TomlTable* toml_load_nstr(cstr s, u64 len) TomlTable* toml_load_str(str s)
{ {
return toml_load_nstr_filename(s, len, "<string>"); return toml_load_str_filename(s, "<string>");
}
TomlTable* toml_load_str(cstr s)
{
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)
@ -1701,7 +1602,7 @@ TomlTable* toml_load_file_filename(FILE* file, cstr filename)
s->s[s->len] = 0; s->s[s->len] = 0;
table = toml_load_nstr_filename(s->s, s->len, filename); table = toml_load_str_filename(str_construct(s->s, s->len, false), filename);
goto cleanup; goto cleanup;

View File

@ -59,7 +59,7 @@ void print_keyval(const TomlKeyValue *keyval)
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("{"); printf("{");
u64 i = 0; u64 i = 0;
@ -117,7 +117,7 @@ i32 main(void)
PROJECT_SOURCE_DIR "/tests/hard_example_unicode.toml" PROJECT_SOURCE_DIR "/tests/hard_example_unicode.toml"
}; };
i32 total_tests = sizeof(filenames) / sizeof(char* ); i32 total_tests = sizeof(filenames) / sizeof(char*);
i32 num_passed = 0; i32 num_passed = 0;
i32 num_failed = 0; i32 num_failed = 0;