list.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "list.h"
00009 #include "mem.h"
00010 #include <assert.h>
00011 #include <string.h>
00012
00013 extern struct memPool_s *com_genericPool;
00014
00022 linkedList_t* LIST_Add (linkedList_t** listDest, const byte* data, size_t length)
00023 {
00024 linkedList_t *newEntry;
00025 linkedList_t *list;
00026
00027 assert(listDest);
00028 assert(data);
00029
00030
00031 if (!*listDest) {
00032 *listDest = (linkedList_t*)Mem_PoolAlloc(sizeof(**listDest), com_genericPool, 0);
00033 (*listDest)->data = (byte*)Mem_PoolAlloc(length, com_genericPool, 0);
00034 memcpy(((*listDest)->data), data, length);
00035 (*listDest)->next = NULL;
00036 return *listDest;
00037 } else
00038 list = *listDest;
00039
00040 while (list->next)
00041 list = list->next;
00042
00043 newEntry = (linkedList_t*)Mem_PoolAlloc(sizeof(*newEntry), com_genericPool, 0);
00044 list->next = newEntry;
00045 newEntry->data = (byte*)Mem_PoolAlloc(length, com_genericPool, 0);
00046 memcpy(newEntry->data, data, length);
00047 newEntry->next = NULL;
00048
00049 return newEntry;
00050 }
00051
00058 const linkedList_t* LIST_ContainsString (const linkedList_t* list, const char* string)
00059 {
00060 assert(list);
00061
00062 while ((string != NULL) && (list != NULL)) {
00063 if (!strcmp((const char*)list->data, string))
00064 return list;
00065 list = list->next;
00066 }
00067
00068 return NULL;
00069 }
00070
00071
00077 linkedList_t* LIST_GetPointer (linkedList_t* list, const void* data)
00078 {
00079 assert(list);
00080
00081 while ((data != NULL) && (list != NULL)) {
00082 if (list->data == data)
00083 return list;
00084 list = list->next;
00085 }
00086
00087 return NULL;
00088 }
00089
00096 void LIST_AddString (linkedList_t** listDest, const char* data)
00097 {
00098 linkedList_t *newEntry;
00099 linkedList_t *list;
00100
00101 assert(listDest);
00102 assert(data);
00103
00104
00105 if (!*listDest) {
00106 *listDest = (linkedList_t*)Mem_PoolAlloc(sizeof(**listDest), com_genericPool, 0);
00107 (*listDest)->data = (byte*)Mem_StrDup(data);
00108 (*listDest)->next = NULL;
00109 return;
00110 } else
00111 list = *listDest;
00112
00113 while (list->next)
00114 list = list->next;
00115
00116 newEntry = (linkedList_t*)Mem_PoolAlloc(sizeof(*newEntry), com_genericPool, 0);
00117 list->next = newEntry;
00118 newEntry->data = (byte*)Mem_StrDup(data);
00119 newEntry->next = NULL;
00120 }
00121
00128 void LIST_AddPointer (linkedList_t** listDest, void* data)
00129 {
00130 linkedList_t *newEntry;
00131 linkedList_t *list;
00132
00133 assert(listDest);
00134 assert(data);
00135
00136
00137 if (!*listDest) {
00138 *listDest = (linkedList_t*)Mem_PoolAlloc(sizeof(**listDest), com_genericPool, 0);
00139 (*listDest)->data = (byte *)data;
00140 (*listDest)->ptr = qtrue;
00141 (*listDest)->next = NULL;
00142 return;
00143 } else
00144 list = *listDest;
00145
00146 while (list->next)
00147 list = list->next;
00148
00149 newEntry = (linkedList_t*)Mem_PoolAlloc(sizeof(*newEntry), com_genericPool, 0);
00150 list->next = newEntry;
00151 newEntry->data = (byte *)data;
00152 newEntry->ptr = qtrue;
00153 newEntry->next = NULL;
00154 }
00155
00163 void LIST_RemoveEntry (linkedList_t **list, linkedList_t *entry)
00164 {
00165 linkedList_t* prev, *listPos;
00166
00167 assert(list);
00168 assert(entry);
00169
00170 prev = *list;
00171 listPos = *list;
00172
00173
00174 if (*list == entry) {
00175 if (!(*list)->ptr)
00176 Mem_Free((*list)->data);
00177 listPos = (*list)->next;
00178 Mem_Free(*list);
00179 *list = listPos;
00180 } else {
00181 while (listPos) {
00182 if (listPos == entry) {
00183 prev->next = listPos->next;
00184 if (!listPos->ptr)
00185 Mem_Free(listPos->data);
00186 Mem_Free(listPos);
00187 break;
00188 }
00189 prev = listPos;
00190 listPos = listPos->next;
00191 }
00192 }
00193 }
00194
00199 void LIST_Delete (linkedList_t **list)
00200 {
00201 linkedList_t *next;
00202 linkedList_t *l = *list;
00203
00204 while (l) {
00205 next = l->next;
00206 if (!l->ptr)
00207 Mem_Free(l->data);
00208 Mem_Free(l);
00209 l = next;
00210 }
00211 *list = NULL;
00212 }
00213
00219 void *LIST_GetNext (linkedList_t *list, void *lastData)
00220 {
00221 linkedList_t *entry;
00222
00223 if (LIST_IsEmpty(list))
00224 return NULL;
00225
00226 if (lastData == NULL)
00227 return list->data;
00228
00229 entry = LIST_GetPointer(list, lastData);
00230 assert(entry);
00231
00232 if (entry->next)
00233 return entry->next->data;
00234
00235 return NULL;
00236 }
00237
00242 qboolean LIST_Remove (linkedList_t **list, const void *data)
00243 {
00244 linkedList_t *l = LIST_GetPointer(*list, data);
00245 if (l != NULL)
00246 LIST_RemoveEntry(list, l);
00247 return l != NULL;
00248 }
00249
00255 qboolean LIST_IsEmpty (const linkedList_t *list)
00256 {
00257 return list == NULL;
00258 }
00259
00264 int LIST_Count (const linkedList_t *list)
00265 {
00266 const linkedList_t *l = list;
00267 int count = 0;
00268
00269 while (l) {
00270 count++;
00271 l = l->next;
00272 }
00273 return count;
00274 }
00275
00280 void *LIST_GetLast (linkedList_t *list)
00281 {
00282 while (list) {
00283 linkedList_t *next = list->next;
00284 if (next == NULL)
00285 return (void *)list->data;
00286 list = next;
00287 }
00288
00289 return NULL;
00290 }
00291
00298 void *LIST_GetByIdx (linkedList_t *list, int index)
00299 {
00300 int i;
00301 const int count = LIST_Count(list);
00302
00303 if (!count || !list)
00304 return NULL;
00305
00306 if (index >= count || index < 0)
00307 return NULL;
00308
00309 i = 0;
00310 while (list) {
00311 if (i == index)
00312 return (void *)list->data;
00313 i++;
00314 list = list->next;
00315 }
00316
00317 return NULL;
00318 }