diff options
| author | spl3g <spleefer6@yandex.ru> | 2025-03-22 21:41:16 +0300 |
|---|---|---|
| committer | spl3g <spleefer6@yandex.ru> | 2025-03-22 21:41:16 +0300 |
| commit | 62454373db4cd2add82ded0cf7b21f7d69ade597 (patch) | |
| tree | 1627133fe4567e9a3abb707af5e7289353410d5f | |
| parent | ef5f52fb739c9f0d71c579f08363ef0dfd5c227d (diff) | |
Reorganize the lib
| -rw-r--r-- | Makefile | 28 | ||||
| -rw-r--r-- | README.org | 11 | ||||
| -rw-r--r-- | examples/main.c (renamed from main.c) | 8 | ||||
| -rw-r--r-- | include/chttp/arena.h (renamed from lib/arena.h) | 0 | ||||
| -rw-r--r-- | include/chttp/arena_strings.h (renamed from lib/arena_strings.h) | 0 | ||||
| -rw-r--r-- | include/chttp/const_strings.h (renamed from lib/const_strings.h) | 0 | ||||
| -rw-r--r-- | include/chttp/hashmap.h | 27 | ||||
| -rw-r--r-- | include/chttp/http.h (renamed from lib/http.h) | 0 | ||||
| -rw-r--r-- | src/arena.c (renamed from lib/arena.c) | 2 | ||||
| -rw-r--r-- | src/arena.o | bin | 0 -> 3592 bytes | |||
| -rw-r--r-- | src/arena_strings.c (renamed from lib/arena_strings.c) | 2 | ||||
| -rw-r--r-- | src/arena_strings.o | bin | 0 -> 2544 bytes | |||
| -rw-r--r-- | src/const_strings.c (renamed from lib/const_strings.c) | 2 | ||||
| -rw-r--r-- | src/const_strings.o | bin | 0 -> 3048 bytes | |||
| -rw-r--r-- | src/hashmap.c | 43 | ||||
| -rw-r--r-- | src/hashmap.o | bin | 0 -> 2296 bytes | |||
| -rw-r--r-- | src/http.c (renamed from lib/http.c) | 2 | ||||
| -rw-r--r-- | src/http.o | bin | 0 -> 23744 bytes |
18 files changed, 112 insertions, 13 deletions
@@ -1,2 +1,26 @@ -main: main.c lib - cc -Wall -Wextra -ggdb -o main main.c lib/arena_strings.c lib/http.c lib/arena.c lib/const_strings.c +CC = gcc +AR = ar +CFLAGS = -Wall -Wextra -fPIC -Iinclude/ + +LIBNAME = chttp +SOURCES = $(wildcard src/*.c) +OBJECTS = $(SOURCES:.c=.o) + +all: static + +static: $(OBJECTS) + $(AR) rcs $(LIBNAME).a $(OBJECTS) + +shared: $(OBJECTS) + $(CC) -shared -o $(LIBNAME).so $(OBJECTS) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +install: all + install -d /usr/local/include/yourlib/ + install include/yourlib/*.h /usr/local/include/yourlib/ + install $(LIBNAME) /usr/local/lib/ + +clean: + rm -f src/*.o *.a *.so @@ -1,7 +1,12 @@ * chttp This is not really a library, but a little project for my education. But if you really want to use it, you can. ** Installation -Just copy the lib directory and use the header files. +#+begin_src shell + make static + # or + make shared +#+end_src +Copy the directory in the =include/= folder to your project. ** Usage *** Initializing the server #+begin_src c @@ -43,8 +48,8 @@ And to wrap things up, we need to run #+end_src *** Full example #+begin_src c - #include "lib/http.h" - #include "lib/const_strings.h" + #include <chttp/http.h> + #include <chttp/const_strings.h> void logging_func(http_middleware *self, http_request req) { http_run_next(self, req); @@ -1,5 +1,5 @@ -#include "lib/const_strings.h" -#include "lib/http.h" +#include <chttp/const_strings.h> +#include <chttp/http.h> void success(http_request req) { req.resp->code = OK; @@ -13,7 +13,7 @@ void logging_func(http_middleware *self, http_request req) { } void whatever(http_middleware *self, http_request req) { - http_log(HTTP_INFO, "Whatever"); + http_log(HTTP_INFO, "Whatever\n"); http_run_next(self, req); } @@ -25,7 +25,7 @@ int main() { } http_register_global_middleware(&serv, logging_func); - http_handler *success_handler = http_handle_path(&serv, CS("GET"), CS("/"), success); + http_handler *success_handler = http_handle_path(&serv, "GET", "/", success); http_register_handler_middleware(&arena, success_handler, whatever); listen_and_serve(&serv); diff --git a/lib/arena.h b/include/chttp/arena.h index 1f53355..1f53355 100644 --- a/lib/arena.h +++ b/include/chttp/arena.h diff --git a/lib/arena_strings.h b/include/chttp/arena_strings.h index b25435d..b25435d 100644 --- a/lib/arena_strings.h +++ b/include/chttp/arena_strings.h diff --git a/lib/const_strings.h b/include/chttp/const_strings.h index cb92442..cb92442 100644 --- a/lib/const_strings.h +++ b/include/chttp/const_strings.h diff --git a/include/chttp/hashmap.h b/include/chttp/hashmap.h new file mode 100644 index 0000000..eefb5ee --- /dev/null +++ b/include/chttp/hashmap.h @@ -0,0 +1,27 @@ +#ifndef HASHMAP_H_ +#define HASHMAP_H_ + +#include <stddef.h> +#include "const_strings.h" +#include "arena.h" + +#define HASHMAP_DEFAULT_R 31 + +typedef struct hashitem hashitem; +struct hashitem { + hashitem *next; + const_string key; + void* val; +}; + +typedef struct { + size_t cap; + hashitem data[]; +} hashmap; + +size_t hash_str(const_string str, size_t cap); +hashmap *hashmap_init(arena *a, size_t cap); +size_t hashmap_kstr_insert(arena *a, hashmap* map, const_string key, void* val); +void *hashmap_kstr_get(hashmap *map, const_string key); + +#endif // HASHMAP_H_ diff --git a/lib/http.h b/include/chttp/http.h index 4fcc418..4fcc418 100644 --- a/lib/http.h +++ b/include/chttp/http.h diff --git a/lib/arena.c b/src/arena.c index 8cd1748..8b4828f 100644 --- a/lib/arena.c +++ b/src/arena.c @@ -1,4 +1,4 @@ -#include "arena.h" +#include <chttp/arena.h> region *new_region(size_t cap) { size_t size_bytes = sizeof(region) + sizeof(uintptr_t)*cap; diff --git a/src/arena.o b/src/arena.o Binary files differnew file mode 100644 index 0000000..845d142 --- /dev/null +++ b/src/arena.o diff --git a/lib/arena_strings.c b/src/arena_strings.c index 3497c2e..78f98a1 100644 --- a/lib/arena_strings.c +++ b/src/arena_strings.c @@ -1,4 +1,4 @@ -#include "arena_strings.h" +#include <chttp/arena_strings.h> const_string arena_cs_init(arena *a, int len) { char *str = new(a, char, len+1); diff --git a/src/arena_strings.o b/src/arena_strings.o Binary files differnew file mode 100644 index 0000000..b16bcec --- /dev/null +++ b/src/arena_strings.o diff --git a/lib/const_strings.c b/src/const_strings.c index 2d743b1..7712eac 100644 --- a/lib/const_strings.c +++ b/src/const_strings.c @@ -1,4 +1,4 @@ -#include "const_strings.h" +#include <chttp/const_strings.h> const_string cs_from_parts(const char* data, int len) { const_string str; diff --git a/src/const_strings.o b/src/const_strings.o Binary files differnew file mode 100644 index 0000000..7154db9 --- /dev/null +++ b/src/const_strings.o diff --git a/src/hashmap.c b/src/hashmap.c new file mode 100644 index 0000000..c68d110 --- /dev/null +++ b/src/hashmap.c @@ -0,0 +1,43 @@ +#include <chttp/hashmap.h> + +size_t hash_str(const_string str, size_t cap) { + size_t hash = 0; + for (int i = 0; i < str.len; i++) { + hash = (HASHMAP_DEFAULT_R * hash + str.data[i]) % cap; + } + return hash; +} + +hashmap *hashmap_init(arena *a, size_t cap) { + hashmap *map = arena_alloc(a, sizeof(hashmap) + sizeof(ptrdiff_t) * cap); + map->cap = cap; + return map; +} + +size_t hashmap_kstr_insert(arena *a, hashmap* map, const_string key, void* val) { + size_t hash = hash_str(key, map->cap); + if (map->data[hash].key.len > 0) { + map->data[hash].next = arena_alloc(a, sizeof(hashitem)); + map->data[hash].next->key = key; + map->data[hash].next->val = val; + } else { + map->data[hash].key = key; + map->data[hash].val = val; + } + + return hash; +} + +void *hashmap_kstr_get(hashmap *map, const_string key) { + size_t hash = hash_str(key, map->cap); + hashitem *item = &map->data[hash]; + while (item->next != NULL && !cs_eq(item->key, key)) { + item = item->next; + } + + if (!cs_eq(item->key, key)) { + return NULL; + } + + return item->val; +} diff --git a/src/hashmap.o b/src/hashmap.o Binary files differnew file mode 100644 index 0000000..5dd78ec --- /dev/null +++ b/src/hashmap.o @@ -1,4 +1,4 @@ -#include "http.h" +#include <chttp/http.h> #define MAX_DATA_SIZE 256 #define BUFFER_SIZE 4096 diff --git a/src/http.o b/src/http.o Binary files differnew file mode 100644 index 0000000..9d4103c --- /dev/null +++ b/src/http.o |
