1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/*
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <newlib.h>
#define _DEFAULT_SOURCE
#include <_ansi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include <math.h>
#include <wchar.h>
#include <sys/lock.h>
#include <stdarg.h>
#include "local.h"
#include "../stdlib/local.h"
#include "fvwrite.h"
#include "vfieeefp.h"
#include "nano-vfprintf_local.h"
char *__cvt (_PRINTF_FLOAT_TYPE value, int ndigits,
int flags, char *sign, int *decpt, int ch, int *length,
char *buf);
int __exponent (char *p0, int exp, int fmtch);
#ifdef FLOATING_POINT
/* Using reentrant DATA, convert finite VALUE into a string of digits
with no decimal point, using NDIGITS precision and FLAGS as guides
to whether trailing zeros must be included. Set *SIGN to nonzero
if VALUE was negative. Set *DECPT to the exponent plus one. Set
*LENGTH to the length of the returned string. CH must be one of
[aAeEfFgG]; if it is [aA], then the return string lives in BUF,
otherwise the return value shares the mprec reentrant storage. */
char *
__cvt (struct _reent *data, _PRINTF_FLOAT_TYPE value, int ndigits, int flags,
char *sign, int *decpt, int ch, int *length, char *buf)
{
int mode, dsgn;
char *digits, *bp, *rve;
union double_union tmp;
tmp.d = value;
/* This will check for "< 0" and "-0.0". */
if (word0 (tmp) & Sign_bit)
{
value = -value;
*sign = '-';
}
else
*sign = '\000';
if (ch == 'f' || ch == 'F')
{
/* Ndigits after the decimal point. */
mode = 3;
}
else
{
/* To obtain ndigits after the decimal point for the 'e'
and 'E' formats, round to ndigits + 1 significant figures. */
if (ch == 'e' || ch == 'E')
{
ndigits++;
}
/* Ndigits significant digits. */
mode = 2;
}
digits = _DTOA (value, mode, ndigits, decpt, &dsgn, &rve);
/* Print trailing zeros. */
if ((ch != 'g' && ch != 'G') || flags & ALT)
{
bp = digits + ndigits;
if (ch == 'f' || ch == 'F')
{
if (*digits == '0' && value)
*decpt = -ndigits + 1;
bp += *decpt;
}
/* Kludge for __dtoa irregularity. */
if (value == 0)
rve = bp;
while (rve < bp)
*rve++ = '0';
}
*length = rve - digits;
return (digits);
}
/* This function is copied from exponent in vfprintf.c with support for
C99 formats removed. We don't use the original function in order to
decouple nano implementation of formatted IO from the Newlib one. */
int
__exponent (char *p0, int exp, int fmtch)
{
register char *p, *t;
char expbuf[MAXEXPLEN];
#define isa 0
p = p0;
*p++ = isa ? 'p' - 'a' + fmtch : fmtch;
if (exp < 0)
{
exp = -exp;
*p++ = '-';
}
else
*p++ = '+';
t = expbuf + MAXEXPLEN;
if (exp > 9)
{
do
{
*--t = to_char (exp % 10);
}
while ((exp /= 10) > 9);
*--t = to_char (exp);
for (; t < expbuf + MAXEXPLEN; *p++ = *t++);
}
else
{
if (!isa)
*p++ = '0';
*p++ = to_char (exp);
}
return (p - p0);
}
/* Decode and print floating point number specified by "eEfgG". */
int
_printf_float (struct _reent *data,
struct _prt_data_t *pdata,
FILE * fp,
int (*pfunc) (struct _reent *, FILE *, const char *,
size_t len), va_list * ap)
{
#define _fpvalue (pdata->_double_)
char *decimal_point = localeconv ()->decimal_point;
size_t decp_len = strlen (decimal_point);
/* Temporary negative sign for floats. */
char softsign;
/* Integer value of exponent. */
int expt;
/* Character count for expstr. */
int expsize = 0;
/* Actual number of digits returned by cvt. */
int ndig = 0;
char *cp;
int n;
/* Field size expanded by dprec(not for _printf_float). */
int realsz;
char code = pdata->code;
if (pdata->flags & LONGDBL)
{
_fpvalue = (double) GET_ARG (N, *ap, _LONG_DOUBLE);
}
else
{
_fpvalue = GET_ARG (N, *ap, double);
}
/* Do this before tricky precision changes.
If the output is infinite or NaN, leading
zeros are not permitted. Otherwise, scanf
could not read what printf wrote. */
if (isinf (_fpvalue))
{
if (_fpvalue < 0)
pdata->l_buf[0] = '-';
if (code <= 'G') /* 'A', 'E', 'F', or 'G'. */
cp = "INF";
else
cp = "inf";
pdata->size = 3;
pdata->flags &= ~ZEROPAD;
goto print_float;
}
if (isnan (_fpvalue))
{
if (signbit (_fpvalue))
pdata->l_buf[0] = '-';
if (code <= 'G') /* 'A', 'E', 'F', or 'G'. */
cp = "NAN";
else
cp = "nan";
pdata->size = 3;
pdata->flags &= ~ZEROPAD;
goto print_float;
}
if (pdata->prec == -1)
{
pdata->prec = DEFPREC;
}
else if ((code == 'g' || code == 'G') && pdata->prec == 0)
{
pdata->prec = 1;
}
pdata->flags |= FPT;
cp = __cvt (data, _fpvalue, pdata->prec, pdata->flags, &softsign,
&expt, code, &ndig, cp);
if (code == 'g' || code == 'G')
{
if (expt <= -4 || expt > pdata->prec)
/* 'e' or 'E'. */
code -= 2;
else
code = 'g';
}
if (code <= 'e')
{
/* 'a', 'A', 'e', or 'E' fmt. */
--expt;
expsize = __exponent (pdata->expstr, expt, code);
pdata->size = expsize + ndig;
if (ndig > 1 || pdata->flags & ALT)
++pdata->size;
}
else
{
if (code == 'f')
{
/* 'f' fmt. */
if (expt > 0)
{
pdata->size = expt;
if (pdata->prec || pdata->flags & ALT)
pdata->size += pdata->prec + 1;
}
else
/* "0.X". */
pdata->size = (pdata->prec || pdata->flags & ALT)
? pdata->prec + 2 : 1;
}
else if (expt >= ndig)
{
/* Fixed g fmt. */
pdata->size = expt;
if (pdata->flags & ALT)
++pdata->size;
}
else
pdata->size = ndig + (expt > 0 ? 1 : 2 - expt);
pdata->lead = expt;
}
if (softsign)
pdata->l_buf[0] = '-';
print_float:
if (_printf_common (data, pdata, &realsz, fp, pfunc) == -1)
goto error;
if ((pdata->flags & FPT) == 0)
{
PRINT (cp, pdata->size);
}
else
{
/* Glue together f_p fragments. */
if (code >= 'f')
{
/* 'f' or 'g'. */
if (_fpvalue == 0)
{
/* Kludge for __dtoa irregularity. */
PRINT ("0", 1);
if (expt < ndig || pdata->flags & ALT)
{
PRINT (decimal_point, decp_len);
PAD (ndig - 1, pdata->zero);
}
}
else if (expt <= 0)
{
PRINT ("0", 1);
if (expt || ndig || pdata->flags & ALT)
{
PRINT (decimal_point, decp_len);
PAD (-expt, pdata->zero);
PRINT (cp, ndig);
}
}
else
{
char *convbuf = cp;
PRINTANDPAD (cp, convbuf + ndig, pdata->lead, pdata->zero);
cp += pdata->lead;
if (expt < ndig || pdata->flags & ALT)
PRINT (decimal_point, decp_len);
PRINTANDPAD (cp, convbuf + ndig, ndig - expt, pdata->zero);
}
}
else
{
/* 'a', 'A', 'e', or 'E'. */
if (ndig > 1 || pdata->flags & ALT)
{
PRINT (cp, 1);
cp++;
PRINT (decimal_point, decp_len);
if (_fpvalue)
{
PRINT (cp, ndig - 1);
}
/* "0.[0..]". */
else
/* __dtoa irregularity. */
PAD (ndig - 1, pdata->zero);
}
else /* "XeYYY". */
PRINT (cp, 1);
PRINT (pdata->expstr, expsize);
}
}
/* Left-adjusting padding (always blank). */
if (pdata->flags & LADJUST)
PAD (pdata->width - realsz, pdata->blank);
return (pdata->width > realsz ? pdata->width : realsz);
error:
return -1;
#undef _fpvalue
}
#endif
|