1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wxcrt.cpp
3 // Purpose: wxChar CRT wrappers implementation
5 // Modified by: Ron Lee, Francesco Montorsi
8 // Copyright: (c) wxWidgets copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
13 // headers, declarations, constants
14 // ===========================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/wxchar.h"
25 #define _ISOC9X_SOURCE 1 // to get vsscanf()
26 #define _BSD_SOURCE 1 // to still get strdup()
36 #include "wx/msw/wince/time.h"
40 #include "wx/string.h"
42 #include "wx/utils.h" // for wxMin and wxMax
46 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
55 // there is no errno.h under CE apparently
56 #define wxSET_ERRNO(value)
60 #define wxSET_ERRNO(value) errno = value
64 #if defined(__MWERKS__) && __MSL__ >= 0x6000
70 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
72 // assume that we have mbsrtowcs() too if we have wcsrtombs()
75 memset(&mbstate
, 0, sizeof(mbstate_t));
80 if (n
) *buf
= wxT('\0');
84 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
86 return wxMbstowcs(buf
, psz
, n
);
90 // note that we rely on common (and required by Unix98 but unfortunately not
91 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
92 // to just get the size of the needed buffer -- this is needed as otherwise
93 // we have no idea about how much space we need and if the CRT doesn't
94 // support it (the only currently known example being Metrowerks, see
95 // wx/wxchar.h) we don't use its mbstowcs() at all
97 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
99 return wxMbstowcs((wchar_t *) NULL
, psz
, 0);
103 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
105 #ifdef HAVE_WCSRTOMBS
107 memset(&mbstate
, 0, sizeof(mbstate_t));
112 // glibc2.1 chokes on null input
116 #ifdef HAVE_WCSRTOMBS
117 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
119 return wxWcstombs(buf
, pwz
, n
);
123 #ifdef HAVE_WCSRTOMBS
124 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
126 return wxWcstombs((char *) NULL
, pwz
, 0);
129 #endif // wxUSE_WCHAR_T
131 bool WXDLLEXPORT
wxOKlibc()
133 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
134 // glibc 2.0 uses UTF-8 even when it shouldn't
136 if ((MB_CUR_MAX
== 2) &&
137 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
139 // this is UTF-8 allright, check whether that's what we want
140 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
141 if ((strlen(cur_locale
) < 4) ||
142 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
143 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
144 // nope, don't use libc conversion
152 // ============================================================================
153 // printf() functions business
154 // ============================================================================
156 // special test mode: define all functions below even if we don't really need
157 // them to be able to test them
168 #define wxNEED_WPRINTF
170 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
173 #if !defined(wxSnprintf_)
174 int WXDLLEXPORT
wxDoSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
177 va_start(argptr
, format
);
179 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
185 #endif // wxSnprintf_
188 /* Digital Mars adds count to _stprintf (C99) so convert */
190 int wxDoSprintf (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
194 va_start( arglist
, format
);
195 int iLen
= swprintf ( s
, -1, format
, arglist
);
200 #endif // wxUSE_UNICODE
204 // ----------------------------------------------------------------------------
205 // implement the standard IO functions for wide char if libc doesn't have them
206 // ----------------------------------------------------------------------------
209 int wxFputs(const wchar_t *ws
, FILE *stream
)
211 wxCharBuffer
buf(wxConvLibc
.cWC2MB(ws
));
215 // counting the number of wide characters written isn't worth the trouble,
216 // simply distinguish between ok and error
217 return fputs(buf
, stream
) == -1 ? -1 : 0;
219 #endif // wxNEED_FPUTS
222 int wxPuts(const wxChar
*ws
)
224 int rc
= wxFputs(ws
, stdout
);
227 if ( wxFputs(L
"\n", stdout
) == -1 )
235 #endif // wxNEED_PUTS
238 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
240 wchar_t ws
[2] = { wc
, L
'\0' };
242 return wxFputs(ws
, stream
);
244 #endif // wxNEED_PUTC
246 // NB: we only implement va_list functions here, the ones taking ... are
247 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
248 // the definitions there to avoid duplicating them here
249 #ifdef wxNEED_WPRINTF
251 // TODO: implement the scanf() functions
252 int vwscanf(const wxChar
*format
, va_list argptr
)
254 wxFAIL_MSG( _T("TODO") );
259 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
261 // The best we can do without proper Unicode support in glibc is to
262 // convert the strings into MB representation and run ANSI version
263 // of the function. This doesn't work with %c and %s because of difference
264 // in size of char and wchar_t, though.
266 wxCHECK_MSG( wxStrstr(format
, _T("%s")) == NULL
, -1,
267 _T("incomplete vswscanf implementation doesn't allow %s") );
268 wxCHECK_MSG( wxStrstr(format
, _T("%c")) == NULL
, -1,
269 _T("incomplete vswscanf implementation doesn't allow %c") );
272 wxVaCopy(argcopy
, argptr
);
273 return vsscanf(wxConvLibc
.cWX2MB(ws
), wxConvLibc
.cWX2MB(format
), argcopy
);
276 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
278 wxFAIL_MSG( _T("TODO") );
283 #define vswprintf wxVsnprintf_
285 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
288 int rc
= s
.PrintfV(format
, argptr
);
292 // we can't do much better without Unicode support in libc...
293 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
300 int vwprintf(const wxChar
*format
, va_list argptr
)
302 return wxVfprintf(stdout
, format
, argptr
);
305 #endif // wxNEED_WPRINTF
307 #ifdef wxNEED_PRINTF_CONVERSION
309 // ----------------------------------------------------------------------------
310 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
311 // ----------------------------------------------------------------------------
314 Here are the gory details. We want to follow the Windows/MS conventions,
319 format specifier results in
320 -----------------------------------
326 format specifier results in
327 -----------------------------------
332 while on POSIX systems we have %C identical to %lc and %c always means char
333 (in any mode) while %lc always means wchar_t,
335 So to use native functions in order to get our semantics we must do the
336 following translations in Unicode mode (nothing to do in ANSI mode):
338 wxWidgets specifier POSIX specifier
339 ----------------------------------------
345 And, of course, the same should be done for %s as well.
348 class wxFormatConverter
351 wxFormatConverter(const wxChar
*format
);
353 // notice that we only translated the string if m_fmtOrig == NULL (as set
354 // by CopyAllBefore()), otherwise we should simply use the original format
355 operator const wxChar
*() const
356 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
359 // copy another character to the translated format: this function does the
360 // copy if we are translating but doesn't do anything at all if we don't,
361 // so we don't create the translated format string at all unless we really
362 // need to (i.e. InsertFmtChar() is called)
363 wxChar
CopyFmtChar(wxChar ch
)
367 // we're translating, do copy
372 // simply increase the count which should be copied by
373 // CopyAllBefore() later if needed
380 // insert an extra character
381 void InsertFmtChar(wxChar ch
)
385 // so far we haven't translated anything yet
394 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
396 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
398 // we won't need it any longer
402 static bool IsFlagChar(wxChar ch
)
404 return ch
== _T('-') || ch
== _T('+') ||
405 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
408 void SkipDigits(const wxChar
**ptpc
)
410 while ( **ptpc
>= _T('0') && **ptpc
<= _T('9') )
411 CopyFmtChar(*(*ptpc
)++);
414 // the translated format
417 // the original format
418 const wxChar
*m_fmtOrig
;
420 // the number of characters already copied
424 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
431 if ( CopyFmtChar(*format
++) == _T('%') )
434 while ( IsFlagChar(*format
) )
435 CopyFmtChar(*format
++);
437 // and possible width
438 if ( *format
== _T('*') )
439 CopyFmtChar(*format
++);
444 if ( *format
== _T('.') )
446 CopyFmtChar(*format
++);
447 if ( *format
== _T('*') )
448 CopyFmtChar(*format
++);
453 // next we can have a size modifier
469 // "ll" has a different meaning!
470 if ( format
[1] != _T('l') )
482 // and finally we should have the type
487 // %C and %hC -> %c and %lC -> %lc
489 CopyFmtChar(_T('l'));
491 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
496 // %c -> %lc but %hc stays %hc and %lc is still %lc
497 if ( size
== Default
)
498 InsertFmtChar(_T('l'));
502 // nothing special to do
503 if ( size
!= Default
)
504 CopyFmtChar(*(format
- 1));
505 CopyFmtChar(*format
++);
511 #else // !wxNEED_PRINTF_CONVERSION
512 // no conversion necessary
513 #define wxFormatConverter(x) (x)
514 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
517 // For testing the format converter
518 wxString
wxConvertFormat(const wxChar
*format
)
520 return wxString(wxFormatConverter(format
));
524 // ----------------------------------------------------------------------------
525 // wxPrintf(), wxScanf() and relatives
526 // ----------------------------------------------------------------------------
528 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
530 int wxDoScanf( const wxChar
*format
, ... )
533 va_start(argptr
, format
);
535 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
542 int wxDoSscanf( const wxChar
*str
, const wxChar
*format
, ... )
545 va_start(argptr
, format
);
547 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
554 int wxDoFscanf( FILE *stream
, const wxChar
*format
, ... )
557 va_start(argptr
, format
);
558 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
565 int wxDoPrintf( const wxChar
*format
, ... )
568 va_start(argptr
, format
);
570 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
578 int wxDoSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... )
581 va_start(argptr
, format
);
583 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
585 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
586 // doesn't nul terminate on truncation.
595 int wxDoSprintf( wxChar
*str
, const wxChar
*format
, ... )
598 va_start(argptr
, format
);
600 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
601 // it's safe to implement this one in terms of it
602 wxString
s(wxString::FormatV(format
, argptr
));
610 int wxDoFprintf( FILE *stream
, const wxChar
*format
, ... )
613 va_start( argptr
, format
);
615 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
622 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
624 return vswscanf( str
, wxFormatConverter(format
), argptr
);
627 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
629 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
632 int wxVprintf( const wxChar
*format
, va_list argptr
)
634 return vwprintf( wxFormatConverter(format
), argptr
);
638 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
640 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
642 #endif // wxVsnprintf
644 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
646 // same as for wxSprintf()
647 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
650 #endif // wxNEED_PRINTF_CONVERSION
654 // ----------------------------------------------------------------------------
655 // ctype.h stuff (currently unused)
656 // ----------------------------------------------------------------------------
658 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
659 inline WORD
wxMSW_ctype(wxChar ch
)
662 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
666 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
667 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
668 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
669 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
670 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
671 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
672 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
673 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
674 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
675 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
676 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
677 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
678 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
681 #ifdef wxNEED_WX_MBSTOWCS
683 WXDLLEXPORT
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
693 const char* origin
= in
;
695 while (outlen
-- && *in
)
697 *out
++ = (wchar_t) *in
++;
705 WXDLLEXPORT
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
715 const wchar_t* origin
= in
;
717 while (outlen
-- && *in
)
719 *out
++ = (char) *in
++;
727 #endif // wxNEED_WX_MBSTOWCS
729 #if defined(wxNEED_WX_CTYPE_H)
731 #include <CoreFoundation/CoreFoundation.h>
733 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
734 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
735 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
736 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
737 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
738 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
739 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
740 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
741 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
742 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
744 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalnumset
, ch
); }
745 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalphaset
, ch
); }
746 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
747 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfdigitset
, ch
); }
748 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
) && ch
!= ' '; }
749 WXDLLEXPORT
int wxIslower(wxChar ch
) { return CFCharacterSetIsCharacterMember(cflowerset
, ch
); }
750 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
751 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfpunctset
, ch
); }
752 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfspaceset
, ch
); }
753 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfupperset
, ch
); }
754 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxIsdigit(ch
) || (ch
>='a' && ch
<='f') || (ch
>='A' && ch
<='F'); }
755 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)tolower((char)(ch
)); }
756 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)toupper((char)(ch
)); }
758 #endif // wxNEED_WX_CTYPE_H
762 WXDLLEXPORT
char *wxStrdupA(const char *s
)
764 return strcpy((char *)malloc(strlen(s
) + 1), s
);
771 WXDLLEXPORT
wchar_t * wxStrdupW(const wchar_t *pwz
)
773 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
774 wchar_t *ret
= (wchar_t *) malloc(size
);
775 memcpy(ret
, pwz
, size
);
782 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
784 register wxChar c1
, c2
;
786 c1
= wxTolower(*psz1
++);
787 c2
= wxTolower(*psz2
++);
788 } while ( c1
&& (c1
== c2
) );
794 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
796 // initialize the variables just to suppress stupid gcc warning
797 register wxChar c1
= 0, c2
= 0;
798 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
800 if (c1
< c2
) return -1;
801 if (c1
> c2
) return 1;
808 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
810 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
812 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
816 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
817 WXDLLEXPORT
size_t wxWcslen(const wchar_t *s
)
827 // ----------------------------------------------------------------------------
828 // string.h functions
829 // ----------------------------------------------------------------------------
831 #ifdef wxNEED_WX_STRING_H
833 // RN: These need to be c externed for the regex lib
838 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
841 while (*dest
) dest
++;
842 while ((*dest
++ = *src
++));
846 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
848 // be careful here as the terminating NUL makes part of the string
858 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
860 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
861 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
862 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
866 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
869 while ((*dest
++ = *src
++));
873 WXDLLEXPORT
size_t wxStrlen_(const wxChar
*s
)
883 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
886 while (*dest
) dest
++;
887 while (n
&& (*dest
++ = *src
++)) n
--;
891 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
893 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
895 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
896 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
901 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
904 while (n
&& (*dest
++ = *src
++)) n
--;
905 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
909 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
911 while (*s
&& !wxStrchr(accept
, *s
))
914 return *s
? s
: NULL
;
917 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
919 const wxChar
*ret
= NULL
;
931 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
934 while (wxStrchr(accept
, *s
++)) len
++;
938 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
940 wxASSERT_MSG( needle
!= NULL
, _T("NULL argument in wxStrstr") );
942 // VZ: this is not exactly the most efficient string search algorithm...
944 const size_t len
= wxStrlen(needle
);
946 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
948 if ( !wxStrncmp(fnd
, needle
, len
) )
961 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
963 const wxChar
*start
= nptr
;
965 // FIXME: only correct for C locale
966 while (wxIsspace(*nptr
)) nptr
++;
967 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
968 while (wxIsdigit(*nptr
)) nptr
++;
969 if (*nptr
== wxT('.')) {
971 while (wxIsdigit(*nptr
)) nptr
++;
973 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
975 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
976 while (wxIsdigit(*nptr
)) nptr
++;
979 wxString
data(nptr
, nptr
-start
);
980 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
981 char *rdat
= wxMBSTRINGCAST dat
;
982 double ret
= strtod(dat
, &rdat
);
984 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
989 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
991 const wxChar
*start
= nptr
;
993 // FIXME: only correct for C locale
994 while (wxIsspace(*nptr
)) nptr
++;
995 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
996 if (((base
== 0) || (base
== 16)) &&
997 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1001 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1002 else if (base
== 0) base
= 10;
1004 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1005 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1007 wxString
data(start
, nptr
-start
);
1008 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1009 char *rdat
= wxMBSTRINGCAST dat
;
1010 long int ret
= strtol(dat
, &rdat
, base
);
1012 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1017 WXDLLEXPORT
unsigned long int wxStrtoul(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1019 return (unsigned long int) wxStrtol(nptr
, endptr
, base
);
1022 #endif // wxNEED_WX_STRING_H
1024 #ifdef wxNEED_WX_STDIO_H
1025 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1027 char mode_buffer
[10];
1028 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1029 mode_buffer
[i
] = (char) mode
[i
];
1031 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1034 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1036 char mode_buffer
[10];
1037 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1038 mode_buffer
[i
] = (char) mode
[i
];
1040 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1043 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1045 return remove( wxConvFile
.cWX2MB(path
) );
1048 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1050 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1055 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1060 if (str
.ToDouble(& d
))
1065 return atof(wxConvLibc
.cWX2MB(psz
));
1070 #ifdef wxNEED_WX_STDLIB_H
1071 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1073 return atoi(wxConvLibc
.cWX2MB(psz
));
1076 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1078 return atol(wxConvLibc
.cWX2MB(psz
));
1081 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1084 // NB: buffer returned by getenv() is allowed to be overwritten next
1085 // time getenv() is called, so it is OK to use static string
1086 // buffer to hold the data.
1087 static wxWCharBuffer
value((wxChar
*)NULL
);
1088 value
= wxConvLibc
.cMB2WX(getenv(wxConvLibc
.cWX2MB(name
)));
1089 return value
.data();
1091 return getenv(name
);
1095 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1097 return system(wxConvLibc
.cWX2MB(psz
));
1100 #endif // wxNEED_WX_STDLIB_H
1102 #ifdef wxNEED_WX_TIME_H
1104 wxStrftime(wxChar
*s
, size_t maxsize
, const wxChar
*fmt
, const struct tm
*tm
)
1109 wxCharBuffer
buf(maxsize
);
1111 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
1115 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
1119 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
1123 wxStrncpy(s
, wbuf
, maxsize
);
1126 #endif // wxNEED_WX_TIME_H
1129 WXDLLEXPORT wxChar
*wxCtime(const time_t *timep
)
1131 // normally the string is 26 chars but give one more in case some broken
1132 // DOS compiler decides to use "\r\n" instead of "\n" at the end
1133 static wxChar buf
[27];
1135 // ctime() is guaranteed to return a string containing only ASCII
1136 // characters, as its format is always the same for any locale
1137 wxStrncpy(buf
, wxString::FromAscii(ctime(timep
)), WXSIZEOF(buf
));
1138 buf
[WXSIZEOF(buf
) - 1] = _T('\0');
1144 #endif // wxUSE_WCHAR_T
1147 static wxULongLong_t
wxStrtoullBase(const wxChar
* nptr
, wxChar
** endptr
, int base
, wxChar
* sign
)
1149 wxULongLong_t sum
= 0;
1150 wxString
wxstr(nptr
);
1151 wxString::const_iterator i
= wxstr
.begin();
1152 wxString::const_iterator end
= wxstr
.end();
1155 while ( i
!= end
&& wxIsspace(*i
) ) i
++;
1157 // Starts with sign?
1162 if ( c
== wxT('+') || c
== wxT('-') )
1170 if ( i
!= end
&& *i
== wxT('0') )
1175 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
1183 *endptr
= (wxChar
*) nptr
;
1184 wxSET_ERRNO(EINVAL
);
1195 for ( ; i
!= end
; i
++ )
1200 if ( c
>= wxT('0') )
1202 if ( c
<= wxT('9') )
1205 n
= wxTolower(c
) - wxT('a') + 10;
1210 if ( n
>= (unsigned int)base
)
1211 // Invalid character (for this base)
1214 wxULongLong_t prevsum
= sum
;
1215 sum
= (sum
* base
) + n
;
1217 if ( sum
< prevsum
)
1219 wxSET_ERRNO(ERANGE
);
1226 const wxChar
& endref
= *i
;
1227 *endptr
= &(wxChar
&)endref
;
1233 wxULongLong_t
wxStrtoull(const wxChar
* nptr
, wxChar
** endptr
, int base
)
1236 wxULongLong_t uval
= wxStrtoullBase(nptr
, endptr
, base
, &sign
);
1238 if ( sign
== wxT('-') )
1240 wxSET_ERRNO(ERANGE
);
1247 wxLongLong_t
wxStrtoll(const wxChar
* nptr
, wxChar
** endptr
, int base
)
1250 wxULongLong_t uval
= wxStrtoullBase(nptr
, endptr
, base
, &sign
);
1251 wxLongLong_t val
= 0;
1253 if ( sign
== wxT('-') )
1255 if ( uval
<= wxULL(wxINT64_MAX
+1) )
1257 if ( uval
== wxULL(wxINT64_MAX
+1))
1258 val
= -((wxLongLong_t
)wxINT64_MAX
) - 1;
1260 val
= -((wxLongLong_t
)uval
);
1264 wxSET_ERRNO(ERANGE
);
1267 else if ( uval
<= wxINT64_MAX
)
1273 wxSET_ERRNO(ERANGE
);
1280 // ----------------------------------------------------------------------------
1281 // functions which we may need even if !wxUSE_WCHAR_T
1282 // ----------------------------------------------------------------------------
1286 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
1295 psz
+= wxStrspn(psz
, delim
);
1298 *save_ptr
= (wxChar
*)NULL
;
1299 return (wxChar
*)NULL
;
1303 psz
= wxStrpbrk(psz
, delim
);
1306 *save_ptr
= (wxChar
*)NULL
;
1311 *save_ptr
= psz
+ 1;
1319 // ----------------------------------------------------------------------------
1320 // missing C RTL functions
1321 // ----------------------------------------------------------------------------
1323 #ifdef wxNEED_STRDUP
1325 char *strdup(const char *s
)
1327 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1329 strcpy( dest
, s
) ;
1332 #endif // wxNEED_STRDUP
1334 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1336 void *calloc( size_t num
, size_t size
)
1338 void** ptr
= (void **)malloc(num
* size
);
1339 memset( ptr
, 0, num
* size
);
1343 #endif // __WXWINCE__ <= 211
1347 int wxRemove(const wxChar
*path
)
1349 return ::DeleteFile(path
) == 0;