]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
Warning fix.
[wxWidgets.git] / src / common / wxchar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wxchar.cpp
3 // Purpose: wxChar 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 // ===========================================================================
13 // headers, declarations, constants
14 // ===========================================================================
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/wxchar.h"
24
25 #define _ISOC9X_SOURCE 1 // to get vsscanf()
26 #define _BSD_SOURCE 1 // to still get strdup()
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifndef __WXWINCE__
33 #include <time.h>
34 #include <locale.h>
35 #else
36 #include "wx/msw/wince/time.h"
37 #endif
38
39 #ifndef WX_PRECOMP
40 #include "wx/string.h"
41 #include "wx/hash.h"
42 #include "wx/utils.h" // for wxMin and wxMax
43 #include "wx/log.h"
44 #endif
45
46 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
47 #include <windef.h>
48 #include <winbase.h>
49 #include <winnls.h>
50 #include <winnt.h>
51 #endif
52
53 #if defined(__MWERKS__) && __MSL__ >= 0x6000
54 namespace std {}
55 using namespace std ;
56 #endif
57
58 #if wxUSE_WCHAR_T
59 size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n)
60 {
61 // assume that we have mbsrtowcs() too if we have wcsrtombs()
62 #ifdef HAVE_WCSRTOMBS
63 mbstate_t mbstate;
64 memset(&mbstate, 0, sizeof(mbstate_t));
65 #endif
66
67 if (buf) {
68 if (!n || !*psz) {
69 if (n) *buf = wxT('\0');
70 return 0;
71 }
72 #ifdef HAVE_WCSRTOMBS
73 return mbsrtowcs(buf, &psz, n, &mbstate);
74 #else
75 return wxMbstowcs(buf, psz, n);
76 #endif
77 }
78
79 // note that we rely on common (and required by Unix98 but unfortunately not
80 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
81 // to just get the size of the needed buffer -- this is needed as otherwise
82 // we have no idea about how much space we need and if the CRT doesn't
83 // support it (the only currently known example being Metrowerks, see
84 // wx/wxchar.h) we don't use its mbstowcs() at all
85 #ifdef HAVE_WCSRTOMBS
86 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
87 #else
88 return wxMbstowcs((wchar_t *) NULL, psz, 0);
89 #endif
90 }
91
92 size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
93 {
94 #ifdef HAVE_WCSRTOMBS
95 mbstate_t mbstate;
96 memset(&mbstate, 0, sizeof(mbstate_t));
97 #endif
98
99 if (buf) {
100 if (!n || !*pwz) {
101 // glibc2.1 chokes on null input
102 if (n) *buf = '\0';
103 return 0;
104 }
105 #ifdef HAVE_WCSRTOMBS
106 return wcsrtombs(buf, &pwz, n, &mbstate);
107 #else
108 return wxWcstombs(buf, pwz, n);
109 #endif
110 }
111
112 #ifdef HAVE_WCSRTOMBS
113 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
114 #else
115 return wxWcstombs((char *) NULL, pwz, 0);
116 #endif
117 }
118 #endif // wxUSE_WCHAR_T
119
120 bool WXDLLEXPORT wxOKlibc()
121 {
122 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
123 // glibc 2.0 uses UTF-8 even when it shouldn't
124 wchar_t res = 0;
125 if ((MB_CUR_MAX == 2) &&
126 (wxMB2WC(&res, "\xdd\xa5", 1) == 1) &&
127 (res==0x765)) {
128 // this is UTF-8 allright, check whether that's what we want
129 char *cur_locale = setlocale(LC_CTYPE, NULL);
130 if ((strlen(cur_locale) < 4) ||
131 (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) ||
132 (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) {
133 // nope, don't use libc conversion
134 return false;
135 }
136 }
137 #endif
138 return true;
139 }
140
141 // ============================================================================
142 // printf() functions business
143 // ============================================================================
144
145 // special test mode: define all functions below even if we don't really need
146 // them to be able to test them
147 #ifdef wxTEST_PRINTF
148 #undef wxFprintf
149 #undef wxPrintf
150 #undef wxSprintf
151 #undef wxVfprintf
152 #undef wxVsprintf
153 #undef wxVprintf
154 #undef wxVsnprintf_
155 #undef wxSnprintf_
156
157 #define wxNEED_WPRINTF
158
159 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr );
160 #endif
161
162 // ----------------------------------------------------------------------------
163 // implement [v]snprintf() if the system doesn't provide a safe one
164 // or if the system's one does not support positional parameters
165 // (very useful for i18n purposes)
166 // ----------------------------------------------------------------------------
167
168 #if !defined(wxVsnprintf_)
169
170 // wxUSE_STRUTILS says our wxVsnprintf_ implementation to use or not to
171 // use wxStrlen and wxStrncpy functions over one-char processing loops.
172 //
173 // Some benchmarking revealed that wxUSE_STRUTILS == 1 has the following
174 // effects:
175 // -> on Windows:
176 // when in ANSI mode, this setting does not change almost anything
177 // when in Unicode mode, it gives ~ 50% of slowdown !
178 // -> on Linux:
179 // both in ANSI and Unicode mode it gives ~ 60% of speedup !
180 //
181 #if defined(WIN32) && wxUSE_UNICODE
182 #define wxUSE_STRUTILS 0
183 #else
184 #define wxUSE_STRUTILS 1
185 #endif
186
187 // some limits of our implementation
188 #define wxMAX_SVNPRINTF_ARGUMENTS 16
189 #define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32
190 #define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512
191
192
193 // wxVsnprintf() needs to use a *system* implementation of swnprintf()
194 // in order to perform some internal tasks.
195 // NB: we cannot just use wxSnprintf() because for some systems it maybe
196 // implemented later in this file using wxVsnprintf() and that would
197 // result in an endless recursion and thus in a stack overflow
198 #if wxUSE_UNICODE
199
200 #if defined(__WINDOWS__)
201 // all compilers under Windows should have swprintf()
202 #define HAVE_SWPRINTF
203 #endif
204
205 // NB: MSVC 6 has only non-standard swprintf() declaration and while MSVC 7
206 // and 7.1 do have the standard one, it's completely broken unless
207 // /Zc:wchar_t is used while the other one works so use it instead, and
208 // only VC8 has a working standard-compliant swprintf()
209 #if defined(__WXWINCE__) || \
210 (defined(__VISUALC__) && __VISUALC__ < 1400) || \
211 defined(__GNUWIN32__) || \
212 defined(__BORLANDC__)
213 #define HAVE_BROKEN_SWPRINTF_DECL
214 #endif
215
216
217 // problem: on some systems swprintf takes the 'max' argument while on others
218 // it doesn't
219 #if defined(HAVE_BROKEN_SWPRINTF_DECL)
220
221 // like when using sprintf(), since 'max' is not used, wxVsnprintf() should
222 // always ensure that 'buff' is big enough for all common needs
223 #define system_sprintf(buff, max, flags, data) \
224 ::swprintf(buff, flags, data)
225 #else
226
227 #if !defined(HAVE_SWPRINTF)
228 #error wxVsnprintf() needs a system swprintf() implementation!
229 #endif
230
231 #define system_sprintf(buff, max, flags, data) \
232 ::swprintf(buff, max, flags, data)
233 #endif
234
235 #else
236
237 #if defined(HAVE_SNPRINTF)
238
239 #define system_sprintf(buff, max, flags, data) \
240 ::snprintf(buff, max, flags, data)
241
242 #else // NB: at least sprintf() should *always* be available
243
244 // since 'max' is not used in this case, wxVsnprintf() should always ensure
245 // that 'buff' is big enough for all common needs
246 // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN)
247 #define system_sprintf(buff, max, flags, data) \
248 ::sprintf(buff, flags, data)
249
250 #endif
251 #endif
252
253
254
255 // the conversion specifiers accepted by wxVsnprintf_
256 enum wxPrintfArgType {
257 wxPAT_INVALID = -1,
258
259 wxPAT_INT, // %d, %i, %o, %u, %x, %X
260 wxPAT_LONGINT, // %ld, etc
261 #if SIZEOF_LONG_LONG
262 wxPAT_LONGLONGINT, // %Ld, etc
263 #endif
264 wxPAT_SIZET, // %Zd, etc
265
266 wxPAT_DOUBLE, // %e, %E, %f, %g, %G
267 wxPAT_LONGDOUBLE, // %le, etc
268
269 wxPAT_POINTER, // %p
270
271 wxPAT_CHAR, // %hc (in ANSI mode: %c, too)
272 wxPAT_WCHAR, // %lc (in Unicode mode: %c, too)
273
274 wxPAT_PCHAR, // %s (related to a char *)
275 wxPAT_PWCHAR, // %s (related to a wchar_t *)
276
277 wxPAT_NINT, // %n
278 wxPAT_NSHORTINT, // %hn
279 wxPAT_NLONGINT // %ln
280 };
281
282 // an argument passed to wxVsnprintf_
283 typedef union {
284 int pad_int; // %d, %i, %o, %u, %x, %X
285 long int pad_longint; // %ld, etc
286 #if SIZEOF_LONG_LONG
287 long long int pad_longlongint; // %Ld, etc
288 #endif
289 size_t pad_sizet; // %Zd, etc
290
291 double pad_double; // %e, %E, %f, %g, %G
292 long double pad_longdouble; // %le, etc
293
294 void *pad_pointer; // %p
295
296 char pad_char; // %hc (in ANSI mode: %c, too)
297 wchar_t pad_wchar; // %lc (in Unicode mode: %c, too)
298
299 char *pad_pchar; // %s (related to a char *)
300 wchar_t *pad_pwchar; // %s (related to a wchar_t *)
301
302 int *pad_nint; // %n
303 short int *pad_nshortint; // %hn
304 long int *pad_nlongint; // %ln
305 } wxPrintfArg;
306
307
308 // Contains parsed data relative to a conversion specifier given to
309 // wxVsnprintf_ and parsed from the format string
310 // NOTE: in C++ there is almost no difference between struct & classes thus
311 // there is no performance gain by using a struct here...
312 class wxPrintfConvSpec
313 {
314 public:
315
316 // the position of the argument relative to this conversion specifier
317 size_t m_pos;
318
319 // the type of this conversion specifier
320 wxPrintfArgType m_type;
321
322 // the minimum and maximum width
323 // when one of this var is set to -1 it means: use the following argument
324 // in the stack as minimum/maximum width for this conversion specifier
325 int m_nMinWidth, m_nMaxWidth;
326
327 // does the argument need to the be aligned to left ?
328 bool m_bAlignLeft;
329
330 // pointer to the '%' of this conversion specifier in the format string
331 // NOTE: this points somewhere in the string given to the Parse() function -
332 // it's task of the caller ensure that memory is still valid !
333 const wxChar *m_pArgPos;
334
335 // pointer to the last character of this conversion specifier in the
336 // format string
337 // NOTE: this points somewhere in the string given to the Parse() function -
338 // it's task of the caller ensure that memory is still valid !
339 const wxChar *m_pArgEnd;
340
341 // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse()
342 // for use in Process()
343 // NB: even if this buffer is used only for numeric conversion specifiers and
344 // thus could be safely declared as a char[] buffer, we want it to be wxChar
345 // so that in Unicode builds we can avoid to convert its contents to Unicode
346 // chars when copying it in user's buffer.
347 wxChar m_szFlags[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
348
349
350 public:
351
352 // we don't declare this as a constructor otherwise it would be called
353 // automatically and we don't want this: to be optimized, wxVsnprintf_
354 // calls this function only on really-used instances of this class.
355 void Init();
356
357 // Parses the first conversion specifier in the given string, which must
358 // begin with a '%'. Returns false if the first '%' does not introduce a
359 // (valid) conversion specifier and thus should be ignored.
360 bool Parse(const wxChar *format);
361
362 // Process this conversion specifier and puts the result in the given
363 // buffer. Returns the number of characters written in 'buf' or -1 if
364 // there's not enough space.
365 int Process(wxChar *buf, size_t lenMax, wxPrintfArg *p);
366
367 // Loads the argument of this conversion specifier from given va_list.
368 bool LoadArg(wxPrintfArg *p, va_list &argptr);
369
370 private:
371 // An helper function of LoadArg() which is used to handle the '*' flag
372 void ReplaceAsteriskWith(int w);
373 };
374
375 void wxPrintfConvSpec::Init()
376 {
377 m_nMinWidth = 0;
378 m_nMaxWidth = 0xFFFF;
379 m_pos = 0;
380 m_bAlignLeft = false;
381 m_pArgPos = m_pArgEnd = NULL;
382 m_type = wxPAT_INVALID;
383
384 // this character will never be removed from m_szFlags array and
385 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
386 m_szFlags[0] = wxT('%');
387 }
388
389 bool wxPrintfConvSpec::Parse(const wxChar *format)
390 {
391 bool done = false;
392
393 // temporary parse data
394 size_t flagofs = 1;
395 bool in_prec, prec_dot;
396 int ilen = 0;
397
398 m_bAlignLeft = in_prec = prec_dot = false;
399 m_pArgPos = m_pArgEnd = format;
400 do
401 {
402 #define CHECK_PREC \
403 if (in_prec && !prec_dot) \
404 { \
405 m_szFlags[flagofs++] = wxT('.'); \
406 prec_dot = true; \
407 }
408
409 // what follows '%'?
410 const wxChar ch = *(++m_pArgEnd);
411 switch ( ch )
412 {
413 case wxT('\0'):
414 return false; // not really an argument
415
416 case wxT('%'):
417 return false; // not really an argument
418
419 case wxT('#'):
420 case wxT('0'):
421 case wxT(' '):
422 case wxT('+'):
423 case wxT('\''):
424 CHECK_PREC
425 m_szFlags[flagofs++] = ch;
426 break;
427
428 case wxT('-'):
429 CHECK_PREC
430 m_bAlignLeft = true;
431 m_szFlags[flagofs++] = ch;
432 break;
433
434 case wxT('.'):
435 CHECK_PREC
436 in_prec = true;
437 prec_dot = false;
438 m_nMaxWidth = 0;
439 // dot will be auto-added to m_szFlags if non-negative
440 // number follows
441 break;
442
443 case wxT('h'):
444 ilen = -1;
445 CHECK_PREC
446 m_szFlags[flagofs++] = ch;
447 break;
448
449 case wxT('l'):
450 // NB: it's safe to use flagofs-1 as flagofs always start from 1
451 if (m_szFlags[flagofs-1] == 'l') // 'll' modifier is the same as 'L' or 'q'
452 ilen = 2;
453 else
454 ilen = 1;
455 CHECK_PREC
456 m_szFlags[flagofs++] = ch;
457 break;
458
459 case wxT('q'):
460 case wxT('L'):
461 ilen = 2;
462 CHECK_PREC
463 m_szFlags[flagofs++] = ch;
464 break;
465
466 case wxT('Z'):
467 ilen = 3;
468 CHECK_PREC
469 m_szFlags[flagofs++] = ch;
470 break;
471
472 case wxT('*'):
473 if (in_prec)
474 {
475 CHECK_PREC
476
477 // tell Process() to use the next argument
478 // in the stack as maxwidth...
479 m_nMaxWidth = -1;
480 }
481 else
482 {
483 // tell Process() to use the next argument
484 // in the stack as minwidth...
485 m_nMinWidth = -1;
486 }
487
488 // save the * in our formatting buffer...
489 // will be replaced later by Process()
490 m_szFlags[flagofs++] = ch;
491 break;
492
493 case wxT('1'): case wxT('2'): case wxT('3'):
494 case wxT('4'): case wxT('5'): case wxT('6'):
495 case wxT('7'): case wxT('8'): case wxT('9'):
496 {
497 int len = 0;
498 CHECK_PREC
499 while ( (*m_pArgEnd >= wxT('0')) &&
500 (*m_pArgEnd <= wxT('9')) )
501 {
502 m_szFlags[flagofs++] = (*m_pArgEnd);
503 len = len*10 + (*m_pArgEnd - wxT('0'));
504 m_pArgEnd++;
505 }
506
507 if (in_prec)
508 m_nMaxWidth = len;
509 else
510 m_nMinWidth = len;
511
512 m_pArgEnd--; // the main loop pre-increments n again
513 }
514 break;
515
516 case wxT('$'): // a positional parameter (e.g. %2$s) ?
517 {
518 if (m_nMinWidth <= 0)
519 break; // ignore this formatting flag as no
520 // numbers are preceding it
521
522 // remove from m_szFlags all digits previously added
523 do {
524 flagofs--;
525 } while (m_szFlags[flagofs] >= '1' &&
526 m_szFlags[flagofs] <= '9');
527
528 // re-adjust the offset making it point to the
529 // next free char of m_szFlags
530 flagofs++;
531
532 m_pos = m_nMinWidth;
533 m_nMinWidth = 0;
534 }
535 break;
536
537 case wxT('d'):
538 case wxT('i'):
539 case wxT('o'):
540 case wxT('u'):
541 case wxT('x'):
542 case wxT('X'):
543 CHECK_PREC
544 m_szFlags[flagofs++] = ch;
545 m_szFlags[flagofs] = wxT('\0');
546 if (ilen == 0)
547 m_type = wxPAT_INT;
548 else if (ilen == -1)
549 // NB: 'short int' value passed through '...'
550 // is promoted to 'int', so we have to get
551 // an int from stack even if we need a short
552 m_type = wxPAT_INT;
553 else if (ilen == 1)
554 m_type = wxPAT_LONGINT;
555 else if (ilen == 2)
556 #if SIZEOF_LONG_LONG
557 m_type = wxPAT_LONGLONGINT;
558 #else // !long long
559 m_type = wxPAT_LONGINT;
560 #endif // long long/!long long
561 else if (ilen == 3)
562 m_type = wxPAT_SIZET;
563 done = true;
564 break;
565
566 case wxT('e'):
567 case wxT('E'):
568 case wxT('f'):
569 case wxT('g'):
570 case wxT('G'):
571 CHECK_PREC
572 m_szFlags[flagofs++] = ch;
573 m_szFlags[flagofs] = wxT('\0');
574 if (ilen == 2)
575 m_type = wxPAT_LONGDOUBLE;
576 else
577 m_type = wxPAT_DOUBLE;
578 done = true;
579 break;
580
581 case wxT('p'):
582 m_type = wxPAT_POINTER;
583 done = true;
584 break;
585
586 case wxT('c'):
587 if (ilen == -1)
588 {
589 // in Unicode mode %hc == ANSI character
590 // and in ANSI mode, %hc == %c == ANSI...
591 m_type = wxPAT_CHAR;
592 }
593 else if (ilen == 1)
594 {
595 // in ANSI mode %lc == Unicode character
596 // and in Unicode mode, %lc == %c == Unicode...
597 m_type = wxPAT_WCHAR;
598 }
599 else
600 {
601 #if wxUSE_UNICODE
602 // in Unicode mode, %c == Unicode character
603 m_type = wxPAT_WCHAR;
604 #else
605 // in ANSI mode, %c == ANSI character
606 m_type = wxPAT_CHAR;
607 #endif
608 }
609 done = true;
610 break;
611
612 case wxT('s'):
613 if (ilen == -1)
614 {
615 // Unicode mode wx extension: we'll let %hs mean non-Unicode
616 // strings (when in ANSI mode, %s == %hs == ANSI string)
617 m_type = wxPAT_PCHAR;
618 }
619 else if (ilen == 1)
620 {
621 // in Unicode mode, %ls == %s == Unicode string
622 // in ANSI mode, %ls == Unicode string
623 m_type = wxPAT_PWCHAR;
624 }
625 else
626 {
627 #if wxUSE_UNICODE
628 m_type = wxPAT_PWCHAR;
629 #else
630 m_type = wxPAT_PCHAR;
631 #endif
632 }
633 done = true;
634 break;
635
636 case wxT('n'):
637 if (ilen == 0)
638 m_type = wxPAT_NINT;
639 else if (ilen == -1)
640 m_type = wxPAT_NSHORTINT;
641 else if (ilen >= 1)
642 m_type = wxPAT_NLONGINT;
643 done = true;
644 break;
645
646 default:
647 // bad format, don't consider this an argument;
648 // leave it unchanged
649 return false;
650 }
651
652 if (flagofs == wxMAX_SVNPRINTF_FLAGBUFFER_LEN)
653 {
654 wxLogDebug(wxT("Too many flags specified for a single conversion specifier!"));
655 return false;
656 }
657 }
658 while (!done);
659
660 return true; // parsing was successful
661 }
662
663
664 void wxPrintfConvSpec::ReplaceAsteriskWith(int width)
665 {
666 wxChar temp[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
667
668 // find the first * in our flag buffer
669 wxChar *pwidth = wxStrchr(m_szFlags, wxT('*'));
670 wxASSERT(pwidth);
671
672 // save what follows the * (the +1 is to skip the asterisk itself!)
673 wxStrcpy(temp, pwidth+1);
674 if (width < 0)
675 {
676 pwidth[0] = wxT('-');
677 pwidth++;
678 }
679
680 // replace * with the actual integer given as width
681 int maxlen = (m_szFlags + wxMAX_SVNPRINTF_FLAGBUFFER_LEN - pwidth) / sizeof(wxChar);
682 int offset = system_sprintf(pwidth, maxlen, wxT("%d"), abs(width));
683
684 #ifdef HAVE_BROKEN_SWPRINTF_DECL
685 wxUnusedVar(maxlen); // avoid dummy warnings
686 #endif
687
688 // restore after the expanded * what was following it
689 wxStrcpy(pwidth+offset, temp);
690 }
691
692 bool wxPrintfConvSpec::LoadArg(wxPrintfArg *p, va_list &argptr)
693 {
694 // did the '*' width/precision specifier was used ?
695 if (m_nMaxWidth == -1)
696 {
697 // take the maxwidth specifier from the stack
698 m_nMaxWidth = va_arg(argptr, int);
699 if (m_nMaxWidth < 0)
700 m_nMaxWidth = 0;
701 else
702 ReplaceAsteriskWith(m_nMaxWidth);
703 }
704
705 if (m_nMinWidth == -1)
706 {
707 // take the minwidth specifier from the stack
708 m_nMinWidth = va_arg(argptr, int);
709
710 ReplaceAsteriskWith(m_nMinWidth);
711 if (m_nMinWidth < 0)
712 {
713 m_bAlignLeft = !m_bAlignLeft;
714 m_nMinWidth = -m_nMinWidth;
715 }
716 }
717
718 switch (m_type) {
719 case wxPAT_INT:
720 p->pad_int = va_arg(argptr, int);
721 break;
722 case wxPAT_LONGINT:
723 p->pad_longint = va_arg(argptr, long int);
724 break;
725 #if SIZEOF_LONG_LONG
726 case wxPAT_LONGLONGINT:
727 p->pad_longlongint = va_arg(argptr, long long int);
728 break;
729 #endif
730 case wxPAT_SIZET:
731 p->pad_sizet = va_arg(argptr, size_t);
732 break;
733 case wxPAT_DOUBLE:
734 p->pad_double = va_arg(argptr, double);
735 break;
736 case wxPAT_LONGDOUBLE:
737 p->pad_longdouble = va_arg(argptr, long double);
738 break;
739 case wxPAT_POINTER:
740 p->pad_pointer = va_arg(argptr, void *);
741 break;
742
743 case wxPAT_CHAR:
744 p->pad_char = (char)va_arg(argptr, int); // char is promoted to int when passed through '...'
745 break;
746 case wxPAT_WCHAR:
747 p->pad_wchar = (wchar_t)va_arg(argptr, int); // char is promoted to int when passed through '...'
748 break;
749
750 case wxPAT_PCHAR:
751 p->pad_pchar = va_arg(argptr, char *);
752 break;
753 case wxPAT_PWCHAR:
754 p->pad_pwchar = va_arg(argptr, wchar_t *);
755 break;
756
757 case wxPAT_NINT:
758 p->pad_nint = va_arg(argptr, int *);
759 break;
760 case wxPAT_NSHORTINT:
761 p->pad_nshortint = va_arg(argptr, short int *);
762 break;
763 case wxPAT_NLONGINT:
764 p->pad_nlongint = va_arg(argptr, long int *);
765 break;
766
767 case wxPAT_INVALID:
768 default:
769 return false;
770 }
771
772 return true; // loading was successful
773 }
774
775 int wxPrintfConvSpec::Process(wxChar *buf, size_t lenMax, wxPrintfArg *p)
776 {
777 // buffer to avoid dynamic memory allocation each time for small strings;
778 // note that this buffer is used only to hold results of number formatting,
779 // %s directly writes user's string in buf, without using szScratch
780 wxChar szScratch[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN];
781 size_t lenScratch = 0, lenCur = 0;
782
783 #define APPEND_CH(ch) \
784 { \
785 if ( lenCur == lenMax ) \
786 return -1; \
787 \
788 buf[lenCur++] = ch; \
789 }
790
791 #define APPEND_STR(s) \
792 { \
793 for ( const wxChar *p = s; *p; p++ ) \
794 { \
795 APPEND_CH(*p); \
796 } \
797 }
798
799 switch ( m_type )
800 {
801 case wxPAT_INT:
802 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_int);
803 break;
804
805 case wxPAT_LONGINT:
806 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longint);
807 break;
808
809 #if SIZEOF_LONG_LONG
810 case wxPAT_LONGLONGINT:
811 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longlongint);
812 break;
813 #endif // SIZEOF_LONG_LONG
814
815 case wxPAT_SIZET:
816 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_sizet);
817 break;
818
819 case wxPAT_LONGDOUBLE:
820 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longdouble);
821 break;
822
823 case wxPAT_DOUBLE:
824 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_double);
825 break;
826
827 case wxPAT_POINTER:
828 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_pointer);
829 break;
830
831 case wxPAT_CHAR:
832 case wxPAT_WCHAR:
833 {
834 wxChar val =
835 #if wxUSE_UNICODE
836 p->pad_wchar;
837
838 if (m_type == wxPAT_CHAR)
839 {
840 // user passed a character explicitely indicated as ANSI...
841 const char buf[2] = { p->pad_char, 0 };
842 val = wxString(buf, wxConvLibc)[0u];
843
844 //wprintf(L"converting ANSI=>Unicode"); // for debug
845 }
846 #else
847 p->pad_char;
848
849 #if wxUSE_WCHAR_T
850 if (m_type == wxPAT_WCHAR)
851 {
852 // user passed a character explicitely indicated as Unicode...
853 const wchar_t buf[2] = { p->pad_wchar, 0 };
854 val = wxString(buf, wxConvLibc)[0u];
855
856 //printf("converting Unicode=>ANSI"); // for debug
857 }
858 #endif
859 #endif
860
861 size_t i;
862
863 if (!m_bAlignLeft)
864 for (i = 1; i < (size_t)m_nMinWidth; i++)
865 APPEND_CH(_T(' '));
866
867 APPEND_CH(val);
868
869 if (m_bAlignLeft)
870 for (i = 1; i < (size_t)m_nMinWidth; i++)
871 APPEND_CH(_T(' '));
872 }
873 break;
874
875 case wxPAT_PCHAR:
876 case wxPAT_PWCHAR:
877 {
878 wxString s;
879 const wxChar *val =
880 #if wxUSE_UNICODE
881 p->pad_pwchar;
882
883 if (m_type == wxPAT_PCHAR)
884 {
885 // user passed a string explicitely indicated as ANSI...
886 val = s = wxString(p->pad_pchar, wxConvLibc);
887
888 //wprintf(L"converting ANSI=>Unicode"); // for debug
889 }
890 #else
891 p->pad_pchar;
892
893 #if wxUSE_WCHAR_T
894 if (m_type == wxPAT_PWCHAR)
895 {
896 // user passed a string explicitely indicated as Unicode...
897 val = s = wxString(p->pad_pwchar, wxConvLibc);
898
899 //printf("converting Unicode=>ANSI"); // for debug
900 }
901 #endif
902 #endif
903 int len;
904
905 if (val)
906 {
907 #if wxUSE_STRUTILS
908 // at this point we are sure that m_nMaxWidth is positive or null
909 // (see top of wxPrintfConvSpec::LoadArg)
910 len = wxMin((unsigned int)m_nMaxWidth, wxStrlen(val));
911 #else
912 for ( len = 0; val[len] && (len < m_nMaxWidth); len++ )
913 ;
914 #endif
915 }
916 else if (m_nMaxWidth >= 6)
917 {
918 val = wxT("(null)");
919 len = 6;
920 }
921 else
922 {
923 val = wxEmptyString;
924 len = 0;
925 }
926
927 int i;
928
929 if (!m_bAlignLeft)
930 {
931 for (i = len; i < m_nMinWidth; i++)
932 APPEND_CH(_T(' '));
933 }
934
935 #if wxUSE_STRUTILS
936 len = wxMin((unsigned int)len, lenMax-lenCur);
937 wxStrncpy(buf+lenCur, val, len);
938 lenCur += len;
939 #else
940 for (i = 0; i < len; i++)
941 APPEND_CH(val[i]);
942 #endif
943
944 if (m_bAlignLeft)
945 {
946 for (i = len; i < m_nMinWidth; i++)
947 APPEND_CH(_T(' '));
948 }
949 }
950 break;
951
952 case wxPAT_NINT:
953 *p->pad_nint = lenCur;
954 break;
955
956 case wxPAT_NSHORTINT:
957 *p->pad_nshortint = (short int)lenCur;
958 break;
959
960 case wxPAT_NLONGINT:
961 *p->pad_nlongint = lenCur;
962 break;
963
964 case wxPAT_INVALID:
965 default:
966 return -1;
967 }
968
969 #ifdef HAVE_BROKEN_SWPRINTF_DECL
970 wxUnusedVar(lenScratch); // avoid dummy warnings
971 #endif
972
973 // if we used system's sprintf() then we now need to append the s_szScratch
974 // buffer to the given one...
975 switch (m_type)
976 {
977 case wxPAT_INT:
978 case wxPAT_LONGINT:
979 #if SIZEOF_LONG_LONG
980 case wxPAT_LONGLONGINT:
981 #endif
982 case wxPAT_SIZET:
983 case wxPAT_LONGDOUBLE:
984 case wxPAT_DOUBLE:
985 case wxPAT_POINTER:
986 #if wxUSE_STRUTILS
987 {
988 wxASSERT(lenScratch >= 0 && lenScratch < wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN);
989 if (lenMax < lenScratch)
990 {
991 // fill output buffer and then return -1
992 wxStrncpy(buf, szScratch, lenMax);
993 return -1;
994 }
995 wxStrncpy(buf, szScratch, lenScratch);
996 lenCur += lenScratch;
997 }
998 #else
999 {
1000 APPEND_STR(szScratch);
1001 }
1002 #endif
1003 break;
1004
1005 default:
1006 break; // all other cases were completed previously
1007 }
1008
1009 return lenCur;
1010 }
1011
1012 // differences from standard strncpy:
1013 // 1) copies everything from 'source' except for '%%' sequence which is copied as '%'
1014 // 2) returns the number of written characters in 'dest' as it could differ from given 'n'
1015 // 3) much less optimized, unfortunately...
1016 static int wxCopyStrWithPercents(wxChar *dest, const wxChar *source, size_t n)
1017 {
1018 size_t written = 0;
1019
1020 if (n == 0)
1021 return 0;
1022
1023 size_t i;
1024 for ( i = 0; i < n-1; source++, i++)
1025 {
1026 dest[written++] = *source;
1027 if (*(source+1) == wxT('%'))
1028 {
1029 // skip this additional '%' character
1030 source++;
1031 i++;
1032 }
1033 }
1034
1035 if (i < n)
1036 // copy last character inconditionally
1037 dest[written++] = *source;
1038
1039 return written;
1040 }
1041
1042 int WXDLLEXPORT wxVsnprintf_(wxChar *buf, size_t lenMax,
1043 const wxChar *format, va_list argptr)
1044 {
1045 // useful for debugging, to understand if we are really using this function
1046 // rather than the system implementation
1047 #if 0
1048 wprintf(L"Using wxVsnprintf_\n");
1049 #endif
1050
1051 // required memory:
1052 wxPrintfConvSpec arg[wxMAX_SVNPRINTF_ARGUMENTS];
1053 wxPrintfArg argdata[wxMAX_SVNPRINTF_ARGUMENTS];
1054 wxPrintfConvSpec *pspec[wxMAX_SVNPRINTF_ARGUMENTS] = { NULL };
1055
1056 size_t i;
1057
1058 // number of characters in the buffer so far, must be less than lenMax
1059 size_t lenCur = 0;
1060
1061 size_t nargs = 0;
1062 const wxChar *toparse = format;
1063
1064 // parse the format string
1065 bool posarg_present = false, nonposarg_present = false;
1066 for (; *toparse != wxT('\0'); toparse++)
1067 {
1068 if (*toparse == wxT('%') )
1069 {
1070 arg[nargs].Init();
1071
1072 // let's see if this is a (valid) conversion specifier...
1073 if (arg[nargs].Parse(toparse))
1074 {
1075 // ...yes it is
1076 wxPrintfConvSpec *current = &arg[nargs];
1077
1078 // make toparse point to the end of this specifier
1079 toparse = current->m_pArgEnd;
1080
1081 if (current->m_pos > 0)
1082 {
1083 // the positionals start from number 1... adjust the index
1084 current->m_pos--;
1085 posarg_present = true;
1086 }
1087 else
1088 {
1089 // not a positional argument...
1090 current->m_pos = nargs;
1091 nonposarg_present = true;
1092 }
1093
1094 // this conversion specifier is tied to the pos-th argument...
1095 pspec[current->m_pos] = current;
1096 nargs++;
1097
1098 if (nargs == wxMAX_SVNPRINTF_ARGUMENTS)
1099 {
1100 wxLogDebug(wxT("A single call to wxVsnprintf() has more than %d arguments; ")
1101 wxT("ignoring all remaining arguments."), wxMAX_SVNPRINTF_ARGUMENTS);
1102 break; // cannot handle any additional conv spec
1103 }
1104 }
1105 else
1106 {
1107 // it's safe to look in the next character of toparse as at worst
1108 // we'll hit its \0
1109 if (*(toparse+1) == wxT('%'))
1110 toparse++; // the Parse() returned false because we've found a %%
1111 }
1112 }
1113 }
1114
1115 if (posarg_present && nonposarg_present)
1116 return -1; // format strings with both positional and
1117 // non-positional conversion specifier are unsupported !!
1118
1119 // on platforms where va_list is an array type, it is necessary to make a
1120 // copy to be able to pass it to LoadArg as a reference.
1121 bool ok = true;
1122 va_list ap;
1123 wxVaCopy(ap, argptr);
1124
1125 // now load arguments from stack
1126 for (i=0; i < nargs && ok; i++)
1127 {
1128 // !pspec[i] means that the user forgot a positional parameter (e.g. %$1s %$3s);
1129 // LoadArg == false means that wxPrintfConvSpec::Parse failed to set the
1130 // conversion specifier 'type' to a valid value...
1131 ok = pspec[i] && pspec[i]->LoadArg(&argdata[i], ap);
1132 }
1133
1134 va_end(ap);
1135
1136 // something failed while loading arguments from the variable list...
1137 if (!ok)
1138 return -1;
1139
1140 // finally, process each conversion specifier with its own argument
1141 toparse = format;
1142 for (i=0; i < nargs; i++)
1143 {
1144 // copy in the output buffer the portion of the format string between
1145 // last specifier and the current one
1146 size_t tocopy = ( arg[i].m_pArgPos - toparse );
1147 if (lenCur+tocopy >= lenMax)
1148 {
1149 // not enough space in the output buffer !
1150 // copy until the end of remaining space and then stop
1151 wxCopyStrWithPercents(buf+lenCur, toparse, lenMax - lenCur - 1);
1152 buf[lenMax-1] = wxT('\0');
1153 return -1;
1154 }
1155
1156 lenCur += wxCopyStrWithPercents(buf+lenCur, toparse, tocopy);
1157
1158 // process this specifier directly in the output buffer
1159 int n = arg[i].Process(buf+lenCur, lenMax - lenCur, &argdata[arg[i].m_pos]);
1160 if (n == -1)
1161 {
1162 buf[lenMax-1] = wxT('\0'); // be sure to always NUL-terminate the string
1163 return -1; // not enough space in the output buffer !
1164 }
1165 lenCur += n;
1166
1167 // the +1 is because wxPrintfConvSpec::m_pArgEnd points to the last character
1168 // of the format specifier, but we are not interested to it...
1169 toparse = arg[i].m_pArgEnd + 1;
1170 }
1171
1172 // copy portion of the format string after last specifier
1173 // NOTE: toparse is pointing to the character just after the last processed
1174 // conversion specifier
1175 // NOTE2: the +1 is because we want to copy also the '\0'
1176 size_t tocopy = wxStrlen(format) + 1 - ( toparse - format ) ;
1177 if (lenCur+tocopy >= lenMax)
1178 return -1; // not enough space in the output buffer !
1179
1180 // the -1 is because of the '\0'
1181 lenCur += wxCopyStrWithPercents(buf+lenCur, toparse, tocopy) - 1;
1182
1183 wxASSERT(lenCur == wxStrlen(buf));
1184 return lenCur;
1185 }
1186
1187 #undef APPEND_CH
1188 #undef APPEND_STR
1189 #undef CHECK_PREC
1190
1191 #endif // !wxVsnprintfA
1192
1193 #if !defined(wxSnprintf_)
1194 int WXDLLEXPORT wxSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...)
1195 {
1196 va_list argptr;
1197 va_start(argptr, format);
1198
1199 int iLen = wxVsnprintf_(buf, len, format, argptr);
1200
1201 va_end(argptr);
1202
1203 return iLen;
1204 }
1205 #endif // wxSnprintf_
1206
1207 #if defined(__DMC__)
1208 /* Digital Mars adds count to _stprintf (C99) so convert */
1209 #if wxUSE_UNICODE
1210 int wxSprintf (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... )
1211 {
1212 va_list arglist;
1213
1214 va_start( arglist, format );
1215 int iLen = swprintf ( s, -1, format, arglist );
1216 va_end( arglist );
1217 return iLen ;
1218 }
1219
1220 #endif // wxUSE_UNICODE
1221
1222 #endif //__DMC__
1223
1224 // ----------------------------------------------------------------------------
1225 // implement the standard IO functions for wide char if libc doesn't have them
1226 // ----------------------------------------------------------------------------
1227
1228 #ifdef wxNEED_FPUTS
1229 int wxFputs(const wchar_t *ws, FILE *stream)
1230 {
1231 // counting the number of wide characters written isn't worth the trouble,
1232 // simply distinguish between ok and error
1233 return fputs(wxConvLibc.cWC2MB(ws), stream) == -1 ? -1 : 0;
1234 }
1235 #endif // wxNEED_FPUTS
1236
1237 #ifdef wxNEED_PUTS
1238 int wxPuts(const wxChar *ws)
1239 {
1240 int rc = wxFputs(ws, stdout);
1241 if ( rc != -1 )
1242 {
1243 if ( wxFputs(L"\n", stdout) == -1 )
1244 return -1;
1245
1246 rc++;
1247 }
1248
1249 return rc;
1250 }
1251 #endif // wxNEED_PUTS
1252
1253 #ifdef wxNEED_PUTC
1254 int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream)
1255 {
1256 wchar_t ws[2] = { wc, L'\0' };
1257
1258 return wxFputs(ws, stream);
1259 }
1260 #endif // wxNEED_PUTC
1261
1262 // NB: we only implement va_list functions here, the ones taking ... are
1263 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
1264 // the definitions there to avoid duplicating them here
1265 #ifdef wxNEED_WPRINTF
1266
1267 // TODO: implement the scanf() functions
1268 int vwscanf(const wxChar *format, va_list argptr)
1269 {
1270 wxFAIL_MSG( _T("TODO") );
1271
1272 return -1;
1273 }
1274
1275 int vswscanf(const wxChar *ws, const wxChar *format, va_list argptr)
1276 {
1277 // The best we can do without proper Unicode support in glibc is to
1278 // convert the strings into MB representation and run ANSI version
1279 // of the function. This doesn't work with %c and %s because of difference
1280 // in size of char and wchar_t, though.
1281
1282 wxCHECK_MSG( wxStrstr(format, _T("%s")) == NULL, -1,
1283 _T("incomplete vswscanf implementation doesn't allow %s") );
1284 wxCHECK_MSG( wxStrstr(format, _T("%c")) == NULL, -1,
1285 _T("incomplete vswscanf implementation doesn't allow %c") );
1286
1287 va_list argcopy;
1288 wxVaCopy(argcopy, argptr);
1289 return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argcopy);
1290 }
1291
1292 int vfwscanf(FILE *stream, const wxChar *format, va_list argptr)
1293 {
1294 wxFAIL_MSG( _T("TODO") );
1295
1296 return -1;
1297 }
1298
1299 #define vswprintf wxVsnprintf_
1300
1301 int vfwprintf(FILE *stream, const wxChar *format, va_list argptr)
1302 {
1303 wxString s;
1304 int rc = s.PrintfV(format, argptr);
1305
1306 if ( rc != -1 )
1307 {
1308 // we can't do much better without Unicode support in libc...
1309 if ( fprintf(stream, "%s", (const char*)s.mb_str() ) == -1 )
1310 return -1;
1311 }
1312
1313 return rc;
1314 }
1315
1316 int vwprintf(const wxChar *format, va_list argptr)
1317 {
1318 return wxVfprintf(stdout, format, argptr);
1319 }
1320
1321 #endif // wxNEED_WPRINTF
1322
1323 #ifdef wxNEED_PRINTF_CONVERSION
1324
1325 // ----------------------------------------------------------------------------
1326 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
1327 // ----------------------------------------------------------------------------
1328
1329 /*
1330 Here are the gory details. We want to follow the Windows/MS conventions,
1331 that is to have
1332
1333 In ANSI mode:
1334
1335 format specifier results in
1336 -----------------------------------
1337 %c, %hc, %hC char
1338 %lc, %C, %lC wchar_t
1339
1340 In Unicode mode:
1341
1342 format specifier results in
1343 -----------------------------------
1344 %hc, %C, %hC char
1345 %c, %lc, %lC wchar_t
1346
1347
1348 while on POSIX systems we have %C identical to %lc and %c always means char
1349 (in any mode) while %lc always means wchar_t,
1350
1351 So to use native functions in order to get our semantics we must do the
1352 following translations in Unicode mode (nothing to do in ANSI mode):
1353
1354 wxWidgets specifier POSIX specifier
1355 ----------------------------------------
1356
1357 %hc, %C, %hC %c
1358 %c %lc
1359
1360
1361 And, of course, the same should be done for %s as well.
1362 */
1363
1364 class wxFormatConverter
1365 {
1366 public:
1367 wxFormatConverter(const wxChar *format);
1368
1369 // notice that we only translated the string if m_fmtOrig == NULL (as set
1370 // by CopyAllBefore()), otherwise we should simply use the original format
1371 operator const wxChar *() const
1372 { return m_fmtOrig ? m_fmtOrig : m_fmt.c_str(); }
1373
1374 private:
1375 // copy another character to the translated format: this function does the
1376 // copy if we are translating but doesn't do anything at all if we don't,
1377 // so we don't create the translated format string at all unless we really
1378 // need to (i.e. InsertFmtChar() is called)
1379 wxChar CopyFmtChar(wxChar ch)
1380 {
1381 if ( !m_fmtOrig )
1382 {
1383 // we're translating, do copy
1384 m_fmt += ch;
1385 }
1386 else
1387 {
1388 // simply increase the count which should be copied by
1389 // CopyAllBefore() later if needed
1390 m_nCopied++;
1391 }
1392
1393 return ch;
1394 }
1395
1396 // insert an extra character
1397 void InsertFmtChar(wxChar ch)
1398 {
1399 if ( m_fmtOrig )
1400 {
1401 // so far we haven't translated anything yet
1402 CopyAllBefore();
1403 }
1404
1405 m_fmt += ch;
1406 }
1407
1408 void CopyAllBefore()
1409 {
1410 wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") );
1411
1412 m_fmt = wxString(m_fmtOrig, m_nCopied);
1413
1414 // we won't need it any longer
1415 m_fmtOrig = NULL;
1416 }
1417
1418 static bool IsFlagChar(wxChar ch)
1419 {
1420 return ch == _T('-') || ch == _T('+') ||
1421 ch == _T('0') || ch == _T(' ') || ch == _T('#');
1422 }
1423
1424 void SkipDigits(const wxChar **ptpc)
1425 {
1426 while ( **ptpc >= _T('0') && **ptpc <= _T('9') )
1427 CopyFmtChar(*(*ptpc)++);
1428 }
1429
1430 // the translated format
1431 wxString m_fmt;
1432
1433 // the original format
1434 const wxChar *m_fmtOrig;
1435
1436 // the number of characters already copied
1437 size_t m_nCopied;
1438 };
1439
1440 wxFormatConverter::wxFormatConverter(const wxChar *format)
1441 {
1442 m_fmtOrig = format;
1443 m_nCopied = 0;
1444
1445 while ( *format )
1446 {
1447 if ( CopyFmtChar(*format++) == _T('%') )
1448 {
1449 // skip any flags
1450 while ( IsFlagChar(*format) )
1451 CopyFmtChar(*format++);
1452
1453 // and possible width
1454 if ( *format == _T('*') )
1455 CopyFmtChar(*format++);
1456 else
1457 SkipDigits(&format);
1458
1459 // precision?
1460 if ( *format == _T('.') )
1461 {
1462 CopyFmtChar(*format++);
1463 if ( *format == _T('*') )
1464 CopyFmtChar(*format++);
1465 else
1466 SkipDigits(&format);
1467 }
1468
1469 // next we can have a size modifier
1470 enum
1471 {
1472 Default,
1473 Short,
1474 Long
1475 } size;
1476
1477 switch ( *format )
1478 {
1479 case _T('h'):
1480 size = Short;
1481 format++;
1482 break;
1483
1484 case _T('l'):
1485 // "ll" has a different meaning!
1486 if ( format[1] != _T('l') )
1487 {
1488 size = Long;
1489 format++;
1490 break;
1491 }
1492 //else: fall through
1493
1494 default:
1495 size = Default;
1496 }
1497
1498 // and finally we should have the type
1499 switch ( *format )
1500 {
1501 case _T('C'):
1502 case _T('S'):
1503 // %C and %hC -> %c and %lC -> %lc
1504 if ( size == Long )
1505 CopyFmtChar(_T('l'));
1506
1507 InsertFmtChar(*format++ == _T('C') ? _T('c') : _T('s'));
1508 break;
1509
1510 case _T('c'):
1511 case _T('s'):
1512 // %c -> %lc but %hc stays %hc and %lc is still %lc
1513 if ( size == Default)
1514 InsertFmtChar(_T('l'));
1515 // fall through
1516
1517 default:
1518 // nothing special to do
1519 if ( size != Default )
1520 CopyFmtChar(*(format - 1));
1521 CopyFmtChar(*format++);
1522 }
1523 }
1524 }
1525 }
1526
1527 #else // !wxNEED_PRINTF_CONVERSION
1528 // no conversion necessary
1529 #define wxFormatConverter(x) (x)
1530 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
1531
1532 #ifdef __WXDEBUG__
1533 // For testing the format converter
1534 wxString wxConvertFormat(const wxChar *format)
1535 {
1536 return wxString(wxFormatConverter(format));
1537 }
1538 #endif
1539
1540 // ----------------------------------------------------------------------------
1541 // wxPrintf(), wxScanf() and relatives
1542 // ----------------------------------------------------------------------------
1543
1544 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
1545
1546 int wxScanf( const wxChar *format, ... )
1547 {
1548 va_list argptr;
1549 va_start(argptr, format);
1550
1551 int ret = vwscanf(wxFormatConverter(format), argptr );
1552
1553 va_end(argptr);
1554
1555 return ret;
1556 }
1557
1558 int wxSscanf( const wxChar *str, const wxChar *format, ... )
1559 {
1560 va_list argptr;
1561 va_start(argptr, format);
1562
1563 int ret = vswscanf( str, wxFormatConverter(format), argptr );
1564
1565 va_end(argptr);
1566
1567 return ret;
1568 }
1569
1570 int wxFscanf( FILE *stream, const wxChar *format, ... )
1571 {
1572 va_list argptr;
1573 va_start(argptr, format);
1574 int ret = vfwscanf(stream, wxFormatConverter(format), argptr);
1575
1576 va_end(argptr);
1577
1578 return ret;
1579 }
1580
1581 int wxPrintf( const wxChar *format, ... )
1582 {
1583 va_list argptr;
1584 va_start(argptr, format);
1585
1586 int ret = vwprintf( wxFormatConverter(format), argptr );
1587
1588 va_end(argptr);
1589
1590 return ret;
1591 }
1592
1593 #ifndef wxSnprintf
1594 int wxSnprintf( wxChar *str, size_t size, const wxChar *format, ... )
1595 {
1596 va_list argptr;
1597 va_start(argptr, format);
1598
1599 int ret = vswprintf( str, size, wxFormatConverter(format), argptr );
1600
1601 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
1602 // doesn't nul terminate on truncation.
1603 str[size - 1] = 0;
1604
1605 va_end(argptr);
1606
1607 return ret;
1608 }
1609 #endif // wxSnprintf
1610
1611 int wxSprintf( wxChar *str, const wxChar *format, ... )
1612 {
1613 va_list argptr;
1614 va_start(argptr, format);
1615
1616 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
1617 // it's safe to implement this one in terms of it
1618 wxString s(wxString::FormatV(format, argptr));
1619 wxStrcpy(str, s);
1620
1621 va_end(argptr);
1622
1623 return s.length();
1624 }
1625
1626 int wxFprintf( FILE *stream, const wxChar *format, ... )
1627 {
1628 va_list argptr;
1629 va_start( argptr, format );
1630
1631 int ret = vfwprintf( stream, wxFormatConverter(format), argptr );
1632
1633 va_end(argptr);
1634
1635 return ret;
1636 }
1637
1638 int wxVsscanf( const wxChar *str, const wxChar *format, va_list argptr )
1639 {
1640 return vswscanf( str, wxFormatConverter(format), argptr );
1641 }
1642
1643 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr )
1644 {
1645 return vfwprintf( stream, wxFormatConverter(format), argptr );
1646 }
1647
1648 int wxVprintf( const wxChar *format, va_list argptr )
1649 {
1650 return vwprintf( wxFormatConverter(format), argptr );
1651 }
1652
1653 #ifndef wxVsnprintf
1654 int wxVsnprintf( wxChar *str, size_t size, const wxChar *format, va_list argptr )
1655 {
1656 return vswprintf( str, size, wxFormatConverter(format), argptr );
1657 }
1658 #endif // wxVsnprintf
1659
1660 int wxVsprintf( wxChar *str, const wxChar *format, va_list argptr )
1661 {
1662 // same as for wxSprintf()
1663 return vswprintf(str, INT_MAX / 4, wxFormatConverter(format), argptr);
1664 }
1665
1666 #endif // wxNEED_PRINTF_CONVERSION
1667
1668 #if wxUSE_WCHAR_T
1669
1670 // ----------------------------------------------------------------------------
1671 // ctype.h stuff (currently unused)
1672 // ----------------------------------------------------------------------------
1673
1674 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
1675 inline WORD wxMSW_ctype(wxChar ch)
1676 {
1677 WORD ret;
1678 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
1679 return ret;
1680 }
1681
1682 WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); }
1683 WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); }
1684 WXDLLEXPORT int wxIscntrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; }
1685 WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; }
1686 WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); }
1687 WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); }
1688 WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); }
1689 WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; }
1690 WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; }
1691 WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); }
1692 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; }
1693 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
1694 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
1695 #endif
1696
1697 #ifdef wxNEED_WX_MBSTOWCS
1698
1699 WXDLLEXPORT size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen)
1700 {
1701 if (!out)
1702 {
1703 size_t outsize = 0;
1704 while(*in++)
1705 outsize++;
1706 return outsize;
1707 }
1708
1709 const char* origin = in;
1710
1711 while (outlen-- && *in)
1712 {
1713 *out++ = (wchar_t) *in++;
1714 }
1715
1716 *out = '\0';
1717
1718 return in - origin;
1719 }
1720
1721 WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen)
1722 {
1723 if (!out)
1724 {
1725 size_t outsize = 0;
1726 while(*in++)
1727 outsize++;
1728 return outsize;
1729 }
1730
1731 const wchar_t* origin = in;
1732
1733 while (outlen-- && *in)
1734 {
1735 *out++ = (char) *in++;
1736 }
1737
1738 *out = '\0';
1739
1740 return in - origin;
1741 }
1742
1743 #endif // wxNEED_WX_MBSTOWCS
1744
1745 #if defined(wxNEED_WX_CTYPE_H)
1746
1747 #include <CoreFoundation/CoreFoundation.h>
1748
1749 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
1750 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
1751 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
1752 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
1753 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
1754 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
1755 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
1756 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
1757 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
1758 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
1759
1760 WXDLLEXPORT int wxIsalnum(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalnumset, ch); }
1761 WXDLLEXPORT int wxIsalpha(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalphaset, ch); }
1762 WXDLLEXPORT int wxIscntrl(wxChar ch) { return CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
1763 WXDLLEXPORT int wxIsdigit(wxChar ch) { return CFCharacterSetIsCharacterMember(cfdigitset, ch); }
1764 WXDLLEXPORT int wxIsgraph(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch) && ch != ' '; }
1765 WXDLLEXPORT int wxIslower(wxChar ch) { return CFCharacterSetIsCharacterMember(cflowerset, ch); }
1766 WXDLLEXPORT int wxIsprint(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
1767 WXDLLEXPORT int wxIspunct(wxChar ch) { return CFCharacterSetIsCharacterMember(cfpunctset, ch); }
1768 WXDLLEXPORT int wxIsspace(wxChar ch) { return CFCharacterSetIsCharacterMember(cfspaceset, ch); }
1769 WXDLLEXPORT int wxIsupper(wxChar ch) { return CFCharacterSetIsCharacterMember(cfupperset, ch); }
1770 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxIsdigit(ch) || (ch>='a' && ch<='f') || (ch>='A' && ch<='F'); }
1771 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)tolower((char)(ch)); }
1772 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)toupper((char)(ch)); }
1773
1774 #endif // wxNEED_WX_CTYPE_H
1775
1776 #ifndef wxStrdupA
1777
1778 WXDLLEXPORT char *wxStrdupA(const char *s)
1779 {
1780 return strcpy((char *)malloc(strlen(s) + 1), s);
1781 }
1782
1783 #endif // wxStrdupA
1784
1785 #ifndef wxStrdupW
1786
1787 WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz)
1788 {
1789 size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t);
1790 wchar_t *ret = (wchar_t *) malloc(size);
1791 memcpy(ret, pwz, size);
1792 return ret;
1793 }
1794
1795 #endif // wxStrdupW
1796
1797 #ifndef wxStricmp
1798 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
1799 {
1800 register wxChar c1, c2;
1801 do {
1802 c1 = wxTolower(*psz1++);
1803 c2 = wxTolower(*psz2++);
1804 } while ( c1 && (c1 == c2) );
1805 return c1 - c2;
1806 }
1807 #endif
1808
1809 #ifndef wxStricmp
1810 int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n)
1811 {
1812 // initialize the variables just to suppress stupid gcc warning
1813 register wxChar c1 = 0, c2 = 0;
1814 while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
1815 if (n) {
1816 if (c1 < c2) return -1;
1817 if (c1 > c2) return 1;
1818 }
1819 return 0;
1820 }
1821 #endif
1822
1823 #ifndef wxSetlocale
1824 WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
1825 {
1826 char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale));
1827
1828 return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld));
1829 }
1830 #endif
1831
1832 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
1833 WXDLLEXPORT size_t wxWcslen(const wchar_t *s)
1834 {
1835 size_t n = 0;
1836 while ( *s++ )
1837 n++;
1838
1839 return n;
1840 }
1841 #endif
1842
1843 // ----------------------------------------------------------------------------
1844 // string.h functions
1845 // ----------------------------------------------------------------------------
1846
1847 #ifdef wxNEED_WX_STRING_H
1848
1849 // RN: These need to be c externed for the regex lib
1850 #ifdef __cplusplus
1851 extern "C" {
1852 #endif
1853
1854 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
1855 {
1856 wxChar *ret = dest;
1857 while (*dest) dest++;
1858 while ((*dest++ = *src++));
1859 return ret;
1860 }
1861
1862 WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c)
1863 {
1864 // be careful here as the terminating NUL makes part of the string
1865 while ( *s != c )
1866 {
1867 if ( !*s++ )
1868 return NULL;
1869 }
1870
1871 return s;
1872 }
1873
1874 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
1875 {
1876 while ((*s1 == *s2) && *s1) s1++, s2++;
1877 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
1878 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
1879 return 0;
1880 }
1881
1882 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
1883 {
1884 wxChar *ret = dest;
1885 while ((*dest++ = *src++));
1886 return ret;
1887 }
1888
1889 WXDLLEXPORT size_t wxStrlen_(const wxChar *s)
1890 {
1891 size_t n = 0;
1892 while ( *s++ )
1893 n++;
1894
1895 return n;
1896 }
1897
1898
1899 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
1900 {
1901 wxChar *ret = dest;
1902 while (*dest) dest++;
1903 while (n && (*dest++ = *src++)) n--;
1904 return ret;
1905 }
1906
1907 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
1908 {
1909 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
1910 if (n) {
1911 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
1912 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
1913 }
1914 return 0;
1915 }
1916
1917 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
1918 {
1919 wxChar *ret = dest;
1920 while (n && (*dest++ = *src++)) n--;
1921 while (n) *dest++=0, n--; // the docs specify padding with zeroes
1922 return ret;
1923 }
1924
1925 WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
1926 {
1927 while (*s && !wxStrchr(accept, *s))
1928 s++;
1929
1930 return *s ? s : NULL;
1931 }
1932
1933 WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c)
1934 {
1935 const wxChar *ret = NULL;
1936 do
1937 {
1938 if ( *s == c )
1939 ret = s;
1940 s++;
1941 }
1942 while ( *s );
1943
1944 return ret;
1945 }
1946
1947 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
1948 {
1949 size_t len = 0;
1950 while (wxStrchr(accept, *s++)) len++;
1951 return len;
1952 }
1953
1954 WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle)
1955 {
1956 wxASSERT_MSG( needle != NULL, _T("NULL argument in wxStrstr") );
1957
1958 // VZ: this is not exactly the most efficient string search algorithm...
1959
1960 const size_t len = wxStrlen(needle);
1961
1962 while ( const wxChar *fnd = wxStrchr(haystack, *needle) )
1963 {
1964 if ( !wxStrncmp(fnd, needle, len) )
1965 return fnd;
1966
1967 haystack = fnd + 1;
1968 }
1969
1970 return NULL;
1971 }
1972
1973 #ifdef __cplusplus
1974 }
1975 #endif
1976
1977 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
1978 {
1979 const wxChar *start = nptr;
1980
1981 // FIXME: only correct for C locale
1982 while (wxIsspace(*nptr)) nptr++;
1983 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1984 while (wxIsdigit(*nptr)) nptr++;
1985 if (*nptr == wxT('.')) {
1986 nptr++;
1987 while (wxIsdigit(*nptr)) nptr++;
1988 }
1989 if (*nptr == wxT('E') || *nptr == wxT('e')) {
1990 nptr++;
1991 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1992 while (wxIsdigit(*nptr)) nptr++;
1993 }
1994
1995 wxString data(nptr, nptr-start);
1996 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
1997 char *rdat = wxMBSTRINGCAST dat;
1998 double ret = strtod(dat, &rdat);
1999
2000 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
2001
2002 return ret;
2003 }
2004
2005 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
2006 {
2007 const wxChar *start = nptr;
2008
2009 // FIXME: only correct for C locale
2010 while (wxIsspace(*nptr)) nptr++;
2011 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
2012 if (((base == 0) || (base == 16)) &&
2013 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
2014 nptr += 2;
2015 base = 16;
2016 }
2017 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
2018 else if (base == 0) base = 10;
2019
2020 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
2021 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
2022
2023 wxString data(start, nptr-start);
2024 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
2025 char *rdat = wxMBSTRINGCAST dat;
2026 long int ret = strtol(dat, &rdat, base);
2027
2028 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
2029
2030 return ret;
2031 }
2032
2033 WXDLLEXPORT unsigned long int wxStrtoul(const wxChar *nptr, wxChar **endptr, int base)
2034 {
2035 return (unsigned long int) wxStrtol(nptr, endptr, base);
2036 }
2037
2038 #endif // wxNEED_WX_STRING_H
2039
2040 #ifdef wxNEED_WX_STDIO_H
2041 WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
2042 {
2043 char mode_buffer[10];
2044 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
2045 mode_buffer[i] = (char) mode[i];
2046
2047 return fopen( wxConvFile.cWX2MB(path), mode_buffer );
2048 }
2049
2050 WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
2051 {
2052 char mode_buffer[10];
2053 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
2054 mode_buffer[i] = (char) mode[i];
2055
2056 return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream );
2057 }
2058
2059 WXDLLEXPORT int wxRemove(const wxChar *path)
2060 {
2061 return remove( wxConvFile.cWX2MB(path) );
2062 }
2063
2064 WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
2065 {
2066 return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) );
2067 }
2068 #endif
2069
2070 #ifndef wxAtof
2071 double WXDLLEXPORT wxAtof(const wxChar *psz)
2072 {
2073 #ifdef __WXWINCE__
2074 double d;
2075 wxString str(psz);
2076 if (str.ToDouble(& d))
2077 return d;
2078
2079 return 0.0;
2080 #else
2081 return atof(wxConvLibc.cWX2MB(psz));
2082 #endif
2083 }
2084 #endif
2085
2086 #ifdef wxNEED_WX_STDLIB_H
2087 int WXDLLEXPORT wxAtoi(const wxChar *psz)
2088 {
2089 return atoi(wxConvLibc.cWX2MB(psz));
2090 }
2091
2092 long WXDLLEXPORT wxAtol(const wxChar *psz)
2093 {
2094 return atol(wxConvLibc.cWX2MB(psz));
2095 }
2096
2097 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
2098 {
2099 #if wxUSE_UNICODE
2100 // NB: buffer returned by getenv() is allowed to be overwritten next
2101 // time getenv() is called, so it is OK to use static string
2102 // buffer to hold the data.
2103 static wxWCharBuffer value((wxChar*)NULL);
2104 value = wxConvLibc.cMB2WX(getenv(wxConvLibc.cWX2MB(name)));
2105 return value.data();
2106 #else
2107 return getenv(name);
2108 #endif
2109 }
2110
2111 int WXDLLEXPORT wxSystem(const wxChar *psz)
2112 {
2113 return system(wxConvLibc.cWX2MB(psz));
2114 }
2115
2116 #endif // wxNEED_WX_STDLIB_H
2117
2118 #ifdef wxNEED_WX_TIME_H
2119 WXDLLEXPORT size_t
2120 wxStrftime(wxChar *s, size_t maxsize, const wxChar *fmt, const struct tm *tm)
2121 {
2122 if ( !maxsize )
2123 return 0;
2124
2125 wxCharBuffer buf(maxsize);
2126
2127 wxCharBuffer bufFmt(wxConvLibc.cWX2MB(fmt));
2128 if ( !bufFmt )
2129 return 0;
2130
2131 size_t ret = strftime(buf.data(), maxsize, bufFmt, tm);
2132 if ( !ret )
2133 return 0;
2134
2135 wxWCharBuffer wbuf = wxConvLibc.cMB2WX(buf);
2136 if ( !wbuf )
2137 return 0;
2138
2139 wxStrncpy(s, wbuf, maxsize);
2140 return wxStrlen(s);
2141 }
2142 #endif // wxNEED_WX_TIME_H
2143
2144 #ifndef wxCtime
2145 WXDLLEXPORT wxChar *wxCtime(const time_t *timep)
2146 {
2147 // normally the string is 26 chars but give one more in case some broken
2148 // DOS compiler decides to use "\r\n" instead of "\n" at the end
2149 static wxChar buf[27];
2150
2151 // ctime() is guaranteed to return a string containing only ASCII
2152 // characters, as its format is always the same for any locale
2153 wxStrncpy(buf, wxString::FromAscii(ctime(timep)), WXSIZEOF(buf));
2154 buf[WXSIZEOF(buf) - 1] = _T('\0');
2155
2156 return buf;
2157 }
2158 #endif // wxCtime
2159
2160 #endif // wxUSE_WCHAR_T
2161
2162 // ----------------------------------------------------------------------------
2163 // functions which we may need even if !wxUSE_WCHAR_T
2164 // ----------------------------------------------------------------------------
2165
2166 #ifndef wxStrtok
2167
2168 WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
2169 {
2170 if (!psz)
2171 {
2172 psz = *save_ptr;
2173 if ( !psz )
2174 return NULL;
2175 }
2176
2177 psz += wxStrspn(psz, delim);
2178 if (!*psz)
2179 {
2180 *save_ptr = (wxChar *)NULL;
2181 return (wxChar *)NULL;
2182 }
2183
2184 wxChar *ret = psz;
2185 psz = wxStrpbrk(psz, delim);
2186 if (!psz)
2187 {
2188 *save_ptr = (wxChar*)NULL;
2189 }
2190 else
2191 {
2192 *psz = wxT('\0');
2193 *save_ptr = psz + 1;
2194 }
2195
2196 return ret;
2197 }
2198
2199 #endif // wxStrtok
2200
2201 // ----------------------------------------------------------------------------
2202 // missing C RTL functions
2203 // ----------------------------------------------------------------------------
2204
2205 #ifdef wxNEED_STRDUP
2206
2207 char *strdup(const char *s)
2208 {
2209 char *dest = (char*) malloc( strlen( s ) + 1 ) ;
2210 if ( dest )
2211 strcpy( dest , s ) ;
2212 return dest ;
2213 }
2214 #endif // wxNEED_STRDUP
2215
2216 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
2217
2218 void *calloc( size_t num, size_t size )
2219 {
2220 void** ptr = (void **)malloc(num * size);
2221 memset( ptr, 0, num * size);
2222 return ptr;
2223 }
2224
2225 #endif // __WXWINCE__ <= 211
2226
2227 #ifdef __WXWINCE__
2228
2229 int wxRemove(const wxChar *path)
2230 {
2231 return ::DeleteFile(path) == 0;
2232 }
2233
2234 #endif