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