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)
53 #if defined(__MWERKS__) && __MSL__ >= 0x6000
59 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
61 // assume that we have mbsrtowcs() too if we have wcsrtombs()
64 memset(&mbstate
, 0, sizeof(mbstate_t));
69 if (n
) *buf
= wxT('\0');
73 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
75 return wxMbstowcs(buf
, psz
, n
);
79 // note that we rely on common (and required by Unix98 but unfortunately not
80 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
81 // to just get the size of the needed buffer -- this is needed as otherwise
82 // we have no idea about how much space we need and if the CRT doesn't
83 // support it (the only currently known example being Metrowerks, see
84 // wx/wxchar.h) we don't use its mbstowcs() at all
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
105 #ifdef HAVE_WCSRTOMBS
106 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
108 return wxWcstombs(buf
, pwz
, n
);
112 #ifdef HAVE_WCSRTOMBS
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 #if !defined(wxSnprintf_)
163 int WXDLLEXPORT
wxDoSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
166 va_start(argptr
, format
);
168 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
174 #endif // wxSnprintf_
177 /* Digital Mars adds count to _stprintf (C99) so convert */
179 int wxDoSprintf (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
183 va_start( arglist
, format
);
184 int iLen
= swprintf ( s
, -1, format
, arglist
);
189 #endif // wxUSE_UNICODE
193 // ----------------------------------------------------------------------------
194 // implement the standard IO functions for wide char if libc doesn't have them
195 // ----------------------------------------------------------------------------
198 int wxFputs(const wchar_t *ws
, FILE *stream
)
200 wxCharBuffer
buf(wxConvLibc
.cWC2MB(ws
));
204 // counting the number of wide characters written isn't worth the trouble,
205 // simply distinguish between ok and error
206 return fputs(buf
, stream
) == -1 ? -1 : 0;
208 #endif // wxNEED_FPUTS
211 int wxPuts(const wxChar
*ws
)
213 int rc
= wxFputs(ws
, stdout
);
216 if ( wxFputs(L
"\n", stdout
) == -1 )
224 #endif // wxNEED_PUTS
227 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
229 wchar_t ws
[2] = { wc
, L
'\0' };
231 return wxFputs(ws
, stream
);
233 #endif // wxNEED_PUTC
235 // NB: we only implement va_list functions here, the ones taking ... are
236 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
237 // the definitions there to avoid duplicating them here
238 #ifdef wxNEED_WPRINTF
240 // TODO: implement the scanf() functions
241 int vwscanf(const wxChar
*format
, va_list argptr
)
243 wxFAIL_MSG( _T("TODO") );
248 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
250 // The best we can do without proper Unicode support in glibc is to
251 // convert the strings into MB representation and run ANSI version
252 // of the function. This doesn't work with %c and %s because of difference
253 // in size of char and wchar_t, though.
255 wxCHECK_MSG( wxStrstr(format
, _T("%s")) == NULL
, -1,
256 _T("incomplete vswscanf implementation doesn't allow %s") );
257 wxCHECK_MSG( wxStrstr(format
, _T("%c")) == NULL
, -1,
258 _T("incomplete vswscanf implementation doesn't allow %c") );
261 wxVaCopy(argcopy
, argptr
);
262 return vsscanf(wxConvLibc
.cWX2MB(ws
), wxConvLibc
.cWX2MB(format
), argcopy
);
265 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
267 wxFAIL_MSG( _T("TODO") );
272 #define vswprintf wxVsnprintf_
274 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
277 int rc
= s
.PrintfV(format
, argptr
);
281 // we can't do much better without Unicode support in libc...
282 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
289 int vwprintf(const wxChar
*format
, va_list argptr
)
291 return wxVfprintf(stdout
, format
, argptr
);
294 #endif // wxNEED_WPRINTF
296 #ifdef wxNEED_PRINTF_CONVERSION
298 // ----------------------------------------------------------------------------
299 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
300 // ----------------------------------------------------------------------------
303 Here are the gory details. We want to follow the Windows/MS conventions,
308 format specifier results in
309 -----------------------------------
315 format specifier results in
316 -----------------------------------
321 while on POSIX systems we have %C identical to %lc and %c always means char
322 (in any mode) while %lc always means wchar_t,
324 So to use native functions in order to get our semantics we must do the
325 following translations in Unicode mode (nothing to do in ANSI mode):
327 wxWidgets specifier POSIX specifier
328 ----------------------------------------
334 And, of course, the same should be done for %s as well.
337 class wxFormatConverter
340 wxFormatConverter(const wxChar
*format
);
342 // notice that we only translated the string if m_fmtOrig == NULL (as set
343 // by CopyAllBefore()), otherwise we should simply use the original format
344 operator const wxChar
*() const
345 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
348 // copy another character to the translated format: this function does the
349 // copy if we are translating but doesn't do anything at all if we don't,
350 // so we don't create the translated format string at all unless we really
351 // need to (i.e. InsertFmtChar() is called)
352 wxChar
CopyFmtChar(wxChar ch
)
356 // we're translating, do copy
361 // simply increase the count which should be copied by
362 // CopyAllBefore() later if needed
369 // insert an extra character
370 void InsertFmtChar(wxChar ch
)
374 // so far we haven't translated anything yet
383 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
385 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
387 // we won't need it any longer
391 static bool IsFlagChar(wxChar ch
)
393 return ch
== _T('-') || ch
== _T('+') ||
394 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
397 void SkipDigits(const wxChar
**ptpc
)
399 while ( **ptpc
>= _T('0') && **ptpc
<= _T('9') )
400 CopyFmtChar(*(*ptpc
)++);
403 // the translated format
406 // the original format
407 const wxChar
*m_fmtOrig
;
409 // the number of characters already copied
413 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
420 if ( CopyFmtChar(*format
++) == _T('%') )
423 while ( IsFlagChar(*format
) )
424 CopyFmtChar(*format
++);
426 // and possible width
427 if ( *format
== _T('*') )
428 CopyFmtChar(*format
++);
433 if ( *format
== _T('.') )
435 CopyFmtChar(*format
++);
436 if ( *format
== _T('*') )
437 CopyFmtChar(*format
++);
442 // next we can have a size modifier
458 // "ll" has a different meaning!
459 if ( format
[1] != _T('l') )
471 // and finally we should have the type
476 // %C and %hC -> %c and %lC -> %lc
478 CopyFmtChar(_T('l'));
480 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
485 // %c -> %lc but %hc stays %hc and %lc is still %lc
486 if ( size
== Default
)
487 InsertFmtChar(_T('l'));
491 // nothing special to do
492 if ( size
!= Default
)
493 CopyFmtChar(*(format
- 1));
494 CopyFmtChar(*format
++);
500 #else // !wxNEED_PRINTF_CONVERSION
501 // no conversion necessary
502 #define wxFormatConverter(x) (x)
503 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
506 // For testing the format converter
507 wxString
wxConvertFormat(const wxChar
*format
)
509 return wxString(wxFormatConverter(format
));
513 // ----------------------------------------------------------------------------
514 // wxPrintf(), wxScanf() and relatives
515 // ----------------------------------------------------------------------------
517 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
519 int wxDoScanf( const wxChar
*format
, ... )
522 va_start(argptr
, format
);
524 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
531 int wxDoSscanf( const wxChar
*str
, const wxChar
*format
, ... )
534 va_start(argptr
, format
);
536 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
543 int wxDoFscanf( FILE *stream
, const wxChar
*format
, ... )
546 va_start(argptr
, format
);
547 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
554 int wxDoPrintf( const wxChar
*format
, ... )
557 va_start(argptr
, format
);
559 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
567 int wxDoSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... )
570 va_start(argptr
, format
);
572 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
574 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
575 // doesn't nul terminate on truncation.
584 int wxDoSprintf( wxChar
*str
, const wxChar
*format
, ... )
587 va_start(argptr
, format
);
589 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
590 // it's safe to implement this one in terms of it
591 wxString
s(wxString::FormatV(format
, argptr
));
599 int wxDoFprintf( FILE *stream
, const wxChar
*format
, ... )
602 va_start( argptr
, format
);
604 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
611 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
613 return vswscanf( str
, wxFormatConverter(format
), argptr
);
616 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
618 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
621 int wxVprintf( const wxChar
*format
, va_list argptr
)
623 return vwprintf( wxFormatConverter(format
), argptr
);
627 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
629 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
631 #endif // wxVsnprintf
633 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
635 // same as for wxSprintf()
636 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
639 #endif // wxNEED_PRINTF_CONVERSION
643 // ----------------------------------------------------------------------------
644 // ctype.h stuff (currently unused)
645 // ----------------------------------------------------------------------------
647 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
648 inline WORD
wxMSW_ctype(wxChar ch
)
651 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
655 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
656 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
657 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
658 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
659 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
660 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
661 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
662 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
663 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
664 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
665 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
666 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
667 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
670 #ifdef wxNEED_WX_MBSTOWCS
672 WXDLLEXPORT
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
682 const char* origin
= in
;
684 while (outlen
-- && *in
)
686 *out
++ = (wchar_t) *in
++;
694 WXDLLEXPORT
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
704 const wchar_t* origin
= in
;
706 while (outlen
-- && *in
)
708 *out
++ = (char) *in
++;
716 #endif // wxNEED_WX_MBSTOWCS
718 #if defined(wxNEED_WX_CTYPE_H)
720 #include <CoreFoundation/CoreFoundation.h>
722 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
723 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
724 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
725 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
726 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
727 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
728 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
729 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
730 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
731 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
733 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalnumset
, ch
); }
734 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalphaset
, ch
); }
735 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
736 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfdigitset
, ch
); }
737 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
) && ch
!= ' '; }
738 WXDLLEXPORT
int wxIslower(wxChar ch
) { return CFCharacterSetIsCharacterMember(cflowerset
, ch
); }
739 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
740 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfpunctset
, ch
); }
741 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfspaceset
, ch
); }
742 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfupperset
, ch
); }
743 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxIsdigit(ch
) || (ch
>='a' && ch
<='f') || (ch
>='A' && ch
<='F'); }
744 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)tolower((char)(ch
)); }
745 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)toupper((char)(ch
)); }
747 #endif // wxNEED_WX_CTYPE_H
751 WXDLLEXPORT
char *wxStrdupA(const char *s
)
753 return strcpy((char *)malloc(strlen(s
) + 1), s
);
760 WXDLLEXPORT
wchar_t * wxStrdupW(const wchar_t *pwz
)
762 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
763 wchar_t *ret
= (wchar_t *) malloc(size
);
764 memcpy(ret
, pwz
, size
);
771 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
773 register wxChar c1
, c2
;
775 c1
= wxTolower(*psz1
++);
776 c2
= wxTolower(*psz2
++);
777 } while ( c1
&& (c1
== c2
) );
783 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
785 // initialize the variables just to suppress stupid gcc warning
786 register wxChar c1
= 0, c2
= 0;
787 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
789 if (c1
< c2
) return -1;
790 if (c1
> c2
) return 1;
797 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
799 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
801 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
805 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
806 WXDLLEXPORT
size_t wxWcslen(const wchar_t *s
)
816 // ----------------------------------------------------------------------------
817 // string.h functions
818 // ----------------------------------------------------------------------------
820 #ifdef wxNEED_WX_STRING_H
822 // RN: These need to be c externed for the regex lib
827 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
830 while (*dest
) dest
++;
831 while ((*dest
++ = *src
++));
835 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
837 // be careful here as the terminating NUL makes part of the string
847 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
849 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
850 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
851 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
855 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
858 while ((*dest
++ = *src
++));
862 WXDLLEXPORT
size_t wxStrlen_(const wxChar
*s
)
872 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
875 while (*dest
) dest
++;
876 while (n
&& (*dest
++ = *src
++)) n
--;
880 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
882 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
884 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
885 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
890 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
893 while (n
&& (*dest
++ = *src
++)) n
--;
894 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
898 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
900 while (*s
&& !wxStrchr(accept
, *s
))
903 return *s
? s
: NULL
;
906 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
908 const wxChar
*ret
= NULL
;
920 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
923 while (wxStrchr(accept
, *s
++)) len
++;
927 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
929 wxASSERT_MSG( needle
!= NULL
, _T("NULL argument in wxStrstr") );
931 // VZ: this is not exactly the most efficient string search algorithm...
933 const size_t len
= wxStrlen(needle
);
935 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
937 if ( !wxStrncmp(fnd
, needle
, len
) )
950 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
952 const wxChar
*start
= nptr
;
954 // FIXME: only correct for C locale
955 while (wxIsspace(*nptr
)) nptr
++;
956 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
957 while (wxIsdigit(*nptr
)) nptr
++;
958 if (*nptr
== wxT('.')) {
960 while (wxIsdigit(*nptr
)) nptr
++;
962 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
964 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
965 while (wxIsdigit(*nptr
)) nptr
++;
968 wxString
data(nptr
, nptr
-start
);
969 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
970 char *rdat
= wxMBSTRINGCAST dat
;
971 double ret
= strtod(dat
, &rdat
);
973 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
978 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
980 const wxChar
*start
= nptr
;
982 // FIXME: only correct for C locale
983 while (wxIsspace(*nptr
)) nptr
++;
984 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
985 if (((base
== 0) || (base
== 16)) &&
986 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
990 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
991 else if (base
== 0) base
= 10;
993 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
994 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
996 wxString
data(start
, nptr
-start
);
997 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
998 char *rdat
= wxMBSTRINGCAST dat
;
999 long int ret
= strtol(dat
, &rdat
, base
);
1001 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1006 WXDLLEXPORT
unsigned long int wxStrtoul(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1008 return (unsigned long int) wxStrtol(nptr
, endptr
, base
);
1011 #endif // wxNEED_WX_STRING_H
1013 #ifdef wxNEED_WX_STDIO_H
1014 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1016 char mode_buffer
[10];
1017 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1018 mode_buffer
[i
] = (char) mode
[i
];
1020 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1023 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1025 char mode_buffer
[10];
1026 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1027 mode_buffer
[i
] = (char) mode
[i
];
1029 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1032 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1034 return remove( wxConvFile
.cWX2MB(path
) );
1037 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1039 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1044 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1049 if (str
.ToDouble(& d
))
1054 return atof(wxConvLibc
.cWX2MB(psz
));
1059 #ifdef wxNEED_WX_STDLIB_H
1060 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1062 return atoi(wxConvLibc
.cWX2MB(psz
));
1065 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1067 return atol(wxConvLibc
.cWX2MB(psz
));
1070 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1073 // NB: buffer returned by getenv() is allowed to be overwritten next
1074 // time getenv() is called, so it is OK to use static string
1075 // buffer to hold the data.
1076 static wxWCharBuffer
value((wxChar
*)NULL
);
1077 value
= wxConvLibc
.cMB2WX(getenv(wxConvLibc
.cWX2MB(name
)));
1078 return value
.data();
1080 return getenv(name
);
1084 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1086 return system(wxConvLibc
.cWX2MB(psz
));
1089 #endif // wxNEED_WX_STDLIB_H
1091 #ifdef wxNEED_WX_TIME_H
1093 wxStrftime(wxChar
*s
, size_t maxsize
, const wxChar
*fmt
, const struct tm
*tm
)
1098 wxCharBuffer
buf(maxsize
);
1100 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
1104 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
1108 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
1112 wxStrncpy(s
, wbuf
, maxsize
);
1115 #endif // wxNEED_WX_TIME_H
1118 WXDLLEXPORT wxChar
*wxCtime(const time_t *timep
)
1120 // normally the string is 26 chars but give one more in case some broken
1121 // DOS compiler decides to use "\r\n" instead of "\n" at the end
1122 static wxChar buf
[27];
1124 // ctime() is guaranteed to return a string containing only ASCII
1125 // characters, as its format is always the same for any locale
1126 wxStrncpy(buf
, wxString::FromAscii(ctime(timep
)), WXSIZEOF(buf
));
1127 buf
[WXSIZEOF(buf
) - 1] = _T('\0');
1133 #endif // wxUSE_WCHAR_T
1135 // ----------------------------------------------------------------------------
1136 // functions which we may need even if !wxUSE_WCHAR_T
1137 // ----------------------------------------------------------------------------
1141 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
1150 psz
+= wxStrspn(psz
, delim
);
1153 *save_ptr
= (wxChar
*)NULL
;
1154 return (wxChar
*)NULL
;
1158 psz
= wxStrpbrk(psz
, delim
);
1161 *save_ptr
= (wxChar
*)NULL
;
1166 *save_ptr
= psz
+ 1;
1174 // ----------------------------------------------------------------------------
1175 // missing C RTL functions
1176 // ----------------------------------------------------------------------------
1178 #ifdef wxNEED_STRDUP
1180 char *strdup(const char *s
)
1182 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1184 strcpy( dest
, s
) ;
1187 #endif // wxNEED_STRDUP
1189 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1191 void *calloc( size_t num
, size_t size
)
1193 void** ptr
= (void **)malloc(num
* size
);
1194 memset( ptr
, 0, num
* size
);
1198 #endif // __WXWINCE__ <= 211
1202 int wxRemove(const wxChar
*path
)
1204 return ::DeleteFile(path
) == 0;