2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2009-03-09 20:39:13 +00:00
|
|
|
/*
|
|
|
|
******************************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2009-2016, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2009-03-09 20:39:13 +00:00
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ulist.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "uenumimp.h"
|
|
|
|
|
|
|
|
typedef struct UListNode UListNode;
|
|
|
|
struct UListNode {
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
UListNode *next;
|
|
|
|
UListNode *previous;
|
|
|
|
|
|
|
|
/* When data is created with uprv_malloc, needs to be freed during deleteList function. */
|
|
|
|
UBool forceDelete;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UList {
|
|
|
|
UListNode *curr;
|
|
|
|
UListNode *head;
|
|
|
|
UListNode *tail;
|
|
|
|
|
|
|
|
int32_t size;
|
|
|
|
};
|
|
|
|
|
2009-03-09 21:56:08 +00:00
|
|
|
static void ulist_addFirstItem(UList *list, UListNode *newItem);
|
|
|
|
|
2009-03-09 20:39:13 +00:00
|
|
|
U_CAPI UList *U_EXPORT2 ulist_createEmptyList(UErrorCode *status) {
|
|
|
|
UList *newList = NULL;
|
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
newList = (UList *)uprv_malloc(sizeof(UList));
|
|
|
|
if (newList == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
newList->curr = NULL;
|
|
|
|
newList->head = NULL;
|
|
|
|
newList->tail = NULL;
|
|
|
|
newList->size = 0;
|
|
|
|
|
|
|
|
return newList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Function called by addItemEndList or addItemBeginList when the first item is added to the list.
|
|
|
|
* This function properly sets the pointers for the first item added.
|
|
|
|
*/
|
2009-03-09 21:56:08 +00:00
|
|
|
static void ulist_addFirstItem(UList *list, UListNode *newItem) {
|
2009-03-09 20:39:13 +00:00
|
|
|
newItem->next = NULL;
|
|
|
|
newItem->previous = NULL;
|
|
|
|
list->head = newItem;
|
|
|
|
list->tail = newItem;
|
2016-04-29 23:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ulist_removeItem(UList *list, UListNode *p) {
|
|
|
|
if (p->previous == NULL) {
|
|
|
|
// p is the list head.
|
|
|
|
list->head = p->next;
|
|
|
|
} else {
|
|
|
|
p->previous->next = p->next;
|
|
|
|
}
|
|
|
|
if (p->next == NULL) {
|
|
|
|
// p is the list tail.
|
|
|
|
list->tail = p->previous;
|
|
|
|
} else {
|
|
|
|
p->next->previous = p->previous;
|
|
|
|
}
|
2016-10-26 22:05:50 +00:00
|
|
|
if (p == list->curr) {
|
|
|
|
list->curr = p->next;
|
|
|
|
}
|
2016-04-29 23:04:00 +00:00
|
|
|
--list->size;
|
|
|
|
if (p->forceDelete) {
|
|
|
|
uprv_free(p->data);
|
|
|
|
}
|
|
|
|
uprv_free(p);
|
2009-03-09 20:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 ulist_addItemEndList(UList *list, const void *data, UBool forceDelete, UErrorCode *status) {
|
|
|
|
UListNode *newItem = NULL;
|
|
|
|
|
|
|
|
if (U_FAILURE(*status) || list == NULL || data == NULL) {
|
2016-04-29 23:04:00 +00:00
|
|
|
if (forceDelete) {
|
|
|
|
uprv_free((void *)data);
|
|
|
|
}
|
2009-03-09 20:39:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
newItem = (UListNode *)uprv_malloc(sizeof(UListNode));
|
|
|
|
if (newItem == NULL) {
|
2016-04-29 23:04:00 +00:00
|
|
|
if (forceDelete) {
|
|
|
|
uprv_free((void *)data);
|
|
|
|
}
|
2009-03-09 20:39:13 +00:00
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2009-03-27 17:08:12 +00:00
|
|
|
newItem->data = (void *)(data);
|
2009-03-09 20:39:13 +00:00
|
|
|
newItem->forceDelete = forceDelete;
|
|
|
|
|
|
|
|
if (list->size == 0) {
|
|
|
|
ulist_addFirstItem(list, newItem);
|
|
|
|
} else {
|
|
|
|
newItem->next = NULL;
|
|
|
|
newItem->previous = list->tail;
|
|
|
|
list->tail->next = newItem;
|
|
|
|
list->tail = newItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 ulist_addItemBeginList(UList *list, const void *data, UBool forceDelete, UErrorCode *status) {
|
|
|
|
UListNode *newItem = NULL;
|
|
|
|
|
|
|
|
if (U_FAILURE(*status) || list == NULL || data == NULL) {
|
2016-04-29 23:04:00 +00:00
|
|
|
if (forceDelete) {
|
|
|
|
uprv_free((void *)data);
|
|
|
|
}
|
2009-03-09 20:39:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
newItem = (UListNode *)uprv_malloc(sizeof(UListNode));
|
|
|
|
if (newItem == NULL) {
|
2016-04-29 23:04:00 +00:00
|
|
|
if (forceDelete) {
|
|
|
|
uprv_free((void *)data);
|
|
|
|
}
|
2009-03-09 20:39:13 +00:00
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2009-03-27 17:08:12 +00:00
|
|
|
newItem->data = (void *)(data);
|
2009-03-09 20:39:13 +00:00
|
|
|
newItem->forceDelete = forceDelete;
|
|
|
|
|
|
|
|
if (list->size == 0) {
|
|
|
|
ulist_addFirstItem(list, newItem);
|
|
|
|
} else {
|
|
|
|
newItem->previous = NULL;
|
|
|
|
newItem->next = list->head;
|
|
|
|
list->head->previous = newItem;
|
|
|
|
list->head = newItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UBool U_EXPORT2 ulist_containsString(const UList *list, const char *data, int32_t length) {
|
2016-04-29 23:04:00 +00:00
|
|
|
if (list != NULL) {
|
2016-05-02 17:22:49 +00:00
|
|
|
const UListNode *pointer;
|
|
|
|
for (pointer = list->head; pointer != NULL; pointer = pointer->next) {
|
2016-12-07 21:14:27 +00:00
|
|
|
if (length == (int32_t)uprv_strlen((const char *)pointer->data)) {
|
2009-03-09 20:39:13 +00:00
|
|
|
if (uprv_memcmp(data, pointer->data, length) == 0) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2009-03-09 20:39:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2016-04-29 23:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UBool U_EXPORT2 ulist_removeString(UList *list, const char *data) {
|
|
|
|
if (list != NULL) {
|
2016-05-02 17:22:49 +00:00
|
|
|
UListNode *pointer;
|
|
|
|
for (pointer = list->head; pointer != NULL; pointer = pointer->next) {
|
2016-12-07 21:14:27 +00:00
|
|
|
if (uprv_strcmp(data, (const char *)pointer->data) == 0) {
|
2016-04-29 23:04:00 +00:00
|
|
|
ulist_removeItem(list, pointer);
|
|
|
|
// Remove only the first occurrence, like Java LinkedList.remove(Object).
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2016-04-29 23:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2009-03-09 20:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void *U_EXPORT2 ulist_getNext(UList *list) {
|
|
|
|
UListNode *curr = NULL;
|
|
|
|
|
|
|
|
if (list == NULL || list->curr == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
curr = list->curr;
|
|
|
|
list->curr = curr->next;
|
|
|
|
|
|
|
|
return curr->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2 ulist_getListSize(const UList *list) {
|
|
|
|
if (list != NULL) {
|
|
|
|
return list->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 ulist_resetList(UList *list) {
|
|
|
|
if (list != NULL) {
|
|
|
|
list->curr = list->head;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 ulist_deleteList(UList *list) {
|
|
|
|
UListNode *listHead = NULL;
|
2014-09-11 05:25:13 +00:00
|
|
|
|
2009-03-09 20:39:13 +00:00
|
|
|
if (list != NULL) {
|
|
|
|
listHead = list->head;
|
|
|
|
while (listHead != NULL) {
|
2014-09-11 05:25:13 +00:00
|
|
|
UListNode *listPointer = listHead->next;
|
|
|
|
|
2009-03-09 20:39:13 +00:00
|
|
|
if (listHead->forceDelete) {
|
|
|
|
uprv_free(listHead->data);
|
|
|
|
}
|
2014-09-11 05:25:13 +00:00
|
|
|
|
2009-03-09 20:39:13 +00:00
|
|
|
uprv_free(listHead);
|
|
|
|
listHead = listPointer;
|
|
|
|
}
|
|
|
|
uprv_free(list);
|
|
|
|
list = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 ulist_close_keyword_values_iterator(UEnumeration *en) {
|
2009-03-09 21:56:08 +00:00
|
|
|
if (en != NULL) {
|
|
|
|
ulist_deleteList((UList *)(en->context));
|
|
|
|
uprv_free(en);
|
|
|
|
}
|
2009-03-09 20:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2 ulist_count_keyword_values(UEnumeration *en, UErrorCode *status) {
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-03-09 21:56:08 +00:00
|
|
|
return ulist_getListSize((UList *)(en->context));
|
2009-03-09 20:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI const char * U_EXPORT2 ulist_next_keyword_value(UEnumeration *en, int32_t *resultLength, UErrorCode *status) {
|
2012-10-07 04:52:16 +00:00
|
|
|
const char *s;
|
2009-03-09 20:39:13 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-07 04:52:16 +00:00
|
|
|
|
|
|
|
s = (const char *)ulist_getNext((UList *)(en->context));
|
|
|
|
if (s != NULL && resultLength != NULL) {
|
2017-07-22 01:11:59 +00:00
|
|
|
*resultLength = static_cast<int32_t>(uprv_strlen(s));
|
2012-10-07 04:52:16 +00:00
|
|
|
}
|
|
|
|
return s;
|
2009-03-09 20:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 ulist_reset_keyword_values_iterator(UEnumeration *en, UErrorCode *status) {
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return ;
|
|
|
|
}
|
2009-03-09 21:56:08 +00:00
|
|
|
|
|
|
|
ulist_resetList((UList *)(en->context));
|
2009-03-09 20:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UList * U_EXPORT2 ulist_getListFromEnum(UEnumeration *en) {
|
|
|
|
return (UList *)(en->context);
|
|
|
|
}
|