4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * This code is duplicated in shlwapi. If you change something here make sure
22 * to change it in shlwapi too.
34 #include "wine/winbase16.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(string);
41 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
42 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
43 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
44 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
45 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
46 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
47 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
77 static const CHAR null_stringA[] = "(null)";
78 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
80 /***********************************************************************
81 * WPRINTF_ParseFormatA
83 * Parse a format specification. A format specification has the form:
85 * [-][#][0][width][.precision]type
87 * Return value is the length of the format specification in characters.
89 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
96 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
97 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
98 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
99 while ((*p >= '0') && (*p <= '9')) /* width field */
101 res->width = res->width * 10 + *p - '0';
104 if (*p == '.') /* precision field */
107 while ((*p >= '0') && (*p <= '9'))
109 res->precision = res->precision * 10 + *p - '0';
113 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
114 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
115 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
119 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
122 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
126 res->type = WPR_SIGNED;
129 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
132 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
135 res->type = WPR_UNSIGNED;
139 res->flags |= WPRINTF_ZEROPAD;
142 res->flags |= WPRINTF_UPPER_HEX;
145 res->type = WPR_HEXA;
147 default: /* unknown format char */
148 res->type = WPR_UNKNOWN;
149 p--; /* print format as normal char */
152 return (INT)(p - format) + 1;
156 /***********************************************************************
157 * WPRINTF_ParseFormatW
159 * Parse a format specification. A format specification has the form:
161 * [-][#][0][width][.precision]type
163 * Return value is the length of the format specification in characters.
165 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
172 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
173 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
174 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
175 while ((*p >= '0') && (*p <= '9')) /* width field */
177 res->width = res->width * 10 + *p - '0';
180 if (*p == '.') /* precision field */
183 while ((*p >= '0') && (*p <= '9'))
185 res->precision = res->precision * 10 + *p - '0';
189 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
190 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
191 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
195 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
198 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
202 res->type = WPR_SIGNED;
205 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
208 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
211 res->type = WPR_UNSIGNED;
215 res->flags |= WPRINTF_ZEROPAD;
218 res->flags |= WPRINTF_UPPER_HEX;
221 res->type = WPR_HEXA;
224 res->type = WPR_UNKNOWN;
225 p--; /* print format as normal char */
228 return (INT)(p - format) + 1;
232 /***********************************************************************
235 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
236 LPSTR number, UINT maxlen )
240 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
241 if (format->width > maxlen) format->width = maxlen;
246 return (format->precision = 1);
248 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
249 for (len = 0; !format->precision || (len < format->precision); len++)
250 if (!*(arg->lpcstr_view + len)) break;
251 if (len > maxlen) len = maxlen;
252 return (format->precision = len);
254 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
255 for (len = 0; !format->precision || (len < format->precision); len++)
256 if (!*(arg->lpcwstr_view + len)) break;
257 if (len > maxlen) len = maxlen;
258 return (format->precision = len);
260 len = sprintf( number, "%d", arg->int_view );
263 len = sprintf( number, "%u", (UINT)arg->int_view );
266 len = sprintf( number,
267 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
268 (UINT)arg->int_view);
273 if (len > maxlen) len = maxlen;
274 if (format->precision < len) format->precision = len;
275 if (format->precision > maxlen) format->precision = maxlen;
276 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
277 format->precision = format->width;
278 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
283 /***********************************************************************
284 * wvsnprintf16 (Not a Windows API)
286 static INT16 wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec, VA_LIST16 args )
288 WPRINTF_FORMAT format;
292 WPRINTF_DATA cur_arg;
295 while (*spec && (maxlen > 1))
297 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
299 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
300 spec += WPRINTF_ParseFormatA( spec, &format );
303 case WPR_WCHAR: /* No Unicode in Win16 */
305 cur_arg.char_view = VA_ARG16( args, CHAR );
307 case WPR_WSTRING: /* No Unicode in Win16 */
309 seg_str = VA_ARG16( args, SEGPTR );
310 if (IsBadReadPtr16(seg_str, 1 )) cur_arg.lpcstr_view = "";
311 else cur_arg.lpcstr_view = MapSL( seg_str );
314 if (!(format.flags & WPRINTF_LONG))
316 cur_arg.int_view = VA_ARG16( args, INT16 );
322 if (format.flags & WPRINTF_LONG)
323 cur_arg.int_view = VA_ARG16( args, UINT );
325 cur_arg.int_view = VA_ARG16( args, UINT16 );
330 len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
332 if (!(format.flags & WPRINTF_LEFTALIGN))
333 for (i = format.precision; i < format.width; i++, maxlen--)
337 case WPR_WCHAR: /* No Unicode in Win16 */
339 *p= cur_arg.char_view;
340 /* wsprintf16 (unlike wsprintf) ignores null characters */
342 else if (format.width > 1) *p++ = ' ';
345 case WPR_WSTRING: /* No Unicode in Win16 */
347 if (len) memcpy( p, cur_arg.lpcstr_view, len );
351 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
354 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
360 /* Transfer the sign now, just in case it will be zero-padded*/
361 if (number[0] == '-')
368 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
369 if (len > sign) memcpy( p, number + sign, len - sign );
375 if (format.flags & WPRINTF_LEFTALIGN)
376 for (i = format.precision; i < format.width; i++, maxlen--)
381 return (maxlen > 1) ? (INT)(p - buffer) : -1;
385 /***********************************************************************
386 * wvsnprintfA (internal)
388 static INT wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, __ms_va_list args )
390 WPRINTF_FORMAT format;
394 WPRINTF_DATA argData;
396 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
398 while (*spec && (maxlen > 1))
400 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
402 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
403 spec += WPRINTF_ParseFormatA( spec, &format );
408 argData.wchar_view = (WCHAR)va_arg( args, int );
411 argData.char_view = (CHAR)va_arg( args, int );
414 argData.lpcstr_view = va_arg( args, LPCSTR );
417 argData.lpcwstr_view = va_arg( args, LPCWSTR );
422 argData.int_view = va_arg( args, INT );
425 argData.wchar_view = 0;
429 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
431 if (!(format.flags & WPRINTF_LEFTALIGN))
432 for (i = format.precision; i < format.width; i++, maxlen--)
437 *p++ = argData.wchar_view;
440 *p++ = argData.char_view;
443 memcpy( p, argData.lpcstr_view, len );
448 LPCWSTR ptr = argData.lpcwstr_view;
449 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
453 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
456 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
462 /* Transfer the sign now, just in case it will be zero-padded*/
463 if (number[0] == '-')
470 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
471 memcpy( p, number + sign, len - sign );
477 if (format.flags & WPRINTF_LEFTALIGN)
478 for (i = format.precision; i < format.width; i++, maxlen--)
483 TRACE("%s\n",debugstr_a(buffer));
484 return (maxlen > 1) ? (INT)(p - buffer) : -1;
488 /***********************************************************************
489 * wvsnprintfW (internal)
491 static INT wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, __ms_va_list args )
493 WPRINTF_FORMAT format;
497 WPRINTF_DATA argData;
499 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
501 while (*spec && (maxlen > 1))
503 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
505 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
506 spec += WPRINTF_ParseFormatW( spec, &format );
511 argData.wchar_view = (WCHAR)va_arg( args, int );
514 argData.char_view = (CHAR)va_arg( args, int );
517 argData.lpcstr_view = va_arg( args, LPCSTR );
520 argData.lpcwstr_view = va_arg( args, LPCWSTR );
525 argData.int_view = va_arg( args, INT );
528 argData.wchar_view = 0;
532 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
534 if (!(format.flags & WPRINTF_LEFTALIGN))
535 for (i = format.precision; i < format.width; i++, maxlen--)
540 *p++ = argData.wchar_view;
543 *p++ = argData.char_view;
547 LPCSTR ptr = argData.lpcstr_view;
548 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
552 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
556 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
559 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
565 /* Transfer the sign now, just in case it will be zero-padded*/
566 if (number[0] == '-')
573 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
574 for (i = sign; i < len; i++) *p++ = (WCHAR)number[i];
579 if (format.flags & WPRINTF_LEFTALIGN)
580 for (i = format.precision; i < format.width; i++, maxlen--)
585 TRACE("%s\n",debugstr_w(buffer));
586 return (maxlen > 1) ? (INT)(p - buffer) : -1;
590 /***********************************************************************
591 * wvsprintf (USER.421)
593 INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, VA_LIST16 args )
597 TRACE("for %p got:\n",buffer);
598 res = wvsnprintf16( buffer, 1024, spec, args );
599 return ( res == -1 ) ? 1024 : res;
603 /***********************************************************************
604 * wvsprintfA (USER32.@)
606 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, __ms_va_list args )
608 INT res = wvsnprintfA( buffer, 1024, spec, args );
609 return ( res == -1 ) ? 1024 : res;
613 /***********************************************************************
614 * wvsprintfW (USER32.@)
616 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, __ms_va_list args )
618 INT res = wvsnprintfW( buffer, 1024, spec, args );
619 return ( res == -1 ) ? 1024 : res;
623 /***********************************************************************
624 * _wsprintf (USER.420)
626 INT16 WINAPIV wsprintf16( LPSTR buffer, LPCSTR spec, VA_LIST16 valist )
630 res = wvsnprintf16( buffer, 1024, spec, valist );
631 return ( res == -1 ) ? 1024 : res;
635 /***********************************************************************
636 * wsprintfA (USER32.@)
638 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
643 __ms_va_start( valist, spec );
644 res = wvsnprintfA( buffer, 1024, spec, valist );
645 __ms_va_end( valist );
646 return ( res == -1 ) ? 1024 : res;
650 /***********************************************************************
651 * wsprintfW (USER32.@)
653 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
658 __ms_va_start( valist, spec );
659 res = wvsnprintfW( buffer, 1024, spec, valist );
660 __ms_va_end( valist );
661 return ( res == -1 ) ? 1024 : res;