Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 2002-2012, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | * file name: uenum.c |
michael@0 | 9 | * encoding: US-ASCII |
michael@0 | 10 | * tab size: 8 (not used) |
michael@0 | 11 | * indentation:2 |
michael@0 | 12 | * |
michael@0 | 13 | * created on: 2002jul08 |
michael@0 | 14 | * created by: Vladimir Weinstein |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #include "unicode/putil.h" |
michael@0 | 18 | #include "uenumimp.h" |
michael@0 | 19 | #include "cmemory.h" |
michael@0 | 20 | |
michael@0 | 21 | /* Layout of the baseContext buffer. */ |
michael@0 | 22 | typedef struct { |
michael@0 | 23 | int32_t len; /* number of bytes available starting at 'data' */ |
michael@0 | 24 | char data; /* actual data starts here */ |
michael@0 | 25 | } _UEnumBuffer; |
michael@0 | 26 | |
michael@0 | 27 | /* Extra bytes to allocate in the baseContext buffer. */ |
michael@0 | 28 | static const int32_t PAD = 8; |
michael@0 | 29 | |
michael@0 | 30 | /* Return a pointer to the baseContext buffer, possibly allocating |
michael@0 | 31 | or reallocating it if at least 'capacity' bytes are not available. */ |
michael@0 | 32 | static void* _getBuffer(UEnumeration* en, int32_t capacity) { |
michael@0 | 33 | |
michael@0 | 34 | if (en->baseContext != NULL) { |
michael@0 | 35 | if (((_UEnumBuffer*) en->baseContext)->len < capacity) { |
michael@0 | 36 | capacity += PAD; |
michael@0 | 37 | en->baseContext = uprv_realloc(en->baseContext, |
michael@0 | 38 | sizeof(int32_t) + capacity); |
michael@0 | 39 | if (en->baseContext == NULL) { |
michael@0 | 40 | return NULL; |
michael@0 | 41 | } |
michael@0 | 42 | ((_UEnumBuffer*) en->baseContext)->len = capacity; |
michael@0 | 43 | } |
michael@0 | 44 | } else { |
michael@0 | 45 | capacity += PAD; |
michael@0 | 46 | en->baseContext = uprv_malloc(sizeof(int32_t) + capacity); |
michael@0 | 47 | if (en->baseContext == NULL) { |
michael@0 | 48 | return NULL; |
michael@0 | 49 | } |
michael@0 | 50 | ((_UEnumBuffer*) en->baseContext)->len = capacity; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | return (void*) & ((_UEnumBuffer*) en->baseContext)->data; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | U_CAPI void U_EXPORT2 |
michael@0 | 57 | uenum_close(UEnumeration* en) |
michael@0 | 58 | { |
michael@0 | 59 | if (en) { |
michael@0 | 60 | if (en->close != NULL) { |
michael@0 | 61 | if (en->baseContext) { |
michael@0 | 62 | uprv_free(en->baseContext); |
michael@0 | 63 | } |
michael@0 | 64 | en->close(en); |
michael@0 | 65 | } else { /* this seems dangerous, but we better kill the object */ |
michael@0 | 66 | uprv_free(en); |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 72 | uenum_count(UEnumeration* en, UErrorCode* status) |
michael@0 | 73 | { |
michael@0 | 74 | if (!en || U_FAILURE(*status)) { |
michael@0 | 75 | return -1; |
michael@0 | 76 | } |
michael@0 | 77 | if (en->count != NULL) { |
michael@0 | 78 | return en->count(en, status); |
michael@0 | 79 | } else { |
michael@0 | 80 | *status = U_UNSUPPORTED_ERROR; |
michael@0 | 81 | return -1; |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | /* Don't call this directly. Only uenum_unext should be calling this. */ |
michael@0 | 86 | U_CAPI const UChar* U_EXPORT2 |
michael@0 | 87 | uenum_unextDefault(UEnumeration* en, |
michael@0 | 88 | int32_t* resultLength, |
michael@0 | 89 | UErrorCode* status) |
michael@0 | 90 | { |
michael@0 | 91 | UChar *ustr = NULL; |
michael@0 | 92 | int32_t len = 0; |
michael@0 | 93 | if (en->next != NULL) { |
michael@0 | 94 | const char *cstr = en->next(en, &len, status); |
michael@0 | 95 | if (cstr != NULL) { |
michael@0 | 96 | ustr = (UChar*) _getBuffer(en, (len+1) * sizeof(UChar)); |
michael@0 | 97 | if (ustr == NULL) { |
michael@0 | 98 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 99 | } else { |
michael@0 | 100 | u_charsToUChars(cstr, ustr, len+1); |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | } else { |
michael@0 | 104 | *status = U_UNSUPPORTED_ERROR; |
michael@0 | 105 | } |
michael@0 | 106 | if (resultLength) { |
michael@0 | 107 | *resultLength = len; |
michael@0 | 108 | } |
michael@0 | 109 | return ustr; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | /* Don't call this directly. Only uenum_next should be calling this. */ |
michael@0 | 113 | U_CAPI const char* U_EXPORT2 |
michael@0 | 114 | uenum_nextDefault(UEnumeration* en, |
michael@0 | 115 | int32_t* resultLength, |
michael@0 | 116 | UErrorCode* status) |
michael@0 | 117 | { |
michael@0 | 118 | if (en->uNext != NULL) { |
michael@0 | 119 | char *tempCharVal; |
michael@0 | 120 | const UChar *tempUCharVal = en->uNext(en, resultLength, status); |
michael@0 | 121 | if (tempUCharVal == NULL) { |
michael@0 | 122 | return NULL; |
michael@0 | 123 | } |
michael@0 | 124 | tempCharVal = (char*) |
michael@0 | 125 | _getBuffer(en, (*resultLength+1) * sizeof(char)); |
michael@0 | 126 | if (!tempCharVal) { |
michael@0 | 127 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 128 | return NULL; |
michael@0 | 129 | } |
michael@0 | 130 | u_UCharsToChars(tempUCharVal, tempCharVal, *resultLength + 1); |
michael@0 | 131 | return tempCharVal; |
michael@0 | 132 | } else { |
michael@0 | 133 | *status = U_UNSUPPORTED_ERROR; |
michael@0 | 134 | return NULL; |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | U_CAPI const UChar* U_EXPORT2 |
michael@0 | 139 | uenum_unext(UEnumeration* en, |
michael@0 | 140 | int32_t* resultLength, |
michael@0 | 141 | UErrorCode* status) |
michael@0 | 142 | { |
michael@0 | 143 | if (!en || U_FAILURE(*status)) { |
michael@0 | 144 | return NULL; |
michael@0 | 145 | } |
michael@0 | 146 | if (en->uNext != NULL) { |
michael@0 | 147 | return en->uNext(en, resultLength, status); |
michael@0 | 148 | } else { |
michael@0 | 149 | *status = U_UNSUPPORTED_ERROR; |
michael@0 | 150 | return NULL; |
michael@0 | 151 | } |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | U_CAPI const char* U_EXPORT2 |
michael@0 | 155 | uenum_next(UEnumeration* en, |
michael@0 | 156 | int32_t* resultLength, |
michael@0 | 157 | UErrorCode* status) |
michael@0 | 158 | { |
michael@0 | 159 | if (!en || U_FAILURE(*status)) { |
michael@0 | 160 | return NULL; |
michael@0 | 161 | } |
michael@0 | 162 | if (en->next != NULL) { |
michael@0 | 163 | if (resultLength != NULL) { |
michael@0 | 164 | return en->next(en, resultLength, status); |
michael@0 | 165 | } |
michael@0 | 166 | else { |
michael@0 | 167 | int32_t dummyLength=0; |
michael@0 | 168 | return en->next(en, &dummyLength, status); |
michael@0 | 169 | } |
michael@0 | 170 | } else { |
michael@0 | 171 | *status = U_UNSUPPORTED_ERROR; |
michael@0 | 172 | return NULL; |
michael@0 | 173 | } |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | U_CAPI void U_EXPORT2 |
michael@0 | 177 | uenum_reset(UEnumeration* en, UErrorCode* status) |
michael@0 | 178 | { |
michael@0 | 179 | if (!en || U_FAILURE(*status)) { |
michael@0 | 180 | return; |
michael@0 | 181 | } |
michael@0 | 182 | if (en->reset != NULL) { |
michael@0 | 183 | en->reset(en, status); |
michael@0 | 184 | } else { |
michael@0 | 185 | *status = U_UNSUPPORTED_ERROR; |
michael@0 | 186 | } |
michael@0 | 187 | } |