]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
Remove all extensions before inport, not just .py
[wxWidgets.git] / src / common / wxchar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxchar.cpp
3 // Purpose: wxChar implementation
4 // Author: Ove Kåven
5 // Modified by:
6 // Created: 09/04/99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "wxchar.h"
14 #endif
15
16 // ===========================================================================
17 // headers, declarations, constants
18 // ===========================================================================
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #define _ISOC9X_SOURCE 1 // to get vsscanf()
28 #define _BSD_SOURCE 1 // to still get strdup()
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifndef __WXWINCE__
35 #include <time.h>
36 #include <locale.h>
37 #else
38 #include "wx/msw/wince/time.h"
39 #endif
40
41 #ifndef WX_PRECOMP
42 #include "wx/defs.h"
43 #include "wx/wxchar.h"
44 #include "wx/string.h"
45 #include "wx/hash.h"
46 #endif
47
48 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
49 #include <windef.h>
50 #include <winbase.h>
51 #include <winnls.h>
52 #include <winnt.h>
53 #endif
54
55 #if defined(__MWERKS__) && __MSL__ >= 0x6000
56 using namespace std ;
57 #endif
58
59 #ifdef __WXMAC__
60 #include "wx/mac/private.h"
61 #endif
62
63 #if wxUSE_WCHAR_T
64 size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n)
65 {
66 // assume that we have mbsrtowcs() too if we have wcsrtombs()
67 #if HAVE_WCSRTOMBS
68 mbstate_t mbstate;
69 memset(&mbstate, 0, sizeof(mbstate_t));
70 #endif
71
72 if (buf) {
73 if (!n || !*psz) {
74 if (n) *buf = wxT('\0');
75 return 0;
76 }
77 #ifdef HAVE_WCSRTOMBS
78 return mbsrtowcs(buf, &psz, n, &mbstate);
79 #else
80 return mbstowcs(buf, psz, n);
81 #endif
82 }
83
84 #ifdef HAVE_WCSRTOMBS
85 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
86 #else
87 return mbstowcs((wchar_t *) NULL, psz, 0);
88 #endif
89 }
90
91 size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
92 {
93 #if HAVE_WCSRTOMBS
94 mbstate_t mbstate;
95 memset(&mbstate, 0, sizeof(mbstate_t));
96 #endif
97
98 if (buf) {
99 if (!n || !*pwz) {
100 // glibc2.1 chokes on null input
101 if (n) *buf = '\0';
102 return 0;
103 }
104 #if HAVE_WCSRTOMBS
105 return wcsrtombs(buf, &pwz, n, &mbstate);
106 #else
107 return wcstombs(buf, pwz, n);
108 #endif
109 }
110
111 #if HAVE_WCSRTOMBS
112 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
113 #else
114 return wcstombs((char *) NULL, pwz, 0);
115 #endif
116 }
117 #endif // wxUSE_WCHAR_T
118
119 bool WXDLLEXPORT wxOKlibc()
120 {
121 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
122 // glibc 2.0 uses UTF-8 even when it shouldn't
123 wchar_t res = 0;
124 if ((MB_CUR_MAX == 2) &&
125 (wxMB2WC(&res, "\xdd\xa5", 1) == 1) &&
126 (res==0x765)) {
127 // this is UTF-8 allright, check whether that's what we want
128 char *cur_locale = setlocale(LC_CTYPE, NULL);
129 if ((strlen(cur_locale) < 4) ||
130 (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) ||
131 (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) {
132 // nope, don't use libc conversion
133 return FALSE;
134 }
135 }
136 #endif
137 return TRUE;
138 }
139
140 // ============================================================================
141 // printf() functions business
142 // ============================================================================
143
144 // special test mode: define all functions below even if we don't really need
145 // them to be able to test them
146 #ifdef wxTEST_PRINTF
147 #undef wxFprintf
148 #undef wxPrintf
149 #undef wxSprintf
150 #undef wxVfprintf
151 #undef wxVsprintf
152 #undef wxVprintf
153 #undef wxVsnprintf_
154 #undef wxSnprintf_
155
156 #define wxNEED_WPRINTF
157
158 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr );
159 #endif
160
161 // ----------------------------------------------------------------------------
162 // implement [v]snprintf() if the system doesn't provide a safe one
163 // ----------------------------------------------------------------------------
164
165 #if !defined(wxVsnprintf_)
166 int WXDLLEXPORT wxVsnprintf_(wxChar *buf, size_t lenMax,
167 const wxChar *format, va_list argptr)
168 {
169 // buffer to avoid dynamic memory allocation each time for small strings
170 char szScratch[1024];
171
172 // number of characters in the buffer so far, must be less than lenMax
173 size_t lenCur = 0;
174
175 for ( size_t n = 0; ; n++ )
176 {
177 const wxChar chCur = format[n];
178
179 if ( chCur == wxT('%') )
180 {
181 static char s_szFlags[256] = "%";
182 size_t flagofs = 1;
183 bool adj_left = FALSE,
184 in_prec = FALSE,
185 prec_dot = FALSE,
186 done = FALSE;
187 int ilen = 0;
188 size_t min_width = 0,
189 max_width = wxSTRING_MAXLEN;
190 do
191 {
192
193 #define CHECK_PREC \
194 if (in_prec && !prec_dot) \
195 { \
196 s_szFlags[flagofs++] = '.'; \
197 prec_dot = TRUE; \
198 }
199
200 #define APPEND_CH(ch) \
201 if ( lenCur == lenMax ) \
202 return -1; \
203 \
204 buf[lenCur++] = ch
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 wxChar val = va_arg(argptr, int);
423 // we don't need to honor padding here, do we?
424 APPEND_CH(val);
425
426 done = TRUE;
427 }
428 break;
429
430 case wxT('s'):
431 if (ilen == -1)
432 {
433 // wx extension: we'll let %hs mean non-Unicode
434 // strings
435 char *val = va_arg(argptr, char *);
436 #if wxUSE_UNICODE
437 // ASCII->Unicode constructor handles max_width
438 // right
439 wxString s(val, wxConvLibc, max_width);
440 #else
441 size_t len = wxSTRING_MAXLEN;
442 if (val)
443 {
444 for ( len = 0;
445 val[len] && (len < max_width);
446 len++ )
447 ;
448 }
449 else
450 val = wxT("(null)");
451 wxString s(val, len);
452 #endif
453 if (s.Len() < min_width)
454 s.Pad(min_width - s.Len(), wxT(' '), adj_left);
455
456 APPEND_STR(s);
457 }
458 else
459 {
460 wxChar *val = va_arg(argptr, wxChar *);
461 size_t len = wxSTRING_MAXLEN;
462 if (val)
463 {
464 for ( len = 0;
465 val[len] && (len < max_width);
466 len++ )
467 ;
468 }
469 else
470 val = wxT("(null)");
471
472 wxString s(val, len);
473 if (s.Len() < min_width)
474 s.Pad(min_width - s.Len(), wxT(' '), adj_left);
475
476 APPEND_STR(s);
477 }
478 done = TRUE;
479 break;
480
481 case wxT('n'):
482 if (ilen == 0)
483 {
484 int *val = va_arg(argptr, int *);
485 *val = lenCur;
486 }
487 else if (ilen == -1)
488 {
489 short int *val = va_arg(argptr, short int *);
490 *val = lenCur;
491 }
492 else if (ilen >= 1)
493 {
494 long int *val = va_arg(argptr, long int *);
495 *val = lenCur;
496 }
497 done = TRUE;
498 break;
499
500 default:
501 // bad format, leave unchanged
502 APPEND_CH(_T('%'));
503 APPEND_CH(ch);
504 done = TRUE;
505 break;
506 }
507 }
508 while (!done);
509 }
510 else
511 {
512 APPEND_CH(chCur);
513 }
514
515 // terminating NUL?
516 if ( !chCur )
517 break;
518 }
519
520 return lenCur;
521 }
522
523 #undef APPEND_CH
524 #undef APPEND_STR
525 #undef CHECK_PREC
526
527 #endif // !wxVsnprintfA
528
529 #if !defined(wxSnprintf_)
530 int WXDLLEXPORT wxSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...)
531 {
532 va_list argptr;
533 va_start(argptr, format);
534
535 int iLen = wxVsnprintf_(buf, len, format, argptr);
536
537 va_end(argptr);
538
539 return iLen;
540 }
541 #endif // wxSnprintf_
542
543 // ----------------------------------------------------------------------------
544 // implement the standard IO functions for wide char if libc doesn't have them
545 // ----------------------------------------------------------------------------
546
547 #ifdef wxNEED_FPUTWC
548
549 int wxFputs(const wchar_t *ws, FILE *stream)
550 {
551 // counting the number of wide characters written isn't worth the trouble,
552 // simply distinguish between ok and error
553 return fputs(wxConvLibc.cWC2MB(ws), stream) == -1 ? -1 : 0;
554 }
555
556 int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream)
557 {
558 wchar_t ws[2] = { wc, L'\0' };
559
560 return wxFputs(ws, stream);
561 }
562
563 #endif // wxNEED_FPUTWC
564
565 // NB: we only implement va_list functions here, the ones taking ... are
566 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
567 // the definitions there to avoid duplicating them here
568 #ifdef wxNEED_WPRINTF
569
570 // TODO: implement the scanf() functions
571 int vwscanf(const wxChar *format, va_list argptr)
572 {
573 wxFAIL_MSG( _T("TODO") );
574
575 return -1;
576 }
577
578 int vswscanf(const wxChar *ws, const wxChar *format, va_list argptr)
579 {
580 wxFAIL_MSG( _T("TODO") );
581
582 return -1;
583 }
584
585 int vfwscanf(FILE *stream, const wxChar *format, va_list argptr)
586 {
587 wxFAIL_MSG( _T("TODO") );
588
589 return -1;
590 }
591
592 #define vswprintf wxVsnprintf_
593
594 int vfwprintf(FILE *stream, const wxChar *format, va_list argptr)
595 {
596 wxString s;
597 int rc = s.PrintfV(format, argptr);
598
599 if ( rc != -1 )
600 {
601 // we can't do much better without Unicode support in libc...
602 if ( fprintf(stream, "%s", (const char*)s.mb_str() ) == -1 )
603 return -1;
604 }
605
606 return rc;
607 }
608
609 int vwprintf(const wxChar *format, va_list argptr)
610 {
611 return wxVfprintf(stdout, format, argptr);
612 }
613
614 #endif // wxNEED_WPRINTF
615
616 #ifdef wxNEED_PRINTF_CONVERSION
617
618 // ----------------------------------------------------------------------------
619 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
620 // ----------------------------------------------------------------------------
621
622 /*
623 Here are the gory details. We want to follow the Windows/MS conventions,
624 that is to have
625
626 In ANSI mode:
627
628 format specifier results in
629 -----------------------------------
630 %c, %hc, %hC char
631 %lc, %C, %lC wchar_t
632
633 In Unicode mode:
634
635 format specifier results in
636 -----------------------------------
637 %hc, %C, %hC char
638 %c, %lc, %lC wchar_t
639
640
641 while on POSIX systems we have %C identical to %lc and %c always means char
642 (in any mode) while %lc always means wchar_t,
643
644 So to use native functions in order to get our semantics we must do the
645 following translations in Unicode mode (nothing to do in ANSI mode):
646
647 wxWindows specifier POSIX specifier
648 ----------------------------------------
649
650 %hc, %C, %hC %c
651 %c %lc
652
653
654 And, of course, the same should be done for %s as well.
655 */
656
657 class wxFormatConverter
658 {
659 public:
660 wxFormatConverter(const wxChar *format);
661
662 // notice that we only translated the string if m_fmtOrig == NULL (as set
663 // by CopyAllBefore()), otherwise we should simply use the original format
664 operator const wxChar *() const
665 { return m_fmtOrig ? m_fmtOrig : m_fmt.c_str(); }
666
667 private:
668 // copy another character to the translated format: this function does the
669 // copy if we are translating but doesn't do anything at all if we don't,
670 // so we don't create the translated format string at all unless we really
671 // need to (i.e. InsertFmtChar() is called)
672 wxChar CopyFmtChar(wxChar ch)
673 {
674 if ( !m_fmtOrig )
675 {
676 // we're translating, do copy
677 m_fmt += ch;
678 }
679 else
680 {
681 // simply increase the count which should be copied by
682 // CopyAllBefore() later if needed
683 m_nCopied++;
684 }
685
686 return ch;
687 }
688
689 // insert an extra character
690 void InsertFmtChar(wxChar ch)
691 {
692 if ( m_fmtOrig )
693 {
694 // so far we haven't translated anything yet
695 CopyAllBefore();
696 }
697
698 m_fmt += ch;
699 }
700
701 void CopyAllBefore()
702 {
703 wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") );
704
705 m_fmt = wxString(m_fmtOrig, m_nCopied);
706
707 // we won't need it any longer
708 m_fmtOrig = NULL;
709 }
710
711 static bool IsFlagChar(wxChar ch)
712 {
713 return ch == _T('-') || ch == _T('+') ||
714 ch == _T('0') || ch == _T(' ') || ch == _T('#');
715 }
716
717 void SkipDigits(const wxChar **ppc)
718 {
719 while ( **ppc >= _T('0') && **ppc <= _T('9') )
720 CopyFmtChar(*(*ppc)++);
721 }
722
723 // the translated format
724 wxString m_fmt;
725
726 // the original format
727 const wxChar *m_fmtOrig;
728
729 // the number of characters already copied
730 size_t m_nCopied;
731 };
732
733 wxFormatConverter::wxFormatConverter(const wxChar *format)
734 {
735 m_fmtOrig = format;
736 m_nCopied = 0;
737
738 while ( *format )
739 {
740 if ( CopyFmtChar(*format++) == _T('%') )
741 {
742 // skip any flags
743 while ( IsFlagChar(*format) )
744 CopyFmtChar(*format++);
745
746 // and possible width
747 if ( *format == _T('*') )
748 CopyFmtChar(*format++);
749 else
750 SkipDigits(&format);
751
752 // precision?
753 if ( *format == _T('.') )
754 {
755 CopyFmtChar(*format++);
756 if ( *format == _T('*') )
757 CopyFmtChar(*format++);
758 else
759 SkipDigits(&format);
760 }
761
762 // next we can have a size modifier
763 enum
764 {
765 Default,
766 Short,
767 Long
768 } size;
769
770 switch ( *format )
771 {
772 case _T('h'):
773 size = Short;
774 format++;
775 break;
776
777 case _T('l'):
778 // "ll" has a different meaning!
779 if ( format[1] != _T('l') )
780 {
781 size = Long;
782 format++;
783 break;
784 }
785 //else: fall through
786
787 default:
788 size = Default;
789 }
790
791 // and finally we should have the type
792 switch ( *format )
793 {
794 case _T('C'):
795 case _T('S'):
796 // %C and %hC -> %c and %lC -> %lc
797 if ( size == Long )
798 CopyFmtChar(_T('l'));
799
800 InsertFmtChar(*format++ == _T('C') ? _T('c') : _T('s'));
801 break;
802
803 case _T('c'):
804 case _T('s'):
805 // %c -> %lc but %hc stays %hc and %lc is still %lc
806 if ( size == Default)
807 InsertFmtChar(_T('l'));
808 // fall through
809
810 default:
811 // nothing special to do
812 if ( size != Default )
813 CopyFmtChar(*(format - 1));
814 CopyFmtChar(*format++);
815 }
816 }
817 }
818 }
819
820 #else // !wxNEED_PRINTF_CONVERSION
821 // no conversion necessary
822 #define wxFormatConverter(x) (x)
823 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
824
825 #ifdef __WXDEBUG__
826 // For testing the format converter
827 wxString wxConvertFormat(const wxChar *format)
828 {
829 return wxString(wxFormatConverter(format));
830 }
831 #endif
832
833 // ----------------------------------------------------------------------------
834 // wxPrintf(), wxScanf() and relatives
835 // ----------------------------------------------------------------------------
836
837 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
838
839 int wxScanf( const wxChar *format, ... )
840 {
841 va_list argptr;
842 va_start(argptr, format);
843
844 int ret = vwscanf(wxFormatConverter(format), argptr );
845
846 va_end(argptr);
847
848 return ret;
849 }
850
851 int wxSscanf( const wxChar *str, const wxChar *format, ... )
852 {
853 va_list argptr;
854 va_start(argptr, format);
855
856 int ret = vswscanf( str, wxFormatConverter(format), argptr );
857
858 va_end(argptr);
859
860 return ret;
861 }
862
863 int wxFscanf( FILE *stream, const wxChar *format, ... )
864 {
865 va_list argptr;
866 va_start(argptr, format);
867 int ret = vfwscanf(stream, wxFormatConverter(format), argptr);
868
869 va_end(argptr);
870
871 return ret;
872 }
873
874 int wxPrintf( const wxChar *format, ... )
875 {
876 va_list argptr;
877 va_start(argptr, format);
878
879 int ret = vwprintf( wxFormatConverter(format), argptr );
880
881 va_end(argptr);
882
883 return ret;
884 }
885
886 #ifndef wxSnprintf
887 int wxSnprintf( wxChar *str, size_t size, const wxChar *format, ... )
888 {
889 va_list argptr;
890 va_start(argptr, format);
891
892 int ret = vswprintf( str, size, wxFormatConverter(format), argptr );
893
894 va_end(argptr);
895
896 return ret;
897 }
898 #endif // wxSnprintf
899
900 int wxSprintf( wxChar *str, const wxChar *format, ... )
901 {
902 va_list argptr;
903 va_start(argptr, format);
904
905 // callers of wxSprintf() deserve what they get
906 int ret = vswprintf( str, UINT_MAX, wxFormatConverter(format), argptr );
907
908 va_end(argptr);
909
910 return ret;
911 }
912
913 int wxFprintf( FILE *stream, const wxChar *format, ... )
914 {
915 va_list argptr;
916 va_start( argptr, format );
917
918 int ret = vfwprintf( stream, wxFormatConverter(format), argptr );
919
920 va_end(argptr);
921
922 return ret;
923 }
924
925 int wxVsscanf( const wxChar *str, const wxChar *format, va_list argptr )
926 {
927 return vswscanf( str, wxFormatConverter(format), argptr );
928 }
929
930 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr )
931 {
932 return vfwprintf( stream, wxFormatConverter(format), argptr );
933 }
934
935 int wxVprintf( const wxChar *format, va_list argptr )
936 {
937 return vwprintf( wxFormatConverter(format), argptr );
938 }
939
940 #ifndef wxVsnprintf
941 int wxVsnprintf( wxChar *str, size_t size, const wxChar *format, va_list argptr )
942 {
943 return vswprintf( str, size, wxFormatConverter(format), argptr );
944 }
945 #endif // wxVsnprintf
946
947 int wxVsprintf( wxChar *str, const wxChar *format, va_list argptr )
948 {
949 // same as for wxSprintf()
950 return vswprintf(str, UINT_MAX, wxFormatConverter(format), argptr);
951 }
952
953 #endif // wxNEED_PRINTF_CONVERSION
954
955 #if wxUSE_WCHAR_T
956
957 // ----------------------------------------------------------------------------
958 // ctype.h stuff (currently unused)
959 // ----------------------------------------------------------------------------
960
961 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
962 inline WORD wxMSW_ctype(wxChar ch)
963 {
964 WORD ret;
965 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
966 return ret;
967 }
968
969 WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); }
970 WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); }
971 WXDLLEXPORT int wxIsctrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; }
972 WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; }
973 WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); }
974 WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); }
975 WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); }
976 WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; }
977 WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; }
978 WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); }
979 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; }
980 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
981 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
982 #endif
983
984 #ifndef wxStrdupA
985
986 WXDLLEXPORT char *wxStrdupA(const char *s)
987 {
988 return strcpy((char *)malloc(strlen(s) + 1), s);
989 }
990
991 #endif // wxStrdupA
992
993 #ifndef wxStrdupW
994
995 WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz)
996 {
997 size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t);
998 wchar_t *ret = (wchar_t *) malloc(size);
999 memcpy(ret, pwz, size);
1000 return ret;
1001 }
1002
1003 #endif // wxStrdupW
1004
1005 #ifndef wxStricmp
1006 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
1007 {
1008 register wxChar c1, c2;
1009 do {
1010 c1 = wxTolower(*psz1++);
1011 c2 = wxTolower(*psz2++);
1012 } while ( c1 && (c1 == c2) );
1013 return c1 - c2;
1014 }
1015 #endif
1016
1017 #ifndef wxStricmp
1018 int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n)
1019 {
1020 // initialize the variables just to suppress stupid gcc warning
1021 register wxChar c1 = 0, c2 = 0;
1022 while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
1023 if (n) {
1024 if (c1 < c2) return -1;
1025 if (c1 > c2) return 1;
1026 }
1027 return 0;
1028 }
1029 #endif
1030
1031 #ifndef wxSetlocale
1032 WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
1033 {
1034 char *localeOld = setlocale(category, wxConvLocal.cWX2MB(locale));
1035
1036 return wxWCharBuffer(wxConvLocal.cMB2WC(localeOld));
1037 }
1038 #endif
1039
1040 // ----------------------------------------------------------------------------
1041 // string.h functions
1042 // ----------------------------------------------------------------------------
1043
1044 #ifdef wxNEED_WX_STRING_H
1045 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
1046 {
1047 wxChar *ret = dest;
1048 while (*dest) dest++;
1049 while ((*dest++ = *src++));
1050 return ret;
1051 }
1052
1053 WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c)
1054 {
1055 // be careful here as the terminating NUL makes part of the string
1056 while ( *s != c )
1057 {
1058 if ( !*s++ )
1059 return NULL;
1060 }
1061
1062 return s;
1063 }
1064
1065 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
1066 {
1067 while ((*s1 == *s2) && *s1) s1++, s2++;
1068 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
1069 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
1070 return 0;
1071 }
1072
1073 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
1074 {
1075 wxChar *ret = dest;
1076 while ((*dest++ = *src++));
1077 return ret;
1078 }
1079
1080 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
1081 {
1082 wxChar *ret = dest;
1083 while (*dest) dest++;
1084 while (n && (*dest++ = *src++)) n--;
1085 return ret;
1086 }
1087
1088 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
1089 {
1090 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
1091 if (n) {
1092 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
1093 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
1094 }
1095 return 0;
1096 }
1097
1098 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
1099 {
1100 wxChar *ret = dest;
1101 while (n && (*dest++ = *src++)) n--;
1102 while (n) *dest++=0, n--; // the docs specify padding with zeroes
1103 return ret;
1104 }
1105
1106 WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
1107 {
1108 while (*s && !wxStrchr(accept, *s))
1109 s++;
1110
1111 return *s ? s : NULL;
1112 }
1113
1114 WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c)
1115 {
1116 const wxChar *ret = NULL;
1117 do
1118 {
1119 if ( *s == c )
1120 ret = s;
1121 s++;
1122 }
1123 while ( *s );
1124
1125 return ret;
1126 }
1127
1128 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
1129 {
1130 size_t len = 0;
1131 while (wxStrchr(accept, *s++)) len++;
1132 return len;
1133 }
1134
1135 WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle)
1136 {
1137 wxCHECK_RET( needle, NULL, _T("NULL argument in wxStrstr") );
1138
1139 // VZ: this is not exactly the most efficient string search algorithm...
1140
1141 const size_t len = wxStrlen(needle);
1142
1143 while ( const wxChar *fnd = wxStrchr(haystack, *needle) )
1144 {
1145 if ( !wxStrncmp(fnd, needle, len) )
1146 return fnd;
1147
1148 haystack = fnd + 1;
1149 }
1150
1151 return NULL;
1152 }
1153
1154 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
1155 {
1156 const wxChar *start = nptr;
1157
1158 // FIXME: only correct for C locale
1159 while (wxIsspace(*nptr)) nptr++;
1160 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1161 while (wxIsdigit(*nptr)) nptr++;
1162 if (*nptr == wxT('.')) {
1163 nptr++;
1164 while (wxIsdigit(*nptr)) nptr++;
1165 }
1166 if (*nptr == wxT('E') || *nptr == wxT('e')) {
1167 nptr++;
1168 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1169 while (wxIsdigit(*nptr)) nptr++;
1170 }
1171
1172 wxString data(nptr, nptr-start);
1173 wxWX2MBbuf dat = data.mb_str(wxConvLocal);
1174 char *rdat = wxMBSTRINGCAST dat;
1175 double ret = strtod(dat, &rdat);
1176
1177 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
1178
1179 return ret;
1180 }
1181
1182 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
1183 {
1184 const wxChar *start = nptr;
1185
1186 // FIXME: only correct for C locale
1187 while (wxIsspace(*nptr)) nptr++;
1188 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1189 if (((base == 0) || (base == 16)) &&
1190 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
1191 nptr += 2;
1192 base = 16;
1193 }
1194 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
1195 else if (base == 0) base = 10;
1196
1197 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
1198 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
1199
1200 wxString data(nptr, nptr-start);
1201 wxWX2MBbuf dat = data.mb_str(wxConvLocal);
1202 char *rdat = wxMBSTRINGCAST dat;
1203 long int ret = strtol(dat, &rdat, base);
1204
1205 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
1206
1207 return ret;
1208 }
1209 #endif // wxNEED_WX_STRING_H
1210
1211 #ifdef wxNEED_WX_STDIO_H
1212 WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
1213 {
1214 char mode_buffer[10];
1215 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
1216 mode_buffer[i] = (char) mode[i];
1217
1218 return fopen( wxConvFile.cWX2MB(path), mode_buffer );
1219 }
1220
1221 WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
1222 {
1223 char mode_buffer[10];
1224 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
1225 mode_buffer[i] = (char) mode[i];
1226
1227 return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream );
1228 }
1229
1230 WXDLLEXPORT int wxRemove(const wxChar *path)
1231 {
1232 return remove( wxConvFile.cWX2MB(path) );
1233 }
1234
1235 WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
1236 {
1237 return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) );
1238 }
1239 #endif
1240
1241 #ifndef wxAtof
1242 double WXDLLEXPORT wxAtof(const wxChar *psz)
1243 {
1244 #ifdef __WXWINCE__
1245 double d;
1246 wxString str(psz);
1247 if (str.ToDouble(& d))
1248 return d;
1249 else
1250 return 0.0;
1251 #else
1252 return atof(wxConvLocal.cWX2MB(psz));
1253 #endif
1254 }
1255 #endif
1256
1257 #ifdef wxNEED_WX_STDLIB_H
1258 int WXDLLEXPORT wxAtoi(const wxChar *psz)
1259 {
1260 return atoi(wxConvLocal.cWX2MB(psz));
1261 }
1262
1263 long WXDLLEXPORT wxAtol(const wxChar *psz)
1264 {
1265 return atol(wxConvLocal.cWX2MB(psz));
1266 }
1267
1268 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
1269 {
1270 static wxHashTable env;
1271
1272 // check if we already have stored the converted env var
1273 wxObject *data = env.Get(name);
1274 if (!data)
1275 {
1276 // nope, retrieve it,
1277 #if wxUSE_UNICODE
1278 wxCharBuffer buffer = wxConvLocal.cWX2MB(name);
1279 // printf( "buffer %s\n", (const char*) buffer );
1280 const char *val = getenv( (const char *)buffer );
1281 #else
1282 const char *val = getenv( name );
1283 #endif
1284
1285 if (!val) return (wxChar *)NULL;
1286 // printf( "home %s\n", val );
1287
1288 // convert it,
1289 #if wxUSE_UNICODE
1290 data = (wxObject *)new wxString(val, wxConvLocal);
1291 #else
1292 data = (wxObject *)new wxString(val);
1293 #endif
1294
1295 // and store it
1296 env.Put(name, data);
1297 }
1298 // return converted env var
1299 return (wxChar *)((wxString *)data)->c_str();
1300 }
1301
1302 int WXDLLEXPORT wxSystem(const wxChar *psz)
1303 {
1304 return system(wxConvLocal.cWX2MB(psz));
1305 }
1306
1307 #endif // wxNEED_WX_STDLIB_H
1308
1309 #ifdef wxNEED_WX_TIME_H
1310 WXDLLEXPORT size_t wxStrftime(wxChar *s, size_t max, const wxChar *fmt, const struct tm *tm)
1311 {
1312 if (!max) return 0;
1313
1314 char *buf = (char *)malloc(max);
1315 size_t ret = strftime(buf, max, wxConvLocal.cWX2MB(fmt), tm);
1316 if (ret)
1317 {
1318 wxStrcpy(s, wxConvLocal.cMB2WX(buf));
1319 free(buf);
1320 return wxStrlen(s);
1321 }
1322 else
1323 {
1324 free(buf);
1325 *s = 0;
1326 return 0;
1327 }
1328 }
1329 #endif // wxNEED_WX_TIME_H
1330
1331 #endif // wxUSE_WCHAR_T
1332
1333 // ----------------------------------------------------------------------------
1334 // functions which we may need even if !wxUSE_WCHAR_T
1335 // ----------------------------------------------------------------------------
1336
1337 #ifndef wxStrtok
1338
1339 WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
1340 {
1341 if (!psz)
1342 {
1343 psz = *save_ptr;
1344 if ( !psz )
1345 return NULL;
1346 }
1347
1348 psz += wxStrspn(psz, delim);
1349 if (!*psz)
1350 {
1351 *save_ptr = (wxChar *)NULL;
1352 return (wxChar *)NULL;
1353 }
1354
1355 wxChar *ret = psz;
1356 psz = wxStrpbrk(psz, delim);
1357 if (!psz)
1358 {
1359 *save_ptr = (wxChar*)NULL;
1360 }
1361 else
1362 {
1363 *psz = wxT('\0');
1364 *save_ptr = psz + 1;
1365 }
1366
1367 return ret;
1368 }
1369
1370 #endif // wxStrtok
1371
1372 // ----------------------------------------------------------------------------
1373 // missing C RTL functions
1374 // ----------------------------------------------------------------------------
1375
1376 #if (defined(__MWERKS__) && !defined(__MACH__) && (__MSL__ < 0x00008000)) || \
1377 defined(__WXWINCE__)
1378 char *strdup(const char *s)
1379 {
1380 char *dest = (char*) malloc( strlen( s ) + 1 ) ;
1381 if ( dest )
1382 strcpy( dest , s ) ;
1383 return dest ;
1384 }
1385 #endif
1386
1387 #if (defined(__MWERKS__) && !defined(__MACH__)) || (defined(__WXWINCE__) && _WIN32_WCE <= 211)
1388
1389 int isascii( int c )
1390 {
1391 return ( c >= 0 && c < 128 );
1392 }
1393 #endif
1394
1395 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1396 #if (_WIN32_WCE < 300)
1397 void *calloc( size_t num, size_t size )
1398 {
1399 void** ptr = (void **)malloc(num * size);
1400 memset( ptr, 0, num * size);
1401 return ptr;
1402 }
1403 #endif
1404
1405 int isspace(int c)
1406 {
1407 return (c == ' ');
1408 }
1409
1410 #endif
1411