dsound: Merge IUnknown into the main DirectSound object.
[wine/multimedia.git] / dlls / msvcrt / printf.h
blob79a03a96470368d45dc9a4a0a4095bec396881e9
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");
502 *MSVCRT__errno() = MSVCRT_EINVAL;
503 return -1;
506 used = pf_args(args_ctx, pos, VT_PTR, valist).get_ptr;
507 *used = written;
508 i = 0;
509 } else if(flags.Format && strchr("diouxX", flags.Format)) {
510 APICHAR *tmp = buf;
511 int max_len;
513 /* 0 padding is added after '0x' if Alternate flag is in use */
514 if((flags.Format=='x' || flags.Format=='X') && flags.PadZero && flags.Alternate
515 && !flags.LeftAlign && flags.Precision<flags.FieldLength-2)
516 flags.Precision = flags.FieldLength - 2;
518 max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10;
519 if(max_len > sizeof(buf)/sizeof(APICHAR))
520 tmp = HeapAlloc(GetProcessHeap(), 0, max_len);
521 if(!tmp)
522 return -1;
524 if(flags.IntegerDouble)
525 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags, pf_args(args_ctx, pos,
526 VT_I8, valist).get_longlong);
527 else if(flags.Format=='d' || flags.Format=='i')
528 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags, flags.IntegerLength!='h' ?
529 pf_args(args_ctx, pos, VT_INT, valist).get_int :
530 (short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
531 else
532 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags, flags.IntegerLength!='h' ?
533 (unsigned)pf_args(args_ctx, pos, VT_INT, valist).get_int :
534 (unsigned short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
536 #ifdef PRINTF_WIDE
537 i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, tmp, -1, &flags, locinfo);
538 #else
539 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, tmp, -1, &flags, locinfo);
540 #endif
541 if(tmp != buf)
542 HeapFree(GetProcessHeap(), 0, tmp);
543 } else if(flags.Format && strchr("aeEfgG", flags.Format)) {
544 char float_fmt[20], buf_a[32], *tmp = buf_a, *decimal_point;
545 int max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10;
546 double val = pf_args(args_ctx, pos, VT_R8, valist).get_double;
548 if(flags.Format=='f') {
549 if(val>-10.0 && val<10.0)
550 i = 1;
551 else
552 i = 1 + log10(val<0 ? -val : val);
553 /* Default precision is 6, additional space for sign, separator and nullbyte is required */
554 i += (flags.Precision==-1 ? 6 : flags.Precision) + 3;
556 if(i > max_len)
557 max_len = i;
560 if(max_len > sizeof(buf_a))
561 tmp = HeapAlloc(GetProcessHeap(), 0, max_len);
562 if(!tmp)
563 return -1;
565 FUNC_NAME(pf_rebuild_format_string)(float_fmt, &flags);
567 sprintf(tmp, float_fmt, val);
568 if(toupper(flags.Format)=='E' || toupper(flags.Format)=='G')
569 FUNC_NAME(pf_fixup_exponent)(tmp);
571 decimal_point = strchr(tmp, '.');
572 if(decimal_point)
573 *decimal_point = *locinfo->lconv->decimal_point;
575 i = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, tmp, strlen(tmp), locinfo);
576 if(tmp != buf_a)
577 HeapFree(GetProcessHeap(), 0, tmp);
578 } else {
579 if(invoke_invalid_param_handler) {
580 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
581 *MSVCRT__errno() = MSVCRT_EINVAL;
582 return -1;
585 continue;
588 if(i < 0)
589 return i;
590 written += i;
591 p++;
594 return written;
597 #ifndef PRINTF_WIDE
598 enum types_clbk_flags {
599 TYPE_CLBK_VA_LIST = 1,
600 TYPE_CLBK_POSITIONAL = 2,
601 TYPE_CLBK_ERROR_POS = 4,
602 TYPE_CLBK_ERROR_TYPE = 8
605 /* This functions stores types of arguments. It uses args[0] internally */
606 static printf_arg arg_clbk_type(void *ctx, int pos, int type, __ms_va_list *valist)
608 static const printf_arg ret;
609 printf_arg *args = ctx;
611 if(pos == -1) {
612 args[0].get_int |= TYPE_CLBK_VA_LIST;
613 return ret;
614 } else
615 args[0].get_int |= TYPE_CLBK_POSITIONAL;
617 if(pos<1 || pos>MSVCRT__ARGMAX)
618 args[0].get_int |= TYPE_CLBK_ERROR_POS;
619 else if(args[pos].get_int && args[pos].get_int!=type)
620 args[0].get_int |= TYPE_CLBK_ERROR_TYPE;
621 else
622 args[pos].get_int = type;
624 return ret;
626 #endif
628 static int FUNC_NAME(create_positional_ctx)(void *args_ctx, const APICHAR *format, __ms_va_list valist)
630 struct FUNC_NAME(_str_ctx) puts_ctx = {INT_MAX, NULL};
631 printf_arg *args = args_ctx;
632 int i, j;
634 i = FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk_str), &puts_ctx, format, NULL, TRUE, FALSE,
635 arg_clbk_type, args_ctx, NULL);
636 if(i < 0)
637 return i;
639 if(args[0].get_int==0 || args[0].get_int==TYPE_CLBK_VA_LIST)
640 return 0;
641 if(args[0].get_int != TYPE_CLBK_POSITIONAL)
642 return -1;
644 for(i=MSVCRT__ARGMAX; i>0; i--)
645 if(args[i].get_int)
646 break;
648 for(j=1; j<=i; j++) {
649 switch(args[j].get_int) {
650 case VT_I8:
651 args[j].get_longlong = va_arg(valist, LONGLONG);
652 break;
653 case VT_INT:
654 args[j].get_int = va_arg(valist, int);
655 break;
656 case VT_R8:
657 args[j].get_double = va_arg(valist, double);
658 break;
659 case VT_PTR:
660 args[j].get_ptr = va_arg(valist, void*);
661 break;
662 default:
663 return -1;
667 return j;
670 #undef APICHAR
671 #undef CONVCHAR
672 #undef FUNC_NAME