2 **********************************************************************
3 * Copyright (c) 2002-2011, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
7 * Created: November 11 2002
9 **********************************************************************
11 #include <typeinfo> // for 'typeid' to work
13 #include "unicode/ustring.h"
14 #include "unicode/strenum.h"
15 #include "unicode/putil.h"
23 // StringEnumeration implementation ---------------------------------------- ***
25 StringEnumeration::StringEnumeration()
26 : chars(charsBuffer
), charsCapacity(sizeof(charsBuffer
)) {
29 StringEnumeration::~StringEnumeration() {
30 if (chars
!= NULL
&& chars
!= charsBuffer
) {
35 // StringEnumeration base class clone() default implementation, does not clone
37 StringEnumeration::clone() const {
42 StringEnumeration::next(int32_t *resultLength
, UErrorCode
&status
) {
43 const UnicodeString
*s
=snext(status
);
46 ensureCharsCapacity(unistr
.length()+1, status
);
47 if(U_SUCCESS(status
)) {
48 if(resultLength
!=NULL
) {
49 *resultLength
=unistr
.length();
51 unistr
.extract(0, INT32_MAX
, chars
, charsCapacity
, US_INV
);
60 StringEnumeration::unext(int32_t *resultLength
, UErrorCode
&status
) {
61 const UnicodeString
*s
=snext(status
);
64 if(U_SUCCESS(status
)) {
65 if(resultLength
!=NULL
) {
66 *resultLength
=unistr
.length();
68 return unistr
.getTerminatedBuffer();
76 StringEnumeration::ensureCharsCapacity(int32_t capacity
, UErrorCode
&status
) {
77 if(U_SUCCESS(status
) && capacity
>charsCapacity
) {
78 if(capacity
<(charsCapacity
+charsCapacity
/2)) {
79 // avoid allocation thrashing
80 capacity
=charsCapacity
+charsCapacity
/2;
82 if(chars
!=charsBuffer
) {
85 chars
=(char *)uprv_malloc(capacity
);
88 charsCapacity
=sizeof(charsBuffer
);
89 status
=U_MEMORY_ALLOCATION_ERROR
;
91 charsCapacity
=capacity
;
97 StringEnumeration::setChars(const char *s
, int32_t length
, UErrorCode
&status
) {
98 if(U_SUCCESS(status
) && s
!=NULL
) {
100 length
=(int32_t)uprv_strlen(s
);
103 UChar
*buffer
=unistr
.getBuffer(length
+1);
105 u_charsToUChars(s
, buffer
, length
);
107 unistr
.releaseBuffer(length
);
110 status
=U_MEMORY_ALLOCATION_ERROR
;
117 StringEnumeration::operator==(const StringEnumeration
& that
)const {
118 return typeid(*this) == typeid(that
);
122 StringEnumeration::operator!=(const StringEnumeration
& that
)const {
123 return !operator==(that
);
126 // UStringEnumeration implementation --------------------------------------- ***
128 UStringEnumeration::UStringEnumeration(UEnumeration
* _uenum
) :
130 U_ASSERT(_uenum
!= 0);
133 UStringEnumeration::~UStringEnumeration() {
137 int32_t UStringEnumeration::count(UErrorCode
& status
) const {
138 return uenum_count(uenum
, &status
);
141 const UnicodeString
* UStringEnumeration::snext(UErrorCode
& status
) {
143 const UChar
* str
= uenum_unext(uenum
, &length
, &status
);
144 if (str
== 0 || U_FAILURE(status
)) {
147 return &unistr
.setTo(str
, length
);
150 void UStringEnumeration::reset(UErrorCode
& status
) {
151 uenum_reset(uenum
, &status
);
154 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UStringEnumeration
)
157 // C wrapper --------------------------------------------------------------- ***
159 #define THIS(en) ((icu::StringEnumeration*)(en->context))
164 * Wrapper API to make StringEnumeration look like UEnumeration.
166 static void U_CALLCONV
167 ustrenum_close(UEnumeration
* en
) {
173 * Wrapper API to make StringEnumeration look like UEnumeration.
175 static int32_t U_CALLCONV
176 ustrenum_count(UEnumeration
* en
,
179 return THIS(en
)->count(*ec
);
183 * Wrapper API to make StringEnumeration look like UEnumeration.
185 static const UChar
* U_CALLCONV
186 ustrenum_unext(UEnumeration
* en
,
187 int32_t* resultLength
,
190 return THIS(en
)->unext(resultLength
, *ec
);
194 * Wrapper API to make StringEnumeration look like UEnumeration.
196 static const char* U_CALLCONV
197 ustrenum_next(UEnumeration
* en
,
198 int32_t* resultLength
,
201 return THIS(en
)->next(resultLength
, *ec
);
205 * Wrapper API to make StringEnumeration look like UEnumeration.
207 static void U_CALLCONV
208 ustrenum_reset(UEnumeration
* en
,
211 THIS(en
)->reset(*ec
);
215 * Pseudo-vtable for UEnumeration wrapper around StringEnumeration.
216 * The StringEnumeration pointer will be stored in 'context'.
218 static const UEnumeration USTRENUM_VT
= {
220 NULL
, // store StringEnumeration pointer here
231 * Given a StringEnumeration, wrap it in a UEnumeration. The
232 * StringEnumeration is adopted; after this call, the caller must not
233 * delete it (regardless of error status).
235 U_CAPI UEnumeration
* U_EXPORT2
236 uenum_openFromStringEnumeration(icu::StringEnumeration
* adopted
, UErrorCode
* ec
) {
237 UEnumeration
* result
= NULL
;
238 if (U_SUCCESS(*ec
) && adopted
!= NULL
) {
239 result
= (UEnumeration
*) uprv_malloc(sizeof(UEnumeration
));
240 if (result
== NULL
) {
241 *ec
= U_MEMORY_ALLOCATION_ERROR
;
243 uprv_memcpy(result
, &USTRENUM_VT
, sizeof(USTRENUM_VT
));
244 result
->context
= adopted
;
247 if (result
== NULL
) {
253 // C wrapper --------------------------------------------------------------- ***
257 typedef struct UCharStringEnumeration
{
259 int32_t index
, count
;
260 } UCharStringEnumeration
;
262 static void U_CALLCONV
263 ucharstrenum_close(UEnumeration
* en
) {
267 static int32_t U_CALLCONV
268 ucharstrenum_count(UEnumeration
* en
,
269 UErrorCode
* /*ec*/) {
270 return ((UCharStringEnumeration
*)en
)->count
;
273 static const char* U_CALLCONV
274 ucharstrenum_next(UEnumeration
* en
,
275 int32_t* resultLength
,
276 UErrorCode
* /*ec*/) {
277 UCharStringEnumeration
*e
= (UCharStringEnumeration
*) en
;
278 if (e
->index
>= e
->count
) {
281 const char* result
= ((const char**)e
->uenum
.context
)[e
->index
++];
283 *resultLength
= (int32_t)uprv_strlen(result
);
288 static void U_CALLCONV
289 ucharstrenum_reset(UEnumeration
* en
,
290 UErrorCode
* /*ec*/) {
291 ((UCharStringEnumeration
*)en
)->index
= 0;
294 static const UEnumeration UCHARSTRENUM_VT
= {
296 NULL
, // store StringEnumeration pointer here
306 U_CAPI UEnumeration
* U_EXPORT2
307 uenum_openCharStringsEnumeration(const char* const* strings
, int32_t count
,
309 UCharStringEnumeration
* result
= NULL
;
310 if (U_SUCCESS(*ec
) && count
>= 0 && (count
== 0 || strings
!= 0)) {
311 result
= (UCharStringEnumeration
*) uprv_malloc(sizeof(UCharStringEnumeration
));
312 if (result
== NULL
) {
313 *ec
= U_MEMORY_ALLOCATION_ERROR
;
315 U_ASSERT((char*)result
==(char*)(&result
->uenum
));
316 uprv_memcpy(result
, &UCHARSTRENUM_VT
, sizeof(UCHARSTRENUM_VT
));
317 result
->uenum
.context
= (void*)strings
;
319 result
->count
= count
;
322 return (UEnumeration
*) result
;