blob: 8da1bd45937afd94e02484023e5d98f83d8f3e3a [file] [log] [blame]
Fredrik Roubert64339d32016-10-21 19:43:16 +02001// Copyright (C) 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07003/*
4*******************************************************************************
5*
ccornelius59d709d2014-02-20 10:29:46 -08006* Copyright (C) 2002-2013, International Business Machines
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07007* Corporation and others. All Rights Reserved.
8*
9*******************************************************************************
10* file name: uenum.h
11* encoding: US-ASCII
12* tab size: 8 (not used)
13* indentation:2
14*
15* created on: 2002jul08
16* created by: Vladimir Weinstein
17*/
18
19#ifndef __UENUM_H
20#define __UENUM_H
21
22#include "unicode/utypes.h"
claireho50294ea2010-05-03 15:44:48 -070023#include "unicode/localpointer.h"
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070024
claireho50294ea2010-05-03 15:44:48 -070025#if U_SHOW_CPLUSPLUS_API
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -070026#include "unicode/strenum.h"
27#endif
28
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070029/**
30 * \file
31 * \brief C API: String Enumeration
32 */
33
34/**
35 * An enumeration object.
36 * For usage in C programs.
37 * @stable ICU 2.2
38 */
39struct UEnumeration;
40/** structure representing an enumeration object instance @stable ICU 2.2 */
41typedef struct UEnumeration UEnumeration;
42
43/**
44 * Disposes of resources in use by the iterator. If en is NULL,
45 * does nothing. After this call, any char* or UChar* pointer
46 * returned by uenum_unext() or uenum_next() is invalid.
47 * @param en UEnumeration structure pointer
48 * @stable ICU 2.2
49 */
50U_STABLE void U_EXPORT2
51uenum_close(UEnumeration* en);
52
claireho50294ea2010-05-03 15:44:48 -070053#if U_SHOW_CPLUSPLUS_API
54
55U_NAMESPACE_BEGIN
56
57/**
58 * \class LocalUEnumerationPointer
59 * "Smart pointer" class, closes a UEnumeration via uenum_close().
60 * For most methods see the LocalPointerBase base class.
61 *
62 * @see LocalPointerBase
63 * @see LocalPointer
claireho27f65472011-06-09 11:11:49 -070064 * @stable ICU 4.4
claireho50294ea2010-05-03 15:44:48 -070065 */
66U_DEFINE_LOCAL_OPEN_POINTER(LocalUEnumerationPointer, UEnumeration, uenum_close);
67
68U_NAMESPACE_END
69
70#endif
71
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070072/**
73 * Returns the number of elements that the iterator traverses. If
74 * the iterator is out-of-sync with its service, status is set to
75 * U_ENUM_OUT_OF_SYNC_ERROR.
76 * This is a convenience function. It can end up being very
77 * expensive as all the items might have to be pre-fetched (depending
78 * on the type of data being traversed). Use with caution and only
79 * when necessary.
80 * @param en UEnumeration structure pointer
81 * @param status error code, can be U_ENUM_OUT_OF_SYNC_ERROR if the
82 * iterator is out of sync.
83 * @return number of elements in the iterator
84 * @stable ICU 2.2
85 */
86U_STABLE int32_t U_EXPORT2
87uenum_count(UEnumeration* en, UErrorCode* status);
88
89/**
90 * Returns the next element in the iterator's list. If there are
91 * no more elements, returns NULL. If the iterator is out-of-sync
92 * with its service, status is set to U_ENUM_OUT_OF_SYNC_ERROR and
93 * NULL is returned. If the native service string is a char* string,
94 * it is converted to UChar* with the invariant converter.
95 * The result is terminated by (UChar)0.
96 * @param en the iterator object
97 * @param resultLength pointer to receive the length of the result
98 * (not including the terminating \\0).
99 * If the pointer is NULL it is ignored.
100 * @param status the error code, set to U_ENUM_OUT_OF_SYNC_ERROR if
101 * the iterator is out of sync with its service.
102 * @return a pointer to the string. The string will be
103 * zero-terminated. The return pointer is owned by this iterator
104 * and must not be deleted by the caller. The pointer is valid
105 * until the next call to any uenum_... method, including
106 * uenum_next() or uenum_unext(). When all strings have been
107 * traversed, returns NULL.
108 * @stable ICU 2.2
109 */
110U_STABLE const UChar* U_EXPORT2
111uenum_unext(UEnumeration* en,
112 int32_t* resultLength,
113 UErrorCode* status);
114
115/**
116 * Returns the next element in the iterator's list. If there are
117 * no more elements, returns NULL. If the iterator is out-of-sync
118 * with its service, status is set to U_ENUM_OUT_OF_SYNC_ERROR and
119 * NULL is returned. If the native service string is a UChar*
120 * string, it is converted to char* with the invariant converter.
121 * The result is terminated by (char)0. If the conversion fails
122 * (because a character cannot be converted) then status is set to
123 * U_INVARIANT_CONVERSION_ERROR and the return value is undefined
124 * (but non-NULL).
125 * @param en the iterator object
126 * @param resultLength pointer to receive the length of the result
127 * (not including the terminating \\0).
128 * If the pointer is NULL it is ignored.
129 * @param status the error code, set to U_ENUM_OUT_OF_SYNC_ERROR if
130 * the iterator is out of sync with its service. Set to
131 * U_INVARIANT_CONVERSION_ERROR if the underlying native string is
132 * UChar* and conversion to char* with the invariant converter
133 * fails. This error pertains only to current string, so iteration
134 * might be able to continue successfully.
135 * @return a pointer to the string. The string will be
136 * zero-terminated. The return pointer is owned by this iterator
137 * and must not be deleted by the caller. The pointer is valid
138 * until the next call to any uenum_... method, including
139 * uenum_next() or uenum_unext(). When all strings have been
140 * traversed, returns NULL.
141 * @stable ICU 2.2
142 */
143U_STABLE const char* U_EXPORT2
144uenum_next(UEnumeration* en,
145 int32_t* resultLength,
146 UErrorCode* status);
147
148/**
149 * Resets the iterator to the current list of service IDs. This
150 * re-establishes sync with the service and rewinds the iterator
151 * to start at the first element.
152 * @param en the iterator object
153 * @param status the error code, set to U_ENUM_OUT_OF_SYNC_ERROR if
154 * the iterator is out of sync with its service.
155 * @stable ICU 2.2
156 */
157U_STABLE void U_EXPORT2
158uenum_reset(UEnumeration* en, UErrorCode* status);
159
claireho50294ea2010-05-03 15:44:48 -0700160#if U_SHOW_CPLUSPLUS_API
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700161
162/**
163 * Given a StringEnumeration, wrap it in a UEnumeration. The
164 * StringEnumeration is adopted; after this call, the caller must not
165 * delete it (regardless of error status).
166 * @param adopted the C++ StringEnumeration to be wrapped in a UEnumeration.
167 * @param ec the error code.
168 * @return a UEnumeration wrapping the adopted StringEnumeration.
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700169 * @stable ICU 4.2
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700170 */
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800171U_STABLE UEnumeration* U_EXPORT2
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700172uenum_openFromStringEnumeration(icu::StringEnumeration* adopted, UErrorCode* ec);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700173
174#endif
175
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800176/**
177 * Given an array of const UChar* strings, return a UEnumeration. String pointers from 0..count-1 must not be null.
178 * Do not free or modify either the string array or the characters it points to until this object has been destroyed with uenum_close.
179 * \snippet test/cintltst/uenumtst.c uenum_openUCharStringsEnumeration
180 * @param strings array of const UChar* strings (each null terminated). All storage is owned by the caller.
181 * @param count length of the array
182 * @param ec error code
183 * @return the new UEnumeration object. Caller is responsible for calling uenum_close to free memory.
184 * @see uenum_close
ccornelius59d709d2014-02-20 10:29:46 -0800185 * @stable ICU 50
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800186 */
ccornelius59d709d2014-02-20 10:29:46 -0800187U_STABLE UEnumeration* U_EXPORT2
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800188uenum_openUCharStringsEnumeration(const UChar* const strings[], int32_t count,
189 UErrorCode* ec);
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800190
191/* Note: next function is not hidden as draft, as it is used internally (it was formerly an internal function). */
192
193/**
194 * Given an array of const char* strings (invariant chars only), return a UEnumeration. String pointers from 0..count-1 must not be null.
195 * Do not free or modify either the string array or the characters it points to until this object has been destroyed with uenum_close.
196 * \snippet test/cintltst/uenumtst.c uenum_openCharStringsEnumeration
197 * @param strings array of char* strings (each null terminated). All storage is owned by the caller.
198 * @param count length of the array
199 * @param ec error code
200 * @return the new UEnumeration object. Caller is responsible for calling uenum_close to free memory
201 * @see uenum_close
ccornelius59d709d2014-02-20 10:29:46 -0800202 * @stable ICU 50
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800203 */
ccornelius59d709d2014-02-20 10:29:46 -0800204U_STABLE UEnumeration* U_EXPORT2
Craig Cornelius54dcd9b2013-02-15 14:03:14 -0800205uenum_openCharStringsEnumeration(const char* const strings[], int32_t count,
206 UErrorCode* ec);
207
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700208#endif