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