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