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 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
62 // honor the 3rd parameter, thus it will happily crash here).
64 // don't know if it's really needed (or if we can pass NULL), but better safe
67 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
69 return mbstowcs((wchar_t *) NULL
, psz
, 0);
73 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
77 // glibc2.1 chokes on null input
81 return wcstombs(buf
, pwz
, n
);
84 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
85 // honor the 3rd parameter, thus it will happily crash here).
87 // don't know if it's really needed (or if we can pass NULL), but better safe
90 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
92 return wcstombs((char *) NULL
, pwz
, 0);
97 bool WXDLLEXPORT
wxOKlibc()
99 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
100 // glibc 2.0 uses UTF-8 even when it shouldn't
102 if ((MB_CUR_MAX
== 2) &&
103 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
105 // this is UTF-8 allright, check whether that's what we want
106 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
107 if ((strlen(cur_locale
) < 4) ||
108 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
109 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
110 // nope, don't use libc conversion
119 size_t WXDLLEXPORT
wcslen(const wchar_t *s
)
122 while (s
[len
]) len
++;
127 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
128 inline WORD
wxMSW_ctype(wxChar ch
)
131 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
135 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
136 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
137 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
138 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
139 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
140 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
141 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
142 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
143 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
144 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
145 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
146 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
147 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
151 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
153 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
154 wxChar
*ret
= (wxChar
*) malloc(size
);
155 memcpy(ret
, psz
, size
);
161 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
163 register wxChar c1
, c2
;
165 c1
= wxTolower(*psz1
++);
166 c2
= wxTolower(*psz2
++);
167 } while ( c1
&& (c1
== c2
) );
173 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
175 register wxChar c1
, c2
;
176 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
178 if (c1
< c2
) return -1;
179 if (c1
> c2
) return 1;
186 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
188 if (!psz
) psz
= *save_ptr
;
189 psz
+= wxStrspn(psz
, delim
);
191 *save_ptr
= (wxChar
*)NULL
;
192 return (wxChar
*)NULL
;
195 psz
= wxStrpbrk(psz
, delim
);
196 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
206 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
208 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
210 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
214 #ifdef wxNEED_WX_STRING_H
215 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
218 while (*dest
) dest
++;
219 while ((*dest
++ = *src
++));
223 WXDLLEXPORT wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
225 while (*s
&& *s
!= c
) s
++;
226 return (*s
) ? (wxChar
*)s
: (wxChar
*)NULL
;
229 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
231 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
232 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
233 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
237 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
240 while ((*dest
++ = *src
++));
244 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
247 while (*dest
) dest
++;
248 while (n
&& (*dest
++ = *src
++)) n
--;
252 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
254 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
256 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
257 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
262 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
265 while (n
&& (*dest
++ = *src
++)) n
--;
266 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
270 WXDLLEXPORT wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
272 while (*s
&& !wxStrchr(accept
, *s
)) s
++;
273 return (*s
) ? (wxChar
*)s
: (wxChar
*)NULL
;
276 WXDLLEXPORT wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
278 wxChar
*ret
= (wxChar
*)NULL
;
280 if (*s
== c
) ret
= (wxChar
*)s
;
286 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
289 while (wxStrchr(accept
, *s
++)) len
++;
293 WXDLLEXPORT wxChar
* wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
296 while ((fnd
= wxStrchr(haystack
, *needle
))) {
297 if (!wxStrcmp(fnd
, needle
)) return fnd
;
300 return (wxChar
*)NULL
;
303 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
305 const wxChar
*start
= nptr
;
307 // FIXME: only correct for C locale
308 while (wxIsspace(*nptr
)) nptr
++;
309 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
310 while (wxIsdigit(*nptr
)) nptr
++;
311 if (*nptr
== wxT('.')) {
313 while (wxIsdigit(*nptr
)) nptr
++;
315 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
317 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
318 while (wxIsdigit(*nptr
)) nptr
++;
321 wxString
data(nptr
, nptr
-start
);
322 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
323 char *rdat
= wxMBSTRINGCAST dat
;
324 double ret
= strtod(dat
, &rdat
);
326 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
331 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
333 const wxChar
*start
= nptr
;
335 // FIXME: only correct for C locale
336 while (wxIsspace(*nptr
)) nptr
++;
337 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
338 if (((base
== 0) || (base
== 16)) &&
339 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
343 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
344 else if (base
== 0) base
= 10;
346 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
347 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
349 wxString
data(nptr
, nptr
-start
);
350 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
351 char *rdat
= wxMBSTRINGCAST dat
;
352 long int ret
= strtol(dat
, &rdat
, base
);
354 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
360 #ifdef wxNEED_WX_STDIO_H
361 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
363 return fopen(wxConvFile
.cWX2MB(path
), wxConvLibc
.cWX2MB(mode
));
366 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
368 return freopen(wxConvFile
.cWX2MB(path
), wxConvLibc
.cWX2MB(mode
), stream
);
371 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
373 return remove(wxConvFile
.cWX2MB(path
));
376 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
378 return rename(wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
));
381 int WXDLLEXPORT
wxPrintf(const wxChar
*fmt
, ...)
386 va_start(argptr
, fmt
);
387 ret
= wxVprintf(fmt
, argptr
);
392 int WXDLLEXPORT
wxVprintf(const wxChar
*fmt
, va_list argptr
)
395 str
.PrintfV(fmt
,argptr
);
396 printf("%s", (const char*)str
.mb_str());
400 int WXDLLEXPORT
wxFprintf(FILE *stream
, const wxChar
*fmt
, ...)
405 va_start(argptr
, fmt
);
406 ret
= wxVfprintf(stream
, fmt
, argptr
);
411 int WXDLLEXPORT
wxVfprintf(FILE *stream
, const wxChar
*fmt
, va_list argptr
)
414 str
.PrintfV(fmt
,argptr
);
415 fprintf(stream
, "%s", (const char*)str
.mb_str());
419 int WXDLLEXPORT
wxSprintf(wxChar
*buf
, const wxChar
*fmt
, ...)
424 va_start(argptr
, fmt
);
425 ret
= wxVsprintf(buf
, fmt
, argptr
);
430 int WXDLLEXPORT
wxVsprintf(wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
432 // this might be sort of inefficient, but it doesn't matter since
433 // we'd prefer people to use wxString::Printf directly instead anyway
435 str
.PrintfV(fmt
,argptr
);
436 wxStrcpy(buf
,str
.c_str());
440 int WXDLLEXPORT
wxSscanf(const wxChar
*buf
, const wxChar
*fmt
, ...)
445 va_start(argptr
, fmt
);
446 ret
= wxVsscanf(buf
, fmt
, argptr
);
451 int WXDLLEXPORT
wxVsscanf(const wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
454 // this will work only for numeric conversion! Strings will not be converted correctly
455 // hopefully this is all we'll need
456 ret
= vsscanf(wxConvLibc
.cWX2MB(buf
), wxConvLibc
.cWX2MB(fmt
), argptr
);
462 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
464 return atof(wxConvLibc
.cWX2MB(psz
));
468 #ifdef wxNEED_WX_STDLIB_H
469 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
471 return atoi(wxConvLibc
.cWX2MB(psz
));
474 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
476 return atol(wxConvLibc
.cWX2MB(psz
));
479 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
481 static wxHashTable env
;
482 // check if we already have stored the converted env var
483 wxObject
*data
= env
.Get(name
);
485 // nope, retrieve it,
486 const char *val
= getenv(wxConvLibc
.cWX2MB(name
));
487 if (!val
) return (wxChar
*)NULL
;
489 data
= (wxObject
*)new wxString(val
);
493 // return converted env var
494 return (wxChar
*)((wxString
*)data
)->c_str();
497 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
499 return system(wxConvLibc
.cWX2MB(psz
));
504 #ifdef wxNEED_WX_TIME_H
505 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
508 char *buf
= (char *)malloc(max
);
509 size_t ret
= strftime(buf
, max
, wxConvLibc
.cWX2MB(fmt
), tm
);
511 wxStrcpy(s
, wxConvLibc
.cMB2WX(buf
));