4 * Copyright 1996 Alexandre Julliard
9 #include "wine/winbase16.h"
12 #include "stackframe.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) */
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 )
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';
83 if (*p == '.') /* precision field */
86 while ((*p >= '0') && (*p <= '9'))
88 res->precision = res->precision * 10 + *p - '0';
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++; }
98 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
101 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
105 res->type = WPR_SIGNED;
108 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE))
109 ? WPR_WSTRING : WPR_STRING;
112 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE))
113 ? WPR_STRING : WPR_WSTRING;
116 res->type = WPR_UNSIGNED;
119 res->flags |= WPRINTF_UPPER_HEX;
122 res->type = WPR_HEXA;
124 default: /* unknown format char */
125 res->type = WPR_UNKNOWN;
126 p--; /* print format as normal char */
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 )
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';
157 if (*p == '.') /* precision field */
160 while ((*p >= '0') && (*p <= '9'))
162 res->precision = res->precision * 10 + *p - '0';
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++; }
172 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
175 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
179 res->type = WPR_SIGNED;
182 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
185 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
188 res->type = WPR_UNSIGNED;
191 res->flags |= WPRINTF_UPPER_HEX;
194 res->type = WPR_HEXA;
197 res->type = WPR_UNKNOWN;
198 p--; /* print format as normal char */
201 return (INT)(p - format) + 1;
205 /***********************************************************************
208 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
209 LPSTR number, UINT maxlen )
213 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
214 if (format->width > maxlen) format->width = maxlen;
219 return (format->precision = 1);
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);
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);
233 len = sprintf( number, "%d", arg->int_view );
236 len = sprintf( number, "%u", (UINT)arg->int_view );
239 len = sprintf( number,
240 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
241 (UINT)arg->int_view);
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;
255 /***********************************************************************
256 * WPRINTF_ExtractVAPtr (Not a Windows API)
258 static WPRINTF_DATA WPRINTF_ExtractVAPtr( WPRINTF_FORMAT *format, va_list* args )
264 result.wchar_view = va_arg( *args, WCHAR ); break;
266 result.char_view = va_arg( *args, CHAR ); break;
268 result.lpcstr_view = va_arg( *args, LPCSTR); break;
270 result.lpcwstr_view = va_arg( *args, LPCWSTR); break;
274 result.int_view = va_arg( *args, INT ); break;
276 result.wchar_view = 0; break;
281 /***********************************************************************
282 * wvsnprintf16 (Not a Windows API)
284 INT16 WINAPI wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec,
287 WPRINTF_FORMAT format;
291 WPRINTF_DATA cur_arg;
294 while (*spec && (maxlen > 1))
296 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
298 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
299 spec += WPRINTF_ParseFormatA( spec, &format );
302 case WPR_WCHAR: /* No Unicode in Win16 */
304 cur_arg.char_view = VA_ARG16( args, CHAR );
306 case WPR_WSTRING: /* No Unicode in Win16 */
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 );
313 if (!(format.flags & WPRINTF_LONG))
315 cur_arg.int_view = VA_ARG16( args, INT16 );
321 if (format.flags & WPRINTF_LONG)
322 cur_arg.int_view = VA_ARG16( args, UINT );
324 cur_arg.int_view = VA_ARG16( args, UINT16 );
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--)
335 case WPR_WCHAR: /* No Unicode in Win16 */
337 *p= cur_arg.char_view;
339 else if (format.width > 1) *p++ = ' ';
342 case WPR_WSTRING: /* No Unicode in Win16 */
344 if (len) memcpy( p, cur_arg.lpcstr_view, len );
348 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
351 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
358 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
359 if (len) memcpy( p, number, len );
365 if (format.flags & WPRINTF_LEFTALIGN)
366 for (i = format.precision; i < format.width; i++, maxlen--)
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,
381 WPRINTF_FORMAT format;
385 WPRINTF_DATA argData;
387 while (*spec && (maxlen > 1))
389 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
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--)
401 *p = argData.wchar_view;
403 else if (format.width > 1) *p++ = ' ';
407 *p = argData.char_view;
409 else if (format.width > 1) *p++ = ' ';
413 memcpy( p, argData.lpcstr_view, len );
418 LPCWSTR ptr = argData.lpcwstr_view;
419 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
423 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
426 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
433 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
434 memcpy( p, number, len );
436 /* Go to the next arg */
441 if (format.flags & WPRINTF_LEFTALIGN)
442 for (i = format.precision; i < format.width; i++, maxlen--)
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,
458 WPRINTF_FORMAT format;
463 while (*spec && (maxlen > 1))
465 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
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--)
476 *p = va_arg( args, WCHAR );
478 else if (format.width > 1) *p++ = ' ';
482 *p = (WCHAR)va_arg( args, CHAR );
484 else if (format.width > 1) *p++ = ' ';
489 LPCSTR ptr = va_arg( args, LPCSTR );
490 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
494 if (len) memcpy( p, va_arg( args, LPCWSTR ), len * sizeof(WCHAR) );
498 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
501 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
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 */
515 if (format.flags & WPRINTF_LEFTALIGN)
516 for (i = format.precision; i < format.width; i++, maxlen--)
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, ... )
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 );
572 /* Emulator version */
573 INT16 WINAPIV WIN16_wsprintf16(void)
579 VA_START16( valist );
580 buffer = VA_ARG16( valist, SEGPTR );
581 spec = VA_ARG16( valist, SEGPTR );
583 res = wvsnprintf16( (LPSTR)PTR_SEG_TO_LIN(buffer), 0xffff,
584 (LPCSTR)PTR_SEG_TO_LIN(spec), valist );
590 /***********************************************************************
591 * wsprintfA (USER32.585)
593 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
598 TRACE("for %p got:\n",buffer);
599 va_start( valist, spec );
600 res = wvsnprintfA( buffer, 0xffffffff, spec, valist );
606 /***********************************************************************
607 * wsprintfW (USER32.586)
609 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
614 TRACE("wsprintfW for %p\n",buffer);
615 va_start( valist, spec );
616 res = wvsnprintfW( buffer, 0xffffffff, spec, valist );
622 /***********************************************************************
623 * wsnprintfA (Not a Windows API)
625 INT WINAPIV wsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, ... )
630 va_start( valist, spec );
631 res = wvsnprintfA( buffer, maxlen, spec, valist );
637 /***********************************************************************
638 * wsnprintfW (Not a Windows API)
640 INT WINAPIV wsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, ... )
645 va_start( valist, spec );
646 res = wvsnprintfW( buffer, maxlen, spec, valist );
651 /***********************************************************************
652 * _DebugOutput (KERNEL.328)
654 void WINAPIV _DebugOutput( void )
661 char caller[101], temp[512];
663 /* Decode caller address */
664 pModule = NE_GetPtr( CURRENT_STACK16->cs );
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 )
676 sprintf( caller, "%s %02X:%04X", NE_MODULE_NAME( pModule ),
677 nSeg, CURRENT_STACK16->ip );
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 );
688 DPRINTF( "_DebugOutput: %s %04X %s\n",
689 caller, flags, debugstr_an(temp, sizeof(temp)) );