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 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
121 inline WORD
wxMSW_ctype(wxChar ch
)
124 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
128 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
129 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
130 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
131 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
132 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
133 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
134 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
135 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
136 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
137 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
138 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
139 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
140 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
144 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
146 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
147 wxChar
*ret
= (wxChar
*) malloc(size
);
148 memcpy(ret
, psz
, size
);
154 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
156 register wxChar c1
, c2
;
158 c1
= wxTolower(*psz1
++);
159 c2
= wxTolower(*psz2
++);
160 } while ( c1
&& (c1
== c2
) );
166 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
168 register wxChar c1
, c2
;
169 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
171 if (c1
< c2
) return -1;
172 if (c1
> c2
) return 1;
179 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
181 if (!psz
) psz
= *save_ptr
;
182 psz
+= wxStrspn(psz
, delim
);
184 *save_ptr
= (wxChar
*)NULL
;
185 return (wxChar
*)NULL
;
188 psz
= wxStrpbrk(psz
, delim
);
189 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
199 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
201 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
203 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
207 #ifdef wxNEED_WX_STRING_H
208 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
211 while (*dest
) dest
++;
212 while ((*dest
++ = *src
++));
216 WXDLLEXPORT wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
218 while (*s
&& *s
!= c
) s
++;
219 return (*s
) ? (wxChar
*)s
: (wxChar
*)NULL
;
222 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
224 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
225 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
226 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
230 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
233 while ((*dest
++ = *src
++));
237 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
240 while (*dest
) dest
++;
241 while (n
&& (*dest
++ = *src
++)) n
--;
245 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
247 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
249 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
250 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
255 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
258 while (n
&& (*dest
++ = *src
++)) n
--;
259 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
263 WXDLLEXPORT wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
265 while (*s
&& !wxStrchr(accept
, *s
)) s
++;
266 return (*s
) ? (wxChar
*)s
: (wxChar
*)NULL
;
269 WXDLLEXPORT wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
271 wxChar
*ret
= (wxChar
*)NULL
;
273 if (*s
== c
) ret
= (wxChar
*)s
;
279 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
282 while (wxStrchr(accept
, *s
++)) len
++;
286 WXDLLEXPORT wxChar
* wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
288 wxCHECK_RET( needle
, NULL
, _T("NULL argument in wxStrstr") );
290 const size_t len
= wxStrlen(needle
);
293 while ( (fnd
= wxStrchr(haystack
, *needle
)) )
295 if ( !wxStrncmp(fnd
, needle
, len
) )
301 return (wxChar
*)NULL
;
304 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
306 const wxChar
*start
= nptr
;
308 // FIXME: only correct for C locale
309 while (wxIsspace(*nptr
)) nptr
++;
310 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
311 while (wxIsdigit(*nptr
)) nptr
++;
312 if (*nptr
== wxT('.')) {
314 while (wxIsdigit(*nptr
)) nptr
++;
316 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
318 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
319 while (wxIsdigit(*nptr
)) nptr
++;
322 wxString
data(nptr
, nptr
-start
);
323 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
324 char *rdat
= wxMBSTRINGCAST dat
;
325 double ret
= strtod(dat
, &rdat
);
327 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
332 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
334 const wxChar
*start
= nptr
;
336 // FIXME: only correct for C locale
337 while (wxIsspace(*nptr
)) nptr
++;
338 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
339 if (((base
== 0) || (base
== 16)) &&
340 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
344 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
345 else if (base
== 0) base
= 10;
347 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
348 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
350 wxString
data(nptr
, nptr
-start
);
351 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
352 char *rdat
= wxMBSTRINGCAST dat
;
353 long int ret
= strtol(dat
, &rdat
, base
);
355 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
361 #ifdef wxNEED_WX_STDIO_H
362 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
364 return fopen(wxConvFile
.cWX2MB(path
), wxConvLibc
.cWX2MB(mode
));
367 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
369 return freopen(wxConvFile
.cWX2MB(path
), wxConvLibc
.cWX2MB(mode
), stream
);
372 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
374 return remove(wxConvFile
.cWX2MB(path
));
377 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
379 return rename(wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
));
382 int WXDLLEXPORT
wxPrintf(const wxChar
*fmt
, ...)
387 va_start(argptr
, fmt
);
388 ret
= wxVprintf(fmt
, argptr
);
393 int WXDLLEXPORT
wxVprintf(const wxChar
*fmt
, va_list argptr
)
396 str
.PrintfV(fmt
,argptr
);
397 printf("%s", (const char*)str
.mb_str());
401 int WXDLLEXPORT
wxFprintf(FILE *stream
, const wxChar
*fmt
, ...)
406 va_start(argptr
, fmt
);
407 ret
= wxVfprintf(stream
, fmt
, argptr
);
412 int WXDLLEXPORT
wxVfprintf(FILE *stream
, const wxChar
*fmt
, va_list argptr
)
415 str
.PrintfV(fmt
,argptr
);
416 fprintf(stream
, "%s", (const char*)str
.mb_str());
420 int WXDLLEXPORT
wxSprintf(wxChar
*buf
, const wxChar
*fmt
, ...)
425 va_start(argptr
, fmt
);
426 ret
= wxVsprintf(buf
, fmt
, argptr
);
431 int WXDLLEXPORT
wxVsprintf(wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
433 // this might be sort of inefficient, but it doesn't matter since
434 // we'd prefer people to use wxString::Printf directly instead anyway
436 str
.PrintfV(fmt
,argptr
);
437 wxStrcpy(buf
,str
.c_str());
441 int WXDLLEXPORT
wxSscanf(const wxChar
*buf
, const wxChar
*fmt
, ...)
446 va_start(argptr
, fmt
);
447 ret
= wxVsscanf(buf
, fmt
, argptr
);
452 int WXDLLEXPORT
wxVsscanf(const wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
455 // this will work only for numeric conversion! Strings will not be converted correctly
456 // hopefully this is all we'll need
457 ret
= vsscanf(wxConvLibc
.cWX2MB(buf
), wxConvLibc
.cWX2MB(fmt
), argptr
);
463 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
465 return atof(wxConvLibc
.cWX2MB(psz
));
469 #ifdef wxNEED_WX_STDLIB_H
470 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
472 return atoi(wxConvLibc
.cWX2MB(psz
));
475 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
477 return atol(wxConvLibc
.cWX2MB(psz
));
480 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
482 static wxHashTable env
;
483 // check if we already have stored the converted env var
484 wxObject
*data
= env
.Get(name
);
486 // nope, retrieve it,
487 const char *val
= getenv(wxConvLibc
.cWX2MB(name
));
488 if (!val
) return (wxChar
*)NULL
;
490 data
= (wxObject
*)new wxString(val
);
494 // return converted env var
495 return (wxChar
*)((wxString
*)data
)->c_str();
498 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
500 return system(wxConvLibc
.cWX2MB(psz
));
505 #ifdef wxNEED_WX_TIME_H
506 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
509 char *buf
= (char *)malloc(max
);
510 size_t ret
= strftime(buf
, max
, wxConvLibc
.cWX2MB(fmt
), tm
);
512 wxStrcpy(s
, wxConvLibc
.cMB2WX(buf
));