#ifndef NO_FLOATING_POINT
#define dtoa __dtoa
#define freedtoa __freedtoa
#include <float.h>
#include <math.h>
#include "floatio.h"
#include "gdtoa.h"
#define DEFPREC 6
static int exponent(CHAR *, int, CHAR);
#endif
static CHAR *__ujtoa(uintmax_t, CHAR *, int, int, const char *);
static CHAR *__ultoa(u_long, CHAR *, int, int, const char *);
#define NIOV 8
struct io_state {
FILE *fp;
struct __suio uio;
struct __siov iov[NIOV];
};
static inline void
io_init(struct io_state *iop, FILE *fp)
{
iop->uio.uio_iov = iop->iov;
iop->uio.uio_resid = 0;
iop->uio.uio_iovcnt = 0;
iop->fp = fp;
}
static inline int
io_print(struct io_state *iop, const CHAR * __restrict ptr, int len, locale_t loc)
{
iop->iov[iop->uio.uio_iovcnt].iov_base = (char *)ptr;
iop->iov[iop->uio.uio_iovcnt].iov_len = len;
iop->uio.uio_resid += len;
if (++iop->uio.uio_iovcnt >= NIOV)
return (__sprint(iop->fp, loc, &iop->uio));
else
return (0);
}
#define PADSIZE 16
static const CHAR blanks[PADSIZE] =
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
static const CHAR zeroes[PADSIZE] =
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
static inline int
io_pad(struct io_state *iop, int howmany, const CHAR * __restrict with, locale_t loc)
{
int n;
while (howmany > 0) {
n = (howmany >= PADSIZE) ? PADSIZE : howmany;
if (io_print(iop, with, n, loc))
return (-1);
howmany -= n;
}
return (0);
}
static inline int
io_printandpad(struct io_state *iop, const CHAR *p, const CHAR *ep,
int len, const CHAR * __restrict with, locale_t loc)
{
int p_len;
p_len = (int)(ep - p);
if (p_len > len)
p_len = len;
if (p_len > 0) {
if (io_print(iop, p, p_len, loc))
return (-1);
} else {
p_len = 0;
}
return (io_pad(iop, len - p_len, with, loc));
}
static inline int
io_flush(struct io_state *iop, locale_t loc)
{
return (__sprint(iop->fp, loc, &iop->uio));
}
static CHAR *
__ultoa(u_long val, CHAR *endp, int base, int octzero, const char *xdigs)
{
CHAR *cp = endp;
long sval;
switch (base) {
case 10:
if (val < 10) {
*--cp = (CHAR)to_char(val);
return (cp);
}
if (val > LONG_MAX) {
*--cp = to_char(val % 10);
sval = val / 10;
} else
sval = val;
do {
*--cp = to_char(sval % 10);
sval /= 10;
} while (sval != 0);
break;
case 8:
do {
*--cp = to_char(val & 7);
val >>= 3;
} while (val);
if (octzero && *cp != '0')
*--cp = '0';
break;
case 16:
do {
*--cp = xdigs[val & 15];
val >>= 4;
} while (val);
break;
default:
LIBC_ABORT("__ultoa: invalid base=%d", base);
}
return (cp);
}
static CHAR *
__ujtoa(uintmax_t val, CHAR *endp, int base, int octzero, const char *xdigs)
{
CHAR *cp = endp;
intmax_t sval;
if (val <= ULONG_MAX)
return (__ultoa((u_long)val, endp, base, octzero, xdigs));
switch (base) {
case 10:
if (val < 10) {
*--cp = to_char(val % 10);
return (cp);
}
if (val > INTMAX_MAX) {
*--cp = to_char(val % 10);
sval = val / 10;
} else
sval = val;
do {
*--cp = to_char(sval % 10);
sval /= 10;
} while (sval != 0);
break;
case 8:
do {
*--cp = to_char(val & 7);
val >>= 3;
} while (val);
if (octzero && *cp != '0')
*--cp = '0';
break;
case 16:
do {
*--cp = xdigs[val & 15];
val >>= 4;
} while (val);
break;
default:
LIBC_ABORT("__ujtoa: invalid base=%d", base);
}
return (cp);
}
#ifndef NO_FLOATING_POINT
static int
exponent(CHAR *p0, int exp, CHAR fmtch)
{
CHAR *p, *t;
CHAR expbuf[MAXEXPDIG];
p = p0;
*p++ = fmtch;
if (exp < 0) {
exp = -exp;
*p++ = '-';
}
else
*p++ = '+';
t = expbuf + MAXEXPDIG;
if (exp > 9) {
do {
*--t = to_char(exp % 10);
} while ((exp /= 10) > 9);
*--t = to_char(exp);
for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
}
else {
if (fmtch == 'e' || fmtch == 'E')
*p++ = '0';
*p++ = to_char(exp);
}
return (int)(p - p0);
}
#endif