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