Get rid of string pool helper routines
[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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 #include <wine/debug.h>
37
38 WINE_DEFAULT_DEBUG_CHANNEL(string);
39
40
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) */
48
49 typedef enum
50 {
51 WPR_UNKNOWN,
52 WPR_CHAR,
53 WPR_WCHAR,
54 WPR_STRING,
55 WPR_WSTRING,
56 WPR_SIGNED,
57 WPR_UNSIGNED,
58 WPR_HEXA
59 } WPRINTF_TYPE;
60
61 typedef struct
62 {
63 UINT flags;
64 UINT width;
65 UINT precision;
66 WPRINTF_TYPE type;
67 } WPRINTF_FORMAT;
68
69 typedef union {
70 WCHAR wchar_view;
71 CHAR char_view;
72 LPCSTR lpcstr_view;
73 LPCWSTR lpcwstr_view;
74 INT int_view;
75 } WPRINTF_DATA;
76
77 static const CHAR null_stringA[] = "(null)";
78 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
79
80 /***********************************************************************
81 * WPRINTF_ParseFormatA
82 *
83 * Parse a format specification. A format specification has the form:
84 *
85 * [-][#][0][width][.precision]type
86 *
87 * Return value is the length of the format specification in characters.
88 */
89 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
90 {
91 LPCSTR p = format;
92
93 res->flags = 0;
94 res->width = 0;
95 res->precision = 0;
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 */
100 {
101 res->width = res->width * 10 + *p - '0';
102 p++;
103 }
104 if (*p == '.') /* precision field */
105 {
106 p++;
107 while ((*p >= '0') && (*p <= '9'))
108 {
109 res->precision = res->precision * 10 + *p - '0';
110 p++;
111 }
112 }
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++; }
116 switch(*p)
117 {
118 case 'c':
119 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
120 break;
121 case 'C':
122 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
123 break;
124 case 'd':
125 case 'i':
126 res->type = WPR_SIGNED;
127 break;
128 case 's':
129 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
130 break;
131 case 'S':
132 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
133 break;
134 case 'u':
135 res->type = WPR_UNSIGNED;
136 break;
137 case 'p':
138 res->width = 8;
139 res->flags |= WPRINTF_ZEROPAD;
140 /* fall through */
141 case 'X':
142 res->flags |= WPRINTF_UPPER_HEX;
143 /* fall through */
144 case 'x':
145 res->type = WPR_HEXA;
146 break;
147 default: /* unknown format char */
148 res->type = WPR_UNKNOWN;
149 p--; /* print format as normal char */
150 break;
151 }
152 return (INT)(p - format) + 1;
153 }
154
155
156 /***********************************************************************
157 * WPRINTF_ParseFormatW
158 *
159 * Parse a format specification. A format specification has the form:
160 *
161 * [-][#][0][width][.precision]type
162 *
163 * Return value is the length of the format specification in characters.
164 */
165 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
166 {
167 LPCWSTR p = format;
168
169 res->flags = 0;
170 res->width = 0;
171 res->precision = 0;
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 */
176 {
177 res->width = res->width * 10 + *p - '0';
178 p++;
179 }
180 if (*p == '.') /* precision field */
181 {
182 p++;
183 while ((*p >= '0') && (*p <= '9'))
184 {
185 res->precision = res->precision * 10 + *p - '0';
186 p++;
187 }
188 }
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++; }
192 switch((CHAR)*p)
193 {
194 case 'c':
195 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
196 break;
197 case 'C':
198 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
199 break;
200 case 'd':
201 case 'i':
202 res->type = WPR_SIGNED;
203 break;
204 case 's':
205 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
206 break;
207 case 'S':
208 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
209 break;
210 case 'u':
211 res->type = WPR_UNSIGNED;
212 break;
213 case 'p':
214 res->width = 8;
215 res->flags |= WPRINTF_ZEROPAD;
216 /* fall through */
217 case 'X':
218 res->flags |= WPRINTF_UPPER_HEX;
219 /* fall through */
220 case 'x':
221 res->type = WPR_HEXA;
222 break;
223 default:
224 res->type = WPR_UNKNOWN;
225 p--; /* print format as normal char */
226 break;
227 }
228 return (INT)(p - format) + 1;
229 }
230
231
232 /***********************************************************************
233 * WPRINTF_GetLen
234 */
235 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
236 LPSTR number, UINT maxlen )
237 {
238 UINT len;
239
240 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
241 if (format->width > maxlen) format->width = maxlen;
242 switch(format->type)
243 {
244 case WPR_CHAR:
245 case WPR_WCHAR:
246 return (format->precision = 1);
247 case WPR_STRING:
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);
253 case WPR_WSTRING:
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);
259 case WPR_SIGNED:
260 len = sprintf( number, "%d", arg->int_view );
261 break;
262 case WPR_UNSIGNED:
263 len = sprintf( number, "%u", (UINT)arg->int_view );
264 break;
265 case WPR_HEXA:
266 len = sprintf( number,
267 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
268 (UINT)arg->int_view);
269 break;
270 default:
271 return 0;
272 }
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;
279 return len;
280 }
281
282 /***********************************************************************
283 * wvsnprintfA (internal)
284 */
285 static INT wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, va_list args )
286 {
287 WPRINTF_FORMAT format;
288 LPSTR p = buffer;
289 UINT i, len, sign;
290 CHAR number[20];
291 WPRINTF_DATA argData;
292
293 //TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
294
295 while (*spec && (maxlen > 1))
296 {
297 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
298 spec++;
299 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
300 spec += WPRINTF_ParseFormatA( spec, &format );
301
302 switch(format.type)
303 {
304 case WPR_WCHAR:
305 argData.wchar_view = (WCHAR)va_arg( args, int );
306 break;
307 case WPR_CHAR:
308 argData.char_view = (CHAR)va_arg( args, int );
309 break;
310 case WPR_STRING:
311 argData.lpcstr_view = va_arg( args, LPCSTR );
312 break;
313 case WPR_WSTRING:
314 argData.lpcwstr_view = va_arg( args, LPCWSTR );
315 break;
316 case WPR_HEXA:
317 case WPR_SIGNED:
318 case WPR_UNSIGNED:
319 argData.int_view = va_arg( args, INT );
320 break;
321 default:
322 argData.wchar_view = 0;
323 break;
324 }
325
326 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
327 sign = 0;
328 if (!(format.flags & WPRINTF_LEFTALIGN))
329 for (i = format.precision; i < format.width; i++, maxlen--)
330 *p++ = ' ';
331 switch(format.type)
332 {
333 case WPR_WCHAR:
334 *p++ = argData.wchar_view;
335 break;
336 case WPR_CHAR:
337 *p++ = argData.char_view;
338 break;
339 case WPR_STRING:
340 memcpy( p, argData.lpcstr_view, len );
341 p += len;
342 break;
343 case WPR_WSTRING:
344 {
345 LPCWSTR ptr = argData.lpcwstr_view;
346 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
347 }
348 break;
349 case WPR_HEXA:
350 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
351 {
352 *p++ = '0';
353 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
354 maxlen -= 2;
355 len -= 2;
356 }
357 /* fall through */
358 case WPR_SIGNED:
359 /* Transfer the sign now, just in case it will be zero-padded*/
360 if (number[0] == '-')
361 {
362 *p++ = '-';
363 sign = 1;
364 }
365 /* fall through */
366 case WPR_UNSIGNED:
367 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
368 memcpy( p, number + sign, len - sign );
369 p += len - sign;
370 break;
371 case WPR_UNKNOWN:
372 continue;
373 }
374 if (format.flags & WPRINTF_LEFTALIGN)
375 for (i = format.precision; i < format.width; i++, maxlen--)
376 *p++ = ' ';
377 maxlen -= len;
378 }
379 *p = 0;
380 //TRACE("%s\n",debugstr_a(buffer));
381 return (maxlen > 1) ? (INT)(p - buffer) : -1;
382 }
383
384
385 /***********************************************************************
386 * wvsnprintfW (internal)
387 */
388 static INT wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, va_list args )
389 {
390 WPRINTF_FORMAT format;
391 LPWSTR p = buffer;
392 UINT i, len, sign;
393 CHAR number[20];
394 WPRINTF_DATA argData;
395
396 //TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
397
398 while (*spec && (maxlen > 1))
399 {
400 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
401 spec++;
402 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
403 spec += WPRINTF_ParseFormatW( spec, &format );
404
405 switch(format.type)
406 {
407 case WPR_WCHAR:
408 argData.wchar_view = (WCHAR)va_arg( args, int );
409 break;
410 case WPR_CHAR:
411 argData.char_view = (CHAR)va_arg( args, int );
412 break;
413 case WPR_STRING:
414 argData.lpcstr_view = va_arg( args, LPCSTR );
415 break;
416 case WPR_WSTRING:
417 argData.lpcwstr_view = va_arg( args, LPCWSTR );
418 break;
419 case WPR_HEXA:
420 case WPR_SIGNED:
421 case WPR_UNSIGNED:
422 argData.int_view = va_arg( args, INT );
423 break;
424 default:
425 argData.wchar_view = 0;
426 break;
427 }
428
429 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
430 sign = 0;
431 if (!(format.flags & WPRINTF_LEFTALIGN))
432 for (i = format.precision; i < format.width; i++, maxlen--)
433 *p++ = ' ';
434 switch(format.type)
435 {
436 case WPR_WCHAR:
437 *p++ = argData.wchar_view;
438 break;
439 case WPR_CHAR:
440 *p++ = argData.char_view;
441 break;
442 case WPR_STRING:
443 {
444 LPCSTR ptr = argData.lpcstr_view;
445 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
446 }
447 break;
448 case WPR_WSTRING:
449 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
450 p += len;
451 break;
452 case WPR_HEXA:
453 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
454 {
455 *p++ = '0';
456 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
457 maxlen -= 2;
458 len -= 2;
459 }
460 /* fall through */
461 case WPR_SIGNED:
462 /* Transfer the sign now, just in case it will be zero-padded*/
463 if (number[0] == '-')
464 {
465 *p++ = '-';
466 sign = 1;
467 }
468 /* fall through */
469 case WPR_UNSIGNED:
470 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
471 for (i = sign; i < len; i++) *p++ = (WCHAR)number[i];
472 break;
473 case WPR_UNKNOWN:
474 continue;
475 }
476 if (format.flags & WPRINTF_LEFTALIGN)
477 for (i = format.precision; i < format.width; i++, maxlen--)
478 *p++ = ' ';
479 maxlen -= len;
480 }
481 *p = 0;
482 //TRACE("%s\n",debugstr_w(buffer));
483 return (maxlen > 1) ? (INT)(p - buffer) : -1;
484 }
485
486 /***********************************************************************
487 * wvsprintfA (USER32.@)
488 * @implemented
489 */
490 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, va_list args )
491 {
492 INT res = wvsnprintfA( buffer, 1024, spec, args );
493 return ( res == -1 ) ? 1024 : res;
494 }
495
496
497 /***********************************************************************
498 * wvsprintfW (USER32.@)
499 * @implemented
500 */
501 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, va_list args )
502 {
503 INT res = wvsnprintfW( buffer, 1024, spec, args );
504 return ( res == -1 ) ? 1024 : res;
505 }
506
507 /***********************************************************************
508 * wsprintfA (USER32.@)
509 * @implemented
510 */
511 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
512 {
513 va_list valist;
514 INT res;
515
516 va_start( valist, spec );
517 res = wvsnprintfA( buffer, 1024, spec, valist );
518 va_end( valist );
519 return ( res == -1 ) ? 1024 : res;
520 }
521
522 /***********************************************************************
523 * wsprintfW (USER32.@)
524 * @implemented
525 */
526 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
527 {
528 va_list valist;
529 INT res;
530
531 va_start( valist, spec );
532 res = wvsnprintfW( buffer, 1024, spec, valist );
533 va_end( valist );
534 return ( res == -1 ) ? 1024 : res;
535 }
536 /*
537 * @implemented
538 */
539 DWORD STDCALL WCSToMBEx(WORD CodePage,LPWSTR UnicodeString,LONG UnicodeSize,LPSTR *MBString,LONG MBSize,BOOL Allocate)
540 {
541 DWORD Size;
542 if (UnicodeSize == -1)
543 {
544 UnicodeSize = wcslen(UnicodeString)+1;
545 }
546 if (MBSize == -1)
547 {
548 if (!Allocate)
549 {
550 return 0;
551 }
552 MBSize = UnicodeSize * 2;
553 }
554 if (Allocate)
555 {
556 *MBString = RtlAllocateHeap(GetProcessHeap(), 0, MBSize);
557 }
558 if (CodePage == 0)
559 {
560 RtlUnicodeToMultiByteN(*MBString,MBSize,&Size,UnicodeString,UnicodeSize);
561 }
562 else
563 {
564 WideCharToMultiByte(CodePage,0,UnicodeString,UnicodeSize,*MBString,MBSize,0,0);
565 }
566 return UnicodeSize;
567 }
568 /*
569 * @implemented
570 */
571 DWORD STDCALL MBToWCSEx(WORD CodePage,LPSTR MBString,LONG MBSize,LPWSTR *UnicodeString,LONG UnicodeSize,BOOL Allocate)
572 {
573 DWORD Size;
574 if (MBSize == -1)
575 {
576 MBSize = strlen(MBString)+1;
577 }
578 if (UnicodeSize == -1)
579 {
580 if (!Allocate)
581 {
582 return 0;
583 }
584 UnicodeSize = MBSize;
585 }
586 if (Allocate)
587 {
588 *UnicodeString = RtlAllocateHeap(GetProcessHeap(), 0, UnicodeSize);
589 }
590 UnicodeSize *= sizeof(WCHAR);
591 if (CodePage == 0)
592 {
593 RtlMultiByteToUnicodeN(*UnicodeString,UnicodeSize,&Size,MBString,MBSize);
594 }
595 else
596 {
597 Size = MultiByteToWideChar(CodePage,0,MBString,MBSize,*UnicodeString,UnicodeSize);
598 }
599 return Size;
600 }