1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxChar implementation
8 // Copyright: (c) wxWindows copyright
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
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/wxchar.h"
39 #include "wx/string.h"
43 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
51 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
55 if (n
) *buf
= wxT('\0');
58 return mbstowcs(buf
, psz
, n
);
61 // assume that we have mbsrtowcs() too if we have wcsrtombs()
64 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
66 return mbstowcs((wchar_t *) NULL
, psz
, 0);
70 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
74 // glibc2.1 chokes on null input
78 return wcstombs(buf
, pwz
, n
);
83 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
85 return wcstombs((char *) NULL
, pwz
, 0);
88 #endif // wxUSE_WCHAR_T
90 bool WXDLLEXPORT
wxOKlibc()
92 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
93 // glibc 2.0 uses UTF-8 even when it shouldn't
95 if ((MB_CUR_MAX
== 2) &&
96 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
98 // this is UTF-8 allright, check whether that's what we want
99 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
100 if ((strlen(cur_locale
) < 4) ||
101 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
102 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
103 // nope, don't use libc conversion
111 // ============================================================================
112 // printf() functions business
113 // ============================================================================
115 // special test mode: define all functions below even if we don't really need
116 // them to be able to test them
127 #define wxNEED_WPRINTF
129 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
132 // ----------------------------------------------------------------------------
133 // implement [v]snprintf() if the system doesn't provide a safe one
134 // ----------------------------------------------------------------------------
136 #if !defined(wxVsnprintf_)
137 int WXDLLEXPORT
wxVsnprintf_(wxChar
*buf
, size_t lenMax
,
138 const wxChar
*format
, va_list argptr
)
140 // buffer to avoid dynamic memory allocation each time for small strings
141 char szScratch
[1024];
143 // number of characters in the buffer so far, must be less than lenMax
146 for ( size_t n
= 0; ; n
++ )
148 const wxChar chCur
= format
[n
];
150 if ( chCur
== wxT('%') )
152 static char s_szFlags
[256] = "%";
154 bool adj_left
= FALSE
,
159 size_t min_width
= 0,
160 max_width
= wxSTRING_MAXLEN
;
165 if (in_prec && !prec_dot) \
167 s_szFlags[flagofs++] = '.'; \
171 #define APPEND_CH(ch) \
172 if ( lenCur == lenMax ) \
177 #define APPEND_STR(s) \
179 for ( const wxChar *p = s; *p; p++ ) \
186 const wxChar ch
= format
[++n
];
206 s_szFlags
[flagofs
++] = ch
;
212 s_szFlags
[flagofs
++] = ch
;
220 // dot will be auto-added to s_szFlags if non-negative
227 s_szFlags
[flagofs
++] = ch
;
233 s_szFlags
[flagofs
++] = ch
;
240 s_szFlags
[flagofs
++] = ch
;
246 s_szFlags
[flagofs
++] = ch
;
251 int len
= va_arg(argptr
, int);
262 adj_left
= !adj_left
;
263 s_szFlags
[flagofs
++] = '-';
268 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
272 case wxT('1'): case wxT('2'): case wxT('3'):
273 case wxT('4'): case wxT('5'): case wxT('6'):
274 case wxT('7'): case wxT('8'): case wxT('9'):
278 while ( (format
[n
] >= wxT('0')) &&
279 (format
[n
] <= wxT('9')) )
281 s_szFlags
[flagofs
++] = format
[n
];
282 len
= len
*10 + (format
[n
] - wxT('0'));
291 n
--; // the main loop pre-increments n again
302 s_szFlags
[flagofs
++] = ch
;
303 s_szFlags
[flagofs
] = '\0';
306 int val
= va_arg(argptr
, int);
307 ::sprintf(szScratch
, s_szFlags
, val
);
311 // NB: 'short int' value passed through '...'
312 // is promoted to 'int', so we have to get
313 // an int from stack even if we need a short
314 short int val
= (short int) va_arg(argptr
, int);
315 ::sprintf(szScratch
, s_szFlags
, val
);
319 long int val
= va_arg(argptr
, long int);
320 ::sprintf(szScratch
, s_szFlags
, val
);
325 long long int val
= va_arg(argptr
, long long int);
326 ::sprintf(szScratch
, s_szFlags
, val
);
328 long int val
= va_arg(argptr
, long int);
329 ::sprintf(szScratch
, s_szFlags
, val
);
330 #endif // long long/!long long
334 size_t val
= va_arg(argptr
, size_t);
335 ::sprintf(szScratch
, s_szFlags
, val
);
339 const wxMB2WXbuf tmp
=
340 wxConvLibc
.cMB2WX(szScratch
);
353 s_szFlags
[flagofs
++] = ch
;
354 s_szFlags
[flagofs
] = '\0';
357 long double val
= va_arg(argptr
, long double);
358 ::sprintf(szScratch
, s_szFlags
, val
);
362 double val
= va_arg(argptr
, double);
363 ::sprintf(szScratch
, s_szFlags
, val
);
367 const wxMB2WXbuf tmp
=
368 wxConvLibc
.cMB2WX(szScratch
);
377 void *val
= va_arg(argptr
, void *);
379 s_szFlags
[flagofs
++] = ch
;
380 s_szFlags
[flagofs
] = '\0';
381 ::sprintf(szScratch
, s_szFlags
, val
);
383 const wxMB2WXbuf tmp
=
384 wxConvLibc
.cMB2WX(szScratch
);
393 wxChar val
= va_arg(argptr
, int);
394 // we don't need to honor padding here, do we?
404 // wx extension: we'll let %hs mean non-Unicode
406 char *val
= va_arg(argptr
, char *);
408 // ASCII->Unicode constructor handles max_width
410 wxString
s(val
, wxConvLibc
, max_width
);
412 size_t len
= wxSTRING_MAXLEN
;
416 val
[len
] && (len
< max_width
);
422 wxString
s(val
, len
);
424 if (s
.Len() < min_width
)
425 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
431 wxChar
*val
= va_arg(argptr
, wxChar
*);
432 size_t len
= wxSTRING_MAXLEN
;
436 val
[len
] && (len
< max_width
);
443 wxString
s(val
, len
);
444 if (s
.Len() < min_width
)
445 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
455 int *val
= va_arg(argptr
, int *);
460 short int *val
= va_arg(argptr
, short int *);
465 long int *val
= va_arg(argptr
, long int *);
472 // bad format, leave unchanged
498 #endif // !wxVsnprintfA
500 #if !defined(wxSnprintf_)
501 int WXDLLEXPORT
wxSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
504 va_start(argptr
, format
);
506 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
512 #endif // wxSnprintf_
514 // ----------------------------------------------------------------------------
515 // implement the standard IO functions for wide char if libc doesn't have them
516 // ----------------------------------------------------------------------------
520 int wxFputs(const wchar_t *ws
, FILE *stream
)
522 // counting the number of wide characters written isn't worth the trouble,
523 // simply distinguish between ok and error
524 return fputs(wxConvLibc
.cWC2MB(ws
), stream
) == -1 ? -1 : 0;
527 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
529 wchar_t ws
[2] = { wc
, L
'\0' };
531 return wxFputs(ws
, stream
);
534 #endif // wxNEED_FPUTWC
536 // NB: we only implement va_list functions here, the ones taking ... are
537 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
538 // the definitions there to avoid duplicating them here
539 #ifdef wxNEED_WPRINTF
541 // TODO: implement the scanf() functions
542 int vwscanf(const wxChar
*format
, va_list argptr
)
544 wxFAIL_MSG( _T("TODO") );
549 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
551 wxFAIL_MSG( _T("TODO") );
556 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
558 wxFAIL_MSG( _T("TODO") );
563 #define vswprintf wxVsnprintf_
565 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
568 int rc
= s
.PrintfV(format
, argptr
);
572 // we can't do much better without Unicode support in libc...
573 if ( fprintf(stream
, "%s", s
.mb_str()) == -1 )
580 int vwprintf(const wxChar
*format
, va_list argptr
)
582 return wxVfprintf(stdout
, format
, argptr
);
585 #endif // wxNEED_WPRINTF
587 #ifdef wxNEED_PRINTF_CONVERSION
589 // ----------------------------------------------------------------------------
590 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
591 // ----------------------------------------------------------------------------
594 Here are the gory details. We want to follow the Windows/MS conventions,
599 format specifier results in
600 -----------------------------------
606 format specifier results in
607 -----------------------------------
612 while on POSIX systems we have %C identical to %lc and %c always means char
613 (in any mode) while %lc always means wchar_t,
615 So to use native functions in order to get our semantics we must do the
616 following translations in Unicode mode (nothing to do in ANSI mode):
618 wxWindows specifier POSIX specifier
619 ----------------------------------------
625 And, of course, the same should be done for %s as well.
628 class wxFormatConverter
631 wxFormatConverter(const wxChar
*format
);
633 // notice that we only translated the string if m_fmtOrig == NULL (as set
634 // by CopyAllBefore()), otherwise we should simply use the original format
635 operator const wxChar
*() const
636 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
639 // copy another character to the translated format: this function does the
640 // copy if we are translating but doesn't do anything at all if we don't,
641 // so we don't create the translated format string at all unless we really
642 // need to (i.e. InsertFmtChar() is called)
643 wxChar
CopyFmtChar(wxChar ch
)
647 // we're translating, do copy
652 // simply increase the count which should be copied by
653 // CopyAllBefore() later if needed
660 // insert an extra character
661 void InsertFmtChar(wxChar ch
)
665 // so far we haven't translated anything yet
674 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
676 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
678 // we won't need it any longer
682 static bool IsFlagChar(wxChar ch
)
684 return ch
== _T('-') || ch
== _T('+') ||
685 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
688 void SkipDigits(const wxChar
**ppc
)
690 while ( **ppc
>= _T('0') && **ppc
<= _T('9') )
691 CopyFmtChar(*(*ppc
)++);
694 // the translated format
697 // the original format
698 const wxChar
*m_fmtOrig
;
700 // the number of characters already copied
704 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
711 if ( CopyFmtChar(*format
++) == _T('%') )
714 while ( IsFlagChar(*format
) )
715 CopyFmtChar(*format
++);
717 // and possible width
718 if ( *format
== _T('*') )
719 CopyFmtChar(*format
++);
724 if ( *format
== _T('.') )
729 // next we can have a size modifier
745 // "ll" has a different meaning!
746 if ( format
[1] != _T('l') )
758 // and finally we should have the type
763 // %C and %hC -> %c and %lC -> %lc
765 CopyFmtChar(_T('l'));
767 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
772 // %c -> %lc but %hc stays %hc and %lc is still %lc
776 InsertFmtChar(_T('l'));
780 CopyFmtChar(_T('h'));
789 // nothing special to do
790 CopyFmtChar(*format
++);
796 #else // !wxNEED_PRINTF_CONVERSION
797 // no conversion necessary
798 #define wxFormatConverter(x) (x)
799 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
801 // ----------------------------------------------------------------------------
802 // wxPrintf(), wxScanf() and relatives
803 // ----------------------------------------------------------------------------
805 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
807 int wxScanf( const wxChar
*format
, ... )
810 va_start(argptr
, format
);
812 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
819 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... )
822 va_start(argptr
, format
);
824 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
831 int wxFscanf( FILE *stream
, const wxChar
*format
, ... )
834 va_start(argptr
, format
);
836 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
843 int wxPrintf( const wxChar
*format
, ... )
846 va_start(argptr
, format
);
848 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
856 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... )
859 va_start(argptr
, format
);
861 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
869 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... )
872 va_start(argptr
, format
);
874 // callers of wxSprintf() deserve what they get
875 int ret
= vswprintf( str
, UINT_MAX
, wxFormatConverter(format
), argptr
);
882 int wxFprintf( FILE *stream
, const wxChar
*format
, ... )
885 va_start( argptr
, format
);
887 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
894 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
896 return vswscanf( str
, wxFormatConverter(format
), argptr
);
899 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
901 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
904 int wxVprintf( const wxChar
*format
, va_list argptr
)
906 return vwprintf( wxFormatConverter(format
), argptr
);
910 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
912 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
914 #endif // wxVsnprintf
916 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
918 // same as for wxSprintf()
919 return vswprintf(str
, UINT_MAX
, wxFormatConverter(format
), argptr
);
922 #endif // wxNEED_PRINTF_CONVERSION
924 // ----------------------------------------------------------------------------
925 // ctype.h stuff (currently unused)
926 // ----------------------------------------------------------------------------
928 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
929 inline WORD
wxMSW_ctype(wxChar ch
)
932 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
936 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
937 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
938 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
939 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
940 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
941 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
942 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
943 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
944 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
945 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
946 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
947 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
948 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
952 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
954 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
955 wxChar
*ret
= (wxChar
*) malloc(size
);
956 memcpy(ret
, psz
, size
);
962 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
964 register wxChar c1
, c2
;
966 c1
= wxTolower(*psz1
++);
967 c2
= wxTolower(*psz2
++);
968 } while ( c1
&& (c1
== c2
) );
974 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
976 register wxChar c1
, c2
;
977 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
979 if (c1
< c2
) return -1;
980 if (c1
> c2
) return 1;
987 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
996 psz
+= wxStrspn(psz
, delim
);
999 *save_ptr
= (wxChar
*)NULL
;
1000 return (wxChar
*)NULL
;
1004 psz
= wxStrpbrk(psz
, delim
);
1007 *save_ptr
= (wxChar
*)NULL
;
1012 *save_ptr
= psz
+ 1;
1020 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
1022 char *localeOld
= setlocale(category
, wxConvLocal
.cWX2MB(locale
));
1024 return wxWCharBuffer(wxConvLocal
.cMB2WC(localeOld
));
1028 // ----------------------------------------------------------------------------
1029 // string.h functions
1030 // ----------------------------------------------------------------------------
1032 #ifdef wxNEED_WX_STRING_H
1033 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
1036 while (*dest
) dest
++;
1037 while ((*dest
++ = *src
++));
1041 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
1043 // be careful here as the terminating NUL makes part of the string
1053 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
1055 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
1056 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1057 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1061 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
1064 while ((*dest
++ = *src
++));
1068 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1071 while (*dest
) dest
++;
1072 while (n
&& (*dest
++ = *src
++)) n
--;
1076 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1078 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1080 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1081 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1086 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1089 while (n
&& (*dest
++ = *src
++)) n
--;
1090 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1094 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1096 while (*s
&& !wxStrchr(accept
, *s
))
1099 return *s
? s
: NULL
;
1102 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1104 const wxChar
*ret
= NULL
;
1116 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1119 while (wxStrchr(accept
, *s
++)) len
++;
1123 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1125 wxCHECK_RET( needle
, NULL
, _T("NULL argument in wxStrstr") );
1127 // VZ: this is not exactly the most efficient string search algorithm...
1129 const size_t len
= wxStrlen(needle
);
1131 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1133 if ( !wxStrncmp(fnd
, needle
, len
) )
1142 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1144 const wxChar
*start
= nptr
;
1146 // FIXME: only correct for C locale
1147 while (wxIsspace(*nptr
)) nptr
++;
1148 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1149 while (wxIsdigit(*nptr
)) nptr
++;
1150 if (*nptr
== wxT('.')) {
1152 while (wxIsdigit(*nptr
)) nptr
++;
1154 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1156 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1157 while (wxIsdigit(*nptr
)) nptr
++;
1160 wxString
data(nptr
, nptr
-start
);
1161 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1162 char *rdat
= wxMBSTRINGCAST dat
;
1163 double ret
= strtod(dat
, &rdat
);
1165 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1170 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1172 const wxChar
*start
= nptr
;
1174 // FIXME: only correct for C locale
1175 while (wxIsspace(*nptr
)) nptr
++;
1176 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1177 if (((base
== 0) || (base
== 16)) &&
1178 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1182 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1183 else if (base
== 0) base
= 10;
1185 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1186 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1188 wxString
data(nptr
, nptr
-start
);
1189 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1190 char *rdat
= wxMBSTRINGCAST dat
;
1191 long int ret
= strtol(dat
, &rdat
, base
);
1193 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1197 #endif // wxNEED_WX_STRING_H
1199 #ifdef wxNEED_WX_STDIO_H
1200 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1202 char mode_buffer
[10];
1203 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1204 mode_buffer
[i
] = (char) mode
[i
];
1206 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1209 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1211 char mode_buffer
[10];
1212 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1213 mode_buffer
[i
] = (char) mode
[i
];
1215 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1218 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1220 return remove( wxConvFile
.cWX2MB(path
) );
1223 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1225 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1230 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1232 return atof(wxConvLocal
.cWX2MB(psz
));
1236 #ifdef wxNEED_WX_STDLIB_H
1237 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1239 return atoi(wxConvLocal
.cWX2MB(psz
));
1242 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1244 return atol(wxConvLocal
.cWX2MB(psz
));
1247 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1249 static wxHashTable env
;
1251 // check if we already have stored the converted env var
1252 wxObject
*data
= env
.Get(name
);
1255 // nope, retrieve it,
1257 wxCharBuffer buffer
= wxConvLocal
.cWX2MB(name
);
1258 // printf( "buffer %s\n", (const char*) buffer );
1259 const char *val
= getenv( (const char *)buffer
);
1261 const char *val
= getenv( name
);
1264 if (!val
) return (wxChar
*)NULL
;
1265 // printf( "home %s\n", val );
1268 #ifdef wxUSE_UNICODE
1269 data
= (wxObject
*)new wxString(val
, wxConvLocal
);
1271 data
= (wxObject
*)new wxString(val
);
1275 env
.Put(name
, data
);
1277 // return converted env var
1278 return (wxChar
*)((wxString
*)data
)->c_str();
1281 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1283 return system(wxConvLocal
.cWX2MB(psz
));
1288 #ifdef wxNEED_WX_TIME_H
1289 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
1293 char *buf
= (char *)malloc(max
);
1294 size_t ret
= strftime(buf
, max
, wxConvLocal
.cWX2MB(fmt
), tm
);
1297 wxStrcpy(s
, wxConvLocal
.cMB2WX(buf
));