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