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
112 size_t WXDLLEXPORT
wcslen(const wchar_t *s
)
115 while (s
[len
]) len
++;
120 // ============================================================================
121 // printf() functions business
122 // ============================================================================
124 // ----------------------------------------------------------------------------
125 // implement [v]snprintf() if the system doesn't provide a safe one
126 // ----------------------------------------------------------------------------
128 #if !defined(wxVsnprintf_)
129 int WXDLLEXPORT
wxVsnprintf_(wxChar
*buf
, size_t lenMax
,
130 const wxChar
*format
, va_list argptr
)
132 // buffer to avoid dynamic memory allocation each time for small strings
133 char szScratch
[1024];
135 // number of characters in the buffer so far, must be less than lenMax
138 for (size_t n
= 0; format
[n
]; n
++)
140 if (format
[n
] == wxT('%')) {
141 static char s_szFlags
[256] = "%";
143 bool adj_left
= FALSE
,
148 size_t min_width
= 0,
149 max_width
= wxSTRING_MAXLEN
;
153 if (in_prec && !prec_dot) \
155 s_szFlags[flagofs++] = '.'; \
159 #define APPEND_CH(ch) \
160 if ( lenCur == lenMax ) \
165 #define APPEND_STR(s) \
167 for ( const wxChar *p = s; *p; p++ ) \
173 switch (format
[++n
]) {
191 s_szFlags
[flagofs
++] = format
[n
];
197 s_szFlags
[flagofs
++] = format
[n
];
205 // dot will be auto-added to s_szFlags if non-negative number follows
211 s_szFlags
[flagofs
++] = format
[n
];
217 s_szFlags
[flagofs
++] = format
[n
];
224 s_szFlags
[flagofs
++] = format
[n
];
230 s_szFlags
[flagofs
++] = format
[n
];
235 int len
= va_arg(argptr
, int);
242 adj_left
= !adj_left
;
243 s_szFlags
[flagofs
++] = '-';
248 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
252 case wxT('1'): case wxT('2'): case wxT('3'):
253 case wxT('4'): case wxT('5'): case wxT('6'):
254 case wxT('7'): case wxT('8'): case wxT('9'):
258 while ((format
[n
]>=wxT('0')) && (format
[n
]<=wxT('9'))) {
259 s_szFlags
[flagofs
++] = format
[n
];
260 len
= len
*10 + (format
[n
] - wxT('0'));
263 if (in_prec
) max_width
= len
;
264 else min_width
= len
;
265 n
--; // the main loop pre-increments n again
276 s_szFlags
[flagofs
++] = format
[n
];
277 s_szFlags
[flagofs
] = '\0';
279 int val
= va_arg(argptr
, int);
280 ::sprintf(szScratch
, s_szFlags
, val
);
282 else if (ilen
== -1) {
283 // NB: 'short int' value passed through '...'
284 // is promoted to 'int'
285 short int val
= (short int) va_arg(argptr
, int);
286 ::sprintf(szScratch
, s_szFlags
, val
);
288 else if (ilen
== 1) {
289 long int val
= va_arg(argptr
, long int);
290 ::sprintf(szScratch
, s_szFlags
, val
);
292 else if (ilen
== 2) {
294 long long int val
= va_arg(argptr
, long long int);
295 ::sprintf(szScratch
, s_szFlags
, val
);
297 long int val
= va_arg(argptr
, long int);
298 ::sprintf(szScratch
, s_szFlags
, val
);
301 else if (ilen
== 3) {
302 size_t val
= va_arg(argptr
, size_t);
303 ::sprintf(szScratch
, s_szFlags
, val
);
306 wxMB2WXbuf tmp
= wxConvLibc
.cMB2WX(szScratch
);
319 s_szFlags
[flagofs
++] = format
[n
];
320 s_szFlags
[flagofs
] = '\0';
322 long double val
= va_arg(argptr
, long double);
323 ::sprintf(szScratch
, s_szFlags
, val
);
325 double val
= va_arg(argptr
, double);
326 ::sprintf(szScratch
, s_szFlags
, val
);
329 wxMB2WXbuf tmp
= wxConvLibc
.cMB2WX(szScratch
);
338 void *val
= va_arg(argptr
, void *);
340 s_szFlags
[flagofs
++] = format
[n
];
341 s_szFlags
[flagofs
] = '\0';
342 ::sprintf(szScratch
, s_szFlags
, val
);
344 wxMB2WXbuf tmp
= wxConvLibc
.cMB2WX(szScratch
);
353 wxChar val
= va_arg(argptr
, int);
354 // we don't need to honor padding here, do we?
363 // wx extension: we'll let %hs mean non-Unicode strings
364 char *val
= va_arg(argptr
, char *);
366 // ASCII->Unicode constructor handles max_width right
367 wxString
s(val
, wxConvLibc
, max_width
);
369 size_t len
= wxSTRING_MAXLEN
;
371 for (len
= 0; val
[len
] && (len
<max_width
); len
++);
372 } else val
= wxT("(null)");
373 wxString
s(val
, len
);
375 if (s
.Len() < min_width
)
376 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
380 wxChar
*val
= va_arg(argptr
, wxChar
*);
381 size_t len
= wxSTRING_MAXLEN
;
383 for (len
= 0; val
[len
] && (len
<max_width
); len
++);
384 } else val
= wxT("(null)");
385 wxString
s(val
, len
);
386 if (s
.Len() < min_width
)
387 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
396 int *val
= va_arg(argptr
, int *);
399 else if (ilen
== -1) {
400 short int *val
= va_arg(argptr
, short int *);
403 else if (ilen
>= 1) {
404 long int *val
= va_arg(argptr
, long int *);
411 if (wxIsalpha(format
[n
]))
412 // probably some flag not taken care of here yet
413 s_szFlags
[flagofs
++] = format
[n
];
416 APPEND_CH(_T('%')); // just to pass the glibc tst-printf.c
426 APPEND_CH(format
[n
]);
437 #endif // !wxVsnprintfA
439 #if !defined(wxSnprintf_)
440 int WXDLLEXPORT
wxSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
443 va_start(argptr
, format
);
445 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
451 #endif // wxSnprintf_
453 // ----------------------------------------------------------------------------
454 // implement the standard IO functions for wide char if libc doesn't have them
455 // ----------------------------------------------------------------------------
459 int wxFputs(const wchar_t *ws
, FILE *stream
)
461 // counting the number of wide characters written isn't worth the trouble,
462 // simply distinguish between ok and error
463 return fputs(wxConvLibc
.cWC2MB(ws
), stream
) == -1 ? -1 : 0;
466 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
468 wchar_t ws
[2] = { wc
, L
'\0' };
470 return wxFputs(ws
, stream
);
473 #endif // wxNEED_FPUTWC
475 // NB: we only implement va_list functions here, the ones taking ... are
476 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
477 // the definitions there to avoid duplicating them here
478 #ifdef wxNEED_WPRINTF
480 // TODO: implement the scanf() functions
481 int vwscanf(const wchar_t *format
, va_list argptr
)
483 wxFAIL_MSG( _T("TODO") );
488 int vswscanf(const wchar_t *ws
, const wchar_t *format
, va_list argptr
)
490 wxFAIL_MSG( _T("TODO") );
495 int vfwscanf(FILE *stream
, const wchar_t *format
, va_list argptr
)
497 wxFAIL_MSG( _T("TODO") );
502 #define vswprintf wxVsnprintf_
504 int vfwprintf(FILE *stream
, const wchar_t *format
, va_list argptr
)
507 int rc
= s
.PrintfV(format
, argptr
);
511 // we can't do much better without Unicode support in libc...
512 if ( fprintf(stream
, s
.mb_str()) == -1 )
519 int vwprintf(const wchar_t *format
, va_list argptr
)
521 return wxVfprintf(stdout
, format
, argptr
);
524 #endif // wxNEED_WPRINTF
526 #ifdef wxNEED_PRINTF_CONVERSION
528 // ----------------------------------------------------------------------------
529 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
530 // ----------------------------------------------------------------------------
533 Here are the gory details. We want to follow the Windows/MS conventions,
538 format specifier results in
539 -----------------------------------
545 format specifier results in
546 -----------------------------------
551 while on POSIX systems we have %C identical to %lc and %c always means char
552 (in any mode) while %lc always means wchar_t,
554 So to use native functions in order to get our semantics we must do the
555 following translations in Unicode mode (nothing to do in ANSI mode):
557 wxWindows specifier POSIX specifier
558 ----------------------------------------
564 And, of course, the same should be done for %s as well.
567 class wxFormatConverter
570 wxFormatConverter(const wxChar
*format
);
572 // notice that we only translated the string if m_fmtOrig == NULL (as set
573 // by CopyAllBefore()), otherwise we should simply use the original format
574 operator const wxChar
*() const
575 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
578 // copy another character to the translated format: this function does the
579 // copy if we are translating but doesn't do anything at all if we don't,
580 // so we don't create the translated format string at all unless we really
581 // need to (i.e. InsertFmtChar() is called)
582 wxChar
CopyFmtChar(wxChar ch
)
586 // we're translating, do copy
591 // simply increase the count which should be copied by
592 // CopyAllBefore() later if needed
599 // insert an extra character
600 void InsertFmtChar(wxChar ch
)
604 // so far we haven't translated anything yet
613 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
615 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
617 // we won't need it any longer
621 static bool IsFlagChar(wxChar ch
)
623 return ch
== _T('-') || ch
== _T('+') ||
624 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
627 void SkipDigits(const wxChar
**ppc
)
629 while ( **ppc
>= _T('0') && **ppc
<= _T('9') )
630 CopyFmtChar(*(*ppc
)++);
633 // the translated format
636 // the original format
637 const wxChar
*m_fmtOrig
;
639 // the number of characters already copied
643 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
650 if ( CopyFmtChar(*format
++) == _T('%') )
653 while ( IsFlagChar(*format
) )
654 CopyFmtChar(*format
++);
656 // and possible width
657 if ( *format
== _T('*') )
658 CopyFmtChar(*format
++);
663 if ( *format
== _T('.') )
668 // next we can have a size modifier
684 // "ll" has a different meaning!
685 if ( format
[1] != _T('l') )
697 // and finally we should have the type
702 // %C and %hC -> %c and %lC -> %lc
704 CopyFmtChar(_T('l'));
706 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
711 // %c -> %lc but %hc stays %hc and %lc is still %lc
715 InsertFmtChar(_T('l'));
719 CopyFmtChar(_T('h'));
728 // nothing special to do
729 CopyFmtChar(*format
++);
735 #else // !wxNEED_PRINTF_CONVERSION
736 // no conversion necessary
737 #define wxFormatConverter(x) (x)
738 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
740 // ----------------------------------------------------------------------------
741 // wxPrintf(), wxScanf() and relatives
742 // ----------------------------------------------------------------------------
744 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
746 int wxScanf( const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_2
749 va_start(argptr
, format
);
751 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
758 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
761 va_start(argptr
, format
);
763 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
770 int wxFscanf( FILE *stream
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
773 va_start(argptr
, format
);
775 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
782 int wxPrintf( const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_2
785 va_start(argptr
, format
);
787 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
795 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_4
798 va_start(argptr
, format
);
800 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
808 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
811 va_start(argptr
, format
);
813 // callers of wxSprintf() deserve what they get
814 int ret
= vswprintf( str
, UINT_MAX
, wxFormatConverter(format
), argptr
);
821 int wxFprintf( FILE *stream
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
824 va_start( argptr
, format
);
826 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
833 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
835 return vswscanf( str
, wxFormatConverter(format
), argptr
);
838 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
840 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
843 int wxVprintf( const wxChar
*format
, va_list argptr
)
845 return vwprintf( wxFormatConverter(format
), argptr
);
849 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
851 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
853 #endif // wxVsnprintf
855 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
857 // same as for wxSprintf()
858 return vswprintf(str
, UINT_MAX
, wxFormatConverter(format
), argptr
);
861 #endif // wxNEED_PRINTF_CONVERSION
863 // ----------------------------------------------------------------------------
864 // ctype.h stuff (currently unused)
865 // ----------------------------------------------------------------------------
867 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
868 inline WORD
wxMSW_ctype(wxChar ch
)
871 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
875 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
876 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
877 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
878 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
879 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
880 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
881 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
882 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
883 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
884 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
885 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
886 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
887 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
891 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
893 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
894 wxChar
*ret
= (wxChar
*) malloc(size
);
895 memcpy(ret
, psz
, size
);
901 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
903 register wxChar c1
, c2
;
905 c1
= wxTolower(*psz1
++);
906 c2
= wxTolower(*psz2
++);
907 } while ( c1
&& (c1
== c2
) );
913 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
915 register wxChar c1
, c2
;
916 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
918 if (c1
< c2
) return -1;
919 if (c1
> c2
) return 1;
926 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
935 psz
+= wxStrspn(psz
, delim
);
938 *save_ptr
= (wxChar
*)NULL
;
939 return (wxChar
*)NULL
;
943 psz
= wxStrpbrk(psz
, delim
);
946 *save_ptr
= (wxChar
*)NULL
;
959 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
961 char *localeOld
= setlocale(category
, wxConvLocal
.cWX2MB(locale
));
963 return wxWCharBuffer(wxConvLocal
.cMB2WC(localeOld
));
967 // ----------------------------------------------------------------------------
968 // string.h functions
969 // ----------------------------------------------------------------------------
971 #ifdef wxNEED_WX_STRING_H
972 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
975 while (*dest
) dest
++;
976 while ((*dest
++ = *src
++));
980 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
982 // be careful here as the terminating NUL makes part of the string
992 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
994 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
995 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
996 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1000 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
1003 while ((*dest
++ = *src
++));
1007 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1010 while (*dest
) dest
++;
1011 while (n
&& (*dest
++ = *src
++)) n
--;
1015 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1017 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1019 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1020 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1025 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1028 while (n
&& (*dest
++ = *src
++)) n
--;
1029 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1033 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1035 while (*s
&& !wxStrchr(accept
, *s
))
1038 return *s
? s
: NULL
;
1041 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1043 const wxChar
*ret
= NULL
;
1055 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1058 while (wxStrchr(accept
, *s
++)) len
++;
1062 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1064 wxCHECK_RET( needle
, NULL
, _T("NULL argument in wxStrstr") );
1066 // VZ: this is not exactly the most efficient string search algorithm...
1068 const size_t len
= wxStrlen(needle
);
1070 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1072 if ( !wxStrncmp(fnd
, needle
, len
) )
1081 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1083 const wxChar
*start
= nptr
;
1085 // FIXME: only correct for C locale
1086 while (wxIsspace(*nptr
)) nptr
++;
1087 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1088 while (wxIsdigit(*nptr
)) nptr
++;
1089 if (*nptr
== wxT('.')) {
1091 while (wxIsdigit(*nptr
)) nptr
++;
1093 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1095 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1096 while (wxIsdigit(*nptr
)) nptr
++;
1099 wxString
data(nptr
, nptr
-start
);
1100 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1101 char *rdat
= wxMBSTRINGCAST dat
;
1102 double ret
= strtod(dat
, &rdat
);
1104 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1109 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1111 const wxChar
*start
= nptr
;
1113 // FIXME: only correct for C locale
1114 while (wxIsspace(*nptr
)) nptr
++;
1115 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1116 if (((base
== 0) || (base
== 16)) &&
1117 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1121 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1122 else if (base
== 0) base
= 10;
1124 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1125 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1127 wxString
data(nptr
, nptr
-start
);
1128 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1129 char *rdat
= wxMBSTRINGCAST dat
;
1130 long int ret
= strtol(dat
, &rdat
, base
);
1132 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1136 #endif // wxNEED_WX_STRING_H
1138 #ifdef wxNEED_WX_STDIO_H
1139 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1141 char mode_buffer
[10];
1142 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1143 mode_buffer
[i
] = (char) mode
[i
];
1145 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1148 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1150 char mode_buffer
[10];
1151 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1152 mode_buffer
[i
] = (char) mode
[i
];
1154 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1157 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1159 return remove( wxConvFile
.cWX2MB(path
) );
1162 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1164 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1169 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1171 return atof(wxConvLocal
.cWX2MB(psz
));
1175 #ifdef wxNEED_WX_STDLIB_H
1176 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1178 return atoi(wxConvLocal
.cWX2MB(psz
));
1181 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1183 return atol(wxConvLocal
.cWX2MB(psz
));
1186 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1188 static wxHashTable env
;
1190 // check if we already have stored the converted env var
1191 wxObject
*data
= env
.Get(name
);
1194 // nope, retrieve it,
1196 wxCharBuffer buffer
= wxConvLocal
.cWX2MB(name
);
1197 // printf( "buffer %s\n", (const char*) buffer );
1198 const char *val
= getenv( (const char *)buffer
);
1200 const char *val
= getenv( name
);
1203 if (!val
) return (wxChar
*)NULL
;
1204 // printf( "home %s\n", val );
1207 #ifdef wxUSE_UNICODE
1208 data
= (wxObject
*)new wxString(val
, wxConvLocal
);
1210 data
= (wxObject
*)new wxString(val
);
1214 env
.Put(name
, data
);
1216 // return converted env var
1217 return (wxChar
*)((wxString
*)data
)->c_str();
1220 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1222 return system(wxConvLocal
.cWX2MB(psz
));
1227 #ifdef wxNEED_WX_TIME_H
1228 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
1232 char *buf
= (char *)malloc(max
);
1233 size_t ret
= strftime(buf
, max
, wxConvLocal
.cWX2MB(fmt
), tm
);
1236 wxStrcpy(s
, wxConvLocal
.cMB2WX(buf
));