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