Bug fix: added temp structure to cope with debug traces during thread
[wine/hacks.git] / misc / wsprintf.c
blob9c4830519d30416a6423f720e1ae066fb9f293b4
1 /*
2 * wsprintf functions
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <stdarg.h>
8 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "winuser.h"
11 #include "ldt.h"
12 #include "stackframe.h"
13 #include "module.h"
14 #include "global.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(string)
20 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
21 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
22 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
23 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
24 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
25 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
26 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
28 typedef enum
30 WPR_UNKNOWN,
31 WPR_CHAR,
32 WPR_WCHAR,
33 WPR_STRING,
34 WPR_WSTRING,
35 WPR_SIGNED,
36 WPR_UNSIGNED,
37 WPR_HEXA
38 } WPRINTF_TYPE;
40 typedef struct
42 UINT flags;
43 UINT width;
44 UINT precision;
45 WPRINTF_TYPE type;
46 } WPRINTF_FORMAT;
48 typedef union {
49 WCHAR wchar_view;
50 CHAR char_view;
51 LPCSTR lpcstr_view;
52 LPCWSTR lpcwstr_view;
53 INT int_view;
54 } WPRINTF_DATA;
56 static const CHAR null_stringA[] = "(null)";
57 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
59 /***********************************************************************
60 * WPRINTF_ParseFormatA
62 * Parse a format specification. A format specification has the form:
64 * [-][#][0][width][.precision]type
66 * Return value is the length of the format specification in characters.
68 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
70 LPCSTR p = format;
72 res->flags = 0;
73 res->width = 0;
74 res->precision = 0;
75 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
76 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
77 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
78 while ((*p >= '0') && (*p <= '9')) /* width field */
80 res->width = res->width * 10 + *p - '0';
81 p++;
83 if (*p == '.') /* precision field */
85 p++;
86 while ((*p >= '0') && (*p <= '9'))
88 res->precision = res->precision * 10 + *p - '0';
89 p++;
92 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
93 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
94 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
95 switch(*p)
97 case 'c':
98 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
99 break;
100 case 'C':
101 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
102 break;
103 case 'd':
104 case 'i':
105 res->type = WPR_SIGNED;
106 break;
107 case 's':
108 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE))
109 ? WPR_WSTRING : WPR_STRING;
110 break;
111 case 'S':
112 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE))
113 ? WPR_STRING : WPR_WSTRING;
114 break;
115 case 'u':
116 res->type = WPR_UNSIGNED;
117 break;
118 case 'X':
119 res->flags |= WPRINTF_UPPER_HEX;
120 /* fall through */
121 case 'x':
122 res->type = WPR_HEXA;
123 break;
124 default: /* unknown format char */
125 res->type = WPR_UNKNOWN;
126 p--; /* print format as normal char */
127 break;
129 return (INT)(p - format) + 1;
133 /***********************************************************************
134 * WPRINTF_ParseFormatW
136 * Parse a format specification. A format specification has the form:
138 * [-][#][0][width][.precision]type
140 * Return value is the length of the format specification in characters.
142 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
144 LPCWSTR p = format;
146 res->flags = 0;
147 res->width = 0;
148 res->precision = 0;
149 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
150 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
151 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
152 while ((*p >= '0') && (*p <= '9')) /* width field */
154 res->width = res->width * 10 + *p - '0';
155 p++;
157 if (*p == '.') /* precision field */
159 p++;
160 while ((*p >= '0') && (*p <= '9'))
162 res->precision = res->precision * 10 + *p - '0';
163 p++;
166 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
167 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
168 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
169 switch((CHAR)*p)
171 case 'c':
172 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
173 break;
174 case 'C':
175 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
176 break;
177 case 'd':
178 case 'i':
179 res->type = WPR_SIGNED;
180 break;
181 case 's':
182 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
183 break;
184 case 'S':
185 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
186 break;
187 case 'u':
188 res->type = WPR_UNSIGNED;
189 break;
190 case 'X':
191 res->flags |= WPRINTF_UPPER_HEX;
192 /* fall through */
193 case 'x':
194 res->type = WPR_HEXA;
195 break;
196 default:
197 res->type = WPR_UNKNOWN;
198 p--; /* print format as normal char */
199 break;
201 return (INT)(p - format) + 1;
205 /***********************************************************************
206 * WPRINTF_GetLen
208 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
209 LPSTR number, UINT maxlen )
211 UINT len;
213 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
214 if (format->width > maxlen) format->width = maxlen;
215 switch(format->type)
217 case WPR_CHAR:
218 case WPR_WCHAR:
219 return (format->precision = 1);
220 case WPR_STRING:
221 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
222 for (len = 0; !format->precision || (len < format->precision); len++)
223 if (!*(arg->lpcstr_view + len)) break;
224 if (len > maxlen) len = maxlen;
225 return (format->precision = len);
226 case WPR_WSTRING:
227 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
228 for (len = 0; !format->precision || (len < format->precision); len++)
229 if (!*(arg->lpcwstr_view + len)) break;
230 if (len > maxlen) len = maxlen;
231 return (format->precision = len);
232 case WPR_SIGNED:
233 len = sprintf( number, "%d", arg->int_view );
234 break;
235 case WPR_UNSIGNED:
236 len = sprintf( number, "%u", (UINT)arg->int_view );
237 break;
238 case WPR_HEXA:
239 len = sprintf( number,
240 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
241 (UINT)arg->int_view);
242 break;
243 default:
244 return 0;
246 if (len > maxlen) len = maxlen;
247 if (format->precision < len) format->precision = len;
248 if (format->precision > maxlen) format->precision = maxlen;
249 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
250 format->precision = format->width;
251 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
252 return len;
255 /***********************************************************************
256 * WPRINTF_ExtractVAPtr (Not a Windows API)
258 static WPRINTF_DATA WPRINTF_ExtractVAPtr( WPRINTF_FORMAT *format, va_list* args )
260 WPRINTF_DATA result;
261 switch(format->type)
263 case WPR_WCHAR:
264 result.wchar_view = va_arg( *args, WCHAR ); break;
265 case WPR_CHAR:
266 result.char_view = va_arg( *args, CHAR ); break;
267 case WPR_STRING:
268 result.lpcstr_view = va_arg( *args, LPCSTR); break;
269 case WPR_WSTRING:
270 result.lpcwstr_view = va_arg( *args, LPCWSTR); break;
271 case WPR_HEXA:
272 case WPR_SIGNED:
273 case WPR_UNSIGNED:
274 result.int_view = va_arg( *args, INT ); break;
275 default:
276 result.wchar_view = 0; break;
278 return result;
281 /***********************************************************************
282 * wvsnprintf16 (Not a Windows API)
284 INT16 WINAPI wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec,
285 LPCVOID args )
287 WPRINTF_FORMAT format;
288 LPSTR p = buffer;
289 UINT i, len;
290 CHAR number[20];
291 WPRINTF_DATA cur_arg;
292 SEGPTR seg_str;
294 while (*spec && (maxlen > 1))
296 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
297 spec++;
298 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
299 spec += WPRINTF_ParseFormatA( spec, &format );
300 switch(format.type)
302 case WPR_WCHAR: /* No Unicode in Win16 */
303 case WPR_CHAR:
304 cur_arg.char_view = VA_ARG16( args, CHAR );
305 break;
306 case WPR_WSTRING: /* No Unicode in Win16 */
307 case WPR_STRING:
308 seg_str = VA_ARG16( args, SEGPTR );
309 if (IsBadReadPtr16(seg_str, 1 )) cur_arg.lpcstr_view = "";
310 else cur_arg.lpcstr_view = PTR_SEG_TO_LIN( seg_str );
311 break;
312 case WPR_SIGNED:
313 if (!(format.flags & WPRINTF_LONG))
315 cur_arg.int_view = VA_ARG16( args, INT16 );
316 break;
318 /* fall through */
319 case WPR_HEXA:
320 case WPR_UNSIGNED:
321 if (format.flags & WPRINTF_LONG)
322 cur_arg.int_view = VA_ARG16( args, UINT );
323 else
324 cur_arg.int_view = VA_ARG16( args, UINT16 );
325 break;
326 case WPR_UNKNOWN:
327 continue;
329 len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
330 if (!(format.flags & WPRINTF_LEFTALIGN))
331 for (i = format.precision; i < format.width; i++, maxlen--)
332 *p++ = ' ';
333 switch(format.type)
335 case WPR_WCHAR: /* No Unicode in Win16 */
336 case WPR_CHAR:
337 *p= cur_arg.char_view;
338 if (*p != '\0') p++;
339 else if (format.width > 1) *p++ = ' ';
340 else len = 0;
341 break;
342 case WPR_WSTRING: /* No Unicode in Win16 */
343 case WPR_STRING:
344 if (len) memcpy( p, cur_arg.lpcstr_view, len );
345 p += len;
346 break;
347 case WPR_HEXA:
348 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
350 *p++ = '0';
351 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
352 maxlen -= 2;
353 len -= 2;
355 /* fall through */
356 case WPR_SIGNED:
357 case WPR_UNSIGNED:
358 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
359 if (len) memcpy( p, number, len );
360 p += len;
361 break;
362 case WPR_UNKNOWN:
363 continue;
365 if (format.flags & WPRINTF_LEFTALIGN)
366 for (i = format.precision; i < format.width; i++, maxlen--)
367 *p++ = ' ';
368 maxlen -= len;
370 *p = 0;
371 return (maxlen > 1) ? (INT)(p - buffer) : -1;
375 /***********************************************************************
376 * wvsnprintfA (Not a Windows API)
378 INT WINAPI wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec,
379 va_list args )
381 WPRINTF_FORMAT format;
382 LPSTR p = buffer;
383 UINT i, len;
384 CHAR number[20];
385 WPRINTF_DATA argData;
387 while (*spec && (maxlen > 1))
389 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
390 spec++;
391 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
392 spec += WPRINTF_ParseFormatA( spec, &format );
393 argData = WPRINTF_ExtractVAPtr( &format, &args );
394 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
395 if (!(format.flags & WPRINTF_LEFTALIGN))
396 for (i = format.precision; i < format.width; i++, maxlen--)
397 *p++ = ' ';
398 switch(format.type)
400 case WPR_WCHAR:
401 *p = argData.wchar_view;
402 if (*p != '\0') p++;
403 else if (format.width > 1) *p++ = ' ';
404 else len = 0;
405 break;
406 case WPR_CHAR:
407 *p = argData.char_view;
408 if (*p != '\0') p++;
409 else if (format.width > 1) *p++ = ' ';
410 else len = 0;
411 break;
412 case WPR_STRING:
413 memcpy( p, argData.lpcstr_view, len );
414 p += len;
415 break;
416 case WPR_WSTRING:
418 LPCWSTR ptr = argData.lpcwstr_view;
419 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
421 break;
422 case WPR_HEXA:
423 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
425 *p++ = '0';
426 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
427 maxlen -= 2;
428 len -= 2;
430 /* fall through */
431 case WPR_SIGNED:
432 case WPR_UNSIGNED:
433 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
434 memcpy( p, number, len );
435 p += len;
436 /* Go to the next arg */
437 break;
438 case WPR_UNKNOWN:
439 continue;
441 if (format.flags & WPRINTF_LEFTALIGN)
442 for (i = format.precision; i < format.width; i++, maxlen--)
443 *p++ = ' ';
444 maxlen -= len;
446 *p = 0;
447 TRACE("%s\n",buffer);
448 return (maxlen > 1) ? (INT)(p - buffer) : -1;
452 /***********************************************************************
453 * wvsnprintfW (Not a Windows API)
455 INT WINAPI wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec,
456 va_list args )
458 WPRINTF_FORMAT format;
459 LPWSTR p = buffer;
460 UINT i, len;
461 CHAR number[20];
463 while (*spec && (maxlen > 1))
465 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
466 spec++;
467 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
468 spec += WPRINTF_ParseFormatW( spec, &format );
469 len = WPRINTF_GetLen( &format, args, number, maxlen - 1 );
470 if (!(format.flags & WPRINTF_LEFTALIGN))
471 for (i = format.precision; i < format.width; i++, maxlen--)
472 *p++ = ' ';
473 switch(format.type)
475 case WPR_WCHAR:
476 *p = va_arg( args, WCHAR );
477 if (*p != '\0') p++;
478 else if (format.width > 1) *p++ = ' ';
479 else len = 0;
480 break;
481 case WPR_CHAR:
482 *p = (WCHAR)va_arg( args, CHAR );
483 if (*p != '\0') p++;
484 else if (format.width > 1) *p++ = ' ';
485 else len = 0;
486 break;
487 case WPR_STRING:
489 LPCSTR ptr = va_arg( args, LPCSTR );
490 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
492 break;
493 case WPR_WSTRING:
494 if (len) memcpy( p, va_arg( args, LPCWSTR ), len * sizeof(WCHAR) );
495 p += len;
496 break;
497 case WPR_HEXA:
498 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
500 *p++ = '0';
501 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
502 maxlen -= 2;
503 len -= 2;
505 /* fall through */
506 case WPR_SIGNED:
507 case WPR_UNSIGNED:
508 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
509 for (i = 0; i < len; i++) *p++ = (WCHAR)number[i];
510 (void)va_arg( args, INT ); /* Go to the next arg */
511 break;
512 case WPR_UNKNOWN:
513 continue;
515 if (format.flags & WPRINTF_LEFTALIGN)
516 for (i = format.precision; i < format.width; i++, maxlen--)
517 *p++ = ' ';
518 maxlen -= len;
520 *p = 0;
521 return (maxlen > 1) ? (INT)(p - buffer) : -1;
525 /***********************************************************************
526 * wvsprintf16 (USER.421)
528 INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, LPCVOID args )
530 TRACE("for %p got:\n",buffer);
531 return wvsnprintf16( buffer, 0xffff, spec, args );
535 /***********************************************************************
536 * wvsprintfA (USER32.587)
538 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, va_list args )
540 TRACE("for %p got:\n",buffer);
541 return wvsnprintfA( buffer, 0xffffffff, spec, args );
545 /***********************************************************************
546 * wvsprintfW (USER32.588)
548 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, va_list args )
550 TRACE("for %p got:\n",buffer);
551 return wvsnprintfW( buffer, 0xffffffff, spec, args );
555 /***********************************************************************
556 * wsprintf16 (USER.420)
558 /* Winelib version */
559 INT16 WINAPIV wsprintf16( LPSTR buffer, LPCSTR spec, ... )
561 va_list valist;
562 INT16 res;
564 TRACE("for %p got:\n",buffer);
565 va_start( valist, spec );
566 /* Note: we call the 32-bit version, because the args are 32-bit */
567 res = (INT16)wvsnprintfA( buffer, 0xffffffff, spec, valist );
568 va_end( valist );
569 return res;
572 /* Emulator version */
573 INT16 WINAPIV WIN16_wsprintf16(void)
575 VA_LIST16 valist;
576 INT16 res;
577 SEGPTR buffer, spec;
579 VA_START16( valist );
580 buffer = VA_ARG16( valist, SEGPTR );
581 spec = VA_ARG16( valist, SEGPTR );
582 TRACE("got:\n");
583 res = wvsnprintf16( (LPSTR)PTR_SEG_TO_LIN(buffer), 0xffff,
584 (LPCSTR)PTR_SEG_TO_LIN(spec), valist );
585 VA_END16( valist );
586 return res;
590 /***********************************************************************
591 * wsprintfA (USER32.585)
593 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
595 va_list valist;
596 INT res;
598 TRACE("for %p got:\n",buffer);
599 va_start( valist, spec );
600 res = wvsnprintfA( buffer, 0xffffffff, spec, valist );
601 va_end( valist );
602 return res;
606 /***********************************************************************
607 * wsprintfW (USER32.586)
609 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
611 va_list valist;
612 INT res;
614 TRACE("wsprintfW for %p\n",buffer);
615 va_start( valist, spec );
616 res = wvsnprintfW( buffer, 0xffffffff, spec, valist );
617 va_end( valist );
618 return res;
622 /***********************************************************************
623 * wsnprintfA (Not a Windows API)
625 INT WINAPIV wsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, ... )
627 va_list valist;
628 INT res;
630 va_start( valist, spec );
631 res = wvsnprintfA( buffer, maxlen, spec, valist );
632 va_end( valist );
633 return res;
637 /***********************************************************************
638 * wsnprintfW (Not a Windows API)
640 INT WINAPIV wsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, ... )
642 va_list valist;
643 INT res;
645 va_start( valist, spec );
646 res = wvsnprintfW( buffer, maxlen, spec, valist );
647 va_end( valist );
648 return res;
651 /***********************************************************************
652 * _DebugOutput (KERNEL.328)
654 void WINAPIV _DebugOutput( void )
656 VA_LIST16 valist;
657 WORD flags;
658 SEGPTR spec;
659 int i, nSeg = 0;
660 NE_MODULE *pModule;
661 char caller[101], temp[512];
663 /* Decode caller address */
664 pModule = NE_GetPtr( CURRENT_STACK16->cs );
665 if ( pModule )
667 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
668 for ( i = 0; i < pModule->seg_count; i++, pSeg++ )
669 if ( GlobalHandleToSel16( pSeg->hSeg ) == CURRENT_STACK16->cs )
671 nSeg = i+1;
672 break;
675 if ( nSeg )
676 sprintf( caller, "%s %02X:%04X", NE_MODULE_NAME( pModule ),
677 nSeg, CURRENT_STACK16->ip );
678 else
679 sprintf( caller, "%04X:%04X", CURRENT_STACK16->cs, CURRENT_STACK16->ip );
681 /* Build debug message string */
682 VA_START16( valist );
683 flags = VA_ARG16( valist, WORD );
684 spec = VA_ARG16( valist, SEGPTR );
685 wvsnprintf16( temp, sizeof(temp), (LPCSTR)PTR_SEG_TO_LIN(spec), valist );
687 /* Output */
688 DPRINTF( "_DebugOutput: %s %04X %s\n",
689 caller, flags, debugstr_an(temp, sizeof(temp)) );