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