2012-08-21 18:11:24 -03:00
|
|
|
// -*- Mode: C++; c-basic-offset: 8; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
|
|
|
Adapted from the avr-libc vfprintf:
|
|
|
|
|
|
|
|
Copyright (c) 2002, Alexander Popov (sasho@vip.bg)
|
|
|
|
Copyright (c) 2002,2004,2005 Joerg Wunsch
|
|
|
|
Copyright (c) 2005, Helmut Wallner
|
|
|
|
Copyright (c) 2007, Dmitry Xmelkov
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* 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.
|
|
|
|
* Neither the name of the copyright holders nor the names of
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* From: Id: printf_p_new.c,v 1.1.1.9 2002/10/15 20:10:28 joerg_wunsch Exp */
|
|
|
|
/* $Id: vfprintf.c,v 1.18.2.1 2009/04/01 23:12:06 arcanum Exp $ */
|
|
|
|
|
|
|
|
|
2012-12-12 17:39:53 -04:00
|
|
|
#include <AP_HAL.h>
|
|
|
|
#include <AP_Progmem.h>
|
2012-08-21 18:11:24 -03:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2013-09-21 00:28:46 -03:00
|
|
|
#include <math.h>
|
2012-09-13 20:46:07 -03:00
|
|
|
|
2012-08-21 18:11:24 -03:00
|
|
|
#include "ftoa_engine.h"
|
|
|
|
#include "xtoa_fast.h"
|
|
|
|
|
2012-12-19 18:01:26 -04:00
|
|
|
#include "print_vprintf.h"
|
2012-08-21 18:11:24 -03:00
|
|
|
|
2013-09-21 00:28:46 -03:00
|
|
|
#define GETBYTE(flag, mask, pnt) ((flag)&(mask)?pgm_read_byte(pnt++):*pnt++)
|
2012-08-21 18:11:24 -03:00
|
|
|
|
|
|
|
#define FL_ZFILL 0x01
|
|
|
|
#define FL_PLUS 0x02
|
|
|
|
#define FL_SPACE 0x04
|
|
|
|
#define FL_LPAD 0x08
|
|
|
|
#define FL_ALT 0x10
|
|
|
|
#define FL_WIDTH 0x20
|
|
|
|
#define FL_PREC 0x40
|
|
|
|
#define FL_LONG 0x80
|
2015-05-26 22:50:22 -03:00
|
|
|
#define FL_LONGLONG 0x100
|
2012-08-21 18:11:24 -03:00
|
|
|
|
|
|
|
#define FL_PGMSTRING FL_LONG
|
|
|
|
#define FL_NEGATIVE FL_LONG
|
|
|
|
|
|
|
|
#define FL_ALTUPP FL_PLUS
|
|
|
|
#define FL_ALTHEX FL_SPACE
|
|
|
|
|
|
|
|
#define FL_FLTUPP FL_ALT
|
|
|
|
#define FL_FLTEXP FL_PREC
|
|
|
|
#define FL_FLTFIX FL_LONG
|
|
|
|
|
2012-12-19 17:28:56 -04:00
|
|
|
void print_vprintf (AP_HAL::Print *s, unsigned char in_progmem, const char *fmt, va_list ap)
|
2012-08-21 18:11:24 -03:00
|
|
|
{
|
|
|
|
unsigned char c; /* holds a char from the format string */
|
2015-05-26 22:50:22 -03:00
|
|
|
uint16_t flags;
|
2012-08-21 18:11:24 -03:00
|
|
|
unsigned char width;
|
|
|
|
unsigned char prec;
|
2015-05-26 22:50:22 -03:00
|
|
|
unsigned char buf[23];
|
2012-08-21 18:11:24 -03:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process non-format characters
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
c = GETBYTE (in_progmem, 1, fmt);
|
|
|
|
if (!c) return;
|
|
|
|
if (c == '%') {
|
|
|
|
c = GETBYTE (in_progmem, 1, fmt);
|
|
|
|
if (c != '%') break;
|
|
|
|
}
|
|
|
|
/* emit cr before lf to make most terminals happy */
|
|
|
|
if (c == '\n')
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write('\r');
|
|
|
|
s->write(c);
|
2012-08-21 18:11:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
flags = 0;
|
|
|
|
width = 0;
|
|
|
|
prec = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process format adjustment characters, precision, width.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
if (flags < FL_WIDTH) {
|
|
|
|
switch (c) {
|
|
|
|
case '0':
|
|
|
|
flags |= FL_ZFILL;
|
|
|
|
continue;
|
|
|
|
case '+':
|
|
|
|
flags |= FL_PLUS;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case ' ':
|
|
|
|
flags |= FL_SPACE;
|
|
|
|
continue;
|
|
|
|
case '-':
|
|
|
|
flags |= FL_LPAD;
|
|
|
|
continue;
|
|
|
|
case '#':
|
|
|
|
flags |= FL_ALT;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags < FL_LONG) {
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
c -= '0';
|
|
|
|
if (flags & FL_PREC) {
|
|
|
|
prec = 10*prec + c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
width = 10*width + c;
|
|
|
|
flags |= FL_WIDTH;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == '.') {
|
|
|
|
if (flags & FL_PREC)
|
|
|
|
return;
|
|
|
|
flags |= FL_PREC;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == 'l') {
|
|
|
|
flags |= FL_LONG;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == 'h')
|
|
|
|
continue;
|
2015-05-26 22:50:22 -03:00
|
|
|
} else if ((flags & FL_LONG) && c == 'l') {
|
|
|
|
flags |= FL_LONGLONG;
|
|
|
|
continue;
|
2012-08-21 18:11:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
} while ( (c = GETBYTE (in_progmem, 1, fmt)) != 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle floating-point formats E, F, G, e, f, g.
|
|
|
|
*/
|
|
|
|
if (c >= 'E' && c <= 'G') {
|
|
|
|
flags |= FL_FLTUPP;
|
|
|
|
c += 'e' - 'E';
|
|
|
|
goto flt_oper;
|
|
|
|
|
|
|
|
} else if (c >= 'e' && c <= 'g') {
|
|
|
|
|
|
|
|
int exp; /* exponent of master decimal digit */
|
|
|
|
int n;
|
|
|
|
unsigned char vtype; /* result of float value parse */
|
|
|
|
unsigned char sign; /* sign character (or 0) */
|
|
|
|
unsigned char ndigs;
|
|
|
|
|
|
|
|
flags &= ~FL_FLTUPP;
|
|
|
|
|
|
|
|
flt_oper:
|
2013-09-21 00:28:46 -03:00
|
|
|
float value = va_arg(ap,double);
|
2012-08-21 18:11:24 -03:00
|
|
|
if (!(flags & FL_PREC))
|
2013-09-21 02:45:05 -03:00
|
|
|
prec = 6;
|
2012-08-21 18:11:24 -03:00
|
|
|
flags &= ~(FL_FLTEXP | FL_FLTFIX);
|
2013-09-21 00:28:46 -03:00
|
|
|
if (c == 'e') {
|
2012-08-21 18:11:24 -03:00
|
|
|
flags |= FL_FLTEXP;
|
2013-09-21 00:28:46 -03:00
|
|
|
} else if (c == 'f') {
|
2012-08-21 18:11:24 -03:00
|
|
|
flags |= FL_FLTFIX;
|
2013-09-21 00:28:46 -03:00
|
|
|
} else if (prec > 0)
|
2012-08-21 18:11:24 -03:00
|
|
|
prec -= 1;
|
2015-05-08 15:43:05 -03:00
|
|
|
if ((flags & FL_FLTFIX) && fabsf(value) > 9999999) {
|
2013-09-21 00:28:46 -03:00
|
|
|
flags = (flags & ~FL_FLTFIX) | FL_FLTEXP;
|
|
|
|
}
|
2012-08-21 18:11:24 -03:00
|
|
|
|
|
|
|
if (flags & FL_FLTFIX) {
|
|
|
|
vtype = 7; /* 'prec' arg for 'ftoa_engine' */
|
|
|
|
ndigs = prec < 60 ? prec + 1 : 60;
|
|
|
|
} else {
|
2013-09-21 00:28:46 -03:00
|
|
|
if (prec > 10) prec = 10;
|
2012-08-21 18:11:24 -03:00
|
|
|
vtype = prec;
|
|
|
|
ndigs = 0;
|
|
|
|
}
|
2013-09-21 00:28:46 -03:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
exp = ftoa_engine(value, (char *)buf, vtype, ndigs);
|
2012-08-21 18:11:24 -03:00
|
|
|
vtype = buf[0];
|
|
|
|
|
|
|
|
sign = 0;
|
|
|
|
if ((vtype & FTOA_MINUS) && !(vtype & FTOA_NAN))
|
|
|
|
sign = '-';
|
|
|
|
else if (flags & FL_PLUS)
|
|
|
|
sign = '+';
|
|
|
|
else if (flags & FL_SPACE)
|
|
|
|
sign = ' ';
|
|
|
|
|
|
|
|
if (vtype & (FTOA_NAN | FTOA_INF)) {
|
|
|
|
ndigs = sign ? 4 : 3;
|
|
|
|
if (width > ndigs) {
|
|
|
|
width -= ndigs;
|
|
|
|
if (!(flags & FL_LPAD)) {
|
|
|
|
do {
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(' ');
|
2012-08-21 18:11:24 -03:00
|
|
|
} while (--width);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
width = 0;
|
|
|
|
}
|
|
|
|
if (sign)
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(sign);
|
2013-09-21 00:28:46 -03:00
|
|
|
const prog_char_t *p = PSTR("inf");
|
2012-08-21 18:11:24 -03:00
|
|
|
if (vtype & FTOA_NAN)
|
|
|
|
p = PSTR("nan");
|
|
|
|
while ( (ndigs = pgm_read_byte((const prog_char *)p)) != 0) {
|
|
|
|
if (flags & FL_FLTUPP)
|
|
|
|
ndigs += 'I' - 'i';
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(ndigs);
|
2012-08-21 18:11:24 -03:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
goto tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output format adjustment, number of decimal digits in buf[] */
|
|
|
|
if (flags & FL_FLTFIX) {
|
|
|
|
ndigs += exp;
|
|
|
|
if ((vtype & FTOA_CARRY) && buf[1] == '1')
|
|
|
|
ndigs -= 1;
|
|
|
|
if ((signed char)ndigs < 1)
|
|
|
|
ndigs = 1;
|
|
|
|
else if (ndigs > 8)
|
|
|
|
ndigs = 8;
|
|
|
|
} else if (!(flags & FL_FLTEXP)) { /* 'g(G)' format */
|
|
|
|
if (exp <= prec && exp >= -4)
|
|
|
|
flags |= FL_FLTFIX;
|
|
|
|
while (prec && buf[1+prec] == '0')
|
|
|
|
prec--;
|
|
|
|
if (flags & FL_FLTFIX) {
|
|
|
|
ndigs = prec + 1; /* number of digits in buf */
|
|
|
|
prec = prec > exp
|
|
|
|
? prec - exp : 0; /* fractional part length */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Conversion result length, width := free space length */
|
|
|
|
if (flags & FL_FLTFIX)
|
|
|
|
n = (exp>0 ? exp+1 : 1);
|
|
|
|
else
|
|
|
|
n = 5; /* 1e+00 */
|
|
|
|
if (sign) n += 1;
|
|
|
|
if (prec) n += prec + 1;
|
|
|
|
width = width > n ? width - n : 0;
|
|
|
|
|
|
|
|
/* Output before first digit */
|
|
|
|
if (!(flags & (FL_LPAD | FL_ZFILL))) {
|
|
|
|
while (width) {
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(' ');
|
2012-08-21 18:11:24 -03:00
|
|
|
width--;
|
|
|
|
}
|
|
|
|
}
|
2012-09-13 20:46:07 -03:00
|
|
|
if (sign) s->write(sign);
|
2012-08-21 18:11:24 -03:00
|
|
|
if (!(flags & FL_LPAD)) {
|
|
|
|
while (width) {
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write('0');
|
2012-08-21 18:11:24 -03:00
|
|
|
width--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & FL_FLTFIX) { /* 'f' format */
|
|
|
|
|
|
|
|
n = exp > 0 ? exp : 0; /* exponent of left digit */
|
2015-05-26 22:50:22 -03:00
|
|
|
unsigned char v = 0;
|
2012-08-21 18:11:24 -03:00
|
|
|
do {
|
|
|
|
if (n == -1)
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write('.');
|
2015-05-26 22:50:22 -03:00
|
|
|
v = (n <= exp && n > exp - ndigs)
|
2012-08-21 18:11:24 -03:00
|
|
|
? buf[exp - n + 1] : '0';
|
2015-05-26 22:50:22 -03:00
|
|
|
if (--n < -prec || v == 0)
|
2012-08-21 18:11:24 -03:00
|
|
|
break;
|
2015-05-26 22:50:22 -03:00
|
|
|
s->write(v);
|
2012-08-21 18:11:24 -03:00
|
|
|
} while (1);
|
|
|
|
if (n == exp
|
|
|
|
&& (buf[1] > '5'
|
|
|
|
|| (buf[1] == '5' && !(vtype & FTOA_CARRY))) )
|
|
|
|
{
|
2015-05-26 22:50:22 -03:00
|
|
|
v = '1';
|
2012-08-21 18:11:24 -03:00
|
|
|
}
|
2015-05-26 22:50:22 -03:00
|
|
|
if (v) s->write(v);
|
2012-08-21 18:11:24 -03:00
|
|
|
|
|
|
|
} else { /* 'e(E)' format */
|
|
|
|
|
|
|
|
/* mantissa */
|
|
|
|
if (buf[1] != '1')
|
|
|
|
vtype &= ~FTOA_CARRY;
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(buf[1]);
|
2012-08-21 18:11:24 -03:00
|
|
|
if (prec) {
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write('.');
|
2012-08-21 18:11:24 -03:00
|
|
|
sign = 2;
|
|
|
|
do {
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(buf[sign++]);
|
2012-08-21 18:11:24 -03:00
|
|
|
} while (--prec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* exponent */
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(flags & FL_FLTUPP ? 'E' : 'e');
|
2012-08-21 18:11:24 -03:00
|
|
|
ndigs = '+';
|
|
|
|
if (exp < 0 || (exp == 0 && (vtype & FTOA_CARRY) != 0)) {
|
|
|
|
exp = -exp;
|
|
|
|
ndigs = '-';
|
|
|
|
}
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(ndigs);
|
2012-08-21 18:11:24 -03:00
|
|
|
for (ndigs = '0'; exp >= 10; exp -= 10)
|
|
|
|
ndigs += 1;
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(ndigs);
|
|
|
|
s->write('0' + exp);
|
2012-08-21 18:11:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
goto tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle string formats c, s, S.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
const char * pnt;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
buf[0] = va_arg (ap, int);
|
|
|
|
pnt = (char *)buf;
|
|
|
|
size = 1;
|
|
|
|
goto no_pgmstring;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
pnt = va_arg (ap, char *);
|
|
|
|
size = strnlen (pnt, (flags & FL_PREC) ? prec : ~0);
|
|
|
|
no_pgmstring:
|
|
|
|
flags &= ~FL_PGMSTRING;
|
|
|
|
goto str_lpad;
|
|
|
|
|
|
|
|
case 'S':
|
|
|
|
// pgmstring: // not yet used
|
|
|
|
pnt = va_arg (ap, char *);
|
|
|
|
size = strnlen_P (pnt, (flags & FL_PREC) ? prec : ~0);
|
|
|
|
flags |= FL_PGMSTRING;
|
|
|
|
|
|
|
|
str_lpad:
|
|
|
|
if (!(flags & FL_LPAD)) {
|
|
|
|
while (size < width) {
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(' ');
|
2012-08-21 18:11:24 -03:00
|
|
|
width--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (size) {
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(GETBYTE (flags, FL_PGMSTRING, pnt));
|
2012-08-21 18:11:24 -03:00
|
|
|
if (width) width -= 1;
|
|
|
|
size -= 1;
|
|
|
|
}
|
|
|
|
goto tail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle integer formats variations for d/i, u, o, p, x, X.
|
|
|
|
*/
|
|
|
|
if (c == 'd' || c == 'i') {
|
2015-05-26 22:50:22 -03:00
|
|
|
if (flags & FL_LONGLONG) {
|
|
|
|
int64_t x = va_arg(ap,long long);
|
|
|
|
flags &= ~(FL_NEGATIVE | FL_ALT);
|
|
|
|
if (x < 0) {
|
|
|
|
x = -x;
|
|
|
|
flags |= FL_NEGATIVE;
|
|
|
|
}
|
|
|
|
c = ulltoa_invert (x, (char *)buf, 10) - (char *)buf;
|
|
|
|
} else {
|
|
|
|
long x = (flags & FL_LONG) ? va_arg(ap,long) : va_arg(ap,int);
|
|
|
|
flags &= ~(FL_NEGATIVE | FL_ALT);
|
|
|
|
if (x < 0) {
|
|
|
|
x = -x;
|
|
|
|
flags |= FL_NEGATIVE;
|
|
|
|
}
|
|
|
|
c = ultoa_invert (x, (char *)buf, 10) - (char *)buf;
|
2012-08-21 18:11:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
int base;
|
|
|
|
|
|
|
|
if (c == 'u') {
|
|
|
|
flags &= ~FL_ALT;
|
|
|
|
base = 10;
|
|
|
|
goto ultoa;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags &= ~(FL_PLUS | FL_SPACE);
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'o':
|
|
|
|
base = 8;
|
|
|
|
goto ultoa;
|
|
|
|
case 'p':
|
|
|
|
flags |= FL_ALT;
|
|
|
|
/* no break */
|
|
|
|
case 'x':
|
|
|
|
if (flags & FL_ALT)
|
|
|
|
flags |= FL_ALTHEX;
|
|
|
|
base = 16;
|
|
|
|
goto ultoa;
|
|
|
|
case 'X':
|
|
|
|
if (flags & FL_ALT)
|
|
|
|
flags |= (FL_ALTHEX | FL_ALTUPP);
|
|
|
|
base = 16 | XTOA_UPPER;
|
|
|
|
ultoa:
|
2015-05-26 22:50:22 -03:00
|
|
|
if (flags & FL_LONGLONG) {
|
|
|
|
c = ulltoa_invert (va_arg(ap, unsigned long long),
|
|
|
|
(char *)buf, base) - (char *)buf;
|
|
|
|
} else {
|
|
|
|
c = ultoa_invert ((flags & FL_LONG)
|
|
|
|
? va_arg(ap, unsigned long)
|
|
|
|
: va_arg(ap, unsigned int),
|
|
|
|
(char *)buf, base) - (char *)buf;
|
|
|
|
}
|
2012-08-21 18:11:24 -03:00
|
|
|
flags &= ~FL_NEGATIVE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Format integers.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
unsigned char len;
|
|
|
|
|
|
|
|
len = c;
|
|
|
|
if (flags & FL_PREC) {
|
|
|
|
flags &= ~FL_ZFILL;
|
|
|
|
if (len < prec) {
|
|
|
|
len = prec;
|
|
|
|
if ((flags & FL_ALT) && !(flags & FL_ALTHEX))
|
|
|
|
flags &= ~FL_ALT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags & FL_ALT) {
|
|
|
|
if (buf[c-1] == '0') {
|
|
|
|
flags &= ~(FL_ALT | FL_ALTHEX | FL_ALTUPP);
|
|
|
|
} else {
|
|
|
|
len += 1;
|
|
|
|
if (flags & FL_ALTHEX)
|
|
|
|
len += 1;
|
|
|
|
}
|
|
|
|
} else if (flags & (FL_NEGATIVE | FL_PLUS | FL_SPACE)) {
|
|
|
|
len += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & FL_LPAD)) {
|
|
|
|
if (flags & FL_ZFILL) {
|
|
|
|
prec = c;
|
|
|
|
if (len < width) {
|
|
|
|
prec += width - len;
|
|
|
|
len = width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (len < width) {
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(' ');
|
2012-08-21 18:11:24 -03:00
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
width = (len < width) ? width - len : 0;
|
|
|
|
|
|
|
|
if (flags & FL_ALT) {
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write('0');
|
2012-08-21 18:11:24 -03:00
|
|
|
if (flags & FL_ALTHEX)
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(flags & FL_ALTUPP ? 'X' : 'x');
|
2012-08-21 18:11:24 -03:00
|
|
|
} else if (flags & (FL_NEGATIVE | FL_PLUS | FL_SPACE)) {
|
|
|
|
unsigned char z = ' ';
|
|
|
|
if (flags & FL_PLUS) z = '+';
|
|
|
|
if (flags & FL_NEGATIVE) z = '-';
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(z);
|
2012-08-21 18:11:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
while (prec > c) {
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write('0');
|
2012-08-21 18:11:24 -03:00
|
|
|
prec--;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(buf[--c]);
|
2012-08-21 18:11:24 -03:00
|
|
|
} while (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
tail:
|
|
|
|
/* Tail is possible. */
|
|
|
|
while (width) {
|
2012-09-13 20:46:07 -03:00
|
|
|
s->write(' ');
|
2012-08-21 18:11:24 -03:00
|
|
|
width--;
|
|
|
|
}
|
|
|
|
} /* for (;;) */
|
|
|
|
}
|
2012-12-12 17:39:53 -04:00
|
|
|
|