comparison gsm-fw/sprintf/integer.c @ 145:7e45ada9c365

gsm-fw: sprintf overhaul in preparation for adding %f format support
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Thu, 14 Nov 2013 17:51:33 +0000
parents
children
comparison
equal deleted inserted replaced
144:ea819b60fe0d 145:7e45ada9c365
1 /*
2 * Embedded [v]sprintf() implementation by Michael Spacefalcon,
3 * loosely based on the 4.3BSD-Tahoe version.
4 *
5 * This module contains the integer conversion functions.
6 */
7
8 #include <sys/types.h>
9 #include <ctype.h>
10 #include "defs.h"
11
12 extern u_char * _sprintf_field(u_char *op, int width, int flags, int sign,
13 u_char *body, int size, int dprec, int fpprec);
14
15 static const char lcdigits[] = "0123456789abcdef";
16 static const char ucdigits[] = "0123456789ABCDEF";
17
18 u_char *
19 _sprintf_integer(u_char *op, int width, int flags, int sign,
20 unsigned number, int base, int prec)
21 {
22 const char *digits;
23 char buf[12];
24 char *t, *endp;
25
26 /*
27 * ``... diouXx conversions ... if a precision is
28 * specified, the 0 flag will be ignored.''
29 * -- ANSI X3J11
30 */
31 if (prec >= 0)
32 flags &= ~ZEROPAD;
33
34 if (flags & UPPERCASE)
35 digits = ucdigits;
36 else
37 digits = lcdigits;
38
39 /*
40 * ``The result of converting a zero value with an
41 * explicit precision of zero is no characters.''
42 * -- ANSI X3J11
43 */
44 t = endp = buf + sizeof(buf);
45 if (number != 0 || prec != 0) {
46 do {
47 *--t = digits[number % base];
48 number /= base;
49 } while (number);
50 if (flags & ALT && base == 8 && *t != '0')
51 *--t = '0'; /* octal leading 0 */
52 }
53 return _sprintf_field(op, width, flags, sign, t, endp - t, prec, 0);
54 }