]> git.saurik.com Git - wxWidgets.git/blame - src/common/wxprintf.cpp
normalize printf/scanf format strings correctly on all platforms, while accounting...
[wxWidgets.git] / src / common / wxprintf.cpp
CommitLineData
c9e089e9 1/////////////////////////////////////////////////////////////////////////////
dd0ef332
VS
2// Name: src/common/wxprintf.cpp
3// Purpose: wxWidgets wxPrintf() implementation
247c23b4
VZ
4// Author: Ove Kaven
5// Modified by: Ron Lee, Francesco Montorsi
c9e089e9
OK
6// Created: 09/04/99
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets copyright
65571936 9// Licence: wxWindows licence
c9e089e9
OK
10/////////////////////////////////////////////////////////////////////////////
11
c9e089e9
OK
12// ===========================================================================
13// headers, declarations, constants
14// ===========================================================================
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
8898456d 20 #pragma hdrstop
c9e089e9
OK
21#endif
22
3a3dde0d 23#include "wx/crt.h"
5ff14574 24
c9e089e9 25#include <string.h>
1c193821 26
c9e089e9 27#ifndef WX_PRECOMP
8898456d
WS
28 #include "wx/string.h"
29 #include "wx/hash.h"
5ff14574
PC
30 #include "wx/utils.h" // for wxMin and wxMax
31 #include "wx/log.h"
c9e089e9
OK
32#endif
33
31907d03 34#if defined(__MWERKS__) && __MSL__ >= 0x6000
52cbcda3 35namespace std {}
31907d03
SC
36using namespace std ;
37#endif
38
434d2cb3 39
f6f5941b 40// ============================================================================
dd0ef332 41// printf() implementation
f6f5941b
VZ
42// ============================================================================
43
df17b887
VZ
44// special test mode: define all functions below even if we don't really need
45// them to be able to test them
46#ifdef wxTEST_PRINTF
50e27899 47 #undef wxCRT_VsnprintfW
dd25c6ee 48 #undef wxCRT_VsnprintfA
df17b887
VZ
49#endif
50
f6f5941b
VZ
51// ----------------------------------------------------------------------------
52// implement [v]snprintf() if the system doesn't provide a safe one
7a828c7f
VZ
53// or if the system's one does not support positional parameters
54// (very useful for i18n purposes)
f6f5941b
VZ
55// ----------------------------------------------------------------------------
56
dd25c6ee
VS
57// ----------------------------------------------------------------------------
58// common code for both ANSI and Unicode versions
59// ----------------------------------------------------------------------------
7a828c7f 60
50e27899 61#if !defined(wxCRT_VsnprintfW) || !defined(wxCRT_VsnprintfA)
f2bbe5b6 62
50e27899 63// wxUSE_STRUTILS says our wxCRT_VsnprintfW implementation to use or not to
7a828c7f
VZ
64// use wxStrlen and wxStrncpy functions over one-char processing loops.
65//
66// Some benchmarking revealed that wxUSE_STRUTILS == 1 has the following
67// effects:
68// -> on Windows:
69// when in ANSI mode, this setting does not change almost anything
70// when in Unicode mode, it gives ~ 50% of slowdown !
71// -> on Linux:
72// both in ANSI and Unicode mode it gives ~ 60% of speedup !
73//
74#if defined(WIN32) && wxUSE_UNICODE
75#define wxUSE_STRUTILS 0
76#else
77#define wxUSE_STRUTILS 1
78#endif
79
80// some limits of our implementation
91e9bcc9 81#define wxMAX_SVNPRINTF_ARGUMENTS 64
7a828c7f 82#define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32
297eb8e6
RR
83#define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512
84
644241e1
MW
85// prefer snprintf over sprintf
86#if defined(__VISUALC__) || \
87 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
88 #define system_sprintf(buff, max, flags, data) \
89 ::_snprintf(buff, max, flags, data)
90#elif defined(HAVE_SNPRINTF)
91 #define system_sprintf(buff, max, flags, data) \
92 ::snprintf(buff, max, flags, data)
93#else // NB: at least sprintf() should always be available
94 // since 'max' is not used in this case, wxVsnprintf() should always
95 // ensure that 'buff' is big enough for all common needs
96 // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN)
97 #define system_sprintf(buff, max, flags, data) \
98 ::sprintf(buff, flags, data)
99
100 #define SYSTEM_SPRINTF_IS_UNSAFE
101#endif
7a828c7f 102
dd25c6ee
VS
103namespace
104{
105
50e27899 106// the conversion specifiers accepted by wxCRT_VsnprintfW
7a828c7f
VZ
107enum wxPrintfArgType {
108 wxPAT_INVALID = -1,
109
110 wxPAT_INT, // %d, %i, %o, %u, %x, %X
111 wxPAT_LONGINT, // %ld, etc
b726328b 112#ifdef wxLongLong_t
7a828c7f
VZ
113 wxPAT_LONGLONGINT, // %Ld, etc
114#endif
115 wxPAT_SIZET, // %Zd, etc
116
117 wxPAT_DOUBLE, // %e, %E, %f, %g, %G
118 wxPAT_LONGDOUBLE, // %le, etc
119
120 wxPAT_POINTER, // %p
121
122 wxPAT_CHAR, // %hc (in ANSI mode: %c, too)
123 wxPAT_WCHAR, // %lc (in Unicode mode: %c, too)
124
125 wxPAT_PCHAR, // %s (related to a char *)
126 wxPAT_PWCHAR, // %s (related to a wchar_t *)
127
128 wxPAT_NINT, // %n
129 wxPAT_NSHORTINT, // %hn
130 wxPAT_NLONGINT // %ln
131};
132
50e27899 133// an argument passed to wxCRT_VsnprintfW
7a828c7f
VZ
134typedef union {
135 int pad_int; // %d, %i, %o, %u, %x, %X
136 long int pad_longint; // %ld, etc
b726328b 137#ifdef wxLongLong_t
6f8415ca 138 wxLongLong_t pad_longlongint; // %Ld, etc
7a828c7f
VZ
139#endif
140 size_t pad_sizet; // %Zd, etc
141
142 double pad_double; // %e, %E, %f, %g, %G
143 long double pad_longdouble; // %le, etc
144
145 void *pad_pointer; // %p
146
147 char pad_char; // %hc (in ANSI mode: %c, too)
148 wchar_t pad_wchar; // %lc (in Unicode mode: %c, too)
149
2523e9b7 150 void *pad_str; // %s
7a828c7f
VZ
151
152 int *pad_nint; // %n
153 short int *pad_nshortint; // %hn
154 long int *pad_nlongint; // %ln
155} wxPrintfArg;
156
dd25c6ee
VS
157// helper for converting string into either char* or wchar_t* dependening
158// on the type of wxPrintfConvSpec<T> instantiation:
159template<typename CharType> struct wxPrintfStringHelper {};
160
161template<> struct wxPrintfStringHelper<char>
162{
163 typedef const wxWX2MBbuf ConvertedType;
164 static ConvertedType Convert(const wxString& s) { return s.mb_str(); }
165};
166
167template<> struct wxPrintfStringHelper<wchar_t>
168{
169 typedef const wxWX2WCbuf ConvertedType;
170 static ConvertedType Convert(const wxString& s) { return s.wc_str(); }
171};
172
7a828c7f
VZ
173
174// Contains parsed data relative to a conversion specifier given to
50e27899 175// wxCRT_VsnprintfW and parsed from the format string
7a828c7f
VZ
176// NOTE: in C++ there is almost no difference between struct & classes thus
177// there is no performance gain by using a struct here...
dd25c6ee 178template<typename CharType>
7a828c7f 179class wxPrintfConvSpec
f6f5941b 180{
7a828c7f 181public:
f6f5941b 182
7a828c7f 183 // the position of the argument relative to this conversion specifier
412a5c57 184 size_t m_pos;
7a828c7f
VZ
185
186 // the type of this conversion specifier
412a5c57 187 wxPrintfArgType m_type;
7a828c7f
VZ
188
189 // the minimum and maximum width
190 // when one of this var is set to -1 it means: use the following argument
191 // in the stack as minimum/maximum width for this conversion specifier
412a5c57 192 int m_nMinWidth, m_nMaxWidth;
5f1d3069 193
7a828c7f 194 // does the argument need to the be aligned to left ?
412a5c57 195 bool m_bAlignLeft;
7a828c7f
VZ
196
197 // pointer to the '%' of this conversion specifier in the format string
198 // NOTE: this points somewhere in the string given to the Parse() function -
199 // it's task of the caller ensure that memory is still valid !
dd25c6ee 200 const CharType *m_pArgPos;
7a828c7f
VZ
201
202 // pointer to the last character of this conversion specifier in the
203 // format string
204 // NOTE: this points somewhere in the string given to the Parse() function -
205 // it's task of the caller ensure that memory is still valid !
dd25c6ee 206 const CharType *m_pArgEnd;
7a828c7f
VZ
207
208 // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse()
209 // for use in Process()
412a5c57 210 // NB: even if this buffer is used only for numeric conversion specifiers and
52de37c7 211 // thus could be safely declared as a char[] buffer, we want it to be wchar_t
412a5c57
VZ
212 // so that in Unicode builds we can avoid to convert its contents to Unicode
213 // chars when copying it in user's buffer.
644241e1 214 char m_szFlags[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
7a828c7f
VZ
215
216
217public:
218
219 // we don't declare this as a constructor otherwise it would be called
50e27899 220 // automatically and we don't want this: to be optimized, wxCRT_VsnprintfW
7a828c7f
VZ
221 // calls this function only on really-used instances of this class.
222 void Init();
223
224 // Parses the first conversion specifier in the given string, which must
225 // begin with a '%'. Returns false if the first '%' does not introduce a
226 // (valid) conversion specifier and thus should be ignored.
dd25c6ee 227 bool Parse(const CharType *format);
7a828c7f
VZ
228
229 // Process this conversion specifier and puts the result in the given
230 // buffer. Returns the number of characters written in 'buf' or -1 if
231 // there's not enough space.
dd25c6ee 232 int Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written);
7a828c7f
VZ
233
234 // Loads the argument of this conversion specifier from given va_list.
235 bool LoadArg(wxPrintfArg *p, va_list &argptr);
236
237private:
238 // An helper function of LoadArg() which is used to handle the '*' flag
239 void ReplaceAsteriskWith(int w);
240};
241
dd25c6ee
VS
242template<typename CharType>
243void wxPrintfConvSpec<CharType>::Init()
7a828c7f 244{
412a5c57
VZ
245 m_nMinWidth = 0;
246 m_nMaxWidth = 0xFFFF;
247 m_pos = 0;
248 m_bAlignLeft = false;
249 m_pArgPos = m_pArgEnd = NULL;
250 m_type = wxPAT_INVALID;
251
252 // this character will never be removed from m_szFlags array and
7d70c309 253 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
644241e1 254 m_szFlags[0] = '%';
7a828c7f
VZ
255}
256
dd25c6ee
VS
257template<typename CharType>
258bool wxPrintfConvSpec<CharType>::Parse(const CharType *format)
7a828c7f
VZ
259{
260 bool done = false;
261
262 // temporary parse data
263 size_t flagofs = 1;
b726328b
VZ
264 bool in_prec, // true if we found the dot in some previous iteration
265 prec_dot; // true if the dot has been already added to m_szFlags
7a828c7f
VZ
266 int ilen = 0;
267
412a5c57
VZ
268 m_bAlignLeft = in_prec = prec_dot = false;
269 m_pArgPos = m_pArgEnd = format;
7a828c7f 270 do
f6f5941b 271 {
7a828c7f
VZ
272#define CHECK_PREC \
273 if (in_prec && !prec_dot) \
274 { \
644241e1 275 m_szFlags[flagofs++] = '.'; \
7a828c7f
VZ
276 prec_dot = true; \
277 }
df17b887 278
7a828c7f 279 // what follows '%'?
dd25c6ee 280 const CharType ch = *(++m_pArgEnd);
7a828c7f 281 switch ( ch )
df17b887 282 {
7a828c7f
VZ
283 case wxT('\0'):
284 return false; // not really an argument
285
286 case wxT('%'):
287 return false; // not really an argument
288
289 case wxT('#'):
290 case wxT('0'):
291 case wxT(' '):
292 case wxT('+'):
293 case wxT('\''):
294 CHECK_PREC
644241e1 295 m_szFlags[flagofs++] = char(ch);
7a828c7f
VZ
296 break;
297
298 case wxT('-'):
299 CHECK_PREC
412a5c57 300 m_bAlignLeft = true;
644241e1 301 m_szFlags[flagofs++] = char(ch);
7a828c7f
VZ
302 break;
303
304 case wxT('.'):
305 CHECK_PREC
306 in_prec = true;
307 prec_dot = false;
412a5c57
VZ
308 m_nMaxWidth = 0;
309 // dot will be auto-added to m_szFlags if non-negative
7a828c7f
VZ
310 // number follows
311 break;
312
313 case wxT('h'):
314 ilen = -1;
315 CHECK_PREC
644241e1 316 m_szFlags[flagofs++] = char(ch);
7a828c7f
VZ
317 break;
318
319 case wxT('l'):
412a5c57
VZ
320 // NB: it's safe to use flagofs-1 as flagofs always start from 1
321 if (m_szFlags[flagofs-1] == 'l') // 'll' modifier is the same as 'L' or 'q'
322 ilen = 2;
323 else
7a828c7f
VZ
324 ilen = 1;
325 CHECK_PREC
644241e1 326 m_szFlags[flagofs++] = char(ch);
7a828c7f
VZ
327 break;
328
329 case wxT('q'):
330 case wxT('L'):
331 ilen = 2;
332 CHECK_PREC
644241e1 333 m_szFlags[flagofs++] = char(ch);
7a828c7f 334 break;
b726328b
VZ
335#ifdef __WXMSW__
336 // under Windows we support the special '%I64' notation as longlong
337 // integer conversion specifier for MSVC compatibility
338 // (it behaves exactly as '%lli' or '%Li' or '%qi')
339 case wxT('I'):
340 if (*(m_pArgEnd+1) != wxT('6') ||
341 *(m_pArgEnd+2) != wxT('4'))
342 return false; // bad format
343
344 m_pArgEnd++;
345 m_pArgEnd++;
346
347 ilen = 2;
348 CHECK_PREC
349 m_szFlags[flagofs++] = char(ch);
350 m_szFlags[flagofs++] = '6';
351 m_szFlags[flagofs++] = '4';
352 break;
353#endif // __WXMSW__
7a828c7f
VZ
354
355 case wxT('Z'):
356 ilen = 3;
357 CHECK_PREC
644241e1 358 m_szFlags[flagofs++] = char(ch);
7a828c7f
VZ
359 break;
360
361 case wxT('*'):
362 if (in_prec)
363 {
364 CHECK_PREC
f6f5941b 365
7a828c7f
VZ
366 // tell Process() to use the next argument
367 // in the stack as maxwidth...
412a5c57 368 m_nMaxWidth = -1;
7a828c7f
VZ
369 }
370 else
371 {
372 // tell Process() to use the next argument
373 // in the stack as minwidth...
412a5c57 374 m_nMinWidth = -1;
7a828c7f
VZ
375 }
376
377 // save the * in our formatting buffer...
378 // will be replaced later by Process()
644241e1 379 m_szFlags[flagofs++] = char(ch);
7a828c7f
VZ
380 break;
381
382 case wxT('1'): case wxT('2'): case wxT('3'):
383 case wxT('4'): case wxT('5'): case wxT('6'):
384 case wxT('7'): case wxT('8'): case wxT('9'):
385 {
386 int len = 0;
387 CHECK_PREC
dd25c6ee
VS
388 while ( (*m_pArgEnd >= CharType('0')) &&
389 (*m_pArgEnd <= CharType('9')) )
7a828c7f 390 {
644241e1 391 m_szFlags[flagofs++] = char(*m_pArgEnd);
412a5c57
VZ
392 len = len*10 + (*m_pArgEnd - wxT('0'));
393 m_pArgEnd++;
7a828c7f
VZ
394 }
395
396 if (in_prec)
412a5c57 397 m_nMaxWidth = len;
7a828c7f 398 else
412a5c57 399 m_nMinWidth = len;
7a828c7f 400
412a5c57 401 m_pArgEnd--; // the main loop pre-increments n again
f6f5941b 402 }
7a828c7f
VZ
403 break;
404
405 case wxT('$'): // a positional parameter (e.g. %2$s) ?
406 {
412a5c57 407 if (m_nMinWidth <= 0)
7a828c7f
VZ
408 break; // ignore this formatting flag as no
409 // numbers are preceding it
410
412a5c57 411 // remove from m_szFlags all digits previously added
7a828c7f
VZ
412 do {
413 flagofs--;
412a5c57
VZ
414 } while (m_szFlags[flagofs] >= '1' &&
415 m_szFlags[flagofs] <= '9');
7a828c7f
VZ
416
417 // re-adjust the offset making it point to the
412a5c57 418 // next free char of m_szFlags
7a828c7f
VZ
419 flagofs++;
420
412a5c57
VZ
421 m_pos = m_nMinWidth;
422 m_nMinWidth = 0;
7a828c7f
VZ
423 }
424 break;
425
426 case wxT('d'):
427 case wxT('i'):
428 case wxT('o'):
429 case wxT('u'):
430 case wxT('x'):
431 case wxT('X'):
432 CHECK_PREC
644241e1
MW
433 m_szFlags[flagofs++] = char(ch);
434 m_szFlags[flagofs] = '\0';
7a828c7f 435 if (ilen == 0)
412a5c57 436 m_type = wxPAT_INT;
7a828c7f
VZ
437 else if (ilen == -1)
438 // NB: 'short int' value passed through '...'
439 // is promoted to 'int', so we have to get
440 // an int from stack even if we need a short
412a5c57 441 m_type = wxPAT_INT;
7a828c7f 442 else if (ilen == 1)
412a5c57 443 m_type = wxPAT_LONGINT;
7a828c7f 444 else if (ilen == 2)
b726328b 445#ifdef wxLongLong_t
412a5c57 446 m_type = wxPAT_LONGLONGINT;
6f8415ca 447#else // !wxLongLong_t
412a5c57 448 m_type = wxPAT_LONGINT;
6f8415ca 449#endif // wxLongLong_t/!wxLongLong_t
7a828c7f 450 else if (ilen == 3)
412a5c57 451 m_type = wxPAT_SIZET;
7a828c7f
VZ
452 done = true;
453 break;
454
455 case wxT('e'):
456 case wxT('E'):
457 case wxT('f'):
458 case wxT('g'):
459 case wxT('G'):
460 CHECK_PREC
644241e1
MW
461 m_szFlags[flagofs++] = char(ch);
462 m_szFlags[flagofs] = '\0';
7a828c7f 463 if (ilen == 2)
412a5c57 464 m_type = wxPAT_LONGDOUBLE;
7a828c7f 465 else
412a5c57 466 m_type = wxPAT_DOUBLE;
7a828c7f
VZ
467 done = true;
468 break;
469
470 case wxT('p'):
412a5c57 471 m_type = wxPAT_POINTER;
644241e1 472 m_szFlags[flagofs++] = char(ch);
4c268e6a 473 m_szFlags[flagofs] = '\0';
7a828c7f
VZ
474 done = true;
475 break;
476
477 case wxT('c'):
478 if (ilen == -1)
479 {
480 // in Unicode mode %hc == ANSI character
481 // and in ANSI mode, %hc == %c == ANSI...
412a5c57 482 m_type = wxPAT_CHAR;
7a828c7f
VZ
483 }
484 else if (ilen == 1)
485 {
486 // in ANSI mode %lc == Unicode character
487 // and in Unicode mode, %lc == %c == Unicode...
412a5c57 488 m_type = wxPAT_WCHAR;
7a828c7f
VZ
489 }
490 else
491 {
492#if wxUSE_UNICODE
493 // in Unicode mode, %c == Unicode character
412a5c57 494 m_type = wxPAT_WCHAR;
7a828c7f
VZ
495#else
496 // in ANSI mode, %c == ANSI character
412a5c57 497 m_type = wxPAT_CHAR;
7a828c7f
VZ
498#endif
499 }
500 done = true;
501 break;
502
503 case wxT('s'):
504 if (ilen == -1)
505 {
506 // Unicode mode wx extension: we'll let %hs mean non-Unicode
507 // strings (when in ANSI mode, %s == %hs == ANSI string)
412a5c57 508 m_type = wxPAT_PCHAR;
7a828c7f
VZ
509 }
510 else if (ilen == 1)
511 {
512 // in Unicode mode, %ls == %s == Unicode string
513 // in ANSI mode, %ls == Unicode string
412a5c57 514 m_type = wxPAT_PWCHAR;
7a828c7f
VZ
515 }
516 else
517 {
518#if wxUSE_UNICODE
412a5c57 519 m_type = wxPAT_PWCHAR;
7a828c7f 520#else
412a5c57 521 m_type = wxPAT_PCHAR;
7a828c7f
VZ
522#endif
523 }
524 done = true;
525 break;
526
527 case wxT('n'):
528 if (ilen == 0)
412a5c57 529 m_type = wxPAT_NINT;
7a828c7f 530 else if (ilen == -1)
412a5c57 531 m_type = wxPAT_NSHORTINT;
7a828c7f 532 else if (ilen >= 1)
412a5c57 533 m_type = wxPAT_NLONGINT;
7a828c7f
VZ
534 done = true;
535 break;
536
537 default:
538 // bad format, don't consider this an argument;
539 // leave it unchanged
540 return false;
541 }
412a5c57
VZ
542
543 if (flagofs == wxMAX_SVNPRINTF_FLAGBUFFER_LEN)
544 {
545 wxLogDebug(wxT("Too many flags specified for a single conversion specifier!"));
546 return false;
547 }
7a828c7f
VZ
548 }
549 while (!done);
550
551 return true; // parsing was successful
552}
553
dd25c6ee
VS
554template<typename CharType>
555void wxPrintfConvSpec<CharType>::ReplaceAsteriskWith(int width)
7a828c7f 556{
644241e1 557 char temp[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
7a828c7f
VZ
558
559 // find the first * in our flag buffer
644241e1 560 char *pwidth = strchr(m_szFlags, '*');
b1bc4b65 561 wxCHECK_RET(pwidth, _T("field width must be specified"));
7a828c7f 562
412a5c57 563 // save what follows the * (the +1 is to skip the asterisk itself!)
644241e1 564 strcpy(temp, pwidth+1);
412a5c57
VZ
565 if (width < 0)
566 {
567 pwidth[0] = wxT('-');
7a828c7f
VZ
568 pwidth++;
569 }
570
571 // replace * with the actual integer given as width
c57cdbd8
VZ
572#ifndef SYSTEM_SPRINTF_IS_UNSAFE
573 int maxlen = (m_szFlags + wxMAX_SVNPRINTF_FLAGBUFFER_LEN - pwidth) /
644241e1 574 sizeof(*m_szFlags);
f2fac41a 575#endif
644241e1 576 int offset = system_sprintf(pwidth, maxlen, "%d", abs(width));
f2fac41a 577
7a828c7f 578 // restore after the expanded * what was following it
644241e1 579 strcpy(pwidth+offset, temp);
7a828c7f
VZ
580}
581
dd25c6ee
VS
582template<typename CharType>
583bool wxPrintfConvSpec<CharType>::LoadArg(wxPrintfArg *p, va_list &argptr)
7a828c7f
VZ
584{
585 // did the '*' width/precision specifier was used ?
412a5c57 586 if (m_nMaxWidth == -1)
7a828c7f
VZ
587 {
588 // take the maxwidth specifier from the stack
412a5c57
VZ
589 m_nMaxWidth = va_arg(argptr, int);
590 if (m_nMaxWidth < 0)
591 m_nMaxWidth = 0;
7a828c7f 592 else
412a5c57 593 ReplaceAsteriskWith(m_nMaxWidth);
7a828c7f
VZ
594 }
595
412a5c57 596 if (m_nMinWidth == -1)
7a828c7f
VZ
597 {
598 // take the minwidth specifier from the stack
412a5c57 599 m_nMinWidth = va_arg(argptr, int);
7a828c7f 600
412a5c57
VZ
601 ReplaceAsteriskWith(m_nMinWidth);
602 if (m_nMinWidth < 0)
7a828c7f 603 {
412a5c57
VZ
604 m_bAlignLeft = !m_bAlignLeft;
605 m_nMinWidth = -m_nMinWidth;
7a828c7f
VZ
606 }
607 }
608
412a5c57 609 switch (m_type) {
7a828c7f
VZ
610 case wxPAT_INT:
611 p->pad_int = va_arg(argptr, int);
612 break;
613 case wxPAT_LONGINT:
614 p->pad_longint = va_arg(argptr, long int);
615 break;
b726328b 616#ifdef wxLongLong_t
7a828c7f 617 case wxPAT_LONGLONGINT:
6f8415ca 618 p->pad_longlongint = va_arg(argptr, wxLongLong_t);
7a828c7f 619 break;
6f8415ca 620#endif // wxLongLong_t
7a828c7f
VZ
621 case wxPAT_SIZET:
622 p->pad_sizet = va_arg(argptr, size_t);
623 break;
624 case wxPAT_DOUBLE:
625 p->pad_double = va_arg(argptr, double);
626 break;
627 case wxPAT_LONGDOUBLE:
628 p->pad_longdouble = va_arg(argptr, long double);
629 break;
630 case wxPAT_POINTER:
631 p->pad_pointer = va_arg(argptr, void *);
632 break;
633
634 case wxPAT_CHAR:
5e52e029 635 p->pad_char = (char)va_arg(argptr, int); // char is promoted to int when passed through '...'
7a828c7f
VZ
636 break;
637 case wxPAT_WCHAR:
7d70c309 638 p->pad_wchar = (wchar_t)va_arg(argptr, int); // char is promoted to int when passed through '...'
7a828c7f
VZ
639 break;
640
641 case wxPAT_PCHAR:
7a828c7f 642 case wxPAT_PWCHAR:
2523e9b7 643 p->pad_str = va_arg(argptr, void *);
7a828c7f
VZ
644 break;
645
646 case wxPAT_NINT:
647 p->pad_nint = va_arg(argptr, int *);
648 break;
649 case wxPAT_NSHORTINT:
650 p->pad_nshortint = va_arg(argptr, short int *);
651 break;
652 case wxPAT_NLONGINT:
653 p->pad_nlongint = va_arg(argptr, long int *);
654 break;
655
656 case wxPAT_INVALID:
657 default:
658 return false;
659 }
660
661 return true; // loading was successful
662}
663
dd25c6ee
VS
664template<typename CharType>
665int wxPrintfConvSpec<CharType>::Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written)
7a828c7f 666{
412a5c57
VZ
667 // buffer to avoid dynamic memory allocation each time for small strings;
668 // note that this buffer is used only to hold results of number formatting,
669 // %s directly writes user's string in buf, without using szScratch
644241e1 670 char szScratch[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN];
412a5c57
VZ
671 size_t lenScratch = 0, lenCur = 0;
672
f6f5941b 673#define APPEND_CH(ch) \
210f4bcd
VS
674 { \
675 if ( lenCur == lenMax ) \
676 return -1; \
677 \
678 buf[lenCur++] = ch; \
679 }
f6f5941b 680
412a5c57 681 switch ( m_type )
7a828c7f
VZ
682 {
683 case wxPAT_INT:
297eb8e6 684 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_int);
7a828c7f
VZ
685 break;
686
687 case wxPAT_LONGINT:
297eb8e6 688 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longint);
7a828c7f
VZ
689 break;
690
b726328b 691#ifdef wxLongLong_t
7a828c7f 692 case wxPAT_LONGLONGINT:
297eb8e6 693 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longlongint);
7a828c7f
VZ
694 break;
695#endif // SIZEOF_LONG_LONG
696
697 case wxPAT_SIZET:
297eb8e6 698 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_sizet);
7a828c7f
VZ
699 break;
700
701 case wxPAT_LONGDOUBLE:
297eb8e6 702 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longdouble);
7a828c7f
VZ
703 break;
704
705 case wxPAT_DOUBLE:
297eb8e6 706 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_double);
7a828c7f
VZ
707 break;
708
709 case wxPAT_POINTER:
297eb8e6 710 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_pointer);
7a828c7f
VZ
711 break;
712
713 case wxPAT_CHAR:
714 case wxPAT_WCHAR:
715 {
dd25c6ee 716 wxUniChar ch;
412a5c57 717 if (m_type == wxPAT_CHAR)
dd25c6ee
VS
718 ch = p->pad_char;
719 else // m_type == wxPAT_WCHAR
720 ch = p->pad_wchar;
412a5c57 721
dd25c6ee 722 CharType val = ch;
210f4bcd 723
7a828c7f 724 size_t i;
210f4bcd 725
412a5c57
VZ
726 if (!m_bAlignLeft)
727 for (i = 1; i < (size_t)m_nMinWidth; i++)
7a828c7f 728 APPEND_CH(_T(' '));
f6f5941b 729
7a828c7f 730 APPEND_CH(val);
210f4bcd 731
412a5c57
VZ
732 if (m_bAlignLeft)
733 for (i = 1; i < (size_t)m_nMinWidth; i++)
7a828c7f
VZ
734 APPEND_CH(_T(' '));
735 }
736 break;
f6f5941b 737
7a828c7f
VZ
738 case wxPAT_PCHAR:
739 case wxPAT_PWCHAR:
740 {
2523e9b7
VS
741 wxArgNormalizedString arg(p->pad_str);
742 wxString s = arg;
412a5c57 743
2523e9b7
VS
744 if ( !arg.IsValid() && m_nMaxWidth >= 6 )
745 s = wxT("(null)");
7a828c7f 746
dd25c6ee
VS
747 typename wxPrintfStringHelper<CharType>::ConvertedType strbuf(
748 wxPrintfStringHelper<CharType>::Convert(s));
749
2523e9b7
VS
750 // at this point we are sure that m_nMaxWidth is positive or
751 // null (see top of wxPrintfConvSpec::LoadArg)
dd25c6ee 752 int len = wxMin((unsigned int)m_nMaxWidth, wxStrlen(strbuf));
7a828c7f
VZ
753
754 int i;
755
412a5c57 756 if (!m_bAlignLeft)
7a828c7f 757 {
412a5c57 758 for (i = len; i < m_nMinWidth; i++)
7a828c7f
VZ
759 APPEND_CH(_T(' '));
760 }
761
a31746c7 762 len = wxMin((unsigned int)len, lenMax-lenCur);
dd25c6ee 763 wxStrncpy(buf+lenCur, strbuf, len);
7a828c7f 764 lenCur += len;
7a828c7f 765
412a5c57 766 if (m_bAlignLeft)
7a828c7f 767 {
412a5c57 768 for (i = len; i < m_nMinWidth; i++)
7a828c7f 769 APPEND_CH(_T(' '));
f6f5941b 770 }
df17b887 771 }
7a828c7f
VZ
772 break;
773
774 case wxPAT_NINT:
e98d3205 775 *p->pad_nint = written;
7a828c7f
VZ
776 break;
777
778 case wxPAT_NSHORTINT:
e98d3205 779 *p->pad_nshortint = (short int)written;
7a828c7f
VZ
780 break;
781
782 case wxPAT_NLONGINT:
e98d3205 783 *p->pad_nlongint = written;
7a828c7f
VZ
784 break;
785
786 case wxPAT_INVALID:
787 default:
788 return -1;
789 }
790
791 // if we used system's sprintf() then we now need to append the s_szScratch
792 // buffer to the given one...
412a5c57 793 switch (m_type)
7a828c7f
VZ
794 {
795 case wxPAT_INT:
796 case wxPAT_LONGINT:
b726328b 797#ifdef wxLongLong_t
7a828c7f
VZ
798 case wxPAT_LONGLONGINT:
799#endif
800 case wxPAT_SIZET:
801 case wxPAT_LONGDOUBLE:
802 case wxPAT_DOUBLE:
803 case wxPAT_POINTER:
644241e1 804 wxASSERT(lenScratch < wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN);
dd25c6ee
VS
805 // NB: 1) we can compare lenMax (for CharType*, i.e. possibly
806 // wchar_t*) with lenScratch (char*) because this code is
807 // formatting integers and that will have the same length
808 // even in UTF-8 (the only case when char* length may be
809 // more than wchar_t* length of the same string)
810 // 2) wxStrncpy converts the 2nd argument to 1st argument's
811 // type transparently if their types differ, so this code
812 // works for both instantiations
813 if (lenMax < lenScratch)
7a828c7f 814 {
dd25c6ee
VS
815 // fill output buffer and then return -1
816 wxStrncpy(buf, szScratch, lenMax);
817 return -1;
7a828c7f 818 }
dd25c6ee
VS
819 wxStrncpy(buf, szScratch, lenScratch);
820 lenCur += lenScratch;
7a828c7f
VZ
821 break;
822
823 default:
824 break; // all other cases were completed previously
825 }
826
827 return lenCur;
828}
829
3c7f37ed
MW
830// Copy chars from source to dest converting '%%' to '%'. Takes at most maxIn
831// chars from source and write at most outMax chars to dest, returns the
832// number of chars actually written. Does not treat null specially.
dd25c6ee 833template<typename CharType>
3c7f37ed
MW
834static int wxCopyStrWithPercents(
835 size_t maxOut,
dd25c6ee 836 CharType *dest,
3c7f37ed 837 size_t maxIn,
dd25c6ee 838 const CharType *source)
247c23b4
VZ
839{
840 size_t written = 0;
841
3c7f37ed 842 if (maxIn == 0)
247c23b4
VZ
843 return 0;
844
845 size_t i;
3c7f37ed 846 for ( i = 0; i < maxIn-1 && written < maxOut; source++, i++)
247c23b4
VZ
847 {
848 dest[written++] = *source;
849 if (*(source+1) == wxT('%'))
850 {
851 // skip this additional '%' character
852 source++;
853 i++;
854 }
855 }
856
3c7f37ed 857 if (i < maxIn && written < maxOut)
247c23b4
VZ
858 // copy last character inconditionally
859 dest[written++] = *source;
860
861 return written;
862}
863
dd25c6ee
VS
864template<typename CharType>
865static int wxDoVsnprintf(CharType *buf, size_t lenMax,
866 const CharType *format, va_list argptr)
7a828c7f 867{
412a5c57
VZ
868 // useful for debugging, to understand if we are really using this function
869 // rather than the system implementation
870#if 0
50e27899 871 wprintf(L"Using wxCRT_VsnprintfW\n");
412a5c57
VZ
872#endif
873
874 // required memory:
dd25c6ee 875 wxPrintfConvSpec<CharType> arg[wxMAX_SVNPRINTF_ARGUMENTS];
412a5c57 876 wxPrintfArg argdata[wxMAX_SVNPRINTF_ARGUMENTS];
dd25c6ee 877 wxPrintfConvSpec<CharType> *pspec[wxMAX_SVNPRINTF_ARGUMENTS] = { NULL };
7a828c7f
VZ
878
879 size_t i;
880
881 // number of characters in the buffer so far, must be less than lenMax
882 size_t lenCur = 0;
883
884 size_t nargs = 0;
dd25c6ee 885 const CharType *toparse = format;
7a828c7f
VZ
886
887 // parse the format string
888 bool posarg_present = false, nonposarg_present = false;
889 for (; *toparse != wxT('\0'); toparse++)
890 {
891 if (*toparse == wxT('%') )
f6f5941b 892 {
7a828c7f
VZ
893 arg[nargs].Init();
894
895 // let's see if this is a (valid) conversion specifier...
896 if (arg[nargs].Parse(toparse))
897 {
898 // ...yes it is
dd25c6ee 899 wxPrintfConvSpec<CharType> *current = &arg[nargs];
7a828c7f
VZ
900
901 // make toparse point to the end of this specifier
412a5c57 902 toparse = current->m_pArgEnd;
7a828c7f 903
412a5c57
VZ
904 if (current->m_pos > 0)
905 {
7a828c7f 906 // the positionals start from number 1... adjust the index
412a5c57 907 current->m_pos--;
7a828c7f 908 posarg_present = true;
412a5c57
VZ
909 }
910 else
911 {
7a828c7f 912 // not a positional argument...
412a5c57 913 current->m_pos = nargs;
7a828c7f
VZ
914 nonposarg_present = true;
915 }
916
917 // this conversion specifier is tied to the pos-th argument...
412a5c57 918 pspec[current->m_pos] = current;
7a828c7f
VZ
919 nargs++;
920
921 if (nargs == wxMAX_SVNPRINTF_ARGUMENTS)
412a5c57
VZ
922 {
923 wxLogDebug(wxT("A single call to wxVsnprintf() has more than %d arguments; ")
924 wxT("ignoring all remaining arguments."), wxMAX_SVNPRINTF_ARGUMENTS);
7a828c7f 925 break; // cannot handle any additional conv spec
412a5c57
VZ
926 }
927 }
928 else
929 {
930 // it's safe to look in the next character of toparse as at worst
931 // we'll hit its \0
932 if (*(toparse+1) == wxT('%'))
933 toparse++; // the Parse() returned false because we've found a %%
7a828c7f 934 }
f6f5941b 935 }
7a828c7f 936 }
df17b887 937
7a828c7f 938 if (posarg_present && nonposarg_present)
3c7f37ed
MW
939 {
940 buf[0] = 0;
7a828c7f 941 return -1; // format strings with both positional and
3c7f37ed 942 } // non-positional conversion specifier are unsupported !!
7a828c7f 943
3aa077ce
MW
944 // on platforms where va_list is an array type, it is necessary to make a
945 // copy to be able to pass it to LoadArg as a reference.
946 bool ok = true;
947 va_list ap;
948 wxVaCopy(ap, argptr);
949
7a828c7f 950 // now load arguments from stack
412a5c57
VZ
951 for (i=0; i < nargs && ok; i++)
952 {
953 // !pspec[i] means that the user forgot a positional parameter (e.g. %$1s %$3s);
954 // LoadArg == false means that wxPrintfConvSpec::Parse failed to set the
955 // conversion specifier 'type' to a valid value...
3aa077ce 956 ok = pspec[i] && pspec[i]->LoadArg(&argdata[i], ap);
7a828c7f
VZ
957 }
958
3aa077ce
MW
959 va_end(ap);
960
247c23b4 961 // something failed while loading arguments from the variable list...
f2bbe5b6 962 // (e.g. the user repeated twice the same positional argument)
3aa077ce 963 if (!ok)
3c7f37ed
MW
964 {
965 buf[0] = 0;
3aa077ce 966 return -1;
3c7f37ed 967 }
3aa077ce 968
7a828c7f
VZ
969 // finally, process each conversion specifier with its own argument
970 toparse = format;
971 for (i=0; i < nargs; i++)
972 {
973 // copy in the output buffer the portion of the format string between
974 // last specifier and the current one
412a5c57 975 size_t tocopy = ( arg[i].m_pArgPos - toparse );
3c7f37ed
MW
976
977 lenCur += wxCopyStrWithPercents(lenMax - lenCur, buf + lenCur,
978 tocopy, toparse);
979 if (lenCur == lenMax)
412a5c57 980 {
3c7f37ed 981 buf[lenMax - 1] = 0;
f2bbe5b6 982 return lenMax+1; // not enough space in the output buffer !
412a5c57 983 }
7a828c7f 984
7a828c7f 985 // process this specifier directly in the output buffer
3c7f37ed 986 int n = arg[i].Process(buf+lenCur, lenMax - lenCur, &argdata[arg[i].m_pos], lenCur);
7a828c7f 987 if (n == -1)
412a5c57
VZ
988 {
989 buf[lenMax-1] = wxT('\0'); // be sure to always NUL-terminate the string
f2bbe5b6 990 return lenMax+1; // not enough space in the output buffer !
412a5c57 991 }
7a828c7f
VZ
992 lenCur += n;
993
412a5c57 994 // the +1 is because wxPrintfConvSpec::m_pArgEnd points to the last character
7a828c7f 995 // of the format specifier, but we are not interested to it...
412a5c57 996 toparse = arg[i].m_pArgEnd + 1;
5f1d3069
RR
997 }
998
7a828c7f
VZ
999 // copy portion of the format string after last specifier
1000 // NOTE: toparse is pointing to the character just after the last processed
1001 // conversion specifier
1002 // NOTE2: the +1 is because we want to copy also the '\0'
1003 size_t tocopy = wxStrlen(format) + 1 - ( toparse - format ) ;
247c23b4 1004
3c7f37ed
MW
1005 lenCur += wxCopyStrWithPercents(lenMax - lenCur, buf + lenCur,
1006 tocopy, toparse) - 1;
1007 if (buf[lenCur])
1008 {
1009 buf[lenCur] = 0;
f2bbe5b6 1010 return lenMax+1; // not enough space in the output buffer !
3c7f37ed 1011 }
7a828c7f 1012
13ab552e
VZ
1013 // Don't do:
1014 // wxASSERT(lenCur == wxStrlen(buf));
1015 // in fact if we embedded NULLs in the output buffer (using %c with a '\0')
1016 // such check would fail
1017
f6f5941b
VZ
1018 return lenCur;
1019}
5f1d3069 1020
f6f5941b 1021#undef APPEND_CH
f6f5941b 1022#undef CHECK_PREC
5f1d3069 1023
dd25c6ee
VS
1024} // anonymous namespace
1025
50e27899 1026#endif // !defined(wxCRT_VsnprintfW) || !defined(wxCRT_VsnprintfA)
dd25c6ee
VS
1027
1028// ----------------------------------------------------------------------------
50e27899 1029// wxCRT_VsnprintfW
dd25c6ee
VS
1030// ----------------------------------------------------------------------------
1031
50e27899 1032#if !defined(wxCRT_VsnprintfW)
dd25c6ee
VS
1033
1034#if !wxUSE_WXVSNPRINTFW
50e27899 1035 #error "wxUSE_WXVSNPRINTFW must be 1 if our wxCRT_VsnprintfW is used"
dd25c6ee
VS
1036#endif
1037
50e27899
VS
1038int wxCRT_VsnprintfW(wchar_t *buf, size_t len,
1039 const wchar_t *format, va_list argptr)
dd25c6ee
VS
1040{
1041 return wxDoVsnprintf(buf, len, format, argptr);
1042}
1043
50e27899 1044#else // wxCRT_VsnprintfW is defined
f2bbe5b6 1045
52de37c7 1046#if wxUSE_WXVSNPRINTFW
50e27899 1047 #error "wxUSE_WXVSNPRINTFW must be 0 if our wxCRT_VsnprintfW is not used"
f2bbe5b6
VZ
1048#endif
1049
50e27899 1050#endif // !wxCRT_VsnprintfW
dd25c6ee
VS
1051
1052// ----------------------------------------------------------------------------
1053// wxCRT_VsnprintfA
1054// ----------------------------------------------------------------------------
1055
1056#ifndef wxCRT_VsnprintfA
1057
1058#if !wxUSE_WXVSNPRINTFA
1059 #error "wxUSE_WXVSNPRINTFA must be 1 if our wxCRT_VsnprintfA is used"
1060#endif
1061
1062int wxCRT_VsnprintfA(char *buf, size_t len,
1063 const char *format, va_list argptr)
1064{
1065 return wxDoVsnprintf(buf, len, format, argptr);
1066}
1067
1068#else // wxCRT_VsnprintfA is defined
1069
1070#if wxUSE_WXVSNPRINTFA
1071 #error "wxUSE_WXVSNPRINTFA must be 0 if our wxCRT_VsnprintfA is not used"
1072#endif
1073
1074#endif // !wxCRT_VsnprintfA