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 #include "wx/msgdlg.h"
45 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
53 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
57 if (n
) *buf
= wxT('\0');
60 return mbstowcs(buf
, psz
, n
);
63 // assume that we have mbsrtowcs() too if we have wcsrtombs()
66 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
68 return mbstowcs((wchar_t *) NULL
, psz
, 0);
72 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
76 // glibc2.1 chokes on null input
80 return wcstombs(buf
, pwz
, n
);
85 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
87 return wcstombs((char *) NULL
, pwz
, 0);
90 #endif // wxUSE_WCHAR_T
92 bool WXDLLEXPORT
wxOKlibc()
94 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
95 // glibc 2.0 uses UTF-8 even when it shouldn't
97 if ((MB_CUR_MAX
== 2) &&
98 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
100 // this is UTF-8 allright, check whether that's what we want
101 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
102 if ((strlen(cur_locale
) < 4) ||
103 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
104 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
105 // nope, don't use libc conversion
114 size_t WXDLLEXPORT
wcslen(const wchar_t *s
)
117 while (s
[len
]) len
++;
122 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
123 inline WORD
wxMSW_ctype(wxChar ch
)
126 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
130 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
131 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
132 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
133 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
134 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
135 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
136 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
137 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
138 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
139 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
140 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
141 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
142 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
146 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
148 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
149 wxChar
*ret
= (wxChar
*) malloc(size
);
150 memcpy(ret
, psz
, size
);
156 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
158 register wxChar c1
, c2
;
160 c1
= wxTolower(*psz1
++);
161 c2
= wxTolower(*psz2
++);
162 } while ( c1
&& (c1
== c2
) );
168 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
170 register wxChar c1
, c2
;
171 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
173 if (c1
< c2
) return -1;
174 if (c1
> c2
) return 1;
181 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
183 if (!psz
) psz
= *save_ptr
;
184 psz
+= wxStrspn(psz
, delim
);
186 *save_ptr
= (wxChar
*)NULL
;
187 return (wxChar
*)NULL
;
190 psz
= wxStrpbrk(psz
, delim
);
191 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
201 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
203 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
205 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
209 #ifdef wxNEED_WX_STRING_H
210 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
213 while (*dest
) dest
++;
214 while ((*dest
++ = *src
++));
218 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
220 // be careful here as the terminating NUL makes part of the string
230 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
232 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
233 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
234 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
238 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
241 while ((*dest
++ = *src
++));
245 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
248 while (*dest
) dest
++;
249 while (n
&& (*dest
++ = *src
++)) n
--;
253 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
255 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
257 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
258 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
263 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
266 while (n
&& (*dest
++ = *src
++)) n
--;
267 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
271 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
273 while (*s
&& !wxStrchr(accept
, *s
))
276 return *s
? s
: NULL
;
279 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
281 const wxChar
*ret
= NULL
;
293 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
296 while (wxStrchr(accept
, *s
++)) len
++;
300 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
302 wxCHECK_RET( needle
, NULL
, _T("NULL argument in wxStrstr") );
304 // VZ: this is not exactly the most efficient string search algorithm...
306 const size_t len
= wxStrlen(needle
);
308 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
310 if ( !wxStrncmp(fnd
, needle
, len
) )
319 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
321 const wxChar
*start
= nptr
;
323 // FIXME: only correct for C locale
324 while (wxIsspace(*nptr
)) nptr
++;
325 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
326 while (wxIsdigit(*nptr
)) nptr
++;
327 if (*nptr
== wxT('.')) {
329 while (wxIsdigit(*nptr
)) nptr
++;
331 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
333 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
334 while (wxIsdigit(*nptr
)) nptr
++;
337 wxString
data(nptr
, nptr
-start
);
338 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
339 char *rdat
= wxMBSTRINGCAST dat
;
340 double ret
= strtod(dat
, &rdat
);
342 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
347 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
349 const wxChar
*start
= nptr
;
351 // FIXME: only correct for C locale
352 while (wxIsspace(*nptr
)) nptr
++;
353 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
354 if (((base
== 0) || (base
== 16)) &&
355 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
359 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
360 else if (base
== 0) base
= 10;
362 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
363 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
365 wxString
data(nptr
, nptr
-start
);
366 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
367 char *rdat
= wxMBSTRINGCAST dat
;
368 long int ret
= strtol(dat
, &rdat
, base
);
370 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
376 #ifdef wxNEED_WX_STDIO_H
377 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
379 return fopen( wxConvFile
.cWX2MB(path
), wxConvLibc
.cWX2MB(mode
) );
382 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
384 return freopen( wxConvFile
.cWX2MB(path
), wxConvLibc
.cWX2MB(mode
), stream
);
387 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
389 return remove( wxConvFile
.cWX2MB(path
) );
392 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
394 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
399 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
401 return atof(wxConvLibc
.cWX2MB(psz
));
405 #ifdef wxNEED_WX_STDLIB_H
406 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
408 return atoi(wxConvLibc
.cWX2MB(psz
));
411 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
413 return atol(wxConvLibc
.cWX2MB(psz
));
416 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
418 static wxHashTable env
;
420 // check if we already have stored the converted env var
421 wxObject
*data
= env
.Get(name
);
424 // nope, retrieve it,
426 wxCharBuffer buffer
= wxConvLibc
.cWX2MB(name
);
427 // printf( "buffer %s\n", (const char*) buffer );
428 const char *val
= getenv( (const char *)buffer
);
430 const char *val
= getenv( name
);
433 if (!val
) return (wxChar
*)NULL
;
434 // printf( "home %s\n", val );
438 data
= (wxObject
*)new wxString(val
, wxConvLibc
);
440 data
= (wxObject
*)new wxString(val
);
446 // return converted env var
447 return (wxChar
*)((wxString
*)data
)->c_str();
450 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
452 return system(wxConvLibc
.cWX2MB(psz
));
457 #ifdef wxNEED_WX_TIME_H
458 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
461 char *buf
= (char *)malloc(max
);
462 size_t ret
= strftime(buf
, max
, wxConvLibc
.cWX2MB(fmt
), tm
);
464 wxStrcpy(s
, wxConvLibc
.cMB2WX(buf
));