]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wxcrt.cpp
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 wxWCharBuffer
wxSetlocale_(int category
, const wxChar
*locale
)
810 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
812 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
815 wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
817 wxWCharBuffer rv
= wxSetlocale_(category
, locale
);
819 wxUpdateLocaleIsUtf8();
822 #else // defined(wxSetlocale_)
823 wxChar
*wxSetlocale(int category
, const wxChar
*locale
)
825 wxChar
*rv
= wxSetlocale_(category
, locale
);
827 wxUpdateLocaleIsUtf8();
830 #endif // wxSetlocale_ defined or not
832 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
833 WXDLLEXPORT
size_t wxWcslen(const wchar_t *s
)
843 // ----------------------------------------------------------------------------
844 // string.h functions
845 // ----------------------------------------------------------------------------
847 #ifdef wxNEED_WX_STRING_H
849 // RN: These need to be c externed for the regex lib
854 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
857 while (*dest
) dest
++;
858 while ((*dest
++ = *src
++));
862 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
864 // be careful here as the terminating NUL makes part of the string
874 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
876 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
877 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
878 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
882 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
885 while ((*dest
++ = *src
++));
889 WXDLLEXPORT
size_t wxStrlen_(const wxChar
*s
)
899 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
902 while (*dest
) dest
++;
903 while (n
&& (*dest
++ = *src
++)) n
--;
907 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
909 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
911 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
912 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
917 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
920 while (n
&& (*dest
++ = *src
++)) n
--;
921 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
925 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
927 while (*s
&& !wxStrchr(accept
, *s
))
930 return *s
? s
: NULL
;
933 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
935 const wxChar
*ret
= NULL
;
947 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
950 while (wxStrchr(accept
, *s
++)) len
++;
954 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
956 wxASSERT_MSG( needle
!= NULL
, _T("NULL argument in wxStrstr") );
958 // VZ: this is not exactly the most efficient string search algorithm...
960 const size_t len
= wxStrlen(needle
);
962 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
964 if ( !wxStrncmp(fnd
, needle
, len
) )
977 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
979 const wxChar
*start
= nptr
;
981 // FIXME: only correct for C locale
982 while (wxIsspace(*nptr
)) nptr
++;
983 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
984 while (wxIsdigit(*nptr
)) nptr
++;
985 if (*nptr
== wxT('.')) {
987 while (wxIsdigit(*nptr
)) nptr
++;
989 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
991 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
992 while (wxIsdigit(*nptr
)) nptr
++;
995 wxString
data(nptr
, nptr
-start
);
996 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
997 char *rdat
= wxMBSTRINGCAST dat
;
998 double ret
= strtod(dat
, &rdat
);
1000 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1005 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1007 const wxChar
*start
= nptr
;
1009 // FIXME: only correct for C locale
1010 while (wxIsspace(*nptr
)) nptr
++;
1011 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1012 if (((base
== 0) || (base
== 16)) &&
1013 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1017 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1018 else if (base
== 0) base
= 10;
1020 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1021 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1023 wxString
data(start
, nptr
-start
);
1024 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1025 char *rdat
= wxMBSTRINGCAST dat
;
1026 long int ret
= strtol(dat
, &rdat
, base
);
1028 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1033 WXDLLEXPORT
unsigned long int wxStrtoul(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1035 return (unsigned long int) wxStrtol(nptr
, endptr
, base
);
1038 #endif // wxNEED_WX_STRING_H
1040 #ifdef wxNEED_WX_STDIO_H
1041 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1043 char mode_buffer
[10];
1044 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1045 mode_buffer
[i
] = (char) mode
[i
];
1047 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1050 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1052 char mode_buffer
[10];
1053 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1054 mode_buffer
[i
] = (char) mode
[i
];
1056 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1059 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1061 return remove( wxConvFile
.cWX2MB(path
) );
1064 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1066 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1071 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1076 if (str
.ToDouble(& d
))
1081 return atof(wxConvLibc
.cWX2MB(psz
));
1086 #ifdef wxNEED_WX_STDLIB_H
1087 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1089 return atoi(wxConvLibc
.cWX2MB(psz
));
1092 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1094 return atol(wxConvLibc
.cWX2MB(psz
));
1097 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1100 // NB: buffer returned by getenv() is allowed to be overwritten next
1101 // time getenv() is called, so it is OK to use static string
1102 // buffer to hold the data.
1103 static wxWCharBuffer
value((wxChar
*)NULL
);
1104 value
= wxConvLibc
.cMB2WX(getenv(wxConvLibc
.cWX2MB(name
)));
1105 return value
.data();
1107 return getenv(name
);
1111 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1113 return system(wxConvLibc
.cWX2MB(psz
));
1116 #endif // wxNEED_WX_STDLIB_H
1118 #ifdef wxNEED_WX_TIME_H
1120 wxStrftime(wxChar
*s
, size_t maxsize
, const wxChar
*fmt
, const struct tm
*tm
)
1125 wxCharBuffer
buf(maxsize
);
1127 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
1131 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
1135 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
1139 wxStrncpy(s
, wbuf
, maxsize
);
1142 #endif // wxNEED_WX_TIME_H
1145 WXDLLEXPORT wxChar
*wxCtime(const time_t *timep
)
1147 // normally the string is 26 chars but give one more in case some broken
1148 // DOS compiler decides to use "\r\n" instead of "\n" at the end
1149 static wxChar buf
[27];
1151 // ctime() is guaranteed to return a string containing only ASCII
1152 // characters, as its format is always the same for any locale
1153 wxStrncpy(buf
, wxString::FromAscii(ctime(timep
)), WXSIZEOF(buf
));
1154 buf
[WXSIZEOF(buf
) - 1] = _T('\0');
1160 #endif // wxUSE_WCHAR_T
1163 static wxULongLong_t
wxStrtoullBase(const wxChar
* nptr
, wxChar
** endptr
, int base
, wxChar
* sign
)
1165 wxULongLong_t sum
= 0;
1166 wxString
wxstr(nptr
);
1167 wxString::const_iterator i
= wxstr
.begin();
1168 wxString::const_iterator end
= wxstr
.end();
1171 while ( i
!= end
&& wxIsspace(*i
) ) i
++;
1173 // Starts with sign?
1178 if ( c
== wxT('+') || c
== wxT('-') )
1186 if ( i
!= end
&& *i
== wxT('0') )
1191 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
1199 *endptr
= (wxChar
*) nptr
;
1200 wxSET_ERRNO(EINVAL
);
1211 for ( ; i
!= end
; i
++ )
1216 if ( c
>= wxT('0') )
1218 if ( c
<= wxT('9') )
1221 n
= wxTolower(c
) - wxT('a') + 10;
1226 if ( n
>= (unsigned int)base
)
1227 // Invalid character (for this base)
1230 wxULongLong_t prevsum
= sum
;
1231 sum
= (sum
* base
) + n
;
1233 if ( sum
< prevsum
)
1235 wxSET_ERRNO(ERANGE
);
1242 const wxChar
& endref
= *i
;
1243 *endptr
= &(wxChar
&)endref
;
1249 wxULongLong_t
wxStrtoull(const wxChar
* nptr
, wxChar
** endptr
, int base
)
1252 wxULongLong_t uval
= wxStrtoullBase(nptr
, endptr
, base
, &sign
);
1254 if ( sign
== wxT('-') )
1256 wxSET_ERRNO(ERANGE
);
1263 wxLongLong_t
wxStrtoll(const wxChar
* nptr
, wxChar
** endptr
, int base
)
1266 wxULongLong_t uval
= wxStrtoullBase(nptr
, endptr
, base
, &sign
);
1267 wxLongLong_t val
= 0;
1269 if ( sign
== wxT('-') )
1271 if ( uval
<= wxULL(wxINT64_MAX
+1) )
1273 if ( uval
== wxULL(wxINT64_MAX
+1))
1274 val
= -((wxLongLong_t
)wxINT64_MAX
) - 1;
1276 val
= -((wxLongLong_t
)uval
);
1280 wxSET_ERRNO(ERANGE
);
1283 else if ( uval
<= wxINT64_MAX
)
1289 wxSET_ERRNO(ERANGE
);
1296 // ----------------------------------------------------------------------------
1297 // functions which we may need even if !wxUSE_WCHAR_T
1298 // ----------------------------------------------------------------------------
1302 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
1311 psz
+= wxStrspn(psz
, delim
);
1314 *save_ptr
= (wxChar
*)NULL
;
1315 return (wxChar
*)NULL
;
1319 psz
= wxStrpbrk(psz
, delim
);
1322 *save_ptr
= (wxChar
*)NULL
;
1327 *save_ptr
= psz
+ 1;
1335 // ----------------------------------------------------------------------------
1336 // missing C RTL functions
1337 // ----------------------------------------------------------------------------
1339 #ifdef wxNEED_STRDUP
1341 char *strdup(const char *s
)
1343 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1345 strcpy( dest
, s
) ;
1348 #endif // wxNEED_STRDUP
1350 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1352 void *calloc( size_t num
, size_t size
)
1354 void** ptr
= (void **)malloc(num
* size
);
1355 memset( ptr
, 0, num
* size
);
1359 #endif // __WXWINCE__ <= 211
1363 int wxRemove(const wxChar
*path
)
1365 return ::DeleteFile(path
) == 0;
1371 // ----------------------------------------------------------------------------
1373 // ----------------------------------------------------------------------------
1375 #if wxUSE_UNICODE_UTF8
1377 #if !wxUSE_UTF8_LOCALE_ONLY
1378 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1381 static bool wxIsLocaleUtf8()
1383 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1384 // because a) it may be unavailable in some builds and b) has slightly
1385 // different semantics (default locale instead of current)
1387 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1388 // GNU libc provides current character set this way (this conforms to
1390 const char *charset
= nl_langinfo(CODESET
);
1393 // "UTF-8" is used by modern glibc versions, but test other variants
1394 // as well, just in case:
1395 return strcmp(charset
, "UTF-8") == 0 ||
1396 strcmp(charset
, "utf-8") == 0 ||
1397 strcmp(charset
, "UTF8") == 0 ||
1398 strcmp(charset
, "utf8") == 0;
1400 else // nl_langinfo() failed
1403 // we don't know what charset libc is using, so assume the worst
1409 void wxUpdateLocaleIsUtf8()
1411 #if wxUSE_UTF8_LOCALE_ONLY
1412 if ( !wxIsLocaleUtf8() )
1414 wxLogFatalError(_T("This program requires UTF-8 locale to run."));
1416 #else // !wxUSE_UTF8_LOCALE_ONLY
1417 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1421 #endif // wxUSE_UTF8_LOCALE_ONLY