]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
fixed wxFRAME_TOOL_WINDOW to use 'utility' type hint instead of not rendering any...
[wxWidgets.git] / src / common / wxchar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxchar.cpp
3 // Purpose: wxChar implementation
4 // Author: Ove Kåven
5 // Modified by: Ron Lee
6 // Created: 09/04/99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "wxchar.h"
14 #endif
15
16 // ===========================================================================
17 // headers, declarations, constants
18 // ===========================================================================
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #define _ISOC9X_SOURCE 1 // to get vsscanf()
28 #define _BSD_SOURCE 1 // to still get strdup()
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifndef __WXWINCE__
35 #include <time.h>
36 #include <locale.h>
37 #else
38 #include "wx/msw/wince/time.h"
39 #endif
40
41 #ifndef WX_PRECOMP
42 #include "wx/defs.h"
43 #include "wx/wxchar.h"
44 #include "wx/string.h"
45 #include "wx/hash.h"
46 #endif
47
48 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
49 #include <windef.h>
50 #include <winbase.h>
51 #include <winnls.h>
52 #include <winnt.h>
53 #endif
54
55 #if defined(__MWERKS__) && __MSL__ >= 0x6000
56 using namespace std ;
57 #endif
58
59 #ifdef __WXMAC__
60 #include "wx/mac/private.h"
61 #endif
62
63 #if wxUSE_WCHAR_T
64 size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n)
65 {
66 // assume that we have mbsrtowcs() too if we have wcsrtombs()
67 #if HAVE_WCSRTOMBS
68 mbstate_t mbstate;
69 memset(&mbstate, 0, sizeof(mbstate_t));
70 #endif
71
72 if (buf) {
73 if (!n || !*psz) {
74 if (n) *buf = wxT('\0');
75 return 0;
76 }
77 #ifdef HAVE_WCSRTOMBS
78 return mbsrtowcs(buf, &psz, n, &mbstate);
79 #else
80 return mbstowcs(buf, psz, n);
81 #endif
82 }
83
84 #ifdef HAVE_WCSRTOMBS
85 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
86 #else
87 return mbstowcs((wchar_t *) NULL, psz, 0);
88 #endif
89 }
90
91 size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
92 {
93 #if HAVE_WCSRTOMBS
94 mbstate_t mbstate;
95 memset(&mbstate, 0, sizeof(mbstate_t));
96 #endif
97
98 if (buf) {
99 if (!n || !*pwz) {
100 // glibc2.1 chokes on null input
101 if (n) *buf = '\0';
102 return 0;
103 }
104 #if HAVE_WCSRTOMBS
105 return wcsrtombs(buf, &pwz, n, &mbstate);
106 #else
107 return wcstombs(buf, pwz, n);
108 #endif
109 }
110
111 #if HAVE_WCSRTOMBS
112 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
113 #else
114 return wcstombs((char *) NULL, pwz, 0);
115 #endif
116 }
117 #endif // wxUSE_WCHAR_T
118
119 bool WXDLLEXPORT wxOKlibc()
120 {
121 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
122 // glibc 2.0 uses UTF-8 even when it shouldn't
123 wchar_t res = 0;
124 if ((MB_CUR_MAX == 2) &&
125 (wxMB2WC(&res, "\xdd\xa5", 1) == 1) &&
126 (res==0x765)) {
127 // this is UTF-8 allright, check whether that's what we want
128 char *cur_locale = setlocale(LC_CTYPE, NULL);
129 if ((strlen(cur_locale) < 4) ||
130 (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) ||
131 (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) {
132 // nope, don't use libc conversion
133 return false;
134 }
135 }
136 #endif
137 return true;
138 }
139
140 // ============================================================================
141 // printf() functions business
142 // ============================================================================
143
144 // special test mode: define all functions below even if we don't really need
145 // them to be able to test them
146 #ifdef wxTEST_PRINTF
147 #undef wxFprintf
148 #undef wxPrintf
149 #undef wxSprintf
150 #undef wxVfprintf
151 #undef wxVsprintf
152 #undef wxVprintf
153 #undef wxVsnprintf_
154 #undef wxSnprintf_
155
156 #define wxNEED_WPRINTF
157
158 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr );
159 #endif
160
161 // ----------------------------------------------------------------------------
162 // implement [v]snprintf() if the system doesn't provide a safe one
163 // ----------------------------------------------------------------------------
164
165 #if !defined(wxVsnprintf_)
166 int WXDLLEXPORT wxVsnprintf_(wxChar *buf, size_t lenMax,
167 const wxChar *format, va_list argptr)
168 {
169 // buffer to avoid dynamic memory allocation each time for small strings
170 char szScratch[1024];
171
172 // number of characters in the buffer so far, must be less than lenMax
173 size_t lenCur = 0;
174
175 for ( size_t n = 0; ; n++ )
176 {
177 const wxChar chCur = format[n];
178
179 if ( chCur == wxT('%') )
180 {
181 static char s_szFlags[256] = "%";
182 size_t flagofs = 1;
183 bool adj_left = false,
184 in_prec = false,
185 prec_dot = false,
186 done = false;
187 int ilen = 0;
188 size_t min_width = 0,
189 max_width = wxSTRING_MAXLEN;
190 do
191 {
192
193 #define CHECK_PREC \
194 if (in_prec && !prec_dot) \
195 { \
196 s_szFlags[flagofs++] = '.'; \
197 prec_dot = true; \
198 }
199
200 #define APPEND_CH(ch) \
201 { \
202 if ( lenCur == lenMax ) \
203 return -1; \
204 \
205 buf[lenCur++] = ch; \
206 }
207
208 #define APPEND_STR(s) \
209 { \
210 for ( const wxChar *p = s; *p; p++ ) \
211 { \
212 APPEND_CH(*p); \
213 } \
214 }
215
216 // what follows '%'?
217 const wxChar ch = format[++n];
218 switch ( ch )
219 {
220 case wxT('\0'):
221 APPEND_CH(_T('\0'));
222
223 done = true;
224 break;
225
226 case wxT('%'):
227 APPEND_CH(_T('%'));
228 done = true;
229 break;
230
231 case wxT('#'):
232 case wxT('0'):
233 case wxT(' '):
234 case wxT('+'):
235 case wxT('\''):
236 CHECK_PREC
237 s_szFlags[flagofs++] = ch;
238 break;
239
240 case wxT('-'):
241 CHECK_PREC
242 adj_left = true;
243 s_szFlags[flagofs++] = ch;
244 break;
245
246 case wxT('.'):
247 CHECK_PREC
248 in_prec = true;
249 prec_dot = false;
250 max_width = 0;
251 // dot will be auto-added to s_szFlags if non-negative
252 // number follows
253 break;
254
255 case wxT('h'):
256 ilen = -1;
257 CHECK_PREC
258 s_szFlags[flagofs++] = ch;
259 break;
260
261 case wxT('l'):
262 ilen = 1;
263 CHECK_PREC
264 s_szFlags[flagofs++] = ch;
265 break;
266
267 case wxT('q'):
268 case wxT('L'):
269 ilen = 2;
270 CHECK_PREC
271 s_szFlags[flagofs++] = ch;
272 break;
273
274 case wxT('Z'):
275 ilen = 3;
276 CHECK_PREC
277 s_szFlags[flagofs++] = ch;
278 break;
279
280 case wxT('*'):
281 {
282 int len = va_arg(argptr, int);
283 if (in_prec)
284 {
285 if (len<0) break;
286 CHECK_PREC
287 max_width = len;
288 }
289 else
290 {
291 if (len<0)
292 {
293 adj_left = !adj_left;
294 s_szFlags[flagofs++] = '-';
295 len = -len;
296 }
297 min_width = len;
298 }
299 flagofs += ::sprintf(s_szFlags+flagofs,"%d",len);
300 }
301 break;
302
303 case wxT('1'): case wxT('2'): case wxT('3'):
304 case wxT('4'): case wxT('5'): case wxT('6'):
305 case wxT('7'): case wxT('8'): case wxT('9'):
306 {
307 int len = 0;
308 CHECK_PREC
309 while ( (format[n] >= wxT('0')) &&
310 (format[n] <= wxT('9')) )
311 {
312 s_szFlags[flagofs++] = format[n];
313 len = len*10 + (format[n] - wxT('0'));
314 n++;
315 }
316
317 if (in_prec)
318 max_width = len;
319 else
320 min_width = len;
321
322 n--; // the main loop pre-increments n again
323 }
324 break;
325
326 case wxT('d'):
327 case wxT('i'):
328 case wxT('o'):
329 case wxT('u'):
330 case wxT('x'):
331 case wxT('X'):
332 CHECK_PREC
333 s_szFlags[flagofs++] = ch;
334 s_szFlags[flagofs] = '\0';
335 if (ilen == 0 )
336 {
337 int val = va_arg(argptr, int);
338 ::sprintf(szScratch, s_szFlags, val);
339 }
340 else if (ilen == -1)
341 {
342 // NB: 'short int' value passed through '...'
343 // is promoted to 'int', so we have to get
344 // an int from stack even if we need a short
345 short int val = (short int) va_arg(argptr, int);
346 ::sprintf(szScratch, s_szFlags, val);
347 }
348 else if (ilen == 1)
349 {
350 long int val = va_arg(argptr, long int);
351 ::sprintf(szScratch, s_szFlags, val);
352 }
353 else if (ilen == 2)
354 {
355 #if SIZEOF_LONG_LONG
356 long long int val = va_arg(argptr, long long int);
357 ::sprintf(szScratch, s_szFlags, val);
358 #else // !long long
359 long int val = va_arg(argptr, long int);
360 ::sprintf(szScratch, s_szFlags, val);
361 #endif // long long/!long long
362 }
363 else if (ilen == 3)
364 {
365 size_t val = va_arg(argptr, size_t);
366 ::sprintf(szScratch, s_szFlags, val);
367 }
368
369 {
370 const wxMB2WXbuf tmp =
371 wxConvLibc.cMB2WX(szScratch);
372 APPEND_STR(tmp);
373 }
374
375 done = true;
376 break;
377
378 case wxT('e'):
379 case wxT('E'):
380 case wxT('f'):
381 case wxT('g'):
382 case wxT('G'):
383 CHECK_PREC
384 s_szFlags[flagofs++] = ch;
385 s_szFlags[flagofs] = '\0';
386 if (ilen == 2)
387 {
388 long double val = va_arg(argptr, long double);
389 ::sprintf(szScratch, s_szFlags, val);
390 }
391 else
392 {
393 double val = va_arg(argptr, double);
394 ::sprintf(szScratch, s_szFlags, val);
395 }
396
397 {
398 const wxMB2WXbuf tmp =
399 wxConvLibc.cMB2WX(szScratch);
400 APPEND_STR(tmp);
401 }
402
403 done = true;
404 break;
405
406 case wxT('p'):
407 {
408 void *val = va_arg(argptr, void *);
409 CHECK_PREC
410 s_szFlags[flagofs++] = ch;
411 s_szFlags[flagofs] = '\0';
412 ::sprintf(szScratch, s_szFlags, val);
413
414 const wxMB2WXbuf tmp =
415 wxConvLibc.cMB2WX(szScratch);
416 APPEND_STR(tmp);
417
418 done = true;
419 }
420 break;
421
422 case wxT('c'):
423 {
424 int val = va_arg(argptr, int);
425 #if wxUSE_UNICODE
426 if (ilen == -1)
427 {
428 const char buf[2] = { val, 0 };
429 val = wxString(buf, wxConvLibc)[0u];
430 }
431 #elif wxUSE_WCHAR_T
432 if (ilen == 1)
433 {
434 const wchar_t buf[2] = { val, 0 };
435 val = wxString(buf, wxConvLibc)[0u];
436 }
437 #endif
438 size_t i;
439
440 if (!adj_left)
441 for (i = 1; i < min_width; i++)
442 APPEND_CH(_T(' '));
443
444 APPEND_CH(val);
445
446 if (adj_left)
447 for (i = 1; i < min_width; i++)
448 APPEND_CH(_T(' '));
449
450 done = true;
451 }
452 break;
453
454 case wxT('s'):
455 {
456 const wxChar *val = NULL;
457 #if wxUSE_UNICODE
458 wxString s;
459
460 if (ilen == -1)
461 {
462 // wx extension: we'll let %hs mean non-Unicode
463 // strings
464 char *v = va_arg(argptr, char *);
465
466 if (v)
467 val = s = wxString(v, wxConvLibc);
468 }
469 else
470 #elif wxUSE_WCHAR_T
471 wxString s;
472
473 if (ilen == 1)
474 {
475 // %ls means Unicode strings
476 wchar_t *v = va_arg(argptr, wchar_t *);
477
478 if (v)
479 val = s = wxString(v, wxConvLibc);
480 }
481 else
482 #endif
483 {
484 val = va_arg(argptr, wxChar *);
485 }
486
487 size_t len = 0;
488
489 if (val)
490 {
491 for ( len = 0;
492 val[len] && (len < max_width);
493 len++ )
494 ;
495 }
496 else if (max_width >= 6)
497 {
498 val = wxT("(null)");
499 len = 6;
500 }
501 else
502 {
503 val = wxT("");
504 len = 0;
505 }
506
507 size_t i;
508
509 if (!adj_left)
510 for (i = len; i < min_width; i++)
511 APPEND_CH(_T(' '));
512
513 for (i = 0; i < len; i++)
514 APPEND_CH(val[i]);
515
516 if (adj_left)
517 for (i = len; i < min_width; i++)
518 APPEND_CH(_T(' '));
519
520 done = true;
521 }
522 break;
523
524 case wxT('n'):
525 if (ilen == 0)
526 {
527 int *val = va_arg(argptr, int *);
528 *val = lenCur;
529 }
530 else if (ilen == -1)
531 {
532 short int *val = va_arg(argptr, short int *);
533 *val = lenCur;
534 }
535 else if (ilen >= 1)
536 {
537 long int *val = va_arg(argptr, long int *);
538 *val = lenCur;
539 }
540 done = true;
541 break;
542
543 default:
544 // bad format, leave unchanged
545 APPEND_CH(_T('%'));
546 APPEND_CH(ch);
547 done = true;
548 break;
549 }
550 }
551 while (!done);
552 }
553 else
554 {
555 APPEND_CH(chCur);
556 }
557
558 // terminating NUL?
559 if ( !chCur )
560 break;
561 }
562
563 return lenCur;
564 }
565
566 #undef APPEND_CH
567 #undef APPEND_STR
568 #undef CHECK_PREC
569
570 #endif // !wxVsnprintfA
571
572 #if !defined(wxSnprintf_)
573 int WXDLLEXPORT wxSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...)
574 {
575 va_list argptr;
576 va_start(argptr, format);
577
578 int iLen = wxVsnprintf_(buf, len, format, argptr);
579
580 va_end(argptr);
581
582 return iLen;
583 }
584 #endif // wxSnprintf_
585
586 #if defined(__DMC__)
587 /* Digital Mars adds count to _stprintf (C99) so convert */
588 #if wxUSE_UNICODE
589 int wxSprintf (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... )
590 {
591 va_list arglist;
592
593 va_start( arglist, format );
594 int iLen = swprintf ( s, -1, format, arglist );
595 va_end( arglist );
596 return iLen ;
597 }
598
599 #endif // wxUSE_UNICODE
600
601 #endif //__DMC__
602
603 // ----------------------------------------------------------------------------
604 // implement the standard IO functions for wide char if libc doesn't have them
605 // ----------------------------------------------------------------------------
606
607 #ifdef wxNEED_FPUTS
608 int wxFputs(const wchar_t *ws, FILE *stream)
609 {
610 // counting the number of wide characters written isn't worth the trouble,
611 // simply distinguish between ok and error
612 return fputs(wxConvLibc.cWC2MB(ws), stream) == -1 ? -1 : 0;
613 }
614 #endif // wxNEED_FPUTS
615
616 #ifdef wxNEED_PUTS
617 int wxPuts(const wxChar *ws)
618 {
619 int rc = wxFputs(ws, stdout);
620 if ( rc != -1 )
621 {
622 if ( wxFputs(L"\n", stdout) == -1 )
623 return -1;
624
625 rc++;
626 }
627
628 return rc;
629 }
630 #endif // wxNEED_PUTS
631
632 #ifdef wxNEED_PUTC
633 int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream)
634 {
635 wchar_t ws[2] = { wc, L'\0' };
636
637 return wxFputs(ws, stream);
638 }
639 #endif // wxNEED_PUTC
640
641 // NB: we only implement va_list functions here, the ones taking ... are
642 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
643 // the definitions there to avoid duplicating them here
644 #ifdef wxNEED_WPRINTF
645
646 // TODO: implement the scanf() functions
647 int vwscanf(const wxChar *format, va_list argptr)
648 {
649 wxFAIL_MSG( _T("TODO") );
650
651 return -1;
652 }
653
654 int vswscanf(const wxChar *ws, const wxChar *format, va_list argptr)
655 {
656 // The best we can do without proper Unicode support in glibc is to
657 // convert the strings into MB representation and run ANSI version
658 // of the function. This doesn't work with %c and %s because of difference
659 // in size of char and wchar_t, though.
660
661 wxCHECK_MSG( wxStrstr(format, _T("%s")) == NULL, -1,
662 _T("incomplete vswscanf implementation doesn't allow %s") );
663 wxCHECK_MSG( wxStrstr(format, _T("%c")) == NULL, -1,
664 _T("incomplete vswscanf implementation doesn't allow %c") );
665
666 va_list argcopy;
667 wxVaCopy(argcopy, argptr);
668 return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argcopy);
669 }
670
671 int vfwscanf(FILE *stream, const wxChar *format, va_list argptr)
672 {
673 wxFAIL_MSG( _T("TODO") );
674
675 return -1;
676 }
677
678 #define vswprintf wxVsnprintf_
679
680 int vfwprintf(FILE *stream, const wxChar *format, va_list argptr)
681 {
682 wxString s;
683 int rc = s.PrintfV(format, argptr);
684
685 if ( rc != -1 )
686 {
687 // we can't do much better without Unicode support in libc...
688 if ( fprintf(stream, "%s", (const char*)s.mb_str() ) == -1 )
689 return -1;
690 }
691
692 return rc;
693 }
694
695 int vwprintf(const wxChar *format, va_list argptr)
696 {
697 return wxVfprintf(stdout, format, argptr);
698 }
699
700 #endif // wxNEED_WPRINTF
701
702 #ifdef wxNEED_PRINTF_CONVERSION
703
704 // ----------------------------------------------------------------------------
705 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
706 // ----------------------------------------------------------------------------
707
708 /*
709 Here are the gory details. We want to follow the Windows/MS conventions,
710 that is to have
711
712 In ANSI mode:
713
714 format specifier results in
715 -----------------------------------
716 %c, %hc, %hC char
717 %lc, %C, %lC wchar_t
718
719 In Unicode mode:
720
721 format specifier results in
722 -----------------------------------
723 %hc, %C, %hC char
724 %c, %lc, %lC wchar_t
725
726
727 while on POSIX systems we have %C identical to %lc and %c always means char
728 (in any mode) while %lc always means wchar_t,
729
730 So to use native functions in order to get our semantics we must do the
731 following translations in Unicode mode (nothing to do in ANSI mode):
732
733 wxWidgets specifier POSIX specifier
734 ----------------------------------------
735
736 %hc, %C, %hC %c
737 %c %lc
738
739
740 And, of course, the same should be done for %s as well.
741 */
742
743 class wxFormatConverter
744 {
745 public:
746 wxFormatConverter(const wxChar *format);
747
748 // notice that we only translated the string if m_fmtOrig == NULL (as set
749 // by CopyAllBefore()), otherwise we should simply use the original format
750 operator const wxChar *() const
751 { return m_fmtOrig ? m_fmtOrig : m_fmt.c_str(); }
752
753 private:
754 // copy another character to the translated format: this function does the
755 // copy if we are translating but doesn't do anything at all if we don't,
756 // so we don't create the translated format string at all unless we really
757 // need to (i.e. InsertFmtChar() is called)
758 wxChar CopyFmtChar(wxChar ch)
759 {
760 if ( !m_fmtOrig )
761 {
762 // we're translating, do copy
763 m_fmt += ch;
764 }
765 else
766 {
767 // simply increase the count which should be copied by
768 // CopyAllBefore() later if needed
769 m_nCopied++;
770 }
771
772 return ch;
773 }
774
775 // insert an extra character
776 void InsertFmtChar(wxChar ch)
777 {
778 if ( m_fmtOrig )
779 {
780 // so far we haven't translated anything yet
781 CopyAllBefore();
782 }
783
784 m_fmt += ch;
785 }
786
787 void CopyAllBefore()
788 {
789 wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") );
790
791 m_fmt = wxString(m_fmtOrig, m_nCopied);
792
793 // we won't need it any longer
794 m_fmtOrig = NULL;
795 }
796
797 static bool IsFlagChar(wxChar ch)
798 {
799 return ch == _T('-') || ch == _T('+') ||
800 ch == _T('0') || ch == _T(' ') || ch == _T('#');
801 }
802
803 void SkipDigits(const wxChar **ptpc)
804 {
805 while ( **ptpc >= _T('0') && **ptpc <= _T('9') )
806 CopyFmtChar(*(*ptpc)++);
807 }
808
809 // the translated format
810 wxString m_fmt;
811
812 // the original format
813 const wxChar *m_fmtOrig;
814
815 // the number of characters already copied
816 size_t m_nCopied;
817 };
818
819 wxFormatConverter::wxFormatConverter(const wxChar *format)
820 {
821 m_fmtOrig = format;
822 m_nCopied = 0;
823
824 while ( *format )
825 {
826 if ( CopyFmtChar(*format++) == _T('%') )
827 {
828 // skip any flags
829 while ( IsFlagChar(*format) )
830 CopyFmtChar(*format++);
831
832 // and possible width
833 if ( *format == _T('*') )
834 CopyFmtChar(*format++);
835 else
836 SkipDigits(&format);
837
838 // precision?
839 if ( *format == _T('.') )
840 {
841 CopyFmtChar(*format++);
842 if ( *format == _T('*') )
843 CopyFmtChar(*format++);
844 else
845 SkipDigits(&format);
846 }
847
848 // next we can have a size modifier
849 enum
850 {
851 Default,
852 Short,
853 Long
854 } size;
855
856 switch ( *format )
857 {
858 case _T('h'):
859 size = Short;
860 format++;
861 break;
862
863 case _T('l'):
864 // "ll" has a different meaning!
865 if ( format[1] != _T('l') )
866 {
867 size = Long;
868 format++;
869 break;
870 }
871 //else: fall through
872
873 default:
874 size = Default;
875 }
876
877 // and finally we should have the type
878 switch ( *format )
879 {
880 case _T('C'):
881 case _T('S'):
882 // %C and %hC -> %c and %lC -> %lc
883 if ( size == Long )
884 CopyFmtChar(_T('l'));
885
886 InsertFmtChar(*format++ == _T('C') ? _T('c') : _T('s'));
887 break;
888
889 case _T('c'):
890 case _T('s'):
891 // %c -> %lc but %hc stays %hc and %lc is still %lc
892 if ( size == Default)
893 InsertFmtChar(_T('l'));
894 // fall through
895
896 default:
897 // nothing special to do
898 if ( size != Default )
899 CopyFmtChar(*(format - 1));
900 CopyFmtChar(*format++);
901 }
902 }
903 }
904 }
905
906 #else // !wxNEED_PRINTF_CONVERSION
907 // no conversion necessary
908 #define wxFormatConverter(x) (x)
909 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
910
911 #ifdef __WXDEBUG__
912 // For testing the format converter
913 wxString wxConvertFormat(const wxChar *format)
914 {
915 return wxString(wxFormatConverter(format));
916 }
917 #endif
918
919 // ----------------------------------------------------------------------------
920 // wxPrintf(), wxScanf() and relatives
921 // ----------------------------------------------------------------------------
922
923 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
924
925 int wxScanf( const wxChar *format, ... )
926 {
927 va_list argptr;
928 va_start(argptr, format);
929
930 int ret = vwscanf(wxFormatConverter(format), argptr );
931
932 va_end(argptr);
933
934 return ret;
935 }
936
937 int wxSscanf( const wxChar *str, const wxChar *format, ... )
938 {
939 va_list argptr;
940 va_start(argptr, format);
941
942 int ret = vswscanf( str, wxFormatConverter(format), argptr );
943
944 va_end(argptr);
945
946 return ret;
947 }
948
949 int wxFscanf( FILE *stream, const wxChar *format, ... )
950 {
951 va_list argptr;
952 va_start(argptr, format);
953 int ret = vfwscanf(stream, wxFormatConverter(format), argptr);
954
955 va_end(argptr);
956
957 return ret;
958 }
959
960 int wxPrintf( const wxChar *format, ... )
961 {
962 va_list argptr;
963 va_start(argptr, format);
964
965 int ret = vwprintf( wxFormatConverter(format), argptr );
966
967 va_end(argptr);
968
969 return ret;
970 }
971
972 #ifndef wxSnprintf
973 int wxSnprintf( wxChar *str, size_t size, const wxChar *format, ... )
974 {
975 va_list argptr;
976 va_start(argptr, format);
977
978 int ret = vswprintf( str, size, wxFormatConverter(format), argptr );
979
980 va_end(argptr);
981
982 return ret;
983 }
984 #endif // wxSnprintf
985
986 int wxSprintf( wxChar *str, const wxChar *format, ... )
987 {
988 va_list argptr;
989 va_start(argptr, format);
990
991 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
992 // it's safe to implement this one in terms of it
993 wxString s(wxString::FormatV(format, argptr));
994 wxStrcpy(str, s);
995
996 va_end(argptr);
997
998 return s.length();
999 }
1000
1001 int wxFprintf( FILE *stream, const wxChar *format, ... )
1002 {
1003 va_list argptr;
1004 va_start( argptr, format );
1005
1006 int ret = vfwprintf( stream, wxFormatConverter(format), argptr );
1007
1008 va_end(argptr);
1009
1010 return ret;
1011 }
1012
1013 int wxVsscanf( const wxChar *str, const wxChar *format, va_list argptr )
1014 {
1015 return vswscanf( str, wxFormatConverter(format), argptr );
1016 }
1017
1018 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr )
1019 {
1020 return vfwprintf( stream, wxFormatConverter(format), argptr );
1021 }
1022
1023 int wxVprintf( const wxChar *format, va_list argptr )
1024 {
1025 return vwprintf( wxFormatConverter(format), argptr );
1026 }
1027
1028 #ifndef wxVsnprintf
1029 int wxVsnprintf( wxChar *str, size_t size, const wxChar *format, va_list argptr )
1030 {
1031 return vswprintf( str, size, wxFormatConverter(format), argptr );
1032 }
1033 #endif // wxVsnprintf
1034
1035 int wxVsprintf( wxChar *str, const wxChar *format, va_list argptr )
1036 {
1037 // same as for wxSprintf()
1038 return vswprintf(str, INT_MAX / 4, wxFormatConverter(format), argptr);
1039 }
1040
1041 #endif // wxNEED_PRINTF_CONVERSION
1042
1043 #if wxUSE_WCHAR_T
1044
1045 // ----------------------------------------------------------------------------
1046 // ctype.h stuff (currently unused)
1047 // ----------------------------------------------------------------------------
1048
1049 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
1050 inline WORD wxMSW_ctype(wxChar ch)
1051 {
1052 WORD ret;
1053 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
1054 return ret;
1055 }
1056
1057 WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); }
1058 WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); }
1059 WXDLLEXPORT int wxIscntrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; }
1060 WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; }
1061 WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); }
1062 WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); }
1063 WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); }
1064 WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; }
1065 WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; }
1066 WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); }
1067 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; }
1068 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
1069 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
1070 #endif
1071
1072 #if defined(__DARWIN__) && ( MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2 )
1073
1074 WXDLLEXPORT size_t wxInternalMbstowcs (wchar_t * out, const char * in, size_t outlen)
1075 {
1076 if (!out)
1077 {
1078 size_t outsize = 0;
1079 while(*in++)
1080 outsize++;
1081 return outsize;
1082 }
1083
1084 const char* origin = in;
1085
1086 while (outlen-- && *in)
1087 {
1088 *out++ = (wchar_t) *in++;
1089 }
1090
1091 *out = '\0';
1092
1093 return in - origin;
1094 }
1095
1096 WXDLLEXPORT size_t wxInternalWcstombs (char * out, const wchar_t * in, size_t outlen)
1097 {
1098 if (!out)
1099 {
1100 size_t outsize = 0;
1101 while(*in++)
1102 outsize++;
1103 return outsize;
1104 }
1105
1106 const wchar_t* origin = in;
1107
1108 while (outlen-- && *in)
1109 {
1110 *out++ = (char) *in++;
1111 }
1112
1113 *out = '\0';
1114
1115 return in - origin;
1116 }
1117
1118 #if defined(wxNEED_WX_CTYPE_H)
1119
1120 #include <CoreFoundation/CoreFoundation.h>
1121
1122 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
1123 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
1124 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
1125 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
1126 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
1127 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
1128 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
1129 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
1130 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
1131 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
1132
1133 WXDLLEXPORT int wxIsalnum(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalnumset, ch); }
1134 WXDLLEXPORT int wxIsalpha(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalphaset, ch); }
1135 WXDLLEXPORT int wxIscntrl(wxChar ch) { return CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
1136 WXDLLEXPORT int wxIsdigit(wxChar ch) { return CFCharacterSetIsCharacterMember(cfdigitset, ch); }
1137 WXDLLEXPORT int wxIsgraph(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch) && ch != ' '; }
1138 WXDLLEXPORT int wxIslower(wxChar ch) { return CFCharacterSetIsCharacterMember(cflowerset, ch); }
1139 WXDLLEXPORT int wxIsprint(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
1140 WXDLLEXPORT int wxIspunct(wxChar ch) { return CFCharacterSetIsCharacterMember(cfpunctset, ch); }
1141 WXDLLEXPORT int wxIsspace(wxChar ch) { return CFCharacterSetIsCharacterMember(cfspaceset, ch); }
1142 WXDLLEXPORT int wxIsupper(wxChar ch) { return CFCharacterSetIsCharacterMember(cfupperset, ch); }
1143 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxIsdigit(ch) || (ch>='a' && ch<='f') || (ch>='A' && ch<='F'); }
1144 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)tolower((char)(ch)); }
1145 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)toupper((char)(ch)); }
1146
1147 #endif // wxNEED_WX_CTYPE_H
1148
1149 #endif // defined(__DARWIN__) and OSX <= 10.2
1150
1151 #ifndef wxStrdupA
1152
1153 WXDLLEXPORT char *wxStrdupA(const char *s)
1154 {
1155 return strcpy((char *)malloc(strlen(s) + 1), s);
1156 }
1157
1158 #endif // wxStrdupA
1159
1160 #ifndef wxStrdupW
1161
1162 WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz)
1163 {
1164 size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t);
1165 wchar_t *ret = (wchar_t *) malloc(size);
1166 memcpy(ret, pwz, size);
1167 return ret;
1168 }
1169
1170 #endif // wxStrdupW
1171
1172 #ifndef wxStricmp
1173 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
1174 {
1175 register wxChar c1, c2;
1176 do {
1177 c1 = wxTolower(*psz1++);
1178 c2 = wxTolower(*psz2++);
1179 } while ( c1 && (c1 == c2) );
1180 return c1 - c2;
1181 }
1182 #endif
1183
1184 #ifndef wxStricmp
1185 int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n)
1186 {
1187 // initialize the variables just to suppress stupid gcc warning
1188 register wxChar c1 = 0, c2 = 0;
1189 while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
1190 if (n) {
1191 if (c1 < c2) return -1;
1192 if (c1 > c2) return 1;
1193 }
1194 return 0;
1195 }
1196 #endif
1197
1198 #ifndef wxSetlocale
1199 WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
1200 {
1201 char *localeOld = setlocale(category, wxConvLocal.cWX2MB(locale));
1202
1203 return wxWCharBuffer(wxConvLocal.cMB2WC(localeOld));
1204 }
1205 #endif
1206
1207 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
1208 WXDLLEXPORT size_t wxWcslen(const wchar_t *s)
1209 {
1210 size_t n = 0;
1211 while ( *s++ )
1212 n++;
1213
1214 return n;
1215 }
1216 #endif
1217
1218 // ----------------------------------------------------------------------------
1219 // string.h functions
1220 // ----------------------------------------------------------------------------
1221
1222 #ifdef wxNEED_WX_STRING_H
1223
1224 // RN: These need to be c externed for the regex lib
1225 #ifdef __cplusplus
1226 extern "C" {
1227 #endif
1228
1229 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
1230 {
1231 wxChar *ret = dest;
1232 while (*dest) dest++;
1233 while ((*dest++ = *src++));
1234 return ret;
1235 }
1236
1237 WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c)
1238 {
1239 // be careful here as the terminating NUL makes part of the string
1240 while ( *s != c )
1241 {
1242 if ( !*s++ )
1243 return NULL;
1244 }
1245
1246 return s;
1247 }
1248
1249 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
1250 {
1251 while ((*s1 == *s2) && *s1) s1++, s2++;
1252 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
1253 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
1254 return 0;
1255 }
1256
1257 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
1258 {
1259 wxChar *ret = dest;
1260 while ((*dest++ = *src++));
1261 return ret;
1262 }
1263
1264 WXDLLEXPORT size_t wxStrlen_(const wxChar *s)
1265 {
1266 size_t n = 0;
1267 while ( *s++ )
1268 n++;
1269
1270 return n;
1271 }
1272
1273
1274 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
1275 {
1276 wxChar *ret = dest;
1277 while (*dest) dest++;
1278 while (n && (*dest++ = *src++)) n--;
1279 return ret;
1280 }
1281
1282 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
1283 {
1284 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
1285 if (n) {
1286 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
1287 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
1288 }
1289 return 0;
1290 }
1291
1292 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
1293 {
1294 wxChar *ret = dest;
1295 while (n && (*dest++ = *src++)) n--;
1296 while (n) *dest++=0, n--; // the docs specify padding with zeroes
1297 return ret;
1298 }
1299
1300 WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
1301 {
1302 while (*s && !wxStrchr(accept, *s))
1303 s++;
1304
1305 return *s ? s : NULL;
1306 }
1307
1308 WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c)
1309 {
1310 const wxChar *ret = NULL;
1311 do
1312 {
1313 if ( *s == c )
1314 ret = s;
1315 s++;
1316 }
1317 while ( *s );
1318
1319 return ret;
1320 }
1321
1322 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
1323 {
1324 size_t len = 0;
1325 while (wxStrchr(accept, *s++)) len++;
1326 return len;
1327 }
1328
1329 WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle)
1330 {
1331 wxASSERT_MSG( needle != NULL, _T("NULL argument in wxStrstr") );
1332
1333 // VZ: this is not exactly the most efficient string search algorithm...
1334
1335 const size_t len = wxStrlen(needle);
1336
1337 while ( const wxChar *fnd = wxStrchr(haystack, *needle) )
1338 {
1339 if ( !wxStrncmp(fnd, needle, len) )
1340 return fnd;
1341
1342 haystack = fnd + 1;
1343 }
1344
1345 return NULL;
1346 }
1347
1348 #ifdef __cplusplus
1349 }
1350 #endif
1351
1352 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
1353 {
1354 const wxChar *start = nptr;
1355
1356 // FIXME: only correct for C locale
1357 while (wxIsspace(*nptr)) nptr++;
1358 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1359 while (wxIsdigit(*nptr)) nptr++;
1360 if (*nptr == wxT('.')) {
1361 nptr++;
1362 while (wxIsdigit(*nptr)) nptr++;
1363 }
1364 if (*nptr == wxT('E') || *nptr == wxT('e')) {
1365 nptr++;
1366 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1367 while (wxIsdigit(*nptr)) nptr++;
1368 }
1369
1370 wxString data(nptr, nptr-start);
1371 wxWX2MBbuf dat = data.mb_str(wxConvLocal);
1372 char *rdat = wxMBSTRINGCAST dat;
1373 double ret = strtod(dat, &rdat);
1374
1375 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
1376
1377 return ret;
1378 }
1379
1380 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
1381 {
1382 const wxChar *start = nptr;
1383
1384 // FIXME: only correct for C locale
1385 while (wxIsspace(*nptr)) nptr++;
1386 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1387 if (((base == 0) || (base == 16)) &&
1388 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
1389 nptr += 2;
1390 base = 16;
1391 }
1392 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
1393 else if (base == 0) base = 10;
1394
1395 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
1396 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
1397
1398 wxString data(start, nptr-start);
1399 wxWX2MBbuf dat = data.mb_str(wxConvLocal);
1400 char *rdat = wxMBSTRINGCAST dat;
1401 long int ret = strtol(dat, &rdat, base);
1402
1403 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
1404
1405 return ret;
1406 }
1407
1408 WXDLLEXPORT unsigned long int wxStrtoul(const wxChar *nptr, wxChar **endptr, int base)
1409 {
1410 return (unsigned long int) wxStrtol(nptr, endptr, base);
1411 }
1412
1413 #endif // wxNEED_WX_STRING_H
1414
1415 #ifdef wxNEED_WX_STDIO_H
1416 WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
1417 {
1418 char mode_buffer[10];
1419 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
1420 mode_buffer[i] = (char) mode[i];
1421
1422 return fopen( wxConvFile.cWX2MB(path), mode_buffer );
1423 }
1424
1425 WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
1426 {
1427 char mode_buffer[10];
1428 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
1429 mode_buffer[i] = (char) mode[i];
1430
1431 return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream );
1432 }
1433
1434 WXDLLEXPORT int wxRemove(const wxChar *path)
1435 {
1436 return remove( wxConvFile.cWX2MB(path) );
1437 }
1438
1439 WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
1440 {
1441 return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) );
1442 }
1443 #endif
1444
1445 #ifndef wxAtof
1446 double WXDLLEXPORT wxAtof(const wxChar *psz)
1447 {
1448 #ifdef __WXWINCE__
1449 double d;
1450 wxString str(psz);
1451 if (str.ToDouble(& d))
1452 return d;
1453 else
1454 return 0.0;
1455 #else
1456 return atof(wxConvLocal.cWX2MB(psz));
1457 #endif
1458 }
1459 #endif
1460
1461 #ifdef wxNEED_WX_STDLIB_H
1462 int WXDLLEXPORT wxAtoi(const wxChar *psz)
1463 {
1464 return atoi(wxConvLocal.cWX2MB(psz));
1465 }
1466
1467 long WXDLLEXPORT wxAtol(const wxChar *psz)
1468 {
1469 return atol(wxConvLocal.cWX2MB(psz));
1470 }
1471
1472 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
1473 {
1474 #if wxUSE_UNICODE
1475 // NB: buffer returned by getenv() is allowed to be overwritten next
1476 // time getenv() is called, so it is OK to use static string
1477 // buffer to hold the data.
1478 static wxWCharBuffer value((wxChar*)NULL);
1479 value = wxConvLocal.cMB2WX(getenv(wxConvLocal.cWX2MB(name)));
1480 return value.data();
1481 #else
1482 return getenv(name);
1483 #endif
1484 }
1485
1486 int WXDLLEXPORT wxSystem(const wxChar *psz)
1487 {
1488 return system(wxConvLocal.cWX2MB(psz));
1489 }
1490
1491 #endif // wxNEED_WX_STDLIB_H
1492
1493 #ifdef wxNEED_WX_TIME_H
1494 WXDLLEXPORT size_t wxStrftime(wxChar *s, size_t max, const wxChar *fmt, const struct tm *tm)
1495 {
1496 if (!max) return 0;
1497
1498 char *buf = (char *)malloc(max);
1499 size_t ret = strftime(buf, max, wxConvLocal.cWX2MB(fmt), tm);
1500 if (ret)
1501 {
1502 wxStrcpy(s, wxConvLocal.cMB2WX(buf));
1503 free(buf);
1504 return wxStrlen(s);
1505 }
1506 else
1507 {
1508 free(buf);
1509 *s = 0;
1510 return 0;
1511 }
1512 }
1513 #endif // wxNEED_WX_TIME_H
1514
1515 #ifndef wxCtime
1516 WXDLLEXPORT wxChar *wxCtime(const time_t *timep)
1517 {
1518 static wxChar buf[128];
1519
1520 wxStrncpy( buf, wxConvertMB2WX( ctime( timep ) ), sizeof( buf ) );
1521 buf[ sizeof( buf ) - 1 ] = _T('\0');
1522
1523 return buf;
1524 }
1525 #endif // wxCtime
1526
1527 #endif // wxUSE_WCHAR_T
1528
1529 // ----------------------------------------------------------------------------
1530 // functions which we may need even if !wxUSE_WCHAR_T
1531 // ----------------------------------------------------------------------------
1532
1533 #ifndef wxStrtok
1534
1535 WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
1536 {
1537 if (!psz)
1538 {
1539 psz = *save_ptr;
1540 if ( !psz )
1541 return NULL;
1542 }
1543
1544 psz += wxStrspn(psz, delim);
1545 if (!*psz)
1546 {
1547 *save_ptr = (wxChar *)NULL;
1548 return (wxChar *)NULL;
1549 }
1550
1551 wxChar *ret = psz;
1552 psz = wxStrpbrk(psz, delim);
1553 if (!psz)
1554 {
1555 *save_ptr = (wxChar*)NULL;
1556 }
1557 else
1558 {
1559 *psz = wxT('\0');
1560 *save_ptr = psz + 1;
1561 }
1562
1563 return ret;
1564 }
1565
1566 #endif // wxStrtok
1567
1568 // ----------------------------------------------------------------------------
1569 // missing C RTL functions
1570 // ----------------------------------------------------------------------------
1571
1572 #if wxNEED_STRDUP
1573
1574 char *strdup(const char *s)
1575 {
1576 char *dest = (char*) malloc( strlen( s ) + 1 ) ;
1577 if ( dest )
1578 strcpy( dest , s ) ;
1579 return dest ;
1580 }
1581 #endif // wxNEED_STRDUP
1582
1583 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1584
1585 void *calloc( size_t num, size_t size )
1586 {
1587 void** ptr = (void **)malloc(num * size);
1588 memset( ptr, 0, num * size);
1589 return ptr;
1590 }
1591
1592 #endif // __WXWINCE__ <= 211
1593