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