Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ****************************************************************************** |
michael@0 | 3 | * Copyright (C) 2009-2012, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ****************************************************************************** |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "ulist.h" |
michael@0 | 9 | #include "cmemory.h" |
michael@0 | 10 | #include "cstring.h" |
michael@0 | 11 | #include "uenumimp.h" |
michael@0 | 12 | |
michael@0 | 13 | typedef struct UListNode UListNode; |
michael@0 | 14 | struct UListNode { |
michael@0 | 15 | void *data; |
michael@0 | 16 | |
michael@0 | 17 | UListNode *next; |
michael@0 | 18 | UListNode *previous; |
michael@0 | 19 | |
michael@0 | 20 | /* When data is created with uprv_malloc, needs to be freed during deleteList function. */ |
michael@0 | 21 | UBool forceDelete; |
michael@0 | 22 | }; |
michael@0 | 23 | |
michael@0 | 24 | struct UList { |
michael@0 | 25 | UListNode *curr; |
michael@0 | 26 | UListNode *head; |
michael@0 | 27 | UListNode *tail; |
michael@0 | 28 | |
michael@0 | 29 | int32_t size; |
michael@0 | 30 | int32_t currentIndex; |
michael@0 | 31 | }; |
michael@0 | 32 | |
michael@0 | 33 | static void ulist_addFirstItem(UList *list, UListNode *newItem); |
michael@0 | 34 | |
michael@0 | 35 | U_CAPI UList *U_EXPORT2 ulist_createEmptyList(UErrorCode *status) { |
michael@0 | 36 | UList *newList = NULL; |
michael@0 | 37 | |
michael@0 | 38 | if (U_FAILURE(*status)) { |
michael@0 | 39 | return NULL; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | newList = (UList *)uprv_malloc(sizeof(UList)); |
michael@0 | 43 | if (newList == NULL) { |
michael@0 | 44 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 45 | return NULL; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | newList->curr = NULL; |
michael@0 | 49 | newList->head = NULL; |
michael@0 | 50 | newList->tail = NULL; |
michael@0 | 51 | newList->size = 0; |
michael@0 | 52 | newList->currentIndex = -1; |
michael@0 | 53 | |
michael@0 | 54 | return newList; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | /* |
michael@0 | 58 | * Function called by addItemEndList or addItemBeginList when the first item is added to the list. |
michael@0 | 59 | * This function properly sets the pointers for the first item added. |
michael@0 | 60 | */ |
michael@0 | 61 | static void ulist_addFirstItem(UList *list, UListNode *newItem) { |
michael@0 | 62 | newItem->next = NULL; |
michael@0 | 63 | newItem->previous = NULL; |
michael@0 | 64 | list->head = newItem; |
michael@0 | 65 | list->tail = newItem; |
michael@0 | 66 | list->currentIndex = 0; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | U_CAPI void U_EXPORT2 ulist_addItemEndList(UList *list, const void *data, UBool forceDelete, UErrorCode *status) { |
michael@0 | 70 | UListNode *newItem = NULL; |
michael@0 | 71 | |
michael@0 | 72 | if (U_FAILURE(*status) || list == NULL || data == NULL) { |
michael@0 | 73 | return; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | newItem = (UListNode *)uprv_malloc(sizeof(UListNode)); |
michael@0 | 77 | if (newItem == NULL) { |
michael@0 | 78 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 79 | return; |
michael@0 | 80 | } |
michael@0 | 81 | newItem->data = (void *)(data); |
michael@0 | 82 | newItem->forceDelete = forceDelete; |
michael@0 | 83 | |
michael@0 | 84 | if (list->size == 0) { |
michael@0 | 85 | ulist_addFirstItem(list, newItem); |
michael@0 | 86 | } else { |
michael@0 | 87 | newItem->next = NULL; |
michael@0 | 88 | newItem->previous = list->tail; |
michael@0 | 89 | list->tail->next = newItem; |
michael@0 | 90 | list->tail = newItem; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | list->size++; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | U_CAPI void U_EXPORT2 ulist_addItemBeginList(UList *list, const void *data, UBool forceDelete, UErrorCode *status) { |
michael@0 | 97 | UListNode *newItem = NULL; |
michael@0 | 98 | |
michael@0 | 99 | if (U_FAILURE(*status) || list == NULL || data == NULL) { |
michael@0 | 100 | return; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | newItem = (UListNode *)uprv_malloc(sizeof(UListNode)); |
michael@0 | 104 | if (newItem == NULL) { |
michael@0 | 105 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 106 | return; |
michael@0 | 107 | } |
michael@0 | 108 | newItem->data = (void *)(data); |
michael@0 | 109 | newItem->forceDelete = forceDelete; |
michael@0 | 110 | |
michael@0 | 111 | if (list->size == 0) { |
michael@0 | 112 | ulist_addFirstItem(list, newItem); |
michael@0 | 113 | } else { |
michael@0 | 114 | newItem->previous = NULL; |
michael@0 | 115 | newItem->next = list->head; |
michael@0 | 116 | list->head->previous = newItem; |
michael@0 | 117 | list->head = newItem; |
michael@0 | 118 | list->currentIndex++; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | list->size++; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | U_CAPI UBool U_EXPORT2 ulist_containsString(const UList *list, const char *data, int32_t length) { |
michael@0 | 125 | UBool result = FALSE; |
michael@0 | 126 | const UListNode *pointer = NULL; |
michael@0 | 127 | |
michael@0 | 128 | if (list != NULL && list->size != 0) { |
michael@0 | 129 | pointer = list->head; |
michael@0 | 130 | |
michael@0 | 131 | while (pointer != NULL) { |
michael@0 | 132 | if (length == uprv_strlen(pointer->data)) { |
michael@0 | 133 | if (uprv_memcmp(data, pointer->data, length) == 0) { |
michael@0 | 134 | result = TRUE; |
michael@0 | 135 | break; |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | pointer = pointer->next; |
michael@0 | 140 | } |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | return result; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | U_CAPI void *U_EXPORT2 ulist_getNext(UList *list) { |
michael@0 | 147 | UListNode *curr = NULL; |
michael@0 | 148 | |
michael@0 | 149 | if (list == NULL || list->curr == NULL) { |
michael@0 | 150 | return NULL; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | curr = list->curr; |
michael@0 | 154 | list->curr = curr->next; |
michael@0 | 155 | list->currentIndex++; |
michael@0 | 156 | |
michael@0 | 157 | return curr->data; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | U_CAPI int32_t U_EXPORT2 ulist_getListSize(const UList *list) { |
michael@0 | 161 | if (list != NULL) { |
michael@0 | 162 | return list->size; |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | return -1; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | U_CAPI void U_EXPORT2 ulist_resetList(UList *list) { |
michael@0 | 169 | if (list != NULL) { |
michael@0 | 170 | list->curr = list->head; |
michael@0 | 171 | list->currentIndex = 0; |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | U_CAPI void U_EXPORT2 ulist_deleteList(UList *list) { |
michael@0 | 176 | UListNode *listHead = NULL; |
michael@0 | 177 | UListNode *listPointer = NULL; |
michael@0 | 178 | |
michael@0 | 179 | if (list != NULL) { |
michael@0 | 180 | listHead = list->head; |
michael@0 | 181 | listPointer = listHead; |
michael@0 | 182 | while (listHead != NULL) { |
michael@0 | 183 | listPointer = listHead->next; |
michael@0 | 184 | |
michael@0 | 185 | if (listHead->forceDelete) { |
michael@0 | 186 | uprv_free(listHead->data); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | uprv_free(listHead); |
michael@0 | 190 | listHead = listPointer; |
michael@0 | 191 | } |
michael@0 | 192 | uprv_free(list); |
michael@0 | 193 | list = NULL; |
michael@0 | 194 | } |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | U_CAPI void U_EXPORT2 ulist_close_keyword_values_iterator(UEnumeration *en) { |
michael@0 | 198 | if (en != NULL) { |
michael@0 | 199 | ulist_deleteList((UList *)(en->context)); |
michael@0 | 200 | uprv_free(en); |
michael@0 | 201 | } |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | U_CAPI int32_t U_EXPORT2 ulist_count_keyword_values(UEnumeration *en, UErrorCode *status) { |
michael@0 | 205 | if (U_FAILURE(*status)) { |
michael@0 | 206 | return -1; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | return ulist_getListSize((UList *)(en->context)); |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | U_CAPI const char * U_EXPORT2 ulist_next_keyword_value(UEnumeration *en, int32_t *resultLength, UErrorCode *status) { |
michael@0 | 213 | const char *s; |
michael@0 | 214 | if (U_FAILURE(*status)) { |
michael@0 | 215 | return NULL; |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | s = (const char *)ulist_getNext((UList *)(en->context)); |
michael@0 | 219 | if (s != NULL && resultLength != NULL) { |
michael@0 | 220 | *resultLength = uprv_strlen(s); |
michael@0 | 221 | } |
michael@0 | 222 | return s; |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | U_CAPI void U_EXPORT2 ulist_reset_keyword_values_iterator(UEnumeration *en, UErrorCode *status) { |
michael@0 | 226 | if (U_FAILURE(*status)) { |
michael@0 | 227 | return ; |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | ulist_resetList((UList *)(en->context)); |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | U_CAPI UList * U_EXPORT2 ulist_getListFromEnum(UEnumeration *en) { |
michael@0 | 234 | return (UList *)(en->context); |
michael@0 | 235 | } |
michael@0 | 236 |