widl: Generate helper macros for WinRT implementation.
[wine/zf.git] / dlls / msvcrt / printf.h
blobc6c074de795cff2606284917523297ae55a065f9
1 /*
2 * Copyright 2011 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "bnum.h"
21 #ifdef PRINTF_WIDE
22 #define APICHAR wchar_t
23 #define CONVCHAR char
24 #define FUNC_NAME(func) func ## _w
25 #else
26 #define APICHAR char
27 #define CONVCHAR wchar_t
28 #define FUNC_NAME(func) func ## _a
29 #endif
31 struct FUNC_NAME(_str_ctx) {
32 size_t len;
33 APICHAR *buf;
36 static int FUNC_NAME(puts_clbk_str)(void *ctx, int len, const APICHAR *str)
38 struct FUNC_NAME(_str_ctx) *out = ctx;
40 if(!out->buf)
41 return len;
43 if(out->len < len) {
44 memmove(out->buf, str, out->len*sizeof(APICHAR));
45 out->buf += out->len;
46 out->len = 0;
47 return -1;
50 memmove(out->buf, str, len*sizeof(APICHAR));
51 out->buf += len;
52 out->len -= len;
53 return len;
56 static inline const APICHAR* FUNC_NAME(pf_parse_int)(const APICHAR *fmt, int *val)
58 *val = 0;
60 while (*fmt >= '0' && *fmt <= '9') {
61 *val *= 10;
62 *val += *fmt++ - '0';
65 return fmt;
68 /* pf_fill: takes care of signs, alignment, zero and field padding */
69 static inline int FUNC_NAME(pf_fill)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
70 int len, pf_flags *flags, BOOL left)
72 int i, r = 0, written;
74 if(flags->Sign && !strchr("diaAeEfFgG", flags->Format))
75 flags->Sign = 0;
77 if(left && flags->Sign) {
78 APICHAR ch = flags->Sign;
79 flags->FieldLength--;
80 if(flags->PadZero)
81 r = pf_puts(puts_ctx, 1, &ch);
83 written = r;
85 if((!left && flags->LeftAlign) || (left && !flags->LeftAlign)) {
86 APICHAR ch;
88 if(left && flags->PadZero)
89 ch = '0';
90 else
91 ch = ' ';
93 for(i=0; i<flags->FieldLength-len && r>=0; i++) {
94 r = pf_puts(puts_ctx, 1, &ch);
95 written += r;
100 if(r>=0 && left && flags->Sign && !flags->PadZero) {
101 APICHAR ch = flags->Sign;
102 r = pf_puts(puts_ctx, 1, &ch);
103 written += r;
106 return r>=0 ? written : r;
109 #ifndef PRINTF_HELPERS
110 #define PRINTF_HELPERS
111 static inline int wcstombs_len(char *mbstr, const wchar_t *wcstr,
112 int len, _locale_t locale)
114 char buf[MB_LEN_MAX];
115 int i, r, mblen = 0;
117 for(i=0; i<len; i++) {
118 r = _wctomb_l(mbstr ? mbstr+mblen : buf, wcstr[i], locale);
119 if(r < 0) return r;
120 mblen += r;
122 return mblen;
125 static inline int mbstowcs_len(wchar_t *wcstr, const char *mbstr,
126 int len, _locale_t locale)
128 int i, r, wlen = 0;
129 WCHAR buf;
131 for(i=0; i<len; wlen++) {
132 r = _mbtowc_l(wcstr ? wcstr+wlen : &buf, mbstr+i, len-i, locale);
133 if(r < 0) return r;
134 i += r ? r : 1;
136 return wlen;
138 #endif
140 static inline int FUNC_NAME(pf_output_wstr)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
141 const wchar_t *str, int len, _locale_t locale)
143 #ifdef PRINTF_WIDE
144 return pf_puts(puts_ctx, len, str);
145 #else
146 LPSTR out;
147 int len_a = wcstombs_len(NULL, str, len, locale);
148 if(len_a < 0)
149 return -1;
151 out = HeapAlloc(GetProcessHeap(), 0, len_a);
152 if(!out)
153 return -1;
155 wcstombs_len(out, str, len, locale);
156 len = pf_puts(puts_ctx, len_a, out);
157 HeapFree(GetProcessHeap(), 0, out);
158 return len;
159 #endif
162 static inline int FUNC_NAME(pf_output_str)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
163 const char *str, int len, _locale_t locale)
165 #ifdef PRINTF_WIDE
166 LPWSTR out;
167 int len_w = mbstowcs_len(NULL, str, len, locale);
168 if(len_w < 0)
169 return -1;
171 out = HeapAlloc(GetProcessHeap(), 0, len_w*sizeof(WCHAR));
172 if(!out)
173 return -1;
175 mbstowcs_len(out, str, len, locale);
176 len = pf_puts(puts_ctx, len_w, out);
177 HeapFree(GetProcessHeap(), 0, out);
178 return len;
179 #else
180 return pf_puts(puts_ctx, len, str);
181 #endif
184 static inline int FUNC_NAME(pf_output_format_wstr)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
185 const wchar_t *str, int len, pf_flags *flags, _locale_t locale)
187 int r, ret;
189 if(len < 0) {
190 /* Do not search past the length specified by the precision. */
191 if(flags->Precision>=0)
192 len = wcsnlen(str, flags->Precision);
193 else
194 len = wcslen(str);
197 if(flags->Precision>=0 && flags->Precision<len)
198 len = flags->Precision;
200 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
201 ret = r;
202 if(r >= 0) {
203 r = FUNC_NAME(pf_output_wstr)(pf_puts, puts_ctx, str, len, locale);
204 ret += r;
206 if(r >= 0) {
207 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
208 ret += r;
211 return r>=0 ? ret : r;
214 static inline int FUNC_NAME(pf_output_format_str)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
215 const char *str, int len, pf_flags *flags, _locale_t locale)
217 int r, ret;
219 if(len < 0) {
220 /* Do not search past the length specified by the precision. */
221 if(flags->Precision>=0)
222 len = strnlen(str, flags->Precision);
223 else
224 len = strlen(str);
227 if(flags->Precision>=0 && flags->Precision<len)
228 len = flags->Precision;
230 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
231 ret = r;
232 if(r >= 0) {
233 r = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, str, len, locale);
234 ret += r;
236 if(r >= 0) {
237 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
238 ret += r;
241 return r>=0 ? ret : r;
244 static inline int FUNC_NAME(pf_handle_string)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
245 const void *str, int len, pf_flags *flags, _locale_t locale, BOOL legacy_wide)
247 BOOL api_is_wide = sizeof(APICHAR) == sizeof(wchar_t);
248 BOOL complement_is_narrow = legacy_wide ? api_is_wide : FALSE;
249 #ifdef PRINTF_WIDE
251 if(!str)
252 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, L"(null)", 6, flags, locale);
253 #else
254 if(!str)
255 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, "(null)", 6, flags, locale);
256 #endif
258 if((flags->NaturalString && api_is_wide) || flags->WideString || flags->IntegerLength == LEN_LONG)
259 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, str, len, flags, locale);
260 if((flags->NaturalString && !api_is_wide) || flags->IntegerLength == LEN_SHORT)
261 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, len, flags, locale);
263 if((flags->Format=='S' || flags->Format=='C') == complement_is_narrow)
264 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, len, flags, locale);
265 else
266 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, str, len, flags, locale);
269 static inline int FUNC_NAME(pf_output_special_fp)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
270 double v, pf_flags *flags, _locale_t locale,
271 BOOL legacy_msvcrt_compat, BOOL three_digit_exp)
273 APICHAR pfx[16], sfx[8], *p;
274 int len = 0, r, frac_len, pfx_len, sfx_len;
276 if(!legacy_msvcrt_compat) {
277 const char *str;
279 if(isinf(v)) {
280 if(strchr("AEFG", flags->Format)) str = "INF";
281 else str = "inf";
282 }else {
283 if(strchr("AEFG", flags->Format)) str = (flags->Sign == '-' ? "NAN(IND)" : "NAN");
284 else str = (flags->Sign == '-' ? "nan(ind)" : "nan");
287 flags->Precision = -1;
288 flags->PadZero = FALSE;
289 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, -1, flags, locale);
292 /* workaround a bug in native implementation */
293 if(flags->Format=='g' || flags->Format=='G')
294 flags->Precision--;
296 p = pfx;
297 if(flags->PadZero && (flags->Format=='a' || flags->Format=='A')) {
298 if (flags->Sign) *p++ = flags->Sign;
299 *p++ = '0';
300 *p++ = (flags->Format=='a' ? 'x' : 'X');
301 r = pf_puts(puts_ctx, p-pfx, pfx);
302 if(r < 0) return r;
303 len += r;
305 flags->FieldLength -= p-pfx;
308 p = pfx;
309 if(!flags->PadZero && (flags->Format=='a' || flags->Format=='A')) {
310 *p++ = '0';
311 *p++ = (flags->Format=='a' ? 'x' : 'X');
314 *p++ = '1';
315 *p++ = *(locale ? locale->locinfo : get_locinfo())->lconv->decimal_point;
316 *p++ = '#';
317 frac_len = 1;
319 if(isinf(v)) {
320 *p++ = 'I';
321 *p++ = 'N';
322 *p++ = 'F';
323 frac_len += 3;
324 }else if(flags->Sign == '-') {
325 *p++ = 'I';
326 *p++ = 'N';
327 *p++ = 'D';
328 frac_len += 3;
329 }else {
330 *p++ = 'Q';
331 *p++ = 'N';
332 *p++ = 'A';
333 *p++ = 'N';
334 frac_len += 4;
336 *p = 0;
337 pfx_len = p - pfx;
339 if(len) flags->Sign = 0;
341 if(flags->Precision>=0 && flags->Precision<frac_len)
342 p[flags->Precision - frac_len - 1]++;
344 p = sfx;
345 if(strchr("aAeE", flags->Format)) {
346 if(flags->Format == 'a') *p++ = 'p';
347 else if(flags->Format == 'A') *p++ = 'P';
348 else if(flags->Format == 'e') *p++ = 'e';
349 else *p++ = 'E';
351 *p++ = '+';
352 *p++ = '0';
354 if(flags->Format == 'e' || flags->Format == 'E') {
355 *p++ = '0';
356 if(three_digit_exp) *p++ = '0';
359 *p = 0;
361 if(!flags->Alternate && (flags->Format == 'g' || flags->Format == 'G')) sfx_len = frac_len;
362 else sfx_len = flags->Precision;
364 if(sfx_len == -1) {
365 if(strchr("fFeE", flags->Format)) sfx_len = 6;
366 else if(flags->Format == 'a' || flags->Format == 'A') sfx_len = 13;
368 sfx_len += p - sfx - frac_len;
370 if(sfx_len > 0) flags->FieldLength -= sfx_len;
371 if(flags->Precision >= 0) {
372 if(!flags->Precision) flags->Precision--;
373 flags->Precision += pfx_len - frac_len;
375 #ifdef PRINTF_WIDE
376 r = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, pfx, -1, flags, locale);
377 #else
378 r = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, pfx, -1, flags, locale);
379 #endif
380 if(r < 0) return r;
381 len += r;
383 flags->FieldLength = sfx_len;
384 flags->PadZero = TRUE;
385 flags->Precision = -1;
386 flags->Sign = 0;
387 #ifdef PRINTF_WIDE
388 r = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, sfx, -1, flags, locale);
389 #else
390 r = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, sfx, -1, flags, locale);
391 #endif
392 if(r < 0) return r;
393 len += r;
395 return len;
398 static inline int FUNC_NAME(pf_output_hex_fp)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
399 double v, pf_flags *flags, _locale_t locale, BOOL standard_rounding)
401 const APICHAR digits[2][16] = {
402 { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' },
403 { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }
406 APICHAR pfx[4+MANT_BITS/4+1], sfx[8], *p;
407 ULONGLONG mant;
408 int len = 0, sfx_len = 0, r, exp;
410 mant = (*(ULONGLONG*)&v) << 1;
411 exp = (mant >> MANT_BITS);
412 exp -= (1 << (EXP_BITS - 1)) - 1;
413 mant = (mant << EXP_BITS) >> (EXP_BITS+1);
415 p = pfx;
416 if(flags->PadZero) {
417 if(flags->Sign) *p++ = flags->Sign;
418 *p++ = '0';
419 *p++ = (flags->Format=='a' ? 'x' : 'X');
420 r = pf_puts(puts_ctx, p-pfx, pfx);
421 if(r < 0) return r;
422 len += r;
424 flags->FieldLength -= p-pfx;
425 flags->Sign = 0;
426 p = pfx;
427 }else {
428 *p++ = '0';
429 *p++ = (flags->Format=='a' ? 'x' : 'X');
431 if(exp == -(1 << (EXP_BITS-1))+1) {
432 if(!mant) exp = 0;
433 else exp++;
434 *p++ = '0';
435 }else {
436 *p++ = '1';
438 *p++ = *(locale ? locale->locinfo : get_locinfo())->lconv->decimal_point;
439 for(r=MANT_BITS/4-1; r>=0; r--) {
440 p[r] = digits[flags->Format == 'A'][mant & 15];
441 mant >>= 4;
443 if(!flags->Precision) {
444 if(p[0] >= '8') p[-2]++;
445 if(!flags->Alternate) p--;
446 }else if(flags->Precision>0 && flags->Precision<MANT_BITS/4) {
447 BOOL round_up = FALSE;
449 if(!standard_rounding) round_up = (p[flags->Precision] >= '8');
450 else if(p[flags->Precision] > '8') round_up = TRUE;
451 else if(p[flags->Precision] == '8') {
452 for(r = flags->Precision+1; r<MANT_BITS/4; r++) {
453 if(p[r] != '0') {
454 round_up = TRUE;
455 break;
459 if(!round_up) {
460 if(p[flags->Precision-1] <= '9') round_up = (p[flags->Precision-1] - '0') & 1;
461 else if(p[flags->Precision-1] <= 'F') round_up = (p[flags->Precision-1] - 'A') & 1;
462 else round_up = (p[flags->Precision-1] - 'a') & 1;
466 for(r=flags->Precision-1; r>=0 && round_up; r--) {
467 round_up = FALSE;
468 if(p[r]=='f' || p[r]=='F') {
469 p[r] = '0';
470 round_up = TRUE;
471 }else if(p[r] == '9') {
472 p[r] = (flags->Format == 'a' ? 'a' : 'A');
473 }else {
474 p[r]++;
477 if(round_up) p[-2]++;
478 p += flags->Precision;
479 }else {
480 p += MANT_BITS/4;
481 if(flags->Precision > MANT_BITS/4) sfx_len += flags->Precision - MANT_BITS/4;
483 *p = 0;
485 p = sfx;
486 *p++ = (flags->Format == 'a' ? 'p' : 'P');
487 if(exp < 0) {
488 *p++ = '-';
489 exp = -exp;
490 }else {
491 *p++ = '+';
493 for(r=3; r>=0; r--) {
494 p[r] = exp%10 + '0';
495 exp /= 10;
496 if(!exp) break;
498 for(exp=0; exp<4-r; exp++)
499 p[exp] = p[exp+r];
500 p += exp;
501 *p = 0;
502 sfx_len += p - sfx;
504 flags->FieldLength -= sfx_len;
505 flags->Precision = -1;
506 #ifdef PRINTF_WIDE
507 r = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, pfx, -1, flags, locale);
508 #else
509 r = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, pfx, -1, flags, locale);
510 #endif
511 if(r < 0) return r;
512 len += r;
514 flags->FieldLength = sfx_len;
515 flags->PadZero = TRUE;
516 flags->Sign = 0;
517 #ifdef PRINTF_WIDE
518 r = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, sfx, -1, flags, locale);
519 #else
520 r = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, sfx, -1, flags, locale);
521 #endif
522 if(r < 0) return r;
523 len += r;
525 return len;
528 /* pf_integer_conv: prints x to buf, including alternate formats and
529 additional precision digits, but not field characters or the sign */
530 static inline void FUNC_NAME(pf_integer_conv)(APICHAR *buf, pf_flags *flags, LONGLONG x)
532 unsigned int base;
533 const char *digits;
534 int i, j, k;
536 if(flags->Format == 'o')
537 base = 8;
538 else if(flags->Format=='x' || flags->Format=='X')
539 base = 16;
540 else
541 base = 10;
543 if(flags->Format == 'X')
544 digits = "0123456789ABCDEFX";
545 else
546 digits = "0123456789abcdefx";
548 if(x<0 && (flags->Format=='d' || flags->Format=='i')) {
549 x = -x;
550 flags->Sign = '-';
553 i = 0;
554 if(x == 0) {
555 flags->Alternate = FALSE;
556 if(flags->Precision)
557 buf[i++] = '0';
558 } else {
559 while(x != 0) {
560 j = (ULONGLONG)x%base;
561 x = (ULONGLONG)x/base;
562 buf[i++] = digits[j];
565 k = flags->Precision-i;
566 while(k-- > 0)
567 buf[i++] = '0';
568 if(flags->Alternate) {
569 if(base == 16) {
570 buf[i++] = digits[16];
571 buf[i++] = '0';
572 } else if(base==8 && buf[i-1]!='0')
573 buf[i++] = '0';
576 /* Adjust precision so pf_fill won't truncate the number later */
577 flags->Precision = i;
579 buf[i] = '\0';
580 j = 0;
581 while(--i > j) {
582 APICHAR tmp = buf[j];
583 buf[j] = buf[i];
584 buf[i] = tmp;
585 j++;
589 static inline int FUNC_NAME(pf_output_fp)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
590 double v, pf_flags *flags, _locale_t locale, BOOL three_digit_exp,
591 BOOL standard_rounding)
593 int e2, e10 = 0, round_pos, round_limb, radix_pos, first_limb_len, i, len, r, ret;
594 BYTE bnum_data[FIELD_OFFSET(struct bnum, data[BNUM_PREC64])];
595 struct bnum *b = (struct bnum*)bnum_data;
596 APICHAR buf[LIMB_DIGITS + 1];
597 BOOL trim_tail = FALSE, round_up = FALSE;
598 pf_flags f;
599 int limb_len, prec;
600 ULONGLONG m;
601 DWORD l;
603 if(flags->Precision == -1)
604 flags->Precision = 6;
606 v = frexp(v, &e2);
607 if(v) {
608 m = (ULONGLONG)1 << (MANT_BITS - 1);
609 m |= (*(ULONGLONG*)&v & (((ULONGLONG)1 << (MANT_BITS - 1)) - 1));
610 b->b = 0;
611 b->e = 2;
612 b->size = BNUM_PREC64;
613 b->data[0] = m % LIMB_MAX;
614 b->data[1] = m / LIMB_MAX;
615 e2 -= MANT_BITS;
617 while(e2 > 0) {
618 int shift = e2 > 29 ? 29 : e2;
619 if(bnum_lshift(b, shift)) e10 += LIMB_DIGITS;
620 e2 -= shift;
622 while(e2 < 0) {
623 int shift = -e2 > 9 ? 9 : -e2;
624 if(bnum_rshift(b, shift)) e10 -= LIMB_DIGITS;
625 e2 += shift;
627 } else {
628 b->b = 0;
629 b->e = 1;
630 b->size = BNUM_PREC64;
631 b->data[0] = 0;
632 e10 = -LIMB_DIGITS;
635 if(!b->data[bnum_idx(b, b->e-1)])
636 first_limb_len = 1;
637 else
638 first_limb_len = floor(log10(b->data[bnum_idx(b, b->e - 1)])) + 1;
639 radix_pos = first_limb_len + LIMB_DIGITS + e10;
641 round_pos = flags->Precision;
642 if(flags->Format=='f' || flags->Format=='F')
643 round_pos += radix_pos;
644 else if(!flags->Precision || flags->Format=='e' || flags->Format=='E')
645 round_pos++;
646 if (round_pos <= first_limb_len)
647 round_limb = b->e + (first_limb_len - round_pos) / LIMB_DIGITS - 1;
648 else
649 round_limb = b->e - (round_pos - first_limb_len - 1) / LIMB_DIGITS - 2;
651 if (b->b<=round_limb && round_limb<b->e) {
652 if (round_pos <= first_limb_len) {
653 round_pos = first_limb_len - round_pos;
654 } else {
655 round_pos = LIMB_DIGITS - (round_pos - first_limb_len) % LIMB_DIGITS;
656 if (round_pos == LIMB_DIGITS) round_pos = 0;
659 if (round_pos) {
660 l = b->data[bnum_idx(b, round_limb)] % p10s[round_pos];
661 b->data[bnum_idx(b, round_limb)] -= l;
662 if(!standard_rounding) round_up = (2*l >= p10s[round_pos]);
663 else if(2*l > p10s[round_pos]) round_up = TRUE;
664 else if(2*l == p10s[round_pos]) {
665 for(r = round_limb-1; r >= b->b; r--) {
666 if(b->data[bnum_idx(b, r)]) {
667 round_up = TRUE;
668 break;
672 if(!round_up) round_up = b->data[bnum_idx(b, round_limb)] / p10s[round_pos] & 1;
674 } else if(round_limb - 1 >= b->b) {
675 if(!standard_rounding) round_up = (2*b->data[bnum_idx(b, round_limb-1)] >= LIMB_MAX);
676 else if(2*b->data[bnum_idx(b, round_limb-1)] > LIMB_MAX) round_up = TRUE;
677 else if(2*b->data[bnum_idx(b, round_limb-1)] == LIMB_MAX) {
678 for(r = round_limb-2; r >= b->b; r--) {
679 if(b->data[bnum_idx(b, r)]) {
680 round_up = TRUE;
681 break;
685 if(!round_up) round_up = b->data[bnum_idx(b, round_limb)] & 1;
688 b->b = round_limb;
690 if(round_up) {
691 b->data[bnum_idx(b, b->b)] += p10s[round_pos];
692 for(i = b->b; i < b->e; i++) {
693 if(b->data[bnum_idx(b, i)] < LIMB_MAX) break;
695 b->data[bnum_idx(b, i)] -= LIMB_MAX;
696 if(i+1 < b->e) b->data[bnum_idx(b, i+1)]++;
697 else b->data[bnum_idx(b, i+1)] = 1;
699 if(i == b->e-1) {
700 if(!b->data[bnum_idx(b, b->e-1)])
701 i = 1;
702 else
703 i = floor(log10(b->data[bnum_idx(b, b->e-1)])) + 1;
704 if(i != first_limb_len) {
705 first_limb_len = i;
706 radix_pos++;
708 } else if(i == b->e) {
709 first_limb_len = 1;
710 radix_pos++;
711 b->e++;
715 else if(b->e <= round_limb) { /* got 0 or 1 after rounding */
716 if(b->e == round_limb) {
717 if(!standard_rounding) round_up = b->data[bnum_idx(b, b->e-1)] >= LIMB_MAX/2;
718 else if(b->data[bnum_idx(b, b->e-1)] > LIMB_MAX/2) round_up = TRUE;
719 else if(b->data[bnum_idx(b, b->e-1)] == LIMB_MAX/2) {
720 for(r = b->e-2; r >= b->b; r--) {
721 if(b->data[bnum_idx(b, r)]) {
722 round_up = TRUE;
723 break;
729 b->data[bnum_idx(b, round_limb)] = round_up;
730 b->b = round_limb;
731 b->e = b->b + 1;
732 first_limb_len = 1;
733 radix_pos++;
736 if(flags->Format=='g' || flags->Format=='G') {
737 trim_tail = TRUE;
739 if(radix_pos>=-3 && radix_pos<=flags->Precision) {
740 flags->Format -= 1;
741 if(!flags->Precision) flags->Precision++;
742 flags->Precision -= radix_pos;
743 } else {
744 flags->Format -= 2;
745 if(flags->Precision > 0) flags->Precision--;
749 if(trim_tail && !flags->Alternate) {
750 for(i=round_limb; flags->Precision>0 && i<b->e; i++) {
751 if(i>=b->b)
752 l = b->data[bnum_idx(b, i)];
753 else
754 l = 0;
756 if(i == round_limb) {
757 if(flags->Format=='f' || flags->Format=='F')
758 r = radix_pos + flags->Precision;
759 else
760 r = flags->Precision + 1;
761 r = first_limb_len + LIMB_DIGITS * (b->e-1 - b->b) - r;
762 r %= LIMB_DIGITS;
763 if(r < 0) r += LIMB_DIGITS;
764 l /= p10s[r];
765 limb_len = LIMB_DIGITS - r;
766 } else {
767 limb_len = LIMB_DIGITS;
770 if(!l) {
771 flags->Precision -= limb_len;
772 } else {
773 while(l % 10 == 0) {
774 flags->Precision--;
775 l /= 10;
779 if(flags->Precision <= 0) {
780 flags->Precision = 0;
781 break;
783 if(l)
784 break;
788 len = flags->Precision;
789 if(flags->Precision || flags->Alternate) len++;
790 if(flags->Format=='f' || flags->Format=='F') {
791 len += (radix_pos > 0 ? radix_pos : 1);
792 } else if(flags->Format=='e' || flags->Format=='E') {
793 radix_pos--;
794 if(!trim_tail || radix_pos) {
795 len += 3; /* strlen("1e+") */
796 if(three_digit_exp || radix_pos<-99 || radix_pos>99) len += 3;
797 else len += 2;
798 } else {
799 len++;
803 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
804 if(r < 0) return r;
805 ret = r;
807 f.Format = 'd';
808 f.PadZero = TRUE;
809 if(flags->Format=='f' || flags->Format=='F') {
810 if(radix_pos <= 0) {
811 buf[0] = '0';
812 r = pf_puts(puts_ctx, 1, buf);
813 if(r < 0) return r;
814 ret += r;
817 limb_len = LIMB_DIGITS;
818 for(i=b->e-1; radix_pos>0 && i>=b->b; i--) {
819 limb_len = (i == b->e-1 ? first_limb_len : LIMB_DIGITS);
820 l = b->data[bnum_idx(b, i)];
821 if(limb_len > radix_pos) {
822 f.Precision = radix_pos;
823 l /= p10s[limb_len - radix_pos];
824 limb_len = limb_len - radix_pos;
825 } else {
826 f.Precision = limb_len;
827 limb_len = LIMB_DIGITS;
829 radix_pos -= f.Precision;
830 FUNC_NAME(pf_integer_conv)(buf, &f, l);
832 r = pf_puts(puts_ctx, f.Precision, buf);
833 if(r < 0) return r;
834 ret += r;
837 buf[0] = '0';
838 for(; radix_pos>0; radix_pos--) {
839 r = pf_puts(puts_ctx, 1, buf);
840 if(r < 0) return r;
841 ret += r;
844 if(flags->Precision || flags->Alternate) {
845 buf[0] = *(locale ? locale->locinfo : get_locinfo())->lconv->decimal_point;
846 r = pf_puts(puts_ctx, 1, buf);
847 if(r < 0) return r;
848 ret += r;
851 prec = flags->Precision;
852 buf[0] = '0';
853 for(; prec>0 && radix_pos+LIMB_DIGITS-first_limb_len<0; radix_pos++, prec--) {
854 r = pf_puts(puts_ctx, 1, buf);
855 if(r < 0) return r;
856 ret += r;
859 for(; prec>0 && i>=b->b; i--) {
860 l = b->data[bnum_idx(b, i)];
861 if(limb_len != LIMB_DIGITS)
862 l %= p10s[limb_len];
863 if(limb_len > prec) {
864 f.Precision = prec;
865 l /= p10s[limb_len - prec];
866 } else {
867 f.Precision = limb_len;
868 limb_len = LIMB_DIGITS;
870 prec -= f.Precision;
871 FUNC_NAME(pf_integer_conv)(buf, &f, l);
873 r = pf_puts(puts_ctx, f.Precision, buf);
874 if(r < 0) return r;
875 ret += r;
878 buf[0] = '0';
879 for(; prec>0; prec--) {
880 r = pf_puts(puts_ctx, 1, buf);
881 if(r < 0) return r;
882 ret += r;
884 } else {
885 l = b->data[bnum_idx(b, b->e - 1)];
886 l /= p10s[first_limb_len - 1];
888 buf[0] = '0' + l;
889 r = pf_puts(puts_ctx, 1, buf);
890 if(r < 0) return r;
891 ret += r;
893 if(flags->Precision || flags->Alternate) {
894 buf[0] = *(locale ? locale->locinfo : get_locinfo())->lconv->decimal_point;
895 r = pf_puts(puts_ctx, 1, buf);
896 if(r < 0) return r;
897 ret += r;
900 prec = flags->Precision;
901 limb_len = LIMB_DIGITS;
902 for(i=b->e-1; prec>0 && i>=b->b; i--) {
903 l = b->data[bnum_idx(b, i)];
904 if(i == b->e-1) {
905 limb_len = first_limb_len - 1;
906 l %= p10s[limb_len];
909 if(limb_len > prec) {
910 f.Precision = prec;
911 l /= p10s[limb_len - prec];
912 } else {
913 f.Precision = limb_len;
914 limb_len = LIMB_DIGITS;
916 prec -= f.Precision;
917 FUNC_NAME(pf_integer_conv)(buf, &f, l);
919 r = pf_puts(puts_ctx, f.Precision, buf);
920 if(r < 0) return r;
921 ret += r;
924 buf[0] = '0';
925 for(; prec>0; prec--) {
926 r = pf_puts(puts_ctx, 1, buf);
927 if(r < 0) return r;
928 ret += r;
931 if(!trim_tail || radix_pos) {
932 buf[0] = flags->Format;
933 buf[1] = radix_pos < 0 ? '-' : '+';
934 r = pf_puts(puts_ctx, 2, buf);
935 if(r < 0) return r;
936 ret += r;
938 f.Precision = three_digit_exp ? 3 : 2;
939 FUNC_NAME(pf_integer_conv)(buf, &f, radix_pos);
940 r = pf_puts(puts_ctx, f.Precision, buf);
941 if(r < 0) return r;
942 ret += r;
946 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
947 if(r < 0) return r;
948 ret += r;
949 return ret;
952 int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const APICHAR *fmt,
953 _locale_t locale, DWORD options,
954 args_clbk pf_args, void *args_ctx, __ms_va_list *valist)
956 const APICHAR *q, *p = fmt;
957 APICHAR buf[32];
958 int written = 0, pos, i;
959 pf_flags flags;
960 BOOL positional_params = options & MSVCRT_PRINTF_POSITIONAL_PARAMS;
961 BOOL invoke_invalid_param_handler = options & MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER;
962 #if _MSVCR_VER >= 140
963 BOOL legacy_wide = options & _CRT_INTERNAL_PRINTF_LEGACY_WIDE_SPECIFIERS;
964 BOOL legacy_msvcrt_compat = options & _CRT_INTERNAL_PRINTF_LEGACY_MSVCRT_COMPATIBILITY;
965 BOOL three_digit_exp = options & _CRT_INTERNAL_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS;
966 BOOL standard_rounding = options & _CRT_INTERNAL_PRINTF_STANDARD_ROUNDING;
967 #else
968 BOOL legacy_wide = TRUE, legacy_msvcrt_compat = TRUE;
969 BOOL three_digit_exp = _get_output_format() != _TWO_DIGIT_EXPONENT;
970 BOOL standard_rounding = FALSE;
971 #endif
973 if (!MSVCRT_CHECK_PMT(fmt != NULL))
974 return -1;
976 while(*p) {
977 /* output characters before '%' */
978 for(q=p; *q && *q!='%'; q++);
979 if(p != q) {
980 i = pf_puts(puts_ctx, q-p, p);
981 if(i < 0)
982 return i;
984 written += i;
985 p = q;
986 continue;
989 /* *p == '%' here */
990 p++;
992 /* output a single '%' character */
993 if(*p == '%') {
994 i = pf_puts(puts_ctx, 1, p++);
995 if(i < 0)
996 return i;
998 written += i;
999 continue;
1002 /* check parameter position */
1003 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &pos)) && *q=='$')
1004 p = q+1;
1005 else
1006 pos = -1;
1008 /* parse the flags */
1009 memset(&flags, 0, sizeof(flags));
1010 while(*p) {
1011 if(*p=='+' || *p==' ') {
1012 if(flags.Sign != '+')
1013 flags.Sign = *p;
1014 } else if(*p == '-')
1015 flags.LeftAlign = TRUE;
1016 else if(*p == '0')
1017 flags.PadZero = TRUE;
1018 else if(*p == '#')
1019 flags.Alternate = TRUE;
1020 else
1021 break;
1023 p++;
1026 /* parse the width */
1027 if(*p == '*') {
1028 p++;
1029 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
1030 p = q+1;
1031 else
1032 i = -1;
1034 flags.FieldLength = pf_args(args_ctx, i, VT_INT, valist).get_int;
1035 if(flags.FieldLength < 0) {
1036 flags.LeftAlign = TRUE;
1037 flags.FieldLength = -flags.FieldLength;
1039 } else while (*p >= '0' && *p <= '9') {
1040 flags.FieldLength *= 10;
1041 flags.FieldLength += *p++ - '0';
1044 /* parse the precision */
1045 flags.Precision = -1;
1046 if(*p == '.') {
1047 flags.Precision = 0;
1048 p++;
1049 if(*p == '*') {
1050 p++;
1051 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
1052 p = q+1;
1053 else
1054 i = -1;
1056 flags.Precision = pf_args(args_ctx, i, VT_INT, valist).get_int;
1057 } else while (*p >= '0' && *p <= '9') {
1058 flags.Precision *= 10;
1059 flags.Precision += *p++ - '0';
1063 /* parse argument size modifier */
1064 while(*p) {
1065 if(*p=='l' && *(p+1)=='l') {
1066 flags.IntegerDouble = TRUE;
1067 p++;
1068 } else if(*p=='l') {
1069 flags.IntegerLength = LEN_LONG;
1070 } else if(*p == 'h') {
1071 flags.IntegerLength = LEN_SHORT;
1072 } else if(*p == 'I') {
1073 if(*(p+1)=='6' && *(p+2)=='4') {
1074 flags.IntegerDouble = TRUE;
1075 p += 2;
1076 } else if(*(p+1)=='3' && *(p+2)=='2')
1077 p += 2;
1078 else if(p[1] && strchr("diouxX", p[1]))
1079 flags.IntegerNative = TRUE;
1080 else
1081 break;
1082 } else if(*p == 'w')
1083 flags.WideString = TRUE;
1084 #if _MSVCR_VER == 0 || _MSVCR_VER >= 140
1085 else if((*p == 'z' || *p == 't') && p[1] && strchr("diouxX", p[1]))
1086 flags.IntegerNative = TRUE;
1087 else if(*p == 'j')
1088 flags.IntegerDouble = TRUE;
1089 #endif
1090 #if _MSVCR_VER >= 140
1091 else if(*p == 'T')
1092 flags.NaturalString = TRUE;
1093 #endif
1094 else if(*p != 'L' && ((*p != 'F' && *p != 'N') || !legacy_msvcrt_compat))
1095 break;
1096 p++;
1099 flags.Format = *p;
1101 if(flags.Format == 's' || flags.Format == 'S') {
1102 i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx,
1103 pf_args(args_ctx, pos, VT_PTR, valist).get_ptr,
1104 -1, &flags, locale, legacy_wide);
1105 } else if(flags.Format == 'c' || flags.Format == 'C') {
1106 int ch = pf_args(args_ctx, pos, VT_INT, valist).get_int;
1108 i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx, &ch, 1, &flags, locale, legacy_wide);
1109 if(i < 0) i = 0; /* ignore conversion error */
1110 } else if(flags.Format == 'p') {
1111 flags.Format = 'X';
1112 flags.PadZero = TRUE;
1113 i = flags.Precision;
1114 flags.Precision = 2*sizeof(void*);
1115 FUNC_NAME(pf_integer_conv)(buf, &flags,
1116 (ULONG_PTR)pf_args(args_ctx, pos, VT_PTR, valist).get_ptr);
1117 flags.PadZero = FALSE;
1118 flags.Precision = i;
1120 #ifdef PRINTF_WIDE
1121 i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, buf, -1, &flags, locale);
1122 #else
1123 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, buf, -1, &flags, locale);
1124 #endif
1125 } else if(flags.Format == 'n') {
1126 int *used;
1128 if(!n_format_enabled) {
1129 MSVCRT_INVALID_PMT("\'n\' format specifier disabled", EINVAL);
1130 return -1;
1133 used = pf_args(args_ctx, pos, VT_PTR, valist).get_ptr;
1134 *used = written;
1135 i = 0;
1136 } else if(flags.Format && strchr("diouxX", flags.Format)) {
1137 APICHAR *tmp = buf;
1138 int max_len;
1140 /* 0 padding is added after '0x' if Alternate flag is in use */
1141 if((flags.Format=='x' || flags.Format=='X') && flags.PadZero && flags.Alternate
1142 && !flags.LeftAlign && flags.Precision<flags.FieldLength-2)
1143 flags.Precision = flags.FieldLength - 2;
1145 max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10;
1146 if(max_len > ARRAY_SIZE(buf))
1147 tmp = HeapAlloc(GetProcessHeap(), 0, max_len);
1148 if(!tmp)
1149 return -1;
1151 if(flags.IntegerDouble || (flags.IntegerNative && sizeof(void*) == 8))
1152 FUNC_NAME(pf_integer_conv)(tmp, &flags, pf_args(args_ctx, pos,
1153 VT_I8, valist).get_longlong);
1154 else if(flags.Format=='d' || flags.Format=='i')
1155 FUNC_NAME(pf_integer_conv)(tmp, &flags,
1156 flags.IntegerLength != LEN_SHORT ?
1157 pf_args(args_ctx, pos, VT_INT, valist).get_int :
1158 (short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
1159 else
1160 FUNC_NAME(pf_integer_conv)(tmp, &flags,
1161 flags.IntegerLength != LEN_SHORT ?
1162 (unsigned)pf_args(args_ctx, pos, VT_INT, valist).get_int :
1163 (unsigned short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
1165 #ifdef PRINTF_WIDE
1166 i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, tmp, -1, &flags, locale);
1167 #else
1168 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, tmp, -1, &flags, locale);
1169 #endif
1170 if(tmp != buf)
1171 HeapFree(GetProcessHeap(), 0, tmp);
1172 } else if(flags.Format && strchr("aAeEfFgG", flags.Format)) {
1173 double val = pf_args(args_ctx, pos, VT_R8, valist).get_double;
1175 if(signbit(val)) {
1176 flags.Sign = '-';
1177 val = -val;
1180 if(isinf(val) || isnan(val))
1181 i = FUNC_NAME(pf_output_special_fp)(pf_puts, puts_ctx, val, &flags,
1182 locale, legacy_msvcrt_compat, three_digit_exp);
1183 else if(flags.Format=='a' || flags.Format=='A')
1184 i = FUNC_NAME(pf_output_hex_fp)(pf_puts, puts_ctx, val, &flags,
1185 locale, standard_rounding);
1186 else
1187 i = FUNC_NAME(pf_output_fp)(pf_puts, puts_ctx, val, &flags,
1188 locale, three_digit_exp, standard_rounding);
1189 } else {
1190 if(invoke_invalid_param_handler) {
1191 _invalid_parameter(NULL, NULL, NULL, 0, 0);
1192 *_errno() = EINVAL;
1193 return -1;
1196 continue;
1199 if(i < 0)
1200 return i;
1201 written += i;
1202 p++;
1205 return written;
1208 #ifndef PRINTF_WIDE
1209 enum types_clbk_flags {
1210 TYPE_CLBK_VA_LIST = 1,
1211 TYPE_CLBK_POSITIONAL = 2,
1212 TYPE_CLBK_ERROR_POS = 4,
1213 TYPE_CLBK_ERROR_TYPE = 8
1216 /* This functions stores types of arguments. It uses args[0] internally */
1217 static printf_arg arg_clbk_type(void *ctx, int pos, int type, __ms_va_list *valist)
1219 static const printf_arg ret;
1220 printf_arg *args = ctx;
1222 if(pos == -1) {
1223 args[0].get_int |= TYPE_CLBK_VA_LIST;
1224 return ret;
1225 } else
1226 args[0].get_int |= TYPE_CLBK_POSITIONAL;
1228 if(pos<1 || pos>_ARGMAX)
1229 args[0].get_int |= TYPE_CLBK_ERROR_POS;
1230 else if(args[pos].get_int && args[pos].get_int!=type)
1231 args[0].get_int |= TYPE_CLBK_ERROR_TYPE;
1232 else
1233 args[pos].get_int = type;
1235 return ret;
1237 #endif
1239 int FUNC_NAME(create_positional_ctx)(void *args_ctx, const APICHAR *format, __ms_va_list valist)
1241 struct FUNC_NAME(_str_ctx) puts_ctx = {INT_MAX, NULL};
1242 printf_arg *args = args_ctx;
1243 int i, j;
1245 i = FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk_str), &puts_ctx, format, NULL,
1246 MSVCRT_PRINTF_POSITIONAL_PARAMS, arg_clbk_type, args_ctx, NULL);
1247 if(i < 0)
1248 return i;
1250 if(args[0].get_int==0 || args[0].get_int==TYPE_CLBK_VA_LIST)
1251 return 0;
1252 if(args[0].get_int != TYPE_CLBK_POSITIONAL)
1253 return -1;
1255 for(i=_ARGMAX; i>0; i--)
1256 if(args[i].get_int)
1257 break;
1259 for(j=1; j<=i; j++) {
1260 switch(args[j].get_int) {
1261 case VT_I8:
1262 args[j].get_longlong = va_arg(valist, LONGLONG);
1263 break;
1264 case VT_INT:
1265 args[j].get_int = va_arg(valist, int);
1266 break;
1267 case VT_R8:
1268 args[j].get_double = va_arg(valist, double);
1269 break;
1270 case VT_PTR:
1271 args[j].get_ptr = va_arg(valist, void*);
1272 break;
1273 default:
1274 return -1;
1278 return j;
1281 #undef APICHAR
1282 #undef CONVCHAR
1283 #undef FUNC_NAME