1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxChar implementation
5 // Modified by: Ron Lee
8 // Copyright: (c) wxWidgets copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "wxchar.h"
16 // ===========================================================================
17 // headers, declarations, constants
18 // ===========================================================================
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #define _ISOC9X_SOURCE 1 // to get vsscanf()
28 #define _BSD_SOURCE 1 // to still get strdup()
38 #include "wx/msw/wince/time.h"
43 #include "wx/wxchar.h"
44 #include "wx/string.h"
48 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
55 #if defined(__MWERKS__) && __MSL__ >= 0x6000
61 #include "wx/mac/private.h"
65 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
67 // assume that we have mbsrtowcs() too if we have wcsrtombs()
70 memset(&mbstate
, 0, sizeof(mbstate_t));
75 if (n
) *buf
= wxT('\0');
79 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
81 return wxMbstowcs(buf
, psz
, n
);
86 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
88 return wxMbstowcs((wchar_t *) NULL
, psz
, 0);
92 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
96 memset(&mbstate
, 0, sizeof(mbstate_t));
101 // glibc2.1 chokes on null input
106 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
108 return wxWcstombs(buf
, pwz
, n
);
113 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
115 return wxWcstombs((char *) NULL
, pwz
, 0);
118 #endif // wxUSE_WCHAR_T
120 bool WXDLLEXPORT
wxOKlibc()
122 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
123 // glibc 2.0 uses UTF-8 even when it shouldn't
125 if ((MB_CUR_MAX
== 2) &&
126 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
128 // this is UTF-8 allright, check whether that's what we want
129 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
130 if ((strlen(cur_locale
) < 4) ||
131 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
132 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
133 // nope, don't use libc conversion
141 // ============================================================================
142 // printf() functions business
143 // ============================================================================
145 // special test mode: define all functions below even if we don't really need
146 // them to be able to test them
157 #define wxNEED_WPRINTF
159 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
162 // ----------------------------------------------------------------------------
163 // implement [v]snprintf() if the system doesn't provide a safe one
164 // ----------------------------------------------------------------------------
166 #if !defined(wxVsnprintf_)
167 int WXDLLEXPORT
wxVsnprintf_(wxChar
*buf
, size_t lenMax
,
168 const wxChar
*format
, va_list argptr
)
170 // buffer to avoid dynamic memory allocation each time for small strings
171 char szScratch
[1024];
173 // number of characters in the buffer so far, must be less than lenMax
176 for ( size_t n
= 0; ; n
++ )
178 const wxChar chCur
= format
[n
];
180 if ( chCur
== wxT('%') )
182 static char s_szFlags
[256] = "%";
184 bool adj_left
= false,
189 size_t min_width
= 0,
190 max_width
= wxSTRING_MAXLEN
;
195 if (in_prec && !prec_dot) \
197 s_szFlags[flagofs++] = '.'; \
201 #define APPEND_CH(ch) \
203 if ( lenCur == lenMax ) \
206 buf[lenCur++] = ch; \
209 #define APPEND_STR(s) \
211 for ( const wxChar *p = s; *p; p++ ) \
218 const wxChar ch
= format
[++n
];
238 s_szFlags
[flagofs
++] = ch
;
244 s_szFlags
[flagofs
++] = ch
;
252 // dot will be auto-added to s_szFlags if non-negative
259 s_szFlags
[flagofs
++] = ch
;
265 s_szFlags
[flagofs
++] = ch
;
272 s_szFlags
[flagofs
++] = ch
;
278 s_szFlags
[flagofs
++] = ch
;
283 int len
= va_arg(argptr
, int);
294 adj_left
= !adj_left
;
295 s_szFlags
[flagofs
++] = '-';
300 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
304 case wxT('1'): case wxT('2'): case wxT('3'):
305 case wxT('4'): case wxT('5'): case wxT('6'):
306 case wxT('7'): case wxT('8'): case wxT('9'):
310 while ( (format
[n
] >= wxT('0')) &&
311 (format
[n
] <= wxT('9')) )
313 s_szFlags
[flagofs
++] = format
[n
];
314 len
= len
*10 + (format
[n
] - wxT('0'));
323 n
--; // the main loop pre-increments n again
334 s_szFlags
[flagofs
++] = ch
;
335 s_szFlags
[flagofs
] = '\0';
338 int val
= va_arg(argptr
, int);
339 ::sprintf(szScratch
, s_szFlags
, val
);
343 // NB: 'short int' value passed through '...'
344 // is promoted to 'int', so we have to get
345 // an int from stack even if we need a short
346 short int val
= (short int) va_arg(argptr
, int);
347 ::sprintf(szScratch
, s_szFlags
, val
);
351 long int val
= va_arg(argptr
, long int);
352 ::sprintf(szScratch
, s_szFlags
, val
);
357 long long int val
= va_arg(argptr
, long long int);
358 ::sprintf(szScratch
, s_szFlags
, val
);
360 long int val
= va_arg(argptr
, long int);
361 ::sprintf(szScratch
, s_szFlags
, val
);
362 #endif // long long/!long long
366 size_t val
= va_arg(argptr
, size_t);
367 ::sprintf(szScratch
, s_szFlags
, val
);
371 const wxMB2WXbuf tmp
=
372 wxConvLibc
.cMB2WX(szScratch
);
385 s_szFlags
[flagofs
++] = ch
;
386 s_szFlags
[flagofs
] = '\0';
389 long double val
= va_arg(argptr
, long double);
390 ::sprintf(szScratch
, s_szFlags
, val
);
394 double val
= va_arg(argptr
, double);
395 ::sprintf(szScratch
, s_szFlags
, val
);
399 const wxMB2WXbuf tmp
=
400 wxConvLibc
.cMB2WX(szScratch
);
409 void *val
= va_arg(argptr
, void *);
411 s_szFlags
[flagofs
++] = ch
;
412 s_szFlags
[flagofs
] = '\0';
413 ::sprintf(szScratch
, s_szFlags
, val
);
415 const wxMB2WXbuf tmp
=
416 wxConvLibc
.cMB2WX(szScratch
);
425 int val
= va_arg(argptr
, int);
429 const char buf
[2] = { val
, 0 };
430 val
= wxString(buf
, wxConvLibc
)[0u];
435 const wchar_t buf
[2] = { val
, 0 };
436 val
= wxString(buf
, wxConvLibc
)[0u];
442 for (i
= 1; i
< min_width
; i
++)
448 for (i
= 1; i
< min_width
; i
++)
457 const wxChar
*val
= NULL
;
463 // wx extension: we'll let %hs mean non-Unicode
465 char *v
= va_arg(argptr
, char *);
468 val
= s
= wxString(v
, wxConvLibc
);
476 // %ls means Unicode strings
477 wchar_t *v
= va_arg(argptr
, wchar_t *);
480 val
= s
= wxString(v
, wxConvLibc
);
485 val
= va_arg(argptr
, wxChar
*);
493 val
[len
] && (len
< max_width
);
497 else if (max_width
>= 6)
511 for (i
= len
; i
< min_width
; i
++)
514 for (i
= 0; i
< len
; i
++)
518 for (i
= len
; i
< min_width
; i
++)
528 int *val
= va_arg(argptr
, int *);
533 short int *val
= va_arg(argptr
, short int *);
538 long int *val
= va_arg(argptr
, long int *);
545 // bad format, leave unchanged
571 #endif // !wxVsnprintfA
573 #if !defined(wxSnprintf_)
574 int WXDLLEXPORT
wxSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
577 va_start(argptr
, format
);
579 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
585 #endif // wxSnprintf_
588 /* Digital Mars adds count to _stprintf (C99) so convert */
590 int wxSprintf (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
594 va_start( arglist
, format
);
595 int iLen
= swprintf ( s
, -1, format
, arglist
);
600 #endif // wxUSE_UNICODE
604 // ----------------------------------------------------------------------------
605 // implement the standard IO functions for wide char if libc doesn't have them
606 // ----------------------------------------------------------------------------
609 int wxFputs(const wchar_t *ws
, FILE *stream
)
611 // counting the number of wide characters written isn't worth the trouble,
612 // simply distinguish between ok and error
613 return fputs(wxConvLibc
.cWC2MB(ws
), stream
) == -1 ? -1 : 0;
615 #endif // wxNEED_FPUTS
618 int wxPuts(const wxChar
*ws
)
620 int rc
= wxFputs(ws
, stdout
);
623 if ( wxFputs(L
"\n", stdout
) == -1 )
631 #endif // wxNEED_PUTS
634 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
636 wchar_t ws
[2] = { wc
, L
'\0' };
638 return wxFputs(ws
, stream
);
640 #endif // wxNEED_PUTC
642 // NB: we only implement va_list functions here, the ones taking ... are
643 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
644 // the definitions there to avoid duplicating them here
645 #ifdef wxNEED_WPRINTF
647 // TODO: implement the scanf() functions
648 int vwscanf(const wxChar
*format
, va_list argptr
)
650 wxFAIL_MSG( _T("TODO") );
655 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
657 // The best we can do without proper Unicode support in glibc is to
658 // convert the strings into MB representation and run ANSI version
659 // of the function. This doesn't work with %c and %s because of difference
660 // in size of char and wchar_t, though.
662 wxCHECK_MSG( wxStrstr(format
, _T("%s")) == NULL
, -1,
663 _T("incomplete vswscanf implementation doesn't allow %s") );
664 wxCHECK_MSG( wxStrstr(format
, _T("%c")) == NULL
, -1,
665 _T("incomplete vswscanf implementation doesn't allow %c") );
668 wxVaCopy(argcopy
, argptr
);
669 return vsscanf(wxConvLibc
.cWX2MB(ws
), wxConvLibc
.cWX2MB(format
), argcopy
);
672 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
674 wxFAIL_MSG( _T("TODO") );
679 #define vswprintf wxVsnprintf_
681 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
684 int rc
= s
.PrintfV(format
, argptr
);
688 // we can't do much better without Unicode support in libc...
689 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
696 int vwprintf(const wxChar
*format
, va_list argptr
)
698 return wxVfprintf(stdout
, format
, argptr
);
701 #endif // wxNEED_WPRINTF
703 #ifdef wxNEED_PRINTF_CONVERSION
705 // ----------------------------------------------------------------------------
706 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
707 // ----------------------------------------------------------------------------
710 Here are the gory details. We want to follow the Windows/MS conventions,
715 format specifier results in
716 -----------------------------------
722 format specifier results in
723 -----------------------------------
728 while on POSIX systems we have %C identical to %lc and %c always means char
729 (in any mode) while %lc always means wchar_t,
731 So to use native functions in order to get our semantics we must do the
732 following translations in Unicode mode (nothing to do in ANSI mode):
734 wxWidgets specifier POSIX specifier
735 ----------------------------------------
741 And, of course, the same should be done for %s as well.
744 class wxFormatConverter
747 wxFormatConverter(const wxChar
*format
);
749 // notice that we only translated the string if m_fmtOrig == NULL (as set
750 // by CopyAllBefore()), otherwise we should simply use the original format
751 operator const wxChar
*() const
752 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
755 // copy another character to the translated format: this function does the
756 // copy if we are translating but doesn't do anything at all if we don't,
757 // so we don't create the translated format string at all unless we really
758 // need to (i.e. InsertFmtChar() is called)
759 wxChar
CopyFmtChar(wxChar ch
)
763 // we're translating, do copy
768 // simply increase the count which should be copied by
769 // CopyAllBefore() later if needed
776 // insert an extra character
777 void InsertFmtChar(wxChar ch
)
781 // so far we haven't translated anything yet
790 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
792 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
794 // we won't need it any longer
798 static bool IsFlagChar(wxChar ch
)
800 return ch
== _T('-') || ch
== _T('+') ||
801 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
804 void SkipDigits(const wxChar
**ptpc
)
806 while ( **ptpc
>= _T('0') && **ptpc
<= _T('9') )
807 CopyFmtChar(*(*ptpc
)++);
810 // the translated format
813 // the original format
814 const wxChar
*m_fmtOrig
;
816 // the number of characters already copied
820 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
827 if ( CopyFmtChar(*format
++) == _T('%') )
830 while ( IsFlagChar(*format
) )
831 CopyFmtChar(*format
++);
833 // and possible width
834 if ( *format
== _T('*') )
835 CopyFmtChar(*format
++);
840 if ( *format
== _T('.') )
842 CopyFmtChar(*format
++);
843 if ( *format
== _T('*') )
844 CopyFmtChar(*format
++);
849 // next we can have a size modifier
865 // "ll" has a different meaning!
866 if ( format
[1] != _T('l') )
878 // and finally we should have the type
883 // %C and %hC -> %c and %lC -> %lc
885 CopyFmtChar(_T('l'));
887 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
892 // %c -> %lc but %hc stays %hc and %lc is still %lc
893 if ( size
== Default
)
894 InsertFmtChar(_T('l'));
898 // nothing special to do
899 if ( size
!= Default
)
900 CopyFmtChar(*(format
- 1));
901 CopyFmtChar(*format
++);
907 #else // !wxNEED_PRINTF_CONVERSION
908 // no conversion necessary
909 #define wxFormatConverter(x) (x)
910 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
913 // For testing the format converter
914 wxString
wxConvertFormat(const wxChar
*format
)
916 return wxString(wxFormatConverter(format
));
920 // ----------------------------------------------------------------------------
921 // wxPrintf(), wxScanf() and relatives
922 // ----------------------------------------------------------------------------
924 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
926 int wxScanf( const wxChar
*format
, ... )
929 va_start(argptr
, format
);
931 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
938 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... )
941 va_start(argptr
, format
);
943 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
950 int wxFscanf( FILE *stream
, const wxChar
*format
, ... )
953 va_start(argptr
, format
);
954 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
961 int wxPrintf( const wxChar
*format
, ... )
964 va_start(argptr
, format
);
966 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
974 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... )
977 va_start(argptr
, format
);
979 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
987 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... )
990 va_start(argptr
, format
);
992 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
993 // it's safe to implement this one in terms of it
994 wxString
s(wxString::FormatV(format
, argptr
));
1002 int wxFprintf( FILE *stream
, const wxChar
*format
, ... )
1005 va_start( argptr
, format
);
1007 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
1014 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
1016 return vswscanf( str
, wxFormatConverter(format
), argptr
);
1019 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
1021 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
1024 int wxVprintf( const wxChar
*format
, va_list argptr
)
1026 return vwprintf( wxFormatConverter(format
), argptr
);
1030 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
1032 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
1034 #endif // wxVsnprintf
1036 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
1038 // same as for wxSprintf()
1039 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
1042 #endif // wxNEED_PRINTF_CONVERSION
1046 // ----------------------------------------------------------------------------
1047 // ctype.h stuff (currently unused)
1048 // ----------------------------------------------------------------------------
1050 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
1051 inline WORD
wxMSW_ctype(wxChar ch
)
1054 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
1058 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
1059 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
1060 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
1061 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
1062 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
1063 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
1064 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
1065 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
1066 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
1067 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
1068 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
1069 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
1070 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
1073 #if defined(__DARWIN__) && ( MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2 )
1075 WXDLLEXPORT
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
1085 const char* origin
= in
;
1087 while (outlen
-- && *in
)
1089 *out
++ = (wchar_t) *in
++;
1097 WXDLLEXPORT
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
1107 const wchar_t* origin
= in
;
1109 while (outlen
-- && *in
)
1111 *out
++ = (char) *in
++;
1119 #if defined(wxNEED_WX_CTYPE_H)
1121 #include <CoreFoundation/CoreFoundation.h>
1123 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
1124 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
1125 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
1126 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
1127 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
1128 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
1129 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
1130 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
1131 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
1132 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
1134 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalnumset
, ch
); }
1135 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalphaset
, ch
); }
1136 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
1137 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfdigitset
, ch
); }
1138 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
) && ch
!= ' '; }
1139 WXDLLEXPORT
int wxIslower(wxChar ch
) { return CFCharacterSetIsCharacterMember(cflowerset
, ch
); }
1140 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
1141 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfpunctset
, ch
); }
1142 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfspaceset
, ch
); }
1143 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfupperset
, ch
); }
1144 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxIsdigit(ch
) || (ch
>='a' && ch
<='f') || (ch
>='A' && ch
<='F'); }
1145 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)tolower((char)(ch
)); }
1146 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)toupper((char)(ch
)); }
1148 #endif // wxNEED_WX_CTYPE_H
1150 #endif // defined(__DARWIN__) and OSX <= 10.2
1154 WXDLLEXPORT
char *wxStrdupA(const char *s
)
1156 return strcpy((char *)malloc(strlen(s
) + 1), s
);
1163 WXDLLEXPORT
wchar_t * wxStrdupW(const wchar_t *pwz
)
1165 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
1166 wchar_t *ret
= (wchar_t *) malloc(size
);
1167 memcpy(ret
, pwz
, size
);
1174 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
1176 register wxChar c1
, c2
;
1178 c1
= wxTolower(*psz1
++);
1179 c2
= wxTolower(*psz2
++);
1180 } while ( c1
&& (c1
== c2
) );
1186 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1188 // initialize the variables just to suppress stupid gcc warning
1189 register wxChar c1
= 0, c2
= 0;
1190 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1192 if (c1
< c2
) return -1;
1193 if (c1
> c2
) return 1;
1200 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
1202 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
1204 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
1208 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
1209 WXDLLEXPORT
size_t wxWcslen(const wchar_t *s
)
1219 // ----------------------------------------------------------------------------
1220 // string.h functions
1221 // ----------------------------------------------------------------------------
1223 #ifdef wxNEED_WX_STRING_H
1225 // RN: These need to be c externed for the regex lib
1230 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
1233 while (*dest
) dest
++;
1234 while ((*dest
++ = *src
++));
1238 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
1240 // be careful here as the terminating NUL makes part of the string
1250 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
1252 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
1253 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1254 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1258 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
1261 while ((*dest
++ = *src
++));
1265 WXDLLEXPORT
size_t wxStrlen_(const wxChar
*s
)
1275 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1278 while (*dest
) dest
++;
1279 while (n
&& (*dest
++ = *src
++)) n
--;
1283 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1285 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1287 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1288 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1293 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1296 while (n
&& (*dest
++ = *src
++)) n
--;
1297 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1301 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1303 while (*s
&& !wxStrchr(accept
, *s
))
1306 return *s
? s
: NULL
;
1309 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1311 const wxChar
*ret
= NULL
;
1323 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1326 while (wxStrchr(accept
, *s
++)) len
++;
1330 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1332 wxASSERT_MSG( needle
!= NULL
, _T("NULL argument in wxStrstr") );
1334 // VZ: this is not exactly the most efficient string search algorithm...
1336 const size_t len
= wxStrlen(needle
);
1338 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1340 if ( !wxStrncmp(fnd
, needle
, len
) )
1353 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1355 const wxChar
*start
= nptr
;
1357 // FIXME: only correct for C locale
1358 while (wxIsspace(*nptr
)) nptr
++;
1359 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1360 while (wxIsdigit(*nptr
)) nptr
++;
1361 if (*nptr
== wxT('.')) {
1363 while (wxIsdigit(*nptr
)) nptr
++;
1365 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1367 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1368 while (wxIsdigit(*nptr
)) nptr
++;
1371 wxString
data(nptr
, nptr
-start
);
1372 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1373 char *rdat
= wxMBSTRINGCAST dat
;
1374 double ret
= strtod(dat
, &rdat
);
1376 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1381 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1383 const wxChar
*start
= nptr
;
1385 // FIXME: only correct for C locale
1386 while (wxIsspace(*nptr
)) nptr
++;
1387 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1388 if (((base
== 0) || (base
== 16)) &&
1389 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1393 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1394 else if (base
== 0) base
= 10;
1396 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1397 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1399 wxString
data(start
, nptr
-start
);
1400 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1401 char *rdat
= wxMBSTRINGCAST dat
;
1402 long int ret
= strtol(dat
, &rdat
, base
);
1404 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1409 WXDLLEXPORT
unsigned long int wxStrtoul(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1411 return (unsigned long int) wxStrtol(nptr
, endptr
, base
);
1414 #endif // wxNEED_WX_STRING_H
1416 #ifdef wxNEED_WX_STDIO_H
1417 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1419 char mode_buffer
[10];
1420 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1421 mode_buffer
[i
] = (char) mode
[i
];
1423 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1426 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1428 char mode_buffer
[10];
1429 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1430 mode_buffer
[i
] = (char) mode
[i
];
1432 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1435 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1437 return remove( wxConvFile
.cWX2MB(path
) );
1440 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1442 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1447 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1452 if (str
.ToDouble(& d
))
1457 return atof(wxConvLibc
.cWX2MB(psz
));
1462 #ifdef wxNEED_WX_STDLIB_H
1463 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1465 return atoi(wxConvLibc
.cWX2MB(psz
));
1468 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1470 return atol(wxConvLibc
.cWX2MB(psz
));
1473 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1476 // NB: buffer returned by getenv() is allowed to be overwritten next
1477 // time getenv() is called, so it is OK to use static string
1478 // buffer to hold the data.
1479 static wxWCharBuffer
value((wxChar
*)NULL
);
1480 value
= wxConvLibc
.cMB2WX(getenv(wxConvLibc
.cWX2MB(name
)));
1481 return value
.data();
1483 return getenv(name
);
1487 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1489 return system(wxConvLibc
.cWX2MB(psz
));
1492 #endif // wxNEED_WX_STDLIB_H
1494 #ifdef wxNEED_WX_TIME_H
1496 wxStrftime(wxChar
*s
, size_t maxsize
, const wxChar
*fmt
, const struct tm
*tm
)
1501 wxCharBuffer
buf(maxsize
);
1503 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
1507 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
1511 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
1515 wxStrncpy(s
, wbuf
, maxsize
);
1518 #endif // wxNEED_WX_TIME_H
1521 WXDLLEXPORT wxChar
*wxCtime(const time_t *timep
)
1523 // normally the string is 26 chars but give one more in case some broken
1524 // DOS compiler decides to use "\r\n" instead of "\n" at the end
1525 static wxChar buf
[27];
1527 // ctime() is guaranteed to return a string containing only ASCII
1528 // characters, as its format is always the same for any locale
1529 wxStrncpy(buf
, wxString::FromAscii(ctime(timep
)), WXSIZEOF(buf
));
1530 buf
[WXSIZEOF(buf
) - 1] = _T('\0');
1536 #endif // wxUSE_WCHAR_T
1538 // ----------------------------------------------------------------------------
1539 // functions which we may need even if !wxUSE_WCHAR_T
1540 // ----------------------------------------------------------------------------
1544 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
1553 psz
+= wxStrspn(psz
, delim
);
1556 *save_ptr
= (wxChar
*)NULL
;
1557 return (wxChar
*)NULL
;
1561 psz
= wxStrpbrk(psz
, delim
);
1564 *save_ptr
= (wxChar
*)NULL
;
1569 *save_ptr
= psz
+ 1;
1577 // ----------------------------------------------------------------------------
1578 // missing C RTL functions
1579 // ----------------------------------------------------------------------------
1583 char *strdup(const char *s
)
1585 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1587 strcpy( dest
, s
) ;
1590 #endif // wxNEED_STRDUP
1592 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1594 void *calloc( size_t num
, size_t size
)
1596 void** ptr
= (void **)malloc(num
* size
);
1597 memset( ptr
, 0, num
* size
);
1601 #endif // __WXWINCE__ <= 211