1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxChar implementation
5 // Modified by: Ron Lee
8 // Copyright: (c) wxWindows copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "wxchar.h"
16 // ===========================================================================
17 // headers, declarations, constants
18 // ===========================================================================
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #define _ISOC9X_SOURCE 1 // to get vsscanf()
28 #define _BSD_SOURCE 1 // to still get strdup()
38 #include "wx/msw/wince/time.h"
43 #include "wx/wxchar.h"
44 #include "wx/string.h"
48 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
55 #if defined(__MWERKS__) && __MSL__ >= 0x6000
60 #include "wx/mac/private.h"
64 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
66 // assume that we have mbsrtowcs() too if we have wcsrtombs()
69 memset(&mbstate
, 0, sizeof(mbstate_t));
74 if (n
) *buf
= wxT('\0');
78 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
80 return mbstowcs(buf
, psz
, n
);
85 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
87 return mbstowcs((wchar_t *) NULL
, psz
, 0);
91 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
95 memset(&mbstate
, 0, sizeof(mbstate_t));
100 // glibc2.1 chokes on null input
105 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
107 return wcstombs(buf
, pwz
, n
);
112 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
114 return wcstombs((char *) NULL
, pwz
, 0);
117 #endif // wxUSE_WCHAR_T
119 bool WXDLLEXPORT
wxOKlibc()
121 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
122 // glibc 2.0 uses UTF-8 even when it shouldn't
124 if ((MB_CUR_MAX
== 2) &&
125 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
127 // this is UTF-8 allright, check whether that's what we want
128 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
129 if ((strlen(cur_locale
) < 4) ||
130 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
131 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
132 // nope, don't use libc conversion
140 // ============================================================================
141 // printf() functions business
142 // ============================================================================
144 // special test mode: define all functions below even if we don't really need
145 // them to be able to test them
156 #define wxNEED_WPRINTF
158 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
161 // ----------------------------------------------------------------------------
162 // implement [v]snprintf() if the system doesn't provide a safe one
163 // ----------------------------------------------------------------------------
165 #if !defined(wxVsnprintf_)
166 int WXDLLEXPORT
wxVsnprintf_(wxChar
*buf
, size_t lenMax
,
167 const wxChar
*format
, va_list argptr
)
169 // buffer to avoid dynamic memory allocation each time for small strings
170 char szScratch
[1024];
172 // number of characters in the buffer so far, must be less than lenMax
175 for ( size_t n
= 0; ; n
++ )
177 const wxChar chCur
= format
[n
];
179 if ( chCur
== wxT('%') )
181 static char s_szFlags
[256] = "%";
183 bool adj_left
= FALSE
,
188 size_t min_width
= 0,
189 max_width
= wxSTRING_MAXLEN
;
194 if (in_prec && !prec_dot) \
196 s_szFlags[flagofs++] = '.'; \
200 #define APPEND_CH(ch) \
201 if ( lenCur == lenMax ) \
206 #define APPEND_STR(s) \
208 for ( const wxChar *p = s; *p; p++ ) \
215 const wxChar ch
= format
[++n
];
235 s_szFlags
[flagofs
++] = ch
;
241 s_szFlags
[flagofs
++] = ch
;
249 // dot will be auto-added to s_szFlags if non-negative
256 s_szFlags
[flagofs
++] = ch
;
262 s_szFlags
[flagofs
++] = ch
;
269 s_szFlags
[flagofs
++] = ch
;
275 s_szFlags
[flagofs
++] = ch
;
280 int len
= va_arg(argptr
, int);
291 adj_left
= !adj_left
;
292 s_szFlags
[flagofs
++] = '-';
297 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
301 case wxT('1'): case wxT('2'): case wxT('3'):
302 case wxT('4'): case wxT('5'): case wxT('6'):
303 case wxT('7'): case wxT('8'): case wxT('9'):
307 while ( (format
[n
] >= wxT('0')) &&
308 (format
[n
] <= wxT('9')) )
310 s_szFlags
[flagofs
++] = format
[n
];
311 len
= len
*10 + (format
[n
] - wxT('0'));
320 n
--; // the main loop pre-increments n again
331 s_szFlags
[flagofs
++] = ch
;
332 s_szFlags
[flagofs
] = '\0';
335 int val
= va_arg(argptr
, int);
336 ::sprintf(szScratch
, s_szFlags
, val
);
340 // NB: 'short int' value passed through '...'
341 // is promoted to 'int', so we have to get
342 // an int from stack even if we need a short
343 short int val
= (short int) va_arg(argptr
, int);
344 ::sprintf(szScratch
, s_szFlags
, val
);
348 long int val
= va_arg(argptr
, long int);
349 ::sprintf(szScratch
, s_szFlags
, val
);
354 long long int val
= va_arg(argptr
, long long int);
355 ::sprintf(szScratch
, s_szFlags
, val
);
357 long int val
= va_arg(argptr
, long int);
358 ::sprintf(szScratch
, s_szFlags
, val
);
359 #endif // long long/!long long
363 size_t val
= va_arg(argptr
, size_t);
364 ::sprintf(szScratch
, s_szFlags
, val
);
368 const wxMB2WXbuf tmp
=
369 wxConvLibc
.cMB2WX(szScratch
);
382 s_szFlags
[flagofs
++] = ch
;
383 s_szFlags
[flagofs
] = '\0';
386 long double val
= va_arg(argptr
, long double);
387 ::sprintf(szScratch
, s_szFlags
, val
);
391 double val
= va_arg(argptr
, double);
392 ::sprintf(szScratch
, s_szFlags
, val
);
396 const wxMB2WXbuf tmp
=
397 wxConvLibc
.cMB2WX(szScratch
);
406 void *val
= va_arg(argptr
, void *);
408 s_szFlags
[flagofs
++] = ch
;
409 s_szFlags
[flagofs
] = '\0';
410 ::sprintf(szScratch
, s_szFlags
, val
);
412 const wxMB2WXbuf tmp
=
413 wxConvLibc
.cMB2WX(szScratch
);
422 wxChar val
= va_arg(argptr
, int);
423 // we don't need to honor padding here, do we?
433 // wx extension: we'll let %hs mean non-Unicode
435 char *val
= va_arg(argptr
, char *);
437 // ASCII->Unicode constructor handles max_width
439 wxString
s(val
, wxConvLibc
, max_width
);
441 size_t len
= wxSTRING_MAXLEN
;
445 val
[len
] && (len
< max_width
);
451 wxString
s(val
, len
);
453 if (s
.Len() < min_width
)
454 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
460 wxChar
*val
= va_arg(argptr
, wxChar
*);
461 size_t len
= wxSTRING_MAXLEN
;
465 val
[len
] && (len
< max_width
);
472 wxString
s(val
, len
);
473 if (s
.Len() < min_width
)
474 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
484 int *val
= va_arg(argptr
, int *);
489 short int *val
= va_arg(argptr
, short int *);
494 long int *val
= va_arg(argptr
, long int *);
501 // bad format, leave unchanged
527 #endif // !wxVsnprintfA
529 #if !defined(wxSnprintf_)
530 int WXDLLEXPORT
wxSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
533 va_start(argptr
, format
);
535 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
541 #endif // wxSnprintf_
544 /* Digital Mars adds count to _stprintf (C99) so convert */
546 int wxSprintf (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
550 va_start( arglist
, format
);
551 int iLen
= swprintf ( s
, -1, format
, arglist
);
556 #endif // wxUSE_UNICODE
560 // ----------------------------------------------------------------------------
561 // implement the standard IO functions for wide char if libc doesn't have them
562 // ----------------------------------------------------------------------------
566 int wxFputs(const wchar_t *ws
, FILE *stream
)
568 // counting the number of wide characters written isn't worth the trouble,
569 // simply distinguish between ok and error
570 return fputs(wxConvLibc
.cWC2MB(ws
), stream
) == -1 ? -1 : 0;
573 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
575 wchar_t ws
[2] = { wc
, L
'\0' };
577 return wxFputs(ws
, stream
);
580 #endif // wxNEED_FPUTWC
582 // NB: we only implement va_list functions here, the ones taking ... are
583 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
584 // the definitions there to avoid duplicating them here
585 #ifdef wxNEED_WPRINTF
587 // TODO: implement the scanf() functions
588 int vwscanf(const wxChar
*format
, va_list argptr
)
590 wxFAIL_MSG( _T("TODO") );
595 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
597 wxFAIL_MSG( _T("TODO") );
602 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
604 wxFAIL_MSG( _T("TODO") );
609 #define vswprintf wxVsnprintf_
611 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
614 int rc
= s
.PrintfV(format
, argptr
);
618 // we can't do much better without Unicode support in libc...
619 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
626 int vwprintf(const wxChar
*format
, va_list argptr
)
628 return wxVfprintf(stdout
, format
, argptr
);
631 #endif // wxNEED_WPRINTF
633 #ifdef wxNEED_PRINTF_CONVERSION
635 // ----------------------------------------------------------------------------
636 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
637 // ----------------------------------------------------------------------------
640 Here are the gory details. We want to follow the Windows/MS conventions,
645 format specifier results in
646 -----------------------------------
652 format specifier results in
653 -----------------------------------
658 while on POSIX systems we have %C identical to %lc and %c always means char
659 (in any mode) while %lc always means wchar_t,
661 So to use native functions in order to get our semantics we must do the
662 following translations in Unicode mode (nothing to do in ANSI mode):
664 wxWindows specifier POSIX specifier
665 ----------------------------------------
671 And, of course, the same should be done for %s as well.
674 class wxFormatConverter
677 wxFormatConverter(const wxChar
*format
);
679 // notice that we only translated the string if m_fmtOrig == NULL (as set
680 // by CopyAllBefore()), otherwise we should simply use the original format
681 operator const wxChar
*() const
682 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
685 // copy another character to the translated format: this function does the
686 // copy if we are translating but doesn't do anything at all if we don't,
687 // so we don't create the translated format string at all unless we really
688 // need to (i.e. InsertFmtChar() is called)
689 wxChar
CopyFmtChar(wxChar ch
)
693 // we're translating, do copy
698 // simply increase the count which should be copied by
699 // CopyAllBefore() later if needed
706 // insert an extra character
707 void InsertFmtChar(wxChar ch
)
711 // so far we haven't translated anything yet
720 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
722 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
724 // we won't need it any longer
728 static bool IsFlagChar(wxChar ch
)
730 return ch
== _T('-') || ch
== _T('+') ||
731 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
734 void SkipDigits(const wxChar
**ppc
)
736 while ( **ppc
>= _T('0') && **ppc
<= _T('9') )
737 CopyFmtChar(*(*ppc
)++);
740 // the translated format
743 // the original format
744 const wxChar
*m_fmtOrig
;
746 // the number of characters already copied
750 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
757 if ( CopyFmtChar(*format
++) == _T('%') )
760 while ( IsFlagChar(*format
) )
761 CopyFmtChar(*format
++);
763 // and possible width
764 if ( *format
== _T('*') )
765 CopyFmtChar(*format
++);
770 if ( *format
== _T('.') )
772 CopyFmtChar(*format
++);
773 if ( *format
== _T('*') )
774 CopyFmtChar(*format
++);
779 // next we can have a size modifier
795 // "ll" has a different meaning!
796 if ( format
[1] != _T('l') )
808 // and finally we should have the type
813 // %C and %hC -> %c and %lC -> %lc
815 CopyFmtChar(_T('l'));
817 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
822 // %c -> %lc but %hc stays %hc and %lc is still %lc
823 if ( size
== Default
)
824 InsertFmtChar(_T('l'));
828 // nothing special to do
829 if ( size
!= Default
)
830 CopyFmtChar(*(format
- 1));
831 CopyFmtChar(*format
++);
837 #else // !wxNEED_PRINTF_CONVERSION
838 // no conversion necessary
839 #define wxFormatConverter(x) (x)
840 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
843 // For testing the format converter
844 wxString
wxConvertFormat(const wxChar
*format
)
846 return wxString(wxFormatConverter(format
));
850 // ----------------------------------------------------------------------------
851 // wxPrintf(), wxScanf() and relatives
852 // ----------------------------------------------------------------------------
854 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
856 int wxScanf( const wxChar
*format
, ... )
859 va_start(argptr
, format
);
861 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
868 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... )
871 va_start(argptr
, format
);
873 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
880 int wxFscanf( FILE *stream
, const wxChar
*format
, ... )
883 va_start(argptr
, format
);
884 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
891 int wxPrintf( const wxChar
*format
, ... )
894 va_start(argptr
, format
);
896 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
904 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... )
907 va_start(argptr
, format
);
909 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
917 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... )
920 va_start(argptr
, format
);
922 // callers of wxSprintf() deserve what they get
923 //int ret = vswprintf( str, UINT_MAX, wxFormatConverter(format), argptr );
925 // ... true, but if we are going to implement it, they probably still
926 // deserve something a little better than absolutely guaranteed silent
927 // failure. For some (very mysterious) reason, this call fails under glibc
928 // 2.3.2 if str was allocated on the heap and maxsize is larger than this.
929 // Even more mysterious is that it does still succeed if str was allocated
930 // on the stack. This should still be plenty large enough for people who
931 // want to overflow a buffer. The bug was first noticed in unicode builds
932 // of tex2rtf, but I'm going to fix that to not use this unsafe function
933 // instead of wasting time diagnosing this further right now.
934 int ret
= vswprintf( str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
941 int wxFprintf( FILE *stream
, const wxChar
*format
, ... )
944 va_start( argptr
, format
);
946 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
953 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
955 return vswscanf( str
, wxFormatConverter(format
), argptr
);
958 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
960 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
963 int wxVprintf( const wxChar
*format
, va_list argptr
)
965 return vwprintf( wxFormatConverter(format
), argptr
);
969 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
971 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
973 #endif // wxVsnprintf
975 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
977 // same as for wxSprintf()
978 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
981 #endif // wxNEED_PRINTF_CONVERSION
985 // ----------------------------------------------------------------------------
986 // ctype.h stuff (currently unused)
987 // ----------------------------------------------------------------------------
989 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
990 inline WORD
wxMSW_ctype(wxChar ch
)
993 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
997 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
998 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
999 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
1000 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
1001 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
1002 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
1003 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
1004 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
1005 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
1006 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
1007 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
1008 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
1009 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
1014 WXDLLEXPORT
char *wxStrdupA(const char *s
)
1016 return strcpy((char *)malloc(strlen(s
) + 1), s
);
1023 WXDLLEXPORT
wchar_t * wxStrdupW(const wchar_t *pwz
)
1025 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
1026 wchar_t *ret
= (wchar_t *) malloc(size
);
1027 memcpy(ret
, pwz
, size
);
1034 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
1036 register wxChar c1
, c2
;
1038 c1
= wxTolower(*psz1
++);
1039 c2
= wxTolower(*psz2
++);
1040 } while ( c1
&& (c1
== c2
) );
1046 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1048 // initialize the variables just to suppress stupid gcc warning
1049 register wxChar c1
= 0, c2
= 0;
1050 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1052 if (c1
< c2
) return -1;
1053 if (c1
> c2
) return 1;
1060 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
1062 char *localeOld
= setlocale(category
, wxConvLocal
.cWX2MB(locale
));
1064 return wxWCharBuffer(wxConvLocal
.cMB2WC(localeOld
));
1068 // ----------------------------------------------------------------------------
1069 // string.h functions
1070 // ----------------------------------------------------------------------------
1072 #ifdef wxNEED_WX_STRING_H
1073 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
1076 while (*dest
) dest
++;
1077 while ((*dest
++ = *src
++));
1081 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
1083 // be careful here as the terminating NUL makes part of the string
1093 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
1095 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
1096 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1097 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1101 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
1104 while ((*dest
++ = *src
++));
1108 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1111 while (*dest
) dest
++;
1112 while (n
&& (*dest
++ = *src
++)) n
--;
1116 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1118 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1120 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1121 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1126 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1129 while (n
&& (*dest
++ = *src
++)) n
--;
1130 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1134 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1136 while (*s
&& !wxStrchr(accept
, *s
))
1139 return *s
? s
: NULL
;
1142 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1144 const wxChar
*ret
= NULL
;
1156 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1159 while (wxStrchr(accept
, *s
++)) len
++;
1163 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1165 wxCHECK_RET( needle
, NULL
, _T("NULL argument in wxStrstr") );
1167 // VZ: this is not exactly the most efficient string search algorithm...
1169 const size_t len
= wxStrlen(needle
);
1171 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1173 if ( !wxStrncmp(fnd
, needle
, len
) )
1182 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1184 const wxChar
*start
= nptr
;
1186 // FIXME: only correct for C locale
1187 while (wxIsspace(*nptr
)) nptr
++;
1188 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1189 while (wxIsdigit(*nptr
)) nptr
++;
1190 if (*nptr
== wxT('.')) {
1192 while (wxIsdigit(*nptr
)) nptr
++;
1194 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1196 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1197 while (wxIsdigit(*nptr
)) nptr
++;
1200 wxString
data(nptr
, nptr
-start
);
1201 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1202 char *rdat
= wxMBSTRINGCAST dat
;
1203 double ret
= strtod(dat
, &rdat
);
1205 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1210 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1212 const wxChar
*start
= nptr
;
1214 // FIXME: only correct for C locale
1215 while (wxIsspace(*nptr
)) nptr
++;
1216 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1217 if (((base
== 0) || (base
== 16)) &&
1218 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1222 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1223 else if (base
== 0) base
= 10;
1225 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1226 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1228 wxString
data(nptr
, nptr
-start
);
1229 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1230 char *rdat
= wxMBSTRINGCAST dat
;
1231 long int ret
= strtol(dat
, &rdat
, base
);
1233 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1237 #endif // wxNEED_WX_STRING_H
1239 #ifdef wxNEED_WX_STDIO_H
1240 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1242 char mode_buffer
[10];
1243 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1244 mode_buffer
[i
] = (char) mode
[i
];
1246 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1249 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1251 char mode_buffer
[10];
1252 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1253 mode_buffer
[i
] = (char) mode
[i
];
1255 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1258 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1260 return remove( wxConvFile
.cWX2MB(path
) );
1263 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1265 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1270 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1275 if (str
.ToDouble(& d
))
1280 return atof(wxConvLocal
.cWX2MB(psz
));
1285 #ifdef wxNEED_WX_STDLIB_H
1286 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1288 return atoi(wxConvLocal
.cWX2MB(psz
));
1291 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1293 return atol(wxConvLocal
.cWX2MB(psz
));
1296 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1298 static wxHashTable env
;
1300 // check if we already have stored the converted env var
1301 wxObject
*data
= env
.Get(name
);
1304 // nope, retrieve it,
1306 wxCharBuffer buffer
= wxConvLocal
.cWX2MB(name
);
1307 // printf( "buffer %s\n", (const char*) buffer );
1308 const char *val
= getenv( (const char *)buffer
);
1310 const char *val
= getenv( name
);
1313 if (!val
) return (wxChar
*)NULL
;
1314 // printf( "home %s\n", val );
1318 data
= (wxObject
*)new wxString(val
, wxConvLocal
);
1320 data
= (wxObject
*)new wxString(val
);
1324 env
.Put(name
, data
);
1326 // return converted env var
1327 return (wxChar
*)((wxString
*)data
)->c_str();
1330 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1332 return system(wxConvLocal
.cWX2MB(psz
));
1335 #endif // wxNEED_WX_STDLIB_H
1337 #ifdef wxNEED_WX_TIME_H
1338 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
1342 char *buf
= (char *)malloc(max
);
1343 size_t ret
= strftime(buf
, max
, wxConvLocal
.cWX2MB(fmt
), tm
);
1346 wxStrcpy(s
, wxConvLocal
.cMB2WX(buf
));
1357 #endif // wxNEED_WX_TIME_H
1360 WXDLLEXPORT wxChar
*wxCtime(const time_t *timep
)
1362 static wxChar buf
[128];
1364 wxStrncpy( buf
, wxConvertMB2WX( ctime( timep
) ), sizeof( buf
) );
1365 buf
[ sizeof( buf
) - 1 ] = _T('\0');
1371 #endif // wxUSE_WCHAR_T
1373 // ----------------------------------------------------------------------------
1374 // functions which we may need even if !wxUSE_WCHAR_T
1375 // ----------------------------------------------------------------------------
1379 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
1388 psz
+= wxStrspn(psz
, delim
);
1391 *save_ptr
= (wxChar
*)NULL
;
1392 return (wxChar
*)NULL
;
1396 psz
= wxStrpbrk(psz
, delim
);
1399 *save_ptr
= (wxChar
*)NULL
;
1404 *save_ptr
= psz
+ 1;
1412 // ----------------------------------------------------------------------------
1413 // missing C RTL functions
1414 // ----------------------------------------------------------------------------
1416 #if (defined(__MWERKS__) && !defined(__MACH__) && (__MSL__ < 0x00008000)) || \
1417 defined(__WXWINCE__)
1418 char *strdup(const char *s
)
1420 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1422 strcpy( dest
, s
) ;
1427 #if (defined(__MWERKS__) && !defined(__MACH__)) || (defined(__WXWINCE__) && _WIN32_WCE <= 211)
1429 int isascii( int c
)
1431 return ( c
>= 0 && c
< 128 );
1435 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1436 #if (_WIN32_WCE < 300)
1437 void *calloc( size_t num
, size_t size
)
1439 void** ptr
= (void **)malloc(num
* size
);
1440 memset( ptr
, 0, num
* size
);