diff options
| author | Murilo Ijanc <[email protected]> | 2026-07-26 12:25:48 -0300 |
|---|---|---|
| committer | Murilo Ijanc <[email protected]> | 2026-07-26 12:25:48 -0300 |
| commit | e2c6ae13af335b311da2429c903032124f926df2 (patch) | |
| tree | 73776fab7f3e52124a217e2768968fc9a8a76c34 /src/util.c | |
| download | c-e2c6ae13af335b311da2429c903032124f926df2.tar.gz | |
Initial importv0.1.0
Diffstat (limited to 'src/util.c')
| -rw-r--r-- | src/util.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..a2c6c18 --- /dev/null +++ b/src/util.c @@ -0,0 +1,78 @@ +#include "internal.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +void cepis_buf_init(cepis_buf_t *buf) +{ + buf->data = NULL; + buf->len = 0; + buf->cap = 0; +} + +bool cepis_buf_append(cepis_buf_t *buf, const char *data, size_t len) +{ + if (buf->len + len + 1 > buf->cap) { + size_t cap = buf->cap ? buf->cap : 256; + char *grown; + while (buf->len + len + 1 > cap) { + if (cap > (size_t)-1 / 2) + return false; + cap *= 2; + } + grown = realloc(buf->data, cap); + if (grown == NULL) + return false; + buf->data = grown; + buf->cap = cap; + } + if (len > 0) + memcpy(buf->data + buf->len, data, len); + buf->len += len; + buf->data[buf->len] = '\0'; + return true; +} + +bool cepis_buf_append_str(cepis_buf_t *buf, const char *text) +{ + return cepis_buf_append(buf, text, strlen(text)); +} + +void cepis_buf_free(cepis_buf_t *buf) +{ + free(buf->data); + cepis_buf_init(buf); +} + +char *cepis_strdup(const char *text) +{ + size_t len; + char *copy; + + if (text == NULL) + text = ""; + len = strlen(text); + copy = malloc(len + 1); + if (copy == NULL) + return NULL; + memcpy(copy, text, len + 1); + return copy; +} + +cepis_status_t cepis_set_error(cepis_error_t *err, cepis_status_t status, + long http_status, double retry_after, + const char *fmt, ...) +{ + va_list args; + + if (err == NULL) + return status; + err->status = status; + err->http_status = http_status; + err->retry_after = retry_after; + va_start(args, fmt); + vsnprintf(err->message, sizeof(err->message), fmt, args); + va_end(args); + return status; +} |