winebus.sys: Rename UDEV bus device variables to be consistent.
[wine.git] / dlls / msvcrt / printf.h
blobd6b945e8ac7149600cdc04ceda769e4edf3a50c9
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 round_pos++;
709 if (round_pos == LIMB_DIGITS)
711 round_pos = 0;
712 round_limb++;
715 } else if(i == b->e) {
716 first_limb_len = 1;
717 radix_pos++;
718 b->e++;
720 round_pos++;
721 if (round_pos == LIMB_DIGITS)
723 round_pos = 0;
724 round_limb++;
729 else if(b->e <= round_limb) { /* got 0 or 1 after rounding */
730 if(b->e == round_limb) {
731 if(!standard_rounding) round_up = b->data[bnum_idx(b, b->e-1)] >= LIMB_MAX/2;
732 else if(b->data[bnum_idx(b, b->e-1)] > LIMB_MAX/2) round_up = TRUE;
733 else if(b->data[bnum_idx(b, b->e-1)] == LIMB_MAX/2) {
734 for(r = b->e-2; r >= b->b; r--) {
735 if(b->data[bnum_idx(b, r)]) {
736 round_up = TRUE;
737 break;
743 b->data[bnum_idx(b, round_limb)] = round_up;
744 b->b = round_limb;
745 b->e = b->b + 1;
746 first_limb_len = 1;
747 radix_pos++;
750 if(flags->Format=='g' || flags->Format=='G') {
751 trim_tail = TRUE;
753 if(radix_pos>=-3 && radix_pos<=flags->Precision) {
754 flags->Format -= 1;
755 if(!flags->Precision) flags->Precision++;
756 flags->Precision -= radix_pos;
757 } else {
758 flags->Format -= 2;
759 if(flags->Precision > 0) flags->Precision--;
763 if(trim_tail && !flags->Alternate) {
764 for(i=round_limb; flags->Precision>0 && i<b->e; i++) {
765 if(i>=b->b)
766 l = b->data[bnum_idx(b, i)];
767 else
768 l = 0;
770 if(i == round_limb) {
771 if(flags->Format=='f' || flags->Format=='F')
772 r = radix_pos + flags->Precision;
773 else
774 r = flags->Precision + 1;
775 r = first_limb_len + LIMB_DIGITS * (b->e-1 - b->b) - r;
776 r %= LIMB_DIGITS;
777 if(r < 0) r += LIMB_DIGITS;
778 l /= p10s[r];
779 limb_len = LIMB_DIGITS - r;
780 } else {
781 limb_len = LIMB_DIGITS;
784 if(!l) {
785 flags->Precision -= limb_len;
786 } else {
787 while(l % 10 == 0) {
788 flags->Precision--;
789 l /= 10;
793 if(flags->Precision <= 0) {
794 flags->Precision = 0;
795 break;
797 if(l)
798 break;
802 len = flags->Precision;
803 if(flags->Precision || flags->Alternate) len++;
804 if(flags->Format=='f' || flags->Format=='F') {
805 len += (radix_pos > 0 ? radix_pos : 1);
806 } else if(flags->Format=='e' || flags->Format=='E') {
807 radix_pos--;
808 if(!trim_tail || radix_pos) {
809 len += 3; /* strlen("1e+") */
810 if(three_digit_exp || radix_pos<-99 || radix_pos>99) len += 3;
811 else len += 2;
812 } else {
813 len++;
817 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
818 if(r < 0) return r;
819 ret = r;
821 f.Format = 'd';
822 f.PadZero = TRUE;
823 if(flags->Format=='f' || flags->Format=='F') {
824 if(radix_pos <= 0) {
825 buf[0] = '0';
826 r = pf_puts(puts_ctx, 1, buf);
827 if(r < 0) return r;
828 ret += r;
831 limb_len = LIMB_DIGITS;
832 for(i=b->e-1; radix_pos>0 && i>=b->b; i--) {
833 limb_len = (i == b->e-1 ? first_limb_len : LIMB_DIGITS);
834 l = b->data[bnum_idx(b, i)];
835 if(limb_len > radix_pos) {
836 f.Precision = radix_pos;
837 l /= p10s[limb_len - radix_pos];
838 limb_len = limb_len - radix_pos;
839 } else {
840 f.Precision = limb_len;
841 limb_len = LIMB_DIGITS;
843 radix_pos -= f.Precision;
844 FUNC_NAME(pf_integer_conv)(buf, &f, l);
846 r = pf_puts(puts_ctx, f.Precision, buf);
847 if(r < 0) return r;
848 ret += r;
851 buf[0] = '0';
852 for(; radix_pos>0; radix_pos--) {
853 r = pf_puts(puts_ctx, 1, buf);
854 if(r < 0) return r;
855 ret += r;
858 if(flags->Precision || flags->Alternate) {
859 buf[0] = *(locale ? locale->locinfo : get_locinfo())->lconv->decimal_point;
860 r = pf_puts(puts_ctx, 1, buf);
861 if(r < 0) return r;
862 ret += r;
865 prec = flags->Precision;
866 buf[0] = '0';
867 for(; prec>0 && radix_pos+LIMB_DIGITS-first_limb_len<0; radix_pos++, prec--) {
868 r = pf_puts(puts_ctx, 1, buf);
869 if(r < 0) return r;
870 ret += r;
873 for(; prec>0 && i>=b->b; i--) {
874 l = b->data[bnum_idx(b, i)];
875 if(limb_len != LIMB_DIGITS)
876 l %= p10s[limb_len];
877 if(limb_len > prec) {
878 f.Precision = prec;
879 l /= p10s[limb_len - prec];
880 } else {
881 f.Precision = limb_len;
882 limb_len = LIMB_DIGITS;
884 prec -= f.Precision;
885 FUNC_NAME(pf_integer_conv)(buf, &f, l);
887 r = pf_puts(puts_ctx, f.Precision, buf);
888 if(r < 0) return r;
889 ret += r;
892 buf[0] = '0';
893 for(; prec>0; prec--) {
894 r = pf_puts(puts_ctx, 1, buf);
895 if(r < 0) return r;
896 ret += r;
898 } else {
899 l = b->data[bnum_idx(b, b->e - 1)];
900 l /= p10s[first_limb_len - 1];
902 buf[0] = '0' + l;
903 r = pf_puts(puts_ctx, 1, buf);
904 if(r < 0) return r;
905 ret += r;
907 if(flags->Precision || flags->Alternate) {
908 buf[0] = *(locale ? locale->locinfo : get_locinfo())->lconv->decimal_point;
909 r = pf_puts(puts_ctx, 1, buf);
910 if(r < 0) return r;
911 ret += r;
914 prec = flags->Precision;
915 limb_len = LIMB_DIGITS;
916 for(i=b->e-1; prec>0 && i>=b->b; i--) {
917 l = b->data[bnum_idx(b, i)];
918 if(i == b->e-1) {
919 limb_len = first_limb_len - 1;
920 l %= p10s[limb_len];
923 if(limb_len > prec) {
924 f.Precision = prec;
925 l /= p10s[limb_len - prec];
926 } else {
927 f.Precision = limb_len;
928 limb_len = LIMB_DIGITS;
930 prec -= f.Precision;
931 FUNC_NAME(pf_integer_conv)(buf, &f, l);
933 r = pf_puts(puts_ctx, f.Precision, buf);
934 if(r < 0) return r;
935 ret += r;
938 buf[0] = '0';
939 for(; prec>0; prec--) {
940 r = pf_puts(puts_ctx, 1, buf);
941 if(r < 0) return r;
942 ret += r;
945 if(!trim_tail || radix_pos) {
946 buf[0] = flags->Format;
947 buf[1] = radix_pos < 0 ? '-' : '+';
948 r = pf_puts(puts_ctx, 2, buf);
949 if(r < 0) return r;
950 ret += r;
952 f.Precision = three_digit_exp ? 3 : 2;
953 FUNC_NAME(pf_integer_conv)(buf, &f, radix_pos);
954 r = pf_puts(puts_ctx, f.Precision, buf);
955 if(r < 0) return r;
956 ret += r;
960 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
961 if(r < 0) return r;
962 ret += r;
963 return ret;
966 int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const APICHAR *fmt,
967 _locale_t locale, DWORD options,
968 args_clbk pf_args, void *args_ctx, __ms_va_list *valist)
970 const APICHAR *q, *p = fmt;
971 APICHAR buf[32];
972 int written = 0, pos, i;
973 pf_flags flags;
974 BOOL positional_params = options & MSVCRT_PRINTF_POSITIONAL_PARAMS;
975 BOOL invoke_invalid_param_handler = options & MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER;
976 #if _MSVCR_VER >= 140
977 BOOL legacy_wide = options & _CRT_INTERNAL_PRINTF_LEGACY_WIDE_SPECIFIERS;
978 BOOL legacy_msvcrt_compat = options & _CRT_INTERNAL_PRINTF_LEGACY_MSVCRT_COMPATIBILITY;
979 BOOL three_digit_exp = options & _CRT_INTERNAL_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS;
980 BOOL standard_rounding = options & _CRT_INTERNAL_PRINTF_STANDARD_ROUNDING;
981 #else
982 BOOL legacy_wide = TRUE, legacy_msvcrt_compat = TRUE;
983 BOOL three_digit_exp = _get_output_format() != _TWO_DIGIT_EXPONENT;
984 BOOL standard_rounding = FALSE;
985 #endif
987 if (!MSVCRT_CHECK_PMT(fmt != NULL))
988 return -1;
990 while(*p) {
991 /* output characters before '%' */
992 for(q=p; *q && *q!='%'; q++);
993 if(p != q) {
994 i = pf_puts(puts_ctx, q-p, p);
995 if(i < 0)
996 return i;
998 written += i;
999 p = q;
1000 continue;
1003 /* *p == '%' here */
1004 p++;
1006 /* output a single '%' character */
1007 if(*p == '%') {
1008 i = pf_puts(puts_ctx, 1, p++);
1009 if(i < 0)
1010 return i;
1012 written += i;
1013 continue;
1016 /* check parameter position */
1017 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &pos)) && *q=='$')
1018 p = q+1;
1019 else
1020 pos = -1;
1022 /* parse the flags */
1023 memset(&flags, 0, sizeof(flags));
1024 while(*p) {
1025 if(*p=='+' || *p==' ') {
1026 if(flags.Sign != '+')
1027 flags.Sign = *p;
1028 } else if(*p == '-')
1029 flags.LeftAlign = TRUE;
1030 else if(*p == '0')
1031 flags.PadZero = TRUE;
1032 else if(*p == '#')
1033 flags.Alternate = TRUE;
1034 else
1035 break;
1037 p++;
1040 /* parse the width */
1041 if(*p == '*') {
1042 p++;
1043 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
1044 p = q+1;
1045 else
1046 i = -1;
1048 flags.FieldLength = pf_args(args_ctx, i, VT_INT, valist).get_int;
1049 if(flags.FieldLength < 0) {
1050 flags.LeftAlign = TRUE;
1051 flags.FieldLength = -flags.FieldLength;
1053 } else while (*p >= '0' && *p <= '9') {
1054 flags.FieldLength *= 10;
1055 flags.FieldLength += *p++ - '0';
1058 /* parse the precision */
1059 flags.Precision = -1;
1060 if(*p == '.') {
1061 flags.Precision = 0;
1062 p++;
1063 if(*p == '*') {
1064 p++;
1065 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
1066 p = q+1;
1067 else
1068 i = -1;
1070 flags.Precision = pf_args(args_ctx, i, VT_INT, valist).get_int;
1071 } else while (*p >= '0' && *p <= '9') {
1072 flags.Precision *= 10;
1073 flags.Precision += *p++ - '0';
1077 /* parse argument size modifier */
1078 while(*p) {
1079 if(*p=='l' && *(p+1)=='l') {
1080 flags.IntegerDouble = TRUE;
1081 p++;
1082 } else if(*p=='l') {
1083 flags.IntegerLength = LEN_LONG;
1084 } else if(*p == 'h') {
1085 flags.IntegerLength = LEN_SHORT;
1086 } else if(*p == 'I') {
1087 if(*(p+1)=='6' && *(p+2)=='4') {
1088 flags.IntegerDouble = TRUE;
1089 p += 2;
1090 } else if(*(p+1)=='3' && *(p+2)=='2')
1091 p += 2;
1092 else if(p[1] && strchr("diouxX", p[1]))
1093 flags.IntegerNative = TRUE;
1094 else
1095 break;
1096 } else if(*p == 'w')
1097 flags.WideString = TRUE;
1098 #if _MSVCR_VER == 0 || _MSVCR_VER >= 140
1099 else if((*p == 'z' || *p == 't') && p[1] && strchr("diouxX", p[1]))
1100 flags.IntegerNative = TRUE;
1101 else if(*p == 'j')
1102 flags.IntegerDouble = TRUE;
1103 #endif
1104 #if _MSVCR_VER >= 140
1105 else if(*p == 'T')
1106 flags.NaturalString = TRUE;
1107 #endif
1108 else if(*p != 'L' && ((*p != 'F' && *p != 'N') || !legacy_msvcrt_compat))
1109 break;
1110 p++;
1113 flags.Format = *p;
1115 if(flags.Format == 's' || flags.Format == 'S') {
1116 i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx,
1117 pf_args(args_ctx, pos, VT_PTR, valist).get_ptr,
1118 -1, &flags, locale, legacy_wide);
1119 } else if(flags.Format == 'c' || flags.Format == 'C') {
1120 int ch = pf_args(args_ctx, pos, VT_INT, valist).get_int;
1122 i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx, &ch, 1, &flags, locale, legacy_wide);
1123 if(i < 0) i = 0; /* ignore conversion error */
1124 } else if(flags.Format == 'p') {
1125 flags.Format = 'X';
1126 flags.PadZero = TRUE;
1127 i = flags.Precision;
1128 flags.Precision = 2*sizeof(void*);
1129 FUNC_NAME(pf_integer_conv)(buf, &flags,
1130 (ULONG_PTR)pf_args(args_ctx, pos, VT_PTR, valist).get_ptr);
1131 flags.PadZero = FALSE;
1132 flags.Precision = i;
1134 #ifdef PRINTF_WIDE
1135 i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, buf, -1, &flags, locale);
1136 #else
1137 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, buf, -1, &flags, locale);
1138 #endif
1139 } else if(flags.Format == 'n') {
1140 int *used;
1142 if(!n_format_enabled) {
1143 MSVCRT_INVALID_PMT("\'n\' format specifier disabled", EINVAL);
1144 return -1;
1147 used = pf_args(args_ctx, pos, VT_PTR, valist).get_ptr;
1148 *used = written;
1149 i = 0;
1150 } else if(flags.Format && strchr("diouxX", flags.Format)) {
1151 APICHAR *tmp = buf;
1152 int max_len;
1154 /* 0 padding is added after '0x' if Alternate flag is in use */
1155 if((flags.Format=='x' || flags.Format=='X') && flags.PadZero && flags.Alternate
1156 && !flags.LeftAlign && flags.Precision<flags.FieldLength-2)
1157 flags.Precision = flags.FieldLength - 2;
1159 max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10;
1160 if(max_len > ARRAY_SIZE(buf))
1161 tmp = HeapAlloc(GetProcessHeap(), 0, max_len);
1162 if(!tmp)
1163 return -1;
1165 if(flags.IntegerDouble || (flags.IntegerNative && sizeof(void*) == 8))
1166 FUNC_NAME(pf_integer_conv)(tmp, &flags, pf_args(args_ctx, pos,
1167 VT_I8, valist).get_longlong);
1168 else if(flags.Format=='d' || flags.Format=='i')
1169 FUNC_NAME(pf_integer_conv)(tmp, &flags,
1170 flags.IntegerLength != LEN_SHORT ?
1171 pf_args(args_ctx, pos, VT_INT, valist).get_int :
1172 (short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
1173 else
1174 FUNC_NAME(pf_integer_conv)(tmp, &flags,
1175 flags.IntegerLength != LEN_SHORT ?
1176 (unsigned)pf_args(args_ctx, pos, VT_INT, valist).get_int :
1177 (unsigned short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
1179 #ifdef PRINTF_WIDE
1180 i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, tmp, -1, &flags, locale);
1181 #else
1182 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, tmp, -1, &flags, locale);
1183 #endif
1184 if(tmp != buf)
1185 HeapFree(GetProcessHeap(), 0, tmp);
1186 } else if(flags.Format && strchr("aAeEfFgG", flags.Format)) {
1187 double val = pf_args(args_ctx, pos, VT_R8, valist).get_double;
1189 if(signbit(val)) {
1190 flags.Sign = '-';
1191 val = -val;
1194 if(isinf(val) || isnan(val))
1195 i = FUNC_NAME(pf_output_special_fp)(pf_puts, puts_ctx, val, &flags,
1196 locale, legacy_msvcrt_compat, three_digit_exp);
1197 else if(flags.Format=='a' || flags.Format=='A')
1198 i = FUNC_NAME(pf_output_hex_fp)(pf_puts, puts_ctx, val, &flags,
1199 locale, standard_rounding);
1200 else
1201 i = FUNC_NAME(pf_output_fp)(pf_puts, puts_ctx, val, &flags,
1202 locale, three_digit_exp, standard_rounding);
1203 } else {
1204 if(invoke_invalid_param_handler) {
1205 _invalid_parameter(NULL, NULL, NULL, 0, 0);
1206 *_errno() = EINVAL;
1207 return -1;
1210 continue;
1213 if(i < 0)
1214 return i;
1215 written += i;
1216 p++;
1219 return written;
1222 #ifndef PRINTF_WIDE
1223 enum types_clbk_flags {
1224 TYPE_CLBK_VA_LIST = 1,
1225 TYPE_CLBK_POSITIONAL = 2,
1226 TYPE_CLBK_ERROR_POS = 4,
1227 TYPE_CLBK_ERROR_TYPE = 8
1230 /* This functions stores types of arguments. It uses args[0] internally */
1231 static printf_arg arg_clbk_type(void *ctx, int pos, int type, __ms_va_list *valist)
1233 static const printf_arg ret;
1234 printf_arg *args = ctx;
1236 if(pos == -1) {
1237 args[0].get_int |= TYPE_CLBK_VA_LIST;
1238 return ret;
1239 } else
1240 args[0].get_int |= TYPE_CLBK_POSITIONAL;
1242 if(pos<1 || pos>_ARGMAX)
1243 args[0].get_int |= TYPE_CLBK_ERROR_POS;
1244 else if(args[pos].get_int && args[pos].get_int!=type)
1245 args[0].get_int |= TYPE_CLBK_ERROR_TYPE;
1246 else
1247 args[pos].get_int = type;
1249 return ret;
1251 #endif
1253 int FUNC_NAME(create_positional_ctx)(void *args_ctx, const APICHAR *format, __ms_va_list valist)
1255 struct FUNC_NAME(_str_ctx) puts_ctx = {INT_MAX, NULL};
1256 printf_arg *args = args_ctx;
1257 int i, j;
1259 i = FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk_str), &puts_ctx, format, NULL,
1260 MSVCRT_PRINTF_POSITIONAL_PARAMS, arg_clbk_type, args_ctx, NULL);
1261 if(i < 0)
1262 return i;
1264 if(args[0].get_int==0 || args[0].get_int==TYPE_CLBK_VA_LIST)
1265 return 0;
1266 if(args[0].get_int != TYPE_CLBK_POSITIONAL)
1267 return -1;
1269 for(i=_ARGMAX; i>0; i--)
1270 if(args[i].get_int)
1271 break;
1273 for(j=1; j<=i; j++) {
1274 switch(args[j].get_int) {
1275 case VT_I8:
1276 args[j].get_longlong = va_arg(valist, LONGLONG);
1277 break;
1278 case VT_INT:
1279 args[j].get_int = va_arg(valist, int);
1280 break;
1281 case VT_R8:
1282 args[j].get_double = va_arg(valist, double);
1283 break;
1284 case VT_PTR:
1285 args[j].get_ptr = va_arg(valist, void*);
1286 break;
1287 default:
1288 return -1;
1292 return j;
1295 #undef APICHAR
1296 #undef CONVCHAR
1297 #undef FUNC_NAME