1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wxchar.cpp
3 // Purpose: wxChar implementation
5 // Modified by: Ron Lee
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 #define _ISOC9X_SOURCE 1 // to get vsscanf()
24 #define _BSD_SOURCE 1 // to still get strdup()
34 #include "wx/msw/wince/time.h"
39 #include "wx/wxchar.h"
40 #include "wx/string.h"
44 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
51 #if defined(__MWERKS__) && __MSL__ >= 0x6000
57 #include "wx/mac/private.h"
61 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
63 // assume that we have mbsrtowcs() too if we have wcsrtombs()
66 memset(&mbstate
, 0, sizeof(mbstate_t));
71 if (n
) *buf
= wxT('\0');
75 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
77 return wxMbstowcs(buf
, psz
, n
);
82 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
84 return wxMbstowcs((wchar_t *) NULL
, psz
, 0);
88 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
92 memset(&mbstate
, 0, sizeof(mbstate_t));
97 // glibc2.1 chokes on null input
101 #ifdef HAVE_WCSRTOMBS
102 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
104 return wxWcstombs(buf
, pwz
, n
);
108 #ifdef HAVE_WCSRTOMBS
109 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
111 return wxWcstombs((char *) NULL
, pwz
, 0);
114 #endif // wxUSE_WCHAR_T
116 bool WXDLLEXPORT
wxOKlibc()
118 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
119 // glibc 2.0 uses UTF-8 even when it shouldn't
121 if ((MB_CUR_MAX
== 2) &&
122 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
124 // this is UTF-8 allright, check whether that's what we want
125 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
126 if ((strlen(cur_locale
) < 4) ||
127 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
128 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
129 // nope, don't use libc conversion
137 // ============================================================================
138 // printf() functions business
139 // ============================================================================
141 // special test mode: define all functions below even if we don't really need
142 // them to be able to test them
153 #define wxNEED_WPRINTF
155 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
158 // ----------------------------------------------------------------------------
159 // implement [v]snprintf() if the system doesn't provide a safe one
160 // ----------------------------------------------------------------------------
162 #if !defined(wxVsnprintf_)
163 int WXDLLEXPORT
wxVsnprintf_(wxChar
*buf
, size_t lenMax
,
164 const wxChar
*format
, va_list argptr
)
166 // buffer to avoid dynamic memory allocation each time for small strings
167 char szScratch
[1024];
169 // number of characters in the buffer so far, must be less than lenMax
172 for ( size_t n
= 0; ; n
++ )
174 const wxChar chCur
= format
[n
];
176 if ( chCur
== wxT('%') )
178 static char s_szFlags
[256] = "%";
180 bool adj_left
= false,
185 size_t min_width
= 0,
186 max_width
= wxSTRING_MAXLEN
;
191 if (in_prec && !prec_dot) \
193 s_szFlags[flagofs++] = '.'; \
197 #define APPEND_CH(ch) \
199 if ( lenCur == lenMax ) \
202 buf[lenCur++] = ch; \
205 #define APPEND_STR(s) \
207 for ( const wxChar *p = s; *p; p++ ) \
214 const wxChar ch
= format
[++n
];
234 s_szFlags
[flagofs
++] = ch
;
240 s_szFlags
[flagofs
++] = ch
;
248 // dot will be auto-added to s_szFlags if non-negative
255 s_szFlags
[flagofs
++] = ch
;
261 s_szFlags
[flagofs
++] = ch
;
268 s_szFlags
[flagofs
++] = ch
;
274 s_szFlags
[flagofs
++] = ch
;
279 int len
= va_arg(argptr
, int);
290 adj_left
= !adj_left
;
291 s_szFlags
[flagofs
++] = '-';
296 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
300 case wxT('1'): case wxT('2'): case wxT('3'):
301 case wxT('4'): case wxT('5'): case wxT('6'):
302 case wxT('7'): case wxT('8'): case wxT('9'):
306 while ( (format
[n
] >= wxT('0')) &&
307 (format
[n
] <= wxT('9')) )
309 s_szFlags
[flagofs
++] = format
[n
];
310 len
= len
*10 + (format
[n
] - wxT('0'));
319 n
--; // the main loop pre-increments n again
330 s_szFlags
[flagofs
++] = ch
;
331 s_szFlags
[flagofs
] = '\0';
334 int val
= va_arg(argptr
, int);
335 ::sprintf(szScratch
, s_szFlags
, val
);
339 // NB: 'short int' value passed through '...'
340 // is promoted to 'int', so we have to get
341 // an int from stack even if we need a short
342 short int val
= (short int) va_arg(argptr
, int);
343 ::sprintf(szScratch
, s_szFlags
, val
);
347 long int val
= va_arg(argptr
, long int);
348 ::sprintf(szScratch
, s_szFlags
, val
);
353 long long int val
= va_arg(argptr
, long long int);
354 ::sprintf(szScratch
, s_szFlags
, val
);
356 long int val
= va_arg(argptr
, long int);
357 ::sprintf(szScratch
, s_szFlags
, val
);
358 #endif // long long/!long long
362 size_t val
= va_arg(argptr
, size_t);
363 ::sprintf(szScratch
, s_szFlags
, val
);
367 const wxMB2WXbuf tmp
=
368 wxConvLibc
.cMB2WX(szScratch
);
381 s_szFlags
[flagofs
++] = ch
;
382 s_szFlags
[flagofs
] = '\0';
385 long double val
= va_arg(argptr
, long double);
386 ::sprintf(szScratch
, s_szFlags
, val
);
390 double val
= va_arg(argptr
, double);
391 ::sprintf(szScratch
, s_szFlags
, val
);
395 const wxMB2WXbuf tmp
=
396 wxConvLibc
.cMB2WX(szScratch
);
405 void *val
= va_arg(argptr
, void *);
407 s_szFlags
[flagofs
++] = ch
;
408 s_szFlags
[flagofs
] = '\0';
409 ::sprintf(szScratch
, s_szFlags
, val
);
411 const wxMB2WXbuf tmp
=
412 wxConvLibc
.cMB2WX(szScratch
);
421 int val
= va_arg(argptr
, int);
425 const char buf
[2] = { val
, 0 };
426 val
= wxString(buf
, wxConvLibc
)[0u];
431 const wchar_t buf
[2] = { val
, 0 };
432 val
= wxString(buf
, wxConvLibc
)[0u];
438 for (i
= 1; i
< min_width
; i
++)
444 for (i
= 1; i
< min_width
; i
++)
453 const wxChar
*val
= NULL
;
459 // wx extension: we'll let %hs mean non-Unicode
461 char *v
= va_arg(argptr
, char *);
464 val
= s
= wxString(v
, wxConvLibc
);
472 // %ls means Unicode strings
473 wchar_t *v
= va_arg(argptr
, wchar_t *);
476 val
= s
= wxString(v
, wxConvLibc
);
481 val
= va_arg(argptr
, wxChar
*);
489 val
[len
] && (len
< max_width
);
493 else if (max_width
>= 6)
507 for (i
= len
; i
< min_width
; i
++)
510 for (i
= 0; i
< len
; i
++)
514 for (i
= len
; i
< min_width
; i
++)
524 int *val
= va_arg(argptr
, int *);
529 short int *val
= va_arg(argptr
, short int *);
534 long int *val
= va_arg(argptr
, long int *);
541 // bad format, leave unchanged
567 #endif // !wxVsnprintfA
569 #if !defined(wxSnprintf_)
570 int WXDLLEXPORT
wxSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
573 va_start(argptr
, format
);
575 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
581 #endif // wxSnprintf_
584 /* Digital Mars adds count to _stprintf (C99) so convert */
586 int wxSprintf (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
590 va_start( arglist
, format
);
591 int iLen
= swprintf ( s
, -1, format
, arglist
);
596 #endif // wxUSE_UNICODE
600 // ----------------------------------------------------------------------------
601 // implement the standard IO functions for wide char if libc doesn't have them
602 // ----------------------------------------------------------------------------
605 int wxFputs(const wchar_t *ws
, FILE *stream
)
607 // counting the number of wide characters written isn't worth the trouble,
608 // simply distinguish between ok and error
609 return fputs(wxConvLibc
.cWC2MB(ws
), stream
) == -1 ? -1 : 0;
611 #endif // wxNEED_FPUTS
614 int wxPuts(const wxChar
*ws
)
616 int rc
= wxFputs(ws
, stdout
);
619 if ( wxFputs(L
"\n", stdout
) == -1 )
627 #endif // wxNEED_PUTS
630 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
632 wchar_t ws
[2] = { wc
, L
'\0' };
634 return wxFputs(ws
, stream
);
636 #endif // wxNEED_PUTC
638 // NB: we only implement va_list functions here, the ones taking ... are
639 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
640 // the definitions there to avoid duplicating them here
641 #ifdef wxNEED_WPRINTF
643 // TODO: implement the scanf() functions
644 int vwscanf(const wxChar
*format
, va_list argptr
)
646 wxFAIL_MSG( _T("TODO") );
651 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
653 // The best we can do without proper Unicode support in glibc is to
654 // convert the strings into MB representation and run ANSI version
655 // of the function. This doesn't work with %c and %s because of difference
656 // in size of char and wchar_t, though.
658 wxCHECK_MSG( wxStrstr(format
, _T("%s")) == NULL
, -1,
659 _T("incomplete vswscanf implementation doesn't allow %s") );
660 wxCHECK_MSG( wxStrstr(format
, _T("%c")) == NULL
, -1,
661 _T("incomplete vswscanf implementation doesn't allow %c") );
664 wxVaCopy(argcopy
, argptr
);
665 return vsscanf(wxConvLibc
.cWX2MB(ws
), wxConvLibc
.cWX2MB(format
), argcopy
);
668 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
670 wxFAIL_MSG( _T("TODO") );
675 #define vswprintf wxVsnprintf_
677 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
680 int rc
= s
.PrintfV(format
, argptr
);
684 // we can't do much better without Unicode support in libc...
685 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
692 int vwprintf(const wxChar
*format
, va_list argptr
)
694 return wxVfprintf(stdout
, format
, argptr
);
697 #endif // wxNEED_WPRINTF
699 #ifdef wxNEED_PRINTF_CONVERSION
701 // ----------------------------------------------------------------------------
702 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
703 // ----------------------------------------------------------------------------
706 Here are the gory details. We want to follow the Windows/MS conventions,
711 format specifier results in
712 -----------------------------------
718 format specifier results in
719 -----------------------------------
724 while on POSIX systems we have %C identical to %lc and %c always means char
725 (in any mode) while %lc always means wchar_t,
727 So to use native functions in order to get our semantics we must do the
728 following translations in Unicode mode (nothing to do in ANSI mode):
730 wxWidgets specifier POSIX specifier
731 ----------------------------------------
737 And, of course, the same should be done for %s as well.
740 class wxFormatConverter
743 wxFormatConverter(const wxChar
*format
);
745 // notice that we only translated the string if m_fmtOrig == NULL (as set
746 // by CopyAllBefore()), otherwise we should simply use the original format
747 operator const wxChar
*() const
748 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
751 // copy another character to the translated format: this function does the
752 // copy if we are translating but doesn't do anything at all if we don't,
753 // so we don't create the translated format string at all unless we really
754 // need to (i.e. InsertFmtChar() is called)
755 wxChar
CopyFmtChar(wxChar ch
)
759 // we're translating, do copy
764 // simply increase the count which should be copied by
765 // CopyAllBefore() later if needed
772 // insert an extra character
773 void InsertFmtChar(wxChar ch
)
777 // so far we haven't translated anything yet
786 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
788 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
790 // we won't need it any longer
794 static bool IsFlagChar(wxChar ch
)
796 return ch
== _T('-') || ch
== _T('+') ||
797 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
800 void SkipDigits(const wxChar
**ptpc
)
802 while ( **ptpc
>= _T('0') && **ptpc
<= _T('9') )
803 CopyFmtChar(*(*ptpc
)++);
806 // the translated format
809 // the original format
810 const wxChar
*m_fmtOrig
;
812 // the number of characters already copied
816 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
823 if ( CopyFmtChar(*format
++) == _T('%') )
826 while ( IsFlagChar(*format
) )
827 CopyFmtChar(*format
++);
829 // and possible width
830 if ( *format
== _T('*') )
831 CopyFmtChar(*format
++);
836 if ( *format
== _T('.') )
838 CopyFmtChar(*format
++);
839 if ( *format
== _T('*') )
840 CopyFmtChar(*format
++);
845 // next we can have a size modifier
861 // "ll" has a different meaning!
862 if ( format
[1] != _T('l') )
874 // and finally we should have the type
879 // %C and %hC -> %c and %lC -> %lc
881 CopyFmtChar(_T('l'));
883 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
888 // %c -> %lc but %hc stays %hc and %lc is still %lc
889 if ( size
== Default
)
890 InsertFmtChar(_T('l'));
894 // nothing special to do
895 if ( size
!= Default
)
896 CopyFmtChar(*(format
- 1));
897 CopyFmtChar(*format
++);
903 #else // !wxNEED_PRINTF_CONVERSION
904 // no conversion necessary
905 #define wxFormatConverter(x) (x)
906 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
909 // For testing the format converter
910 wxString
wxConvertFormat(const wxChar
*format
)
912 return wxString(wxFormatConverter(format
));
916 // ----------------------------------------------------------------------------
917 // wxPrintf(), wxScanf() and relatives
918 // ----------------------------------------------------------------------------
920 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
922 int wxScanf( const wxChar
*format
, ... )
925 va_start(argptr
, format
);
927 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
934 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... )
937 va_start(argptr
, format
);
939 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
946 int wxFscanf( FILE *stream
, const wxChar
*format
, ... )
949 va_start(argptr
, format
);
950 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
957 int wxPrintf( const wxChar
*format
, ... )
960 va_start(argptr
, format
);
962 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
970 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... )
973 va_start(argptr
, format
);
975 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
983 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... )
986 va_start(argptr
, format
);
988 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
989 // it's safe to implement this one in terms of it
990 wxString
s(wxString::FormatV(format
, argptr
));
998 int wxFprintf( FILE *stream
, const wxChar
*format
, ... )
1001 va_start( argptr
, format
);
1003 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
1010 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
1012 return vswscanf( str
, wxFormatConverter(format
), argptr
);
1015 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
1017 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
1020 int wxVprintf( const wxChar
*format
, va_list argptr
)
1022 return vwprintf( wxFormatConverter(format
), argptr
);
1026 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
1028 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
1030 #endif // wxVsnprintf
1032 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
1034 // same as for wxSprintf()
1035 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
1038 #endif // wxNEED_PRINTF_CONVERSION
1042 // ----------------------------------------------------------------------------
1043 // ctype.h stuff (currently unused)
1044 // ----------------------------------------------------------------------------
1046 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
1047 inline WORD
wxMSW_ctype(wxChar ch
)
1050 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
1054 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
1055 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
1056 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
1057 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
1058 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
1059 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
1060 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
1061 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
1062 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
1063 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
1064 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
1065 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
1066 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
1069 #if defined(__DARWIN__) && ( MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2 )
1071 WXDLLEXPORT
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
1081 const char* origin
= in
;
1083 while (outlen
-- && *in
)
1085 *out
++ = (wchar_t) *in
++;
1093 WXDLLEXPORT
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
1103 const wchar_t* origin
= in
;
1105 while (outlen
-- && *in
)
1107 *out
++ = (char) *in
++;
1115 #if defined(wxNEED_WX_CTYPE_H)
1117 #include <CoreFoundation/CoreFoundation.h>
1119 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
1120 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
1121 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
1122 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
1123 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
1124 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
1125 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
1126 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
1127 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
1128 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
1130 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalnumset
, ch
); }
1131 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalphaset
, ch
); }
1132 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
1133 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfdigitset
, ch
); }
1134 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
) && ch
!= ' '; }
1135 WXDLLEXPORT
int wxIslower(wxChar ch
) { return CFCharacterSetIsCharacterMember(cflowerset
, ch
); }
1136 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
1137 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfpunctset
, ch
); }
1138 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfspaceset
, ch
); }
1139 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfupperset
, ch
); }
1140 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxIsdigit(ch
) || (ch
>='a' && ch
<='f') || (ch
>='A' && ch
<='F'); }
1141 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)tolower((char)(ch
)); }
1142 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)toupper((char)(ch
)); }
1144 #endif // wxNEED_WX_CTYPE_H
1146 #endif // defined(__DARWIN__) and OSX <= 10.2
1150 WXDLLEXPORT
char *wxStrdupA(const char *s
)
1152 return strcpy((char *)malloc(strlen(s
) + 1), s
);
1159 WXDLLEXPORT
wchar_t * wxStrdupW(const wchar_t *pwz
)
1161 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
1162 wchar_t *ret
= (wchar_t *) malloc(size
);
1163 memcpy(ret
, pwz
, size
);
1170 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
1172 register wxChar c1
, c2
;
1174 c1
= wxTolower(*psz1
++);
1175 c2
= wxTolower(*psz2
++);
1176 } while ( c1
&& (c1
== c2
) );
1182 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1184 // initialize the variables just to suppress stupid gcc warning
1185 register wxChar c1
= 0, c2
= 0;
1186 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1188 if (c1
< c2
) return -1;
1189 if (c1
> c2
) return 1;
1196 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
1198 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
1200 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
1204 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
1205 WXDLLEXPORT
size_t wxWcslen(const wchar_t *s
)
1215 // ----------------------------------------------------------------------------
1216 // string.h functions
1217 // ----------------------------------------------------------------------------
1219 #ifdef wxNEED_WX_STRING_H
1221 // RN: These need to be c externed for the regex lib
1226 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
1229 while (*dest
) dest
++;
1230 while ((*dest
++ = *src
++));
1234 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
1236 // be careful here as the terminating NUL makes part of the string
1246 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
1248 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
1249 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1250 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1254 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
1257 while ((*dest
++ = *src
++));
1261 WXDLLEXPORT
size_t wxStrlen_(const wxChar
*s
)
1271 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1274 while (*dest
) dest
++;
1275 while (n
&& (*dest
++ = *src
++)) n
--;
1279 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1281 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1283 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1284 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1289 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1292 while (n
&& (*dest
++ = *src
++)) n
--;
1293 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1297 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1299 while (*s
&& !wxStrchr(accept
, *s
))
1302 return *s
? s
: NULL
;
1305 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1307 const wxChar
*ret
= NULL
;
1319 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1322 while (wxStrchr(accept
, *s
++)) len
++;
1326 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1328 wxASSERT_MSG( needle
!= NULL
, _T("NULL argument in wxStrstr") );
1330 // VZ: this is not exactly the most efficient string search algorithm...
1332 const size_t len
= wxStrlen(needle
);
1334 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1336 if ( !wxStrncmp(fnd
, needle
, len
) )
1349 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1351 const wxChar
*start
= nptr
;
1353 // FIXME: only correct for C locale
1354 while (wxIsspace(*nptr
)) nptr
++;
1355 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1356 while (wxIsdigit(*nptr
)) nptr
++;
1357 if (*nptr
== wxT('.')) {
1359 while (wxIsdigit(*nptr
)) nptr
++;
1361 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1363 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1364 while (wxIsdigit(*nptr
)) nptr
++;
1367 wxString
data(nptr
, nptr
-start
);
1368 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1369 char *rdat
= wxMBSTRINGCAST dat
;
1370 double ret
= strtod(dat
, &rdat
);
1372 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1377 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1379 const wxChar
*start
= nptr
;
1381 // FIXME: only correct for C locale
1382 while (wxIsspace(*nptr
)) nptr
++;
1383 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1384 if (((base
== 0) || (base
== 16)) &&
1385 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1389 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1390 else if (base
== 0) base
= 10;
1392 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1393 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1395 wxString
data(start
, nptr
-start
);
1396 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1397 char *rdat
= wxMBSTRINGCAST dat
;
1398 long int ret
= strtol(dat
, &rdat
, base
);
1400 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1405 WXDLLEXPORT
unsigned long int wxStrtoul(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1407 return (unsigned long int) wxStrtol(nptr
, endptr
, base
);
1410 #endif // wxNEED_WX_STRING_H
1412 #ifdef wxNEED_WX_STDIO_H
1413 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1415 char mode_buffer
[10];
1416 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1417 mode_buffer
[i
] = (char) mode
[i
];
1419 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1422 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1424 char mode_buffer
[10];
1425 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1426 mode_buffer
[i
] = (char) mode
[i
];
1428 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1431 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1433 return remove( wxConvFile
.cWX2MB(path
) );
1436 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1438 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1443 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1448 if (str
.ToDouble(& d
))
1453 return atof(wxConvLibc
.cWX2MB(psz
));
1458 #ifdef wxNEED_WX_STDLIB_H
1459 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1461 return atoi(wxConvLibc
.cWX2MB(psz
));
1464 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1466 return atol(wxConvLibc
.cWX2MB(psz
));
1469 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1472 // NB: buffer returned by getenv() is allowed to be overwritten next
1473 // time getenv() is called, so it is OK to use static string
1474 // buffer to hold the data.
1475 static wxWCharBuffer
value((wxChar
*)NULL
);
1476 value
= wxConvLibc
.cMB2WX(getenv(wxConvLibc
.cWX2MB(name
)));
1477 return value
.data();
1479 return getenv(name
);
1483 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1485 return system(wxConvLibc
.cWX2MB(psz
));
1488 #endif // wxNEED_WX_STDLIB_H
1490 #ifdef wxNEED_WX_TIME_H
1492 wxStrftime(wxChar
*s
, size_t maxsize
, const wxChar
*fmt
, const struct tm
*tm
)
1497 wxCharBuffer
buf(maxsize
);
1499 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
1503 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
1507 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
1511 wxStrncpy(s
, wbuf
, maxsize
);
1514 #endif // wxNEED_WX_TIME_H
1517 WXDLLEXPORT wxChar
*wxCtime(const time_t *timep
)
1519 // normally the string is 26 chars but give one more in case some broken
1520 // DOS compiler decides to use "\r\n" instead of "\n" at the end
1521 static wxChar buf
[27];
1523 // ctime() is guaranteed to return a string containing only ASCII
1524 // characters, as its format is always the same for any locale
1525 wxStrncpy(buf
, wxString::FromAscii(ctime(timep
)), WXSIZEOF(buf
));
1526 buf
[WXSIZEOF(buf
) - 1] = _T('\0');
1532 #endif // wxUSE_WCHAR_T
1534 // ----------------------------------------------------------------------------
1535 // functions which we may need even if !wxUSE_WCHAR_T
1536 // ----------------------------------------------------------------------------
1540 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
1549 psz
+= wxStrspn(psz
, delim
);
1552 *save_ptr
= (wxChar
*)NULL
;
1553 return (wxChar
*)NULL
;
1557 psz
= wxStrpbrk(psz
, delim
);
1560 *save_ptr
= (wxChar
*)NULL
;
1565 *save_ptr
= psz
+ 1;
1573 // ----------------------------------------------------------------------------
1574 // missing C RTL functions
1575 // ----------------------------------------------------------------------------
1577 #ifdef wxNEED_STRDUP
1579 char *strdup(const char *s
)
1581 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1583 strcpy( dest
, s
) ;
1586 #endif // wxNEED_STRDUP
1588 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1590 void *calloc( size_t num
, size_t size
)
1592 void** ptr
= (void **)malloc(num
* size
);
1593 memset( ptr
, 0, num
* size
);
1597 #endif // __WXWINCE__ <= 211
1601 int wxRemove(const wxChar
*path
)
1603 return ::DeleteFile(path
) == 0;