From 678e8ec99ac4ae3d9064ac917463efee1a6839dd Mon Sep 17 00:00:00 2001 From: Alan Liu Date: Tue, 12 Nov 2002 23:44:54 +0000 Subject: [PATCH] ICU-2347 add uenum_openStringEnumeration (private) X-SVN-Rev: 10229 --- icu4c/source/common/Makefile.in | 2 +- icu4c/source/common/common.dsp | 4 ++ icu4c/source/common/ustrenum.cpp | 106 +++++++++++++++++++++++++++++++ icu4c/source/common/ustrenum.h | 27 ++++++++ 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 icu4c/source/common/ustrenum.cpp create mode 100644 icu4c/source/common/ustrenum.h diff --git a/icu4c/source/common/Makefile.in b/icu4c/source/common/Makefile.in index 35ca80d1ed4..e81f2ffbb19 100644 --- a/icu4c/source/common/Makefile.in +++ b/icu4c/source/common/Makefile.in @@ -66,7 +66,7 @@ brkiter.o brkdict.o ubrk.o dbbi.o dbbi_tbl.o \ rbbi.o rbbidata.o rbbinode.o rbbirb.o rbbiscan.o rbbisetb.o rbbistbl.o rbbitblb.o \ unicode.o convert.o utrie.o uset.o cmemory.o caniter.o \ unifilt.o unifunct.o uniset.o usetiter.o util.o uenum.o \ -icuserv.o iculserv.o icunotif.o +icuserv.o iculserv.o icunotif.o ustrenum.cpp STATIC_OBJECTS = $(OBJECTS:.o=.$(STATIC_O)) diff --git a/icu4c/source/common/common.dsp b/icu4c/source/common/common.dsp index 194a98c1620..8c6eb4a6001 100644 --- a/icu4c/source/common/common.dsp +++ b/icu4c/source/common/common.dsp @@ -499,6 +499,10 @@ SOURCE=.\ustrcase.c # End Source File # Begin Source File +SOURCE=.\ustrenum.cpp +# End Source File +# Begin Source File + SOURCE=.\ustrfmt.c # End Source File # Begin Source File diff --git a/icu4c/source/common/ustrenum.cpp b/icu4c/source/common/ustrenum.cpp new file mode 100644 index 00000000000..9407ec9ec27 --- /dev/null +++ b/icu4c/source/common/ustrenum.cpp @@ -0,0 +1,106 @@ +/* +********************************************************************** +* Copyright (c) 2002, 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 "uenumimp.h" +#include "ustrenum.h" +#include "cstring.h" +#include "cmemory.h" + +#define THIS(en) ((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) { + const UChar* result = THIS(en)->unext(*ec); + *resultLength = (result != NULL) ? u_strlen(result) : 0; + return result; +} + +/** + * Wrapper API to make StringEnumeration look like UEnumeration. + */ +static const char* U_CALLCONV +ustrenum_next(UEnumeration* en, int32_t* resultLength, UErrorCode* ec) { + const char* result = THIS(en)->next(*ec); + *resultLength = (result != NULL) ? uprv_strlen(result) : 0; + return result; +} + +/** + * 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 TEMPLATE = { + 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 { + uprv_memcpy(result, &TEMPLATE, sizeof(TEMPLATE)); + result->context = adopted; + } + } + if (result == NULL) { + delete adopted; + } + return result; +} + +//eof diff --git a/icu4c/source/common/ustrenum.h b/icu4c/source/common/ustrenum.h new file mode 100644 index 00000000000..ba3a135f9f4 --- /dev/null +++ b/icu4c/source/common/ustrenum.h @@ -0,0 +1,27 @@ +/* +********************************************************************** +* Copyright (c) 2002, International Business Machines +* Corporation and others. All Rights Reserved. +********************************************************************** +* Author: Alan Liu +* Created: November 11 2002 +* Since: ICU 2.4 +********************************************************************** +*/ +#ifndef _USTRENUM_H_ +#define _USTRENUM_H_ + +#include "unicode/uenum.h" +#include "unicode/strenum.h" + +/** + * 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); + +/* _USTRENUM_H_ */ +#endif +/*eof*/