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