6b70d8b8ef635cc29390f5d7d283e8f586ccd76e
[reactos.git] / reactos / dll / win32 / user32 / misc / wsprintf.c
1 /*
2 * wsprintf functions
3 *
4 * Copyright 1996 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 *
20 * NOTE:
21 * This code is duplicated in shlwapi. If you change something here make sure
22 * to change it in shlwapi too.
23 */
24 /*
25 * COPYRIGHT: See COPYING in the top level directory
26 * PROJECT: ReactOS User32
27 * PURPOSE: [w]sprintf functions
28 * FILE: lib/user32/wsprintf.c
29 * PROGRAMER: Steven Edwards
30 * REVISION HISTORY: 2003/07/13 Merged from wine user/wsprintf.c
31 * NOTES: Adapted from Wine
32 */
33
34 #include <user32.h>
35
36 #define WINE_NO_TRACE_MSGS
37 #include <wine/debug.h>
38
39 WINE_DEFAULT_DEBUG_CHANNEL(string);
40
41
42 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
43 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
44 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
45 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
46 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
47 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
48 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
49
50 typedef enum
51 {
52 WPR_UNKNOWN,
53 WPR_CHAR,
54 WPR_WCHAR,
55 WPR_STRING,
56 WPR_WSTRING,
57 WPR_SIGNED,
58 WPR_UNSIGNED,
59 WPR_HEXA
60 } WPRINTF_TYPE;
61
62 typedef struct
63 {
64 UINT flags;
65 UINT width;
66 UINT precision;
67 WPRINTF_TYPE type;
68 } WPRINTF_FORMAT;
69
70 typedef union {
71 WCHAR wchar_view;
72 CHAR char_view;
73 LPCSTR lpcstr_view;
74 LPCWSTR lpcwstr_view;
75 INT int_view;
76 } WPRINTF_DATA;
77
78 static const CHAR null_stringA[] = "(null)";
79 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
80
81 /***********************************************************************
82 * WPRINTF_ParseFormatA
83 *
84 * Parse a format specification. A format specification has the form:
85 *
86 * [-][#][0][width][.precision]type
87 *
88 * Return value is the length of the format specification in characters.
89 */
90 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
91 {
92 LPCSTR p = format;
93
94 res->flags = 0;
95 res->width = 0;
96 res->precision = 0;
97 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
98 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
99 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
100 while ((*p >= '0') && (*p <= '9')) /* width field */
101 {
102 res->width = res->width * 10 + *p - '0';
103 p++;
104 }
105 if (*p == '.') /* precision field */
106 {
107 p++;
108 while ((*p >= '0') && (*p <= '9'))
109 {
110 res->precision = res->precision * 10 + *p - '0';
111 p++;
112 }
113 }
114 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
115 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
116 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
117 switch(*p)
118 {
119 case 'c':
120 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
121 break;
122 case 'C':
123 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
124 break;
125 case 'd':
126 case 'i':
127 res->type = WPR_SIGNED;
128 break;
129 case 's':
130 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
131 break;
132 case 'S':
133 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
134 break;
135 case 'u':
136 res->type = WPR_UNSIGNED;
137 break;
138 case 'p':
139 res->width = 8;
140 res->flags |= WPRINTF_ZEROPAD;
141 /* fall through */
142 case 'X':
143 res->flags |= WPRINTF_UPPER_HEX;
144 /* fall through */
145 case 'x':
146 res->type = WPR_HEXA;
147 break;
148 default: /* unknown format char */
149 res->type = WPR_UNKNOWN;
150 p--; /* print format as normal char */
151 break;
152 }
153 return (INT)(p - format) + 1;
154 }
155
156
157 /***********************************************************************
158 * WPRINTF_ParseFormatW
159 *
160 * Parse a format specification. A format specification has the form:
161 *
162 * [-][#][0][width][.precision]type
163 *
164 * Return value is the length of the format specification in characters.
165 */
166 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
167 {
168 LPCWSTR p = format;
169
170 res->flags = 0;
171 res->width = 0;
172 res->precision = 0;
173 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
174 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
175 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
176 while ((*p >= '0') && (*p <= '9')) /* width field */
177 {
178 res->width = res->width * 10 + *p - '0';
179 p++;
180 }
181 if (*p == '.') /* precision field */
182 {
183 p++;
184 while ((*p >= '0') && (*p <= '9'))
185 {
186 res->precision = res->precision * 10 + *p - '0';
187 p++;
188 }
189 }
190 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
191 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
192 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
193 switch((CHAR)*p)
194 {
195 case 'c':
196 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
197 break;
198 case 'C':
199 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
200 break;
201 case 'd':
202 case 'i':
203 res->type = WPR_SIGNED;
204 break;
205 case 's':
206 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
207 break;
208 case 'S':
209 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
210 break;
211 case 'u':
212 res->type = WPR_UNSIGNED;
213 break;
214 case 'p':
215 res->width = 8;
216 res->flags |= WPRINTF_ZEROPAD;
217 /* fall through */
218 case 'X':
219 res->flags |= WPRINTF_UPPER_HEX;
220 /* fall through */
221 case 'x':
222 res->type = WPR_HEXA;
223 break;
224 default:
225 res->type = WPR_UNKNOWN;
226 p--; /* print format as normal char */
227 break;
228 }
229 return (INT)(p - format) + 1;
230 }
231
232
233 /***********************************************************************
234 * WPRINTF_GetLen
235 */
236 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
237 LPSTR number, UINT maxlen )
238 {
239 UINT len;
240
241 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
242 if (format->width > maxlen) format->width = maxlen;
243 switch(format->type)
244 {
245 case WPR_CHAR:
246 case WPR_WCHAR:
247 return (format->precision = 1);
248 case WPR_STRING:
249 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
250 for (len = 0; !format->precision || (len < format->precision); len++)
251 if (!*(arg->lpcstr_view + len)) break;
252 if (len > maxlen) len = maxlen;
253 return (format->precision = len);
254 case WPR_WSTRING:
255 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
256 for (len = 0; !format->precision || (len < format->precision); len++)
257 if (!*(arg->lpcwstr_view + len)) break;
258 if (len > maxlen) len = maxlen;
259 return (format->precision = len);
260 case WPR_SIGNED:
261 len = sprintf( number, "%d", arg->int_view );
262 break;
263 case WPR_UNSIGNED:
264 len = sprintf( number, "%u", (UINT)arg->int_view );
265 break;
266 case WPR_HEXA:
267 len = sprintf( number,
268 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
269 (UINT)arg->int_view);
270 break;
271 default:
272 return 0;
273 }
274 if (len > maxlen) len = maxlen;
275 if (format->precision < len) format->precision = len;
276 if (format->precision > maxlen) format->precision = maxlen;
277 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
278 format->precision = format->width;
279 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
280 return len;
281 }
282
283
284 /***********************************************************************
285 * wvsnprintfA (internal)
286 */
287 static INT wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, __ms_va_list args )
288 {
289 WPRINTF_FORMAT format;
290 LPSTR p = buffer;
291 UINT i, len, sign;
292 CHAR number[20];
293 WPRINTF_DATA argData;
294
295 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
296
297 while (*spec && (maxlen > 1))
298 {
299 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
300 spec++;
301 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
302 spec += WPRINTF_ParseFormatA( spec, &format );
303
304 switch(format.type)
305 {
306 case WPR_WCHAR:
307 argData.wchar_view = (WCHAR)va_arg( args, int );
308 break;
309 case WPR_CHAR:
310 argData.char_view = (CHAR)va_arg( args, int );
311 break;
312 case WPR_STRING:
313 argData.lpcstr_view = va_arg( args, LPCSTR );
314 break;
315 case WPR_WSTRING:
316 argData.lpcwstr_view = va_arg( args, LPCWSTR );
317 break;
318 case WPR_HEXA:
319 case WPR_SIGNED:
320 case WPR_UNSIGNED:
321 argData.int_view = va_arg( args, INT );
322 break;
323 default:
324 argData.wchar_view = 0;
325 break;
326 }
327
328 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
329 sign = 0;
330 if (!(format.flags & WPRINTF_LEFTALIGN))
331 for (i = format.precision; i < format.width; i++, maxlen--)
332 *p++ = ' ';
333 switch(format.type)
334 {
335 case WPR_WCHAR:
336 *p++ = argData.wchar_view;
337 break;
338 case WPR_CHAR:
339 *p++ = argData.char_view;
340 break;
341 case WPR_STRING:
342 memcpy( p, argData.lpcstr_view, len );
343 p += len;
344 break;
345 case WPR_WSTRING:
346 {
347 LPCWSTR ptr = argData.lpcwstr_view;
348 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
349 }
350 break;
351 case WPR_HEXA:
352 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
353 {
354 *p++ = '0';
355 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
356 maxlen -= 2;
357 len -= 2;
358 }
359 /* fall through */
360 case WPR_SIGNED:
361 /* Transfer the sign now, just in case it will be zero-padded*/
362 if (number[0] == '-')
363 {
364 *p++ = '-';
365 sign = 1;
366 }
367 /* fall through */
368 case WPR_UNSIGNED:
369 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
370 memcpy( p, number + sign, len - sign );
371 p += len - sign;
372 break;
373 case WPR_UNKNOWN:
374 continue;
375 }
376 if (format.flags & WPRINTF_LEFTALIGN)
377 for (i = format.precision; i < format.width; i++, maxlen--)
378 *p++ = ' ';
379 maxlen -= len;
380 }
381 *p = 0;
382 TRACE("%s\n",debugstr_a(buffer));
383 return (maxlen > 1) ? (INT)(p - buffer) : -1;
384 }
385
386
387 /***********************************************************************
388 * wvsnprintfW (internal)
389 */
390 static INT wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, __ms_va_list args )
391 {
392 WPRINTF_FORMAT format;
393 LPWSTR p = buffer;
394 UINT i, len, sign;
395 CHAR number[20];
396 WPRINTF_DATA argData;
397
398 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
399
400 while (*spec && (maxlen > 1))
401 {
402 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
403 spec++;
404 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
405 spec += WPRINTF_ParseFormatW( spec, &format );
406
407 switch(format.type)
408 {
409 case WPR_WCHAR:
410 argData.wchar_view = (WCHAR)va_arg( args, int );
411 break;
412 case WPR_CHAR:
413 argData.char_view = (CHAR)va_arg( args, int );
414 break;
415 case WPR_STRING:
416 argData.lpcstr_view = va_arg( args, LPCSTR );
417 break;
418 case WPR_WSTRING:
419 argData.lpcwstr_view = va_arg( args, LPCWSTR );
420 break;
421 case WPR_HEXA:
422 case WPR_SIGNED:
423 case WPR_UNSIGNED:
424 argData.int_view = va_arg( args, INT );
425 break;
426 default:
427 argData.wchar_view = 0;
428 break;
429 }
430
431 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
432 sign = 0;
433 if (!(format.flags & WPRINTF_LEFTALIGN))
434 for (i = format.precision; i < format.width; i++, maxlen--)
435 *p++ = ' ';
436 switch(format.type)
437 {
438 case WPR_WCHAR:
439 *p++ = argData.wchar_view;
440 break;
441 case WPR_CHAR:
442 *p++ = argData.char_view;
443 break;
444 case WPR_STRING:
445 {
446 LPCSTR ptr = argData.lpcstr_view;
447 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
448 }
449 break;
450 case WPR_WSTRING:
451 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
452 p += len;
453 break;
454 case WPR_HEXA:
455 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
456 {
457 *p++ = '0';
458 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
459 maxlen -= 2;
460 len -= 2;
461 }
462 /* fall through */
463 case WPR_SIGNED:
464 /* Transfer the sign now, just in case it will be zero-padded*/
465 if (number[0] == '-')
466 {
467 *p++ = '-';
468 sign = 1;
469 }
470 /* fall through */
471 case WPR_UNSIGNED:
472 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
473 for (i = sign; i < len; i++) *p++ = (WCHAR)number[i];
474 break;
475 case WPR_UNKNOWN:
476 continue;
477 }
478 if (format.flags & WPRINTF_LEFTALIGN)
479 for (i = format.precision; i < format.width; i++, maxlen--)
480 *p++ = ' ';
481 maxlen -= len;
482 }
483 *p = 0;
484 TRACE("%s\n",debugstr_w(buffer));
485 return (maxlen > 1) ? (INT)(p - buffer) : -1;
486 }
487
488
489 /***********************************************************************
490 * wvsprintfA (USER32.@)
491 */
492 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, __ms_va_list args )
493 {
494 INT res = wvsnprintfA( buffer, 1024, spec, args );
495 return ( res == -1 ) ? 1024 : res;
496 }
497
498
499 /***********************************************************************
500 * wvsprintfW (USER32.@)
501 */
502 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, __ms_va_list args )
503 {
504 INT res = wvsnprintfW( buffer, 1024, spec, args );
505 return ( res == -1 ) ? 1024 : res;
506 }
507
508
509 /***********************************************************************
510 * wsprintfA (USER32.@)
511 */
512 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
513 {
514 __ms_va_list valist;
515 INT res;
516
517 __ms_va_start( valist, spec );
518 res = wvsnprintfA( buffer, 1024, spec, valist );
519 __ms_va_end( valist );
520 return ( res == -1 ) ? 1024 : res;
521 }
522
523
524 /***********************************************************************
525 * wsprintfW (USER32.@)
526 */
527 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
528 {
529 __ms_va_list valist;
530 INT res;
531
532 __ms_va_start( valist, spec );
533 res = wvsnprintfW( buffer, 1024, spec, valist );
534 __ms_va_end( valist );
535 return ( res == -1 ) ? 1024 : res;
536 }