Merge remote-tracking branch 'origin/list' into locations

This commit is contained in:
Walter Julius Hennecke 2013-04-09 22:43:20 +02:00
commit b334114ae2
4 changed files with 125 additions and 19 deletions

View file

@ -7458,7 +7458,7 @@ void addShaderToList(list_p list, char *shader) {
}
}
list_append(list, s, LT_DATA, strlen(s)+1);
list_append(list, s, LT_STRING, strlen(s)+1);
}
extern target_alert_Shaders_s alertShaders;

View file

@ -960,7 +960,7 @@ static void G_LoadTimedMessages(void) {
continue;
}
list_append(level.timedMessages, token, LT_DATA, strlen(token)+1);
list_append(level.timedMessages, token, LT_STRING, strlen(token)+1);
} else {
if(token[0] == '}') {
break;

View file

@ -65,13 +65,15 @@ int list_add(list_p list, void* data, dataType_t type, size_t size, char end) {
if(node->cont == NULL) {
return 0;
}
node->cont->type = type;
node->cont->type = type;
node->cont->data = malloc(size);
if(node->cont->data == NULL) {
return 0;
}
memcpy(node->cont->data, data, size);
node->cont->pointer = 0;
node->cont->size = size;
if(list->first == NULL) {
node->prev = NULL;
@ -107,6 +109,53 @@ int list_prepend(list_p list, void* data, dataType_t type, size_t size) {
return list_add(list, data, type, size, LIST_FRONT);
}
int list_add_ptr(list_p list, void* data, dataType_t type, char end) {
lnode_p node = (lnode_p)malloc(sizeof(struct linked_node));
node->cont = (container_p)malloc(sizeof(container));
if(node->cont == NULL) {
return 0;
}
node->cont->type = type;
node->cont->data = data;
node->cont->pointer = 1;
node->cont->size = 0;
if(list->first == NULL) {
node->prev = NULL;
node->next = NULL;
list->first = node;
list->last = node;
} 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;
list->last = node;
}
list->length++;
return list->length;
}
int list_append_ptr(list_p list, void* data, dataType_t type) {
return list_add_ptr(list, data, type, LIST_BACK);
}
int list_prepend_ptr(list_p list, void* data, dataType_t type) {
return list_add_ptr(list, data, type, LIST_FRONT);
}
container_p list_current(list_iter_p iter){
if(iter->started && iter->current != NULL) {
return iter->current->cont;
@ -244,7 +293,7 @@ void list_remove(list_p list, char end) {
}
if(cont != NULL) {
if(cont->data != NULL) {
if(cont->pointer > 0 && cont->data != NULL) {
destructor(cont->data);
}
@ -259,7 +308,9 @@ void destroy_list(list_p list) {
while(cur!=NULL){
next = cur->next;
if(list->destructor != NULL) { // only destroy data if there is a destructor set
list->destructor(cur->cont->data);
if(cur->cont->pointer > 0 && cur->cont->data != NULL) {
list->destructor(cur->cont->data);
}
free(cur->cont);
}
free(cur);

View file

@ -30,6 +30,10 @@ Can be used as a LIFO stack of FIFO queue. */
#define LIST_FRONT 0
#define LIST_BACK 1
/**
* Possible type the data in a container may have.
* LT_DATA means that a custom struct was added.
*/
typedef enum {
LT_BOOLEAN,
LT_CHAR,
@ -46,37 +50,64 @@ typedef enum {
LT_MAX
} dataType_t;
/**
* Container for data added to the list.
* Use of a container allows to add standard c types to the list
* without the need to embed them into a struct.
*/
struct container {
void* data;
size_t size;
dataType_t type;
void* data; /*!< pointer to the data */
size_t size; /*!< size of the data */
dataType_t type; /*!< type of the data */
char pointer; /*!< determin if the data is a pointer */
} container;
/**
* Type for a pointer to a container.
*/
typedef struct container* container_p;
/**
* Node for a double linked list.
*/
struct linked_node {
container_p cont;
struct linked_node* next;
struct linked_node* prev;
container_p cont; /*!< cointainer with the data */
struct linked_node* next; /*!< next list element */
struct linked_node* prev; /*!< previous list element */
};
/**
* Type for a pointer to a node.
*/
typedef struct linked_node* lnode_p;
/**
* Struct describing a list.
*/
struct list{
int length;
lnode_p first;
lnode_p last;
void (*destructor)(void*);
int length; /*!< count of elements in the list */
lnode_p first; /*!< first element of the list */
lnode_p last; /*!< last element of the list */
void (*destructor)(void*); /*!< pointer to destructor for data. Default is free. */
};
/**
* Type for a pointer to a list.
*/
typedef struct list * list_p;
/**
* Struct describing a list iterator.
*/
struct list_iter{
list_p list;
lnode_p current;
char started;
list_p list; /*!< the list */
lnode_p current; /*!< current node */
char started; /*!< has iteration started */
};
/**
* Type for a pointer to a list iterator.
*/
typedef struct list_iter * list_iter_p;
/**
@ -93,7 +124,31 @@ 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 of the list.
* Add a pointer to an item with the given value and type to 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_ptr(list_p list, void* data, dataType_t type, char end);
/**
* Add a pointer to 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_ptr(list_p list, void* data, dataType_t type);
/**
* Add a pointer to 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_ptr(list_p list, void* data, dataType_t type);
/**
* Add an item with the given value, type, and size to 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.