]>
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 | ||
25 | #if defined(__MWERKS__) && __MSL__ >= 0x6000 | |
26 | namespace std {} | |
27 | using namespace std ; | |
28 | #endif | |
29 | ||
30 | // prefer snprintf over sprintf | |
31 | #if defined(__VISUALC__) || \ | |
32 | (defined(__BORLANDC__) && __BORLANDC__ >= 0x540) | |
33 | #define system_sprintf(buff, max, flags, data) \ | |
34 | ::_snprintf(buff, max, flags, data) | |
35 | #elif defined(HAVE_SNPRINTF) | |
36 | #define system_sprintf(buff, max, flags, data) \ | |
37 | ::snprintf(buff, max, flags, data) | |
38 | #else // NB: at least sprintf() should always be available | |
39 | // since 'max' is not used in this case, wxVsnprintf() should always | |
40 | // ensure that 'buff' is big enough for all common needs | |
41 | // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN) | |
42 | #define system_sprintf(buff, max, flags, data) \ | |
43 | ::sprintf(buff, flags, data) | |
44 | ||
45 | #define SYSTEM_SPRINTF_IS_UNSAFE | |
46 | #endif | |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | // printf format string parsing | |
50 | // --------------------------------------------------------------------------- | |
51 | ||
52 | // some limits of our implementation | |
53 | #define wxMAX_SVNPRINTF_ARGUMENTS 64 | |
54 | #define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32 | |
55 | #define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512 | |
56 | ||
57 | ||
58 | // the conversion specifiers accepted by wxCRT_VsnprintfW | |
59 | enum wxPrintfArgType { | |
60 | wxPAT_INVALID = -1, | |
61 | ||
62 | wxPAT_INT, // %d, %i, %o, %u, %x, %X | |
63 | wxPAT_LONGINT, // %ld, etc | |
64 | #ifdef wxLongLong_t | |
65 | wxPAT_LONGLONGINT, // %Ld, etc | |
66 | #endif | |
67 | wxPAT_SIZET, // %Zd, etc | |
68 | ||
69 | wxPAT_DOUBLE, // %e, %E, %f, %g, %G | |
70 | wxPAT_LONGDOUBLE, // %le, etc | |
71 | ||
72 | wxPAT_POINTER, // %p | |
73 | ||
74 | wxPAT_CHAR, // %hc (in ANSI mode: %c, too) | |
75 | wxPAT_WCHAR, // %lc (in Unicode mode: %c, too) | |
76 | ||
77 | wxPAT_PCHAR, // %s (related to a char *) | |
78 | wxPAT_PWCHAR, // %s (related to a wchar_t *) | |
79 | ||
80 | wxPAT_NINT, // %n | |
81 | wxPAT_NSHORTINT, // %hn | |
82 | wxPAT_NLONGINT // %ln | |
83 | }; | |
84 | ||
85 | // an argument passed to wxCRT_VsnprintfW | |
86 | typedef union { | |
87 | int pad_int; // %d, %i, %o, %u, %x, %X | |
88 | long int pad_longint; // %ld, etc | |
89 | #ifdef wxLongLong_t | |
90 | wxLongLong_t pad_longlongint; // %Ld, etc | |
91 | #endif | |
92 | size_t pad_sizet; // %Zd, etc | |
93 | ||
94 | double pad_double; // %e, %E, %f, %g, %G | |
95 | long double pad_longdouble; // %le, etc | |
96 | ||
97 | void *pad_pointer; // %p | |
98 | ||
99 | char pad_char; // %hc (in ANSI mode: %c, too) | |
100 | wchar_t pad_wchar; // %lc (in Unicode mode: %c, too) | |
101 | ||
102 | void *pad_str; // %s | |
103 | ||
104 | int *pad_nint; // %n | |
105 | short int *pad_nshortint; // %hn | |
106 | long int *pad_nlongint; // %ln | |
107 | } wxPrintfArg; | |
108 | ||
109 | // helper for converting string into either char* or wchar_t* dependening | |
110 | // on the type of wxPrintfConvSpec<T> instantiation: | |
111 | template<typename CharType> struct wxPrintfStringHelper {}; | |
112 | ||
113 | template<> struct wxPrintfStringHelper<char> | |
114 | { | |
115 | typedef const wxWX2MBbuf ConvertedType; | |
116 | static ConvertedType Convert(const wxString& s) { return s.mb_str(); } | |
117 | }; | |
118 | ||
119 | template<> struct wxPrintfStringHelper<wchar_t> | |
120 | { | |
121 | typedef const wxWX2WCbuf ConvertedType; | |
122 | static ConvertedType Convert(const wxString& s) { return s.wc_str(); } | |
123 | }; | |
124 | ||
125 | ||
126 | // Contains parsed data relative to a conversion specifier given to | |
127 | // wxCRT_VsnprintfW and parsed from the format string | |
128 | // NOTE: in C++ there is almost no difference between struct & classes thus | |
129 | // there is no performance gain by using a struct here... | |
130 | template<typename CharType> | |
131 | class wxPrintfConvSpec | |
132 | { | |
133 | public: | |
134 | ||
135 | // the position of the argument relative to this conversion specifier | |
136 | size_t m_pos; | |
137 | ||
138 | // the type of this conversion specifier | |
139 | wxPrintfArgType m_type; | |
140 | ||
141 | // the minimum and maximum width | |
142 | // when one of this var is set to -1 it means: use the following argument | |
143 | // in the stack as minimum/maximum width for this conversion specifier | |
144 | int m_nMinWidth, m_nMaxWidth; | |
145 | ||
146 | // does the argument need to the be aligned to left ? | |
147 | bool m_bAlignLeft; | |
148 | ||
149 | // pointer to the '%' of this conversion specifier in the format string | |
150 | // NOTE: this points somewhere in the string given to the Parse() function - | |
151 | // it's task of the caller ensure that memory is still valid ! | |
152 | const CharType *m_pArgPos; | |
153 | ||
154 | // pointer to the last character of this conversion specifier in the | |
155 | // format string | |
156 | // NOTE: this points somewhere in the string given to the Parse() function - | |
157 | // it's task of the caller ensure that memory is still valid ! | |
158 | const CharType *m_pArgEnd; | |
159 | ||
160 | // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse() | |
161 | // for use in Process() | |
162 | // NB: even if this buffer is used only for numeric conversion specifiers | |
163 | // and thus could be safely declared as a char[] buffer, we want it to | |
164 | // be wchar_t so that in Unicode builds we can avoid to convert its | |
165 | // contents to Unicode chars when copying it in user's buffer. | |
166 | char m_szFlags[wxMAX_SVNPRINTF_FLAGBUFFER_LEN]; | |
167 | ||
168 | ||
169 | public: | |
170 | ||
171 | // we don't declare this as a constructor otherwise it would be called | |
172 | // automatically and we don't want this: to be optimized, wxCRT_VsnprintfW | |
173 | // calls this function only on really-used instances of this class. | |
174 | void Init(); | |
175 | ||
176 | // Parses the first conversion specifier in the given string, which must | |
177 | // begin with a '%'. Returns false if the first '%' does not introduce a | |
178 | // (valid) conversion specifier and thus should be ignored. | |
179 | bool Parse(const CharType *format); | |
180 | ||
181 | // Process this conversion specifier and puts the result in the given | |
182 | // buffer. Returns the number of characters written in 'buf' or -1 if | |
183 | // there's not enough space. | |
184 | int Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written); | |
185 | ||
186 | // Loads the argument of this conversion specifier from given va_list. | |
187 | bool LoadArg(wxPrintfArg *p, va_list &argptr); | |
188 | ||
189 | private: | |
190 | // An helper function of LoadArg() which is used to handle the '*' flag | |
191 | void ReplaceAsteriskWith(int w); | |
192 | }; | |
193 | ||
194 | template<typename CharType> | |
195 | void wxPrintfConvSpec<CharType>::Init() | |
196 | { | |
197 | m_nMinWidth = 0; | |
198 | m_nMaxWidth = 0xFFFF; | |
199 | m_pos = 0; | |
200 | m_bAlignLeft = false; | |
201 | m_pArgPos = m_pArgEnd = NULL; | |
202 | m_type = wxPAT_INVALID; | |
203 | ||
204 | // this character will never be removed from m_szFlags array and | |
205 | // is important when calling sprintf() in wxPrintfConvSpec::Process() ! | |
206 | m_szFlags[0] = '%'; | |
207 | } | |
208 | ||
209 | template<typename CharType> | |
210 | bool wxPrintfConvSpec<CharType>::Parse(const CharType *format) | |
211 | { | |
212 | bool done = false; | |
213 | ||
214 | // temporary parse data | |
215 | size_t flagofs = 1; | |
216 | bool in_prec, // true if we found the dot in some previous iteration | |
217 | prec_dot; // true if the dot has been already added to m_szFlags | |
218 | int ilen = 0; | |
219 | ||
220 | m_bAlignLeft = in_prec = prec_dot = false; | |
221 | m_pArgPos = m_pArgEnd = format; | |
222 | do | |
223 | { | |
224 | #define CHECK_PREC \ | |
225 | if (in_prec && !prec_dot) \ | |
226 | { \ | |
227 | m_szFlags[flagofs++] = '.'; \ | |
228 | prec_dot = true; \ | |
229 | } | |
230 | ||
231 | // what follows '%'? | |
232 | const CharType ch = *(++m_pArgEnd); | |
233 | switch ( ch ) | |
234 | { | |
235 | case wxT('\0'): | |
236 | return false; // not really an argument | |
237 | ||
238 | case wxT('%'): | |
239 | return false; // not really an argument | |
240 | ||
241 | case wxT('#'): | |
242 | case wxT('0'): | |
243 | case wxT(' '): | |
244 | case wxT('+'): | |
245 | case wxT('\''): | |
246 | CHECK_PREC | |
247 | m_szFlags[flagofs++] = char(ch); | |
248 | break; | |
249 | ||
250 | case wxT('-'): | |
251 | CHECK_PREC | |
252 | m_bAlignLeft = true; | |
253 | m_szFlags[flagofs++] = char(ch); | |
254 | break; | |
255 | ||
256 | case wxT('.'): | |
e822d1bd VZ |
257 | // don't use CHECK_PREC here to avoid warning about the value |
258 | // assigned to prec_dot inside it being never used (because | |
259 | // overwritten just below) from Borland in release build | |
260 | if (in_prec && !prec_dot) | |
261 | m_szFlags[flagofs++] = '.'; | |
47346406 VS |
262 | in_prec = true; |
263 | prec_dot = false; | |
264 | m_nMaxWidth = 0; | |
265 | // dot will be auto-added to m_szFlags if non-negative | |
266 | // number follows | |
267 | break; | |
268 | ||
269 | case wxT('h'): | |
270 | ilen = -1; | |
271 | CHECK_PREC | |
272 | m_szFlags[flagofs++] = char(ch); | |
273 | break; | |
274 | ||
275 | case wxT('l'): | |
276 | // NB: it's safe to use flagofs-1 as flagofs always start from 1 | |
277 | if (m_szFlags[flagofs-1] == 'l') // 'll' modifier is the same as 'L' or 'q' | |
278 | ilen = 2; | |
279 | else | |
280 | ilen = 1; | |
281 | CHECK_PREC | |
282 | m_szFlags[flagofs++] = char(ch); | |
283 | break; | |
284 | ||
285 | case wxT('q'): | |
286 | case wxT('L'): | |
287 | ilen = 2; | |
288 | CHECK_PREC | |
289 | m_szFlags[flagofs++] = char(ch); | |
290 | break; | |
291 | #ifdef __WXMSW__ | |
292 | // under Windows we support the special '%I64' notation as longlong | |
293 | // integer conversion specifier for MSVC compatibility | |
294 | // (it behaves exactly as '%lli' or '%Li' or '%qi') | |
295 | case wxT('I'): | |
296 | if (*(m_pArgEnd+1) != wxT('6') || | |
297 | *(m_pArgEnd+2) != wxT('4')) | |
298 | return false; // bad format | |
299 | ||
300 | m_pArgEnd++; | |
301 | m_pArgEnd++; | |
302 | ||
303 | ilen = 2; | |
304 | CHECK_PREC | |
305 | m_szFlags[flagofs++] = char(ch); | |
306 | m_szFlags[flagofs++] = '6'; | |
307 | m_szFlags[flagofs++] = '4'; | |
308 | break; | |
309 | #endif // __WXMSW__ | |
310 | ||
311 | case wxT('Z'): | |
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); | |
390 | m_szFlags[flagofs] = '\0'; | |
391 | if (ilen == 0) | |
392 | m_type = wxPAT_INT; | |
393 | else if (ilen == -1) | |
394 | // NB: 'short int' value passed through '...' | |
395 | // is promoted to 'int', so we have to get | |
396 | // an int from stack even if we need a short | |
397 | m_type = wxPAT_INT; | |
398 | else if (ilen == 1) | |
399 | m_type = wxPAT_LONGINT; | |
400 | else if (ilen == 2) | |
401 | #ifdef wxLongLong_t | |
402 | m_type = wxPAT_LONGLONGINT; | |
403 | #else // !wxLongLong_t | |
404 | m_type = wxPAT_LONGINT; | |
405 | #endif // wxLongLong_t/!wxLongLong_t | |
406 | else if (ilen == 3) | |
407 | m_type = wxPAT_SIZET; | |
408 | done = true; | |
409 | break; | |
410 | ||
411 | case wxT('e'): | |
412 | case wxT('E'): | |
413 | case wxT('f'): | |
414 | case wxT('g'): | |
415 | case wxT('G'): | |
416 | CHECK_PREC | |
417 | m_szFlags[flagofs++] = char(ch); | |
418 | m_szFlags[flagofs] = '\0'; | |
419 | if (ilen == 2) | |
420 | m_type = wxPAT_LONGDOUBLE; | |
421 | else | |
422 | m_type = wxPAT_DOUBLE; | |
423 | done = true; | |
424 | break; | |
425 | ||
426 | case wxT('p'): | |
427 | m_type = wxPAT_POINTER; | |
428 | m_szFlags[flagofs++] = char(ch); | |
429 | m_szFlags[flagofs] = '\0'; | |
430 | done = true; | |
431 | break; | |
432 | ||
433 | case wxT('c'): | |
434 | if (ilen == -1) | |
435 | { | |
436 | // in Unicode mode %hc == ANSI character | |
437 | // and in ANSI mode, %hc == %c == ANSI... | |
438 | m_type = wxPAT_CHAR; | |
439 | } | |
440 | else if (ilen == 1) | |
441 | { | |
442 | // in ANSI mode %lc == Unicode character | |
443 | // and in Unicode mode, %lc == %c == Unicode... | |
444 | m_type = wxPAT_WCHAR; | |
445 | } | |
446 | else | |
447 | { | |
448 | #if wxUSE_UNICODE | |
449 | // in Unicode mode, %c == Unicode character | |
450 | m_type = wxPAT_WCHAR; | |
451 | #else | |
452 | // in ANSI mode, %c == ANSI character | |
453 | m_type = wxPAT_CHAR; | |
454 | #endif | |
455 | } | |
456 | done = true; | |
457 | break; | |
458 | ||
459 | case wxT('s'): | |
460 | if (ilen == -1) | |
461 | { | |
462 | // Unicode mode wx extension: we'll let %hs mean non-Unicode | |
463 | // strings (when in ANSI mode, %s == %hs == ANSI string) | |
464 | m_type = wxPAT_PCHAR; | |
465 | } | |
466 | else if (ilen == 1) | |
467 | { | |
468 | // in Unicode mode, %ls == %s == Unicode string | |
469 | // in ANSI mode, %ls == Unicode string | |
470 | m_type = wxPAT_PWCHAR; | |
471 | } | |
472 | else | |
473 | { | |
474 | #if wxUSE_UNICODE | |
475 | m_type = wxPAT_PWCHAR; | |
476 | #else | |
477 | m_type = wxPAT_PCHAR; | |
478 | #endif | |
479 | } | |
480 | done = true; | |
481 | break; | |
482 | ||
483 | case wxT('n'): | |
484 | if (ilen == 0) | |
485 | m_type = wxPAT_NINT; | |
486 | else if (ilen == -1) | |
487 | m_type = wxPAT_NSHORTINT; | |
488 | else if (ilen >= 1) | |
489 | m_type = wxPAT_NLONGINT; | |
490 | done = true; | |
491 | break; | |
492 | ||
493 | default: | |
494 | // bad format, don't consider this an argument; | |
495 | // leave it unchanged | |
496 | return false; | |
497 | } | |
498 | ||
499 | if (flagofs == wxMAX_SVNPRINTF_FLAGBUFFER_LEN) | |
500 | { | |
501 | wxLogDebug(wxT("Too many flags specified for a single conversion specifier!")); | |
502 | return false; | |
503 | } | |
504 | } | |
505 | while (!done); | |
506 | ||
507 | return true; // parsing was successful | |
508 | } | |
509 | ||
510 | template<typename CharType> | |
511 | void wxPrintfConvSpec<CharType>::ReplaceAsteriskWith(int width) | |
512 | { | |
513 | char temp[wxMAX_SVNPRINTF_FLAGBUFFER_LEN]; | |
514 | ||
515 | // find the first * in our flag buffer | |
516 | char *pwidth = strchr(m_szFlags, '*'); | |
517 | wxCHECK_RET(pwidth, _T("field width must be specified")); | |
518 | ||
519 | // save what follows the * (the +1 is to skip the asterisk itself!) | |
520 | strcpy(temp, pwidth+1); | |
521 | if (width < 0) | |
522 | { | |
523 | pwidth[0] = wxT('-'); | |
524 | pwidth++; | |
525 | } | |
526 | ||
527 | // replace * with the actual integer given as width | |
528 | #ifndef SYSTEM_SPRINTF_IS_UNSAFE | |
529 | int maxlen = (m_szFlags + wxMAX_SVNPRINTF_FLAGBUFFER_LEN - pwidth) / | |
530 | sizeof(*m_szFlags); | |
531 | #endif | |
532 | int offset = system_sprintf(pwidth, maxlen, "%d", abs(width)); | |
533 | ||
534 | // restore after the expanded * what was following it | |
535 | strcpy(pwidth+offset, temp); | |
536 | } | |
537 | ||
538 | template<typename CharType> | |
539 | bool wxPrintfConvSpec<CharType>::LoadArg(wxPrintfArg *p, va_list &argptr) | |
540 | { | |
541 | // did the '*' width/precision specifier was used ? | |
542 | if (m_nMaxWidth == -1) | |
543 | { | |
544 | // take the maxwidth specifier from the stack | |
545 | m_nMaxWidth = va_arg(argptr, int); | |
546 | if (m_nMaxWidth < 0) | |
547 | m_nMaxWidth = 0; | |
548 | else | |
549 | ReplaceAsteriskWith(m_nMaxWidth); | |
550 | } | |
551 | ||
552 | if (m_nMinWidth == -1) | |
553 | { | |
554 | // take the minwidth specifier from the stack | |
555 | m_nMinWidth = va_arg(argptr, int); | |
556 | ||
557 | ReplaceAsteriskWith(m_nMinWidth); | |
558 | if (m_nMinWidth < 0) | |
559 | { | |
560 | m_bAlignLeft = !m_bAlignLeft; | |
561 | m_nMinWidth = -m_nMinWidth; | |
562 | } | |
563 | } | |
564 | ||
565 | switch (m_type) { | |
566 | case wxPAT_INT: | |
567 | p->pad_int = va_arg(argptr, int); | |
568 | break; | |
569 | case wxPAT_LONGINT: | |
570 | p->pad_longint = va_arg(argptr, long int); | |
571 | break; | |
572 | #ifdef wxLongLong_t | |
573 | case wxPAT_LONGLONGINT: | |
574 | p->pad_longlongint = va_arg(argptr, wxLongLong_t); | |
575 | break; | |
576 | #endif // wxLongLong_t | |
577 | case wxPAT_SIZET: | |
578 | p->pad_sizet = va_arg(argptr, size_t); | |
579 | break; | |
580 | case wxPAT_DOUBLE: | |
581 | p->pad_double = va_arg(argptr, double); | |
582 | break; | |
583 | case wxPAT_LONGDOUBLE: | |
584 | p->pad_longdouble = va_arg(argptr, long double); | |
585 | break; | |
586 | case wxPAT_POINTER: | |
587 | p->pad_pointer = va_arg(argptr, void *); | |
588 | break; | |
589 | ||
590 | case wxPAT_CHAR: | |
591 | p->pad_char = (char)va_arg(argptr, int); // char is promoted to int when passed through '...' | |
592 | break; | |
593 | case wxPAT_WCHAR: | |
594 | p->pad_wchar = (wchar_t)va_arg(argptr, int); // char is promoted to int when passed through '...' | |
595 | break; | |
596 | ||
597 | case wxPAT_PCHAR: | |
598 | case wxPAT_PWCHAR: | |
599 | p->pad_str = va_arg(argptr, void *); | |
600 | break; | |
601 | ||
602 | case wxPAT_NINT: | |
603 | p->pad_nint = va_arg(argptr, int *); | |
604 | break; | |
605 | case wxPAT_NSHORTINT: | |
606 | p->pad_nshortint = va_arg(argptr, short int *); | |
607 | break; | |
608 | case wxPAT_NLONGINT: | |
609 | p->pad_nlongint = va_arg(argptr, long int *); | |
610 | break; | |
611 | ||
612 | case wxPAT_INVALID: | |
613 | default: | |
614 | return false; | |
615 | } | |
616 | ||
617 | return true; // loading was successful | |
618 | } | |
619 | ||
620 | template<typename CharType> | |
621 | int wxPrintfConvSpec<CharType>::Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written) | |
622 | { | |
623 | // buffer to avoid dynamic memory allocation each time for small strings; | |
624 | // note that this buffer is used only to hold results of number formatting, | |
625 | // %s directly writes user's string in buf, without using szScratch | |
626 | char szScratch[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN]; | |
627 | size_t lenScratch = 0, lenCur = 0; | |
628 | ||
629 | #define APPEND_CH(ch) \ | |
630 | { \ | |
631 | if ( lenCur == lenMax ) \ | |
632 | return -1; \ | |
633 | \ | |
634 | buf[lenCur++] = ch; \ | |
635 | } | |
636 | ||
637 | switch ( m_type ) | |
638 | { | |
639 | case wxPAT_INT: | |
640 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_int); | |
641 | break; | |
642 | ||
643 | case wxPAT_LONGINT: | |
644 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longint); | |
645 | break; | |
646 | ||
647 | #ifdef wxLongLong_t | |
648 | case wxPAT_LONGLONGINT: | |
649 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longlongint); | |
650 | break; | |
651 | #endif // SIZEOF_LONG_LONG | |
652 | ||
653 | case wxPAT_SIZET: | |
654 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_sizet); | |
655 | break; | |
656 | ||
657 | case wxPAT_LONGDOUBLE: | |
658 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longdouble); | |
659 | break; | |
660 | ||
661 | case wxPAT_DOUBLE: | |
662 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_double); | |
663 | break; | |
664 | ||
665 | case wxPAT_POINTER: | |
666 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_pointer); | |
667 | break; | |
668 | ||
669 | case wxPAT_CHAR: | |
670 | case wxPAT_WCHAR: | |
671 | { | |
672 | wxUniChar ch; | |
673 | if (m_type == wxPAT_CHAR) | |
674 | ch = p->pad_char; | |
675 | else // m_type == wxPAT_WCHAR | |
676 | ch = p->pad_wchar; | |
677 | ||
678 | CharType val = ch; | |
679 | ||
680 | size_t i; | |
681 | ||
682 | if (!m_bAlignLeft) | |
683 | for (i = 1; i < (size_t)m_nMinWidth; i++) | |
684 | APPEND_CH(_T(' ')); | |
685 | ||
686 | APPEND_CH(val); | |
687 | ||
688 | if (m_bAlignLeft) | |
689 | for (i = 1; i < (size_t)m_nMinWidth; i++) | |
690 | APPEND_CH(_T(' ')); | |
691 | } | |
692 | break; | |
693 | ||
694 | case wxPAT_PCHAR: | |
695 | case wxPAT_PWCHAR: | |
696 | { | |
697 | wxArgNormalizedString arg(p->pad_str); | |
698 | wxString s = arg; | |
699 | ||
700 | if ( !arg.IsValid() && m_nMaxWidth >= 6 ) | |
701 | s = wxT("(null)"); | |
702 | ||
703 | typename wxPrintfStringHelper<CharType>::ConvertedType strbuf( | |
704 | wxPrintfStringHelper<CharType>::Convert(s)); | |
705 | ||
706 | // at this point we are sure that m_nMaxWidth is positive or | |
707 | // null (see top of wxPrintfConvSpec::LoadArg) | |
708 | int len = wxMin((unsigned int)m_nMaxWidth, wxStrlen(strbuf)); | |
709 | ||
710 | int i; | |
711 | ||
712 | if (!m_bAlignLeft) | |
713 | { | |
714 | for (i = len; i < m_nMinWidth; i++) | |
715 | APPEND_CH(_T(' ')); | |
716 | } | |
717 | ||
718 | len = wxMin((unsigned int)len, lenMax-lenCur); | |
719 | wxStrncpy(buf+lenCur, strbuf, len); | |
720 | lenCur += len; | |
721 | ||
722 | if (m_bAlignLeft) | |
723 | { | |
724 | for (i = len; i < m_nMinWidth; i++) | |
725 | APPEND_CH(_T(' ')); | |
726 | } | |
727 | } | |
728 | break; | |
729 | ||
730 | case wxPAT_NINT: | |
731 | *p->pad_nint = written; | |
732 | break; | |
733 | ||
734 | case wxPAT_NSHORTINT: | |
735 | *p->pad_nshortint = (short int)written; | |
736 | break; | |
737 | ||
738 | case wxPAT_NLONGINT: | |
739 | *p->pad_nlongint = written; | |
740 | break; | |
741 | ||
742 | case wxPAT_INVALID: | |
743 | default: | |
744 | return -1; | |
745 | } | |
746 | ||
747 | // if we used system's sprintf() then we now need to append the s_szScratch | |
748 | // buffer to the given one... | |
749 | switch (m_type) | |
750 | { | |
751 | case wxPAT_INT: | |
752 | case wxPAT_LONGINT: | |
753 | #ifdef wxLongLong_t | |
754 | case wxPAT_LONGLONGINT: | |
755 | #endif | |
756 | case wxPAT_SIZET: | |
757 | case wxPAT_LONGDOUBLE: | |
758 | case wxPAT_DOUBLE: | |
759 | case wxPAT_POINTER: | |
760 | wxASSERT(lenScratch < wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN); | |
761 | // NB: 1) we can compare lenMax (for CharType*, i.e. possibly | |
762 | // wchar_t*) with lenScratch (char*) because this code is | |
763 | // formatting integers and that will have the same length | |
764 | // even in UTF-8 (the only case when char* length may be | |
765 | // more than wchar_t* length of the same string) | |
766 | // 2) wxStrncpy converts the 2nd argument to 1st argument's | |
767 | // type transparently if their types differ, so this code | |
768 | // works for both instantiations | |
769 | if (lenMax < lenScratch) | |
770 | { | |
771 | // fill output buffer and then return -1 | |
772 | wxStrncpy(buf, szScratch, lenMax); | |
773 | return -1; | |
774 | } | |
775 | wxStrncpy(buf, szScratch, lenScratch); | |
776 | lenCur += lenScratch; | |
777 | break; | |
778 | ||
779 | default: | |
780 | break; // all other cases were completed previously | |
781 | } | |
782 | ||
783 | return lenCur; | |
784 | } | |
785 | ||
786 | ||
787 | // helper that parses format string | |
788 | template<typename CharType> | |
789 | struct wxPrintfConvSpecParser | |
790 | { | |
791 | wxPrintfConvSpecParser(const CharType *format) | |
792 | : posarg_present(false), nonposarg_present(false), | |
793 | nargs(0) | |
794 | { | |
795 | memset(pspec, 0, sizeof(pspec)); | |
796 | ||
797 | const CharType *toparse = format; | |
798 | ||
799 | // parse the format string | |
800 | for (; *toparse != wxT('\0'); toparse++) | |
801 | { | |
802 | if (*toparse == wxT('%') ) | |
803 | { | |
804 | arg[nargs].Init(); | |
805 | ||
806 | // let's see if this is a (valid) conversion specifier... | |
807 | if (arg[nargs].Parse(toparse)) | |
808 | { | |
809 | // ...yes it is | |
810 | wxPrintfConvSpec<CharType> *current = &arg[nargs]; | |
811 | ||
812 | // make toparse point to the end of this specifier | |
813 | toparse = current->m_pArgEnd; | |
814 | ||
815 | if (current->m_pos > 0) | |
816 | { | |
817 | // the positionals start from number 1... adjust the index | |
818 | current->m_pos--; | |
819 | posarg_present = true; | |
820 | } | |
821 | else | |
822 | { | |
823 | // not a positional argument... | |
824 | current->m_pos = nargs; | |
825 | nonposarg_present = true; | |
826 | } | |
827 | ||
828 | // this conversion specifier is tied to the pos-th argument... | |
829 | pspec[current->m_pos] = current; | |
830 | nargs++; | |
831 | ||
832 | if (nargs == wxMAX_SVNPRINTF_ARGUMENTS) | |
833 | { | |
834 | wxLogDebug(wxT("A single call to wxVsnprintf() has more than %d arguments; ") | |
835 | wxT("ignoring all remaining arguments."), wxMAX_SVNPRINTF_ARGUMENTS); | |
836 | break; // cannot handle any additional conv spec | |
837 | } | |
838 | } | |
839 | else | |
840 | { | |
841 | // it's safe to look in the next character of toparse as at | |
842 | // worst we'll hit its \0 | |
843 | if (*(toparse+1) == wxT('%')) | |
844 | { | |
845 | // the Parse() returned false because we've found a %% | |
846 | toparse++; | |
847 | } | |
848 | } | |
849 | } | |
850 | } | |
851 | } | |
852 | ||
853 | wxPrintfConvSpec<CharType> arg[wxMAX_SVNPRINTF_ARGUMENTS]; | |
854 | wxPrintfConvSpec<CharType> *pspec[wxMAX_SVNPRINTF_ARGUMENTS]; | |
855 | bool posarg_present, nonposarg_present; | |
856 | unsigned nargs; | |
857 | }; | |
858 | ||
859 | #undef APPEND_CH | |
860 | #undef CHECK_PREC | |
861 | ||
862 | #endif // _WX_PRIVATE_WXPRINTF_H_ |