diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-05-03 17:29:12 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-05-03 18:42:58 +0900 |
| commit | 3654ce578e6fff53950874adf7e0e4ae0a6eb956 (patch) | |
| tree | 5b6c04273de38dba70b7c25e55da144f5f7c37da /src/lib/common.c | |
| parent | 1b406b13b03055d2b2d08e8279a4a80c41ca7c20 (diff) | |
| download | ducc-main.tar.gz ducc-main.tar.zst ducc-main.zip | |
Diffstat (limited to 'src/lib/common.c')
| -rw-r--r-- | src/lib/common.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/lib/common.c b/src/lib/common.c new file mode 100644 index 0000000..af35064 --- /dev/null +++ b/src/lib/common.c @@ -0,0 +1,88 @@ +#include "common.h" +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +void fatal_error(const char* msg, ...) { + va_list args; + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); + fprintf(stderr, "\n"); + exit(1); +} + +bool str_starts_with(const char* s, const char* prefix) { + size_t l1 = strlen(s); + size_t l2 = strlen(prefix); + if (l1 < l2) + return false; + return strncmp(s, prefix, l2) == 0; +} + +bool str_ends_with(const char* s, const char* suffix) { + size_t l1 = strlen(s); + size_t l2 = strlen(suffix); + if (l1 < l2) + return false; + return strcmp(s + l1 - l2, suffix) == 0; +} + +void strbuilder_init(StrBuilder* b) { + b->len = 0; + b->capacity = 16; + b->buf = calloc(b->capacity, sizeof(char)); +} + +// `size` must include a trailing null byte. +void strbuilder_reserve(StrBuilder* b, size_t size) { + if (size <= b->capacity) + return; + while (b->capacity < size) { + b->capacity *= 2; + } + b->buf = realloc(b->buf, b->capacity * sizeof(char)); + memset(b->buf + b->len, 0, (b->capacity - b->len) * sizeof(char)); +} + +void strbuilder_append_char(StrBuilder* b, int c) { + strbuilder_reserve(b, b->len + 1 + 1); + b->buf[b->len++] = c; +} + +void strbuilder_append_string(StrBuilder* b, const char* s) { + int len = strlen(s); + strbuilder_reserve(b, b->len + len + 1); + for (int i = 0; i < len; ++i) { + b->buf[b->len++] = s[i]; + } +} + +void strings_init(StrArray* strings) { + strings->len = 0; + strings->capacity = 32; + strings->data = calloc(strings->capacity, sizeof(const char*)); +} + +void strings_reserve(StrArray* strings, size_t size) { + if (size <= strings->capacity) + return; + while (strings->capacity < size) { + strings->capacity *= 2; + } + strings->data = realloc(strings->data, strings->capacity * sizeof(const char*)); + memset(strings->data + strings->len, 0, (strings->capacity - strings->len) * sizeof(const char*)); +} + +int strings_push(StrArray* strings, const char* str) { + strings_reserve(strings, strings->len + 1); + strings->data[strings->len] = str; + return ++strings->len; +} + +void strings_pop(StrArray* strings) { + if (strings->len > 0) { + strings->len--; + } +} |
