Mercurial > libedl
diff src/str.c @ 4:c2408abb258a
*: add dumping to string, rename EDL_file to EDL
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 25 Dec 2023 16:24:16 -0500 |
parents | |
children | a6ab6d9c0dac |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/str.c Mon Dec 25 16:24:16 2023 -0500 @@ -0,0 +1,51 @@ +#include <stdlib.h> +#include <string.h> +#include "str.h" + +int EDL_internal_string_init(EDL_internal_string* str) { + if (!str) + return 0; + + str->size = 0; + str->capacity = 0; + str->data = NULL; + return 1; +} + +int EDL_internal_string_allocate(EDL_internal_string* str, size_t new_capacity) { + if (new_capacity == str->capacity) + return 1; // nothing to do + + str->data = realloc(str->data, new_capacity * sizeof(char)); + if (!str->data) + return 0; + + if (new_capacity > str->capacity) + memset(&str->data[str->capacity], 0, (new_capacity - str->capacity) * sizeof(char)); + + str->capacity = new_capacity; + + return 1; +} + +int EDL_internal_string_append(EDL_internal_string* str, const char* data, const size_t length) { + /* this sucks, but I'm too lazy to write anything smarter. */ + { + size_t capacity = 1; + while (capacity < (str->size + length + 1)) + capacity *= 2; + + if (capacity < str->capacity || !EDL_internal_string_allocate(str, capacity)) + return 0; + } + + strncat(str->data, data, length); + str->size += length; + + return 1; +} + +void EDL_internal_string_free(EDL_internal_string* str) { + if (str->data) + free(str->data); +}