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