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