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