2 ******************************************************************************
3 * Copyright (C) 2009, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ******************************************************************************
13 typedef struct UListNode UListNode
;
20 /* When data is created with uprv_malloc, needs to be freed during deleteList function. */
33 static void ulist_addFirstItem(UList
*list
, UListNode
*newItem
);
35 U_CAPI UList
*U_EXPORT2
ulist_createEmptyList(UErrorCode
*status
) {
36 UList
*newList
= NULL
;
38 if (U_FAILURE(*status
)) {
42 newList
= (UList
*)uprv_malloc(sizeof(UList
));
43 if (newList
== NULL
) {
44 *status
= U_MEMORY_ALLOCATION_ERROR
;
52 newList
->currentIndex
= -1;
58 * Function called by addItemEndList or addItemBeginList when the first item is added to the list.
59 * This function properly sets the pointers for the first item added.
61 static void ulist_addFirstItem(UList
*list
, UListNode
*newItem
) {
63 newItem
->previous
= NULL
;
66 list
->currentIndex
= 0;
69 U_CAPI
void U_EXPORT2
ulist_addItemEndList(UList
*list
, const void *data
, UBool forceDelete
, UErrorCode
*status
) {
70 UListNode
*newItem
= NULL
;
72 if (U_FAILURE(*status
) || list
== NULL
|| data
== NULL
) {
76 newItem
= (UListNode
*)uprv_malloc(sizeof(UListNode
));
77 if (newItem
== NULL
) {
78 *status
= U_MEMORY_ALLOCATION_ERROR
;
81 newItem
->data
= (void *)(data
);
82 newItem
->forceDelete
= forceDelete
;
84 if (list
->size
== 0) {
85 ulist_addFirstItem(list
, newItem
);
88 newItem
->previous
= list
->tail
;
89 list
->tail
->next
= newItem
;
96 U_CAPI
void U_EXPORT2
ulist_addItemBeginList(UList
*list
, const void *data
, UBool forceDelete
, UErrorCode
*status
) {
97 UListNode
*newItem
= NULL
;
99 if (U_FAILURE(*status
) || list
== NULL
|| data
== NULL
) {
103 newItem
= (UListNode
*)uprv_malloc(sizeof(UListNode
));
104 if (newItem
== NULL
) {
105 *status
= U_MEMORY_ALLOCATION_ERROR
;
108 newItem
->data
= (void *)(data
);
109 newItem
->forceDelete
= forceDelete
;
111 if (list
->size
== 0) {
112 ulist_addFirstItem(list
, newItem
);
114 newItem
->previous
= NULL
;
115 newItem
->next
= list
->head
;
116 list
->head
->previous
= newItem
;
117 list
->head
= newItem
;
118 list
->currentIndex
++;
124 U_CAPI UBool U_EXPORT2
ulist_containsString(const UList
*list
, const char *data
, int32_t length
) {
125 UBool result
= FALSE
;
126 const UListNode
*pointer
= NULL
;
128 if (list
!= NULL
&& list
->size
!= 0) {
129 pointer
= list
->head
;
131 while (pointer
!= NULL
) {
132 if (length
== uprv_strlen(pointer
->data
)) {
133 if (uprv_memcmp(data
, pointer
->data
, length
) == 0) {
139 pointer
= pointer
->next
;
146 U_CAPI
void *U_EXPORT2
ulist_getNext(UList
*list
) {
147 UListNode
*curr
= NULL
;
149 if (list
== NULL
|| list
->curr
== NULL
) {
154 list
->curr
= curr
->next
;
155 list
->currentIndex
++;
160 U_CAPI
int32_t U_EXPORT2
ulist_getListSize(const UList
*list
) {
168 U_CAPI
void U_EXPORT2
ulist_resetList(UList
*list
) {
170 list
->curr
= list
->head
;
171 list
->currentIndex
= 0;
175 U_CAPI
void U_EXPORT2
ulist_deleteList(UList
*list
) {
176 UListNode
*listHead
= NULL
;
177 UListNode
*listPointer
= NULL
;
180 listHead
= list
->head
;
181 listPointer
= listHead
;
182 while (listHead
!= NULL
) {
183 listPointer
= listHead
->next
;
185 if (listHead
->forceDelete
) {
186 uprv_free(listHead
->data
);
190 listHead
= listPointer
;
197 U_CAPI
void U_EXPORT2
ulist_close_keyword_values_iterator(UEnumeration
*en
) {
199 ulist_deleteList((UList
*)(en
->context
));
204 U_CAPI
int32_t U_EXPORT2
ulist_count_keyword_values(UEnumeration
*en
, UErrorCode
*status
) {
205 if (U_FAILURE(*status
)) {
209 return ulist_getListSize((UList
*)(en
->context
));
212 U_CAPI
const char * U_EXPORT2
ulist_next_keyword_value(UEnumeration
*en
, int32_t *resultLength
, UErrorCode
*status
) {
213 if (U_FAILURE(*status
)) {
217 /* TODO: resultLength; */
219 return (const char *)ulist_getNext((UList
*)(en
->context
));
222 U_CAPI
void U_EXPORT2
ulist_reset_keyword_values_iterator(UEnumeration
*en
, UErrorCode
*status
) {
223 if (U_FAILURE(*status
)) {
227 ulist_resetList((UList
*)(en
->context
));
230 U_CAPI UList
* U_EXPORT2
ulist_getListFromEnum(UEnumeration
*en
) {
231 return (UList
*)(en
->context
);