00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00034 #include "wbxml.h"
00035
00036
00038 typedef struct WBXMLListElt_s
00039 {
00040 void *item;
00041 struct WBXMLListElt_s *next;
00042 } WBXMLListElt;
00043
00045 struct WBXMLList_s
00046 {
00047 WBXMLListElt *head;
00048 WBXMLListElt *tail;
00049 WB_ULONG len;
00050 };
00051
00052
00053 static WBXMLListElt *wbxml_elt_create_real(void *item);
00054 #define wbxml_elt_create(a) wbxml_mem_cleam(wbxml_elt_create_real(a))
00055
00056 static void wbxml_elt_destroy(WBXMLListElt *elt, WBXMLListEltCleaner *destructor);
00057
00058
00059
00060
00061
00062
00063 WBXML_DECLARE(WBXMLList *) wbxml_list_create_real(void)
00064 {
00065 WBXMLList *list = NULL;
00066
00067 if ((list = (WBXMLList *) wbxml_malloc(sizeof(WBXMLList))) == NULL)
00068 return NULL;
00069
00070 list->head = NULL;
00071 list->tail = NULL;
00072 list->len = 0;
00073
00074 return list;
00075 }
00076
00077
00078 WBXML_DECLARE(void) wbxml_list_destroy(WBXMLList *list, WBXMLListEltCleaner *destructor)
00079 {
00080 WBXMLListElt *elt = NULL, *next = NULL;
00081
00082 if (list == NULL)
00083 return;
00084
00085 elt = list->head;
00086
00087 while (elt != NULL) {
00088 next = elt->next;
00089 wbxml_elt_destroy(elt, destructor);
00090 elt = next;
00091 }
00092
00093 wbxml_free(list);
00094 }
00095
00096
00097 WBXML_DECLARE(WB_ULONG) wbxml_list_len(WBXMLList *list)
00098 {
00099 if (list == NULL)
00100 return 0;
00101 else
00102 return list->len;
00103 }
00104
00105
00106 WBXML_DECLARE(WB_BOOL) wbxml_list_append(WBXMLList *list, void *item)
00107 {
00108 if (list == NULL)
00109 return FALSE;
00110
00111 if (list->head == NULL) {
00112
00113 if ((list->head = wbxml_elt_create(item)) == NULL)
00114 return FALSE;
00115
00116 list->tail = list->head;
00117 }
00118 else {
00119
00120 if ((list->tail->next = wbxml_elt_create(item)) == NULL)
00121 return FALSE;
00122
00123 list->tail = list->tail->next;
00124 }
00125
00126 list->len++;
00127
00128 return TRUE;
00129 }
00130
00131
00132 WBXML_DECLARE(WB_BOOL) wbxml_list_insert(WBXMLList *list, void *item, WB_ULONG pos)
00133 {
00134 WBXMLListElt *elt = NULL, *prev = NULL, *new_elt = NULL;
00135 WB_ULONG i = 0;
00136
00137 if (list == NULL)
00138 return FALSE;
00139
00140 if ((new_elt = wbxml_elt_create(item)) == NULL)
00141 return FALSE;
00142
00143
00144 if (list->len == 0) {
00145 list->head = new_elt;
00146 list->tail = list->head;
00147 }
00148 else {
00149
00150 if (pos == 0) {
00151
00152 new_elt->next = list->head;
00153 list->head = new_elt;
00154 }
00155 else {
00156
00157 if (pos >= list->len) {
00158 list->tail->next = new_elt;
00159 list->tail = list->tail->next;
00160 }
00161 else {
00162
00163 elt = list->head;
00164
00165 for (i=0; i<pos; i++) {
00166 prev = elt;
00167 elt = elt->next;
00168 }
00169
00170 prev->next = new_elt;
00171 new_elt->next = elt;
00172 }
00173 }
00174 }
00175
00176 list->len++;
00177
00178 return TRUE;
00179 }
00180
00181
00182 WBXML_DECLARE(void *) wbxml_list_get(WBXMLList *list, WB_ULONG index)
00183 {
00184 WBXMLListElt *elt = NULL;
00185 WB_ULONG i = 0;
00186
00187 if ((list == NULL) || (index >= wbxml_list_len(list)))
00188 return NULL;
00189
00190
00191 elt = list->head;
00192
00193 for (i=0; i<index; i++)
00194 elt = elt->next;
00195
00196 return elt->item;
00197 }
00198
00199
00200 WBXML_DECLARE(void *) wbxml_list_extract_first(WBXMLList *list)
00201 {
00202 WBXMLListElt *elt = NULL;
00203 void *result = NULL;
00204
00205 if ((list == NULL) || (wbxml_list_len(list) == 0))
00206 return NULL;
00207
00208 elt = list->head;
00209 result = elt->item;
00210
00211 if ((list->head = list->head->next) == NULL)
00212 list->tail = NULL;
00213
00214 wbxml_elt_destroy(elt, NULL);
00215
00216 list->len--;
00217
00218 return result;
00219 }
00220
00221
00222
00223
00224
00225
00232 static WBXMLListElt *wbxml_elt_create_real(void *item)
00233 {
00234 WBXMLListElt *elt = NULL;
00235
00236 if ((elt = (WBXMLListElt *) wbxml_malloc(sizeof(WBXMLListElt))) == NULL)
00237 return NULL;
00238
00239 elt->item = item;
00240 elt->next = NULL;
00241
00242 return elt;
00243 }
00244
00245
00251 static void wbxml_elt_destroy(WBXMLListElt *elt, WBXMLListEltCleaner *destructor)
00252 {
00253 if (elt == NULL)
00254 return;
00255
00256 if (destructor != NULL)
00257 destructor(elt->item);
00258
00259 wbxml_free(elt);
00260 }