00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "list.h"
00026 #include "util.h"
00027 #include "debug.h"
00028
00029 errorcode list_init(list_t *list) {
00030
00031 if (list==NULL)
00032 return ERROR_NULL_ARG_1;
00033
00034 list->head=NULL;
00035 list->last_get = list->head;
00036 list->last_get_num = 0;
00037 list->size = 0;
00038
00039 return SUCCESS;
00040 }
00041
00042 errorcode list_destroy(list_t *list, void (*func)(void*,void*), void *arg) {
00043
00044 list_node_t *node;
00045
00046 if (list==NULL)
00047 return ERROR_NULL_ARG_1;
00048
00049 while (list->head != NULL) {
00050
00051 node = list->head;
00052
00053 list->head = node->next;
00054
00055 if (func!=NULL)
00056 func(node->item,arg);
00057
00058 safe_free(node);
00059 }
00060
00061 return SUCCESS;
00062 }
00063
00064 errorcode list_find(list_t *list, int (*func)(void*,void*), void *arg,
00065 void**found_item) {
00066
00067 list_node_t *node;
00068
00069 if (list==NULL)
00070 return ERROR_NULL_ARG_1;
00071 if (func==NULL)
00072 return ERROR_NULL_ARG_2;
00073 if (found_item==NULL)
00074 return ERROR_NULL_ARG_4;
00075
00076 node = list->head;
00077
00078 while(node!=NULL) {
00079 switch (func(node->item,arg)) {
00080 case LIST_FATAL :
00081 return ERROR_FUNC_POINTER_FUNC_FAILED;
00082 case LIST_NOT_FOUND :
00083 node = node->next;
00084 break;
00085 case LIST_FOUND :
00086 *found_item = node->item;
00087 return SUCCESS;
00088 default :
00089 return ERROR_FUNC_POINTER_FUNC_INVALID;
00090 }
00091 }
00092
00093 return ERROR_NOT_FOUND;
00094 }
00095
00096 errorcode list_get(list_t *list, int index, void **item) {
00097
00098 list_node_t *node, *start;
00099 int num;
00100
00101 if (list==NULL)
00102 return ERROR_NULL_ARG_1;
00103 if (index<0)
00104 return ERROR_NEG_ARG_2;
00105 if (item==NULL)
00106 return ERROR_NULL_ARG_3;
00107 if (index>list->size)
00108 return ERROR_ARG_2;
00109
00110 start = node = list->last_get;
00111 num = list->last_get_num;
00112
00113 do {
00114
00115 if (node==NULL) {
00116 node = list->head;
00117 num = 0;
00118 continue;
00119 }
00120
00121 if (num==index) {
00122 *item = node->item;
00123 list->last_get = node;
00124 list->last_get_num = index;
00125 return SUCCESS;
00126 }
00127 node = node->next;
00128 num++;
00129 } while (start!=node);
00130
00131 return ERROR_NOT_FOUND;
00132 }
00133
00134 errorcode list_add(list_t *list, void *item) {
00135
00136 list_node_t *node;
00137
00138 if (list==NULL)
00139 return ERROR_NULL_ARG_1;
00140
00141 if ( (node=(list_node_t*)malloc(sizeof(list_node_t))) == NULL)
00142 return ERROR_MALLOC_FAILED;
00143
00144
00145 node->item = item;
00146
00147 node->next = list->head;
00148
00149 list->head = node;
00150
00151
00152 list->last_get = list->head;
00153 list->last_get_num = 0;
00154
00155 list->size += 1;
00156
00157 return SUCCESS;
00158 }
00159
00160 errorcode list_remove(list_t *list, int (*func)(void*,void*), void *arg){
00161
00162 list_node_t *node;
00163 list_node_t *prev_node;
00164
00165 if (list==NULL)
00166 return ERROR_NULL_ARG_1;
00167 if (func==NULL)
00168 return ERROR_NULL_ARG_2;
00169
00170 prev_node = NULL;
00171 node = list->head;
00172
00173 while(node!=NULL) {
00174 switch (func(node->item,arg)) {
00175
00176 case LIST_FATAL :
00177 return ERROR_FUNC_POINTER_FUNC_FAILED;
00178
00179 case LIST_NOT_FOUND :
00180 prev_node = node;
00181 node = node->next;
00182 break;
00183
00184 case LIST_FOUND :
00185
00186 if (prev_node==NULL)
00187 list->head = node->next;
00188
00189
00190 else
00191 prev_node->next = node->next;
00192
00193 safe_free(node);
00194
00195 list->last_get = list->head;
00196 list->last_get_num = 0;
00197
00198 list->size -= 1;
00199 return SUCCESS;
00200 default :
00201 return ERROR_FUNC_POINTER_FUNC_INVALID;
00202 }
00203 }
00204
00205 return ERROR_NOT_FOUND;
00206 }
00207
00208 int list_count(list_t *list) {
00209
00210 if (list==NULL)
00211 return -1;
00212 return list->size;
00213 }
00214