msvcrt/tests: Add a trailing '\n' to a win_skip().
[wine.git] / dlls / msvcrt / printf.h
blob0500360e97720650547d123e83f6789611397c9c
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 #ifdef PRINTF_WIDE
20 #define APICHAR MSVCRT_wchar_t
21 #define CONVCHAR char
22 #define FUNC_NAME(func) func ## _w
23 #else
24 #define APICHAR char
25 #define CONVCHAR MSVCRT_wchar_t
26 #define FUNC_NAME(func) func ## _a
27 #endif
29 typedef struct FUNC_NAME(pf_flags_t)
31 APICHAR Sign, LeftAlign, Alternate, PadZero;
32 int FieldLength, Precision;
33 APICHAR IntegerLength, IntegerDouble;
34 APICHAR WideString;
35 APICHAR Format;
36 } FUNC_NAME(pf_flags);
38 struct FUNC_NAME(_str_ctx) {
39 MSVCRT_size_t len;
40 APICHAR *buf;
43 static int FUNC_NAME(puts_clbk_str)(void *ctx, int len, const APICHAR *str)
45 struct FUNC_NAME(_str_ctx) *out = ctx;
47 if(!out->buf)
48 return len;
50 if(out->len < len) {
51 memcpy(out->buf, str, out->len);
52 out->buf += out->len;
53 out->len = 0;
54 return -1;
57 memcpy(out->buf, str, len*sizeof(APICHAR));
58 out->buf += len;
59 out->len -= len;
60 return len;
63 static inline const APICHAR* FUNC_NAME(pf_parse_int)(const APICHAR *fmt, int *val)
65 *val = 0;
67 while(isdigit(*fmt)) {
68 *val *= 10;
69 *val += *fmt++ - '0';
72 return fmt;
75 /* pf_fill: takes care of signs, alignment, zero and field padding */
76 static inline int FUNC_NAME(pf_fill)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
77 int len, FUNC_NAME(pf_flags) *flags, BOOL left)
79 int i, r = 0, written;
81 if(flags->Sign && !(flags->Format=='d' || flags->Format=='i'))
82 flags->Sign = 0;
84 if(left && flags->Sign) {
85 flags->FieldLength--;
86 if(flags->PadZero)
87 r = pf_puts(puts_ctx, 1, &flags->Sign);
89 written = r;
91 if((!left && flags->LeftAlign) || (left && !flags->LeftAlign)) {
92 APICHAR ch;
94 if(left && flags->PadZero)
95 ch = '0';
96 else
97 ch = ' ';
99 for(i=0; i<flags->FieldLength-len && r>=0; i++) {
100 r = pf_puts(puts_ctx, 1, &ch);
101 written += r;
106 if(r>=0 && left && flags->Sign && !flags->PadZero) {
107 r = pf_puts(puts_ctx, 1, &flags->Sign);
108 written += r;
111 return r>=0 ? written : r;
114 static inline int FUNC_NAME(pf_output_wstr)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
115 const MSVCRT_wchar_t *str, int len, MSVCRT_pthreadlocinfo locinfo)
117 #ifdef PRINTF_WIDE
118 return pf_puts(puts_ctx, len, str);
119 #else
120 LPSTR out;
121 int len_a = WideCharToMultiByte(locinfo->lc_codepage, 0, str, len, NULL, 0, NULL, NULL);
123 out = HeapAlloc(GetProcessHeap(), 0, len_a);
124 if(!out)
125 return -1;
127 WideCharToMultiByte(locinfo->lc_codepage, 0, str, len, out, len_a, NULL, NULL);
128 len = pf_puts(puts_ctx, len_a, out);
129 HeapFree(GetProcessHeap(), 0, out);
130 return len;
131 #endif
134 static inline int FUNC_NAME(pf_output_str)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
135 const char *str, int len, MSVCRT_pthreadlocinfo locinfo)
137 #ifdef PRINTF_WIDE
138 LPWSTR out;
139 int len_w = MultiByteToWideChar(locinfo->lc_codepage, 0, str, len, NULL, 0);
141 out = HeapAlloc(GetProcessHeap(), 0, len_w*sizeof(WCHAR));
142 if(!out)
143 return -1;
145 MultiByteToWideChar(locinfo->lc_codepage, 0, str, len, out, len_w);
146 len = pf_puts(puts_ctx, len_w, out);
147 HeapFree(GetProcessHeap(), 0, out);
148 return len;
149 #else
150 return pf_puts(puts_ctx, len, str);
151 #endif
154 static inline int FUNC_NAME(pf_output_format_wstr)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
155 const MSVCRT_wchar_t *str, int len, FUNC_NAME(pf_flags) *flags, MSVCRT_pthreadlocinfo locinfo)
157 int r, ret;
159 if(len < 0)
160 len = strlenW(str);
162 if(flags->Precision>=0 && flags->Precision<len)
163 len = flags->Precision;
165 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
166 ret = r;
167 if(r >= 0) {
168 r = FUNC_NAME(pf_output_wstr)(pf_puts, puts_ctx, str, len, locinfo);
169 ret += r;
171 if(r >= 0) {
172 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
173 ret += r;
176 return r>=0 ? ret : r;
179 static inline int FUNC_NAME(pf_output_format_str)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
180 const char *str, int len, FUNC_NAME(pf_flags) *flags, MSVCRT_pthreadlocinfo locinfo)
182 int r, ret;
184 if(len < 0)
185 len = strlen(str);
187 if(flags->Precision>=0 && flags->Precision<len)
188 len = flags->Precision;
190 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
191 ret = r;
192 if(r >= 0) {
193 r = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, str, len, locinfo);
194 ret += r;
196 if(r >= 0) {
197 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
198 ret += r;
201 return r>=0 ? ret : r;
204 static inline int FUNC_NAME(pf_handle_string)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
205 const void *str, int len, FUNC_NAME(pf_flags) *flags, MSVCRT_pthreadlocinfo locinfo)
207 #ifdef PRINTF_WIDE
208 static const MSVCRT_wchar_t nullW[] = {'(','n','u','l','l',')',0};
210 if(!str)
211 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, nullW, 6, flags, locinfo);
212 #else
213 if(!str)
214 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, "(null)", 6, flags, locinfo);
215 #endif
217 if(flags->WideString || flags->IntegerLength=='l')
218 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, str, len, flags, locinfo);
219 if(flags->IntegerLength == 'h')
220 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, len, flags, locinfo);
222 if((flags->Format=='S' || flags->Format=='C') == (sizeof(APICHAR)==sizeof(MSVCRT_wchar_t)))
223 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, len, flags, locinfo);
224 else
225 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, str, len, flags, locinfo);
228 static inline void FUNC_NAME(pf_rebuild_format_string)(char *p, FUNC_NAME(pf_flags) *flags)
230 *p++ = '%';
231 if(flags->Sign)
232 *p++ = flags->Sign;
233 if(flags->LeftAlign)
234 *p++ = flags->LeftAlign;
235 if(flags->Alternate)
236 *p++ = flags->Alternate;
237 if(flags->PadZero)
238 *p++ = flags->PadZero;
239 if(flags->FieldLength) {
240 sprintf(p, "%d", flags->FieldLength);
241 p += strlen(p);
243 if(flags->Precision >= 0) {
244 sprintf(p, ".%d", flags->Precision);
245 p += strlen(p);
247 *p++ = flags->Format;
248 *p++ = 0;
251 /* pf_integer_conv: prints x to buf, including alternate formats and
252 additional precision digits, but not field characters or the sign */
253 static inline void FUNC_NAME(pf_integer_conv)(APICHAR *buf, int buf_len,
254 FUNC_NAME(pf_flags) *flags, LONGLONG x)
256 unsigned int base;
257 const char *digits;
258 int i, j, k;
260 if(flags->Format == 'o')
261 base = 8;
262 else if(flags->Format=='x' || flags->Format=='X')
263 base = 16;
264 else
265 base = 10;
267 if(flags->Format == 'X')
268 digits = "0123456789ABCDEFX";
269 else
270 digits = "0123456789abcdefx";
272 if(x<0 && (flags->Format=='d' || flags->Format=='i')) {
273 x = -x;
274 flags->Sign = '-';
277 i = 0;
278 if(x==0 && flags->Precision)
279 buf[i++] = '0';
280 else {
281 while(x != 0) {
282 j = (ULONGLONG)x%base;
283 x = (ULONGLONG)x/base;
284 buf[i++] = digits[j];
287 k = flags->Precision-i;
288 while(k-- > 0)
289 buf[i++] = '0';
290 if(flags->Alternate) {
291 if(base == 16) {
292 buf[i++] = digits[16];
293 buf[i++] = '0';
294 } else if(base==8 && buf[i-1]!='0')
295 buf[i++] = '0';
298 /* Adjust precision so pf_fill won't truncate the number later */
299 flags->Precision = i;
301 buf[i] = '\0';
302 j = 0;
303 while(--i > j) {
304 APICHAR tmp = buf[j];
305 buf[j] = buf[i];
306 buf[i] = tmp;
307 j++;
311 static inline void FUNC_NAME(pf_fixup_exponent)(char *buf)
313 char* tmp = buf;
315 while(tmp[0] && toupper(tmp[0])!='E')
316 tmp++;
318 if(tmp[0] && (tmp[1]=='+' || tmp[1]=='-') &&
319 isdigit(tmp[2]) && isdigit(tmp[3])) {
320 char final;
322 if (isdigit(tmp[4]))
323 return; /* Exponent already 3 digits */
325 tmp += 2;
326 final = tmp[2];
327 tmp[2] = tmp[1];
328 tmp[1] = tmp[0];
329 tmp[0] = '0';
331 if(final == '\0') {
332 tmp[3] = '\0';
333 if(buf[0] == ' ')
334 memmove(buf, buf + 1, (tmp - buf) + 3);
339 int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const APICHAR *fmt,
340 MSVCRT__locale_t locale, BOOL positional_params, BOOL invoke_invalid_param_handler,
341 args_clbk pf_args, void *args_ctx, __ms_va_list *valist)
343 MSVCRT_pthreadlocinfo locinfo;
344 const APICHAR *q, *p = fmt;
345 APICHAR buf[32];
346 int written = 0, pos, i;
347 FUNC_NAME(pf_flags) flags;
349 TRACE("Format is: %s\n", FUNC_NAME(debugstr)(fmt));
351 if(!locale)
352 locinfo = get_locinfo();
353 else
354 locinfo = locale->locinfo;
356 while(*p) {
357 /* output characters before '%' */
358 for(q=p; *q && *q!='%'; q++);
359 if(p != q) {
360 i = pf_puts(puts_ctx, q-p, p);
361 if(i < 0)
362 return i;
364 written += i;
365 p = q;
366 continue;
369 /* *p == '%' here */
370 p++;
372 /* output a single '%' character */
373 if(*p == '%') {
374 i = pf_puts(puts_ctx, 1, p++);
375 if(i < 0)
376 return i;
378 written += i;
379 continue;
382 /* check parameter position */
383 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &pos)) && *q=='$')
384 p = q+1;
385 else
386 pos = -1;
388 /* parse the flags */
389 memset(&flags, 0, sizeof(flags));
390 while(*p) {
391 if(*p=='+' || *p==' ') {
392 if(flags.Sign != '+')
393 flags.Sign = *p;
394 } else if(*p == '-')
395 flags.LeftAlign = *p;
396 else if(*p == '0')
397 flags.PadZero = *p;
398 else if(*p == '#')
399 flags.Alternate = *p;
400 else
401 break;
403 p++;
406 /* parse the widh */
407 if(*p == '*') {
408 p++;
409 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
410 p = q+1;
411 else
412 i = -1;
414 flags.FieldLength = pf_args(args_ctx, i, VT_INT, valist).get_int;
415 if(flags.FieldLength < 0) {
416 flags.LeftAlign = '-';
417 flags.FieldLength = -flags.FieldLength;
419 } else while(isdigit(*p)) {
420 flags.FieldLength *= 10;
421 flags.FieldLength += *p++ - '0';
424 /* parse the precision */
425 flags.Precision = -1;
426 if(*p == '.') {
427 flags.Precision = 0;
428 p++;
429 if(*p == '*') {
430 p++;
431 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
432 p = q+1;
433 else
434 i = -1;
436 flags.Precision = pf_args(args_ctx, i, VT_INT, valist).get_int;
437 } else while(isdigit(*p)) {
438 flags.Precision *= 10;
439 flags.Precision += *p++ - '0';
443 /* parse argument size modifier */
444 while(*p) {
445 if(*p=='l' && *(p+1)=='l') {
446 flags.IntegerDouble++;
447 p += 2;
448 } else if(*p=='h' || *p=='l' || *p=='L') {
449 flags.IntegerLength = *p;
450 p++;
451 } else if(*p == 'I') {
452 if(*(p+1)=='6' && *(p+2)=='4') {
453 flags.IntegerDouble++;
454 p += 3;
455 } else if(*(p+1)=='3' && *(p+2)=='2')
456 p += 3;
457 else if(isdigit(*(p+1)) || !*(p+1))
458 break;
459 else
460 p++;
461 } else if(*p == 'w')
462 flags.WideString = *p++;
463 else if(*p == 'F')
464 p++; /* ignore */
465 else
466 break;
469 flags.Format = *p;
471 if(flags.Format == 's' || flags.Format == 'S') {
472 i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx,
473 pf_args(args_ctx, pos, VT_PTR, valist).get_ptr,
474 -1, &flags, locinfo);
475 } else if(flags.Format == 'c' || flags.Format == 'C') {
476 int ch = pf_args(args_ctx, pos, VT_INT, valist).get_int;
478 if((ch&0xff) != ch)
479 FIXME("multibyte characters printing not supported\n");
481 i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx, &ch, 1, &flags, locinfo);
482 } else if(flags.Format == 'p') {
483 flags.Format = 'X';
484 flags.PadZero = '0';
485 i = flags.Precision;
486 flags.Precision = 2*sizeof(void*);
487 FUNC_NAME(pf_integer_conv)(buf, sizeof(buf)/sizeof(APICHAR), &flags,
488 (ULONG_PTR)pf_args(args_ctx, pos, VT_PTR, valist).get_ptr);
489 flags.PadZero = 0;
490 flags.Precision = i;
492 #ifdef PRINTF_WIDE
493 i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, buf, -1, &flags, locinfo);
494 #else
495 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, buf, -1, &flags, locinfo);
496 #endif
497 } else if(flags.Format == 'n') {
498 int *used;
500 if(!n_format_enabled) {
501 MSVCRT_INVALID_PMT("\'n\' format specifier disabled", MSVCRT_EINVAL);
502 return -1;
505 used = pf_args(args_ctx, pos, VT_PTR, valist).get_ptr;
506 *used = written;
507 i = 0;
508 } else if(flags.Format && strchr("diouxX", flags.Format)) {
509 APICHAR *tmp = buf;
510 int max_len;
512 /* 0 padding is added after '0x' if Alternate flag is in use */
513 if((flags.Format=='x' || flags.Format=='X') && flags.PadZero && flags.Alternate
514 && !flags.LeftAlign && flags.Precision<flags.FieldLength-2)
515 flags.Precision = flags.FieldLength - 2;
517 max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10;
518 if(max_len > sizeof(buf)/sizeof(APICHAR))
519 tmp = HeapAlloc(GetProcessHeap(), 0, max_len);
520 if(!tmp)
521 return -1;
523 if(flags.IntegerDouble)
524 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags, pf_args(args_ctx, pos,
525 VT_I8, valist).get_longlong);
526 else if(flags.Format=='d' || flags.Format=='i')
527 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags, flags.IntegerLength!='h' ?
528 pf_args(args_ctx, pos, VT_INT, valist).get_int :
529 (short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
530 else
531 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags, flags.IntegerLength!='h' ?
532 (unsigned)pf_args(args_ctx, pos, VT_INT, valist).get_int :
533 (unsigned short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
535 #ifdef PRINTF_WIDE
536 i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, tmp, -1, &flags, locinfo);
537 #else
538 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, tmp, -1, &flags, locinfo);
539 #endif
540 if(tmp != buf)
541 HeapFree(GetProcessHeap(), 0, tmp);
542 } else if(flags.Format && strchr("aeEfgG", flags.Format)) {
543 char float_fmt[20], buf_a[32], *tmp = buf_a, *decimal_point;
544 int max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10;
545 double val = pf_args(args_ctx, pos, VT_R8, valist).get_double;
547 if(flags.Format=='f') {
548 if(val>-10.0 && val<10.0)
549 i = 1;
550 else
551 i = 1 + log10(val<0 ? -val : val);
552 /* Default precision is 6, additional space for sign, separator and nullbyte is required */
553 i += (flags.Precision==-1 ? 6 : flags.Precision) + 3;
555 if(i > max_len)
556 max_len = i;
559 if(max_len > sizeof(buf_a))
560 tmp = HeapAlloc(GetProcessHeap(), 0, max_len);
561 if(!tmp)
562 return -1;
564 FUNC_NAME(pf_rebuild_format_string)(float_fmt, &flags);
566 sprintf(tmp, float_fmt, val);
567 if(toupper(flags.Format)=='E' || toupper(flags.Format)=='G')
568 FUNC_NAME(pf_fixup_exponent)(tmp);
570 decimal_point = strchr(tmp, '.');
571 if(decimal_point)
572 *decimal_point = *locinfo->lconv->decimal_point;
574 i = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, tmp, strlen(tmp), locinfo);
575 if(tmp != buf_a)
576 HeapFree(GetProcessHeap(), 0, tmp);
577 } else {
578 if(invoke_invalid_param_handler) {
579 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
580 *MSVCRT__errno() = MSVCRT_EINVAL;
581 return -1;
584 continue;
587 if(i < 0)
588 return i;
589 written += i;
590 p++;
593 return written;
596 #ifndef PRINTF_WIDE
597 enum types_clbk_flags {
598 TYPE_CLBK_VA_LIST = 1,
599 TYPE_CLBK_POSITIONAL = 2,
600 TYPE_CLBK_ERROR_POS = 4,
601 TYPE_CLBK_ERROR_TYPE = 8
604 /* This functions stores types of arguments. It uses args[0] internally */
605 static printf_arg arg_clbk_type(void *ctx, int pos, int type, __ms_va_list *valist)
607 static const printf_arg ret;
608 printf_arg *args = ctx;
610 if(pos == -1) {
611 args[0].get_int |= TYPE_CLBK_VA_LIST;
612 return ret;
613 } else
614 args[0].get_int |= TYPE_CLBK_POSITIONAL;
616 if(pos<1 || pos>MSVCRT__ARGMAX)
617 args[0].get_int |= TYPE_CLBK_ERROR_POS;
618 else if(args[pos].get_int && args[pos].get_int!=type)
619 args[0].get_int |= TYPE_CLBK_ERROR_TYPE;
620 else
621 args[pos].get_int = type;
623 return ret;
625 #endif
627 static int FUNC_NAME(create_positional_ctx)(void *args_ctx, const APICHAR *format, __ms_va_list valist)
629 struct FUNC_NAME(_str_ctx) puts_ctx = {INT_MAX, NULL};
630 printf_arg *args = args_ctx;
631 int i, j;
633 i = FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk_str), &puts_ctx, format, NULL, TRUE, FALSE,
634 arg_clbk_type, args_ctx, NULL);
635 if(i < 0)
636 return i;
638 if(args[0].get_int==0 || args[0].get_int==TYPE_CLBK_VA_LIST)
639 return 0;
640 if(args[0].get_int != TYPE_CLBK_POSITIONAL)
641 return -1;
643 for(i=MSVCRT__ARGMAX; i>0; i--)
644 if(args[i].get_int)
645 break;
647 for(j=1; j<=i; j++) {
648 switch(args[j].get_int) {
649 case VT_I8:
650 args[j].get_longlong = va_arg(valist, LONGLONG);
651 break;
652 case VT_INT:
653 args[j].get_int = va_arg(valist, int);
654 break;
655 case VT_R8:
656 args[j].get_double = va_arg(valist, double);
657 break;
658 case VT_PTR:
659 args[j].get_ptr = va_arg(valist, void*);
660 break;
661 default:
662 return -1;
666 return j;
669 #undef APICHAR
670 #undef CONVCHAR
671 #undef FUNC_NAME