Blame stdlib/strfrom-skeleton.c

Packit 6c4009
/* Convert a floating-point number to string.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* Generic implementation for strfrom functions.  The implementation is generic
Packit 6c4009
   for several floating-point types (e.g.: float, double), so that each
Packit 6c4009
   function, such as strfromf and strfroml, share the same code, thus avoiding
Packit 6c4009
   code duplication.  */
Packit 6c4009
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include "../libio/libioP.h"
Packit 6c4009
#include "../libio/strfile.h"
Packit 6c4009
#include <printf.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <locale/localeinfo.h>
Packit 6c4009
Packit 6c4009
#define UCHAR_T char
Packit 6c4009
#define L_(Str) Str
Packit 6c4009
#define ISDIGIT(Ch) isdigit (Ch)
Packit 6c4009
#include "stdio-common/printf-parse.h"
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
STRFROM (char *dest, size_t size, const char *format, FLOAT f)
Packit 6c4009
{
Packit 6c4009
  _IO_strnfile sfile;
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
  sfile.f._sbf._f._lock = NULL;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  int done;
Packit 6c4009
Packit 6c4009
  /* Single-precision values need to be stored in a double type, because
Packit 6c4009
     __printf_fp_l and __printf_fphex do not accept the float type.  */
Packit 6c4009
  union {
Packit 6c4009
    double flt;
Packit 6c4009
    FLOAT value;
Packit 6c4009
  } fpnum;
Packit 6c4009
  const void *fpptr;
Packit 6c4009
  fpptr = &fpnum;
Packit 6c4009
Packit 6c4009
  /* Variables to control the output format.  */
Packit 6c4009
  int precision = -1; /* printf_fp and printf_fphex treat this internally.  */
Packit 6c4009
  int specifier;
Packit 6c4009
  struct printf_info info;
Packit 6c4009
Packit 6c4009
  /* Single-precision values need to be converted into double-precision,
Packit 6c4009
     because __printf_fp and __printf_fphex only accept double and long double
Packit 6c4009
     as the floating-point argument.  */
Packit 6c4009
  if (__builtin_types_compatible_p (FLOAT, float))
Packit 6c4009
    fpnum.flt = f;
Packit 6c4009
  else
Packit 6c4009
    fpnum.value = f;
Packit 6c4009
Packit 6c4009
  /* Check if the first character in the format string is indeed the '%'
Packit 6c4009
     character.  Otherwise, abort.  */
Packit 6c4009
  if (*format == '%')
Packit 6c4009
    format++;
Packit 6c4009
  else
Packit 6c4009
    abort ();
Packit 6c4009
Packit 6c4009
  /* The optional precision specification always starts with a '.'.  If such
Packit 6c4009
     character is present, read the precision.  */
Packit 6c4009
  if (*format == '.')
Packit 6c4009
    {
Packit 6c4009
      format++;
Packit 6c4009
Packit 6c4009
      /* Parse the precision.  */
Packit 6c4009
      if (ISDIGIT (*format))
Packit 6c4009
	precision = read_int (&format);
Packit 6c4009
      /* If only the period is specified, the precision is taken as zero, as
Packit 6c4009
	 described in ISO/IEC 9899:2011, section 7.21.6.1, 4th paragraph, 3rd
Packit 6c4009
	 item.  */
Packit 6c4009
      else
Packit 6c4009
	precision = 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now there is only the conversion specifier to be read.  */
Packit 6c4009
  switch (*format)
Packit 6c4009
    {
Packit 6c4009
    case 'a':
Packit 6c4009
    case 'A':
Packit 6c4009
    case 'e':
Packit 6c4009
    case 'E':
Packit 6c4009
    case 'f':
Packit 6c4009
    case 'F':
Packit 6c4009
    case 'g':
Packit 6c4009
    case 'G':
Packit 6c4009
      specifier = *format;
Packit 6c4009
      break;
Packit 6c4009
    default:
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* The following code to prepare the virtual file has been adapted from the
Packit 6c4009
     function _IO_vsnprintf from libio.  */
Packit 6c4009
Packit 6c4009
  if (size == 0)
Packit 6c4009
    {
Packit 6c4009
    /* When size is zero, nothing is written and dest may be a null pointer.
Packit 6c4009
       This is specified for snprintf in ISO/IEC 9899:2011, Section 7.21.6.5,
Packit 6c4009
       in the second paragraph.  Thus, if size is zero, prepare to use the
Packit 6c4009
       overflow buffer right from the start.  */
Packit 6c4009
      dest = sfile.overflow_buf;
Packit 6c4009
      size = sizeof (sfile.overflow_buf);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Prepare the virtual string file.  */
Packit 6c4009
  _IO_no_init (&sfile.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
Packit 6c4009
  _IO_JUMPS (&sfile.f._sbf) = &_IO_strn_jumps;
Packit 6c4009
  _IO_str_init_static_internal (&sfile.f, dest, size - 1, dest);
Packit 6c4009
Packit 6c4009
  /* Prepare the format specification for printf_fp.  */
Packit 6c4009
  memset (&info, '\0', sizeof (info));
Packit 6c4009
Packit 6c4009
  /* The functions strfromd and strfromf pass a floating-point number with
Packit 6c4009
     double precision to printf_fp, whereas strfroml passes a floating-point
Packit 6c4009
     number with long double precision.  The following line informs printf_fp
Packit 6c4009
     which type of floating-point number is being passed.  */
Packit 6c4009
  info.is_long_double = __builtin_types_compatible_p (FLOAT, long double);
Packit 6c4009
Packit 6c4009
  /* Similarly, the function strfromf128 passes a floating-point number in
Packit 6c4009
     _Float128 format to printf_fp.  */
Packit 6c4009
#if __HAVE_DISTINCT_FLOAT128
Packit 6c4009
  info.is_binary128 = __builtin_types_compatible_p (FLOAT, _Float128);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* Set info according to the format string.  */
Packit 6c4009
  info.prec = precision;
Packit 6c4009
  info.spec = specifier;
Packit 6c4009
Packit 6c4009
  if (info.spec != 'a' && info.spec != 'A')
Packit 6c4009
    done = __printf_fp_l (&sfile.f._sbf._f, _NL_CURRENT_LOCALE, &info, &fpptr);
Packit 6c4009
  else
Packit 6c4009
    done = __printf_fphex (&sfile.f._sbf._f, &info, &fpptr);
Packit 6c4009
Packit 6c4009
  /* Terminate the string.  */
Packit 6c4009
  if (sfile.f._sbf._f._IO_buf_base != sfile.overflow_buf)
Packit 6c4009
    *sfile.f._sbf._f._IO_write_ptr = '\0';
Packit 6c4009
Packit 6c4009
  return done;
Packit 6c4009
}