Some cleanup of crt.a feel free to remove header duplication and use 'precomp.h'...
[reactos.git] / reactos / lib / crt / stdio / vfwprint.c
1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
2
3 #include "precomp.h"
4
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <malloc.h>
8 #include <internal/file.h>
9
10 #include <ntdef.h>
11
12 int _isnanl(double x);
13 int _isinfl(double x);
14 int _isnan(double x);
15 int _isinf(double x);
16
17
18 int
19 __vfwprintf(FILE *fp, const wchar_t *fmt0, va_list argp);
20
21
22 /*
23 * @implemented
24 */
25 int
26 vfwprintf(FILE *f, const wchar_t *fmt, va_list ap)
27 {
28 int len;
29 wchar_t localbuf[BUFSIZ];
30
31 #if 0
32 __fileno_lock(_fileno(f));
33 #endif
34 if (f->_flag & _IONBF) {
35 f->_flag &= ~_IONBF;
36 f->_ptr = f->_base = (char *)localbuf;
37 f->_bufsiz = BUFSIZ;
38 len = __vfwprintf(f,fmt,ap);
39 (void)fflush(f);
40 f->_flag |= _IONBF;
41 f->_base = NULL;
42 f->_bufsiz = 0;
43 f->_cnt = 0;
44 } else
45 len = __vfwprintf(f,fmt,ap);
46 #if 0
47 __fileno_unlock(_fileno(f));
48 #endif
49 return (ferror(f) ? EOF : len);
50 }
51
52
53
54 /*
55 * linux/lib/vsprintf.c
56 *
57 * Copyright (C) 1991, 1992 Linus Torvalds
58 */
59
60 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
61 /*
62 * Wirzenius wrote this portably, Torvalds fucked it up :-)
63 */
64
65 /*
66 * Appropiated for the reactos kernel, March 1998 -- David Welch
67 */
68
69 #include <stdarg.h>
70
71 #include <ctype.h>
72 #include <string.h>
73 #include <stdio.h>
74 #include <math.h>
75 #include <internal/ieee.h>
76
77
78 #define ZEROPAD 1 /* pad with zero */
79 #define SIGN 2 /* unsigned/signed long */
80 #define PLUS 4 /* show plus */
81 #define SPACE 8 /* space if plus */
82 #define LEFT 16 /* left justified */
83 #define SPECIAL 32 /* 0x */
84 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
85 #define ZEROTRUNC 128 /* truncate zero 's */
86
87
88 static int skip_wtoi(const wchar_t **s)
89 {
90 int i=0;
91
92 while (iswdigit(**s))
93 i = i*10 + *((*s)++) - L'0';
94 return i;
95 }
96
97
98 static int do_div(LONGLONG *n,int base)
99 {
100 int __res = ((ULONGLONG) *n) % (unsigned) base;
101 *n = ((ULONGLONG) *n) / (unsigned) base;
102 return __res;
103 }
104
105
106 static int number(FILE * f, LONGLONG num, int base, int size, int precision ,int type)
107 {
108 wchar_t c,sign,tmp[66];
109 const wchar_t *digits=L"0123456789abcdefghijklmnopqrstuvwxyz";
110 int i, done = 0;
111
112 if (type & LARGE)
113 digits = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
114 if (type & LEFT)
115 type &= ~ZEROPAD;
116 if (base < 2 || base > 36)
117 return done;
118 c = (type & ZEROPAD) ? L'0' : L' ';
119 sign = 0;
120 if (type & SIGN) {
121 if (num < 0) {
122 sign = L'-';
123 num = -num;
124 size--;
125 } else if (type & PLUS) {
126 sign = L'+';
127 size--;
128 } else if (type & SPACE) {
129 sign = L' ';
130 size--;
131 }
132 }
133 if (type & SPECIAL) {
134 if (base == 16)
135 size -= 2;
136 else if (base == 8)
137 size--;
138 }
139 i = 0;
140 if (num == 0)
141 tmp[i++]=L'0';
142 else while (num != 0)
143 tmp[i++] = digits[do_div(&num,base)];
144 if (i > precision)
145 precision = i;
146 size -= precision;
147 if (!(type&(ZEROPAD+LEFT)))
148 while(size-->0)
149 {
150 if (putwc(L' ',f) == WEOF)
151 return -1;
152 done++;
153 }
154
155 if (sign)
156 {
157 if (putwc(sign,f) == WEOF)
158 return -1;
159 done++;
160 }
161 if (type & SPECIAL) {
162 if (base==8) {
163 if (putwc(L'0',f) == WEOF)
164 return -1;
165 done++;
166 }
167 else if (base==16) {
168 if (putwc(L'0', f) == WEOF)
169 return -1;
170 done++;
171 if (putwc(digits[33],f) == WEOF)
172 return -1;
173 done++;
174 }
175 }
176 if (!(type & LEFT))
177 while (size-- > 0)
178 {
179 if (putwc(c,f) == WEOF)
180 return -1;
181 done++;
182 }
183 while (i < precision--)
184 {
185 if (putwc(L'0', f) == WEOF)
186 return -1;
187 done++;
188 }
189 while (i-- > 0)
190 {
191 if (putwc(tmp[i],f) == WEOF)
192 return -1;
193 done++;
194 }
195 while (size-- > 0)
196 {
197 if (putwc(L' ', f) == WEOF)
198 return -1;
199 done++;
200 }
201 return done;
202 }
203
204
205 static int numberf(FILE * f, double __n, wchar_t exp_sign, int size, int precision, int type)
206 {
207 double exponent = 0.0;
208 double e;
209 long ie;
210
211 //int x;
212 char *buf, *tmp;
213 int i = 0;
214 int j = 0;
215 //int k = 0;
216
217 double frac, intr;
218 double p;
219 wchar_t sign;
220 wchar_t c;
221 char ro = 0;
222 int result, done = 0;
223
224 union
225 {
226 double* __n;
227 double_t* n;
228 } n;
229
230 n.__n = &__n;
231
232 if ( exp_sign == L'g' || exp_sign == L'G' || exp_sign == L'e' || exp_sign == L'E' ) {
233 ie = ((unsigned int)n.n->exponent - (unsigned int)0x3ff);
234 exponent = ie/3.321928;
235 }
236
237 if ( exp_sign == L'g' || exp_sign == L'G' ) {
238 type |= ZEROTRUNC;
239 if ( exponent < -4 || fabs(exponent) >= precision )
240 exp_sign -= 2; // g -> e and G -> E
241 }
242
243 if ( exp_sign == L'e' || exp_sign == L'E' ) {
244 frac = modf(exponent,&e);
245 if ( frac > 0.5 )
246 e++;
247 else if ( frac < -0.5 )
248 e--;
249
250 result = numberf(f,__n/pow(10.0L,e),L'f',size-4, precision, type);
251 if (result < 0)
252 return -1;
253 done += result;
254 if (putwc( exp_sign,f) == WEOF)
255 return -1;
256 done++;
257 size--;
258 ie = (long)e;
259 type = LEFT | PLUS;
260 if ( ie < 0 )
261 type |= SIGN;
262
263 result = number(f,ie, 10,2, 2,type );
264 if (result < 0)
265 return -1;
266 done += result;
267 return done;
268 }
269
270 if ( exp_sign == 'f' ) {
271 buf = alloca(4096);
272 if (type & LEFT) {
273 type &= ~ZEROPAD;
274 }
275
276 c = (type & ZEROPAD) ? L'0' : L' ';
277 sign = 0;
278 if (type & SIGN) {
279 if (__n < 0) {
280 sign = L'-';
281 __n = fabs(__n);
282 size--;
283 } else if (type & PLUS) {
284 sign = L'+';
285 size--;
286 } else if (type & SPACE) {
287 sign = L' ';
288 size--;
289 }
290 }
291
292 frac = modf(__n,&intr);
293
294 // # flags forces a . and prevents trucation of trailing zero's
295
296 if ( precision > 0 ) {
297 //frac = modfl(__n,&intr);
298 i = precision-1;
299 while ( i >= 0 ) {
300 frac*=10.0L;
301 frac = modf(frac, &p);
302 buf[i] = (int)p + L'0';
303 i--;
304 }
305 i = precision;
306 size -= precision;
307
308 ro = 0;
309 if ( frac > 0.5 ) {
310 ro = 1;
311 }
312
313 if ( precision >= 1 || type & SPECIAL) {
314 buf[i++] = '.';
315 size--;
316 }
317 }
318
319 if ( intr == 0.0 ) {
320 buf[i++] = L'0';
321 size--;
322 }
323 else {
324 while ( intr > 0.0 ) {
325 p = intr;
326 intr/=10.0L;
327 modf(intr, &intr);
328
329 p -= 10.0*intr;
330
331 buf[i++] = (int)p + L'0';
332 size--;
333 }
334 }
335
336 j = 0;
337 while ( j < i && ro == 1 ) {
338 if ( buf[j] >= L'0' && buf[j] <= L'8' ) {
339 buf[j]++;
340 ro = 0;
341 }
342 else if ( buf[j] == L'9' ) {
343 buf[j] = L'0';
344 }
345 j++;
346 }
347 if ( ro == 1 )
348 buf[i++] = L'1';
349
350 buf[i] = 0;
351
352 size -= precision;
353 if (!(type&(ZEROPAD+LEFT)))
354 while(size-->0)
355 {
356 if (putwc(L' ',f) == WEOF)
357 return -1;
358 done++;
359 }
360 if (sign)
361 {
362 if (putwc( sign,f) == WEOF)
363 return -1;
364 done++;
365 }
366
367 if (!(type&(ZEROPAD+LEFT)))
368 while(size-->0)
369 {
370 if (putwc(L' ',f) == WEOF)
371 return -1;
372 done++;
373 }
374 if (type & SPECIAL) {
375 }
376
377 if (!(type & LEFT))
378 while (size-- > 0)
379 {
380 if (putwc(c,f) == WEOF)
381 return -1;
382 done++;
383 }
384
385 tmp = buf;
386 if ( type & ZEROTRUNC && ((type & SPECIAL) != SPECIAL) ) {
387 j = 0;
388 while ( j < i && ( *tmp == L'0' || *tmp == L'.' )) {
389 tmp++;
390 i--;
391 }
392 }
393 // else
394 // while (i < precision--)
395 // putwc(L'0', f);
396 while (i-- > 0)
397 {
398 if (putwc(tmp[i],f) == WEOF)
399 return -1;
400 done++;
401 }
402 while (size-- > 0)
403 {
404 if (putwc(L' ', f) == WEOF)
405 return -1;
406 done++;
407 }
408 }
409 return done;
410 }
411
412
413
414 static int string(FILE *f, const char* s, int len, int field_width, int precision, int flags)
415 {
416 int i, done = 0;
417 if (s == NULL)
418 {
419 s = "<NULL>";
420 len = 6;
421 }
422 else
423 {
424 if (len == -1)
425 {
426 len = 0;
427 while ((unsigned int)len < (unsigned int)precision && s[len])
428 len++;
429 }
430 else
431 {
432 if ((unsigned int)len > (unsigned int)precision)
433 len = precision;
434 }
435 }
436 if (!(flags & LEFT))
437 while (len < field_width--)
438 {
439 if (putwc(L' ', f) == WEOF)
440 return -1;
441 done++;
442 }
443 for (i = 0; i < len; ++i)
444 {
445 if (putwc(*s++, f) == WEOF)
446 return -1;
447 done++;
448 }
449 while (len < field_width--)
450 {
451 if (putwc(L' ', f) == WEOF)
452 return -1;
453 done++;
454 }
455 return done;
456 }
457
458 static int stringw(FILE *f, const wchar_t* sw, int len, int field_width, int precision, int flags)
459 {
460 int i, done = 0;
461 if (sw == NULL)
462 {
463 sw = L"<NULL>";
464 len = 6;
465 }
466 else
467 {
468 if (len == -1)
469 {
470 len = 0;
471 while ((unsigned int)len < (unsigned int)precision && sw[len])
472 len++;
473 }
474 else
475 {
476 if ((unsigned int)len > (unsigned int)precision)
477 len = precision;
478 }
479 }
480 if (!(flags & LEFT))
481 while (len < field_width--)
482 {
483 if (putwc(L' ', f) == WEOF)
484 return -1;
485 done++;
486 }
487 for (i = 0; i < len; ++i)
488 {
489 if (putwc(*sw++, f) == WEOF)
490 return -1;
491 done++;
492 }
493 while (len < field_width--)
494 {
495 if (putwc(L' ', f) == WEOF)
496 return -1;
497 done++;
498 }
499 return done;
500 }
501
502 int __vfwprintf(FILE *f, const wchar_t *fmt, va_list args)
503 {
504 int len = 0;
505 ULONGLONG num;
506 int base;
507 double _double;
508 const char *s;
509 const wchar_t* sw;
510 int result, done = 0;
511
512 int flags; /* flags to number() */
513
514 int field_width; /* width of output field */
515 int precision; /* min. # of digits for integers; max
516 number of chars for from string */
517 int qualifier = 0; /* 'h', 'l', 'L' or 'I64' for integer fields */
518
519 for (; *fmt ; ++fmt) {
520 if (*fmt != L'%') {
521 if (putwc(*fmt,f) == WEOF)
522 return -1;
523 done++;
524 continue;
525 }
526
527 /* process flags */
528 flags = 0;
529 repeat:
530 ++fmt; /* this also skips first '%' */
531 switch (*fmt) {
532 case L'-': flags |= LEFT; goto repeat;
533 case L'+': flags |= PLUS; goto repeat;
534 case L' ': flags |= SPACE; goto repeat;
535 case L'#': flags |= SPECIAL; goto repeat;
536 case L'0': flags |= ZEROPAD; goto repeat;
537 }
538
539 /* get field width */
540 field_width = -1;
541 if (isxdigit(*fmt))
542 field_width = skip_wtoi(&fmt);
543 else if (*fmt == L'*') {
544 ++fmt;
545 /* it's the next argument */
546 field_width = va_arg(args, int);
547 if (field_width < 0) {
548 field_width = -field_width;
549 flags |= LEFT;
550 }
551 }
552
553 /* get the precision */
554 precision = -1;
555 if (*fmt == L'.') {
556 ++fmt;
557 if (iswdigit(*fmt))
558 precision = skip_wtoi(&fmt);
559 else if (*fmt == L'*') {
560 ++fmt;
561 /* it's the next argument */
562 precision = va_arg(args, int);
563 }
564 if (precision < 0)
565 precision = 0;
566 }
567
568 /* get the conversion qualifier */
569 qualifier=0;
570 // %Z can be just stand alone or as size_t qualifier
571 if ( *fmt == 'Z' ) {
572 qualifier = *fmt;
573 switch ( *(fmt+1)) {
574 case L'o':
575 case L'b':
576 case L'X':
577 case L'x':
578 case L'd':
579 case L'i':
580 case L'u':
581 ++fmt;
582 break;
583 default:
584 break;
585 }
586 } else if (*fmt == L'h' || *fmt == L'l' || *fmt == L'L' || *fmt == L'w') {
587 qualifier = *fmt;
588 ++fmt;
589 } else if (*fmt == L'I' && *(fmt+1) == L'6' && *(fmt+2) == L'4') {
590 qualifier = *fmt;
591 fmt += 3;
592 }
593
594 // go fine with ll instead of L
595 if ( *fmt == L'l' ) {
596 ++fmt;
597 qualifier = L'L';
598 }
599
600 /* default base */
601 base = 10;
602
603 switch (*fmt) {
604 case L'c': /* finished */
605 if (!(flags & LEFT))
606 while (--field_width > 0)
607 {
608 if (putwc(L' ', f) == WEOF)
609 return -1;
610 done++;
611 }
612 if (qualifier == L'h')
613 {
614 if (putwc((wchar_t) va_arg(args, int), f) == WEOF)
615 return -1;
616 }
617 else
618 {
619 if (putwc((wchar_t) va_arg(args, int), f) == WEOF)
620 return -1;
621 }
622 done++;
623 while (--field_width > 0)
624 {
625 if (putwc(L' ', f) == WEOF)
626 return -1;
627 done++;
628 }
629 continue;
630
631 case L'C': /* finished */
632 if (!(flags & LEFT))
633 while (--field_width > 0)
634 {
635 if (putwc(L' ', f) == WEOF)
636 return -1;
637 done++;
638 }
639 if (qualifier == L'l' || qualifier == L'w')
640 {
641 if (putwc((unsigned char) va_arg(args, int), f) == WEOF)
642 return -1;
643 }
644 else
645 {
646 if (putwc((unsigned char) va_arg(args, int), f) == WEOF)
647 return -1;
648 }
649 done++;
650 while (--field_width > 0)
651 {
652 if (putwc(L' ', f) == WEOF)
653 return -1;
654 done++;
655 }
656 continue;
657
658 case L's': /* finished */
659 if (qualifier == L'h') {
660 /* print ascii string */
661 s = va_arg(args, char *);
662 result = string(f, s, -1, field_width, precision, flags);
663 } else {
664 /* print unicode string */
665 sw = va_arg(args, wchar_t *);
666 result = stringw(f, sw, -1, field_width, precision, flags);
667 }
668 if (result < 0)
669 return -1;
670 done += result;
671 continue;
672
673 case L'S':
674 if (qualifier == L'l' || qualifier == L'w') {
675 /* print unicode string */
676 sw = va_arg(args, wchar_t *);
677 result = stringw(f, sw, -1, field_width, precision, flags);
678 } else {
679 /* print ascii string */
680 s = va_arg(args, char *);
681 result = string(f, s, -1, field_width, precision, flags);
682 }
683 if (result < 0)
684 return -1;
685 done += result;
686 continue;
687
688 case L'Z': /* finished */
689 if (qualifier == L'w') {
690 /* print counted unicode string */
691 PUNICODE_STRING pus = va_arg(args, PUNICODE_STRING);
692 if ((pus == NULL) || (pus->Buffer)) {
693 sw = NULL;
694 len = -1;
695 } else {
696 sw = pus->Buffer;
697 }
698 result = stringw(f, sw, len, field_width, precision, flags);
699 } else {
700 /* print counted ascii string */
701 PANSI_STRING pus = va_arg(args, PANSI_STRING);
702 if ((pus == NULL) || (pus->Buffer)) {
703 s = NULL;
704 len = -1;
705 } else {
706 s = pus->Buffer;
707 len = pus->Length;
708 }
709 result = string(f, s, len, field_width, precision, flags);
710 }
711 if (result < 0)
712 return -1;
713 done += result;
714 continue;
715
716 case L'e': /* finished */
717 case L'E':
718 case L'f':
719 case L'g':
720 case L'G':
721 _double = (double)va_arg(args, double);
722
723 if ( _isnan(_double) ) {
724 sw = L"Nan";
725 len = 3;
726 while ( len > 0 ) {
727 if (putwc(*sw++,f) == WEOF)
728 return -1;
729 done++;
730 len --;
731 }
732 } else if ( _isinf(_double) < 0 ) {
733 sw = L"-Inf";
734 len = 4;
735 while ( len > 0 ) {
736 if (putwc(*sw++,f) == WEOF)
737 return -1;
738 done++;
739 len --;
740 }
741 } else if ( _isinf(_double) > 0 ) {
742 sw = L"+Inf";
743 len = 4;
744 while ( len > 0 ) {
745 if (putwc(*sw++,f) == WEOF)
746 return -1;
747 done++;
748 len --;
749 }
750 } else {
751 if ( precision == -1 )
752 precision = 6;
753 result = numberf(f,_double,*fmt,field_width,precision,flags);
754 if (result < 0)
755 return -1;
756 done += result;
757 }
758 continue;
759
760 case L'p':
761 if (field_width == -1) {
762 field_width = 2*sizeof(void *);
763 flags |= ZEROPAD;
764 }
765 result = number(f,
766 (unsigned long) va_arg(args, void *), 16,
767 field_width, precision, flags);
768 if (result < 0)
769 return -1;
770 done += result;
771 continue;
772
773 case L'n':
774 if (qualifier == L'l') {
775 long * ip = va_arg(args, long *);
776 *ip = 0;
777 } else {
778 int * ip = va_arg(args, int *);
779 *ip = 0;
780 }
781 continue;
782
783 /* integer number formats - set up the flags and "break" */
784 case L'o':
785 base = 8;
786 break;
787
788 case L'b':
789 base = 2;
790 break;
791
792 case L'X':
793 flags |= LARGE;
794 case L'x':
795 base = 16;
796 break;
797
798 case L'd':
799 case L'i':
800 flags |= SIGN;
801 case L'u':
802 break;
803
804 default:
805 if (*fmt != L'%')
806 {
807 if (putwc(L'%', f) == WEOF)
808 return -1;
809 done++;
810 }
811 if (*fmt)
812 {
813 if (putwc(*fmt, f) == WEOF)
814 return -1;
815 done++;
816 }
817 else
818 --fmt;
819 continue;
820 }
821
822 if (qualifier == L'I')
823 num = va_arg(args, ULONGLONG);
824 else if (qualifier == L'l') {
825 if (flags & SIGN)
826 num = va_arg(args, long);
827 else
828 num = va_arg(args, unsigned long);
829 }
830 else if (qualifier == L'h') {
831 if (flags & SIGN)
832 num = va_arg(args, int);
833 else
834 num = va_arg(args, unsigned int);
835 }
836 else if (flags & SIGN)
837 num = va_arg(args, int);
838 else
839 num = va_arg(args, unsigned int);
840 result = number(f, num, base, field_width, precision, flags);
841 if (result < 0)
842 return -1;
843 done += result;
844 }
845 //putwc(L'\0',f);
846 return done;
847 }
848
849 /* EOF */