intl/icu/source/common/ustrenum.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/ustrenum.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,380 @@
     1.4 +/*
     1.5 +**********************************************************************
     1.6 +* Copyright (c) 2002-2012, International Business Machines
     1.7 +* Corporation and others.  All Rights Reserved.
     1.8 +**********************************************************************
     1.9 +* Author: Alan Liu
    1.10 +* Created: November 11 2002
    1.11 +* Since: ICU 2.4
    1.12 +**********************************************************************
    1.13 +*/
    1.14 +#include "utypeinfo.h"  // for 'typeid' to work 
    1.15 +
    1.16 +#include "unicode/ustring.h"
    1.17 +#include "unicode/strenum.h"
    1.18 +#include "unicode/putil.h"
    1.19 +#include "uenumimp.h"
    1.20 +#include "ustrenum.h"
    1.21 +#include "cstring.h"
    1.22 +#include "cmemory.h"
    1.23 +#include "uassert.h"
    1.24 +
    1.25 +U_NAMESPACE_BEGIN
    1.26 +// StringEnumeration implementation ---------------------------------------- ***
    1.27 +
    1.28 +StringEnumeration::StringEnumeration()
    1.29 +    : chars(charsBuffer), charsCapacity(sizeof(charsBuffer)) {
    1.30 +}
    1.31 +
    1.32 +StringEnumeration::~StringEnumeration() {
    1.33 +    if (chars != NULL && chars != charsBuffer) {
    1.34 +        uprv_free(chars);
    1.35 +    }
    1.36 +}
    1.37 +
    1.38 +// StringEnumeration base class clone() default implementation, does not clone
    1.39 +StringEnumeration *
    1.40 +StringEnumeration::clone() const {
    1.41 +  return NULL;
    1.42 +}
    1.43 +
    1.44 +const char *
    1.45 +StringEnumeration::next(int32_t *resultLength, UErrorCode &status) {
    1.46 +    const UnicodeString *s=snext(status);
    1.47 +    if(U_SUCCESS(status) && s!=NULL) {
    1.48 +        unistr=*s;
    1.49 +        ensureCharsCapacity(unistr.length()+1, status);
    1.50 +        if(U_SUCCESS(status)) {
    1.51 +            if(resultLength!=NULL) {
    1.52 +                *resultLength=unistr.length();
    1.53 +            }
    1.54 +            unistr.extract(0, INT32_MAX, chars, charsCapacity, US_INV);
    1.55 +            return chars;
    1.56 +        }
    1.57 +    }
    1.58 +
    1.59 +    return NULL;
    1.60 +}
    1.61 +
    1.62 +const UChar *
    1.63 +StringEnumeration::unext(int32_t *resultLength, UErrorCode &status) {
    1.64 +    const UnicodeString *s=snext(status);
    1.65 +    if(U_SUCCESS(status) && s!=NULL) {
    1.66 +        unistr=*s;
    1.67 +        if(resultLength!=NULL) {
    1.68 +            *resultLength=unistr.length();
    1.69 +        }
    1.70 +        return unistr.getTerminatedBuffer();
    1.71 +    }
    1.72 +
    1.73 +    return NULL;
    1.74 +}
    1.75 +
    1.76 +const UnicodeString *
    1.77 +StringEnumeration::snext(UErrorCode &status) {
    1.78 +    int32_t length;
    1.79 +    const char *s=next(&length, status);
    1.80 +    return setChars(s, length, status);
    1.81 +}
    1.82 +
    1.83 +void
    1.84 +StringEnumeration::ensureCharsCapacity(int32_t capacity, UErrorCode &status) {
    1.85 +    if(U_SUCCESS(status) && capacity>charsCapacity) {
    1.86 +        if(capacity<(charsCapacity+charsCapacity/2)) {
    1.87 +            // avoid allocation thrashing
    1.88 +            capacity=charsCapacity+charsCapacity/2;
    1.89 +        }
    1.90 +        if(chars!=charsBuffer) {
    1.91 +            uprv_free(chars);
    1.92 +        }
    1.93 +        chars=(char *)uprv_malloc(capacity);
    1.94 +        if(chars==NULL) {
    1.95 +            chars=charsBuffer;
    1.96 +            charsCapacity=sizeof(charsBuffer);
    1.97 +            status=U_MEMORY_ALLOCATION_ERROR;
    1.98 +        } else {
    1.99 +            charsCapacity=capacity;
   1.100 +        }
   1.101 +    }
   1.102 +}
   1.103 +
   1.104 +UnicodeString *
   1.105 +StringEnumeration::setChars(const char *s, int32_t length, UErrorCode &status) {
   1.106 +    if(U_SUCCESS(status) && s!=NULL) {
   1.107 +        if(length<0) {
   1.108 +            length=(int32_t)uprv_strlen(s);
   1.109 +        }
   1.110 +
   1.111 +        UChar *buffer=unistr.getBuffer(length+1);
   1.112 +        if(buffer!=NULL) {
   1.113 +            u_charsToUChars(s, buffer, length);
   1.114 +            buffer[length]=0;
   1.115 +            unistr.releaseBuffer(length);
   1.116 +            return &unistr;
   1.117 +        } else {
   1.118 +            status=U_MEMORY_ALLOCATION_ERROR;
   1.119 +        }
   1.120 +    }
   1.121 +
   1.122 +    return NULL;
   1.123 +}
   1.124 +UBool 
   1.125 +StringEnumeration::operator==(const StringEnumeration& that)const {
   1.126 +    return typeid(*this) == typeid(that); 
   1.127 +}
   1.128 +
   1.129 +UBool
   1.130 +StringEnumeration::operator!=(const StringEnumeration& that)const {
   1.131 +    return !operator==(that);
   1.132 +}
   1.133 +
   1.134 +// UStringEnumeration implementation --------------------------------------- ***
   1.135 +
   1.136 +UStringEnumeration::UStringEnumeration(UEnumeration* _uenum) :
   1.137 +    uenum(_uenum) {
   1.138 +    U_ASSERT(_uenum != 0);
   1.139 +}
   1.140 +
   1.141 +UStringEnumeration::~UStringEnumeration() {
   1.142 +    uenum_close(uenum);
   1.143 +}
   1.144 +
   1.145 +int32_t UStringEnumeration::count(UErrorCode& status) const {
   1.146 +    return uenum_count(uenum, &status);
   1.147 +}
   1.148 +
   1.149 +const char *UStringEnumeration::next(int32_t *resultLength, UErrorCode &status) {
   1.150 +    return uenum_next(uenum, resultLength, &status);
   1.151 +}
   1.152 +
   1.153 +const UnicodeString* UStringEnumeration::snext(UErrorCode& status) {
   1.154 +    int32_t length;
   1.155 +    const UChar* str = uenum_unext(uenum, &length, &status);
   1.156 +    if (str == 0 || U_FAILURE(status)) {
   1.157 +        return 0;
   1.158 +    }
   1.159 +    return &unistr.setTo(str, length);
   1.160 +}
   1.161 +
   1.162 +void UStringEnumeration::reset(UErrorCode& status) {
   1.163 +    uenum_reset(uenum, &status);
   1.164 +}
   1.165 +
   1.166 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UStringEnumeration)
   1.167 +U_NAMESPACE_END
   1.168 +
   1.169 +// C wrapper --------------------------------------------------------------- ***
   1.170 +
   1.171 +#define THIS(en) ((icu::StringEnumeration*)(en->context))
   1.172 +
   1.173 +U_CDECL_BEGIN
   1.174 +
   1.175 +/**
   1.176 + * Wrapper API to make StringEnumeration look like UEnumeration.
   1.177 + */
   1.178 +static void U_CALLCONV
   1.179 +ustrenum_close(UEnumeration* en) {
   1.180 +    delete THIS(en);
   1.181 +    uprv_free(en);
   1.182 +}
   1.183 +
   1.184 +/**
   1.185 + * Wrapper API to make StringEnumeration look like UEnumeration.
   1.186 + */
   1.187 +static int32_t U_CALLCONV
   1.188 +ustrenum_count(UEnumeration* en,
   1.189 +               UErrorCode* ec)
   1.190 +{
   1.191 +    return THIS(en)->count(*ec);
   1.192 +}
   1.193 +
   1.194 +/**
   1.195 + * Wrapper API to make StringEnumeration look like UEnumeration.
   1.196 + */
   1.197 +static const UChar* U_CALLCONV
   1.198 +ustrenum_unext(UEnumeration* en,
   1.199 +               int32_t* resultLength,
   1.200 +               UErrorCode* ec)
   1.201 +{
   1.202 +    return THIS(en)->unext(resultLength, *ec);
   1.203 +}
   1.204 +
   1.205 +/**
   1.206 + * Wrapper API to make StringEnumeration look like UEnumeration.
   1.207 + */
   1.208 +static const char* U_CALLCONV
   1.209 +ustrenum_next(UEnumeration* en,
   1.210 +              int32_t* resultLength,
   1.211 +              UErrorCode* ec)
   1.212 +{
   1.213 +    return THIS(en)->next(resultLength, *ec);
   1.214 +}
   1.215 +
   1.216 +/**
   1.217 + * Wrapper API to make StringEnumeration look like UEnumeration.
   1.218 + */
   1.219 +static void U_CALLCONV
   1.220 +ustrenum_reset(UEnumeration* en,
   1.221 +               UErrorCode* ec)
   1.222 +{
   1.223 +    THIS(en)->reset(*ec);
   1.224 +}
   1.225 +
   1.226 +/**
   1.227 + * Pseudo-vtable for UEnumeration wrapper around StringEnumeration.
   1.228 + * The StringEnumeration pointer will be stored in 'context'.
   1.229 + */
   1.230 +static const UEnumeration USTRENUM_VT = {
   1.231 +    NULL,
   1.232 +    NULL, // store StringEnumeration pointer here
   1.233 +    ustrenum_close,
   1.234 +    ustrenum_count,
   1.235 +    ustrenum_unext,
   1.236 +    ustrenum_next,
   1.237 +    ustrenum_reset
   1.238 +};
   1.239 +
   1.240 +U_CDECL_END
   1.241 +
   1.242 +/**
   1.243 + * Given a StringEnumeration, wrap it in a UEnumeration.  The
   1.244 + * StringEnumeration is adopted; after this call, the caller must not
   1.245 + * delete it (regardless of error status).
   1.246 + */
   1.247 +U_CAPI UEnumeration* U_EXPORT2
   1.248 +uenum_openFromStringEnumeration(icu::StringEnumeration* adopted, UErrorCode* ec) { 
   1.249 +    UEnumeration* result = NULL;
   1.250 +    if (U_SUCCESS(*ec) && adopted != NULL) {
   1.251 +        result = (UEnumeration*) uprv_malloc(sizeof(UEnumeration));
   1.252 +        if (result == NULL) {
   1.253 +            *ec = U_MEMORY_ALLOCATION_ERROR;
   1.254 +        } else {
   1.255 +            uprv_memcpy(result, &USTRENUM_VT, sizeof(USTRENUM_VT));
   1.256 +            result->context = adopted;
   1.257 +        }
   1.258 +    }
   1.259 +    if (result == NULL) {
   1.260 +        delete adopted;
   1.261 +    }
   1.262 +    return result;
   1.263 +}
   1.264 +
   1.265 +// C wrapper --------------------------------------------------------------- ***
   1.266 +
   1.267 +U_CDECL_BEGIN
   1.268 +
   1.269 +typedef struct UCharStringEnumeration {
   1.270 +    UEnumeration uenum;
   1.271 +    int32_t index, count;
   1.272 +} UCharStringEnumeration;
   1.273 +
   1.274 +static void U_CALLCONV
   1.275 +ucharstrenum_close(UEnumeration* en) {
   1.276 +    uprv_free(en);
   1.277 +}
   1.278 +
   1.279 +static int32_t U_CALLCONV
   1.280 +ucharstrenum_count(UEnumeration* en,
   1.281 +                   UErrorCode* /*ec*/) {
   1.282 +    return ((UCharStringEnumeration*)en)->count;
   1.283 +}
   1.284 +
   1.285 +static const UChar* U_CALLCONV
   1.286 +ucharstrenum_unext(UEnumeration* en,
   1.287 +                  int32_t* resultLength,
   1.288 +                  UErrorCode* /*ec*/) {
   1.289 +    UCharStringEnumeration *e = (UCharStringEnumeration*) en;
   1.290 +    if (e->index >= e->count) {
   1.291 +        return NULL;
   1.292 +    }
   1.293 +    const UChar* result = ((const UChar**)e->uenum.context)[e->index++];
   1.294 +    if (resultLength) {
   1.295 +        *resultLength = (int32_t)u_strlen(result);
   1.296 +    }
   1.297 +    return result;
   1.298 +}
   1.299 +
   1.300 +
   1.301 +static const char* U_CALLCONV
   1.302 +ucharstrenum_next(UEnumeration* en,
   1.303 +                  int32_t* resultLength,
   1.304 +                  UErrorCode* /*ec*/) {
   1.305 +    UCharStringEnumeration *e = (UCharStringEnumeration*) en;
   1.306 +    if (e->index >= e->count) {
   1.307 +        return NULL;
   1.308 +    }
   1.309 +    const char* result = ((const char**)e->uenum.context)[e->index++];
   1.310 +    if (resultLength) {
   1.311 +        *resultLength = (int32_t)uprv_strlen(result);
   1.312 +    }
   1.313 +    return result;
   1.314 +}
   1.315 +
   1.316 +static void U_CALLCONV
   1.317 +ucharstrenum_reset(UEnumeration* en,
   1.318 +                   UErrorCode* /*ec*/) {
   1.319 +    ((UCharStringEnumeration*)en)->index = 0;
   1.320 +}
   1.321 +
   1.322 +static const UEnumeration UCHARSTRENUM_VT = {
   1.323 +    NULL,
   1.324 +    NULL, // store StringEnumeration pointer here
   1.325 +    ucharstrenum_close,
   1.326 +    ucharstrenum_count,
   1.327 +    uenum_unextDefault,
   1.328 +    ucharstrenum_next,
   1.329 +    ucharstrenum_reset
   1.330 +};
   1.331 +
   1.332 +static const UEnumeration UCHARSTRENUM_U_VT = {
   1.333 +    NULL,
   1.334 +    NULL, // store StringEnumeration pointer here
   1.335 +    ucharstrenum_close,
   1.336 +    ucharstrenum_count,
   1.337 +    ucharstrenum_unext,
   1.338 +    uenum_nextDefault,
   1.339 +    ucharstrenum_reset
   1.340 +};
   1.341 +
   1.342 +U_CDECL_END
   1.343 +
   1.344 +U_CAPI UEnumeration* U_EXPORT2
   1.345 +uenum_openCharStringsEnumeration(const char* const strings[], int32_t count,
   1.346 +                                 UErrorCode* ec) {
   1.347 +    UCharStringEnumeration* result = NULL;
   1.348 +    if (U_SUCCESS(*ec) && count >= 0 && (count == 0 || strings != 0)) {
   1.349 +        result = (UCharStringEnumeration*) uprv_malloc(sizeof(UCharStringEnumeration));
   1.350 +        if (result == NULL) {
   1.351 +            *ec = U_MEMORY_ALLOCATION_ERROR;
   1.352 +        } else {
   1.353 +            U_ASSERT((char*)result==(char*)(&result->uenum));
   1.354 +            uprv_memcpy(result, &UCHARSTRENUM_VT, sizeof(UCHARSTRENUM_VT));
   1.355 +            result->uenum.context = (void*)strings;
   1.356 +            result->index = 0;
   1.357 +            result->count = count;
   1.358 +        }
   1.359 +    }
   1.360 +    return (UEnumeration*) result;
   1.361 +}
   1.362 +
   1.363 +U_CAPI UEnumeration* U_EXPORT2
   1.364 +uenum_openUCharStringsEnumeration(const UChar* const strings[], int32_t count,
   1.365 +                                 UErrorCode* ec) {
   1.366 +    UCharStringEnumeration* result = NULL;
   1.367 +    if (U_SUCCESS(*ec) && count >= 0 && (count == 0 || strings != 0)) {
   1.368 +        result = (UCharStringEnumeration*) uprv_malloc(sizeof(UCharStringEnumeration));
   1.369 +        if (result == NULL) {
   1.370 +            *ec = U_MEMORY_ALLOCATION_ERROR;
   1.371 +        } else {
   1.372 +            U_ASSERT((char*)result==(char*)(&result->uenum));
   1.373 +            uprv_memcpy(result, &UCHARSTRENUM_U_VT, sizeof(UCHARSTRENUM_U_VT));
   1.374 +            result->uenum.context = (void*)strings;
   1.375 +            result->index = 0;
   1.376 +            result->count = count;
   1.377 +        }
   1.378 +    }
   1.379 +    return (UEnumeration*) result;
   1.380 +}
   1.381 +
   1.382 +
   1.383 +// end C Wrapper

mercurial