uClibc_uintmaxtostr.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (C) 2003 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * The GNU C Library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with the GNU C Library; if not, write to the Free
  15. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. * 02111-1307 USA.
  17. */
  18. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  19. *
  20. * This code is currently under development. Also, I plan to port
  21. * it to elks which is a 16-bit environment with a fairly limited
  22. * compiler. Therefore, please refrain from modifying this code
  23. * and, instead, pass any bug-fixes, etc. to me. Thanks. Manuel
  24. *
  25. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  26. #ifndef _UINTMAXTOSTR_H
  27. #define _UINTMAXTOSTR_H 1
  28. #ifdef _FEATURES_H
  29. # ifndef __USE_ISOC99
  30. # error features was included without defining _ISOC99_SOURCE!
  31. # endif
  32. #else
  33. # ifndef _ISOC99_SOURCE
  34. # define _ISOC99_SOURCE
  35. # endif
  36. #endif
  37. #include <features.h>
  38. #include <limits.h>
  39. #include <stdint.h>
  40. #if INTMAX_MAX <= 2147483647L
  41. #define __UIM_BUFLEN 12 /* 10 digits + 1 nul + 1 sign */
  42. #elif INTMAX_MAX <= 9223372036854775807LL
  43. #define __UIM_BUFLEN 22 /* 20 digits + 1 nul + 1 sign */
  44. #else
  45. #error unknown number of digits for intmax_t!
  46. #endif
  47. #ifdef LLONG_MAX /* --------------- */
  48. #if LLONG_MAX <= 2147483647L
  49. #define __UIM_BUFLEN_LLONG 12 /* 10 digits + 1 nul + 1 sign */
  50. #elif LLONG_MAX <= 9223372036854775807LL
  51. #define __UIM_BUFLEN_LLONG 22 /* 20 digits + 1 nul + 1 sign */
  52. #else
  53. #error unknown number of digits for long long!
  54. #endif
  55. #endif /* ULLONG_MAX ----------------------------- */
  56. #if LONG_MAX <= 2147483647L
  57. #define __UIM_BUFLEN_LONG 12 /* 10 digits + 1 nul + 1 sign */
  58. #elif LONG_MAX <= 9223372036854775807LL
  59. #define __UIM_BUFLEN_LONG 22 /* 20 digits + 1 nul + 1 sign */
  60. #else
  61. #error unknown number of digits for long!
  62. #endif
  63. #if INT_MAX <= 32767
  64. #define __UIM_BUFLEN_INT 7 /* 10 digits + 1 nul + 1 sign */
  65. #elif INT_MAX <= 2147483647L
  66. #define __UIM_BUFLEN_INT 12 /* 10 digits + 1 nul + 1 sign */
  67. #else
  68. #error unknown number of digits for int!
  69. #endif
  70. typedef enum {
  71. __UIM_DECIMAL = 0,
  72. __UIM_GROUP = ',', /* Base 10 locale-dependent grouping. */
  73. __UIM_LOWER = 'a' - 10,
  74. __UIM_UPPER = 'A' - 10,
  75. } __UIM_CASE;
  76. /* Convert the int val to a string in base abs(base). val is treated as
  77. * an unsigned ??? int type if base > 0, and signed if base < 0. This
  78. * is an internal function with _no_ error checking done unless assert()s
  79. * are enabled.
  80. *
  81. * Note: bufend is a pointer to the END of the buffer passed.
  82. * Call like this:
  83. * char buf[SIZE], *p;
  84. * p = _xltostr(buf + sizeof(buf) - 1, {unsigned int}, 10, __UIM_DECIMAL)
  85. * p = _xltostr(buf + sizeof(buf) - 1, {int}, -10, __UIM_DECIMAL)
  86. *
  87. * WARNING: If base > 10, case _must_be_ either __UIM_LOWER or __UIM_UPPER
  88. * for lower and upper case alphas respectively.
  89. * WARNING: If val is really a signed type, make sure base is negative!
  90. * Otherwise, you could overflow your buffer.
  91. */
  92. extern char *_uintmaxtostr(char * __restrict bufend, uintmax_t uval,
  93. int base, __UIM_CASE alphacase) attribute_hidden;
  94. /* TODO -- make this either a (possibly inline) function? */
  95. #ifndef __BCC__
  96. #define _int10tostr(bufend, intval) \
  97. _uintmaxtostr((bufend), (intval), -10, __UIM_DECIMAL)
  98. #else /* bcc doesn't do prototypes, we need to explicitly cast */
  99. #define _int10tostr(bufend, intval) \
  100. _uintmaxtostr((bufend), (uintmax_t)(intval), -10, __UIM_DECIMAL)
  101. #endif
  102. #define __BUFLEN_INT10TOSTR __UIM_BUFLEN_INT
  103. #endif /* _UINTMAXTOSTR_H */