summaryrefslogtreecommitdiff
path: root/src/buffer.c
diff options
context:
space:
mode:
authorAnna (navi) Figueiredo Gomes <navi@vlhl.dev>2023-07-08 15:06:33 -0300
committerAnna (navi) Figueiredo Gomes <navi@vlhl.dev>2023-07-08 15:06:33 -0300
commit7a388dad85152a203033c14fee3c64607301865a (patch)
tree054b7bb03883576a8b68da8470908ff259e029d4 /src/buffer.c
libactivity: (tmp name) http request parse
the parse works via callbacks, registered to a method and path. further work will be done to simplify extraction of path, and to make the request struct more private. missing the parsing and handling of reponses. Signed-off-by: Anna (navi) Figueiredo Gomes <navi@vlhl.dev>
Diffstat (limited to 'src/buffer.c')
-rw-r--r--src/buffer.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/buffer.c b/src/buffer.c
new file mode 100644
index 0000000..0759f5d
--- /dev/null
+++ b/src/buffer.c
@@ -0,0 +1,81 @@
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "buffer.h"
+#include "util.h"
+
+static void buf_realloc(struct buffer *buf, size_t new_size) {
+ if (new_size > buf->size) {
+ buf->size = new_size + 1;
+ buf->data = realloc(buf->data, buf->size);
+ }
+}
+
+struct buffer *buf_new(const char *init) {
+ struct buffer *buf = malloc(sizeof(struct buffer));
+ if (init) {
+ buf->data = strdup(init);
+ buf->len = strlen(init);
+ buf->size = buf->len + 1;
+ } else {
+ buf->size = 1024;
+ buf->data = malloc(buf->size);
+ *buf->data = '\0';
+ buf->len = 0;
+ }
+
+ return buf;
+}
+
+void buf_del(struct buffer **buf) {
+ assert(*buf);
+ struct buffer *tmp = *buf;
+ free(tmp->data);
+ free(tmp);
+ *buf = NULL;
+}
+
+struct buffer *buf_dup(struct buffer *src) {
+ struct buffer *dest = malloc(sizeof(struct buffer));
+ dest->len = src->len;
+ dest->size = src->size;
+ dest->data = malloc(dest->size);
+ memcpy(dest->data, src->data, dest->size);
+ return dest;
+}
+
+char *buf_strdup(struct buffer *buf) {
+ return strdup(buf->data);
+}
+
+void buf_append(struct buffer *buf, const char *str) {
+ size_t len = strlen(str);
+ buf_realloc(buf, buf->len + len + 1);
+ strncat(buf->data, str, buf->size);
+}
+
+void buf_printf(struct buffer *buf, char *fmt, ...) {
+ va_list ap;
+ size_t len;
+
+ va_start(ap, fmt);
+ len = vsnprintf(buf->data, buf->size, fmt, ap);
+ va_end(ap);
+
+ if (len >= buf->size) {
+ buf_realloc(buf, len + 1);
+ va_start(ap, fmt);
+ len = vsnprintf(buf->data, buf->size, fmt, ap);
+ va_end(ap);
+ }
+
+ if (len < 0 || len >= buf->size) {
+ fprintf(stderr, "buf_printf: failed to format str");
+ exit(EXIT_FAILURE);
+ }
+
+ buf->len = len;
+}