uenum.h 7.8 KB

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