strfrom-skeleton.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Convert a floating-point number to string.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  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. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* Generic implementation for strfrom functions. The implementation is generic
  16. for several floating-point types (e.g.: float, double), so that each
  17. function, such as strfromf and strfroml, share the same code, thus avoiding
  18. code duplication. */
  19. #include <ctype.h>
  20. #include "../libio/libioP.h"
  21. #include "../libio/strfile.h"
  22. #include <printf.h>
  23. #include <string.h>
  24. #include <locale/localeinfo.h>
  25. #define UCHAR_T char
  26. #define L_(Str) Str
  27. #define ISDIGIT(Ch) isdigit (Ch)
  28. #include "stdio-common/printf-parse.h"
  29. int
  30. STRFROM (char *dest, size_t size, const char *format, FLOAT f)
  31. {
  32. _IO_strnfile sfile;
  33. #ifdef _IO_MTSAFE_IO
  34. sfile.f._sbf._f._lock = NULL;
  35. #endif
  36. int done;
  37. /* Single-precision values need to be stored in a double type, because
  38. __printf_fp_l and __printf_fphex do not accept the float type. */
  39. union {
  40. double flt;
  41. FLOAT value;
  42. } fpnum;
  43. const void *fpptr;
  44. fpptr = &fpnum;
  45. /* Variables to control the output format. */
  46. int precision = -1; /* printf_fp and printf_fphex treat this internally. */
  47. int specifier;
  48. struct printf_info info;
  49. /* Single-precision values need to be converted into double-precision,
  50. because __printf_fp and __printf_fphex only accept double and long double
  51. as the floating-point argument. */
  52. if (__builtin_types_compatible_p (FLOAT, float))
  53. fpnum.flt = f;
  54. else
  55. fpnum.value = f;
  56. /* Check if the first character in the format string is indeed the '%'
  57. character. Otherwise, abort. */
  58. if (*format == '%')
  59. format++;
  60. else
  61. abort ();
  62. /* The optional precision specification always starts with a '.'. If such
  63. character is present, read the precision. */
  64. if (*format == '.')
  65. {
  66. format++;
  67. /* Parse the precision. */
  68. if (ISDIGIT (*format))
  69. precision = read_int (&format);
  70. /* If only the period is specified, the precision is taken as zero, as
  71. described in ISO/IEC 9899:2011, section 7.21.6.1, 4th paragraph, 3rd
  72. item. */
  73. else
  74. precision = 0;
  75. }
  76. /* Now there is only the conversion specifier to be read. */
  77. switch (*format)
  78. {
  79. case 'a':
  80. case 'A':
  81. case 'e':
  82. case 'E':
  83. case 'f':
  84. case 'F':
  85. case 'g':
  86. case 'G':
  87. specifier = *format;
  88. break;
  89. default:
  90. abort ();
  91. }
  92. /* The following code to prepare the virtual file has been adapted from the
  93. function __vsnprintf_internal from libio. */
  94. if (size == 0)
  95. {
  96. /* When size is zero, nothing is written and dest may be a null pointer.
  97. This is specified for snprintf in ISO/IEC 9899:2011, Section 7.21.6.5,
  98. in the second paragraph. Thus, if size is zero, prepare to use the
  99. overflow buffer right from the start. */
  100. dest = sfile.overflow_buf;
  101. size = sizeof (sfile.overflow_buf);
  102. }
  103. /* Prepare the virtual string file. */
  104. _IO_no_init (&sfile.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
  105. _IO_JUMPS (&sfile.f._sbf) = &_IO_strn_jumps;
  106. _IO_str_init_static_internal (&sfile.f, dest, size - 1, dest);
  107. /* Prepare the format specification for printf_fp. */
  108. memset (&info, '\0', sizeof (info));
  109. /* The functions strfromd and strfromf pass a floating-point number with
  110. double precision to printf_fp, whereas strfroml passes a floating-point
  111. number with long double precision. The following line informs printf_fp
  112. which type of floating-point number is being passed. */
  113. info.is_long_double = __builtin_types_compatible_p (FLOAT, long double);
  114. /* Similarly, the function strfromf128 passes a floating-point number in
  115. _Float128 format to printf_fp. */
  116. #if __HAVE_DISTINCT_FLOAT128
  117. info.is_binary128 = __builtin_types_compatible_p (FLOAT, _Float128);
  118. #endif
  119. /* Set info according to the format string. */
  120. info.prec = precision;
  121. info.spec = specifier;
  122. if (info.spec != 'a' && info.spec != 'A')
  123. done = __printf_fp_l (&sfile.f._sbf._f, _NL_CURRENT_LOCALE, &info, &fpptr);
  124. else
  125. done = __printf_fphex (&sfile.f._sbf._f, &info, &fpptr);
  126. /* Terminate the string. */
  127. if (sfile.f._sbf._f._IO_buf_base != sfile.overflow_buf)
  128. *sfile.f._sbf._f._IO_write_ptr = '\0';
  129. return done;
  130. }