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