uenum.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (C) 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. *
  6. * Copyright (C) 2002-2013, International Business Machines
  7. * 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. #ifndef __UENUM_H
  19. #define __UENUM_H
  20. #include "unicode/utypes.h"
  21. #include "unicode/localpointer.h"
  22. #if U_SHOW_CPLUSPLUS_API
  23. #include "unicode/strenum.h"
  24. #endif
  25. /**
  26. * \file
  27. * \brief C API: String Enumeration
  28. */
  29. /**
  30. * An enumeration object.
  31. * For usage in C programs.
  32. * @stable ICU 2.2
  33. */
  34. struct UEnumeration;
  35. /** structure representing an enumeration object instance @stable ICU 2.2 */
  36. typedef struct UEnumeration UEnumeration;
  37. /**
  38. * Disposes of resources in use by the iterator. If en is NULL,
  39. * does nothing. After this call, any char* or UChar* pointer
  40. * returned by uenum_unext() or uenum_next() is invalid.
  41. * @param en UEnumeration structure pointer
  42. * @stable ICU 2.2
  43. */
  44. U_STABLE void U_EXPORT2
  45. uenum_close(UEnumeration* en);
  46. #if U_SHOW_CPLUSPLUS_API
  47. U_NAMESPACE_BEGIN
  48. /**
  49. * \class LocalUEnumerationPointer
  50. * "Smart pointer" class, closes a UEnumeration via uenum_close().
  51. * For most methods see the LocalPointerBase base class.
  52. *
  53. * @see LocalPointerBase
  54. * @see LocalPointer
  55. * @stable ICU 4.4
  56. */
  57. U_DEFINE_LOCAL_OPEN_POINTER(LocalUEnumerationPointer, UEnumeration, uenum_close);
  58. U_NAMESPACE_END
  59. #endif
  60. /**
  61. * Returns the number of elements that the iterator traverses. If
  62. * the iterator is out-of-sync with its service, status is set to
  63. * U_ENUM_OUT_OF_SYNC_ERROR.
  64. * This is a convenience function. It can end up being very
  65. * expensive as all the items might have to be pre-fetched (depending
  66. * on the type of data being traversed). Use with caution and only
  67. * when necessary.
  68. * @param en UEnumeration structure pointer
  69. * @param status error code, can be U_ENUM_OUT_OF_SYNC_ERROR if the
  70. * iterator is out of sync.
  71. * @return number of elements in the iterator
  72. * @stable ICU 2.2
  73. */
  74. U_STABLE int32_t U_EXPORT2
  75. uenum_count(UEnumeration* en, UErrorCode* status);
  76. /**
  77. * Returns the next element in the iterator's list. If there are
  78. * no more elements, returns NULL. If the iterator is out-of-sync
  79. * with its service, status is set to U_ENUM_OUT_OF_SYNC_ERROR and
  80. * NULL is returned. If the native service string is a char* string,
  81. * it is converted to UChar* with the invariant converter.
  82. * The result is terminated by (UChar)0.
  83. * @param en the iterator object
  84. * @param resultLength pointer to receive the length of the result
  85. * (not including the terminating \\0).
  86. * If the pointer is NULL it is ignored.
  87. * @param status the error code, set to U_ENUM_OUT_OF_SYNC_ERROR if
  88. * the iterator is out of sync with its service.
  89. * @return a pointer to the string. The string will be
  90. * zero-terminated. The return pointer is owned by this iterator
  91. * and must not be deleted by the caller. The pointer is valid
  92. * until the next call to any uenum_... method, including
  93. * uenum_next() or uenum_unext(). When all strings have been
  94. * traversed, returns NULL.
  95. * @stable ICU 2.2
  96. */
  97. U_STABLE const UChar* U_EXPORT2
  98. uenum_unext(UEnumeration* en,
  99. int32_t* resultLength,
  100. UErrorCode* status);
  101. /**
  102. * Returns the next element in the iterator's list. If there are
  103. * no more elements, returns NULL. If the iterator is out-of-sync
  104. * with its service, status is set to U_ENUM_OUT_OF_SYNC_ERROR and
  105. * NULL is returned. If the native service string is a UChar*
  106. * string, it is converted to char* with the invariant converter.
  107. * The result is terminated by (char)0. If the conversion fails
  108. * (because a character cannot be converted) then status is set to
  109. * U_INVARIANT_CONVERSION_ERROR and the return value is undefined
  110. * (but non-NULL).
  111. * @param en the iterator object
  112. * @param resultLength pointer to receive the length of the result
  113. * (not including the terminating \\0).
  114. * If the pointer is NULL it is ignored.
  115. * @param status the error code, set to U_ENUM_OUT_OF_SYNC_ERROR if
  116. * the iterator is out of sync with its service. Set to
  117. * U_INVARIANT_CONVERSION_ERROR if the underlying native string is
  118. * UChar* and conversion to char* with the invariant converter
  119. * fails. This error pertains only to current string, so iteration
  120. * might be able to continue successfully.
  121. * @return a pointer to the string. The string will be
  122. * zero-terminated. The return pointer is owned by this iterator
  123. * and must not be deleted by the caller. The pointer is valid
  124. * until the next call to any uenum_... method, including
  125. * uenum_next() or uenum_unext(). When all strings have been
  126. * traversed, returns NULL.
  127. * @stable ICU 2.2
  128. */
  129. U_STABLE const char* U_EXPORT2
  130. uenum_next(UEnumeration* en,
  131. int32_t* resultLength,
  132. UErrorCode* status);
  133. /**
  134. * Resets the iterator to the current list of service IDs. This
  135. * re-establishes sync with the service and rewinds the iterator
  136. * to start at the first element.
  137. * @param en the iterator object
  138. * @param status the error code, set to U_ENUM_OUT_OF_SYNC_ERROR if
  139. * the iterator is out of sync with its service.
  140. * @stable ICU 2.2
  141. */
  142. U_STABLE void U_EXPORT2
  143. uenum_reset(UEnumeration* en, UErrorCode* status);
  144. #if U_SHOW_CPLUSPLUS_API
  145. /**
  146. * Given a StringEnumeration, wrap it in a UEnumeration. The
  147. * StringEnumeration is adopted; after this call, the caller must not
  148. * delete it (regardless of error status).
  149. * @param adopted the C++ StringEnumeration to be wrapped in a UEnumeration.
  150. * @param ec the error code.
  151. * @return a UEnumeration wrapping the adopted StringEnumeration.
  152. * @stable ICU 4.2
  153. */
  154. U_STABLE UEnumeration* U_EXPORT2
  155. uenum_openFromStringEnumeration(icu::StringEnumeration* adopted, UErrorCode* ec);
  156. #endif
  157. /**
  158. * Given an array of const UChar* strings, return a UEnumeration. String pointers from 0..count-1 must not be null.
  159. * Do not free or modify either the string array or the characters it points to until this object has been destroyed with uenum_close.
  160. * \snippet test/cintltst/uenumtst.c uenum_openUCharStringsEnumeration
  161. * @param strings array of const UChar* strings (each null terminated). All storage is owned by the caller.
  162. * @param count length of the array
  163. * @param ec error code
  164. * @return the new UEnumeration object. Caller is responsible for calling uenum_close to free memory.
  165. * @see uenum_close
  166. * @stable ICU 50
  167. */
  168. U_STABLE UEnumeration* U_EXPORT2
  169. uenum_openUCharStringsEnumeration(const UChar* const strings[], int32_t count,
  170. UErrorCode* ec);
  171. /* Note: next function is not hidden as draft, as it is used internally (it was formerly an internal function). */
  172. /**
  173. * Given an array of const char* strings (invariant chars only), return a UEnumeration. String pointers from 0..count-1 must not be null.
  174. * Do not free or modify either the string array or the characters it points to until this object has been destroyed with uenum_close.
  175. * \snippet test/cintltst/uenumtst.c uenum_openCharStringsEnumeration
  176. * @param strings array of char* strings (each null terminated). All storage is owned by the caller.
  177. * @param count length of the array
  178. * @param ec error code
  179. * @return the new UEnumeration object. Caller is responsible for calling uenum_close to free memory
  180. * @see uenum_close
  181. * @stable ICU 50
  182. */
  183. U_STABLE UEnumeration* U_EXPORT2
  184. uenum_openCharStringsEnumeration(const char* const strings[], int32_t count,
  185. UErrorCode* ec);
  186. #endif