diff options
author | Anna (navi) Figueiredo Gomes <navi@vlhl.dev> | 2023-07-08 15:06:33 -0300 |
---|---|---|
committer | Anna (navi) Figueiredo Gomes <navi@vlhl.dev> | 2023-07-08 15:06:33 -0300 |
commit | 7a388dad85152a203033c14fee3c64607301865a (patch) | |
tree | 054b7bb03883576a8b68da8470908ff259e029d4 /src/util.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/util.c')
-rw-r--r-- | src/util.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..e6b7977 --- /dev/null +++ b/src/util.c @@ -0,0 +1,57 @@ +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> + +#include "util.h" + +int vasprintf(char **dest, const char *fmt, va_list ap) { + assert(*dest == NULL); + char *buf = NULL; size_t len, size; + va_list ap2; + + size = 4096; + buf = malloc(size); + + va_copy(ap2, ap); + len = vsnprintf(buf, size, fmt, ap); + va_end(ap); + + if (len >= size) { + size = len + 1; + buf = realloc(buf, size); + va_copy(ap2, ap); + len = vsnprintf(buf, size, fmt, ap); + va_end(ap2); + } + if (len < 0 || len >= size) { + fprintf(stderr, "asprintf: failed to format buffer"); + free(buf); + exit(EXIT_FAILURE); + } + + *dest = buf; + return len; +} + +int asprintf(char **dest, const char *fmt, ...) { + va_list ap; + int ret; + va_start(ap, fmt); + ret = vasprintf(dest, fmt, ap); + va_end(ap); + return ret; +} + +size_t hash(const char *str, int max_val) { + size_t ret = 0; + + for (const char *p = str; *p != '\0'; p++) { + ret += (size_t)*p + ((size_t)*p << 6) + ((size_t)*p << 16); + } + + ret = ret % max_val; + + return ret; +} |