intl/icu/source/common/ulist.c

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /*
     2 ******************************************************************************
     3 *   Copyright (C) 2009-2012, International Business Machines
     4 *   Corporation and others.  All Rights Reserved.
     5 ******************************************************************************
     6 */
     8 #include "ulist.h"
     9 #include "cmemory.h"
    10 #include "cstring.h"
    11 #include "uenumimp.h"
    13 typedef struct UListNode UListNode;
    14 struct UListNode {
    15     void *data;
    17     UListNode *next;
    18     UListNode *previous;
    20     /* When data is created with uprv_malloc, needs to be freed during deleteList function. */
    21     UBool forceDelete;
    22 };
    24 struct UList {
    25     UListNode *curr;
    26     UListNode *head;
    27     UListNode *tail;
    29     int32_t size;
    30     int32_t currentIndex;
    31 };
    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)) {
    39         return NULL;
    40     }
    42     newList = (UList *)uprv_malloc(sizeof(UList));
    43     if (newList == NULL) {
    44         *status = U_MEMORY_ALLOCATION_ERROR;
    45         return NULL;
    46     }
    48     newList->curr = NULL;
    49     newList->head = NULL;
    50     newList->tail = NULL;
    51     newList->size = 0;
    52     newList->currentIndex = -1;
    54     return newList;
    55 }
    57 /*
    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.
    60  */
    61 static void ulist_addFirstItem(UList *list, UListNode *newItem) {
    62     newItem->next = NULL;
    63     newItem->previous = NULL;
    64     list->head = newItem;
    65     list->tail = newItem;
    66     list->currentIndex = 0;
    67 }
    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) {
    73         return;
    74     }
    76     newItem = (UListNode *)uprv_malloc(sizeof(UListNode));
    77     if (newItem == NULL) {
    78         *status = U_MEMORY_ALLOCATION_ERROR;
    79         return;
    80     }
    81     newItem->data = (void *)(data);
    82     newItem->forceDelete = forceDelete;
    84     if (list->size == 0) {
    85         ulist_addFirstItem(list, newItem);
    86     } else {
    87         newItem->next = NULL;
    88         newItem->previous = list->tail;
    89         list->tail->next = newItem;
    90         list->tail = newItem;
    91     }
    93     list->size++;
    94 }
    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) {
   100         return;
   101     }
   103     newItem = (UListNode *)uprv_malloc(sizeof(UListNode));
   104     if (newItem == NULL) {
   105         *status = U_MEMORY_ALLOCATION_ERROR;
   106         return;
   107     }
   108     newItem->data = (void *)(data);
   109     newItem->forceDelete = forceDelete;
   111     if (list->size == 0) {
   112         ulist_addFirstItem(list, newItem);
   113     } else {
   114         newItem->previous = NULL;
   115         newItem->next = list->head;
   116         list->head->previous = newItem;
   117         list->head = newItem;
   118         list->currentIndex++;
   119     }
   121     list->size++;
   122 }
   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) {
   134                     result = TRUE;
   135                     break;
   136                 }
   137             }
   139             pointer = pointer->next;
   140         }
   141     }
   143     return result;
   144 }
   146 U_CAPI void *U_EXPORT2 ulist_getNext(UList *list) {
   147     UListNode *curr = NULL;
   149     if (list == NULL || list->curr == NULL) {
   150         return NULL;
   151     }
   153     curr = list->curr;
   154     list->curr = curr->next;
   155     list->currentIndex++;
   157     return curr->data;
   158 }
   160 U_CAPI int32_t U_EXPORT2 ulist_getListSize(const UList *list) {
   161     if (list != NULL) {
   162         return list->size;
   163     }
   165     return -1;
   166 }
   168 U_CAPI void U_EXPORT2 ulist_resetList(UList *list) {
   169     if (list != NULL) {
   170         list->curr = list->head;
   171         list->currentIndex = 0;
   172     }
   173 }
   175 U_CAPI void U_EXPORT2 ulist_deleteList(UList *list) {
   176     UListNode *listHead = NULL;
   177     UListNode *listPointer = NULL;
   179     if (list != 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);
   187             }
   189             uprv_free(listHead);
   190             listHead = listPointer;
   191         }
   192         uprv_free(list);
   193         list = NULL;
   194     }
   195 }
   197 U_CAPI void U_EXPORT2 ulist_close_keyword_values_iterator(UEnumeration *en) {
   198     if (en != NULL) {
   199         ulist_deleteList((UList *)(en->context));
   200         uprv_free(en);
   201     }
   202 }
   204 U_CAPI int32_t U_EXPORT2 ulist_count_keyword_values(UEnumeration *en, UErrorCode *status) {
   205     if (U_FAILURE(*status)) {
   206         return -1;
   207     }
   209     return ulist_getListSize((UList *)(en->context));
   210 }
   212 U_CAPI const char * U_EXPORT2 ulist_next_keyword_value(UEnumeration *en, int32_t *resultLength, UErrorCode *status) {
   213     const char *s;
   214     if (U_FAILURE(*status)) {
   215         return NULL;
   216     }
   218     s = (const char *)ulist_getNext((UList *)(en->context));
   219     if (s != NULL && resultLength != NULL) {
   220         *resultLength = uprv_strlen(s);
   221     }
   222     return s;
   223 }
   225 U_CAPI void U_EXPORT2 ulist_reset_keyword_values_iterator(UEnumeration *en, UErrorCode *status) {
   226     if (U_FAILURE(*status)) {
   227         return ;
   228     }
   230     ulist_resetList((UList *)(en->context));
   231 }
   233 U_CAPI UList * U_EXPORT2 ulist_getListFromEnum(UEnumeration *en) {
   234     return (UList *)(en->context);
   235 }

mercurial