quadmath-printf.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* GCC Quad-Precision Math Library
  2. Copyright (C) 2011 Free Software Foundation, Inc.
  3. Written by Jakub Jelinek <jakub@redhat.com>
  4. This file is part of the libquadmath library.
  5. Libquadmath is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. Libquadmath is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with libquadmath; see the file COPYING.LIB. If
  15. not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #ifdef HAVE_LIMITS_H
  20. #include <limits.h>
  21. #endif
  22. #ifdef HAVE_LANGINFO_H
  23. #include <langinfo.h>
  24. #endif
  25. #ifdef HAVE_CTYPE_H
  26. #include <ctype.h>
  27. #endif
  28. #ifdef HAVE_WCHAR_H
  29. #include <wchar.h>
  30. #endif
  31. #ifdef HAVE_WCTYPE_H
  32. #include <wctype.h>
  33. #endif
  34. #ifdef HAVE_PRINTF_HOOKS
  35. #include <printf.h>
  36. #endif
  37. #ifdef HAVE_LOCALE_H
  38. #include <locale.h>
  39. #endif
  40. #include "quadmath-imp.h"
  41. #include "gmp-impl.h"
  42. #ifdef HAVE_WCHAR_H
  43. #define L_(x) L##x
  44. #else
  45. #define L_(x) x
  46. #undef wchar_t
  47. #undef wint_t
  48. #undef putwc
  49. #undef WEOF
  50. #define wchar_t char
  51. #define wint_t int
  52. #define putwc(c,f) putc(c,f)
  53. #define WEOF EOF
  54. #endif
  55. #ifndef HAVE_CTYPE_H
  56. /* Won't work for EBCDIC. */
  57. #undef isupper
  58. #undef isdigit
  59. #undef isxdigit
  60. #undef tolower
  61. #define isupper(x) \
  62. ({__typeof(x) __is_x = (x); __is_x >= 'A' && __is_x <= 'Z'; })
  63. #define isdigit(x) \
  64. ({__typeof(x) __is_x = (x); __is_x >= '0' && __is_x <= '9'; })
  65. #define isxdigit(x) \
  66. ({__typeof(x) __is_x = (x); \
  67. (__is_x >= '0' && __is_x <= '9') \
  68. || ((x) >= 'A' && (x) <= 'F') \
  69. || ((x) >= 'a' && (x) <= 'f'); })
  70. #define tolower(x) \
  71. ({__typeof(x) __is_x = (x); \
  72. (__is_x >= 'A' && __is_x <= 'Z') ? __is_x - 'A' + 'a' : __is_x; })
  73. #endif
  74. #ifndef CHAR_MAX
  75. #ifdef __CHAR_UNSIGNED__
  76. #define CHAR_MAX (2 * __SCHAR_MAX__ + 1)
  77. #else
  78. #define CHAR_MAX __SCHAR_MAX__
  79. #endif
  80. #endif
  81. #ifndef HAVE_PRINTF_HOOKS
  82. #define printf_info __quadmath_printf_info
  83. struct printf_info
  84. {
  85. int prec; /* Precision. */
  86. int width; /* Width. */
  87. wchar_t spec; /* Format letter. */
  88. unsigned int is_long_double:1;/* L flag. */
  89. unsigned int is_short:1; /* h flag. */
  90. unsigned int is_long:1; /* l flag. */
  91. unsigned int alt:1; /* # flag. */
  92. unsigned int space:1; /* Space flag. */
  93. unsigned int left:1; /* - flag. */
  94. unsigned int showsign:1; /* + flag. */
  95. unsigned int group:1; /* ' flag. */
  96. unsigned int extra:1; /* For special use. */
  97. unsigned int is_char:1; /* hh flag. */
  98. unsigned int wide:1; /* Nonzero for wide character streams. */
  99. unsigned int i18n:1; /* I flag. */
  100. unsigned short int user; /* Bits for user-installed modifiers. */
  101. wchar_t pad; /* Padding character. */
  102. };
  103. #endif
  104. struct __quadmath_printf_file
  105. {
  106. FILE *fp;
  107. char *str;
  108. size_t size;
  109. size_t len;
  110. int file_p;
  111. };
  112. int
  113. __quadmath_printf_fp (struct __quadmath_printf_file *fp,
  114. const struct printf_info *info,
  115. const void *const *args) attribute_hidden;
  116. int
  117. __quadmath_printf_fphex (struct __quadmath_printf_file *fp,
  118. const struct printf_info *info,
  119. const void *const *args) attribute_hidden;
  120. size_t __quadmath_do_pad (struct __quadmath_printf_file *fp, int wide,
  121. int c, size_t n) attribute_hidden;
  122. static inline __attribute__((__unused__)) size_t
  123. __quadmath_do_put (struct __quadmath_printf_file *fp, int wide,
  124. const char *s, size_t n)
  125. {
  126. size_t len;
  127. if (fp->file_p)
  128. {
  129. if (wide)
  130. {
  131. size_t cnt;
  132. const wchar_t *ls = (const wchar_t *) s;
  133. for (cnt = 0; cnt < n; cnt++)
  134. if (putwc (ls[cnt], fp->fp) == WEOF)
  135. break;
  136. return cnt;
  137. }
  138. return fwrite (s, 1, n, fp->fp);
  139. }
  140. len = MIN (fp->size, n);
  141. memcpy (fp->str, s, len);
  142. fp->str += len;
  143. fp->size -= len;
  144. fp->len += n;
  145. return n;
  146. }
  147. static inline __attribute__((__unused__)) int
  148. __quadmath_do_putc (struct __quadmath_printf_file *fp, int wide,
  149. wchar_t c)
  150. {
  151. if (fp->file_p)
  152. return wide ? (int) putwc (c, fp->fp) : putc (c, fp->fp);
  153. if (fp->size)
  154. {
  155. *(fp->str++) = c;
  156. fp->size--;
  157. }
  158. fp->len++;
  159. return (unsigned char) c;
  160. }
  161. #define PUT(f, s, n) __quadmath_do_put (f, wide, s, n)
  162. #define PAD(f, c, n) __quadmath_do_pad (f, wide, c, n)
  163. #define PUTC(c, f) __quadmath_do_putc (f, wide, c)
  164. #define nl_langinfo_wc(x) \
  165. ({ union { const char *mb; wchar_t wc; } u; u.mb = nl_langinfo (x); u.wc; })
  166. #undef _itoa
  167. #define _itoa __quadmath_itoa
  168. #undef NAN
  169. #define NAN __builtin_nanf ("")