1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
5 * Copyright (C) 2009-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
15 typedef struct UListNode UListNode;
22 /* When data is created with uprv_malloc, needs to be freed during deleteList function. */
35 static void ulist_addFirstItem(UList *list, UListNode *newItem);
37 U_CAPI UList *U_EXPORT2 ulist_createEmptyList(UErrorCode *status) {
38 UList *newList = NULL;
40 if (U_FAILURE(*status)) {
44 newList = (UList *)uprv_malloc(sizeof(UList));
45 if (newList == NULL) {
46 *status = U_MEMORY_ALLOCATION_ERROR;
54 newList->currentIndex = -1;
60 * Function called by addItemEndList or addItemBeginList when the first item is added to the list.
61 * This function properly sets the pointers for the first item added.
63 static void ulist_addFirstItem(UList *list, UListNode *newItem) {
65 newItem->previous = NULL;
70 static void ulist_removeItem(UList *list, UListNode *p) {
71 if (p->previous == NULL) {
72 // p is the list head.
75 p->previous->next = p->next;
77 if (p->next == NULL) {
78 // p is the list tail.
79 list->tail = p->previous;
81 p->next->previous = p->previous;
84 list->currentIndex = 0;
92 U_CAPI void U_EXPORT2 ulist_addItemEndList(UList *list, const void *data, UBool forceDelete, UErrorCode *status) {
93 UListNode *newItem = NULL;
95 if (U_FAILURE(*status) || list == NULL || data == NULL) {
97 uprv_free((void *)data);
102 newItem = (UListNode *)uprv_malloc(sizeof(UListNode));
103 if (newItem == NULL) {
105 uprv_free((void *)data);
107 *status = U_MEMORY_ALLOCATION_ERROR;
110 newItem->data = (void *)(data);
111 newItem->forceDelete = forceDelete;
113 if (list->size == 0) {
114 ulist_addFirstItem(list, newItem);
116 newItem->next = NULL;
117 newItem->previous = list->tail;
118 list->tail->next = newItem;
119 list->tail = newItem;
125 U_CAPI void U_EXPORT2 ulist_addItemBeginList(UList *list, const void *data, UBool forceDelete, UErrorCode *status) {
126 UListNode *newItem = NULL;
128 if (U_FAILURE(*status) || list == NULL || data == NULL) {
130 uprv_free((void *)data);
135 newItem = (UListNode *)uprv_malloc(sizeof(UListNode));
136 if (newItem == NULL) {
138 uprv_free((void *)data);
140 *status = U_MEMORY_ALLOCATION_ERROR;
143 newItem->data = (void *)(data);
144 newItem->forceDelete = forceDelete;
146 if (list->size == 0) {
147 ulist_addFirstItem(list, newItem);
149 newItem->previous = NULL;
150 newItem->next = list->head;
151 list->head->previous = newItem;
152 list->head = newItem;
153 list->currentIndex++;
159 U_CAPI UBool U_EXPORT2 ulist_containsString(const UList *list, const char *data, int32_t length) {
161 const UListNode *pointer;
162 for (pointer = list->head; pointer != NULL; pointer = pointer->next) {
163 if (length == uprv_strlen(pointer->data)) {
164 if (uprv_memcmp(data, pointer->data, length) == 0) {
173 U_CAPI UBool U_EXPORT2 ulist_removeString(UList *list, const char *data) {
176 for (pointer = list->head; pointer != NULL; pointer = pointer->next) {
177 if (uprv_strcmp(data, pointer->data) == 0) {
178 ulist_removeItem(list, pointer);
179 // Remove only the first occurrence, like Java LinkedList.remove(Object).
187 U_CAPI void *U_EXPORT2 ulist_getNext(UList *list) {
188 UListNode *curr = NULL;
190 if (list == NULL || list->curr == NULL) {
195 list->curr = curr->next;
196 list->currentIndex++;
201 U_CAPI int32_t U_EXPORT2 ulist_getListSize(const UList *list) {
209 U_CAPI void U_EXPORT2 ulist_resetList(UList *list) {
211 list->curr = list->head;
212 list->currentIndex = 0;
216 U_CAPI void U_EXPORT2 ulist_deleteList(UList *list) {
217 UListNode *listHead = NULL;
220 listHead = list->head;
221 while (listHead != NULL) {
222 UListNode *listPointer = listHead->next;
224 if (listHead->forceDelete) {
225 uprv_free(listHead->data);
229 listHead = listPointer;
236 U_CAPI void U_EXPORT2 ulist_close_keyword_values_iterator(UEnumeration *en) {
238 ulist_deleteList((UList *)(en->context));
243 U_CAPI int32_t U_EXPORT2 ulist_count_keyword_values(UEnumeration *en, UErrorCode *status) {
244 if (U_FAILURE(*status)) {
248 return ulist_getListSize((UList *)(en->context));
251 U_CAPI const char * U_EXPORT2 ulist_next_keyword_value(UEnumeration *en, int32_t *resultLength, UErrorCode *status) {
253 if (U_FAILURE(*status)) {
257 s = (const char *)ulist_getNext((UList *)(en->context));
258 if (s != NULL && resultLength != NULL) {
259 *resultLength = uprv_strlen(s);
264 U_CAPI void U_EXPORT2 ulist_reset_keyword_values_iterator(UEnumeration *en, UErrorCode *status) {
265 if (U_FAILURE(*status)) {
269 ulist_resetList((UList *)(en->context));
272 U_CAPI UList * U_EXPORT2 ulist_getListFromEnum(UEnumeration *en) {
273 return (UList *)(en->context);