ucnv_cb.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (C) 2000-2004, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. * ucnv_cb.h:
  9. * External APIs for the ICU's codeset conversion library
  10. * Helena Shih
  11. *
  12. * Modification History:
  13. *
  14. * Date Name Description
  15. */
  16. /**
  17. * \file
  18. * \brief C API: UConverter functions to aid the writers of callbacks
  19. *
  20. * <h2> Callback API for UConverter </h2>
  21. *
  22. * These functions are provided here for the convenience of the callback
  23. * writer. If you are just looking for callback functions to use, please
  24. * see ucnv_err.h. DO NOT call these functions directly when you are
  25. * working with converters, unless your code has been called as a callback
  26. * via ucnv_setFromUCallback or ucnv_setToUCallback !!
  27. *
  28. * A note about error codes and overflow. Unlike other ICU functions,
  29. * these functions do not expect the error status to be U_ZERO_ERROR.
  30. * Callbacks must be much more careful about their error codes.
  31. * The error codes used here are in/out parameters, which should be passed
  32. * back in the callback's error parameter.
  33. *
  34. * For example, if you call ucnv_cbfromUWriteBytes to write data out
  35. * to the output codepage, it may return U_BUFFER_OVERFLOW_ERROR if
  36. * the data did not fit in the target. But this isn't a failing error,
  37. * in fact, ucnv_cbfromUWriteBytes may be called AGAIN with the error
  38. * status still U_BUFFER_OVERFLOW_ERROR to attempt to write further bytes,
  39. * which will also go into the internal overflow buffers.
  40. *
  41. * Concerning offsets, the 'offset' parameters here are relative to the start
  42. * of SOURCE. For example, Suppose the string "ABCD" was being converted
  43. * from Unicode into a codepage which doesn't have a mapping for 'B'.
  44. * 'A' will be written out correctly, but
  45. * The FromU Callback will be called on an unassigned character for 'B'.
  46. * At this point, this is the state of the world:
  47. * Target: A [..] [points after A]
  48. * Source: A B [C] D [points to C - B has been consumed]
  49. * 0 1 2 3
  50. * codePoint = "B" [the unassigned codepoint]
  51. *
  52. * Now, suppose a callback wants to write the substitution character '?' to
  53. * the target. It calls ucnv_cbFromUWriteBytes() to write the ?.
  54. * It should pass ZERO as the offset, because the offset as far as the
  55. * callback is concerned is relative to the SOURCE pointer [which points
  56. * before 'C'.] If the callback goes into the args and consumes 'C' also,
  57. * it would call FromUWriteBytes with an offset of 1 (and advance the source
  58. * pointer).
  59. *
  60. */
  61. #ifndef UCNV_CB_H
  62. #define UCNV_CB_H
  63. #include "unicode/utypes.h"
  64. #if !UCONFIG_NO_CONVERSION
  65. #include "unicode/ucnv.h"
  66. #include "unicode/ucnv_err.h"
  67. /**
  68. * ONLY used by FromU callback functions.
  69. * Writes out the specified byte output bytes to the target byte buffer or to converter internal buffers.
  70. *
  71. * @param args callback fromUnicode arguments
  72. * @param source source bytes to write
  73. * @param length length of bytes to write
  74. * @param offsetIndex the relative offset index from callback.
  75. * @param err error status. If <TT>U_BUFFER_OVERFLOW</TT> is returned, then U_BUFFER_OVERFLOW <STRONG>must</STRONG>
  76. * be returned to the user, because it means that not all data could be written into the target buffer, and some is
  77. * in the converter error buffer.
  78. * @see ucnv_cbFromUWriteSub
  79. * @stable ICU 2.0
  80. */
  81. U_CAPI void U_EXPORT2
  82. ucnv_cbFromUWriteBytes (UConverterFromUnicodeArgs *args,
  83. const char* source,
  84. int32_t length,
  85. int32_t offsetIndex,
  86. UErrorCode * err);
  87. /**
  88. * ONLY used by FromU callback functions.
  89. * This function will write out the correct substitution character sequence
  90. * to the target.
  91. *
  92. * @param args callback fromUnicode arguments
  93. * @param offsetIndex the relative offset index from the current source pointer to be used
  94. * @param err error status. If <TT>U_BUFFER_OVERFLOW</TT> is returned, then U_BUFFER_OVERFLOW <STRONG>must</STRONG>
  95. * be returned to the user, because it means that not all data could be written into the target buffer, and some is
  96. * in the converter error buffer.
  97. * @see ucnv_cbFromUWriteBytes
  98. * @stable ICU 2.0
  99. */
  100. U_CAPI void U_EXPORT2
  101. ucnv_cbFromUWriteSub (UConverterFromUnicodeArgs *args,
  102. int32_t offsetIndex,
  103. UErrorCode * err);
  104. /**
  105. * ONLY used by fromU callback functions.
  106. * This function will write out the error character(s) to the target UChar buffer.
  107. *
  108. * @param args callback fromUnicode arguments
  109. * @param source pointer to pointer to first UChar to write [on exit: 1 after last UChar processed]
  110. * @param sourceLimit pointer after last UChar to write
  111. * @param offsetIndex the relative offset index from callback which will be set
  112. * @param err error status <TT>U_BUFFER_OVERFLOW</TT>
  113. * @see ucnv_cbToUWriteSub
  114. * @stable ICU 2.0
  115. */
  116. U_CAPI void U_EXPORT2 ucnv_cbFromUWriteUChars(UConverterFromUnicodeArgs *args,
  117. const UChar** source,
  118. const UChar* sourceLimit,
  119. int32_t offsetIndex,
  120. UErrorCode * err);
  121. /**
  122. * ONLY used by ToU callback functions.
  123. * This function will write out the specified characters to the target
  124. * UChar buffer.
  125. *
  126. * @param args callback toUnicode arguments
  127. * @param source source string to write
  128. * @param length the length of source string
  129. * @param offsetIndex the relative offset index which will be written.
  130. * @param err error status <TT>U_BUFFER_OVERFLOW</TT>
  131. * @see ucnv_cbToUWriteSub
  132. * @stable ICU 2.0
  133. */
  134. U_CAPI void U_EXPORT2 ucnv_cbToUWriteUChars (UConverterToUnicodeArgs *args,
  135. const UChar* source,
  136. int32_t length,
  137. int32_t offsetIndex,
  138. UErrorCode * err);
  139. /**
  140. * ONLY used by ToU callback functions.
  141. * This function will write out the Unicode substitution character (U+FFFD).
  142. *
  143. * @param args callback fromUnicode arguments
  144. * @param offsetIndex the relative offset index from callback.
  145. * @param err error status <TT>U_BUFFER_OVERFLOW</TT>
  146. * @see ucnv_cbToUWriteUChars
  147. * @stable ICU 2.0
  148. */
  149. U_CAPI void U_EXPORT2 ucnv_cbToUWriteSub (UConverterToUnicodeArgs *args,
  150. int32_t offsetIndex,
  151. UErrorCode * err);
  152. #endif
  153. #endif