summaryrefslogtreecommitdiff
path: root/firmware/common/sprintf.c
blob: 80bdda63363568dfc47310c8244553bc48c2364c (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Gary Czvitkovicz
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

/*
 * Minimal printf and snprintf formatting functions
 *
 * These support %c %s %d and %x
 * Field width and zero-padding flag only
 */

#include <stdarg.h>
#include <string.h>

static const char hexdigit[] = "0123456789ABCDEF";

int vsnprintf (char *buf, int size, const char *fmt, va_list ap)
{
    char *bp = buf;
    char *end = buf + size - 1;

    char *str;
    char tmpbuf[12], pad;
    int ch, width, val, sign;

    tmpbuf[sizeof tmpbuf - 1] = '\0';

    while ((ch = *fmt++) != '\0' && bp < end)
    {
	if (ch == '%')
	{
	    ch = *fmt++;
	    pad = ' ';
	    if (ch == '0')
		pad = '0';

	    width = 0;
	    while (ch >= '0' && ch <= '9')
	    {
		width = 10*width + ch - '0';
		ch = *fmt++;
	    }

	    str = tmpbuf + sizeof tmpbuf - 1;
	    switch (ch)
	    {
		case 'c':
		    *--str = va_arg (ap, int);
		    break;

		case 's':
		    str = va_arg (ap, char*);
		    break;

		case 'd':
		    val = sign = va_arg (ap, int);
		    if (val < 0)
			val = -val;
		    do
		    {
			*--str = (val % 10) + '0';
			val /= 10;
		    }
		    while (val > 0);
		    if (sign < 0)
			*--str = '-';
		    break;

		case 'x':
		case 'X':
		    val = va_arg (ap, int);
		    do
		    {
			*--str = hexdigit[val & 0xf];
			val >>= 4;
		    }
		    while (val > 0);
		    break;

		default:
		    *--str = ch;
		    break;
	    }

	    if (width > 0)
	    {
		width -= strlen (str);
		while (width-- > 0 && buf < end)
		    *bp++ = pad;
	    }
	    while (*str != '\0' && buf < end)
		*bp++ = *str++;
	}
	else
	    *bp++ = ch;
    }

    *bp++ = '\0';
    return bp - buf - 1;
}

int snprintf (char *buf, int size, const char *fmt, ...)
{
    int n;
    va_list ap;

    va_start (ap, fmt);
    n = vsnprintf (buf, size, fmt, ap);
    va_end (ap);

    return n;
}