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