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
20 #define APICHAR MSVCRT_wchar_t
22 #define FUNC_NAME(func) func ## _w
25 #define CONVCHAR MSVCRT_wchar_t
26 #define FUNC_NAME(func) func ## _a
29 typedef struct FUNC_NAME(pf_flags_t)
31 APICHAR Sign, LeftAlign, Alternate, PadZero;
32 int FieldLength, Precision;
33 APICHAR IntegerLength, IntegerDouble;
36 } FUNC_NAME(pf_flags);
38 struct FUNC_NAME(_str_ctx) {
43 static int FUNC_NAME(puts_clbk_str)(void *ctx, int len, const APICHAR *str)
45 struct FUNC_NAME(_str_ctx) *out = ctx;
51 memcpy(out->buf, str, out->len);
57 memcpy(out->buf, str, len*sizeof(APICHAR));
62 static inline const APICHAR* FUNC_NAME(pf_parse_int)(const APICHAR *fmt, int *val)
66 while(isdigit(*fmt)) {
74 /* pf_fill: takes care of signs, alignment, zero and field padding */
75 static inline int FUNC_NAME(pf_fill)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
76 int len, FUNC_NAME(pf_flags) *flags, BOOL left)
78 int i, r = 0, written;
80 if(flags->Sign && !(flags->Format=='d' || flags->Format=='i'))
83 if(left && flags->Sign) {
86 r = pf_puts(puts_ctx, 1, &flags->Sign);
90 if((!left && flags->LeftAlign) || (left && !flags->LeftAlign)) {
93 if(left && flags->PadZero)
98 for(i=0; i<flags->FieldLength-len && r>=0; i++) {
99 r = pf_puts(puts_ctx, 1, &ch);
105 if(r>=0 && left && flags->Sign && !flags->PadZero) {
106 r = pf_puts(puts_ctx, 1, &flags->Sign);
110 return r>=0 ? written : r;
113 static inline int FUNC_NAME(pf_output_wstr)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
114 const MSVCRT_wchar_t *str, int len, MSVCRT__locale_t locale)
117 return pf_puts(puts_ctx, len, str);
120 int len_a = WideCharToMultiByte(locale->locinfo->lc_codepage, 0, str, len, NULL, 0, NULL, NULL);
122 out = HeapAlloc(GetProcessHeap(), 0, len_a);
126 WideCharToMultiByte(locale->locinfo->lc_codepage, 0, str, len, out, len_a, NULL, NULL);
127 len = pf_puts(puts_ctx, len_a, out);
128 HeapFree(GetProcessHeap(), 0, out);
133 static inline int FUNC_NAME(pf_output_str)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
134 const char *str, int len, MSVCRT__locale_t locale)
138 int len_w = MultiByteToWideChar(locale->locinfo->lc_codepage, 0, str, len, NULL, 0);
140 out = HeapAlloc(GetProcessHeap(), 0, len_w*sizeof(WCHAR));
144 MultiByteToWideChar(locale->locinfo->lc_codepage, 0, str, len, out, len_w);
145 len = pf_puts(puts_ctx, len_w, out);
146 HeapFree(GetProcessHeap(), 0, out);
149 return pf_puts(puts_ctx, len, str);
153 static inline int FUNC_NAME(pf_output_format_wstr)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
154 const MSVCRT_wchar_t *str, int len, FUNC_NAME(pf_flags) *flags, MSVCRT__locale_t locale)
161 if(flags->Precision>=0 && flags->Precision<len)
162 len = flags->Precision;
164 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
167 r = FUNC_NAME(pf_output_wstr)(pf_puts, puts_ctx, str, len, locale);
171 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
175 return r>=0 ? ret : r;
178 static inline int FUNC_NAME(pf_output_format_str)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
179 const char *str, int len, FUNC_NAME(pf_flags) *flags, MSVCRT__locale_t locale)
186 if(flags->Precision>=0 && flags->Precision<len)
187 len = flags->Precision;
189 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
192 r = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, str, len, locale);
196 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
200 return r>=0 ? ret : r;
203 static inline int FUNC_NAME(pf_handle_string)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
204 const void *str, int len, FUNC_NAME(pf_flags) *flags, MSVCRT__locale_t locale)
207 static const MSVCRT_wchar_t nullW[] = {'(','n','u','l','l',')',0};
210 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, nullW, 6, flags, locale);
213 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, "(null)", 6, flags, locale);
216 if(flags->WideString || flags->IntegerLength=='l')
217 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, str, len, flags, locale);
218 if(flags->IntegerLength == 'h')
219 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, len, flags, locale);
221 if((flags->Format=='S' || flags->Format=='C') == (sizeof(APICHAR)==sizeof(MSVCRT_wchar_t)))
222 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, len, flags, locale);
224 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, str, len, flags, locale);
227 static inline void FUNC_NAME(pf_rebuild_format_string)(char *p, FUNC_NAME(pf_flags) *flags)
233 *p++ = flags->LeftAlign;
235 *p++ = flags->Alternate;
237 *p++ = flags->PadZero;
238 if(flags->FieldLength) {
239 sprintf(p, "%d", flags->FieldLength);
242 if(flags->Precision >= 0) {
243 sprintf(p, ".%d", flags->Precision);
246 *p++ = flags->Format;
250 /* pf_integer_conv: prints x to buf, including alternate formats and
251 additional precision digits, but not field characters or the sign */
252 static inline void FUNC_NAME(pf_integer_conv)(char *buf, int buf_len,
253 FUNC_NAME(pf_flags) *flags, LONGLONG x)
259 char number[40], *tmp = number;
261 if(buf_len > sizeof number)
262 tmp = HeapAlloc(GetProcessHeap(), 0, buf_len);
264 if(flags->Format == 'o')
266 else if(flags->Format=='x' || flags->Format=='X')
271 if(flags->Format == 'X')
272 digits = "0123456789ABCDEFX";
274 digits = "0123456789abcdefx";
276 if(x<0 && (flags->Format=='d' || flags->Format=='i')) {
282 if(x==0 && flags->Precision)
286 j = (ULONGLONG)x%base;
287 x = (ULONGLONG)x/base;
288 tmp[i++] = digits[j];
291 k = flags->Precision-i;
294 if(flags->Alternate) {
296 tmp[i++] = digits[16];
298 } else if(base==8 && tmp[i-1]!='0')
307 /* Adjust precision so pf_fill won't truncate the number later */
308 flags->Precision = strlen(buf);
311 HeapFree(GetProcessHeap(), 0, tmp);
314 static inline void FUNC_NAME(pf_fixup_exponent)(char *buf)
318 while(tmp[0] && toupper(tmp[0])!='E')
321 if(tmp[0] && (tmp[1]=='+' || tmp[1]=='-') &&
322 isdigit(tmp[2]) && isdigit(tmp[3])) {
326 return; /* Exponent already 3 digits */
337 memmove(buf, buf + 1, (tmp - buf) + 3);
342 int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const APICHAR *fmt,
343 MSVCRT__locale_t locale, BOOL positional_params, BOOL invoke_invalid_param_handler,
344 args_clbk pf_args, void *args_ctx, __ms_va_list valist)
346 const APICHAR *q, *p = fmt;
347 int written = 0, pos, i;
348 FUNC_NAME(pf_flags) flags;
351 TRACE("Format is: %s\n", FUNC_NAME(debugstr)(fmt));
354 locale = get_locale();
357 /* output characters before '%' */
358 for(q=p; *q && *q!='%'; q++);
360 i = pf_puts(puts_ctx, q-p, p);
372 /* output a single '%' character */
374 i = pf_puts(puts_ctx, 1, p++);
382 /* check parameter position */
383 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &pos)) && *q=='$')
388 /* parse the flags */
389 memset(&flags, 0, sizeof(flags));
391 if(*p=='+' || *p==' ') {
392 if(flags.Sign != '+')
395 flags.LeftAlign = *p;
399 flags.Alternate = *p;
409 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
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;
431 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
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 */
445 if(*p=='l' && *(p+1)=='l') {
446 flags.IntegerDouble++;
448 } else if(*p=='h' || *p=='l' || *p=='L') {
449 flags.IntegerLength = *p;
451 } else if(*p == 'I') {
452 if(*(p+1)=='6' && *(p+2)=='4') {
453 flags.IntegerDouble++;
455 } else if(*(p+1)=='3' && *(p+2)=='2')
457 else if(isdigit(*(p+1)) || !*(p+1))
462 flags.WideString = *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,
475 } else if(flags.Format == 'c' || flags.Format == 'C') {
476 int ch = pf_args(args_ctx, pos, VT_INT, &valist).get_int;
479 FIXME("multibyte characters printing not supported\n");
481 i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx, &ch, 1, &flags, locale);
482 } else if(flags.Format == 'p') {
483 void *ptr = pf_args(args_ctx, pos, VT_PTR, &valist).get_ptr;
487 sprintf(buf, "0X%0*lX", 2*(int)sizeof(ptr), (ULONG_PTR)ptr);
489 sprintf(buf, "%0*lX", 2*(int)sizeof(ptr), (ULONG_PTR)ptr);
491 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, buf, -1, &flags, locale);
492 } else if(flags.Format == 'n') {
493 int *used = pf_args(args_ctx, pos, VT_PTR, &valist).get_ptr;
496 } else if(flags.IntegerDouble && flags.Format && strchr("diouxX", flags.Format)) {
498 int max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10;
500 if(max_len > sizeof(buf))
501 tmp = HeapAlloc(GetProcessHeap(), 0, max_len);
505 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags,
506 pf_args(args_ctx, pos, VT_I8, &valist).get_longlong);
508 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, tmp, -1, &flags, locale);
510 HeapFree(GetProcessHeap(), 0, tmp);
511 } else if(flags.Format && strchr("acCdeEfgGinouxX", flags.Format)) {
512 char fmt[20], *tmp = buf, *decimal_point;
513 int max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10;
515 if(max_len > sizeof(buf))
516 tmp = HeapAlloc(GetProcessHeap(), 0, max_len);
520 FUNC_NAME(pf_rebuild_format_string)(fmt, &flags);
522 if(flags.Format && strchr("aeEfgG", flags.Format)) {
523 sprintf(tmp, fmt, pf_args(args_ctx, pos, VT_R8, &valist).get_double);
524 if(toupper(flags.Format)=='E' || toupper(flags.Format)=='G')
525 FUNC_NAME(pf_fixup_exponent)(tmp);
528 sprintf(tmp, fmt, pf_args(args_ctx, pos, VT_INT, &valist).get_int);
530 decimal_point = strchr(tmp, '.');
532 *decimal_point = *locale->locinfo->lconv->decimal_point;
534 i = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, tmp, strlen(tmp), locale);
536 HeapFree(GetProcessHeap(), 0, tmp);
538 if(invoke_invalid_param_handler) {
539 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
540 *MSVCRT__errno() = MSVCRT_EINVAL;
557 enum types_clbk_flags {
558 TYPE_CLBK_VA_LIST = 1,
559 TYPE_CLBK_POSITIONAL = 2,
560 TYPE_CLBK_ERROR_POS = 4,
561 TYPE_CLBK_ERROR_TYPE = 8
564 /* This functions stores types of arguments. It uses args[0] internally */
565 static printf_arg arg_clbk_type(void *ctx, int pos, int type, __ms_va_list *valist)
567 static const printf_arg ret;
568 printf_arg *args = ctx;
571 args[0].get_int |= TYPE_CLBK_VA_LIST;
574 args[0].get_int |= TYPE_CLBK_POSITIONAL;
576 if(pos<1 || pos>MSVCRT__ARGMAX)
577 args[0].get_int |= TYPE_CLBK_ERROR_POS;
578 else if(args[pos].get_int && args[pos].get_int!=type)
579 args[0].get_int |= TYPE_CLBK_ERROR_TYPE;
581 args[pos].get_int = type;
587 static int FUNC_NAME(create_positional_ctx)(void *args_ctx, const APICHAR *format, __ms_va_list valist)
589 struct FUNC_NAME(_str_ctx) puts_ctx = {INT_MAX, NULL};
590 printf_arg *args = args_ctx;
593 i = FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk_str), &puts_ctx, format, NULL, TRUE, FALSE,
594 arg_clbk_type, args_ctx, NULL);
598 if(args[0].get_int==0 || args[0].get_int==TYPE_CLBK_VA_LIST)
600 if(args[0].get_int != TYPE_CLBK_POSITIONAL)
603 for(i=MSVCRT__ARGMAX; i>0; i--)
607 for(j=1; j<=i; j++) {
608 switch(args[j].get_int) {
610 args[j].get_longlong = va_arg(valist, LONGLONG);
613 args[j].get_int = va_arg(valist, int);
616 args[j].get_double = va_arg(valist, double);
619 args[j].get_ptr = va_arg(valist, void*);