Subversion Repositories libSmallPrintf

Rev

Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/*
2
        Copyright 2001, 2002 Georges Menie (www.menie.org)
3
        stdarg version contributed by Christian Ettinger
4
 
5 mjames 5
        This program is free software; you can redistribute it and/or modify
6
        it under the terms of the GNU Lesser General Public License as published by
7
        the Free Software Foundation; either version 2 of the License, or
8
        (at your option) any later version.
2 mjames 9
 
5 mjames 10
        This program is distributed in the hope that it will be useful,
11
        but WITHOUT ANY WARRANTY; without even the implied warranty of
12
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
        GNU Lesser General Public License for more details.
2 mjames 14
 
5 mjames 15
        You should have received a copy of the GNU Lesser General Public License
16
        along with this program; if not, write to the Free Software
17
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2 mjames 18
*/
19
 
20
/* Includes */
21
 
3 mjames 22
#include "libSmallPrintf/small_printf.h"
2 mjames 23
 
24
#include <stdarg.h>
25
#include <stdio.h>
26
#include <string.h>
27
 
28
/*
29
        putchar is the only external dependency for this file,
30
        if you have a working putchar, leave it commented out.
31
        If not, uncomment the define below and
32
        replace outbyte(c) by your own function call.
33
*/
6 mjames 34
#if defined SERIAL_UART1 || defined SERIAL_UART2 || defined SERIAL_UART3 || defined SERIAL_UART4
35
extern void PutCharSerial(char c);
36
#endif
2 mjames 37
 
38
static void printchar(char **str, int c)
39
{
40
 
5 mjames 41
        if (str)
42
        {
2 mjames 43
                **str = c;
44
                ++(*str);
45
        }
5 mjames 46
#if defined SERIAL_UART1 || defined SERIAL_UART2 || defined SERIAL_UART3 || defined SERIAL_UART4
47
        else
48
                (void)PutCharSerial(c);
49
#endif
2 mjames 50
}
51
 
6 mjames 52
char static const PAD_RIGHT = 1;
53
char static const PAD_ZERO = 2;
2 mjames 54
 
55
static int prints(char **out, const char *string, int width, int pad)
56
{
57
        register int pc = 0, padchar = ' ';
58
 
5 mjames 59
        if (width > 0)
60
        {
2 mjames 61
                register int len = 0;
62
                register const char *ptr;
5 mjames 63
                for (ptr = string; *ptr; ++ptr)
64
                        ++len;
65
                if (len >= width)
66
                        width = 0;
67
                else
68
                        width -= len;
69
                if (pad & PAD_ZERO)
70
                        padchar = '0';
2 mjames 71
        }
5 mjames 72
        if (!(pad & PAD_RIGHT))
73
        {
74
                for (; width > 0; --width)
75
                {
76
                        printchar(out, padchar);
2 mjames 77
                        ++pc;
78
                }
79
        }
5 mjames 80
        for (; *string; ++string)
81
        {
82
                printchar(out, *string);
2 mjames 83
                ++pc;
84
        }
5 mjames 85
        for (; width > 0; --width)
86
        {
87
                printchar(out, padchar);
2 mjames 88
                ++pc;
89
        }
90
 
91
        return pc;
92
}
93
 
94
/* the following should be enough for 32 bit int */
95
#define PRINT_BUF_LEN 12
96
 
97
static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
98
{
99
        char print_buf[PRINT_BUF_LEN];
100
        register char *s;
101
        register int t, neg = 0, pc = 0;
102
        register unsigned int u = i;
103
 
5 mjames 104
        if (i == 0)
105
        {
2 mjames 106
                print_buf[0] = '0';
107
                print_buf[1] = '\0';
5 mjames 108
                return prints(out, print_buf, width, pad);
2 mjames 109
        }
110
 
5 mjames 111
        if (sg && b == 10 && i < 0)
112
        {
2 mjames 113
                neg = 1;
114
                u = -i;
115
        }
116
 
5 mjames 117
        s = print_buf + PRINT_BUF_LEN - 1;
2 mjames 118
        *s = '\0';
119
 
5 mjames 120
        while (u)
121
        {
2 mjames 122
                t = u % b;
5 mjames 123
                if (t >= 10)
2 mjames 124
                        t += letbase - '0' - 10;
125
                *--s = t + '0';
126
                u /= b;
127
        }
128
 
5 mjames 129
        if (neg)
130
        {
131
                if (width && (pad & PAD_ZERO))
132
                {
133
                        printchar(out, '-');
2 mjames 134
                        ++pc;
135
                        --width;
136
                }
5 mjames 137
                else
138
                {
2 mjames 139
                        *--s = '-';
140
                }
141
        }
142
 
5 mjames 143
        return pc + prints(out, s, width, pad);
2 mjames 144
}
145
 
5 mjames 146
static int small_print(char **out, const char *format, va_list args)
2 mjames 147
{
148
        register int width, pad;
149
        register int pc = 0;
150
        char scr[2];
151
 
5 mjames 152
        for (; *format != 0; ++format)
153
        {
154
                if (*format == '%')
155
                {
2 mjames 156
                        ++format;
157
                        width = pad = 0;
5 mjames 158
                        if (*format == '\0')
159
                                break;
160
                        if (*format == '%')
161
                                goto out;
162
                        if (*format == '-')
163
                        {
2 mjames 164
                                ++format;
165
                                pad = PAD_RIGHT;
166
                        }
5 mjames 167
                        while (*format == '0')
168
                        {
2 mjames 169
                                ++format;
170
                                pad |= PAD_ZERO;
171
                        }
172
                        if ('*' == *format)
173
                        {
5 mjames 174
                                width = va_arg(args, int); // get width from next argument
2 mjames 175
                                format++;
176
                        }
177
                        else
178
                        {
5 mjames 179
                                for (; *format >= '0' && *format <= '9'; ++format)
2 mjames 180
                                {
181
                                        width *= 10;
182
                                        width += *format - '0';
183
                                }
184
                        }
5 mjames 185
                        if (*format == 'l')
186
                        { /* skip l in ld etc */
2 mjames 187
                                format++;
188
                        }
5 mjames 189
                        if (*format == 's')
190
                        {
191
                                register char *s = (char *)va_arg(args, int);
192
                                pc += prints(out, s ? s : "(null)", width, pad);
2 mjames 193
                                continue;
194
                        }
5 mjames 195
                        if (*format == 'd')
196
                        {
197
                                pc += printi(out, va_arg(args, int), 10, 1, width, pad, 'a');
2 mjames 198
                                continue;
199
                        }
5 mjames 200
                        if (*format == 'x')
201
                        {
202
                                pc += printi(out, va_arg(args, int), 16, 0, width, pad, 'a');
2 mjames 203
                                continue;
204
                        }
5 mjames 205
                        if (*format == 'X')
206
                        {
207
                                pc += printi(out, va_arg(args, int), 16, 0, width, pad, 'A');
2 mjames 208
                                continue;
209
                        }
5 mjames 210
                        if (*format == 'u')
211
                        {
212
                                pc += printi(out, va_arg(args, int), 10, 0, width, pad, 'a');
2 mjames 213
                                continue;
214
                        }
5 mjames 215
                        if (*format == 'c')
216
                        {
2 mjames 217
                                /* char are converted to int then pushed on the stack */
5 mjames 218
                                scr[0] = (char)va_arg(args, int);
2 mjames 219
                                scr[1] = '\0';
5 mjames 220
                                pc += prints(out, scr, width, pad);
2 mjames 221
                                continue;
222
                        }
5 mjames 223
                        if (*format == 'n')
224
                        { /* convert number printed so far and store in *va_arg( args, int)... */
6 mjames 225
                                int *dp = (int *)va_arg(args, int);
2 mjames 226
                                *dp = pc;
227
                                continue;
228
                        }
229
                }
5 mjames 230
                else
231
                {
2 mjames 232
                out:
233
                        /* deal with LF -> CRLF mapping */
5 mjames 234
                        if (*format == '\n')
2 mjames 235
                        {
5 mjames 236
                                printchar(out, '\r');
3 mjames 237
                                ++pc; // extra char
2 mjames 238
                        }
5 mjames 239
                        printchar(out, *format);
2 mjames 240
                        ++pc;
241
                }
242
        }
5 mjames 243
        if (out)
244
                **out = '\0';
2 mjames 245
        return pc;
246
}
247
 
248
int small_printf(const char *format, ...)
249
{
250
        int result = 0;
5 mjames 251
        va_list args;
2 mjames 252
 
5 mjames 253
        va_start(args, format);
254
        result = small_print(0, format, args);
255
        va_end(args);
256
        return result;
2 mjames 257
}
258
 
259
int small_sprintf(char *out, const char *format, ...)
260
{
5 mjames 261
        int result = 0;
262
        va_list args;
263
        va_start(args, format);
264
        result = small_print(&out, format, args);
265
        va_end(args);
266
        return result;
2 mjames 267
}