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