]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/ustrenum.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / common / ustrenum.cpp
CommitLineData
b75a7d8f
A
1/*
2**********************************************************************
3* Copyright (c) 2002, International Business Machines
4* Corporation and others. All Rights Reserved.
5**********************************************************************
6* Author: Alan Liu
7* Created: November 11 2002
8* Since: ICU 2.4
9**********************************************************************
10*/
11#include "unicode/ustring.h"
12#include "unicode/strenum.h"
13#include "uenumimp.h"
14#include "ustrenum.h"
15#include "cstring.h"
16#include "cmemory.h"
17
18#define THIS(en) ((StringEnumeration*)(en->context))
19
20U_CDECL_BEGIN
21
22/**
23 * Wrapper API to make StringEnumeration look like UEnumeration.
24 */
25static void U_CALLCONV
26ustrenum_close(UEnumeration* en) {
27 delete THIS(en);
28 uprv_free(en);
29}
30
31/**
32 * Wrapper API to make StringEnumeration look like UEnumeration.
33 */
34static int32_t U_CALLCONV
35ustrenum_count(UEnumeration* en,
36 UErrorCode* ec)
37{
38 return THIS(en)->count(*ec);
39}
40
41/**
42 * Wrapper API to make StringEnumeration look like UEnumeration.
43 */
44static const UChar* U_CALLCONV
45ustrenum_unext(UEnumeration* en,
46 int32_t* resultLength,
47 UErrorCode* ec)
48{
49 return THIS(en)->unext(resultLength, *ec);
50}
51
52/**
53 * Wrapper API to make StringEnumeration look like UEnumeration.
54 */
55static const char* U_CALLCONV
56ustrenum_next(UEnumeration* en,
57 int32_t* resultLength,
58 UErrorCode* ec)
59{
60 return THIS(en)->next(resultLength, *ec);
61}
62
63/**
64 * Wrapper API to make StringEnumeration look like UEnumeration.
65 */
66static void U_CALLCONV
67ustrenum_reset(UEnumeration* en,
68 UErrorCode* ec)
69{
70 THIS(en)->reset(*ec);
71}
72
73/**
74 * Pseudo-vtable for UEnumeration wrapper around StringEnumeration.
75 * The StringEnumeration pointer will be stored in 'context'.
76 */
77static const UEnumeration TEMPLATE = {
78 NULL,
79 NULL, // store StringEnumeration pointer here
80 ustrenum_close,
81 ustrenum_count,
82 ustrenum_unext,
83 ustrenum_next,
84 ustrenum_reset
85};
86
87U_CDECL_END
88
89/**
90 * Given a StringEnumeration, wrap it in a UEnumeration. The
91 * StringEnumeration is adopted; after this call, the caller must not
92 * delete it (regardless of error status).
93 */
94U_CAPI UEnumeration* U_EXPORT2
95uenum_openStringEnumeration(StringEnumeration* adopted, UErrorCode* ec) {
96 UEnumeration* result = NULL;
97 if (U_SUCCESS(*ec) && adopted != NULL) {
98 result = (UEnumeration*) uprv_malloc(sizeof(UEnumeration));
99 if (result == NULL) {
100 *ec = U_MEMORY_ALLOCATION_ERROR;
101 } else {
102 uprv_memcpy(result, &TEMPLATE, sizeof(TEMPLATE));
103 result->context = adopted;
104 }
105 }
106 if (result == NULL) {
107 delete adopted;
108 }
109 return result;
110}
111
112//eof