Added addtional functions ...

- added list_append
- added list_prepend
This commit is contained in:
Walter Julius Hennecke 2013-08-28 19:15:43 +02:00
parent 86fb3565c4
commit f86e6cef43
4 changed files with 41 additions and 6 deletions

View file

@ -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; extern target_alert_Shaders_s alertShaders;

View file

@ -968,7 +968,7 @@ static void G_LoadTimedMessages(void) {
} }
msg->message = strdup(token); 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 { } else {
if(token[0] == '}') { if(token[0] == '}') {
break; break;

View file

@ -58,13 +58,14 @@ list_iter_p list_iterator(list_p list, char init) {
return iter; 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)); lnode_p node = (lnode_p)malloc(sizeof(struct linked_node));
node->cont = (container_p)(sizeof(container)); node->cont = (container_p)(sizeof(container));
if(node->cont == NULL) { if(node->cont == NULL) {
return 0; return 0;
} }
node->cont->type = type;
node->cont->data = malloc(size); node->cont->data = malloc(size);
if(node->cont->data == NULL) { 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; node->next = NULL;
list->first = node; list->first = node;
list->last = 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; list->last->next = node;
node->prev = list->last; node->prev = list->last;
node->next = NULL; node->next = NULL;
@ -88,6 +99,14 @@ int list_add(list_p list, void* data, dataType_t type, int size) {
return list->length; 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){ container_p list_current(list_iter_p iter){
if(iter->started && iter->current != NULL) { if(iter->started && iter->current != NULL) {
return iter->current->cont; return iter->current->cont;

View file

@ -93,12 +93,28 @@ list_p create_list(void);
list_iter_p list_iterator(list_p list, char init); 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 * The data is copied by value, so the original pointer must be freed if it
* was allocated on the heap. * was allocated on the heap.
* Returns the length of the list if succesfull else returns 0. * 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 * Gets the data stored in the first item of the list or NULL if the list is empty