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