Sync to trunk (r46918)
[reactos.git] / drivers / video / font / ftfd / sprintf.c
1 /*
2 * PROGRAMMERS: David Welch
3 * Eric Kohl
4 *
5 * TODO:
6 * - Verify the implementation of '%Z'.
7 */
8
9 /*
10 * linux/lib/vsprintf.c
11 *
12 * Copyright (C) 1991, 1992 Linus Torvalds
13 */
14
15 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
16 /*
17 * Wirzenius wrote this portably, Torvalds fucked it up :-)
18 */
19
20 #define WIN32_NO_STATUS
21 #include <windows.h>
22 #include <ndk/ntndk.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 #define ZEROPAD 1 /* pad with zero */
27 #define SIGN 2 /* unsigned/signed long */
28 #define PLUS 4 /* show plus */
29 #define SPACE 8 /* space if plus */
30 #define LEFT 16 /* left justified */
31 #define SPECIAL 32 /* 0x */
32 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
33 #define REMOVEHEX 256 /* use 256 as remve 0x frim BASE 16 */
34 typedef struct {
35 unsigned int mantissal:32;
36 unsigned int mantissah:20;
37 unsigned int exponent:11;
38 unsigned int sign:1;
39 } double_t;
40
41 static
42 __inline
43 int
44 _isinf(double __x)
45 {
46 union
47 {
48 double* __x;
49 double_t* x;
50 } x;
51
52 x.__x = &__x;
53 return ( x.x->exponent == 0x7ff && ( x.x->mantissah == 0 && x.x->mantissal == 0 ));
54 }
55
56 static
57 __inline
58 int
59 _isnan(double __x)
60 {
61 union
62 {
63 double* __x;
64 double_t* x;
65 } x;
66 x.__x = &__x;
67 return ( x.x->exponent == 0x7ff && ( x.x->mantissah != 0 || x.x->mantissal != 0 ));
68 }
69
70
71 static
72 __inline
73 int
74 do_div(long long *n, int base)
75 {
76 int a;
77 a = ((unsigned long long) *n) % (unsigned) base;
78 *n = ((unsigned long long) *n) / (unsigned) base;
79 return a;
80 }
81
82
83 static int skip_atoi(const char **s)
84 {
85 int i=0;
86
87 while (isdigit(**s))
88 i = i*10 + *((*s)++) - '0';
89 return i;
90 }
91
92
93 static char *
94 number(char * buf, char * end, long long num, int base, int size, int precision, int type)
95 {
96 char c,sign,tmp[66];
97 const char *digits;
98 const char *small_digits = "0123456789abcdefghijklmnopqrstuvwxyz";
99 const char *large_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
100 int i;
101
102 digits = (type & LARGE) ? large_digits : small_digits;
103 if (type & LEFT)
104 type &= ~ZEROPAD;
105 if (base < 2 || base > 36)
106 return 0;
107 c = (type & ZEROPAD) ? '0' : ' ';
108 sign = 0;
109 if (type & SIGN) {
110 if (num < 0) {
111 sign = '-';
112 num = -num;
113 size--;
114 } else if (type & PLUS) {
115 sign = '+';
116 size--;
117 } else if (type & SPACE) {
118 sign = ' ';
119 size--;
120 }
121 }
122
123 if ((type & SPECIAL) && ((type & REMOVEHEX) == 0)) {
124 if (base == 16)
125 size -= 2;
126
127 }
128 i = 0;
129 if ((num == 0) && (precision !=0))
130 tmp[i++] = '0';
131 else while (num != 0)
132 tmp[i++] = digits[do_div(&num,base)];
133 if (i > precision)
134 precision = i;
135 size -= precision;
136 if (!(type&(ZEROPAD+LEFT))) {
137 while(size-->0) {
138 if (buf <= end)
139 *buf = ' ';
140 ++buf;
141 }
142 }
143 if (sign) {
144 if (buf <= end)
145 *buf = sign;
146 ++buf;
147 }
148
149 if ((type & SPECIAL) && ((type & REMOVEHEX) == 0)) {
150 if (base==16) {
151 if (buf <= end)
152 *buf = '0';
153 ++buf;
154 if (buf <= end)
155 *buf = digits[33];
156 ++buf;
157 }
158 }
159
160 if (!(type & LEFT)) {
161 while (size-- > 0) {
162 if (buf <= end)
163 *buf = c;
164 ++buf;
165 }
166 }
167 while (i < precision--) {
168 if (buf <= end)
169 *buf = '0';
170 ++buf;
171 }
172 while (i-- > 0) {
173 if (buf <= end)
174 *buf = tmp[i];
175 ++buf;
176 }
177 while (size-- > 0) {
178 if (buf <= end)
179 *buf = ' ';
180 ++buf;
181 }
182
183 return buf;
184 }
185
186 static char *
187 numberf(char * buf, char * end, double num, int base, int size, int precision, int type)
188 {
189 char c,sign,tmp[66];
190 const char *digits;
191 const char *small_digits = "0123456789abcdefghijklmnopqrstuvwxyz";
192 const char *large_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
193 int i;
194 long long x;
195
196 /* FIXME
197 the float version of number is direcly copy of number
198 */
199
200 digits = (type & LARGE) ? large_digits : small_digits;
201 if (type & LEFT)
202 type &= ~ZEROPAD;
203 if (base < 2 || base > 36)
204 return 0;
205 c = (type & ZEROPAD) ? '0' : ' ';
206 sign = 0;
207 if (type & SIGN) {
208 if (num < 0) {
209 sign = '-';
210 num = -num;
211 size--;
212 } else if (type & PLUS) {
213 sign = '+';
214 size--;
215 } else if (type & SPACE) {
216 sign = ' ';
217 size--;
218 }
219 }
220 if (type & SPECIAL) {
221 if (base == 16)
222 size -= 2;
223 else if (base == 8)
224 size--;
225 }
226 i = 0;
227 if (num == 0)
228 tmp[i++] = '0';
229 else while (num != 0)
230 {
231 x = num;
232 tmp[i++] = digits[do_div(&x,base)];
233 num=x;
234 }
235 if (i > precision)
236 precision = i;
237 size -= precision;
238 if (!(type&(ZEROPAD+LEFT))) {
239 while(size-->0) {
240 if (buf <= end)
241 *buf = ' ';
242 ++buf;
243 }
244 }
245 if (sign) {
246 if (buf <= end)
247 *buf = sign;
248 ++buf;
249 }
250 if (type & SPECIAL) {
251 if (base==8) {
252 if (buf <= end)
253 *buf = '0';
254 ++buf;
255 } else if (base==16) {
256 if (buf <= end)
257 *buf = '0';
258 ++buf;
259 if (buf <= end)
260 *buf = digits[33];
261 ++buf;
262 }
263 }
264 if (!(type & LEFT)) {
265 while (size-- > 0) {
266 if (buf <= end)
267 *buf = c;
268 ++buf;
269 }
270 }
271 while (i < precision--) {
272 if (buf <= end)
273 *buf = '0';
274 ++buf;
275 }
276 while (i-- > 0) {
277 if (buf <= end)
278 *buf = tmp[i];
279 ++buf;
280 }
281 while (size-- > 0) {
282 if (buf <= end)
283 *buf = ' ';
284 ++buf;
285 }
286 return buf;
287 }
288
289 static char*
290 string(char* buf, char* end, const char* s, int len, int field_width, int precision, int flags)
291 {
292 int i;
293 char c;
294
295 c = (flags & ZEROPAD) ? '0' : ' ';
296
297 if (s == NULL)
298 {
299 s = "<NULL>";
300 len = 6;
301 }
302 else
303 {
304 if (len == -1)
305 {
306 len = 0;
307 while ((unsigned int)len < (unsigned int)precision && s[len])
308 len++;
309 }
310 else
311 {
312 if ((unsigned int)len > (unsigned int)precision)
313 len = precision;
314 }
315 }
316 if (!(flags & LEFT))
317 while (len < field_width--)
318 {
319 if (buf <= end)
320 *buf = c;
321 ++buf;
322 }
323 for (i = 0; i < len; ++i)
324 {
325 if (buf <= end)
326 *buf = *s++;
327 ++buf;
328 }
329 while (len < field_width--)
330 {
331 if (buf <= end)
332 *buf = ' ';
333 ++buf;
334 }
335 return buf;
336 }
337
338 static char*
339 stringw(char* buf, char* end, const wchar_t* sw, int len, int field_width, int precision, int flags)
340 {
341 int i;
342 char c;
343
344 c = (flags & ZEROPAD) ? '0' : ' ';
345
346 if (sw == NULL)
347 {
348 sw = L"<NULL>";
349 len = 6;
350 }
351 else
352 {
353 if (len == -1)
354 {
355 len = 0;
356 while ((unsigned int)len < (unsigned int)precision && sw[len])
357 len++;
358 }
359 else
360 {
361 if ((unsigned int)len > (unsigned int)precision)
362 len = precision;
363 }
364 }
365 if (!(flags & LEFT))
366 while (len < field_width--)
367 {
368 if (buf <= end)
369 *buf = c;
370 buf++;
371 }
372 for (i = 0; i < len; ++i)
373 {
374 if (buf <= end)
375 *buf = (unsigned char)(*sw++);
376 buf++;
377 }
378 while (len < field_width--)
379 {
380 if (buf <= end)
381 *buf = ' ';
382 buf++;
383 }
384 return buf;
385 }
386
387 /*
388 * @implemented
389 */
390 int __cdecl _vsnprintf(char *buf, size_t cnt, const char *fmt, va_list args)
391 {
392 int len;
393 unsigned long long num;
394 double _double;
395
396 int base;
397 char *str, *end;
398 const char *s;
399 const wchar_t *sw;
400
401 int flags; /* flags to number() */
402
403 int field_width; /* width of output field */
404 int precision; /* min. # of digits for integers; max
405 number of chars for from string */
406 int qualifier; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
407
408 /* clear the string buffer with zero so we do not need NULL terment it at end */
409
410 str = buf;
411 end = buf + cnt - 1;
412 if (end < buf - 1) {
413 end = ((char *) -1);
414 cnt = end - buf + 1;
415 }
416
417 for ( ; *fmt ; ++fmt) {
418 if (*fmt != '%') {
419 if (str <= end)
420 *str = *fmt;
421 ++str;
422 continue;
423 }
424
425 /* process flags */
426 flags = 0;
427 repeat:
428 ++fmt; /* this also skips first '%' */
429 switch (*fmt) {
430 case '-': flags |= LEFT; goto repeat;
431 case '+': flags |= PLUS; goto repeat;
432 case ' ': flags |= SPACE; goto repeat;
433 case '#': flags |= SPECIAL; goto repeat;
434 case '0': flags |= ZEROPAD; goto repeat;
435 }
436
437 /* get field width */
438 field_width = -1;
439 if (isdigit(*fmt))
440 field_width = skip_atoi(&fmt);
441 else if (*fmt == '*') {
442 ++fmt;
443 /* it's the next argument */
444 field_width = va_arg(args, int);
445 if (field_width < 0) {
446 field_width = -field_width;
447 flags |= LEFT;
448 }
449 }
450
451 /* get the precision */
452 precision = -1;
453 if (*fmt == '.') {
454 ++fmt;
455 if (isdigit(*fmt))
456 precision = skip_atoi(&fmt);
457 else if (*fmt == '*') {
458 ++fmt;
459 /* it's the next argument */
460 precision = va_arg(args, int);
461 }
462 if (precision < 0)
463 precision = 0;
464 }
465
466 /* get the conversion qualifier */
467 qualifier = -1;
468 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'w') {
469 qualifier = *fmt;
470 ++fmt;
471 } else if (*fmt == 'I' && *(fmt+1) == '6' && *(fmt+2) == '4') {
472 qualifier = *fmt;
473 fmt += 3;
474 } else if (*fmt == 'I' && *(fmt+1) == '3' && *(fmt+2) == '2') {
475 qualifier = 'l';
476 fmt += 3;
477 } else if (*fmt == 'F' && *(fmt+1) == 'p') {
478 fmt += 1;
479 flags |= REMOVEHEX;
480 }
481
482 /* default base */
483 base = 10;
484
485 switch (*fmt) {
486 case 'c': /* finished */
487 if (qualifier == 'l' || qualifier == 'w') {
488 wchar_t sw1[2];
489 /* print unicode string */
490 sw1[0] = (wchar_t) va_arg(args, int);
491 sw1[1] = 0;
492 str = stringw(str, end, (wchar_t *)&sw1, -1, field_width, precision, flags);
493 } else {
494 char s1[2];
495 /* print ascii string */
496 s1[0] = ( unsigned char) va_arg(args, int);
497 s1[1] = 0;
498 str = string(str, end, (char *)&s1, -1, field_width, precision, flags);
499 }
500 continue;
501
502 case 'C': /* finished */
503 if (!(flags & LEFT))
504 while (--field_width > 0) {
505 if (str <= end)
506 *str = ' ';
507 ++str;
508 }
509 if (qualifier == 'h') {
510 if (str <= end)
511 *str = (unsigned char) va_arg(args, int);
512 ++str;
513 } else {
514 if (str <= end)
515 *str = (unsigned char)(wchar_t) va_arg(args, int);
516 ++str;
517 }
518 while (--field_width > 0) {
519 if (str <= end)
520 *str = ' ';
521 ++str;
522 }
523 continue;
524
525 case 's': /* finished */
526 if (qualifier == 'l' || qualifier == 'w') {
527 /* print unicode string */
528 sw = va_arg(args, wchar_t *);
529 str = stringw(str, end, sw, -1, field_width, precision, flags);
530 } else {
531 /* print ascii string */
532 s = va_arg(args, char *);
533 str = string(str, end, s, -1, field_width, precision, flags);
534 }
535 continue;
536
537 case 'S':
538 if (qualifier == 'h') {
539 /* print ascii string */
540 s = va_arg(args, char *);
541 str = string(str, end, s, -1, field_width, precision, flags);
542 } else {
543 /* print unicode string */
544 sw = va_arg(args, wchar_t *);
545 str = stringw(str, end, sw, -1, field_width, precision, flags);
546 }
547 continue;
548
549 case 'Z':
550 if (qualifier == 'w') {
551 /* print counted unicode string */
552 PUNICODE_STRING pus = va_arg(args, PUNICODE_STRING);
553 if ((pus == NULL) || (pus->Buffer == NULL)) {
554 sw = NULL;
555 len = -1;
556 } else {
557 sw = pus->Buffer;
558 len = pus->Length / sizeof(WCHAR);
559 }
560 str = stringw(str, end, sw, len, field_width, precision, flags);
561 } else {
562 /* print counted ascii string */
563 PANSI_STRING pus = va_arg(args, PANSI_STRING);
564 if ((pus == NULL) || (pus->Buffer == NULL)) {
565 s = NULL;
566 len = -1;
567 } else {
568 s = pus->Buffer;
569 len = pus->Length;
570 }
571 str = string(str, end, s, len, field_width, precision, flags);
572 }
573 continue;
574
575 case 'p':
576 if ((flags & LARGE) == 0)
577 flags |= LARGE;
578
579 if (field_width == -1) {
580 field_width = 2 * sizeof(void *);
581 flags |= ZEROPAD;
582 }
583 str = number(str, end,
584 (ULONG_PTR) va_arg(args, void *), 16,
585 field_width, precision, flags);
586 continue;
587
588 case 'n':
589 /* FIXME: What does C99 say about the overflow case here? */
590 if (qualifier == 'l') {
591 long * ip = va_arg(args, long *);
592 *ip = (str - buf);
593 } else {
594 int * ip = va_arg(args, int *);
595 *ip = (str - buf);
596 }
597 continue;
598
599 /* float number formats - set up the flags and "break" */
600 case 'e':
601 case 'E':
602 case 'f':
603 case 'g':
604 case 'G':
605 _double = (double)va_arg(args, double);
606 if ( _isnan(_double) ) {
607 s = "Nan";
608 len = 3;
609 while ( len > 0 ) {
610 if (str <= end)
611 *str = *s++;
612 ++str;
613 len --;
614 }
615 } else if ( _isinf(_double) < 0 ) {
616 s = "-Inf";
617 len = 4;
618 while ( len > 0 ) {
619 if (str <= end)
620 *str = *s++;
621 ++str;
622 len --;
623 }
624 } else if ( _isinf(_double) > 0 ) {
625 s = "+Inf";
626 len = 4;
627 while ( len > 0 ) {
628 if (str <= end)
629 *str = *s++;
630 ++str;
631 len --;
632 }
633 } else {
634 if ( precision == -1 )
635 precision = 6;
636 str = numberf(str, end, (int)_double, base, field_width, precision, flags);
637 }
638
639 continue;
640
641
642 /* integer number formats - set up the flags and "break" */
643 case 'o':
644 base = 8;
645 break;
646
647 case 'b':
648 base = 2;
649 break;
650
651 case 'X':
652 flags |= LARGE;
653 case 'x':
654 base = 16;
655 break;
656
657 case 'd':
658 case 'i':
659 flags |= SIGN;
660 case 'u':
661 break;
662
663 default:
664 if (*fmt) {
665 if (str <= end)
666 *str = *fmt;
667 ++str;
668 } else
669 --fmt;
670 continue;
671 }
672
673 if (qualifier == 'I')
674 num = va_arg(args, unsigned long long);
675 else if (qualifier == 'l') {
676 if (flags & SIGN)
677 num = va_arg(args, long);
678 else
679 num = va_arg(args, unsigned long);
680 }
681 else if (qualifier == 'h') {
682 if (flags & SIGN)
683 num = va_arg(args, int);
684 else
685 num = va_arg(args, unsigned int);
686 }
687 else {
688 if (flags & SIGN)
689 num = va_arg(args, int);
690 else
691 num = va_arg(args, unsigned int);
692 }
693 str = number(str, end, num, base, field_width, precision, flags);
694 }
695 if (str <= end)
696 *str = '\0';
697 else if (cnt > 0)
698 /* don't write out a null byte if the buf size is zero */
699 *end = '\0';
700 return str-buf;
701 }
702
703
704 /*
705 * @implemented
706 */
707 int sprintf(char * buf, const char *fmt, ...)
708 {
709 va_list args;
710 int i;
711
712 va_start(args, fmt);
713 i=_vsnprintf(buf,MAXLONG,fmt,args);
714 va_end(args);
715 return i;
716 }
717
718
719 /*
720 * @implemented
721 */
722 int _snprintf(char * buf, size_t cnt, const char *fmt, ...)
723 {
724 va_list args;
725 int i;
726
727 va_start(args, fmt);
728 i=_vsnprintf(buf,cnt,fmt,args);
729 va_end(args);
730 return i;
731 }
732
733
734 /*
735 * @implemented
736 */
737 int __cdecl vsprintf(char *buf, const char *fmt, va_list args)
738 {
739 return _vsnprintf(buf,MAXLONG,fmt,args);
740 }
741
742 /* EOF */