2 * COPYRIGHT: GNU GPL, see COPYING in the top level directory
3 * PROJECT: ReactOS crt library
4 * FILE: lib/sdk/crt/printf/streamout.c
5 * PURPOSE: Implementation of streamout
6 * PROGRAMMER: Timo Kreuzer
17 # define streamout wstreamout
18 # define format_float format_floatw
22 #define BUFFER_SIZE (32 + 17)
24 int mbtowc(wchar_t *wchar
, const char *mbchar
, size_t count
);
25 int wctomb(char *mbchar
, wchar_t wchar
);
27 typedef struct _STRING
29 unsigned short Length
;
30 unsigned short MaximumLength
;
36 /* Formatting flags */
37 FLAG_ALIGN_LEFT
= 0x01,
38 FLAG_FORCE_SIGN
= 0x02,
39 FLAG_FORCE_SIGNSP
= 0x04,
43 /* Data format flags */
46 FLAG_WIDECHAR
= FLAG_LONG
,
49 FLAG_INTPTR
= FLAG_INT64
,
53 FLAG_LONGDOUBLE
= 0x800,
56 #define va_arg_f(argptr, flags) \
57 (flags & FLAG_INT64) ? va_arg(argptr, __int64) : \
58 (flags & FLAG_SHORT) ? (short)va_arg(argptr, int) : \
61 #define va_arg_fu(argptr, flags) \
62 (flags & FLAG_INT64) ? va_arg(argptr, unsigned __int64) : \
63 (flags & FLAG_SHORT) ? (unsigned short)va_arg(argptr, int) : \
64 va_arg(argptr, unsigned int)
66 #define va_arg_ffp(argptr, flags) \
67 (flags & FLAG_LONGDOUBLE) ? va_arg(argptr, long double) : \
68 va_arg(argptr, double)
70 #define get_exp(f) floor(f == 0 ? 0 : (f >= 0 ? log10(f) : log10(-f)))
71 #define round(x) floor((x) + 0.5)
73 #ifndef _USER32_WSPRINTF
77 /* Due to restrictions in kernel mode regarding the use of floating point,
78 we prevent it from being inlined */
89 static const TCHAR digits_l
[] = _T("0123456789abcdef0x");
90 static const TCHAR digits_u
[] = _T("0123456789ABCDEF0X");
91 static const TCHAR _nan
[] = _T("#QNAN");
92 static const TCHAR _infinity
[] = _T("#INF");
93 const TCHAR
*digits
= digits_l
;
94 int exponent
= 0, sign
;
95 long double fpval
, fpval2
;
96 int padding
= 0, num_digits
, val32
, base
= 10;
98 /* Normalize the precision */
99 if (precision
< 0) precision
= 6;
100 else if (precision
> 17)
102 padding
= precision
- 17;
106 /* Get the float value and calculate the exponent */
107 fpval
= va_arg_ffp(*argptr
, flags
);
108 exponent
= get_exp(fpval
);
109 sign
= fpval
< 0 ? -1 : 1;
116 if (precision
> 0) precision
--;
117 if (exponent
< -4 || exponent
>= precision
) goto case_e
;
119 /* Shift the decimal point and round */
120 fpval2
= round(sign
* fpval
* pow(10., precision
));
122 /* Skip trailing zeroes */
123 while (precision
&& (unsigned __int64
)fpval2
% 10 == 0)
134 /* Shift the decimal point and round */
135 fpval2
= round(sign
* fpval
* pow(10., precision
- exponent
));
137 /* Compensate for changed exponent through rounding */
138 if (fpval2
>= (unsigned __int64
)pow(10., precision
+ 1))
141 fpval2
= round(sign
* fpval
* pow(10., precision
- exponent
));
144 val32
= exponent
>= 0 ? exponent
: -exponent
;
146 // FIXME: handle length of exponent field:
147 // http://msdn.microsoft.com/de-de/library/0fatw238%28VS.80%29.aspx
151 *--(*string
) = digits
[val32
% 10];
155 /* Sign for the exponent */
156 *--(*string
) = exponent
>= 0 ? _T('+') : _T('-');
158 /* Add 'e' or 'E' separator */
159 *--(*string
) = digits
[0xe];
170 /* Shift the decimal point and round */
171 fpval2
= round(sign
* fpval
* pow(10., precision
));
180 else if (flags
& FLAG_FORCE_SIGN
)
182 else if (flags
& FLAG_FORCE_SIGNSP
)
185 /* Handle special cases first */
188 (*string
) -= sizeof(_nan
) / sizeof(TCHAR
) - 1;
189 _tcscpy((*string
), _nan
);
192 else if (!_finite(fpval
))
194 (*string
) -= sizeof(_infinity
) / sizeof(TCHAR
) - 1;
195 _tcscpy((*string
), _infinity
);
201 while (padding
-- > 0) *--(*string
) = _T('0');
203 /* Digits after the decimal point */
204 num_digits
= precision
;
205 while (num_digits
-- > 0)
207 *--(*string
) = digits
[(unsigned __int64
)fpval2
% 10];
212 if (precision
> 0 || flags
& FLAG_SPECIAL
)
213 *--(*string
) = _T('.');
215 /* Digits before the decimal point */
218 *--(*string
) = digits
[(unsigned __int64
)fpval2
% base
];
221 while ((unsigned __int64
)fpval2
);
228 streamout_char(FILE *stream
, int chr
)
230 #if defined(_USER32_WSPRINTF) || defined(_LIBCNT_)
231 /* Check if the buffer is full */
232 if (stream
->_cnt
< sizeof(TCHAR
))
235 *(TCHAR
*)stream
->_ptr
= chr
;
236 stream
->_ptr
+= sizeof(TCHAR
);
237 stream
->_cnt
-= sizeof(TCHAR
);
241 return _fputtc((TCHAR
)chr
, stream
) != _TEOF
;
247 streamout_astring(FILE *stream
, const char *string
, int count
)
256 if ((len
= mbtowc(&chr
, string
, MB_CUR_MAX
)) < 1) break;
261 if (streamout_char(stream
, chr
) == 0) return -1;
270 streamout_wstring(FILE *stream
, const wchar_t *string
, int count
)
278 char mbchar
[MB_CUR_MAX
], *ptr
= mbchar
;
281 mblen
= wctomb(mbchar
, *string
++);
282 if (mblen
<= 0) return written
;
284 while (chr
= *ptr
++, mblen
--)
289 if (streamout_char(stream
, chr
) == 0) return -1;
298 #define streamout_string streamout_wstring
300 #define streamout_string streamout_astring
303 #ifdef _USER32_WSPRINTF
304 # define USE_MULTISIZE 0
306 # define USE_MULTISIZE 1
311 streamout(FILE *stream
, const TCHAR
*format
, va_list argptr
)
313 static const TCHAR digits_l
[] = _T("0123456789abcdef0x");
314 static const TCHAR digits_u
[] = _T("0123456789ABCDEF0X");
315 static const char *_nullstring
= "(null)";
316 TCHAR buffer
[BUFFER_SIZE
+ 1];
319 const TCHAR
*digits
, *prefix
;
320 int base
, len
, prefixlen
, fieldwidth
, precision
, padding
;
321 int written
= 1, written_all
= 0;
323 unsigned __int64 val64
;
325 buffer
[BUFFER_SIZE
] = '\0';
331 /* Check for end of format string */
332 if (chr
== _T('\0')) break;
334 /* Check for 'normal' character or double % */
335 if ((chr
!= _T('%')) ||
336 (chr
= *format
++) == _T('%'))
338 /* Write the character to the stream */
339 if ((written
= streamout_char(stream
, chr
)) == 0) return -1;
340 written_all
+= written
;
348 if (chr
== _T('-')) flags
|= FLAG_ALIGN_LEFT
;
349 else if (chr
== _T('+')) flags
|= FLAG_FORCE_SIGN
;
350 else if (chr
== _T(' ')) flags
|= FLAG_FORCE_SIGNSP
;
351 else if (chr
== _T('0')) flags
|= FLAG_PAD_ZERO
;
352 else if (chr
== _T('#')) flags
|= FLAG_SPECIAL
;
357 /* Handle field width modifier */
360 fieldwidth
= va_arg(argptr
, int);
363 flags
|= FLAG_ALIGN_LEFT
;
364 fieldwidth
= -fieldwidth
;
371 while (chr
>= _T('0') && chr
<= _T('9'))
373 fieldwidth
= fieldwidth
* 10 + (chr
- _T('0'));
378 /* Handle precision modifier */
385 precision
= va_arg(argptr
, int);
391 while (chr
>= _T('0') && chr
<= _T('9'))
393 precision
= precision
* 10 + (chr
- _T('0'));
400 /* Handle argument size prefix */
403 if (chr
== _T('h')) flags
|= FLAG_SHORT
;
404 else if (chr
== _T('w')) flags
|= FLAG_WIDECHAR
;
405 else if (chr
== _T('L')) flags
|= 0; // FIXME: long double
406 else if (chr
== _T('F')) flags
|= 0; // FIXME: what is that?
407 else if (chr
== _T('l'))
409 /* Check if this is the 2nd 'l' in a row */
410 if (format
[-2] == 'l') flags
|= FLAG_INT64
;
411 else flags
|= FLAG_LONG
;
413 else if (chr
== _T('I'))
415 if (format
[0] == _T('3') && format
[1] == _T('2'))
419 else if (format
[0] == _T('6') && format
[1] == _T('4'))
424 else if (format
[0] == _T('x') || format
[0] == _T('X') ||
425 format
[0] == _T('d') || format
[0] == _T('i') ||
426 format
[0] == _T('u') || format
[0] == _T('o'))
428 flags
|= FLAG_INTPTR
;
435 while (USE_MULTISIZE
);
437 /* Handle the format specifier */
439 string
= &buffer
[BUFFER_SIZE
];
445 if (flags
& FLAG_INT64
)
446 *va_arg(argptr
, __int64
*) = written_all
;
447 else if (flags
& FLAG_SHORT
)
448 *va_arg(argptr
, short*) = written_all
;
450 *va_arg(argptr
, int*) = written_all
;
455 if (!(flags
& FLAG_SHORT
)) flags
|= FLAG_WIDECHAR
;
461 if (!(flags
& FLAG_SHORT
)) flags
|= FLAG_WIDECHAR
;
466 if (flags
& FLAG_WIDECHAR
)
468 ((wchar_t*)string
)[0] = va_arg(argptr
, int);
469 ((wchar_t*)string
)[1] = _T('\0');
473 ((char*)string
)[0] = va_arg(argptr
, int);
474 ((char*)string
)[1] = _T('\0');
479 nt_string
= va_arg(argptr
, void*);
480 if (nt_string
&& (string
= nt_string
->Buffer
))
482 len
= nt_string
->Length
;
483 if (flags
& FLAG_WIDECHAR
) len
/= sizeof(wchar_t);
490 string
= va_arg(argptr
, void*);
492 if (!(flags
& FLAG_SHORT
)) flags
|= FLAG_WIDECHAR
;
497 string
= va_arg(argptr
, void*);
499 if (!(flags
& FLAG_SHORT
)) flags
|= FLAG_WIDECHAR
;
505 string
= (TCHAR
*)_nullstring
;
506 flags
&= ~FLAG_WIDECHAR
;
509 if (flags
& FLAG_WIDECHAR
)
510 len
= wcslen((wchar_t*)string
);
512 len
= strlen((char*)string
);
513 if (precision
>= 0 && len
> precision
) len
= precision
;
517 #ifndef _USER32_WSPRINTF
526 flags
|= FLAG_WIDECHAR
;
528 flags
&= ~FLAG_WIDECHAR
;
530 /* Use external function, one for kernel one for user mode */
531 format_float(chr
, flags
, precision
, &string
, &prefix
, &argptr
);
532 len
= _tcslen(string
);
539 val64
= (__int64
)va_arg_f(argptr
, flags
);
541 if ((__int64
)val64
< 0)
546 else if (flags
& FLAG_FORCE_SIGN
)
548 else if (flags
& FLAG_FORCE_SIGNSP
)
555 if (flags
& FLAG_SPECIAL
)
558 if (precision
> 0) precision
--;
563 precision
= 2 * sizeof(void*);
564 flags
&= ~FLAG_PAD_ZERO
;
565 flags
|= FLAG_INTPTR
;
574 if (flags
& FLAG_SPECIAL
)
576 prefix
= &digits
[16];
577 #ifdef _USER32_WSPRINTF
584 val64
= va_arg_fu(argptr
, flags
);
588 flags
|= FLAG_WIDECHAR
;
590 flags
&= ~FLAG_WIDECHAR
;
592 if (precision
< 0) precision
= 1;
594 /* Gather digits in reverse order */
597 *--string
= digits
[val64
% base
];
602 len
= _tcslen(string
);
606 /* Treat anything else as a new character */
611 /* Calculate padding */
612 prefixlen
= prefix
? _tcslen(prefix
) : 0;
613 if (precision
< 0) precision
= 0;
614 padding
= fieldwidth
- len
- prefixlen
- precision
;
615 if (padding
< 0) padding
= 0;
617 /* Optional left space padding */
618 if ((flags
& (FLAG_ALIGN_LEFT
| FLAG_PAD_ZERO
)) == 0)
620 for (; padding
> 0; padding
--)
622 if ((written
= streamout_char(stream
, _T(' '))) == 0) return -1;
623 written_all
+= written
;
627 /* Optional prefix */
630 written
= streamout_string(stream
, prefix
, prefixlen
);
631 if (written
== -1) return -1;
632 written_all
+= written
;
635 /* Optional left '0' padding */
636 if ((flags
& FLAG_ALIGN_LEFT
) == 0) precision
+= padding
;
637 while (precision
-- > 0)
639 if ((written
= streamout_char(stream
, _T('0'))) == 0) return -1;
640 written_all
+= written
;
643 /* Output the string */
644 if (flags
& FLAG_WIDECHAR
)
645 written
= streamout_wstring(stream
, (wchar_t*)string
, len
);
647 written
= streamout_astring(stream
, (char*)string
, len
);
648 if (written
== -1) return -1;
649 written_all
+= written
;
651 #if 0 && SUPPORT_FLOAT
652 /* Optional right '0' padding */
653 while (precision
-- > 0)
655 if ((written
= streamout_char(stream
, _T('0'))) == 0) return -1;
656 written_all
+= written
;
661 /* Optional right padding */
662 if (flags
& FLAG_ALIGN_LEFT
)
664 while (padding
-- > 0)
666 if ((written
= streamout_char(stream
, _T(' '))) == 0) return -1;
667 written_all
+= written
;
673 if (written
== -1) return -1;