]>
Commit | Line | Data |
---|---|---|
47346406 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/private/wxprintf.h | |
3 | // Purpose: wxWidgets wxPrintf() implementation | |
4 | // Author: Ove Kaven | |
5 | // Modified by: Ron Lee, Francesco Montorsi | |
6 | // Created: 09/04/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) wxWidgets copyright | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_PRIVATE_WXPRINTF_H_ | |
13 | #define _WX_PRIVATE_WXPRINTF_H_ | |
14 | ||
15 | // --------------------------------------------------------------------------- | |
16 | // headers and macros | |
17 | // --------------------------------------------------------------------------- | |
18 | ||
19 | #include "wx/crt.h" | |
05962bdd JS |
20 | #include "wx/log.h" |
21 | #include "wx/utils.h" | |
47346406 VS |
22 | |
23 | #include <string.h> | |
24 | ||
47346406 VS |
25 | // prefer snprintf over sprintf |
26 | #if defined(__VISUALC__) || \ | |
27 | (defined(__BORLANDC__) && __BORLANDC__ >= 0x540) | |
28 | #define system_sprintf(buff, max, flags, data) \ | |
29 | ::_snprintf(buff, max, flags, data) | |
30 | #elif defined(HAVE_SNPRINTF) | |
31 | #define system_sprintf(buff, max, flags, data) \ | |
32 | ::snprintf(buff, max, flags, data) | |
33 | #else // NB: at least sprintf() should always be available | |
34 | // since 'max' is not used in this case, wxVsnprintf() should always | |
35 | // ensure that 'buff' is big enough for all common needs | |
36 | // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN) | |
37 | #define system_sprintf(buff, max, flags, data) \ | |
38 | ::sprintf(buff, flags, data) | |
39 | ||
40 | #define SYSTEM_SPRINTF_IS_UNSAFE | |
41 | #endif | |
42 | ||
43 | // --------------------------------------------------------------------------- | |
44 | // printf format string parsing | |
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | // some limits of our implementation | |
48 | #define wxMAX_SVNPRINTF_ARGUMENTS 64 | |
49 | #define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32 | |
50 | #define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512 | |
51 | ||
52 | ||
53 | // the conversion specifiers accepted by wxCRT_VsnprintfW | |
75ac34ce VZ |
54 | enum wxPrintfArgType |
55 | { | |
47346406 VS |
56 | wxPAT_INVALID = -1, |
57 | ||
58 | wxPAT_INT, // %d, %i, %o, %u, %x, %X | |
59 | wxPAT_LONGINT, // %ld, etc | |
60 | #ifdef wxLongLong_t | |
61 | wxPAT_LONGLONGINT, // %Ld, etc | |
62 | #endif | |
b113edcd | 63 | wxPAT_SIZET, // %zd, etc |
47346406 VS |
64 | |
65 | wxPAT_DOUBLE, // %e, %E, %f, %g, %G | |
66 | wxPAT_LONGDOUBLE, // %le, etc | |
67 | ||
68 | wxPAT_POINTER, // %p | |
69 | ||
70 | wxPAT_CHAR, // %hc (in ANSI mode: %c, too) | |
71 | wxPAT_WCHAR, // %lc (in Unicode mode: %c, too) | |
72 | ||
73 | wxPAT_PCHAR, // %s (related to a char *) | |
74 | wxPAT_PWCHAR, // %s (related to a wchar_t *) | |
75 | ||
76 | wxPAT_NINT, // %n | |
77 | wxPAT_NSHORTINT, // %hn | |
75ac34ce VZ |
78 | wxPAT_NLONGINT, // %ln |
79 | ||
80 | wxPAT_STAR // '*' used for width or precision | |
47346406 VS |
81 | }; |
82 | ||
83 | // an argument passed to wxCRT_VsnprintfW | |
75ac34ce VZ |
84 | union wxPrintfArg |
85 | { | |
47346406 VS |
86 | int pad_int; // %d, %i, %o, %u, %x, %X |
87 | long int pad_longint; // %ld, etc | |
88 | #ifdef wxLongLong_t | |
75ac34ce | 89 | wxLongLong_t pad_longlongint; // %Ld, etc |
47346406 | 90 | #endif |
b113edcd | 91 | size_t pad_sizet; // %zd, etc |
47346406 VS |
92 | |
93 | double pad_double; // %e, %E, %f, %g, %G | |
94 | long double pad_longdouble; // %le, etc | |
95 | ||
96 | void *pad_pointer; // %p | |
97 | ||
98 | char pad_char; // %hc (in ANSI mode: %c, too) | |
99 | wchar_t pad_wchar; // %lc (in Unicode mode: %c, too) | |
100 | ||
101 | void *pad_str; // %s | |
102 | ||
103 | int *pad_nint; // %n | |
104 | short int *pad_nshortint; // %hn | |
105 | long int *pad_nlongint; // %ln | |
75ac34ce | 106 | }; |
47346406 | 107 | |
75ac34ce | 108 | // helper for converting string into either char* or wchar_t* depending |
47346406 VS |
109 | // on the type of wxPrintfConvSpec<T> instantiation: |
110 | template<typename CharType> struct wxPrintfStringHelper {}; | |
111 | ||
112 | template<> struct wxPrintfStringHelper<char> | |
113 | { | |
114 | typedef const wxWX2MBbuf ConvertedType; | |
115 | static ConvertedType Convert(const wxString& s) { return s.mb_str(); } | |
116 | }; | |
117 | ||
118 | template<> struct wxPrintfStringHelper<wchar_t> | |
119 | { | |
120 | typedef const wxWX2WCbuf ConvertedType; | |
121 | static ConvertedType Convert(const wxString& s) { return s.wc_str(); } | |
122 | }; | |
123 | ||
124 | ||
125 | // Contains parsed data relative to a conversion specifier given to | |
126 | // wxCRT_VsnprintfW and parsed from the format string | |
127 | // NOTE: in C++ there is almost no difference between struct & classes thus | |
128 | // there is no performance gain by using a struct here... | |
129 | template<typename CharType> | |
130 | class wxPrintfConvSpec | |
131 | { | |
132 | public: | |
133 | ||
134 | // the position of the argument relative to this conversion specifier | |
135 | size_t m_pos; | |
136 | ||
137 | // the type of this conversion specifier | |
138 | wxPrintfArgType m_type; | |
139 | ||
140 | // the minimum and maximum width | |
141 | // when one of this var is set to -1 it means: use the following argument | |
142 | // in the stack as minimum/maximum width for this conversion specifier | |
143 | int m_nMinWidth, m_nMaxWidth; | |
144 | ||
145 | // does the argument need to the be aligned to left ? | |
146 | bool m_bAlignLeft; | |
147 | ||
148 | // pointer to the '%' of this conversion specifier in the format string | |
149 | // NOTE: this points somewhere in the string given to the Parse() function - | |
150 | // it's task of the caller ensure that memory is still valid ! | |
151 | const CharType *m_pArgPos; | |
152 | ||
153 | // pointer to the last character of this conversion specifier in the | |
154 | // format string | |
155 | // NOTE: this points somewhere in the string given to the Parse() function - | |
156 | // it's task of the caller ensure that memory is still valid ! | |
157 | const CharType *m_pArgEnd; | |
158 | ||
b113edcd | 159 | // a little buffer where formatting flags like #+\.hlqLz are stored by Parse() |
47346406 | 160 | // for use in Process() |
47346406 VS |
161 | char m_szFlags[wxMAX_SVNPRINTF_FLAGBUFFER_LEN]; |
162 | ||
163 | ||
164 | public: | |
165 | ||
166 | // we don't declare this as a constructor otherwise it would be called | |
167 | // automatically and we don't want this: to be optimized, wxCRT_VsnprintfW | |
168 | // calls this function only on really-used instances of this class. | |
169 | void Init(); | |
170 | ||
171 | // Parses the first conversion specifier in the given string, which must | |
172 | // begin with a '%'. Returns false if the first '%' does not introduce a | |
173 | // (valid) conversion specifier and thus should be ignored. | |
174 | bool Parse(const CharType *format); | |
175 | ||
176 | // Process this conversion specifier and puts the result in the given | |
177 | // buffer. Returns the number of characters written in 'buf' or -1 if | |
178 | // there's not enough space. | |
179 | int Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written); | |
180 | ||
181 | // Loads the argument of this conversion specifier from given va_list. | |
182 | bool LoadArg(wxPrintfArg *p, va_list &argptr); | |
183 | ||
184 | private: | |
185 | // An helper function of LoadArg() which is used to handle the '*' flag | |
186 | void ReplaceAsteriskWith(int w); | |
187 | }; | |
188 | ||
189 | template<typename CharType> | |
190 | void wxPrintfConvSpec<CharType>::Init() | |
191 | { | |
192 | m_nMinWidth = 0; | |
193 | m_nMaxWidth = 0xFFFF; | |
194 | m_pos = 0; | |
195 | m_bAlignLeft = false; | |
196 | m_pArgPos = m_pArgEnd = NULL; | |
197 | m_type = wxPAT_INVALID; | |
198 | ||
060ec116 | 199 | memset(m_szFlags, 0, sizeof(m_szFlags)); |
47346406 VS |
200 | // this character will never be removed from m_szFlags array and |
201 | // is important when calling sprintf() in wxPrintfConvSpec::Process() ! | |
202 | m_szFlags[0] = '%'; | |
203 | } | |
204 | ||
205 | template<typename CharType> | |
206 | bool wxPrintfConvSpec<CharType>::Parse(const CharType *format) | |
207 | { | |
208 | bool done = false; | |
209 | ||
210 | // temporary parse data | |
211 | size_t flagofs = 1; | |
212 | bool in_prec, // true if we found the dot in some previous iteration | |
213 | prec_dot; // true if the dot has been already added to m_szFlags | |
214 | int ilen = 0; | |
215 | ||
216 | m_bAlignLeft = in_prec = prec_dot = false; | |
217 | m_pArgPos = m_pArgEnd = format; | |
218 | do | |
219 | { | |
220 | #define CHECK_PREC \ | |
221 | if (in_prec && !prec_dot) \ | |
222 | { \ | |
223 | m_szFlags[flagofs++] = '.'; \ | |
224 | prec_dot = true; \ | |
225 | } | |
226 | ||
227 | // what follows '%'? | |
228 | const CharType ch = *(++m_pArgEnd); | |
229 | switch ( ch ) | |
230 | { | |
231 | case wxT('\0'): | |
232 | return false; // not really an argument | |
233 | ||
234 | case wxT('%'): | |
235 | return false; // not really an argument | |
236 | ||
237 | case wxT('#'): | |
238 | case wxT('0'): | |
239 | case wxT(' '): | |
240 | case wxT('+'): | |
241 | case wxT('\''): | |
242 | CHECK_PREC | |
243 | m_szFlags[flagofs++] = char(ch); | |
244 | break; | |
245 | ||
246 | case wxT('-'): | |
247 | CHECK_PREC | |
248 | m_bAlignLeft = true; | |
249 | m_szFlags[flagofs++] = char(ch); | |
250 | break; | |
251 | ||
252 | case wxT('.'): | |
e822d1bd VZ |
253 | // don't use CHECK_PREC here to avoid warning about the value |
254 | // assigned to prec_dot inside it being never used (because | |
255 | // overwritten just below) from Borland in release build | |
256 | if (in_prec && !prec_dot) | |
257 | m_szFlags[flagofs++] = '.'; | |
47346406 VS |
258 | in_prec = true; |
259 | prec_dot = false; | |
260 | m_nMaxWidth = 0; | |
261 | // dot will be auto-added to m_szFlags if non-negative | |
262 | // number follows | |
263 | break; | |
264 | ||
265 | case wxT('h'): | |
266 | ilen = -1; | |
267 | CHECK_PREC | |
268 | m_szFlags[flagofs++] = char(ch); | |
269 | break; | |
270 | ||
271 | case wxT('l'): | |
272 | // NB: it's safe to use flagofs-1 as flagofs always start from 1 | |
273 | if (m_szFlags[flagofs-1] == 'l') // 'll' modifier is the same as 'L' or 'q' | |
274 | ilen = 2; | |
275 | else | |
276 | ilen = 1; | |
277 | CHECK_PREC | |
278 | m_szFlags[flagofs++] = char(ch); | |
279 | break; | |
280 | ||
281 | case wxT('q'): | |
282 | case wxT('L'): | |
283 | ilen = 2; | |
284 | CHECK_PREC | |
285 | m_szFlags[flagofs++] = char(ch); | |
286 | break; | |
d98a58c5 | 287 | #ifdef __WINDOWS__ |
47346406 VS |
288 | // under Windows we support the special '%I64' notation as longlong |
289 | // integer conversion specifier for MSVC compatibility | |
290 | // (it behaves exactly as '%lli' or '%Li' or '%qi') | |
291 | case wxT('I'): | |
b113edcd VS |
292 | if (*(m_pArgEnd+1) == wxT('6') && |
293 | *(m_pArgEnd+2) == wxT('4')) | |
294 | { | |
295 | m_pArgEnd++; | |
296 | m_pArgEnd++; | |
47346406 | 297 | |
b113edcd VS |
298 | ilen = 2; |
299 | CHECK_PREC | |
300 | m_szFlags[flagofs++] = char(ch); | |
301 | m_szFlags[flagofs++] = '6'; | |
302 | m_szFlags[flagofs++] = '4'; | |
303 | break; | |
304 | } | |
305 | // else: fall-through, 'I' is MSVC equivalent of C99 'z' | |
d98a58c5 | 306 | #endif // __WINDOWS__ |
47346406 | 307 | |
b113edcd | 308 | case wxT('z'): |
47346406 | 309 | case wxT('Z'): |
b113edcd VS |
310 | // 'z' is C99 standard for size_t and ptrdiff_t, 'Z' was used |
311 | // for this purpose in libc5 and by wx <= 2.8 | |
47346406 VS |
312 | ilen = 3; |
313 | CHECK_PREC | |
314 | m_szFlags[flagofs++] = char(ch); | |
315 | break; | |
316 | ||
317 | case wxT('*'): | |
318 | if (in_prec) | |
319 | { | |
320 | CHECK_PREC | |
321 | ||
322 | // tell Process() to use the next argument | |
323 | // in the stack as maxwidth... | |
324 | m_nMaxWidth = -1; | |
325 | } | |
326 | else | |
327 | { | |
328 | // tell Process() to use the next argument | |
329 | // in the stack as minwidth... | |
330 | m_nMinWidth = -1; | |
331 | } | |
332 | ||
333 | // save the * in our formatting buffer... | |
334 | // will be replaced later by Process() | |
335 | m_szFlags[flagofs++] = char(ch); | |
336 | break; | |
337 | ||
338 | case wxT('1'): case wxT('2'): case wxT('3'): | |
339 | case wxT('4'): case wxT('5'): case wxT('6'): | |
340 | case wxT('7'): case wxT('8'): case wxT('9'): | |
341 | { | |
342 | int len = 0; | |
343 | CHECK_PREC | |
344 | while ( (*m_pArgEnd >= CharType('0')) && | |
345 | (*m_pArgEnd <= CharType('9')) ) | |
346 | { | |
347 | m_szFlags[flagofs++] = char(*m_pArgEnd); | |
348 | len = len*10 + (*m_pArgEnd - wxT('0')); | |
349 | m_pArgEnd++; | |
350 | } | |
351 | ||
352 | if (in_prec) | |
353 | m_nMaxWidth = len; | |
354 | else | |
355 | m_nMinWidth = len; | |
356 | ||
357 | m_pArgEnd--; // the main loop pre-increments n again | |
358 | } | |
359 | break; | |
360 | ||
361 | case wxT('$'): // a positional parameter (e.g. %2$s) ? | |
362 | { | |
363 | if (m_nMinWidth <= 0) | |
364 | break; // ignore this formatting flag as no | |
365 | // numbers are preceding it | |
366 | ||
367 | // remove from m_szFlags all digits previously added | |
368 | do { | |
369 | flagofs--; | |
370 | } while (m_szFlags[flagofs] >= '1' && | |
371 | m_szFlags[flagofs] <= '9'); | |
372 | ||
373 | // re-adjust the offset making it point to the | |
374 | // next free char of m_szFlags | |
375 | flagofs++; | |
376 | ||
377 | m_pos = m_nMinWidth; | |
378 | m_nMinWidth = 0; | |
379 | } | |
380 | break; | |
381 | ||
382 | case wxT('d'): | |
383 | case wxT('i'): | |
384 | case wxT('o'): | |
385 | case wxT('u'): | |
386 | case wxT('x'): | |
387 | case wxT('X'): | |
388 | CHECK_PREC | |
389 | m_szFlags[flagofs++] = char(ch); | |
47346406 VS |
390 | if (ilen == 0) |
391 | m_type = wxPAT_INT; | |
392 | else if (ilen == -1) | |
393 | // NB: 'short int' value passed through '...' | |
394 | // is promoted to 'int', so we have to get | |
395 | // an int from stack even if we need a short | |
396 | m_type = wxPAT_INT; | |
397 | else if (ilen == 1) | |
398 | m_type = wxPAT_LONGINT; | |
399 | else if (ilen == 2) | |
400 | #ifdef wxLongLong_t | |
401 | m_type = wxPAT_LONGLONGINT; | |
402 | #else // !wxLongLong_t | |
403 | m_type = wxPAT_LONGINT; | |
404 | #endif // wxLongLong_t/!wxLongLong_t | |
405 | else if (ilen == 3) | |
406 | m_type = wxPAT_SIZET; | |
407 | done = true; | |
408 | break; | |
409 | ||
410 | case wxT('e'): | |
411 | case wxT('E'): | |
412 | case wxT('f'): | |
413 | case wxT('g'): | |
414 | case wxT('G'): | |
415 | CHECK_PREC | |
416 | m_szFlags[flagofs++] = char(ch); | |
47346406 VS |
417 | if (ilen == 2) |
418 | m_type = wxPAT_LONGDOUBLE; | |
419 | else | |
420 | m_type = wxPAT_DOUBLE; | |
421 | done = true; | |
422 | break; | |
423 | ||
424 | case wxT('p'): | |
425 | m_type = wxPAT_POINTER; | |
426 | m_szFlags[flagofs++] = char(ch); | |
47346406 VS |
427 | done = true; |
428 | break; | |
429 | ||
430 | case wxT('c'): | |
431 | if (ilen == -1) | |
432 | { | |
433 | // in Unicode mode %hc == ANSI character | |
434 | // and in ANSI mode, %hc == %c == ANSI... | |
435 | m_type = wxPAT_CHAR; | |
436 | } | |
437 | else if (ilen == 1) | |
438 | { | |
439 | // in ANSI mode %lc == Unicode character | |
440 | // and in Unicode mode, %lc == %c == Unicode... | |
441 | m_type = wxPAT_WCHAR; | |
442 | } | |
443 | else | |
444 | { | |
445 | #if wxUSE_UNICODE | |
446 | // in Unicode mode, %c == Unicode character | |
447 | m_type = wxPAT_WCHAR; | |
448 | #else | |
449 | // in ANSI mode, %c == ANSI character | |
450 | m_type = wxPAT_CHAR; | |
451 | #endif | |
452 | } | |
453 | done = true; | |
454 | break; | |
455 | ||
456 | case wxT('s'): | |
457 | if (ilen == -1) | |
458 | { | |
459 | // Unicode mode wx extension: we'll let %hs mean non-Unicode | |
460 | // strings (when in ANSI mode, %s == %hs == ANSI string) | |
461 | m_type = wxPAT_PCHAR; | |
462 | } | |
463 | else if (ilen == 1) | |
464 | { | |
465 | // in Unicode mode, %ls == %s == Unicode string | |
466 | // in ANSI mode, %ls == Unicode string | |
467 | m_type = wxPAT_PWCHAR; | |
468 | } | |
469 | else | |
470 | { | |
471 | #if wxUSE_UNICODE | |
472 | m_type = wxPAT_PWCHAR; | |
473 | #else | |
474 | m_type = wxPAT_PCHAR; | |
475 | #endif | |
476 | } | |
477 | done = true; | |
478 | break; | |
479 | ||
480 | case wxT('n'): | |
481 | if (ilen == 0) | |
482 | m_type = wxPAT_NINT; | |
483 | else if (ilen == -1) | |
484 | m_type = wxPAT_NSHORTINT; | |
485 | else if (ilen >= 1) | |
486 | m_type = wxPAT_NLONGINT; | |
487 | done = true; | |
488 | break; | |
489 | ||
490 | default: | |
491 | // bad format, don't consider this an argument; | |
492 | // leave it unchanged | |
493 | return false; | |
494 | } | |
495 | ||
496 | if (flagofs == wxMAX_SVNPRINTF_FLAGBUFFER_LEN) | |
497 | { | |
498 | wxLogDebug(wxT("Too many flags specified for a single conversion specifier!")); | |
499 | return false; | |
500 | } | |
501 | } | |
502 | while (!done); | |
503 | ||
504 | return true; // parsing was successful | |
505 | } | |
506 | ||
507 | template<typename CharType> | |
508 | void wxPrintfConvSpec<CharType>::ReplaceAsteriskWith(int width) | |
509 | { | |
510 | char temp[wxMAX_SVNPRINTF_FLAGBUFFER_LEN]; | |
511 | ||
512 | // find the first * in our flag buffer | |
513 | char *pwidth = strchr(m_szFlags, '*'); | |
9a83f860 | 514 | wxCHECK_RET(pwidth, wxT("field width must be specified")); |
47346406 VS |
515 | |
516 | // save what follows the * (the +1 is to skip the asterisk itself!) | |
517 | strcpy(temp, pwidth+1); | |
518 | if (width < 0) | |
519 | { | |
520 | pwidth[0] = wxT('-'); | |
521 | pwidth++; | |
522 | } | |
523 | ||
524 | // replace * with the actual integer given as width | |
525 | #ifndef SYSTEM_SPRINTF_IS_UNSAFE | |
526 | int maxlen = (m_szFlags + wxMAX_SVNPRINTF_FLAGBUFFER_LEN - pwidth) / | |
527 | sizeof(*m_szFlags); | |
528 | #endif | |
529 | int offset = system_sprintf(pwidth, maxlen, "%d", abs(width)); | |
530 | ||
531 | // restore after the expanded * what was following it | |
532 | strcpy(pwidth+offset, temp); | |
533 | } | |
534 | ||
535 | template<typename CharType> | |
536 | bool wxPrintfConvSpec<CharType>::LoadArg(wxPrintfArg *p, va_list &argptr) | |
537 | { | |
538 | // did the '*' width/precision specifier was used ? | |
539 | if (m_nMaxWidth == -1) | |
540 | { | |
541 | // take the maxwidth specifier from the stack | |
542 | m_nMaxWidth = va_arg(argptr, int); | |
543 | if (m_nMaxWidth < 0) | |
544 | m_nMaxWidth = 0; | |
545 | else | |
546 | ReplaceAsteriskWith(m_nMaxWidth); | |
547 | } | |
548 | ||
549 | if (m_nMinWidth == -1) | |
550 | { | |
551 | // take the minwidth specifier from the stack | |
552 | m_nMinWidth = va_arg(argptr, int); | |
553 | ||
554 | ReplaceAsteriskWith(m_nMinWidth); | |
555 | if (m_nMinWidth < 0) | |
556 | { | |
557 | m_bAlignLeft = !m_bAlignLeft; | |
558 | m_nMinWidth = -m_nMinWidth; | |
559 | } | |
560 | } | |
561 | ||
562 | switch (m_type) { | |
563 | case wxPAT_INT: | |
564 | p->pad_int = va_arg(argptr, int); | |
565 | break; | |
566 | case wxPAT_LONGINT: | |
567 | p->pad_longint = va_arg(argptr, long int); | |
568 | break; | |
569 | #ifdef wxLongLong_t | |
570 | case wxPAT_LONGLONGINT: | |
571 | p->pad_longlongint = va_arg(argptr, wxLongLong_t); | |
572 | break; | |
573 | #endif // wxLongLong_t | |
574 | case wxPAT_SIZET: | |
575 | p->pad_sizet = va_arg(argptr, size_t); | |
576 | break; | |
577 | case wxPAT_DOUBLE: | |
578 | p->pad_double = va_arg(argptr, double); | |
579 | break; | |
580 | case wxPAT_LONGDOUBLE: | |
581 | p->pad_longdouble = va_arg(argptr, long double); | |
582 | break; | |
583 | case wxPAT_POINTER: | |
584 | p->pad_pointer = va_arg(argptr, void *); | |
585 | break; | |
586 | ||
587 | case wxPAT_CHAR: | |
588 | p->pad_char = (char)va_arg(argptr, int); // char is promoted to int when passed through '...' | |
589 | break; | |
590 | case wxPAT_WCHAR: | |
591 | p->pad_wchar = (wchar_t)va_arg(argptr, int); // char is promoted to int when passed through '...' | |
592 | break; | |
593 | ||
594 | case wxPAT_PCHAR: | |
595 | case wxPAT_PWCHAR: | |
596 | p->pad_str = va_arg(argptr, void *); | |
597 | break; | |
598 | ||
599 | case wxPAT_NINT: | |
600 | p->pad_nint = va_arg(argptr, int *); | |
601 | break; | |
602 | case wxPAT_NSHORTINT: | |
603 | p->pad_nshortint = va_arg(argptr, short int *); | |
604 | break; | |
605 | case wxPAT_NLONGINT: | |
606 | p->pad_nlongint = va_arg(argptr, long int *); | |
607 | break; | |
608 | ||
75ac34ce VZ |
609 | case wxPAT_STAR: |
610 | // this will be handled as part of the next argument | |
611 | return true; | |
612 | ||
47346406 VS |
613 | case wxPAT_INVALID: |
614 | default: | |
615 | return false; | |
616 | } | |
617 | ||
618 | return true; // loading was successful | |
619 | } | |
620 | ||
621 | template<typename CharType> | |
622 | int wxPrintfConvSpec<CharType>::Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written) | |
623 | { | |
624 | // buffer to avoid dynamic memory allocation each time for small strings; | |
625 | // note that this buffer is used only to hold results of number formatting, | |
626 | // %s directly writes user's string in buf, without using szScratch | |
627 | char szScratch[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN]; | |
628 | size_t lenScratch = 0, lenCur = 0; | |
629 | ||
630 | #define APPEND_CH(ch) \ | |
631 | { \ | |
632 | if ( lenCur == lenMax ) \ | |
633 | return -1; \ | |
634 | \ | |
635 | buf[lenCur++] = ch; \ | |
636 | } | |
637 | ||
638 | switch ( m_type ) | |
639 | { | |
640 | case wxPAT_INT: | |
641 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_int); | |
642 | break; | |
643 | ||
644 | case wxPAT_LONGINT: | |
645 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longint); | |
646 | break; | |
647 | ||
648 | #ifdef wxLongLong_t | |
649 | case wxPAT_LONGLONGINT: | |
650 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longlongint); | |
651 | break; | |
652 | #endif // SIZEOF_LONG_LONG | |
653 | ||
654 | case wxPAT_SIZET: | |
655 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_sizet); | |
656 | break; | |
657 | ||
658 | case wxPAT_LONGDOUBLE: | |
659 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longdouble); | |
660 | break; | |
661 | ||
662 | case wxPAT_DOUBLE: | |
663 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_double); | |
664 | break; | |
665 | ||
666 | case wxPAT_POINTER: | |
667 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_pointer); | |
668 | break; | |
669 | ||
670 | case wxPAT_CHAR: | |
671 | case wxPAT_WCHAR: | |
672 | { | |
673 | wxUniChar ch; | |
674 | if (m_type == wxPAT_CHAR) | |
675 | ch = p->pad_char; | |
676 | else // m_type == wxPAT_WCHAR | |
677 | ch = p->pad_wchar; | |
678 | ||
679 | CharType val = ch; | |
680 | ||
681 | size_t i; | |
682 | ||
683 | if (!m_bAlignLeft) | |
684 | for (i = 1; i < (size_t)m_nMinWidth; i++) | |
9a83f860 | 685 | APPEND_CH(wxT(' ')); |
47346406 VS |
686 | |
687 | APPEND_CH(val); | |
688 | ||
689 | if (m_bAlignLeft) | |
690 | for (i = 1; i < (size_t)m_nMinWidth; i++) | |
9a83f860 | 691 | APPEND_CH(wxT(' ')); |
47346406 VS |
692 | } |
693 | break; | |
694 | ||
695 | case wxPAT_PCHAR: | |
696 | case wxPAT_PWCHAR: | |
697 | { | |
37513b67 VZ |
698 | wxString s; |
699 | if ( !p->pad_str ) | |
700 | { | |
701 | if ( m_nMaxWidth >= 6 ) | |
702 | s = wxT("(null)"); | |
703 | } | |
704 | else if (m_type == wxPAT_PCHAR) | |
705 | s.assign(static_cast<const char *>(p->pad_str)); | |
706 | else // m_type == wxPAT_PWCHAR | |
707 | s.assign(static_cast<const wchar_t *>(p->pad_str)); | |
47346406 VS |
708 | |
709 | typename wxPrintfStringHelper<CharType>::ConvertedType strbuf( | |
710 | wxPrintfStringHelper<CharType>::Convert(s)); | |
711 | ||
712 | // at this point we are sure that m_nMaxWidth is positive or | |
713 | // null (see top of wxPrintfConvSpec::LoadArg) | |
714 | int len = wxMin((unsigned int)m_nMaxWidth, wxStrlen(strbuf)); | |
715 | ||
716 | int i; | |
717 | ||
718 | if (!m_bAlignLeft) | |
719 | { | |
720 | for (i = len; i < m_nMinWidth; i++) | |
9a83f860 | 721 | APPEND_CH(wxT(' ')); |
47346406 VS |
722 | } |
723 | ||
724 | len = wxMin((unsigned int)len, lenMax-lenCur); | |
725 | wxStrncpy(buf+lenCur, strbuf, len); | |
726 | lenCur += len; | |
727 | ||
728 | if (m_bAlignLeft) | |
729 | { | |
730 | for (i = len; i < m_nMinWidth; i++) | |
9a83f860 | 731 | APPEND_CH(wxT(' ')); |
47346406 VS |
732 | } |
733 | } | |
734 | break; | |
735 | ||
736 | case wxPAT_NINT: | |
737 | *p->pad_nint = written; | |
738 | break; | |
739 | ||
740 | case wxPAT_NSHORTINT: | |
741 | *p->pad_nshortint = (short int)written; | |
742 | break; | |
743 | ||
744 | case wxPAT_NLONGINT: | |
745 | *p->pad_nlongint = written; | |
746 | break; | |
747 | ||
748 | case wxPAT_INVALID: | |
749 | default: | |
750 | return -1; | |
751 | } | |
752 | ||
753 | // if we used system's sprintf() then we now need to append the s_szScratch | |
754 | // buffer to the given one... | |
755 | switch (m_type) | |
756 | { | |
757 | case wxPAT_INT: | |
758 | case wxPAT_LONGINT: | |
759 | #ifdef wxLongLong_t | |
760 | case wxPAT_LONGLONGINT: | |
761 | #endif | |
762 | case wxPAT_SIZET: | |
763 | case wxPAT_LONGDOUBLE: | |
764 | case wxPAT_DOUBLE: | |
765 | case wxPAT_POINTER: | |
766 | wxASSERT(lenScratch < wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN); | |
767 | // NB: 1) we can compare lenMax (for CharType*, i.e. possibly | |
768 | // wchar_t*) with lenScratch (char*) because this code is | |
769 | // formatting integers and that will have the same length | |
770 | // even in UTF-8 (the only case when char* length may be | |
771 | // more than wchar_t* length of the same string) | |
772 | // 2) wxStrncpy converts the 2nd argument to 1st argument's | |
773 | // type transparently if their types differ, so this code | |
774 | // works for both instantiations | |
775 | if (lenMax < lenScratch) | |
776 | { | |
777 | // fill output buffer and then return -1 | |
778 | wxStrncpy(buf, szScratch, lenMax); | |
779 | return -1; | |
780 | } | |
781 | wxStrncpy(buf, szScratch, lenScratch); | |
782 | lenCur += lenScratch; | |
783 | break; | |
784 | ||
785 | default: | |
786 | break; // all other cases were completed previously | |
787 | } | |
788 | ||
789 | return lenCur; | |
790 | } | |
791 | ||
792 | ||
793 | // helper that parses format string | |
794 | template<typename CharType> | |
795 | struct wxPrintfConvSpecParser | |
796 | { | |
75ac34ce VZ |
797 | typedef wxPrintfConvSpec<CharType> ConvSpec; |
798 | ||
799 | wxPrintfConvSpecParser(const CharType *fmt) | |
47346406 | 800 | { |
f20e2689 VZ |
801 | nargs = 0; |
802 | posarg_present = | |
803 | nonposarg_present = false; | |
804 | ||
47346406 VS |
805 | memset(pspec, 0, sizeof(pspec)); |
806 | ||
47346406 | 807 | // parse the format string |
75ac34ce | 808 | for ( const CharType *toparse = fmt; *toparse != wxT('\0'); toparse++ ) |
47346406 | 809 | { |
75ac34ce VZ |
810 | // skip everything except format specifications |
811 | if ( *toparse != '%' ) | |
812 | continue; | |
813 | ||
814 | // also skip escaped percent signs | |
815 | if ( toparse[1] == '%' ) | |
47346406 | 816 | { |
75ac34ce VZ |
817 | toparse++; |
818 | continue; | |
819 | } | |
47346406 | 820 | |
75ac34ce VZ |
821 | ConvSpec *spec = &specs[nargs]; |
822 | spec->Init(); | |
47346406 | 823 | |
75ac34ce VZ |
824 | // attempt to parse this format specification |
825 | if ( !spec->Parse(toparse) ) | |
826 | continue; | |
47346406 | 827 | |
75ac34ce VZ |
828 | // advance to the end of this specifier |
829 | toparse = spec->m_pArgEnd; | |
47346406 | 830 | |
75ac34ce VZ |
831 | // special handling for specifications including asterisks: we need |
832 | // to reserve an extra slot (or two if asterisks were used for both | |
833 | // width and precision) in specs array in this case | |
e2dd6247 | 834 | if ( const char *f = strchr(spec->m_szFlags, '*') ) |
75ac34ce | 835 | { |
e2dd6247 VZ |
836 | unsigned numAsterisks = 1; |
837 | if ( strchr(++f, '*') ) | |
838 | numAsterisks++; | |
75ac34ce | 839 | |
e2dd6247 | 840 | for ( unsigned n = 0; n < numAsterisks; n++ ) |
47346406 | 841 | { |
e2dd6247 VZ |
842 | if ( nargs++ == wxMAX_SVNPRINTF_ARGUMENTS ) |
843 | break; | |
844 | ||
845 | // TODO: we need to support specifiers of the form "%2$*1$s" | |
846 | // (this is the same as "%*s") as if any positional arguments | |
847 | // are used all asterisks must be positional as well but this | |
848 | // requires a lot of changes in this code (basically we'd need | |
849 | // to rewrite Parse() to return "*" and conversion itself as | |
850 | // separate entries) | |
851 | if ( posarg_present ) | |
852 | { | |
853 | wxFAIL_MSG | |
75ac34ce | 854 | ( |
e2dd6247 VZ |
855 | wxString::Format |
856 | ( | |
857 | "Format string \"%s\" uses both positional " | |
858 | "parameters and '*' but this is not currently " | |
859 | "supported by this implementation, sorry.", | |
860 | fmt | |
861 | ) | |
862 | ); | |
863 | } | |
75ac34ce | 864 | |
e2dd6247 | 865 | specs[nargs] = *spec; |
75ac34ce | 866 | |
e2dd6247 VZ |
867 | // make an entry for '*' and point to it from pspec |
868 | spec->Init(); | |
869 | spec->m_type = wxPAT_STAR; | |
870 | pspec[nargs - 1] = spec; | |
75ac34ce | 871 | |
e2dd6247 VZ |
872 | spec = &specs[nargs]; |
873 | } | |
75ac34ce VZ |
874 | } |
875 | ||
e2dd6247 | 876 | |
75ac34ce VZ |
877 | // check if this is a positional or normal argument |
878 | if ( spec->m_pos > 0 ) | |
879 | { | |
880 | // the positional arguments start from number 1 so we need | |
881 | // to adjust the index | |
882 | spec->m_pos--; | |
883 | posarg_present = true; | |
884 | } | |
885 | else // not a positional argument... | |
886 | { | |
887 | spec->m_pos = nargs; | |
888 | nonposarg_present = true; | |
47346406 | 889 | } |
75ac34ce VZ |
890 | |
891 | // this conversion specifier is tied to the pos-th argument... | |
892 | pspec[spec->m_pos] = spec; | |
893 | ||
894 | if ( nargs++ == wxMAX_SVNPRINTF_ARGUMENTS ) | |
895 | break; | |
896 | } | |
897 | ||
898 | ||
899 | // warn if we lost any arguments (the program probably will crash | |
900 | // anyhow because of stack corruption...) | |
901 | if ( nargs == wxMAX_SVNPRINTF_ARGUMENTS ) | |
902 | { | |
903 | wxFAIL_MSG | |
904 | ( | |
905 | wxString::Format | |
906 | ( | |
907 | "wxVsnprintf() currently supports only %d arguments, " | |
908 | "but format string \"%s\" defines more of them.\n" | |
909 | "You need to change wxMAX_SVNPRINTF_ARGUMENTS and " | |
910 | "recompile if more are really needed.", | |
911 | fmt, wxMAX_SVNPRINTF_ARGUMENTS | |
912 | ) | |
913 | ); | |
47346406 VS |
914 | } |
915 | } | |
916 | ||
75ac34ce | 917 | // total number of valid elements in specs |
47346406 | 918 | unsigned nargs; |
75ac34ce VZ |
919 | |
920 | // all format specifications in this format string in order of their | |
921 | // appearance (which may be different from arguments order) | |
922 | ConvSpec specs[wxMAX_SVNPRINTF_ARGUMENTS]; | |
923 | ||
924 | // pointer to specs array element for the N-th argument | |
925 | ConvSpec *pspec[wxMAX_SVNPRINTF_ARGUMENTS]; | |
926 | ||
927 | // true if any positional/non-positional parameters are used | |
928 | bool posarg_present, | |
929 | nonposarg_present; | |
47346406 VS |
930 | }; |
931 | ||
932 | #undef APPEND_CH | |
933 | #undef CHECK_PREC | |
934 | ||
935 | #endif // _WX_PRIVATE_WXPRINTF_H_ |