Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / user / wsprintf.c
blobf539bb585da409c247717aee57153c73eb6f0b85
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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/winbase16.h"
35 #include "wine/winuser16.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(string);
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) */
50 typedef enum
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;
62 typedef struct
64 UINT flags;
65 UINT width;
66 UINT precision;
67 WPRINTF_TYPE type;
68 } WPRINTF_FORMAT;
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;
78 static const CHAR null_stringA[] = "(null)";
79 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
81 /***********************************************************************
82 * WPRINTF_ParseFormatA
84 * Parse a format specification. A format specification has the form:
86 * [-][#][0][width][.precision]type
88 * Return value is the length of the format specification in characters.
90 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
92 LPCSTR p = format;
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 */
102 res->width = res->width * 10 + *p - '0';
103 p++;
105 if (*p == '.') /* precision field */
107 p++;
108 while ((*p >= '0') && (*p <= '9'))
110 res->precision = res->precision * 10 + *p - '0';
111 p++;
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)
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;
153 return (INT)(p - format) + 1;
157 /***********************************************************************
158 * WPRINTF_ParseFormatW
160 * Parse a format specification. A format specification has the form:
162 * [-][#][0][width][.precision]type
164 * Return value is the length of the format specification in characters.
166 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
168 LPCWSTR p = format;
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 */
178 res->width = res->width * 10 + *p - '0';
179 p++;
181 if (*p == '.') /* precision field */
183 p++;
184 while ((*p >= '0') && (*p <= '9'))
186 res->precision = res->precision * 10 + *p - '0';
187 p++;
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)
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;
229 return (INT)(p - format) + 1;
233 /***********************************************************************
234 * WPRINTF_GetLen
236 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
237 LPSTR number, UINT maxlen )
239 UINT len;
241 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
242 if (format->width > maxlen) format->width = maxlen;
243 switch(format->type)
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;
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;
284 /***********************************************************************
285 * wvsnprintf16 (Not a Windows API)
287 static INT16 wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec, VA_LIST16 args )
289 WPRINTF_FORMAT format;
290 LPSTR p = buffer;
291 UINT i, len, sign;
292 CHAR number[20];
293 WPRINTF_DATA cur_arg;
294 SEGPTR seg_str;
296 while (*spec && (maxlen > 1))
298 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
299 spec++;
300 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
301 spec += WPRINTF_ParseFormatA( spec, &format );
302 switch(format.type)
304 case WPR_WCHAR: /* No Unicode in Win16 */
305 case WPR_CHAR:
306 cur_arg.char_view = VA_ARG16( args, CHAR );
307 break;
308 case WPR_WSTRING: /* No Unicode in Win16 */
309 case WPR_STRING:
310 seg_str = VA_ARG16( args, SEGPTR );
311 if (IsBadReadPtr16(seg_str, 1 )) cur_arg.lpcstr_view = "";
312 else cur_arg.lpcstr_view = MapSL( seg_str );
313 break;
314 case WPR_SIGNED:
315 if (!(format.flags & WPRINTF_LONG))
317 cur_arg.int_view = VA_ARG16( args, INT16 );
318 break;
320 /* fall through */
321 case WPR_HEXA:
322 case WPR_UNSIGNED:
323 if (format.flags & WPRINTF_LONG)
324 cur_arg.int_view = VA_ARG16( args, UINT );
325 else
326 cur_arg.int_view = VA_ARG16( args, UINT16 );
327 break;
328 case WPR_UNKNOWN:
329 continue;
331 len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
332 sign = 0;
333 if (!(format.flags & WPRINTF_LEFTALIGN))
334 for (i = format.precision; i < format.width; i++, maxlen--)
335 *p++ = ' ';
336 switch(format.type)
338 case WPR_WCHAR: /* No Unicode in Win16 */
339 case WPR_CHAR:
340 *p= cur_arg.char_view;
341 /* wsprintf16 (unlike wsprintf) ignores null characters */
342 if (*p != '\0') p++;
343 else if (format.width > 1) *p++ = ' ';
344 else len = 0;
345 break;
346 case WPR_WSTRING: /* No Unicode in Win16 */
347 case WPR_STRING:
348 if (len) memcpy( p, cur_arg.lpcstr_view, len );
349 p += len;
350 break;
351 case WPR_HEXA:
352 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
354 *p++ = '0';
355 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
356 maxlen -= 2;
357 len -= 2;
359 /* fall through */
360 case WPR_SIGNED:
361 /* Transfer the sign now, just in case it will be zero-padded*/
362 if (number[0] == '-')
364 *p++ = '-';
365 sign = 1;
367 /* fall through */
368 case WPR_UNSIGNED:
369 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
370 if (len > sign) memcpy( p, number + sign, len - sign );
371 p += len-sign;
372 break;
373 case WPR_UNKNOWN:
374 continue;
376 if (format.flags & WPRINTF_LEFTALIGN)
377 for (i = format.precision; i < format.width; i++, maxlen--)
378 *p++ = ' ';
379 maxlen -= len;
381 *p = 0;
382 return (maxlen > 1) ? (INT)(p - buffer) : -1;
386 /***********************************************************************
387 * wvsnprintfA (internal)
389 static INT wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, va_list args )
391 WPRINTF_FORMAT format;
392 LPSTR p = buffer;
393 UINT i, len, sign;
394 CHAR number[20];
395 WPRINTF_DATA argData;
397 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
399 while (*spec && (maxlen > 1))
401 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
402 spec++;
403 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
404 spec += WPRINTF_ParseFormatA( spec, &format );
406 switch(format.type)
408 case WPR_WCHAR:
409 argData.wchar_view = (WCHAR)va_arg( args, int );
410 break;
411 case WPR_CHAR:
412 argData.char_view = (CHAR)va_arg( args, int );
413 break;
414 case WPR_STRING:
415 argData.lpcstr_view = va_arg( args, LPCSTR );
416 break;
417 case WPR_WSTRING:
418 argData.lpcwstr_view = va_arg( args, LPCWSTR );
419 break;
420 case WPR_HEXA:
421 case WPR_SIGNED:
422 case WPR_UNSIGNED:
423 argData.int_view = va_arg( args, INT );
424 break;
425 default:
426 argData.wchar_view = 0;
427 break;
430 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
431 sign = 0;
432 if (!(format.flags & WPRINTF_LEFTALIGN))
433 for (i = format.precision; i < format.width; i++, maxlen--)
434 *p++ = ' ';
435 switch(format.type)
437 case WPR_WCHAR:
438 *p++ = argData.wchar_view;
439 break;
440 case WPR_CHAR:
441 *p++ = argData.char_view;
442 break;
443 case WPR_STRING:
444 memcpy( p, argData.lpcstr_view, len );
445 p += len;
446 break;
447 case WPR_WSTRING:
449 LPCWSTR ptr = argData.lpcwstr_view;
450 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
452 break;
453 case WPR_HEXA:
454 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
456 *p++ = '0';
457 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
458 maxlen -= 2;
459 len -= 2;
461 /* fall through */
462 case WPR_SIGNED:
463 /* Transfer the sign now, just in case it will be zero-padded*/
464 if (number[0] == '-')
466 *p++ = '-';
467 sign = 1;
469 /* fall through */
470 case WPR_UNSIGNED:
471 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
472 memcpy( p, number + sign, len - sign );
473 p += len - sign;
474 break;
475 case WPR_UNKNOWN:
476 continue;
478 if (format.flags & WPRINTF_LEFTALIGN)
479 for (i = format.precision; i < format.width; i++, maxlen--)
480 *p++ = ' ';
481 maxlen -= len;
483 *p = 0;
484 TRACE("%s\n",debugstr_a(buffer));
485 return (maxlen > 1) ? (INT)(p - buffer) : -1;
489 /***********************************************************************
490 * wvsnprintfW (internal)
492 static INT wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, va_list args )
494 WPRINTF_FORMAT format;
495 LPWSTR p = buffer;
496 UINT i, len, sign;
497 CHAR number[20];
498 WPRINTF_DATA argData;
500 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
502 while (*spec && (maxlen > 1))
504 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
505 spec++;
506 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
507 spec += WPRINTF_ParseFormatW( spec, &format );
509 switch(format.type)
511 case WPR_WCHAR:
512 argData.wchar_view = (WCHAR)va_arg( args, int );
513 break;
514 case WPR_CHAR:
515 argData.char_view = (CHAR)va_arg( args, int );
516 break;
517 case WPR_STRING:
518 argData.lpcstr_view = va_arg( args, LPCSTR );
519 break;
520 case WPR_WSTRING:
521 argData.lpcwstr_view = va_arg( args, LPCWSTR );
522 break;
523 case WPR_HEXA:
524 case WPR_SIGNED:
525 case WPR_UNSIGNED:
526 argData.int_view = va_arg( args, INT );
527 break;
528 default:
529 argData.wchar_view = 0;
530 break;
533 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
534 sign = 0;
535 if (!(format.flags & WPRINTF_LEFTALIGN))
536 for (i = format.precision; i < format.width; i++, maxlen--)
537 *p++ = ' ';
538 switch(format.type)
540 case WPR_WCHAR:
541 *p++ = argData.wchar_view;
542 break;
543 case WPR_CHAR:
544 *p++ = argData.char_view;
545 break;
546 case WPR_STRING:
548 LPCSTR ptr = argData.lpcstr_view;
549 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
551 break;
552 case WPR_WSTRING:
553 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
554 p += len;
555 break;
556 case WPR_HEXA:
557 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
559 *p++ = '0';
560 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
561 maxlen -= 2;
562 len -= 2;
564 /* fall through */
565 case WPR_SIGNED:
566 /* Transfer the sign now, just in case it will be zero-padded*/
567 if (number[0] == '-')
569 *p++ = '-';
570 sign = 1;
572 /* fall through */
573 case WPR_UNSIGNED:
574 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
575 for (i = sign; i < len; i++) *p++ = (WCHAR)number[i];
576 break;
577 case WPR_UNKNOWN:
578 continue;
580 if (format.flags & WPRINTF_LEFTALIGN)
581 for (i = format.precision; i < format.width; i++, maxlen--)
582 *p++ = ' ';
583 maxlen -= len;
585 *p = 0;
586 TRACE("%s\n",debugstr_w(buffer));
587 return (maxlen > 1) ? (INT)(p - buffer) : -1;
591 /***********************************************************************
592 * wvsprintf (USER.421)
594 INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, VA_LIST16 args )
596 INT16 res;
598 TRACE("for %p got:\n",buffer);
599 res = wvsnprintf16( buffer, 1024, spec, args );
600 return ( res == -1 ) ? 1024 : res;
604 /***********************************************************************
605 * wvsprintfA (USER32.@)
607 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, va_list args )
609 INT res = wvsnprintfA( buffer, 1024, spec, args );
610 return ( res == -1 ) ? 1024 : res;
614 /***********************************************************************
615 * wvsprintfW (USER32.@)
617 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, va_list args )
619 INT res = wvsnprintfW( buffer, 1024, spec, args );
620 return ( res == -1 ) ? 1024 : res;
624 /***********************************************************************
625 * _wsprintf (USER.420)
627 INT16 WINAPIV wsprintf16( LPSTR buffer, LPCSTR spec, VA_LIST16 valist )
629 INT16 res;
631 res = wvsnprintf16( buffer, 1024, spec, valist );
632 return ( res == -1 ) ? 1024 : res;
636 /***********************************************************************
637 * wsprintfA (USER32.@)
639 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
641 va_list valist;
642 INT res;
644 va_start( valist, spec );
645 res = wvsnprintfA( buffer, 1024, spec, valist );
646 va_end( valist );
647 return ( res == -1 ) ? 1024 : res;
651 /***********************************************************************
652 * wsprintfW (USER32.@)
654 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
656 va_list valist;
657 INT res;
659 va_start( valist, spec );
660 res = wvsnprintfW( buffer, 1024, spec, valist );
661 va_end( valist );
662 return ( res == -1 ) ? 1024 : res;