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