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