migrate substitution keywords to SVN
[reactos.git] / reactos / lib / ntdll / stdio / sprintf.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: lib/ntdll/stdio/sprintf.c
6 * PURPOSE: Single byte sprintf functions
7 * PROGRAMMERS: David Welch
8 * Eric Kohl
9 *
10 */
11
12 /*
13 * linux/lib/vsprintf.c
14 *
15 * Copyright (C) 1991, 1992 Linus Torvalds
16 */
17
18 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
19 /*
20 * Wirzenius wrote this portably, Torvalds fucked it up :-)
21 */
22
23 #include <ddk/ntddk.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #define __NO_CTYPE_INLINES
27 #include <ctype.h>
28 #include <string.h>
29 #include <limits.h>
30
31 #define NDEBUG
32 #include <ntdll/ntdll.h>
33
34
35 #define ZEROPAD 1 /* pad with zero */
36 #define SIGN 2 /* unsigned/signed long */
37 #define PLUS 4 /* show plus */
38 #define SPACE 8 /* space if plus */
39 #define LEFT 16 /* left justified */
40 #define SPECIAL 32 /* 0x */
41 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
42
43
44 #define do_div(n,base) ({ \
45 int __res; \
46 __res = ((unsigned long long) n) % (unsigned) base; \
47 n = ((unsigned long long) n) / (unsigned) base; \
48 __res; })
49
50
51 static int skip_atoi(const char **s)
52 {
53 int i=0;
54
55 while (isdigit(**s))
56 i = i*10 + *((*s)++) - '0';
57 return i;
58 }
59
60
61 static char *
62 number(char * buf, char * end, long long num, int base, int size, int precision, int type)
63 {
64 char c,sign,tmp[66];
65 const char *digits;
66 const char *small_digits = "0123456789abcdefghijklmnopqrstuvwxyz";
67 const char *large_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
68 int i;
69
70 digits = (type & LARGE) ? large_digits : small_digits;
71 if (type & LEFT)
72 type &= ~ZEROPAD;
73 if (base < 2 || base > 36)
74 return 0;
75 c = (type & ZEROPAD) ? '0' : ' ';
76 sign = 0;
77 if (type & SIGN) {
78 if (num < 0) {
79 sign = '-';
80 num = -num;
81 size--;
82 } else if (type & PLUS) {
83 sign = '+';
84 size--;
85 } else if (type & SPACE) {
86 sign = ' ';
87 size--;
88 }
89 }
90 if (type & SPECIAL) {
91 if (base == 16)
92 size -= 2;
93 else if (base == 8)
94 size--;
95 }
96 i = 0;
97 if (num == 0)
98 tmp[i++]='0';
99 else while (num != 0)
100 tmp[i++] = digits[do_div(num,base)];
101 if (i > precision)
102 precision = i;
103 size -= precision;
104 if (!(type&(ZEROPAD+LEFT))) {
105 while(size-->0) {
106 if (buf <= end)
107 *buf = ' ';
108 ++buf;
109 }
110 }
111 if (sign) {
112 if (buf <= end)
113 *buf = sign;
114 ++buf;
115 }
116 if (type & SPECIAL) {
117 if (base==8) {
118 if (buf <= end)
119 *buf = '0';
120 ++buf;
121 } else if (base==16) {
122 if (buf <= end)
123 *buf = '0';
124 ++buf;
125 if (buf <= end)
126 *buf = digits[33];
127 ++buf;
128 }
129 }
130 if (!(type & LEFT)) {
131 while (size-- > 0) {
132 if (buf <= end)
133 *buf = c;
134 ++buf;
135 }
136 }
137 while (i < precision--) {
138 if (buf <= end)
139 *buf = '0';
140 ++buf;
141 }
142 while (i-- > 0) {
143 if (buf <= end)
144 *buf = tmp[i];
145 ++buf;
146 }
147 while (size-- > 0) {
148 if (buf <= end)
149 *buf = ' ';
150 ++buf;
151 }
152 return buf;
153 }
154
155 static char*
156 string(char* buf, char* end, const char* s, int len, int field_width, int precision, int flags)
157 {
158 int i;
159 if (s == NULL)
160 {
161 s = "<NULL>";
162 len = 6;
163 }
164 else
165 {
166 if (len == -1)
167 {
168 len = 0;
169 while ((unsigned int)len < (unsigned int)precision && s[len])
170 len++;
171 }
172 else
173 {
174 if ((unsigned int)len > (unsigned int)precision)
175 len = precision;
176 }
177 }
178 if (!(flags & LEFT))
179 while (len < field_width--)
180 {
181 if (buf <= end)
182 *buf = ' ';
183 ++buf;
184 }
185 for (i = 0; i < len; ++i)
186 {
187 if (buf <= end)
188 *buf = *s++;
189 ++buf;
190 }
191 while (len < field_width--)
192 {
193 if (buf <= end)
194 *buf = ' ';
195 ++buf;
196 }
197 return buf;
198 }
199
200 static char*
201 stringw(char* buf, char* end, const wchar_t* sw, int len, int field_width, int precision, int flags)
202 {
203 int i;
204 if (sw == NULL)
205 {
206 sw = L"<NULL>";
207 len = 6;
208 }
209 else
210 {
211 if (len == -1)
212 {
213 len = 0;
214 while ((unsigned int)len < (unsigned int)precision && sw[len])
215 len++;
216 }
217 else
218 {
219 if ((unsigned int)len > (unsigned int)precision)
220 len = precision;
221 }
222 }
223 if (!(flags & LEFT))
224 while (len < field_width--)
225 {
226 if (buf <= end)
227 *buf = ' ';
228 ++buf;
229 }
230 for (i = 0; i < len; ++i)
231 {
232 if (buf <= end)
233 *buf = (unsigned char)(*sw++);
234 ++buf;
235 }
236 while (len < field_width--)
237 {
238 if (buf <= end)
239 *buf = ' ';
240 ++buf;
241 }
242 return buf;
243 }
244
245 /*
246 * @implemented
247 */
248 int _vsnprintf(char *buf, size_t cnt, const char *fmt, va_list args)
249 {
250 int len;
251 unsigned long long num;
252 int base;
253 char *str, *end;
254 const char *s;
255 const wchar_t *sw;
256
257 int flags; /* flags to number() */
258
259 int field_width; /* width of output field */
260 int precision; /* min. # of digits for integers; max
261 number of chars for from string */
262 int qualifier; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
263
264 str = buf;
265 end = buf + cnt - 1;
266 if (end < buf - 1) {
267 end = ((void *) -1);
268 cnt = end - buf + 1;
269 }
270
271 for ( ; *fmt ; ++fmt) {
272 if (*fmt != '%') {
273 if (str <= end)
274 *str = *fmt;
275 ++str;
276 continue;
277 }
278
279 /* process flags */
280 flags = 0;
281 repeat:
282 ++fmt; /* this also skips first '%' */
283 switch (*fmt) {
284 case '-': flags |= LEFT; goto repeat;
285 case '+': flags |= PLUS; goto repeat;
286 case ' ': flags |= SPACE; goto repeat;
287 case '#': flags |= SPECIAL; goto repeat;
288 case '0': flags |= ZEROPAD; goto repeat;
289 }
290
291 /* get field width */
292 field_width = -1;
293 if (isdigit(*fmt))
294 field_width = skip_atoi(&fmt);
295 else if (*fmt == '*') {
296 ++fmt;
297 /* it's the next argument */
298 field_width = va_arg(args, int);
299 if (field_width < 0) {
300 field_width = -field_width;
301 flags |= LEFT;
302 }
303 }
304
305 /* get the precision */
306 precision = -1;
307 if (*fmt == '.') {
308 ++fmt;
309 if (isdigit(*fmt))
310 precision = skip_atoi(&fmt);
311 else if (*fmt == '*') {
312 ++fmt;
313 /* it's the next argument */
314 precision = va_arg(args, int);
315 }
316 if (precision < 0)
317 precision = 0;
318 }
319
320 /* get the conversion qualifier */
321 qualifier = -1;
322 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'w') {
323 qualifier = *fmt;
324 ++fmt;
325 } else if (*fmt == 'I' && *(fmt+1) == '6' && *(fmt+2) == '4') {
326 qualifier = *fmt;
327 fmt += 3;
328 }
329
330 /* default base */
331 base = 10;
332
333 switch (*fmt) {
334 case 'c': /* finished */
335 if (!(flags & LEFT))
336 while (--field_width > 0) {
337 if (str <= end)
338 *str = ' ';
339 ++str;
340 }
341 if (qualifier == 'l' || qualifier == 'w') {
342 if (str <= end)
343 *str = (unsigned char)(wchar_t) va_arg(args, int);
344 ++str;
345 } else {
346 if (str <= end)
347 *str = (unsigned char) va_arg(args, int);
348 ++str;
349 }
350 while (--field_width > 0) {
351 if (str <= end)
352 *str = ' ';
353 ++str;
354 }
355 continue;
356
357 case 'C': /* finished */
358 if (!(flags & LEFT))
359 while (--field_width > 0) {
360 if (str <= end)
361 *str = ' ';
362 ++str;
363 }
364 if (qualifier == 'h') {
365 if (str <= end)
366 *str = (unsigned char) va_arg(args, int);
367 ++str;
368 } else {
369 if (str <= end)
370 *str = (unsigned char)(wchar_t) va_arg(args, int);
371 ++str;
372 }
373 while (--field_width > 0) {
374 if (str <= end)
375 *str = ' ';
376 ++str;
377 }
378 continue;
379
380 case 's': /* finished */
381 if (qualifier == 'l' || qualifier == 'w') {
382 /* print unicode string */
383 sw = va_arg(args, wchar_t *);
384 str = stringw(str, end, sw, -1, field_width, precision, flags);
385 } else {
386 /* print ascii string */
387 s = va_arg(args, char *);
388 str = string(str, end, s, -1, field_width, precision, flags);
389 }
390 continue;
391
392 case 'S':
393 if (qualifier == 'h') {
394 /* print ascii string */
395 s = va_arg(args, char *);
396 str = string(str, end, s, -1, field_width, precision, flags);
397 } else {
398 /* print unicode string */
399 sw = va_arg(args, wchar_t *);
400 str = stringw(str, end, sw, -1, field_width, precision, flags);
401 }
402 continue;
403
404 case 'Z':
405 if (qualifier == 'w') {
406 /* print counted unicode string */
407 PUNICODE_STRING pus = va_arg(args, PUNICODE_STRING);
408 if ((pus == NULL) || (pus->Buffer == NULL)) {
409 sw = NULL;
410 len = -1;
411 } else {
412 sw = pus->Buffer;
413 len = pus->Length / sizeof(WCHAR);
414 }
415 str = stringw(str, end, sw, len, field_width, precision, flags);
416 } else {
417 /* print counted ascii string */
418 PANSI_STRING pus = va_arg(args, PANSI_STRING);
419 if ((pus == NULL) || (pus->Buffer == NULL)) {
420 s = NULL;
421 len = -1;
422 } else {
423 s = pus->Buffer;
424 len = pus->Length;
425 }
426 str = string(str, end, s, len, field_width, precision, flags);
427 }
428 continue;
429
430 case 'p':
431 if (field_width == -1) {
432 field_width = 2 * sizeof(void *);
433 flags |= ZEROPAD;
434 }
435 str = number(str, end,
436 (unsigned long) va_arg(args, void *), 16,
437 field_width, precision, flags);
438 continue;
439
440 case 'n':
441 /* FIXME: What does C99 say about the overflow case here? */
442 if (qualifier == 'l') {
443 long * ip = va_arg(args, long *);
444 *ip = (str - buf);
445 } else {
446 int * ip = va_arg(args, int *);
447 *ip = (str - buf);
448 }
449 continue;
450
451 /* integer number formats - set up the flags and "break" */
452 case 'o':
453 base = 8;
454 break;
455
456 case 'b':
457 base = 2;
458 break;
459
460 case 'X':
461 flags |= LARGE;
462 case 'x':
463 base = 16;
464 break;
465
466 case 'd':
467 case 'i':
468 flags |= SIGN;
469 case 'u':
470 break;
471
472 default:
473 if (*fmt != '%') {
474 if (str <= end)
475 *str = '%';
476 ++str;
477 }
478 if (*fmt) {
479 if (str <= end)
480 *str = *fmt;
481 ++str;
482 } else
483 --fmt;
484 continue;
485 }
486
487 if (qualifier == 'I')
488 num = va_arg(args, unsigned long long);
489 else if (qualifier == 'l') {
490 if (flags & SIGN)
491 num = va_arg(args, long);
492 else
493 num = va_arg(args, unsigned long);
494 }
495 else if (qualifier == 'h') {
496 if (flags & SIGN)
497 num = va_arg(args, int);
498 else
499 num = va_arg(args, unsigned int);
500 }
501 else {
502 if (flags & SIGN)
503 num = va_arg(args, int);
504 else
505 num = va_arg(args, unsigned int);
506 }
507 str = number(str, end, num, base, field_width, precision, flags);
508 }
509 if (str <= end)
510 *str = '\0';
511 else if (cnt > 0)
512 /* don't write out a null byte if the buf size is zero */
513 *end = '\0';
514 return str-buf;
515 }
516
517
518 /*
519 * @implemented
520 */
521 int sprintf(char * buf, const char *fmt, ...)
522 {
523 va_list args;
524 int i;
525
526 va_start(args, fmt);
527 i=_vsnprintf(buf,INT_MAX,fmt,args);
528 va_end(args);
529 return i;
530 }
531
532
533 /*
534 * @implemented
535 */
536 int _snprintf(char * buf, size_t cnt, const char *fmt, ...)
537 {
538 va_list args;
539 int i;
540
541 va_start(args, fmt);
542 i=_vsnprintf(buf,cnt,fmt,args);
543 va_end(args);
544 return i;
545 }
546
547
548 /*
549 * @implemented
550 */
551 int vsprintf(char *buf, const char *fmt, va_list args)
552 {
553 return _vsnprintf(buf,INT_MAX,fmt,args);
554 }
555
556 /* EOF */