[ACPI]
[reactos.git] / reactos / tools / ssprintf.cpp
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write to the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 */
16
17 // For character conversion functions like "wctomb" and "alloca" under Unix
18 #include <stdlib.h>
19
20 #if defined(WIN32)
21 // Under Win32 hosts, "alloca" is not defined by stdlib.h, but by malloc.h
22 // On the other hand, malloc.h is deprecated under some Unix hosts, so only include it for Win32 hosts.
23 #include <malloc.h>
24 #endif
25
26 #include <ctype.h>
27 #include <math.h>
28 #include <float.h>
29 #include <assert.h>
30 #include "ssprintf.h"
31
32 #ifndef WIN32
33 #define _finite __finite
34 #define _isnan __isnan
35 #endif
36
37 #ifndef __APPLE__
38 inline int iswdigit ( wchar_t c )
39 {
40 return ( c >= L'0' && c <= L'9' );
41 }
42 #endif
43
44 #if defined(__sun__)
45 #include <alloca.h>
46 #include <ieee.h>
47 #endif
48
49 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__sun__) || defined(__CYGWIN__)
50 # define __isnan isnan
51 # define __finite finite
52 # define powl __builtin_powl
53 # define modfl __builtin_modfl
54 #endif // _FreeBSD__
55
56 #ifdef _MSC_VER
57 #define alloca _alloca
58 #endif//_MSC_VER
59
60 #ifdef _MSC_VER
61 typedef __int64 LONGLONG;
62 typedef unsigned __int64 ULONGLONG;
63 #else
64 typedef long long LONGLONG;
65 typedef unsigned long long ULONGLONG;
66 #endif
67
68 typedef struct {
69 unsigned int mantissa:23;
70 unsigned int exponent:8;
71 unsigned int sign:1;
72 } ieee_float_t;
73
74 typedef struct {
75 unsigned int mantissal:32;
76 unsigned int mantissah:20;
77 unsigned int exponent:11;
78 unsigned int sign:1;
79 } ieee_double_t;
80
81 typedef struct {
82 unsigned int mantissal:32;
83 unsigned int mantissah:32;
84 unsigned int exponent:15;
85 unsigned int sign:1;
86 unsigned int empty:16;
87 } ieee_long_double_t;
88
89 std::string
90 ssprintf ( const char* fmt, ... )
91 {
92 va_list arg;
93 va_start(arg, fmt);
94 std::string f = ssvprintf ( fmt, arg );
95 va_end(arg);
96 return f;
97 }
98
99 std::wstring
100 sswprintf ( const wchar_t* fmt, ... )
101 {
102 va_list arg;
103 va_start(arg, fmt);
104 std::wstring f = sswvprintf ( fmt, arg );
105 va_end(arg);
106 return f;
107 }
108
109 #define ZEROPAD 1 /* pad with zero */
110 #define SIGN 2 /* unsigned/signed long */
111 #define PLUS 4 /* show plus */
112 #define SPACE 8 /* space if plus */
113 #define LEFT 16 /* left justified */
114 #define SPECIAL 32 /* 0x */
115 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
116 #define ZEROTRUNC 128 /* truncate zero 's */
117
118
119 static int
120 skip_atoi(const char **s)
121 {
122 int i=0;
123
124 while (isdigit(**s))
125 i = i*10 + *((*s)++) - '0';
126 return i;
127 }
128
129 static int
130 skip_wtoi(const wchar_t **s)
131 {
132 int i=0;
133
134 while (iswdigit(**s))
135 i = i*10 + *((*s)++) - L'0';
136 return i;
137 }
138
139
140 static int
141 do_div(LONGLONG *n,int base)
142 {
143 int __res = ((ULONGLONG) *n) % (unsigned) base;
144 *n = ((ULONGLONG) *n) / (unsigned) base;
145 return __res;
146 }
147
148
149 static bool
150 number(std::string& f, LONGLONG num, int base, int size, int precision ,int type)
151 {
152 char c,sign,tmp[66];
153 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
154 int i;
155
156 if (type & LARGE)
157 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
158 if (type & LEFT)
159 type &= ~ZEROPAD;
160 if (base < 2 || base > 36)
161 return false;
162 c = (type & ZEROPAD) ? '0' : ' ';
163 sign = 0;
164 if (type & SIGN) {
165 if (num < 0) {
166 sign = '-';
167 num = -num;
168 size--;
169 } else if (type & PLUS) {
170 sign = '+';
171 size--;
172 } else if (type & SPACE) {
173 sign = ' ';
174 size--;
175 }
176 }
177 if (type & SPECIAL) {
178 if (base == 16)
179 size -= 2;
180 else if (base == 8)
181 size--;
182 }
183 i = 0;
184 if (num == 0)
185 tmp[i++]='0';
186 else while (num != 0)
187 tmp[i++] = digits[do_div(&num,base)];
188 if (i > precision)
189 precision = i;
190 size -= precision;
191 if (!(type&(ZEROPAD+LEFT)))
192 while(size-->0)
193 f += ' ';
194 if (sign)
195 f += sign;
196 if (type & SPECIAL)
197 {
198 if (base==8)
199 f += '0';
200 else if (base==16)
201 {
202 f += '0';
203 f += digits[33];
204 }
205 }
206 if (!(type & LEFT))
207 {
208 while (size-- > 0)
209 f += c;
210 }
211 while (i < precision--)
212 {
213 f += '0';
214 }
215 while (i-- > 0)
216 {
217 f += tmp[i];
218 }
219 while (size-- > 0)
220 {
221 f += ' ';
222 }
223 return true;
224 }
225
226 static bool
227 wnumber(std::wstring& f, LONGLONG num, int base, int size, int precision ,int type)
228 {
229 wchar_t c,sign,tmp[66];
230 const wchar_t *digits = L"0123456789abcdefghijklmnopqrstuvwxyz";
231 int i;
232
233 if (type & LARGE)
234 digits = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
235 if (type & LEFT)
236 type &= ~ZEROPAD;
237 if (base < 2 || base > 36)
238 return 0;
239 c = (type & ZEROPAD) ? L'0' : L' ';
240 sign = false;
241 if (type & SIGN) {
242 if (num < 0) {
243 sign = L'-';
244 num = -num;
245 size--;
246 } else if (type & PLUS) {
247 sign = L'+';
248 size--;
249 } else if (type & SPACE) {
250 sign = L' ';
251 size--;
252 }
253 }
254 if (type & SPECIAL) {
255 if (base == 16)
256 size -= 2;
257 else if (base == 8)
258 size--;
259 }
260 i = 0;
261 if (num == 0)
262 tmp[i++]=L'0';
263 else while (num != 0)
264 tmp[i++] = digits[do_div(&num,base)];
265 if (i > precision)
266 precision = i;
267 size -= precision;
268 if (!(type&(ZEROPAD+LEFT)))
269 while(size-->0)
270 f += L' ';
271 if (sign)
272 f += sign;
273 if (type & SPECIAL)
274 {
275 if (base==8)
276 f += L'0';
277 else if (base==16)
278 {
279 f += L'0';
280 f += digits[33];
281 }
282 }
283 if (!(type & LEFT))
284 {
285 while (size-- > 0)
286 f += c;
287 }
288 while (i < precision--)
289 {
290 f += L'0';
291 }
292 while (i-- > 0)
293 {
294 f += tmp[i];
295 }
296 while (size-- > 0)
297 {
298 f += L' ';
299 }
300 return true;
301 }
302
303
304 static bool
305 numberf(std::string& f, double __n, char exp_sign, int size, int precision, int type)
306 {
307 double exponent = 0.0;
308 double e;
309 long ie;
310
311 int i = 0;
312 int j = 0;
313 int ro = 0;
314
315 double frac, intr;
316 double p;
317 char *buf, *tmp, sign, c;
318 int result;
319
320 union
321 {
322 double* __n;
323 ieee_double_t* n;
324 } n;
325
326 n.__n = &__n;
327
328 if ( exp_sign == 'g' || exp_sign == 'G' || exp_sign == 'e' || exp_sign == 'E' )
329 {
330 ie = ((unsigned int)n.n->exponent - (unsigned int)0x3ff);
331 exponent = ie/3.321928;
332 }
333
334 if ( exp_sign == 'g' || exp_sign == 'G' )
335 {
336 type |= ZEROTRUNC;
337 if ( exponent < -4 || fabs(exponent) >= precision )
338 exp_sign -= 2; // g -> e and G -> E
339 else
340 exp_sign = 'f';
341 }
342
343 if ( exp_sign == 'e' || exp_sign == 'E' )
344 {
345 frac = modf(exponent,&e);
346 if ( frac > 0.5 )
347 e++;
348 else if ( frac < -0.5 )
349 e--;
350
351 result = numberf(f,__n/pow(10.0L,(long double)e),'f',size-4, precision, type);
352 if (result < 0)
353 return false;
354 f += exp_sign;
355 size--;
356 ie = (long)e;
357 type = LEFT | PLUS;
358 if ( ie < 0 )
359 type |= SIGN;
360
361 result = number(f,ie, 10,2, 2,type );
362 if (result < 0)
363 return false;
364 return true;
365 }
366
367 if ( exp_sign == 'f' )
368 {
369 buf = (char*)alloca(4096);
370 if (type & LEFT)
371 type &= ~ZEROPAD;
372
373 c = (type & ZEROPAD) ? '0' : ' ';
374 sign = 0;
375 if (type & SIGN)
376 {
377 if (__n < 0)
378 {
379 sign = '-';
380 __n = fabs(__n);
381 size--;
382 }
383 else if (type & PLUS)
384 {
385 sign = '+';
386 size--;
387 }
388 else if (type & SPACE)
389 {
390 sign = ' ';
391 size--;
392 }
393 }
394
395 frac = modf(__n,&intr);
396
397 // # flags forces a . and prevents trucation of trailing zero's
398 if ( precision > 0 )
399 {
400 i = precision-1;
401 while ( i >= 0 )
402 {
403 frac*=10.0L;
404 frac = modf(frac, &p);
405 buf[i] = (int)p + '0';
406 i--;
407 }
408 i = precision;
409 size -= precision;
410
411 ro = 0;
412 if ( frac > 0.5 )
413 ro = 1;
414
415 if ( precision >= 1 || type & SPECIAL)
416 {
417 buf[i++] = '.';
418 size--;
419 }
420 }
421
422 if ( intr == 0.0 )
423 {
424 buf[i++] = '0';
425 size--;
426 }
427 else
428 {
429 while ( intr > 0.0 )
430 {
431 p = intr;
432 intr/=10.0L;
433 modf(intr, &intr);
434
435 p -= 10.0*intr;
436
437 buf[i++] = (int)p + '0';
438 size--;
439 }
440 }
441
442 j = 0;
443 while ( j < i && ro == 1)
444 {
445 if ( buf[j] >= '0' && buf[j] <= '8' )
446 {
447 buf[j]++;
448 ro = 0;
449 }
450 else if ( buf[j] == '9' )
451 {
452 buf[j] = '0';
453 }
454 j++;
455 }
456 if ( ro == 1 )
457 buf[i++] = '1';
458
459 buf[i] = 0;
460
461 if (!(type&(ZEROPAD+LEFT)))
462 {
463 while(size-->0)
464 f += ' ';
465 }
466 if (sign)
467 {
468 f += sign;
469 }
470
471 if (!(type&(ZEROPAD+LEFT)))
472 {
473 while(size-->0)
474 f += ' ';
475 }
476
477 if (!(type & LEFT))
478 {
479 while (size-- > 0)
480 f += c;
481 }
482
483 tmp = buf;
484 if ( type & ZEROTRUNC && ((type & SPECIAL) != SPECIAL) )
485 {
486 j = 0;
487 while ( j < i && ( *tmp == '0' || *tmp == '.' ))
488 {
489 tmp++;
490 i--;
491 }
492 }
493 while (i-- > 0)
494 {
495 f += tmp[i];
496 }
497 while (size-- > 0)
498 {
499 f += ' ';
500 }
501 }
502 return true;
503 }
504
505 static bool
506 wnumberf(std::wstring& f, double __n, wchar_t exp_sign, int size, int precision, int type)
507 {
508 double exponent = 0.0;
509 double e;
510 long ie;
511
512 int i = 0;
513 int j = 0;
514 int ro = 0;
515
516 double frac, intr;
517 double p;
518 wchar_t *buf, *tmp, sign, c;
519 int result;
520
521 union
522 {
523 double* __n;
524 ieee_double_t* n;
525 } n;
526
527 n.__n = &__n;
528
529 if ( exp_sign == L'g' || exp_sign == L'G' || exp_sign == L'e' || exp_sign == L'E' )
530 {
531 ie = ((unsigned int)n.n->exponent - (unsigned int)0x3ff);
532 exponent = ie/3.321928;
533 }
534
535 if ( exp_sign == L'g' || exp_sign == L'G' )
536 {
537 type |= ZEROTRUNC;
538 if ( exponent < -4 || fabs(exponent) >= precision )
539 exp_sign -= 2; // g -> e and G -> E
540 else
541 exp_sign = L'f';
542 }
543
544 if ( exp_sign == L'e' || exp_sign == L'E' )
545 {
546 frac = modf(exponent,&e);
547 if ( frac > 0.5 )
548 e++;
549 else if ( frac < -0.5 )
550 e--;
551
552 result = wnumberf(f,__n/pow(10.0L,(long double) e),L'f',size-4, precision, type);
553 if (result < 0)
554 return false;
555 f += exp_sign;
556 size--;
557 ie = (long)e;
558 type = LEFT | PLUS;
559 if ( ie < 0 )
560 type |= SIGN;
561
562 result = wnumber(f,ie, 10,2, 2,type );
563 if (result < 0)
564 return false;
565 return true;
566 }
567
568 if ( exp_sign == L'f' )
569 {
570 buf = (wchar_t*)alloca(4096*sizeof(wchar_t));
571 if (type & LEFT)
572 type &= ~ZEROPAD;
573
574 c = (type & ZEROPAD) ? L'0' : L' ';
575 sign = 0;
576 if (type & SIGN)
577 {
578 if (__n < 0)
579 {
580 sign = L'-';
581 __n = fabs(__n);
582 size--;
583 }
584 else if (type & PLUS)
585 {
586 sign = L'+';
587 size--;
588 }
589 else if (type & SPACE)
590 {
591 sign = L' ';
592 size--;
593 }
594 }
595
596 frac = modf(__n,&intr);
597
598 // # flags forces a . and prevents trucation of trailing zero's
599 if ( precision > 0 )
600 {
601 i = precision-1;
602 while ( i >= 0 )
603 {
604 frac*=10.0L;
605 frac = modf(frac, &p);
606 buf[i] = (int)p + L'0';
607 i--;
608 }
609 i = precision;
610 size -= precision;
611
612 ro = 0;
613 if ( frac > 0.5 )
614 ro = 1;
615
616 if ( precision >= 1 || type & SPECIAL)
617 {
618 buf[i++] = L'.';
619 size--;
620 }
621 }
622
623 if ( intr == 0.0 )
624 {
625 buf[i++] = L'0';
626 size--;
627 }
628 else
629 {
630 while ( intr > 0.0 )
631 {
632 p = intr;
633 intr/=10.0L;
634 modf(intr, &intr);
635
636 p -= 10.0*intr;
637
638 buf[i++] = (int)p + L'0';
639 size--;
640 }
641 }
642
643 j = 0;
644 while ( j < i && ro == 1)
645 {
646 if ( buf[j] >= L'0' && buf[j] <= L'8' )
647 {
648 buf[j]++;
649 ro = 0;
650 }
651 else if ( buf[j] == L'9' )
652 {
653 buf[j] = L'0';
654 }
655 j++;
656 }
657 if ( ro == 1 )
658 buf[i++] = L'1';
659
660 buf[i] = 0;
661
662 if (!(type&(ZEROPAD+LEFT)))
663 {
664 while(size-->0)
665 f += L' ';
666 }
667 if (sign)
668 {
669 f += sign;
670 }
671
672 if (!(type&(ZEROPAD+LEFT)))
673 {
674 while(size-->0)
675 f += L' ';
676 }
677
678 if (!(type & LEFT))
679 {
680 while (size-- > 0)
681 f += c;
682 }
683
684 tmp = buf;
685 if ( type & ZEROTRUNC && ((type & SPECIAL) != SPECIAL) )
686 {
687 j = 0;
688 while ( j < i && ( *tmp == L'0' || *tmp == L'.' ))
689 {
690 tmp++;
691 i--;
692 }
693 }
694 while (i-- > 0)
695 {
696 f += tmp[i];
697 }
698 while (size-- > 0)
699 {
700 f += L' ';
701 }
702 }
703 return true;
704 }
705
706 static bool
707 numberfl(std::string& f, long double __n, char exp_sign, int size, int precision, int type)
708 {
709 long double exponent = 0.0;
710 long double e;
711 long ie;
712
713 int i = 0;
714 int j = 0;
715 int ro = 0;
716
717 long double frac, intr;
718 long double p;
719 char *buf, *tmp, sign, c;
720 int result;
721
722 union
723 {
724 long double* __n;
725 ieee_long_double_t* n;
726 } n;
727
728 n.__n = &__n;
729
730 if ( exp_sign == 'g' || exp_sign == 'G' || exp_sign == 'e' || exp_sign == 'E' )
731 {
732 ie = ((unsigned int)n.n->exponent - (unsigned int)0x3fff);
733 exponent = ie/3.321928;
734 }
735
736 if ( exp_sign == 'g' || exp_sign == 'G' )
737 {
738 type |= ZEROTRUNC;
739 if ( exponent < -4 || fabs(exponent) >= precision )
740 exp_sign -= 2; // g -> e and G -> E
741 else
742 exp_sign = 'f';
743 }
744
745 if ( exp_sign == 'e' || exp_sign == 'E' )
746 {
747 frac = modfl(exponent,&e);
748 if ( frac > 0.5 )
749 e++;
750 else if ( frac < -0.5 )
751 e--;
752
753 result = numberf(f,__n/powl(10.0L,e),'f',size-4, precision, type);
754 if (result < 0)
755 return false;
756 f += exp_sign;
757 size--;
758 ie = (long)e;
759 type = LEFT | PLUS;
760 if ( ie < 0 )
761 type |= SIGN;
762
763 result = number(f,ie, 10,2, 2,type );
764 if (result < 0)
765 return false;
766 return true;
767 }
768
769 if ( exp_sign == 'f' )
770 {
771 buf = (char*)alloca(4096);
772 if (type & LEFT)
773 type &= ~ZEROPAD;
774
775 c = (type & ZEROPAD) ? '0' : ' ';
776 sign = 0;
777 if (type & SIGN)
778 {
779 if (__n < 0)
780 {
781 sign = '-';
782 __n = fabs(__n);
783 size--;
784 }
785 else if (type & PLUS)
786 {
787 sign = '+';
788 size--;
789 }
790 else if (type & SPACE)
791 {
792 sign = ' ';
793 size--;
794 }
795 }
796
797 frac = modfl(__n,&intr);
798
799 // # flags forces a . and prevents trucation of trailing zero's
800 if ( precision > 0 )
801 {
802 i = precision-1;
803 while ( i >= 0 )
804 {
805 frac*=10.0L;
806 frac = modfl((long double)frac, &p);
807 buf[i] = (int)p + '0';
808 i--;
809 }
810 i = precision;
811 size -= precision;
812
813 ro = 0;
814 if ( frac > 0.5 )
815 ro = 1;
816
817 if ( precision >= 1 || type & SPECIAL)
818 {
819 buf[i++] = '.';
820 size--;
821 }
822 }
823
824 if ( intr == 0.0 )
825 {
826 buf[i++] = '0';
827 size--;
828 }
829 else
830 {
831 while ( intr > 0.0 )
832 {
833 p = intr;
834 intr/=10.0L;
835 modfl(intr, &intr);
836
837 p -= 10.0L*intr;
838
839 buf[i++] = (int)p + '0';
840 size--;
841 }
842 }
843
844 j = 0;
845 while ( j < i && ro == 1)
846 {
847 if ( buf[j] >= '0' && buf[j] <= '8' )
848 {
849 buf[j]++;
850 ro = 0;
851 }
852 else if ( buf[j] == '9' )
853 {
854 buf[j] = '0';
855 }
856 j++;
857 }
858 if ( ro == 1 )
859 buf[i++] = '1';
860
861 buf[i] = 0;
862
863 if (!(type&(ZEROPAD+LEFT)))
864 {
865 while(size-->0)
866 f += ' ';
867 }
868 if (sign)
869 {
870 f += sign;
871 }
872
873 if (!(type&(ZEROPAD+LEFT)))
874 {
875 while(size-->0)
876 f += ' ';
877 }
878
879 if (!(type & LEFT))
880 {
881 while (size-- > 0)
882 f += c;
883 }
884
885 tmp = buf;
886 if ( type & ZEROTRUNC && ((type & SPECIAL) != SPECIAL) )
887 {
888 j = 0;
889 while ( j < i && ( *tmp == '0' || *tmp == '.' ))
890 {
891 tmp++;
892 i--;
893 }
894 }
895 while (i-- > 0)
896 {
897 f += tmp[i];
898 }
899 while (size-- > 0)
900 {
901 f += ' ';
902 }
903 }
904 return true;
905 }
906
907 static bool
908 wnumberfl(std::wstring& f, long double __n, wchar_t exp_sign, int size, int precision, int type)
909 {
910 long double exponent = 0.0;
911 long double e;
912 long ie;
913
914 int i = 0;
915 int j = 0;
916 int ro = 0;
917
918 long double frac, intr;
919 long double p;
920 wchar_t *buf, *tmp, sign, c;
921 int result;
922
923 union
924 {
925 long double* __n;
926 ieee_long_double_t* n;
927 } n;
928
929 n.__n = &__n;
930
931 if ( exp_sign == L'g' || exp_sign == L'G' || exp_sign == L'e' || exp_sign == L'E' )
932 {
933 ie = ((unsigned int)n.n->exponent - (unsigned int)0x3fff);
934 exponent = ie/3.321928;
935 }
936
937 if ( exp_sign == L'g' || exp_sign == L'G' )
938 {
939 type |= ZEROTRUNC;
940 if ( exponent < -4 || fabs(exponent) >= precision )
941 exp_sign -= 2; // g -> e and G -> E
942 else
943 exp_sign = 'f';
944 }
945
946 if ( exp_sign == L'e' || exp_sign == L'E' )
947 {
948 frac = modfl(exponent,&e);
949 if ( frac > 0.5 )
950 e++;
951 else if ( frac < -0.5 )
952 e--;
953
954 result = wnumberf(f,__n/powl(10.0L,e),L'f',size-4, precision, type);
955 if (result < 0)
956 return false;
957 f += exp_sign;
958 size--;
959 ie = (long)e;
960 type = LEFT | PLUS;
961 if ( ie < 0 )
962 type |= SIGN;
963
964 result = wnumber(f,ie, 10,2, 2,type );
965 if (result < 0)
966 return false;
967 return true;
968 }
969
970 if ( exp_sign == L'f' )
971 {
972 buf = (wchar_t*)alloca(4096*sizeof(wchar_t));
973 if (type & LEFT)
974 type &= ~ZEROPAD;
975
976 c = (type & ZEROPAD) ? L'0' : L' ';
977 sign = 0;
978 if (type & SIGN)
979 {
980 if (__n < 0)
981 {
982 sign = L'-';
983 __n = fabs(__n);
984 size--;
985 }
986 else if (type & PLUS)
987 {
988 sign = L'+';
989 size--;
990 }
991 else if (type & SPACE)
992 {
993 sign = L' ';
994 size--;
995 }
996 }
997
998 frac = modfl(__n,&intr);
999
1000 // # flags forces a . and prevents trucation of trailing zero's
1001 if ( precision > 0 )
1002 {
1003 i = precision-1;
1004 while ( i >= 0 )
1005 {
1006 frac*=10.0L;
1007 frac = modfl((long double)frac, &p);
1008 buf[i] = (int)p + L'0';
1009 i--;
1010 }
1011 i = precision;
1012 size -= precision;
1013
1014 ro = 0;
1015 if ( frac > 0.5 )
1016 ro = 1;
1017
1018 if ( precision >= 1 || type & SPECIAL)
1019 {
1020 buf[i++] = L'.';
1021 size--;
1022 }
1023 }
1024
1025 if ( intr == 0.0 )
1026 {
1027 buf[i++] = L'0';
1028 size--;
1029 }
1030 else
1031 {
1032 while ( intr > 0.0 )
1033 {
1034 p = intr;
1035 intr/=10.0L;
1036 modfl(intr, &intr);
1037
1038 p -= 10.0L*intr;
1039
1040 buf[i++] = (int)p + L'0';
1041 size--;
1042 }
1043 }
1044
1045 j = 0;
1046 while ( j < i && ro == 1)
1047 {
1048 if ( buf[j] >= L'0' && buf[j] <= L'8' )
1049 {
1050 buf[j]++;
1051 ro = 0;
1052 }
1053 else if ( buf[j] == L'9' )
1054 {
1055 buf[j] = L'0';
1056 }
1057 j++;
1058 }
1059 if ( ro == 1 )
1060 buf[i++] = L'1';
1061
1062 buf[i] = 0;
1063
1064 if (!(type&(ZEROPAD+LEFT)))
1065 {
1066 while(size-->0)
1067 f += L' ';
1068 }
1069 if (sign)
1070 {
1071 f += sign;
1072 }
1073
1074 if (!(type&(ZEROPAD+LEFT)))
1075 {
1076 while(size-->0)
1077 f += L' ';
1078 }
1079
1080 if (!(type & LEFT))
1081 {
1082 while (size-- > 0)
1083 f += c;
1084 }
1085
1086 tmp = buf;
1087 if ( type & ZEROTRUNC && ((type & SPECIAL) != SPECIAL) )
1088 {
1089 j = 0;
1090 while ( j < i && ( *tmp == L'0' || *tmp == L'.' ))
1091 {
1092 tmp++;
1093 i--;
1094 }
1095 }
1096 while (i-- > 0)
1097 {
1098 f += tmp[i];
1099 }
1100 while (size-- > 0)
1101 {
1102 f += L' ';
1103 }
1104 }
1105 return true;
1106 }
1107
1108 static int
1109 do_string(std::string& f, const char* s, int len, int field_width, int precision, int flags)
1110 {
1111 int i, done = 0;
1112 if (s == NULL)
1113 {
1114 s = "<NULL>";
1115 len = 6;
1116 }
1117 else
1118 {
1119 if (len == -1)
1120 {
1121 len = 0;
1122 while ((unsigned int)len < (unsigned int)precision && s[len])
1123 len++;
1124 }
1125 else
1126 {
1127 if ((unsigned int)len > (unsigned int)precision)
1128 len = precision;
1129 }
1130 }
1131 if (!(flags & LEFT))
1132 while (len < field_width--)
1133 {
1134 f += ' ';
1135 done++;
1136 }
1137 for (i = 0; i < len; ++i)
1138 {
1139 f += *s++;
1140 done++;
1141 }
1142 while (len < field_width--)
1143 {
1144 f += ' ';
1145 done++;
1146 }
1147 return done;
1148 }
1149
1150 static int
1151 do_wstring(std::wstring& f, const wchar_t* s, int len, int field_width, int precision, int flags)
1152 {
1153 int i, done = 0;
1154 if (s == NULL)
1155 {
1156 s = L"<NULL>";
1157 len = 6;
1158 }
1159 else
1160 {
1161 if (len == -1)
1162 {
1163 len = 0;
1164 while ((unsigned int)len < (unsigned int)precision && s[len])
1165 len++;
1166 }
1167 else
1168 {
1169 if ((unsigned int)len > (unsigned int)precision)
1170 len = precision;
1171 }
1172 }
1173 if (!(flags & LEFT))
1174 while (len < field_width--)
1175 {
1176 f += L' ';
1177 done++;
1178 }
1179 for (i = 0; i < len; ++i)
1180 {
1181 f += *s++;
1182 done++;
1183 }
1184 while (len < field_width--)
1185 {
1186 f += L' ';
1187 done++;
1188 }
1189 return done;
1190 }
1191
1192 static int
1193 stringw(std::string& f, const wchar_t* sw, int len, int field_width, int precision, int flags)
1194 {
1195 int i, done = 0;
1196 if (sw == NULL)
1197 {
1198 sw = L"<NULL>";
1199 len = 6;
1200 }
1201 else
1202 {
1203 if (len == -1)
1204 {
1205 len = 0;
1206 while ((unsigned int)len < (unsigned int)precision && sw[len])
1207 len++;
1208 }
1209 else
1210 {
1211 if ((unsigned int)len > (unsigned int)precision)
1212 len = precision;
1213 }
1214 }
1215 if (!(flags & LEFT))
1216 while (len < field_width--)
1217 {
1218 f += ' ';
1219 done++;
1220 }
1221 for (i = 0; i < len; ++i)
1222 {
1223 #define MY_MB_CUR_MAX 1
1224 char mb[MY_MB_CUR_MAX];
1225 int mbcount, j;
1226 mbcount = wctomb(mb, *sw++);
1227 if (mbcount <= 0)
1228 {
1229 break;
1230 }
1231 for (j = 0; j < mbcount; j++)
1232 {
1233 f += mb[j];
1234 done++;
1235 }
1236 }
1237 while (len < field_width--)
1238 {
1239 f += ' ';
1240 done++;
1241 }
1242 return done;
1243 }
1244
1245 static int
1246 wstringa(std::wstring& f, const char* sa, int len, int field_width, int precision, int flags)
1247 {
1248 int i, done = 0;
1249 if (sa == NULL)
1250 {
1251 sa = "<NULL>";
1252 len = 6;
1253 }
1254 else
1255 {
1256 if (len == -1)
1257 {
1258 len = 0;
1259 while ((unsigned int)len < (unsigned int)precision && sa[len])
1260 len++;
1261 }
1262 else
1263 {
1264 if ((unsigned int)len > (unsigned int)precision)
1265 len = precision;
1266 }
1267 }
1268 if (!(flags & LEFT))
1269 while (len < field_width--)
1270 {
1271 f += L' ';
1272 done++;
1273 }
1274 for (i = 0; i < len;)
1275 {
1276 wchar_t w;
1277 int mbcount;
1278 mbcount = mbtowc(&w, sa+i, len-i);
1279 if (mbcount <= 0)
1280 break;
1281 f += w;
1282 done++;
1283 i += mbcount;
1284 }
1285 while (len < field_width--)
1286 {
1287 f += L' ';
1288 done++;
1289 }
1290 return done;
1291 }
1292
1293 #define _isnanl _isnan
1294 #define _finitel _finite
1295
1296 std::string
1297 ssvprintf ( const char *fmt, va_list args )
1298 {
1299 ULONGLONG num;
1300 int base;
1301 long double _ldouble;
1302 double _double;
1303 const char *s;
1304 const wchar_t* sw;
1305 int result;
1306 std::string f;
1307
1308 int flags; /* flags to number() */
1309
1310 int field_width; /* width of output field */
1311 int precision; /* min. # of digits for integers; max
1312 number of chars for from string */
1313 int qualifier = 0; /* 'h', 'l', 'L' or 'I64' for integer fields */
1314
1315 for (; *fmt ; ++fmt)
1316 {
1317 if (*fmt != '%')
1318 {
1319 f += *fmt;
1320 continue;
1321 }
1322
1323 /* process flags */
1324 flags = 0;
1325 repeat:
1326 ++fmt; /* this also skips first '%' */
1327 switch (*fmt) {
1328 case '-': flags |= LEFT; goto repeat;
1329 case '+': flags |= PLUS; goto repeat;
1330 case ' ': flags |= SPACE; goto repeat;
1331 case '#': flags |= SPECIAL; goto repeat;
1332 case '0': flags |= ZEROPAD; goto repeat;
1333 }
1334
1335 /* get field width */
1336 field_width = -1;
1337 if (isdigit(*fmt))
1338 field_width = skip_atoi(&fmt);
1339 else if (*fmt == '*') {
1340 ++fmt;
1341 /* it's the next argument */
1342 field_width = va_arg(args, int);
1343 if (field_width < 0) {
1344 field_width = -field_width;
1345 flags |= LEFT;
1346 }
1347 }
1348
1349 /* get the precision */
1350 precision = -1;
1351 if (*fmt == '.') {
1352 ++fmt;
1353 if (isdigit(*fmt))
1354 precision = skip_atoi(&fmt);
1355 else if (*fmt == '*') {
1356 ++fmt;
1357 /* it's the next argument */
1358 precision = va_arg(args, int);
1359 }
1360 if (precision < 0)
1361 precision = 0;
1362 }
1363
1364 /* get the conversion qualifier */
1365 qualifier = 0;
1366 // %Z can be just stand alone or as size_t qualifier
1367 if ( *fmt == 'Z' ) {
1368 qualifier = *fmt;
1369 switch ( *(fmt+1)) {
1370 case 'o':
1371 case 'b':
1372 case 'X':
1373 case 'x':
1374 case 'd':
1375 case 'i':
1376 case 'u':
1377 ++fmt;
1378 break;
1379 default:
1380 break;
1381 }
1382 } else if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'w') {
1383 qualifier = *fmt;
1384 ++fmt;
1385 } else if (*fmt == 'I' && *(fmt+1) == '6' && *(fmt+2) == '4') {
1386 qualifier = *fmt;
1387 fmt += 3;
1388 }
1389
1390 // go fine with ll instead of L
1391 if ( *fmt == 'l' ) {
1392 ++fmt;
1393 qualifier = 'L';
1394 }
1395
1396 /* default base */
1397 base = 10;
1398
1399 switch (*fmt) {
1400 case 'c':
1401 if (!(flags & LEFT))
1402 while (--field_width > 0)
1403 {
1404 f += ' ';
1405 }
1406 if (qualifier == 'l' || qualifier == 'w')
1407 {
1408 f += (char)(unsigned char)(wchar_t) va_arg(args,int);
1409 }
1410 else
1411 {
1412 f += (char)(unsigned char) va_arg(args,int);
1413 }
1414 while (--field_width > 0)
1415 {
1416 f += ' ';
1417 }
1418 continue;
1419
1420 case 'C':
1421 if (!(flags & LEFT))
1422 while (--field_width > 0)
1423 {
1424 f += ' ';
1425 }
1426 if (qualifier == 'h')
1427 {
1428 f += (char)(unsigned char) va_arg(args,int);
1429 }
1430 else
1431 {
1432 f += (char)(unsigned char)(wchar_t) va_arg(args,int);
1433 }
1434 while (--field_width > 0)
1435 {
1436 f += ' ';
1437 }
1438 continue;
1439
1440 case 's':
1441 if (qualifier == 'l' || qualifier == 'w') {
1442 /* print unicode string */
1443 sw = va_arg(args, wchar_t *);
1444 result = stringw(f, sw, -1, field_width, precision, flags);
1445 } else {
1446 /* print ascii string */
1447 s = va_arg(args, char *);
1448 result = do_string(f, s, -1, field_width, precision, flags);
1449 }
1450 if (result < 0)
1451 {
1452 assert(!"TODO FIXME handle error better");
1453 return f;
1454 }
1455 continue;
1456
1457 case 'S':
1458 if (qualifier == 'h') {
1459 /* print ascii string */
1460 s = va_arg(args, char *);
1461 result = do_string(f, s, -1, field_width, precision, flags);
1462 } else {
1463 /* print unicode string */
1464 sw = va_arg(args, wchar_t *);
1465 result = stringw(f, sw, -1, field_width, precision, flags);
1466 }
1467 if (result < 0)
1468 {
1469 assert(!"TODO FIXME handle error better");
1470 return f;
1471 }
1472 continue;
1473
1474 /*case 'Z':
1475 if (qualifier == 'w') {
1476 // print counted unicode string
1477 PUNICODE_STRING pus = va_arg(args, PUNICODE_STRING);
1478 if ((pus == NULL) || (pus->Buffer == NULL)) {
1479 sw = NULL;
1480 len = -1;
1481 } else {
1482 sw = pus->Buffer;
1483 len = pus->Length / sizeof(WCHAR);
1484 }
1485 result = stringw(f, sw, len, field_width, precision, flags);
1486 } else {
1487 // print counted ascii string
1488 PANSI_STRING pas = va_arg(args, PANSI_STRING);
1489 if ((pas == NULL) || (pas->Buffer == NULL)) {
1490 s = NULL;
1491 len = -1;
1492 } else {
1493 s = pas->Buffer;
1494 len = pas->Length;
1495 }
1496 result = string(f, s, -1, field_width, precision, flags);
1497 }
1498 if (result < 0)
1499 return -1;
1500 continue;*/
1501
1502 case 'e':
1503 case 'E':
1504 case 'f':
1505 case 'g':
1506 case 'G':
1507 if (qualifier == 'l' || qualifier == 'L' ) {
1508 _ldouble = va_arg(args, long double);
1509
1510 if ( _isnanl(_ldouble) )
1511 {
1512 f += "Nan";
1513 }
1514 else if ( !_finitel(_ldouble) )
1515 {
1516 if ( _ldouble < 0 )
1517 f += "-Inf";
1518 else
1519 f += "+Inf";
1520 } else {
1521 if ( precision == -1 )
1522 precision = 6;
1523 result = numberfl(f,_ldouble,*fmt,field_width,precision,flags);
1524 if (result < 0)
1525 {
1526 assert(!"TODO FIXME handle error better");
1527 return f;
1528 }
1529 }
1530 } else {
1531 _double = (double)va_arg(args, double);
1532
1533 if ( _isnan(_double) )
1534 {
1535 f += "Nan";
1536 }
1537 else if ( !_finite(_double) )
1538 {
1539 if ( _double < 0 )
1540 f += "-Inf";
1541 else
1542 f += "+Inf";
1543 }
1544 else
1545 {
1546 if ( precision == -1 )
1547 precision = 6;
1548 result = numberf(f,_double,*fmt,field_width,precision,flags);
1549 if (result < 0)
1550 {
1551 assert(!"TODO FIXME handle error better");
1552 return f;
1553 }
1554 }
1555 }
1556 continue;
1557
1558 case 'p':
1559 if (field_width == -1) {
1560 field_width = 2*sizeof(void *);
1561 flags |= ZEROPAD;
1562 }
1563 result = number(f,
1564 (size_t) va_arg(args, void *), 16,
1565 field_width, precision, flags);
1566 if (result < 0)
1567 {
1568 assert(!"TODO FIXME handle error better");
1569 return f;
1570 }
1571 continue;
1572
1573 case 'n':
1574 if (qualifier == 'l') {
1575 long * ip = va_arg(args, long *);
1576 *ip = 0;
1577 } else {
1578 int * ip = va_arg(args, int *);
1579 *ip = 0;
1580 }
1581 continue;
1582
1583 /* integer number formats - set up the flags and "break" */
1584 case 'o':
1585 base = 8;
1586 break;
1587
1588 case 'b':
1589 base = 2;
1590 break;
1591
1592 case 'X':
1593 flags |= LARGE;
1594 case 'x':
1595 base = 16;
1596 break;
1597
1598 case 'd':
1599 case 'i':
1600 flags |= SIGN;
1601 case 'u':
1602 break;
1603
1604 default:
1605 if (*fmt != '%')
1606 {
1607 f += '%';
1608 }
1609 if (*fmt)
1610 {
1611 f += *fmt;
1612 }
1613 else
1614 --fmt;
1615 continue;
1616 }
1617
1618 if (qualifier == 'I')
1619 num = va_arg(args, ULONGLONG);
1620 else if (qualifier == 'l') {
1621 if (flags & SIGN)
1622 num = va_arg(args, long);
1623 else
1624 num = va_arg(args, unsigned long);
1625 }
1626 else if (qualifier == 'h') {
1627 if (flags & SIGN)
1628 num = va_arg(args, int);
1629 else
1630 num = va_arg(args, unsigned int);
1631 }
1632 else if (flags & SIGN)
1633 num = va_arg(args, int);
1634 else
1635 num = va_arg(args, unsigned int);
1636 result = number(f, num, base, field_width, precision, flags);
1637 if (result < 0)
1638 {
1639 assert(!"TODO FIXME handle error better");
1640 return f;
1641 }
1642 }
1643 //putc('\0',f);
1644 return f;
1645 }
1646
1647 std::wstring
1648 sswvprintf ( const wchar_t* fmt, va_list args )
1649 {
1650 ULONGLONG num;
1651 int base;
1652 long double _ldouble;
1653 double _double;
1654 const wchar_t* s;
1655 const char* sa;
1656 int result;
1657 std::wstring f;
1658
1659 int flags; /* flags to number() */
1660
1661 int field_width; /* width of output field */
1662 int precision; /* min. # of digits for integers; max
1663 number of chars for from string */
1664 int qualifier = 0; /* 'h', 'l', 'L' or 'I64' for integer fields */
1665
1666 for (; *fmt ; ++fmt)
1667 {
1668 if (*fmt != L'%')
1669 {
1670 f += *fmt;
1671 continue;
1672 }
1673
1674 /* process flags */
1675 flags = 0;
1676 repeat:
1677 ++fmt; /* this also skips first '%' */
1678 switch (*fmt) {
1679 case L'-': flags |= LEFT; goto repeat;
1680 case L'+': flags |= PLUS; goto repeat;
1681 case L' ': flags |= SPACE; goto repeat;
1682 case L'#': flags |= SPECIAL; goto repeat;
1683 case L'0': flags |= ZEROPAD; goto repeat;
1684 }
1685
1686 /* get field width */
1687 field_width = -1;
1688 if (isdigit(*fmt))
1689 field_width = skip_wtoi(&fmt);
1690 else if (*fmt == L'*') {
1691 ++fmt;
1692 /* it's the next argument */
1693 field_width = va_arg(args, int);
1694 if (field_width < 0) {
1695 field_width = -field_width;
1696 flags |= LEFT;
1697 }
1698 }
1699
1700 /* get the precision */
1701 precision = -1;
1702 if (*fmt == L'.') {
1703 ++fmt;
1704 if (iswdigit(*fmt))
1705 precision = skip_wtoi(&fmt);
1706 else if (*fmt == L'*') {
1707 ++fmt;
1708 /* it's the next argument */
1709 precision = va_arg(args, int);
1710 }
1711 if (precision < 0)
1712 precision = 0;
1713 }
1714
1715 /* get the conversion qualifier */
1716 qualifier = 0;
1717 // %Z can be just stand alone or as size_t qualifier
1718 if ( *fmt == L'Z' ) {
1719 qualifier = *fmt;
1720 switch ( *(fmt+1)) {
1721 case L'o':
1722 case L'b':
1723 case L'X':
1724 case L'x':
1725 case L'd':
1726 case L'i':
1727 case L'u':
1728 ++fmt;
1729 break;
1730 default:
1731 break;
1732 }
1733 } else if (*fmt == L'h' || *fmt == L'l' || *fmt == L'L' || *fmt == L'w') {
1734 qualifier = *fmt;
1735 ++fmt;
1736 } else if (*fmt == L'I' && *(fmt+1) == L'6' && *(fmt+2) == L'4') {
1737 qualifier = *fmt;
1738 fmt += 3;
1739 }
1740
1741 // go fine with ll instead of L
1742 if ( *fmt == L'l' ) {
1743 ++fmt;
1744 qualifier = L'L';
1745 }
1746
1747 /* default base */
1748 base = 10;
1749
1750 switch (*fmt) {
1751 case L'c':
1752 if (!(flags & LEFT))
1753 while (--field_width > 0)
1754 {
1755 f += L' ';
1756 }
1757 if ( qualifier == L'h' )
1758 {
1759 f += (wchar_t)(char)(unsigned char) va_arg(args,int);
1760 }
1761 else
1762 {
1763 f += (wchar_t) va_arg(args,int);
1764 }
1765 while (--field_width > 0)
1766 {
1767 f += ' ';
1768 }
1769 continue;
1770
1771 case 'C':
1772 if (!(flags & LEFT))
1773 while (--field_width > 0)
1774 {
1775 f += L' ';
1776 }
1777 if (qualifier == L'l' || qualifier == L'w')
1778 {
1779 f += (wchar_t) va_arg(args,int);
1780 }
1781 else
1782 {
1783 f += (wchar_t)(char)(unsigned char) va_arg(args,int);
1784 }
1785 while (--field_width > 0)
1786 {
1787 f += L' ';
1788 }
1789 continue;
1790
1791 case 's':
1792 if (qualifier == L'h') {
1793 /* print ascii string */
1794 sa = va_arg(args, char *);
1795 result = wstringa(f, sa, -1, field_width, precision, flags);
1796 } else {
1797 /* print unicode string */
1798 s = va_arg(args, wchar_t *);
1799 result = do_wstring(f, s, -1, field_width, precision, flags);
1800 }
1801 if (result < 0)
1802 {
1803 assert(!"TODO FIXME handle error better");
1804 return f;
1805 }
1806 continue;
1807
1808 case 'S':
1809 if (qualifier == L'l' || qualifier == L'w') {
1810 /* print unicode string */
1811 s = va_arg(args, wchar_t *);
1812 result = do_wstring(f, s, -1, field_width, precision, flags);
1813 } else {
1814 /* print ascii string */
1815 sa = va_arg(args, char *);
1816 result = wstringa(f, sa, -1, field_width, precision, flags);
1817 }
1818 if (result < 0)
1819 {
1820 assert(!"TODO FIXME handle error better");
1821 return f;
1822 }
1823 continue;
1824
1825 case L'e':
1826 case L'E':
1827 case L'f':
1828 case L'g':
1829 case L'G':
1830 if (qualifier == L'l' || qualifier == L'L' )
1831 {
1832 _ldouble = va_arg(args, long double);
1833
1834 if ( _isnanl(_ldouble) )
1835 {
1836 f += L"Nan";
1837 }
1838 else if ( !_finitel(_ldouble) )
1839 {
1840 if ( _ldouble < 0 )
1841 f += L"-Inf";
1842 else
1843 f += L"+Inf";
1844 } else {
1845 if ( precision == -1 )
1846 precision = 6;
1847 result = wnumberfl(f,_ldouble,*fmt,field_width,precision,flags);
1848 if (result < 0)
1849 {
1850 assert(!"TODO FIXME handle error better");
1851 return f;
1852 }
1853 }
1854 } else {
1855 _double = (double)va_arg(args, double);
1856
1857 if ( _isnan(_double) )
1858 {
1859 f += L"Nan";
1860 }
1861 else if ( !_finite(_double) )
1862 {
1863 if ( _double < 0 )
1864 f += L"-Inf";
1865 else
1866 f += L"+Inf";
1867 }
1868 else
1869 {
1870 if ( precision == -1 )
1871 precision = 6;
1872 result = wnumberf(f,_double,*fmt,field_width,precision,flags);
1873 if (result < 0)
1874 {
1875 assert(!"TODO FIXME handle error better");
1876 return f;
1877 }
1878 }
1879 }
1880 continue;
1881
1882 case L'p':
1883 if (field_width == -1) {
1884 field_width = 2*sizeof(void *);
1885 flags |= ZEROPAD;
1886 }
1887 result = wnumber(f,
1888 (size_t) va_arg(args, void *), 16,
1889 field_width, precision, flags);
1890 if (result < 0)
1891 {
1892 assert(!"TODO FIXME handle error better");
1893 return f;
1894 }
1895 continue;
1896
1897 case L'n':
1898 if (qualifier == L'l') {
1899 long * ip = va_arg(args, long *);
1900 *ip = 0;
1901 } else {
1902 int * ip = va_arg(args, int *);
1903 *ip = 0;
1904 }
1905 continue;
1906
1907 /* integer number formats - set up the flags and "break" */
1908 case L'o':
1909 base = 8;
1910 break;
1911
1912 case L'b':
1913 base = 2;
1914 break;
1915
1916 case L'X':
1917 flags |= LARGE;
1918 case L'x':
1919 base = 16;
1920 break;
1921
1922 case L'd':
1923 case L'i':
1924 flags |= SIGN;
1925 case L'u':
1926 break;
1927
1928 default:
1929 if (*fmt != L'%')
1930 {
1931 f += L'%';
1932 }
1933 if (*fmt)
1934 {
1935 f += *fmt;
1936 }
1937 else
1938 --fmt;
1939 continue;
1940 }
1941
1942 if (qualifier == L'I')
1943 num = va_arg(args, ULONGLONG);
1944 else if (qualifier == L'l') {
1945 if (flags & SIGN)
1946 num = va_arg(args, long);
1947 else
1948 num = va_arg(args, unsigned long);
1949 }
1950 else if (qualifier == L'h') {
1951 if (flags & SIGN)
1952 num = va_arg(args, int);
1953 else
1954 num = va_arg(args, unsigned int);
1955 }
1956 else if (flags & SIGN)
1957 num = va_arg(args, int);
1958 else
1959 num = va_arg(args, unsigned int);
1960 result = wnumber(f, num, base, field_width, precision, flags);
1961 if (result < 0)
1962 {
1963 assert(!"TODO FIXME handle error better");
1964 return f;
1965 }
1966 }
1967 //putc('\0',f);
1968 return f;
1969 }