1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/*
**********************************************************************
* Copyright (c) 2002-2009, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* Author: Alan Liu
* Created: November 11 2002
* Since: ICU 2.4
**********************************************************************
*/
#include "unicode/ustring.h"
#include "unicode/strenum.h"
#include "unicode/putil.h"
#include "uenumimp.h"
#include "ustrenum.h"
#include "cstring.h"
#include "cmemory.h"
#include "uassert.h"
U_NAMESPACE_BEGIN
// StringEnumeration implementation ---------------------------------------- ***
StringEnumeration::StringEnumeration()
: chars(charsBuffer), charsCapacity(sizeof(charsBuffer)) {
}
StringEnumeration::~StringEnumeration() {
if (chars != NULL && chars != charsBuffer) {
uprv_free(chars);
}
}
// StringEnumeration base class clone() default implementation, does not clone
StringEnumeration *
StringEnumeration::clone() const {
return NULL;
}
const char *
StringEnumeration::next(int32_t *resultLength, UErrorCode &status) {
const UnicodeString *s=snext(status);
if(s!=NULL) {
unistr=*s;
ensureCharsCapacity(unistr.length()+1, status);
if(U_SUCCESS(status)) {
if(resultLength!=NULL) {
*resultLength=unistr.length();
}
unistr.extract(0, INT32_MAX, chars, charsCapacity, US_INV);
return chars;
}
}
return NULL;
}
const UChar *
StringEnumeration::unext(int32_t *resultLength, UErrorCode &status) {
const UnicodeString *s=snext(status);
if(s!=NULL) {
unistr=*s;
if(U_SUCCESS(status)) {
if(resultLength!=NULL) {
*resultLength=unistr.length();
}
return unistr.getTerminatedBuffer();
}
}
return NULL;
}
void
StringEnumeration::ensureCharsCapacity(int32_t capacity, UErrorCode &status) {
if(U_SUCCESS(status) && capacity>charsCapacity) {
if(capacity<(charsCapacity+charsCapacity/2)) {
// avoid allocation thrashing
capacity=charsCapacity+charsCapacity/2;
}
if(chars!=charsBuffer) {
uprv_free(chars);
}
chars=(char *)uprv_malloc(capacity);
if(chars==NULL) {
chars=charsBuffer;
charsCapacity=sizeof(charsBuffer);
status=U_MEMORY_ALLOCATION_ERROR;
} else {
charsCapacity=capacity;
}
}
}
UnicodeString *
StringEnumeration::setChars(const char *s, int32_t length, UErrorCode &status) {
if(U_SUCCESS(status) && s!=NULL) {
if(length<0) {
length=(int32_t)uprv_strlen(s);
}
UChar *buffer=unistr.getBuffer(length+1);
if(buffer!=NULL) {
u_charsToUChars(s, buffer, length);
buffer[length]=0;
unistr.releaseBuffer(length);
return &unistr;
} else {
status=U_MEMORY_ALLOCATION_ERROR;
}
}
return NULL;
}
UBool
StringEnumeration::operator==(const StringEnumeration& that)const {
return getDynamicClassID() == that.getDynamicClassID();
}
UBool
StringEnumeration::operator!=(const StringEnumeration& that)const {
return !operator==(that);
}
// UStringEnumeration implementation --------------------------------------- ***
UStringEnumeration::UStringEnumeration(UEnumeration* _uenum) :
uenum(_uenum) {
U_ASSERT(_uenum != 0);
}
UStringEnumeration::~UStringEnumeration() {
uenum_close(uenum);
}
int32_t UStringEnumeration::count(UErrorCode& status) const {
return uenum_count(uenum, &status);
}
const UnicodeString* UStringEnumeration::snext(UErrorCode& status) {
int32_t length;
const UChar* str = uenum_unext(uenum, &length, &status);
if (str == 0 || U_FAILURE(status)) {
return 0;
}
return &unistr.setTo(str, length);
}
void UStringEnumeration::reset(UErrorCode& status) {
uenum_reset(uenum, &status);
}
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UStringEnumeration)
U_NAMESPACE_END
// C wrapper --------------------------------------------------------------- ***
#define THIS(en) ((U_NAMESPACE_QUALIFIER StringEnumeration*)(en->context))
U_CDECL_BEGIN
/**
* Wrapper API to make StringEnumeration look like UEnumeration.
*/
static void U_CALLCONV
ustrenum_close(UEnumeration* en) {
delete THIS(en);
uprv_free(en);
}
/**
* Wrapper API to make StringEnumeration look like UEnumeration.
*/
static int32_t U_CALLCONV
ustrenum_count(UEnumeration* en,
UErrorCode* ec)
{
return THIS(en)->count(*ec);
}
/**
* Wrapper API to make StringEnumeration look like UEnumeration.
*/
static const UChar* U_CALLCONV
ustrenum_unext(UEnumeration* en,
int32_t* resultLength,
UErrorCode* ec)
{
return THIS(en)->unext(resultLength, *ec);
}
/**
* Wrapper API to make StringEnumeration look like UEnumeration.
*/
static const char* U_CALLCONV
ustrenum_next(UEnumeration* en,
int32_t* resultLength,
UErrorCode* ec)
{
return THIS(en)->next(resultLength, *ec);
}
/**
* Wrapper API to make StringEnumeration look like UEnumeration.
*/
static void U_CALLCONV
ustrenum_reset(UEnumeration* en,
UErrorCode* ec)
{
THIS(en)->reset(*ec);
}
/**
* Pseudo-vtable for UEnumeration wrapper around StringEnumeration.
* The StringEnumeration pointer will be stored in 'context'.
*/
static const UEnumeration USTRENUM_VT = {
NULL,
NULL, // store StringEnumeration pointer here
ustrenum_close,
ustrenum_count,
ustrenum_unext,
ustrenum_next,
ustrenum_reset
};
U_CDECL_END
/**
* Given a StringEnumeration, wrap it in a UEnumeration. The
* StringEnumeration is adopted; after this call, the caller must not
* delete it (regardless of error status).
*/
U_CAPI UEnumeration* U_EXPORT2
uenum_openFromStringEnumeration(U_NAMESPACE_QUALIFIER StringEnumeration* adopted, UErrorCode* ec) {
UEnumeration* result = NULL;
if (U_SUCCESS(*ec) && adopted != NULL) {
result = (UEnumeration*) uprv_malloc(sizeof(UEnumeration));
if (result == NULL) {
*ec = U_MEMORY_ALLOCATION_ERROR;
} else {
uprv_memcpy(result, &USTRENUM_VT, sizeof(USTRENUM_VT));
result->context = adopted;
}
}
if (result == NULL) {
delete adopted;
}
return result;
}
// C wrapper --------------------------------------------------------------- ***
U_CDECL_BEGIN
typedef struct UCharStringEnumeration {
UEnumeration uenum;
int32_t index, count;
} UCharStringEnumeration;
static void U_CALLCONV
ucharstrenum_close(UEnumeration* en) {
uprv_free(en);
}
static int32_t U_CALLCONV
ucharstrenum_count(UEnumeration* en,
UErrorCode* /*ec*/) {
return ((UCharStringEnumeration*)en)->count;
}
static const char* U_CALLCONV
ucharstrenum_next(UEnumeration* en,
int32_t* resultLength,
UErrorCode* /*ec*/) {
UCharStringEnumeration *e = (UCharStringEnumeration*) en;
if (e->index >= e->count) {
return NULL;
}
const char* result = ((const char**)e->uenum.context)[e->index++];
if (resultLength) {
*resultLength = (int32_t)uprv_strlen(result);
}
return result;
}
static void U_CALLCONV
ucharstrenum_reset(UEnumeration* en,
UErrorCode* /*ec*/) {
((UCharStringEnumeration*)en)->index = 0;
}
static const UEnumeration UCHARSTRENUM_VT = {
NULL,
NULL, // store StringEnumeration pointer here
ucharstrenum_close,
ucharstrenum_count,
uenum_unextDefault,
ucharstrenum_next,
ucharstrenum_reset
};
U_CDECL_END
U_CAPI UEnumeration* U_EXPORT2
uenum_openCharStringsEnumeration(const char* const* strings, int32_t count,
UErrorCode* ec) {
UCharStringEnumeration* result = NULL;
if (U_SUCCESS(*ec) && count >= 0 && (count == 0 || strings != 0)) {
result = (UCharStringEnumeration*) uprv_malloc(sizeof(UCharStringEnumeration));
if (result == NULL) {
*ec = U_MEMORY_ALLOCATION_ERROR;
} else {
U_ASSERT((char*)result==(char*)(&result->uenum));
uprv_memcpy(result, &UCHARSTRENUM_VT, sizeof(UCHARSTRENUM_VT));
result->uenum.context = (void*)strings;
result->index = 0;
result->count = count;
}
}
return (UEnumeration*) result;
}
|