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