msi: Handle some error returns (Coverity).
[wine.git] / dlls / user32 / wsprintf.c
blob533c48a2acc4e85ff26afe2464e4b88c4cf658d4
1 /*
2 * wsprintf functions
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
20 * NOTE:
21 * This code is duplicated in shlwapi. If you change something here make sure
22 * to change it in shlwapi too.
25 #include <stdarg.h>
26 #include <string.h>
27 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(string);
39 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
40 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
41 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
42 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
43 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
44 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
45 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
47 typedef enum
49 WPR_UNKNOWN,
50 WPR_CHAR,
51 WPR_WCHAR,
52 WPR_STRING,
53 WPR_WSTRING,
54 WPR_SIGNED,
55 WPR_UNSIGNED,
56 WPR_HEXA
57 } WPRINTF_TYPE;
59 typedef struct
61 UINT flags;
62 UINT width;
63 UINT precision;
64 WPRINTF_TYPE type;
65 } WPRINTF_FORMAT;
67 typedef union {
68 WCHAR wchar_view;
69 CHAR char_view;
70 LPCSTR lpcstr_view;
71 LPCWSTR lpcwstr_view;
72 INT int_view;
73 } WPRINTF_DATA;
75 static const CHAR null_stringA[] = "(null)";
76 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
78 /***********************************************************************
79 * WPRINTF_ParseFormatA
81 * Parse a format specification. A format specification has the form:
83 * [-][#][0][width][.precision]type
85 * Return value is the length of the format specification in characters.
87 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
89 LPCSTR p = format;
91 res->flags = 0;
92 res->width = 0;
93 res->precision = 0;
94 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
95 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
96 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
97 while ((*p >= '0') && (*p <= '9')) /* width field */
99 res->width = res->width * 10 + *p - '0';
100 p++;
102 if (*p == '.') /* precision field */
104 p++;
105 while ((*p >= '0') && (*p <= '9'))
107 res->precision = res->precision * 10 + *p - '0';
108 p++;
111 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
112 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
113 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
114 switch(*p)
116 case 'c':
117 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
118 break;
119 case 'C':
120 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
121 break;
122 case 'd':
123 case 'i':
124 res->type = WPR_SIGNED;
125 break;
126 case 's':
127 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
128 break;
129 case 'S':
130 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
131 break;
132 case 'u':
133 res->type = WPR_UNSIGNED;
134 break;
135 case 'p':
136 res->width = 8;
137 res->flags |= WPRINTF_ZEROPAD;
138 /* fall through */
139 case 'X':
140 res->flags |= WPRINTF_UPPER_HEX;
141 /* fall through */
142 case 'x':
143 res->type = WPR_HEXA;
144 break;
145 default: /* unknown format char */
146 res->type = WPR_UNKNOWN;
147 p--; /* print format as normal char */
148 break;
150 return (INT)(p - format) + 1;
154 /***********************************************************************
155 * WPRINTF_ParseFormatW
157 * Parse a format specification. A format specification has the form:
159 * [-][#][0][width][.precision]type
161 * Return value is the length of the format specification in characters.
163 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
165 LPCWSTR p = format;
167 res->flags = 0;
168 res->width = 0;
169 res->precision = 0;
170 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
171 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
172 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
173 while ((*p >= '0') && (*p <= '9')) /* width field */
175 res->width = res->width * 10 + *p - '0';
176 p++;
178 if (*p == '.') /* precision field */
180 p++;
181 while ((*p >= '0') && (*p <= '9'))
183 res->precision = res->precision * 10 + *p - '0';
184 p++;
187 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
188 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
189 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
190 switch((CHAR)*p)
192 case 'c':
193 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
194 break;
195 case 'C':
196 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
197 break;
198 case 'd':
199 case 'i':
200 res->type = WPR_SIGNED;
201 break;
202 case 's':
203 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
204 break;
205 case 'S':
206 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
207 break;
208 case 'u':
209 res->type = WPR_UNSIGNED;
210 break;
211 case 'p':
212 res->width = 8;
213 res->flags |= WPRINTF_ZEROPAD;
214 /* fall through */
215 case 'X':
216 res->flags |= WPRINTF_UPPER_HEX;
217 /* fall through */
218 case 'x':
219 res->type = WPR_HEXA;
220 break;
221 default:
222 res->type = WPR_UNKNOWN;
223 p--; /* print format as normal char */
224 break;
226 return (INT)(p - format) + 1;
230 /***********************************************************************
231 * WPRINTF_GetLen
233 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
234 LPSTR number, UINT maxlen )
236 UINT len;
238 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
239 if (format->width > maxlen) format->width = maxlen;
240 switch(format->type)
242 case WPR_CHAR:
243 case WPR_WCHAR:
244 return (format->precision = 1);
245 case WPR_STRING:
246 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
247 for (len = 0; !format->precision || (len < format->precision); len++)
248 if (!*(arg->lpcstr_view + len)) break;
249 if (len > maxlen) len = maxlen;
250 return (format->precision = len);
251 case WPR_WSTRING:
252 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
253 for (len = 0; !format->precision || (len < format->precision); len++)
254 if (!*(arg->lpcwstr_view + len)) break;
255 if (len > maxlen) len = maxlen;
256 return (format->precision = len);
257 case WPR_SIGNED:
258 len = sprintf( number, "%d", arg->int_view );
259 break;
260 case WPR_UNSIGNED:
261 len = sprintf( number, "%u", (UINT)arg->int_view );
262 break;
263 case WPR_HEXA:
264 len = sprintf( number,
265 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
266 (UINT)arg->int_view);
267 break;
268 default:
269 return 0;
271 if (len > maxlen) len = maxlen;
272 if (format->precision < len) format->precision = len;
273 if (format->precision > maxlen) format->precision = maxlen;
274 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
275 format->precision = format->width;
276 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
277 return len;
281 /***********************************************************************
282 * wvsnprintfA (internal)
284 static INT wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, __ms_va_list args )
286 WPRINTF_FORMAT format;
287 LPSTR p = buffer;
288 UINT i, len, sign;
289 CHAR number[20];
290 WPRINTF_DATA argData;
292 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
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 );
301 switch(format.type)
303 case WPR_WCHAR:
304 argData.wchar_view = (WCHAR)va_arg( args, int );
305 break;
306 case WPR_CHAR:
307 argData.char_view = (CHAR)va_arg( args, int );
308 break;
309 case WPR_STRING:
310 argData.lpcstr_view = va_arg( args, LPCSTR );
311 break;
312 case WPR_WSTRING:
313 argData.lpcwstr_view = va_arg( args, LPCWSTR );
314 break;
315 case WPR_HEXA:
316 case WPR_SIGNED:
317 case WPR_UNSIGNED:
318 argData.int_view = va_arg( args, INT );
319 break;
320 default:
321 argData.wchar_view = 0;
322 break;
325 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
326 sign = 0;
327 if (!(format.flags & WPRINTF_LEFTALIGN))
328 for (i = format.precision; i < format.width; i++, maxlen--)
329 *p++ = ' ';
330 switch(format.type)
332 case WPR_WCHAR:
333 *p++ = argData.wchar_view;
334 break;
335 case WPR_CHAR:
336 *p++ = argData.char_view;
337 break;
338 case WPR_STRING:
339 memcpy( p, argData.lpcstr_view, len );
340 p += len;
341 break;
342 case WPR_WSTRING:
344 LPCWSTR ptr = argData.lpcwstr_view;
345 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
347 break;
348 case WPR_HEXA:
349 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
351 *p++ = '0';
352 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
353 maxlen -= 2;
354 len -= 2;
356 /* fall through */
357 case WPR_SIGNED:
358 /* Transfer the sign now, just in case it will be zero-padded*/
359 if (number[0] == '-')
361 *p++ = '-';
362 sign = 1;
364 /* fall through */
365 case WPR_UNSIGNED:
366 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
367 memcpy( p, number + sign, len - sign );
368 p += len - sign;
369 break;
370 case WPR_UNKNOWN:
371 continue;
373 if (format.flags & WPRINTF_LEFTALIGN)
374 for (i = format.precision; i < format.width; i++, maxlen--)
375 *p++ = ' ';
376 maxlen -= len;
378 *p = 0;
379 TRACE("%s\n",debugstr_a(buffer));
380 return (maxlen > 1) ? (INT)(p - buffer) : -1;
384 /***********************************************************************
385 * wvsnprintfW (internal)
387 static INT wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, __ms_va_list args )
389 WPRINTF_FORMAT format;
390 LPWSTR p = buffer;
391 UINT i, len, sign;
392 CHAR number[20];
393 WPRINTF_DATA argData;
395 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
397 while (*spec && (maxlen > 1))
399 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
400 spec++;
401 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
402 spec += WPRINTF_ParseFormatW( spec, &format );
404 switch(format.type)
406 case WPR_WCHAR:
407 argData.wchar_view = (WCHAR)va_arg( args, int );
408 break;
409 case WPR_CHAR:
410 argData.char_view = (CHAR)va_arg( args, int );
411 break;
412 case WPR_STRING:
413 argData.lpcstr_view = va_arg( args, LPCSTR );
414 break;
415 case WPR_WSTRING:
416 argData.lpcwstr_view = va_arg( args, LPCWSTR );
417 break;
418 case WPR_HEXA:
419 case WPR_SIGNED:
420 case WPR_UNSIGNED:
421 argData.int_view = va_arg( args, INT );
422 break;
423 default:
424 argData.wchar_view = 0;
425 break;
428 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
429 sign = 0;
430 if (!(format.flags & WPRINTF_LEFTALIGN))
431 for (i = format.precision; i < format.width; i++, maxlen--)
432 *p++ = ' ';
433 switch(format.type)
435 case WPR_WCHAR:
436 *p++ = argData.wchar_view;
437 break;
438 case WPR_CHAR:
439 *p++ = argData.char_view;
440 break;
441 case WPR_STRING:
443 LPCSTR ptr = argData.lpcstr_view;
444 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
446 break;
447 case WPR_WSTRING:
448 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
449 p += len;
450 break;
451 case WPR_HEXA:
452 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
454 *p++ = '0';
455 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
456 maxlen -= 2;
457 len -= 2;
459 /* fall through */
460 case WPR_SIGNED:
461 /* Transfer the sign now, just in case it will be zero-padded*/
462 if (number[0] == '-')
464 *p++ = '-';
465 sign = 1;
467 /* fall through */
468 case WPR_UNSIGNED:
469 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
470 for (i = sign; i < len; i++) *p++ = (WCHAR)number[i];
471 break;
472 case WPR_UNKNOWN:
473 continue;
475 if (format.flags & WPRINTF_LEFTALIGN)
476 for (i = format.precision; i < format.width; i++, maxlen--)
477 *p++ = ' ';
478 maxlen -= len;
480 *p = 0;
481 TRACE("%s\n",debugstr_w(buffer));
482 return (maxlen > 1) ? (INT)(p - buffer) : -1;
486 /***********************************************************************
487 * wvsprintfA (USER32.@)
489 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, __ms_va_list args )
491 INT res = wvsnprintfA( buffer, 1024, spec, args );
492 return ( res == -1 ) ? 1024 : res;
496 /***********************************************************************
497 * wvsprintfW (USER32.@)
499 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, __ms_va_list args )
501 INT res = wvsnprintfW( buffer, 1024, spec, args );
502 return ( res == -1 ) ? 1024 : res;
506 /***********************************************************************
507 * wsprintfA (USER32.@)
509 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
511 __ms_va_list valist;
512 INT res;
514 __ms_va_start( valist, spec );
515 res = wvsnprintfA( buffer, 1024, spec, valist );
516 __ms_va_end( valist );
517 return ( res == -1 ) ? 1024 : res;
521 /***********************************************************************
522 * wsprintfW (USER32.@)
524 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
526 __ms_va_list valist;
527 INT res;
529 __ms_va_start( valist, spec );
530 res = wvsnprintfW( buffer, 1024, spec, valist );
531 __ms_va_end( valist );
532 return ( res == -1 ) ? 1024 : res;