From f86e6cef4334b0cc9fb308973679f8529528f6c8 Mon Sep 17 00:00:00 2001 From: Walter Julius Hennecke Date: Wed, 28 Aug 2013 19:15:43 +0200 Subject: [PATCH] Added addtional functions ... - added list_append - added list_prepend --- code/game/g_cmds.c | 2 +- code/game/g_main.c | 2 +- code/game/list.c | 23 +++++++++++++++++++++-- code/game/list.h | 20 ++++++++++++++++++-- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/code/game/g_cmds.c b/code/game/g_cmds.c index beedfa5..f113ca3 100644 --- a/code/game/g_cmds.c +++ b/code/game/g_cmds.c @@ -7463,7 +7463,7 @@ void addShaderToList(list_p list, char *shader) { } } - list_add(list, s, LT_DATA, sizeof(rShader_s)); + list_append(list, s, LT_DATA, sizeof(rShader_s)); } extern target_alert_Shaders_s alertShaders; diff --git a/code/game/g_main.c b/code/game/g_main.c index 5008fc3..de44fb1 100644 --- a/code/game/g_main.c +++ b/code/game/g_main.c @@ -968,7 +968,7 @@ static void G_LoadTimedMessages(void) { } msg->message = strdup(token); - list_add(level.timedMessages, msg, LT_DATA, sizeof(timedMessage_s)); + list_append(level.timedMessages, msg, LT_DATA, sizeof(timedMessage_s)); } else { if(token[0] == '}') { break; diff --git a/code/game/list.c b/code/game/list.c index 0b12dd2..55a1563 100644 --- a/code/game/list.c +++ b/code/game/list.c @@ -58,13 +58,14 @@ list_iter_p list_iterator(list_p list, char init) { return iter; } -int list_add(list_p list, void* data, dataType_t type, int size) { +int list_add(list_p list, void* data, dataType_t type, size_t size, char end) { lnode_p node = (lnode_p)malloc(sizeof(struct linked_node)); node->cont = (container_p)(sizeof(container)); if(node->cont == NULL) { return 0; } + node->cont->type = type; node->cont->data = malloc(size); if(node->cont->data == NULL) { @@ -77,7 +78,17 @@ int list_add(list_p list, void* data, dataType_t type, int size) { node->next = NULL; list->first = node; list->last = node; - } else { + } else if(end == LIST_BACK) { + list->last->next = node; + node->prev = list->last; + node->next = NULL; + list->last = node; + } else if(end == LIST_FRONT) { + list->first->prev = node; + node->next = list->first; + node->prev = NULL; + list->first = node; + } else { // assume back list->last->next = node; node->prev = list->last; node->next = NULL; @@ -88,6 +99,14 @@ int list_add(list_p list, void* data, dataType_t type, int size) { return list->length; } +int list_append(list_p list, void* data, dataType_t type, size_t size) { + return list_add(list, data, type, size, LIST_BACK); +} + +int list_prepend(list_p list, void* data, dataType_t type, size_t size) { + return list_add(list, data, type, size, LIST_FRONT); +} + container_p list_current(list_iter_p iter){ if(iter->started && iter->current != NULL) { return iter->current->cont; diff --git a/code/game/list.h b/code/game/list.h index 54884a2..6d28be3 100644 --- a/code/game/list.h +++ b/code/game/list.h @@ -93,12 +93,28 @@ list_p create_list(void); list_iter_p list_iterator(list_p list, char init); /** - * Add an item with the given value, type, and size to the back of the list. + * Add an item with the given value, type, and size of the list. * The data is copied by value, so the original pointer must be freed if it * was allocated on the heap. * Returns the length of the list if succesfull else returns 0. */ -int list_add(list_p list, void* data, dataType_t type, int size); +int list_add(list_p list, void* data, dataType_t type, size_t size, char end); + +/** + * Add an item with the given calue, type, and size to the end of the list. + * The data is copied by value, so the original pointer must be freed if it + * was allocated on the heap. + * Returns the length of the list if successfull else returns 0. + */ +int list_append(list_p list, void* data, dataType_t type, size_t size); + +/** + * Add an item with the given calue, type, and size to the front of the list. + * The data is copied by value, so the original pointer must be freed if it + * was allocated on the heap. + * Returns the length of the list if successfull else returns 0. + */ +int list_prepend(list_p list, void* data, dataType_t type, size_t size); /** * Gets the data stored in the first item of the list or NULL if the list is empty