ufmt_cmn.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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) 1998-2011, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * File ufmt_cmn.h
  12. *
  13. * Modification History:
  14. *
  15. * Date Name Description
  16. * 12/02/98 stephen Creation.
  17. * 03/12/99 stephen Modified for new C API.
  18. * 03/15/99 stephen Added defaultCPToUnicode, unicodeToDefaultCP
  19. ******************************************************************************
  20. */
  21. #ifndef UFMT_CMN_H
  22. #define UFMT_CMN_H
  23. #include "unicode/utypes.h"
  24. #include "unicode/utf16.h"
  25. #define UFMT_DEFAULT_BUFFER_SIZE 128
  26. #define MAX_UCHAR_BUFFER_SIZE(buffer) ((int32_t)(sizeof(buffer)/(U16_MAX_LENGTH*sizeof(UChar))))
  27. #define MAX_UCHAR_BUFFER_NEEDED(strLen) ((strLen+1)*U16_MAX_LENGTH*sizeof(UChar))
  28. /**
  29. * Enum representing the possible argument types for uprintf/uscanf
  30. */
  31. typedef enum ufmt_type_info {
  32. ufmt_empty = 0,
  33. ufmt_simple_percent, /* %% do nothing */
  34. ufmt_count, /* special flag for count */
  35. ufmt_int, /* int */
  36. ufmt_char, /* int, cast to char */
  37. ufmt_string, /* char* */
  38. ufmt_pointer, /* void* */
  39. ufmt_float, /* float */
  40. ufmt_double, /* double */
  41. ufmt_uchar, /* int, cast to UChar */
  42. ufmt_ustring /* UChar* */
  43. /*ufmt_wchar,*/ /* wchar_t */
  44. /*ufmt_wstring,*/ /* wchar_t* */
  45. /*ufmt_date,*/ /* Date */
  46. /*ufmt_last*/
  47. } ufmt_type_info;
  48. /**
  49. * Union representing a uprintf/uscanf argument
  50. */
  51. typedef union ufmt_args {
  52. int64_t int64Value; /* int, UChar */
  53. float floatValue; /* float */
  54. double doubleValue; /* double */
  55. void *ptrValue; /* any pointer - void*, char*, wchar_t*, UChar* */
  56. /*wchar_t wcharValue;*/ /* wchar_t */ /* TODO: Should wchar_t be used? */
  57. /*UDate dateValue;*/ /* Date */
  58. } ufmt_args;
  59. /**
  60. * Macro for determining the minimum of two numbers.
  61. * @param a An integer
  62. * @param b An integer
  63. * @return <TT>a</TT> if </TT>a < b</TT>, <TT>b</TT> otherwise
  64. */
  65. #define ufmt_min(a,b) ((a) < (b) ? (a) : (b))
  66. /**
  67. * Convert a UChar in hex radix to an integer value.
  68. * @param c The UChar to convert.
  69. * @return The integer value of <TT>c</TT>.
  70. */
  71. int
  72. ufmt_digitvalue(UChar c);
  73. /**
  74. * Determine if a UChar is a digit for a specified radix.
  75. * @param c The UChar to check.
  76. * @param radix The desired radix.
  77. * @return true if <TT>c</TT> is a digit in <TT>radix</TT>, false otherwise.
  78. */
  79. UBool
  80. ufmt_isdigit(UChar c,
  81. int32_t radix);
  82. /**
  83. * Convert an int64_t to a UChar* in a specified radix
  84. * @param buffer The target buffer
  85. * @param len On input, the size of <TT>buffer</TT>. On output,
  86. * the number of UChars written to <TT>buffer</TT>.
  87. * @param value The value to be converted
  88. * @param radix The desired radix
  89. * @param uselower true means lower case will be used, false means upper case
  90. * @param minDigits The minimum number of digits for for the formatted number,
  91. * which will be padded with zeroes. -1 means do not pad.
  92. */
  93. void
  94. ufmt_64tou(UChar *buffer,
  95. int32_t *len,
  96. uint64_t value,
  97. uint8_t radix,
  98. UBool uselower,
  99. int32_t minDigits);
  100. /**
  101. * It's like ufmt_64tou, but with a pointer.
  102. * This functions avoids size constraints of 64-bit types.
  103. * Pointers can be at 32-128 bits in size.
  104. */
  105. void
  106. ufmt_ptou(UChar *buffer,
  107. int32_t *len,
  108. void *value,
  109. UBool uselower);
  110. /**
  111. * Convert a UChar* in a specified radix to an int64_t.
  112. * @param buffer The target buffer
  113. * @param len On input, the size of <TT>buffer</TT>. On output,
  114. * the number of UChars read from <TT>buffer</TT>.
  115. * @param radix The desired radix
  116. * @return The numeric value.
  117. */
  118. int64_t
  119. ufmt_uto64(const UChar *buffer,
  120. int32_t *len,
  121. int8_t radix);
  122. /**
  123. * Convert a UChar* in a specified radix to a pointer,
  124. * @param buffer The target buffer
  125. * @param len On input, the size of <TT>buffer</TT>. On output,
  126. * the number of UChars read from <TT>buffer</TT>.
  127. * @param radix The desired radix
  128. * @return The pointer value.
  129. */
  130. void *
  131. ufmt_utop(const UChar *buffer,
  132. int32_t *len);
  133. /**
  134. * Convert a string from the default codepage to Unicode.
  135. * @param s The string to convert, in the default codepage.
  136. * @param sSize The size of s to convert.
  137. * @param target The buffer to convert to.
  138. * @param tSize The size of target
  139. * @return A pointer to a newly allocated converted version of s, or 0
  140. * on error.
  141. */
  142. UChar*
  143. ufmt_defaultCPToUnicode(const char *s, int32_t sSize,
  144. UChar *target, int32_t tSize);
  145. #endif