]>
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
167 #define wxNEED_WPRINTF
169 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
173 /* Digital Mars adds count to _stprintf (C99) so convert */
175 int wxSprintf (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
179 va_start( arglist
, format
);
180 int iLen
= swprintf ( s
, -1, format
, arglist
);
185 #endif // wxUSE_UNICODE
189 // ----------------------------------------------------------------------------
190 // implement the standard IO functions for wide char if libc doesn't have them
191 // ----------------------------------------------------------------------------
194 int wxFputs(const wchar_t *ws
, FILE *stream
)
196 wxCharBuffer
buf(wxConvLibc
.cWC2MB(ws
));
200 // counting the number of wide characters written isn't worth the trouble,
201 // simply distinguish between ok and error
202 return fputs(buf
, stream
) == -1 ? -1 : 0;
204 #endif // wxNEED_FPUTS
207 int wxPuts(const wxChar
*ws
)
209 int rc
= wxFputs(ws
, stdout
);
212 if ( wxFputs(L
"\n", stdout
) == -1 )
220 #endif // wxNEED_PUTS
223 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
225 wchar_t ws
[2] = { wc
, L
'\0' };
227 return wxFputs(ws
, stream
);
229 #endif // wxNEED_PUTC
231 // NB: we only implement va_list functions here, the ones taking ... are
232 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
233 // the definitions there to avoid duplicating them here
234 #ifdef wxNEED_WPRINTF
236 // TODO: implement the scanf() functions
237 int vwscanf(const wxChar
*format
, va_list argptr
)
239 wxFAIL_MSG( _T("TODO") );
244 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
246 // The best we can do without proper Unicode support in glibc is to
247 // convert the strings into MB representation and run ANSI version
248 // of the function. This doesn't work with %c and %s because of difference
249 // in size of char and wchar_t, though.
251 wxCHECK_MSG( wxStrstr(format
, _T("%s")) == NULL
, -1,
252 _T("incomplete vswscanf implementation doesn't allow %s") );
253 wxCHECK_MSG( wxStrstr(format
, _T("%c")) == NULL
, -1,
254 _T("incomplete vswscanf implementation doesn't allow %c") );
257 wxVaCopy(argcopy
, argptr
);
258 return vsscanf(wxConvLibc
.cWX2MB(ws
), wxConvLibc
.cWX2MB(format
), argcopy
);
261 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
263 wxFAIL_MSG( _T("TODO") );
268 #define vswprintf wxVsnprintf_
270 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
273 int rc
= s
.PrintfV(format
, argptr
);
277 // we can't do much better without Unicode support in libc...
278 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
285 int vwprintf(const wxChar
*format
, va_list argptr
)
287 return wxVfprintf(stdout
, format
, argptr
);
290 #endif // wxNEED_WPRINTF
292 #ifdef wxNEED_PRINTF_CONVERSION
294 // ----------------------------------------------------------------------------
295 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
296 // ----------------------------------------------------------------------------
299 Here are the gory details. We want to follow the Windows/MS conventions,
304 format specifier results in
305 -----------------------------------
311 format specifier results in
312 -----------------------------------
317 while on POSIX systems we have %C identical to %lc and %c always means char
318 (in any mode) while %lc always means wchar_t,
320 So to use native functions in order to get our semantics we must do the
321 following translations in Unicode mode (nothing to do in ANSI mode):
323 wxWidgets specifier POSIX specifier
324 ----------------------------------------
330 And, of course, the same should be done for %s as well.
333 class wxFormatConverter
336 wxFormatConverter(const wxChar
*format
);
338 // notice that we only translated the string if m_fmtOrig == NULL (as set
339 // by CopyAllBefore()), otherwise we should simply use the original format
340 operator const wxChar
*() const
341 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
344 // copy another character to the translated format: this function does the
345 // copy if we are translating but doesn't do anything at all if we don't,
346 // so we don't create the translated format string at all unless we really
347 // need to (i.e. InsertFmtChar() is called)
348 wxChar
CopyFmtChar(wxChar ch
)
352 // we're translating, do copy
357 // simply increase the count which should be copied by
358 // CopyAllBefore() later if needed
365 // insert an extra character
366 void InsertFmtChar(wxChar ch
)
370 // so far we haven't translated anything yet
379 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
381 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
383 // we won't need it any longer
387 static bool IsFlagChar(wxChar ch
)
389 return ch
== _T('-') || ch
== _T('+') ||
390 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
393 void SkipDigits(const wxChar
**ptpc
)
395 while ( **ptpc
>= _T('0') && **ptpc
<= _T('9') )
396 CopyFmtChar(*(*ptpc
)++);
399 // the translated format
402 // the original format
403 const wxChar
*m_fmtOrig
;
405 // the number of characters already copied
409 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
416 if ( CopyFmtChar(*format
++) == _T('%') )
419 while ( IsFlagChar(*format
) )
420 CopyFmtChar(*format
++);
422 // and possible width
423 if ( *format
== _T('*') )
424 CopyFmtChar(*format
++);
429 if ( *format
== _T('.') )
431 CopyFmtChar(*format
++);
432 if ( *format
== _T('*') )
433 CopyFmtChar(*format
++);
438 // next we can have a size modifier
454 // "ll" has a different meaning!
455 if ( format
[1] != _T('l') )
467 // and finally we should have the type
472 // %C and %hC -> %c and %lC -> %lc
474 CopyFmtChar(_T('l'));
476 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
481 // %c -> %lc but %hc stays %hc and %lc is still %lc
482 if ( size
== Default
)
483 InsertFmtChar(_T('l'));
487 // nothing special to do
488 if ( size
!= Default
)
489 CopyFmtChar(*(format
- 1));
490 CopyFmtChar(*format
++);
496 #else // !wxNEED_PRINTF_CONVERSION
497 // no conversion necessary
498 #define wxFormatConverter(x) (x)
499 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
502 // For testing the format converter
503 wxString
wxConvertFormat(const wxChar
*format
)
505 return wxString(wxFormatConverter(format
));
509 // ----------------------------------------------------------------------------
510 // wxPrintf(), wxScanf() and relatives
511 // ----------------------------------------------------------------------------
513 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
515 int wxCRT_Scanf( const wxChar
*format
, ... )
518 va_start(argptr
, format
);
520 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
527 int wxCRT_Sscanf( const wxChar
*str
, const wxChar
*format
, ... )
530 va_start(argptr
, format
);
532 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
539 int wxCRT_Fscanf( FILE *stream
, const wxChar
*format
, ... )
542 va_start(argptr
, format
);
543 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
550 int wxCRT_Printf( const wxChar
*format
, ... )
553 va_start(argptr
, format
);
555 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
562 int wxCRT_Fprintf( FILE *stream
, const wxChar
*format
, ... )
565 va_start( argptr
, format
);
567 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
574 int wxCRT_Vsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
576 return vswscanf( str
, wxFormatConverter(format
), argptr
);
579 int wxCRT_Vfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
581 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
584 int wxCRT_Vprintf( const wxChar
*format
, va_list argptr
)
586 return vwprintf( wxFormatConverter(format
), argptr
);
589 #ifndef wxCRT_Vsnprintf
590 int wxCRT_Vsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
592 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
594 #endif // wxCRT_Vsnprintf
596 int wxCRT_Vsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
598 // same as for wxSprintf()
599 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
602 #endif // wxNEED_PRINTF_CONVERSION
605 // ----------------------------------------------------------------------------
606 // wrappers to printf and scanf function families
607 // ----------------------------------------------------------------------------
609 #if !wxUSE_UTF8_LOCALE_ONLY
610 int wxDoSprintfWchar(char *str
, const wxChar
*format
, ...)
613 va_start(argptr
, format
);
615 int rv
= wxVsprintf(str
, format
, argptr
);
620 #endif // !wxUSE_UTF8_LOCALE_ONLY
622 #if wxUSE_UNICODE_UTF8
623 int wxDoSprintfUtf8(char *str
, const char *format
, ...)
626 va_start(argptr
, format
);
628 int rv
= wxVsprintf(str
, format
, argptr
);
633 #endif // wxUSE_UNICODE_UTF8
637 #if !wxUSE_UTF8_LOCALE_ONLY
638 int wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...)
641 va_start(argptr
, format
);
643 int rv
= wxVsprintf(str
, format
, argptr
);
648 #endif // !wxUSE_UTF8_LOCALE_ONLY
650 #if wxUSE_UNICODE_UTF8
651 int wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...)
654 va_start(argptr
, format
);
656 int rv
= wxVsprintf(str
, format
, argptr
);
661 #endif // wxUSE_UNICODE_UTF8
663 #endif // wxUSE_UNICODE
665 #if !wxUSE_UTF8_LOCALE_ONLY
666 int wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...)
669 va_start(argptr
, format
);
671 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
676 #endif // !wxUSE_UTF8_LOCALE_ONLY
678 #if wxUSE_UNICODE_UTF8
679 int wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...)
682 va_start(argptr
, format
);
684 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
689 #endif // wxUSE_UNICODE_UTF8
693 #if !wxUSE_UTF8_LOCALE_ONLY
694 int wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...)
697 va_start(argptr
, format
);
699 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
704 #endif // !wxUSE_UTF8_LOCALE_ONLY
706 #if wxUSE_UNICODE_UTF8
707 int wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...)
710 va_start(argptr
, format
);
712 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
717 #endif // wxUSE_UNICODE_UTF8
719 #endif // wxUSE_UNICODE
722 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
723 #define vsnprintf wx_fixed_vsnprintf
728 #if !wxUSE_UTF8_LOCALE_ONLY
729 static int ConvertStringToBuf(const wxString
& s
, char *out
, size_t outsize
)
731 const wxWX2WCbuf buf
= s
.wc_str();
733 size_t len
= wxConvLibc
.FromWChar(out
, outsize
, buf
);
734 if ( len
!= wxCONV_FAILED
)
737 return wxConvLibc
.FromWChar(NULL
, 0, buf
);
739 #endif // !wxUSE_UTF8_LOCALE_ONLY
741 #if wxUSE_UNICODE_UTF8
742 static int ConvertStringToBuf(const wxString
& s
, wchar_t *out
, size_t outsize
)
744 const wxWX2WCbuf
buf(s
.wc_str());
745 size_t len
= wxWcslen(buf
);
747 memcpy(out
, buf
, (len
+1) * sizeof(wchar_t));
748 // else: not enough space
751 #endif // wxUSE_UNICODE_UTF8
754 static size_t PrintfViaString(T
*out
, size_t outsize
,
755 const wxString
& format
, va_list argptr
)
758 wxVaCopy(argcopy
, argptr
);
761 s
.PrintfV(format
, argcopy
);
763 return ConvertStringToBuf(s
, out
, outsize
);
765 #endif // wxUSE_UNICODE
767 int wxVsprintf(char *str
, const wxString
& format
, va_list argptr
)
770 wxVaCopy(argcopy
, argptr
);
772 #if wxUSE_UTF8_LOCALE_ONLY
773 return vsprintf(str
, format
.wx_str(), argcopy
);
775 #if wxUSE_UNICODE_UTF8
776 if ( wxLocaleIsUtf8
)
777 return vsprintf(str
, format
.wx_str(), argcopy
);
781 return PrintfViaString(str
, wxNO_LEN
, format
, argcopy
);
783 return wxCRT_Vsprintf(str
, format
, argcopy
);
789 int wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
)
792 wxVaCopy(argcopy
, argptr
);
794 #if wxUSE_UNICODE_WCHAR
795 return wxCRT_Vsprintf(str
, format
, argcopy
);
796 #else // wxUSE_UNICODE_UTF8
797 #if !wxUSE_UTF8_LOCALE_ONLY
798 if ( !wxLocaleIsUtf8
)
799 return wxCRT_Vsprintf(str
, format
, argcopy
);
802 return PrintfViaString(str
, wxNO_LEN
, format
, argcopy
);
803 #endif // wxUSE_UNICODE_UTF8
805 #endif // wxUSE_UNICODE
807 int wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
)
811 wxVaCopy(argcopy
, argptr
);
813 #if wxUSE_UTF8_LOCALE_ONLY
814 rv
= vsnprintf(str
, size
, format
.wx_str(), argcopy
);
816 #if wxUSE_UNICODE_UTF8
817 if ( wxLocaleIsUtf8
)
818 rv
= vsnprintf(str
, size
, format
.wx_str(), argcopy
);
823 // NB: if this code is called, then wxString::PrintV() would use the
824 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
826 rv
= PrintfViaString(str
, size
, format
, argcopy
);
829 rv
= wxCRT_Vsnprintf(str
, size
, format
, argcopy
);
833 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
834 // doesn't nul terminate on truncation.
841 int wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
)
845 wxVaCopy(argcopy
, argptr
);
847 #if wxUSE_UNICODE_WCHAR
848 rv
= wxCRT_Vsnprintf(str
, size
, format
, argcopy
);
849 #else // wxUSE_UNICODE_UTF8
850 #if !wxUSE_UTF8_LOCALE_ONLY
851 if ( !wxLocaleIsUtf8
)
852 rv
= wxCRT_Vsnprintf(str
, size
, format
, argcopy
);
856 // NB: if this code is called, then wxString::PrintV() would use the
857 // char* version of wxVsnprintf(), so it's safe to use PrintV()
859 rv
= PrintfViaString(str
, size
, format
, argcopy
);
861 #endif // wxUSE_UNICODE_UTF8
863 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
864 // doesn't nul terminate on truncation.
869 #endif // wxUSE_UNICODE
873 // ----------------------------------------------------------------------------
874 // ctype.h stuff (currently unused)
875 // ----------------------------------------------------------------------------
877 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
878 inline WORD
wxMSW_ctype(wxChar ch
)
881 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
885 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
886 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
887 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
888 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
889 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
890 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
891 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
892 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
893 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
894 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
895 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
896 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
897 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
900 #ifdef wxNEED_WX_MBSTOWCS
902 WXDLLEXPORT
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
912 const char* origin
= in
;
914 while (outlen
-- && *in
)
916 *out
++ = (wchar_t) *in
++;
924 WXDLLEXPORT
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
934 const wchar_t* origin
= in
;
936 while (outlen
-- && *in
)
938 *out
++ = (char) *in
++;
946 #endif // wxNEED_WX_MBSTOWCS
948 #if defined(wxNEED_WX_CTYPE_H)
950 #include <CoreFoundation/CoreFoundation.h>
952 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
953 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
954 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
955 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
956 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
957 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
958 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
959 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
960 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
961 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
963 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalnumset
, ch
); }
964 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalphaset
, ch
); }
965 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
966 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfdigitset
, ch
); }
967 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
) && ch
!= ' '; }
968 WXDLLEXPORT
int wxIslower(wxChar ch
) { return CFCharacterSetIsCharacterMember(cflowerset
, ch
); }
969 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
970 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfpunctset
, ch
); }
971 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfspaceset
, ch
); }
972 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfupperset
, ch
); }
973 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxIsdigit(ch
) || (ch
>='a' && ch
<='f') || (ch
>='A' && ch
<='F'); }
974 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)tolower((char)(ch
)); }
975 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)toupper((char)(ch
)); }
977 #endif // wxNEED_WX_CTYPE_H
981 WXDLLEXPORT
char *wxStrdupA(const char *s
)
983 return strcpy((char *)malloc(strlen(s
) + 1), s
);
990 WXDLLEXPORT
wchar_t * wxStrdupW(const wchar_t *pwz
)
992 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
993 wchar_t *ret
= (wchar_t *) malloc(size
);
994 memcpy(ret
, pwz
, size
);
1001 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
1003 register wxChar c1
, c2
;
1005 c1
= wxTolower(*psz1
++);
1006 c2
= wxTolower(*psz2
++);
1007 } while ( c1
&& (c1
== c2
) );
1013 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1015 // initialize the variables just to suppress stupid gcc warning
1016 register wxChar c1
= 0, c2
= 0;
1017 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1019 if (c1
< c2
) return -1;
1020 if (c1
> c2
) return 1;
1026 #ifndef wxSetlocale_
1027 wxWCharBuffer
wxSetlocale_(int category
, const wxChar
*locale
)
1029 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
1031 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
1034 wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
1036 wxWCharBuffer rv
= wxSetlocale_(category
, locale
);
1038 wxUpdateLocaleIsUtf8();
1041 #else // defined(wxSetlocale_)
1042 wxChar
*wxSetlocale(int category
, const wxChar
*locale
)
1044 wxChar
*rv
= wxSetlocale_(category
, locale
);
1046 wxUpdateLocaleIsUtf8();
1049 #endif // wxSetlocale_ defined or not
1051 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
1052 WXDLLEXPORT
size_t wxWcslen(const wchar_t *s
)
1062 // ----------------------------------------------------------------------------
1063 // string.h functions
1064 // ----------------------------------------------------------------------------
1066 #ifdef wxNEED_WX_STRING_H
1068 // RN: These need to be c externed for the regex lib
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
size_t wxStrlen_(const wxChar
*s
)
1118 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1121 while (*dest
) dest
++;
1122 while (n
&& (*dest
++ = *src
++)) n
--;
1126 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1128 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1130 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1131 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1136 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1139 while (n
&& (*dest
++ = *src
++)) n
--;
1140 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1144 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1146 while (*s
&& !wxStrchr(accept
, *s
))
1149 return *s
? s
: NULL
;
1152 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1154 const wxChar
*ret
= NULL
;
1166 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1169 while (wxStrchr(accept
, *s
++)) len
++;
1173 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1175 wxASSERT_MSG( needle
!= NULL
, _T("NULL argument in wxStrstr") );
1177 // VZ: this is not exactly the most efficient string search algorithm...
1179 const size_t len
= wxStrlen(needle
);
1181 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1183 if ( !wxStrncmp(fnd
, needle
, len
) )
1196 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1198 const wxChar
*start
= nptr
;
1200 // FIXME: only correct for C locale
1201 while (wxIsspace(*nptr
)) nptr
++;
1202 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1203 while (wxIsdigit(*nptr
)) nptr
++;
1204 if (*nptr
== wxT('.')) {
1206 while (wxIsdigit(*nptr
)) nptr
++;
1208 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1210 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1211 while (wxIsdigit(*nptr
)) nptr
++;
1214 wxString
data(nptr
, nptr
-start
);
1215 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1216 char *rdat
= wxMBSTRINGCAST dat
;
1217 double ret
= strtod(dat
, &rdat
);
1219 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1224 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1226 const wxChar
*start
= nptr
;
1228 // FIXME: only correct for C locale
1229 while (wxIsspace(*nptr
)) nptr
++;
1230 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1231 if (((base
== 0) || (base
== 16)) &&
1232 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1236 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1237 else if (base
== 0) base
= 10;
1239 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1240 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1242 wxString
data(start
, nptr
-start
);
1243 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1244 char *rdat
= wxMBSTRINGCAST dat
;
1245 long int ret
= strtol(dat
, &rdat
, base
);
1247 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1252 WXDLLEXPORT
unsigned long int wxStrtoul(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1254 return (unsigned long int) wxStrtol(nptr
, endptr
, base
);
1257 #endif // wxNEED_WX_STRING_H
1259 #ifdef wxNEED_WX_STDIO_H
1260 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1262 char mode_buffer
[10];
1263 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1264 mode_buffer
[i
] = (char) mode
[i
];
1266 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1269 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1271 char mode_buffer
[10];
1272 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1273 mode_buffer
[i
] = (char) mode
[i
];
1275 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1278 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1280 return remove( wxConvFile
.cWX2MB(path
) );
1283 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1285 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1290 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1295 if (str
.ToDouble(& d
))
1300 return atof(wxConvLibc
.cWX2MB(psz
));
1305 #ifdef wxNEED_WX_STDLIB_H
1306 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1308 return atoi(wxConvLibc
.cWX2MB(psz
));
1311 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1313 return atol(wxConvLibc
.cWX2MB(psz
));
1316 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1319 // NB: buffer returned by getenv() is allowed to be overwritten next
1320 // time getenv() is called, so it is OK to use static string
1321 // buffer to hold the data.
1322 static wxWCharBuffer
value((wxChar
*)NULL
);
1323 value
= wxConvLibc
.cMB2WX(getenv(wxConvLibc
.cWX2MB(name
)));
1324 return value
.data();
1326 return getenv(name
);
1330 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1332 return system(wxConvLibc
.cWX2MB(psz
));
1335 #endif // wxNEED_WX_STDLIB_H
1337 #ifdef wxNEED_WX_TIME_H
1339 wxStrftime(wxChar
*s
, size_t maxsize
, const wxChar
*fmt
, const struct tm
*tm
)
1344 wxCharBuffer
buf(maxsize
);
1346 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
1350 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
1354 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
1358 wxStrncpy(s
, wbuf
, maxsize
);
1361 #endif // wxNEED_WX_TIME_H
1364 WXDLLEXPORT wxChar
*wxCtime(const time_t *timep
)
1366 // normally the string is 26 chars but give one more in case some broken
1367 // DOS compiler decides to use "\r\n" instead of "\n" at the end
1368 static wxChar buf
[27];
1370 // ctime() is guaranteed to return a string containing only ASCII
1371 // characters, as its format is always the same for any locale
1372 wxStrncpy(buf
, wxString::FromAscii(ctime(timep
)), WXSIZEOF(buf
));
1373 buf
[WXSIZEOF(buf
) - 1] = _T('\0');
1379 #endif // wxUSE_WCHAR_T
1382 static wxULongLong_t
wxStrtoullBase(const wxChar
* nptr
, wxChar
** endptr
, int base
, wxChar
* sign
)
1384 wxULongLong_t sum
= 0;
1385 wxString
wxstr(nptr
);
1386 wxString::const_iterator i
= wxstr
.begin();
1387 wxString::const_iterator end
= wxstr
.end();
1390 while ( i
!= end
&& wxIsspace(*i
) ) i
++;
1392 // Starts with sign?
1397 if ( c
== wxT('+') || c
== wxT('-') )
1405 if ( i
!= end
&& *i
== wxT('0') )
1410 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
1418 *endptr
= (wxChar
*) nptr
;
1419 wxSET_ERRNO(EINVAL
);
1430 for ( ; i
!= end
; i
++ )
1435 if ( c
>= wxT('0') )
1437 if ( c
<= wxT('9') )
1440 n
= wxTolower(c
) - wxT('a') + 10;
1445 if ( n
>= (unsigned int)base
)
1446 // Invalid character (for this base)
1449 wxULongLong_t prevsum
= sum
;
1450 sum
= (sum
* base
) + n
;
1452 if ( sum
< prevsum
)
1454 wxSET_ERRNO(ERANGE
);
1461 *endptr
= (wxChar
*)(nptr
+ (i
- wxstr
.begin()));
1467 wxULongLong_t
wxStrtoull(const wxChar
* nptr
, wxChar
** endptr
, int base
)
1470 wxULongLong_t uval
= wxStrtoullBase(nptr
, endptr
, base
, &sign
);
1472 if ( sign
== wxT('-') )
1474 wxSET_ERRNO(ERANGE
);
1481 wxLongLong_t
wxStrtoll(const wxChar
* nptr
, wxChar
** endptr
, int base
)
1484 wxULongLong_t uval
= wxStrtoullBase(nptr
, endptr
, base
, &sign
);
1485 wxLongLong_t val
= 0;
1487 if ( sign
== wxT('-') )
1489 if ( uval
<= wxULL(wxINT64_MAX
+1) )
1491 if ( uval
== wxULL(wxINT64_MAX
+1))
1492 val
= -((wxLongLong_t
)wxINT64_MAX
) - 1;
1494 val
= -((wxLongLong_t
)uval
);
1498 wxSET_ERRNO(ERANGE
);
1501 else if ( uval
<= wxINT64_MAX
)
1507 wxSET_ERRNO(ERANGE
);
1514 // ----------------------------------------------------------------------------
1515 // functions which we may need even if !wxUSE_WCHAR_T
1516 // ----------------------------------------------------------------------------
1520 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
1529 psz
+= wxStrspn(psz
, delim
);
1532 *save_ptr
= (wxChar
*)NULL
;
1533 return (wxChar
*)NULL
;
1537 psz
= wxStrpbrk(psz
, delim
);
1540 *save_ptr
= (wxChar
*)NULL
;
1545 *save_ptr
= psz
+ 1;
1553 // ----------------------------------------------------------------------------
1554 // missing C RTL functions
1555 // ----------------------------------------------------------------------------
1557 #ifdef wxNEED_STRDUP
1559 char *strdup(const char *s
)
1561 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1563 strcpy( dest
, s
) ;
1566 #endif // wxNEED_STRDUP
1568 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1570 void *calloc( size_t num
, size_t size
)
1572 void** ptr
= (void **)malloc(num
* size
);
1573 memset( ptr
, 0, num
* size
);
1577 #endif // __WXWINCE__ <= 211
1581 int wxRemove(const wxChar
*path
)
1583 return ::DeleteFile(path
) == 0;
1589 // ----------------------------------------------------------------------------
1591 // ----------------------------------------------------------------------------
1593 #if wxUSE_UNICODE_UTF8
1595 #if !wxUSE_UTF8_LOCALE_ONLY
1596 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1599 static bool wxIsLocaleUtf8()
1601 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1602 // because a) it may be unavailable in some builds and b) has slightly
1603 // different semantics (default locale instead of current)
1605 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1606 // GNU libc provides current character set this way (this conforms to
1608 const char *charset
= nl_langinfo(CODESET
);
1611 // "UTF-8" is used by modern glibc versions, but test other variants
1612 // as well, just in case:
1613 if ( strcmp(charset
, "UTF-8") == 0 ||
1614 strcmp(charset
, "utf-8") == 0 ||
1615 strcmp(charset
, "UTF8") == 0 ||
1616 strcmp(charset
, "utf8") == 0 )
1623 // check if we're running under the "C" locale: it is 7bit subset
1624 // of UTF-8, so it can be safely used with the UTF-8 build:
1625 const char *lc_ctype
= setlocale(LC_CTYPE
, NULL
);
1627 (strcmp(lc_ctype
, "C") == 0 || strcmp(lc_ctype
, "POSIX") == 0) )
1632 // we don't know what charset libc is using, so assume the worst
1637 void wxUpdateLocaleIsUtf8()
1639 #if wxUSE_UTF8_LOCALE_ONLY
1640 if ( !wxIsLocaleUtf8() )
1642 wxLogFatalError(_T("This program requires UTF-8 locale to run."));
1644 #else // !wxUSE_UTF8_LOCALE_ONLY
1645 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1649 #endif // wxUSE_UTF8_LOCALE_ONLY