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