2002-11-12 23:44:54 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2004-05-18 22:01:41 +00:00
|
|
|
* Copyright (c) 2002-2004, International Business Machines
|
2002-11-12 23:44:54 +00:00
|
|
|
* 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 "uenumimp.h"
|
|
|
|
#include "ustrenum.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "cmemory.h"
|
2004-04-24 02:52:31 +00:00
|
|
|
#include "uassert.h"
|
2002-11-12 23:44:54 +00:00
|
|
|
|
2003-11-05 22:00:21 +00:00
|
|
|
// StringEnumeration implementation ---------------------------------------- ***
|
|
|
|
|
|
|
|
StringEnumeration::StringEnumeration()
|
|
|
|
: chars(charsBuffer), charsCapacity(sizeof(charsBuffer)) {
|
|
|
|
}
|
2002-11-12 23:44:54 +00:00
|
|
|
|
2003-12-10 19:06:20 +00:00
|
|
|
StringEnumeration::~StringEnumeration() {
|
|
|
|
if (chars != NULL && chars != charsBuffer) {
|
|
|
|
uprv_free(chars);
|
|
|
|
}
|
|
|
|
}
|
2003-08-29 16:23:22 +00:00
|
|
|
|
2003-11-05 02:26:49 +00:00
|
|
|
// StringEnumeration base class clone() default implementation, does not clone
|
|
|
|
StringEnumeration *
|
|
|
|
StringEnumeration::clone() const {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-11-05 22:00:21 +00:00
|
|
|
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();
|
|
|
|
}
|
2004-08-26 22:51:40 +00:00
|
|
|
unistr.extract(0, INT32_MAX, chars, charsCapacity, US_INV);
|
2003-11-05 22:00:21 +00:00
|
|
|
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=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;
|
|
|
|
}
|
|
|
|
|
2004-04-24 02:52:31 +00:00
|
|
|
// 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/*, StringEnumeration*/)
|
|
|
|
|
2003-11-05 22:00:21 +00:00
|
|
|
// C wrapper --------------------------------------------------------------- ***
|
|
|
|
|
|
|
|
#define THIS(en) ((StringEnumeration*)(en->context))
|
|
|
|
|
2002-11-12 23:44:54 +00:00
|
|
|
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
|
2002-11-21 23:58:25 +00:00
|
|
|
ustrenum_count(UEnumeration* en,
|
|
|
|
UErrorCode* ec)
|
|
|
|
{
|
2002-11-12 23:44:54 +00:00
|
|
|
return THIS(en)->count(*ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper API to make StringEnumeration look like UEnumeration.
|
|
|
|
*/
|
|
|
|
static const UChar* U_CALLCONV
|
2002-11-21 23:58:25 +00:00
|
|
|
ustrenum_unext(UEnumeration* en,
|
|
|
|
int32_t* resultLength,
|
|
|
|
UErrorCode* ec)
|
|
|
|
{
|
2002-11-22 18:16:02 +00:00
|
|
|
return THIS(en)->unext(resultLength, *ec);
|
2002-11-12 23:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper API to make StringEnumeration look like UEnumeration.
|
|
|
|
*/
|
|
|
|
static const char* U_CALLCONV
|
2002-11-21 23:58:25 +00:00
|
|
|
ustrenum_next(UEnumeration* en,
|
|
|
|
int32_t* resultLength,
|
|
|
|
UErrorCode* ec)
|
|
|
|
{
|
2002-11-22 18:16:02 +00:00
|
|
|
return THIS(en)->next(resultLength, *ec);
|
2002-11-12 23:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper API to make StringEnumeration look like UEnumeration.
|
|
|
|
*/
|
|
|
|
static void U_CALLCONV
|
2002-11-21 23:58:25 +00:00
|
|
|
ustrenum_reset(UEnumeration* en,
|
|
|
|
UErrorCode* ec)
|
|
|
|
{
|
2002-11-12 23:44:54 +00:00
|
|
|
THIS(en)->reset(*ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pseudo-vtable for UEnumeration wrapper around StringEnumeration.
|
|
|
|
* The StringEnumeration pointer will be stored in 'context'.
|
|
|
|
*/
|
2004-04-24 02:52:31 +00:00
|
|
|
static const UEnumeration USTRENUM_VT = {
|
2002-11-12 23:44:54 +00:00
|
|
|
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_openStringEnumeration(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 {
|
2004-04-24 02:52:31 +00:00
|
|
|
uprv_memcpy(result, &USTRENUM_VT, sizeof(USTRENUM_VT));
|
2002-11-12 23:44:54 +00:00
|
|
|
result->context = adopted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result == NULL) {
|
|
|
|
delete adopted;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-04-24 02:52:31 +00:00
|
|
|
// 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,
|
2004-04-27 23:09:25 +00:00
|
|
|
UErrorCode* /*ec*/) {
|
2004-04-24 02:52:31 +00:00
|
|
|
return ((UCharStringEnumeration*)en)->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* U_CALLCONV
|
|
|
|
ucharstrenum_next(UEnumeration* en,
|
|
|
|
int32_t* resultLength,
|
2004-04-27 23:09:25 +00:00
|
|
|
UErrorCode* /*ec*/) {
|
2004-04-24 02:52:31 +00:00
|
|
|
UCharStringEnumeration *e = (UCharStringEnumeration*) en;
|
|
|
|
if (e->index >= e->count) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
const char* result = ((const char**)e->uenum.context)[e->index++];
|
|
|
|
if (resultLength) {
|
|
|
|
*resultLength = uprv_strlen(result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void U_CALLCONV
|
|
|
|
ucharstrenum_reset(UEnumeration* en,
|
2004-04-27 23:09:25 +00:00
|
|
|
UErrorCode* /*ec*/) {
|
2004-04-24 02:52:31 +00:00
|
|
|
((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** 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 = strings;
|
|
|
|
result->index = 0;
|
|
|
|
result->count = count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (UEnumeration*) result;
|
|
|
|
}
|
|
|
|
|