diff --git a/src/.list.c.swp b/src/.list.c.swp new file mode 100644 index 0000000..a4f3be2 Binary files /dev/null and b/src/.list.c.swp differ diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000..de45d99 --- /dev/null +++ b/src/list.c @@ -0,0 +1,264 @@ +#include "list.h" + +list_t* list_init() { + list_t* list = (list_t*)malloc(sizeof(list_t)); + list->front = NULL; + list->back = NULL; + list->iterator = NULL; + list->size = 0; + + return list; +} + +static list_node_t* list_node_init() { + list_node_t* node = (list_node_t*)malloc(sizeof(list_node_t)); + node->next = NULL; + node->data = NULL; + + return node; +} + +static list_node_t* list_get_raw(list_t *list, int pos) { + int i = 0; + list_node_t *ptr = list->front; + for(i = 0; i < pos; ++i) { + if(i == pos) + return ptr; + + ptr = ptr->next; + } + + return NULL; +} + +static void list_add_first(list_t *list, void *data) { + if(list->size != 0) + return; + + list->front = list->back = list_node_init(); + list->front->data = data; +} + +void list_append(list_t *list, void *data) { + if(list->size == 0) { + list_add_first(list, data); + return; + } + + list->back->next = list_node_init(); + list->back = list->back->next; + list->back->data = data; + list->size++; +} + +void list_prepend(list_t *list, void *data) { + if(list->size == 0) { + list_add_first(list, data); + return; + } + + list_node_t* node = list_node_init(); + node->data = data; + node->next = list->front; + list->front = node; + list->size++; +} + +void list_insert(list_t *list, void *data, int pos) { + if(list->size == 0) { + list_add_first(list, data); + return; + } + + if(pos <= 0) + list_prepend(list, data); + + if(pos >= list->size) + list_append(list, data); + + list_node_t *node = list_node_init(); + list_node_t *prior = list_get_raw(list, pos - 1); + node->data = data; + node->next = prior->next; + prior->next = node; +} + +int list_size(list_t *list) { + return list->size; +} + +void* list_get(list_t *list, int pos) { + if(pos < 0 || pos > list->size - 1) + return NULL; + + return list_get_raw(list, pos)->data; +} + +int list_find(list_t *list, void *data) { + int i; + list_node_t *ptr = list->front; + for(i = 0; i < list->size; ++i) { + if(ptr->data == data) + return i; + + ptr = ptr->next; + } + + return -1; +} + +int list_func_find(list_t *list, listfindfptr ffptr) { + int i; + list_node_t *ptr = list->front; + for(i = 0; i < list->size; ++i) { + if((*ffptr)(ptr->data) == TRUE) + return i; + + ptr = ptr->next; + } + + return -1; +} + +void* list_remove(list_t *list, int pos) { + if(pos <= 0) + list_remove_front(list); + + list_node_t *prior = list_get_raw(list, pos-1); + list_node_t *node = prior->next; + prior->next = node->next; + void *data = node->data; + free(node); + + if(prior->next == NULL) + list->back = prior; + + return data; +} + +void* list_remove_front(list_t *list) { + list_node_t *after = list->front->next; + void *data = list->front->data; + free(list->front); + list->front = after; + + return data; +} + +void* list_remove_back(list_t *list) { + return list_remove(list, list->size - 1); +} + +void list_iter_reset(list_t *list) { + list->iterator = NULL; + list->iter_prev = NULL; + list->iter_pos = -1; +} + +void* list_iter_next(list_t *list) { + if(list->iter_pos == -1) { + list->iter_prev = list->iterator; + list->iterator = list->front; + } else if(list->iterator != NULL) { + list->iter_prev = list->iterator; + list->iterator = list->iterator->next; + } else + return NULL; + + list->iter_pos++; + return list->iterator == NULL ? NULL : list->iterator->data; +} + +void* list_iter_remove(list_t *list) { + if(list->iter_prev == NULL && list->iter_pos != 0) + return NULL; + + list_node_t *after = list->iterator->next; + void *data = list->iterator->data; + free(list->iterator); + list->iterator = list->iter_prev; + list->iter_prev = NULL; + + if(list->iterator == NULL) + list->front = after; + else + list->iterator->next = after; + + if(after == NULL) + list->back = list->iterator; + + list->size--; + list->iter_pos--; + return data; +} + +void list_iter_insert_before(list_t *list, void *data) { + if(list->size == 0) { + list_add_first(list, data); + return; + } + + list_node_t *node = list_node_init(); + node->data = data; + node->next = list->iterator; + + if(list->iter_pos == 0) + list->front = node; + else + list->iter_prev->next = node; + + list->iter_prev = node; + list->size++; + list->iter_pos++; +} + +void list_iter_insert_after(list_t *list, void *data) { + if(list->size == 0) { + list_add_first(list, data); + return; + } + + list_node_t *node = list_node_init(); + node->data = data; + node->next = list->iterator->next; + list->iterator->next = node; + + if(node->next == NULL) + list->back = node; + + list->size++; +} + +int list_iter_pos(list_t *list) { + return list->iter_pos; +} + +void list_free(list_t *list) { + list_node_t *ptr = list->front, *next; + while(ptr != NULL) { + next = ptr->next; + free(ptr); + ptr = next; + } +} + +void list_nodes_free(list_t *list) { + list_node_t *ptr = list->front, *next; + while(ptr != NULL) { + next = ptr->next; + free(ptr->data); + free(ptr); + ptr = next; + } +} + +void list_nodes_func_free(list_t *list, listfreefptr ffptr) { + list_node_t *ptr = list->front, *next; + while(ptr != NULL) { + next = ptr->next; + (*ffptr)(ptr->data); + free(ptr); + next = ptr; + } +} + diff --git a/src/list.h b/src/list.h new file mode 100644 index 0000000..cfbe10c --- /dev/null +++ b/src/list.h @@ -0,0 +1,53 @@ +#ifndef LIST_H +#define LIST_H + +#include +#include "common.h" + +typedef void(*listfreefptr)(void*); +typedef BOOL(*listfindfptr)(void*); + +typedef struct list_node_t list_node_t; +struct list_node_t { + list_node_t *next; + void *data; +}; + +typedef struct list_t list_t; +struct list_t { + list_node_t *front; + list_node_t *back; + int size; + + list_node_t *iterator; + list_node_t *iter_prev; + int iter_pos; +}; + +list_t* list_init(); + +void list_append(list_t*, void*); +void list_prepend(list_t*, void*); +void list_insert(list_t*, void*, int); + +int list_size(list_t*); +void* list_get(list_t*, int); +int list_find(list_t*, void*); +int list_func_find(list_t*, listfindfptr); + +void* list_remove(list_t*, int); +void* list_remove_front(list_t*); +void* list_remove_back(list_t*); + +void list_iter_reset(list_t*); +void* list_iter_next(list_t*); +void* list_iter_remove(list_t*); +void list_iter_insert_before(list_t*, void*); +void list_iter_insert_after(list_t*, void*); +int list_iter_pos(list_t*); + +void list_free(list_t*); +void list_nodes_free(list_t*); +void list_nodes_func_free(list_t*, listfreefptr); + +#endif diff --git a/src/main.c b/src/main.c index 907073e..1cb7e48 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,8 @@ #include "client/client.h" #include "server/server.h" +#include "stack.h" + +#include int main(int argc, char **argv) { if(argc == 2 && strcmp(argv[1], "server") == 0) { diff --git a/src/stack.c b/src/stack.c new file mode 100644 index 0000000..e6d8e96 --- /dev/null +++ b/src/stack.c @@ -0,0 +1,65 @@ +#include "stack.h" + +stack_t* stack_init() { + stack_t *stack = (stack_t*)malloc(sizeof(stack_t)); + stack->next = NULL; + stack->data = NULL; + + return stack; +} + +void stack_push(stack_t *stack, void *data) { + stack_t *node = stack_init(); + node->data = data; + node->next = stack->next; + stack->next = node; +} + +void* stack_pop(stack_t *stack) { + if(stack_can_pop(stack) == FALSE) + return NULL; + + stack_t *this = stack->next; + void *data = this->data; + stack->next = this->next; + free(this); + + return data; +} + +BOOL stack_can_pop(stack_t *stack) { + return stack->next != NULL; +} + +void stack_free(stack_t *stack) { + stack_t *ptr = stack, *next; + while(ptr != NULL) { + next = ptr->next; + free(ptr); + ptr = next; + } +} + +void stack_node_free(stack_t *stack) { + stack_t *ptr = stack, *next; + while(ptr != NULL) { + next = ptr->next; + if(ptr->data != NULL) + free(ptr->data); + + free(ptr); + ptr = next; + } +} + +void stack_node_func_free(stack_t *stack, stackffptr ffptr) { + stack_t *ptr = stack, *next; + while(ptr != NULL) { + next = ptr->next; + if(ptr->data != NULL) + (*ffptr)(ptr->data); + + free(ptr); + ptr = next; + } +} diff --git a/src/stack.h b/src/stack.h index af4c5da..bdafec5 100644 --- a/src/stack.h +++ b/src/stack.h @@ -1,13 +1,25 @@ #ifndef STACK_H #define STACK_H -struct stack_node_t { +#include +#include "common.h" -}; +typedef void(*stackffptr)(void*); typedef struct stack_t stack_t; struct stack_t { - + stack_t *next; + void *data; }; +stack_t* stack_init(); + +void stack_push(stack_t*, void*); +void* stack_pop(stack_t*); +BOOL stack_can_pop(stack_t*); + +void stack_free(stack_t*); +void stack_node_free(stack_t*); +void stack_node_func_free(stack_t*, stackffptr); + #endif