diff --git a/core/src/CMakeLists.txt b/core/src/CMakeLists.txt index 44a02ce..07c6ac4 100644 --- a/core/src/CMakeLists.txt +++ b/core/src/CMakeLists.txt @@ -1,4 +1,5 @@ set(CORE_SRC + list.c log.c hash_table.c http_server.c diff --git a/core/src/list.c b/core/src/list.c new file mode 100644 index 0000000..dcf9430 --- /dev/null +++ b/core/src/list.c @@ -0,0 +1,98 @@ +#include "list.h" +#include +#include +#include + +list_t *list_init(size_t type_size, size_t capacity) { + list_t *list; + + list = calloc(1, sizeof(list_t)); + if (!list) { + return NULL; + } + + list->real_capacity = capacity * 2; + + list->data = calloc(1, sizeof(type_size * list->real_capacity)); + if (!list) { + free(list); + return NULL; + } + + list->capacity = capacity; + list->type_size = type_size; + + return list; +} + +list_error_e list_get_last_error(list_t *list) { + list_error_e error = list->error; + + list->error = LIST_NONE; + + return error; +} + +void list_resize(list_t *list, size_t new_capacity) { + void *new_data = calloc(new_capacity * 2, list->type_size); + + if (!new_data) { + list->error = LIST_MEM; + return; + } + + memcpy(new_data, list->data, list->type_size * list->capacity); + free(list->data); + list->data = new_data; +} + +void list_append(list_t *list, void *value) { + if (list->capacity + 1 > list->real_capacity) { + list_resize(list, list->real_capacity * 2); + } + + list->capacity++; + list_set(list, list->capacity, value); +} + +void list_set(list_t *list, size_t idx, void *value) { + if (idx > list->capacity) { + list->error = LIST_OOR; + return; + } + + memcpy(list->data + (idx * list->type_size), value, list->type_size); +} + +void *list_get(list_t *list, size_t idx) { + if (idx <= list->capacity) { + return &list->data[idx]; + } + + list->error = LIST_OOR; + + return NULL; +} + +void list_remove(list_t *list, size_t idx) { + if (idx > list->capacity) { + list->error = LIST_OOR; + + return; + } + + if (idx == list->capacity) { + list->capacity -= 1; + } + + if (idx < list->capacity) { + memcpy(list->data + (idx * list->type_size), + list->data + ((idx + 1) * list->type_size), + (list->capacity - idx) * list->type_size); + } +} + +void list_deinit(list_t *list) { + free(list->data); + free(list); +} diff --git a/core/src/list.h b/core/src/list.h new file mode 100644 index 0000000..7ffcf34 --- /dev/null +++ b/core/src/list.h @@ -0,0 +1,30 @@ +#ifndef LIST_H +#define LIST_H + +#include + +typedef enum { + LIST_NONE = 0, + LIST_MEM, + LIST_OOR, // Out of range +} list_error_e; + +typedef struct { + void *data; + size_t type_size; + size_t capacity; + size_t real_capacity; + + list_error_e error; +} list_t; + +list_t *list_init(size_t type_size, size_t capacity); +list_error_e list_get_last_error(list_t *list); +void list_resize(list_t *list, size_t new_capacity); +void list_append(list_t *list, void *value); +void list_set(list_t *list, size_t idx, void *value); +void *list_get(list_t *list, size_t idx); +void list_remove(list_t *list, size_t idx); +void list_deinit(list_t *list); + +#endif // !LIST_H