]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
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()
37 #include "wx/wxchar.h"
38 #include "wx/string.h"
42 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
50 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
54 if (n
) *buf
= _T('\0');
57 return mbstowcs(buf
, psz
, n
);
60 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
61 // honor the 3rd parameter, thus it will happily crash here).
63 // don't know if it's really needed (or if we can pass NULL), but better safe
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
);
83 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
84 // honor the 3rd parameter, thus it will happily crash here).
86 // don't know if it's really needed (or if we can pass NULL), but better safe
89 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
91 return wcstombs((char *) NULL
, pwz
, 0);
96 bool WXDLLEXPORT
wxOKlibc()
98 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
99 // GNU libc uses UTF-8 even when it shouldn't
101 if ((MB_CUR_MAX
== 2) &&
102 (wxMB2WC(&res
, "\xdd\xa5", 1)>0) &&
104 // this is UTF-8 allright, check whether that's what we want
105 char *cur_locale
= setlocale(LC_ALL
, NULL
);
106 if ((strlen(cur_locale
) < 4) ||
107 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8"))) {
108 // nope, don't use libc conversion
117 size_t WXDLLEXPORT
wcslen(const wchar_t *s
)
120 while (s
[len
]) len
++;
125 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
126 inline WORD
wxMSW_ctype(wxChar ch
)
129 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
133 int WXDLLEXPORT
wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
134 int WXDLLEXPORT
wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
135 int WXDLLEXPORT
wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
136 int WXDLLEXPORT
wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
137 int WXDLLEXPORT
wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
138 int WXDLLEXPORT
wxIslower(wxChar ch
) { return IsCharLower(ch
); }
139 int WXDLLEXPORT
wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
140 int WXDLLEXPORT
wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
141 int WXDLLEXPORT
wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
142 int WXDLLEXPORT
wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
143 int WXDLLEXPORT
wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
144 int WXDLLEXPORT
wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
145 int WXDLLEXPORT
wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
149 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
151 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
152 wxChar
*ret
= (wxChar
*) malloc(size
);
153 memcpy(ret
, psz
, size
);
159 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
161 register wxChar c1
, c2
;
163 c1
= wxTolower(*psz1
++);
164 c2
= wxTolower(*psz2
++);
165 } while ( c1
&& (c1
== c2
) );
171 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
173 if (!psz
) psz
= *save_ptr
;
174 psz
+= wxStrspn(psz
, delim
);
176 *save_ptr
= (wxChar
*)NULL
;
177 return (wxChar
*)NULL
;
180 psz
= wxStrpbrk(psz
, delim
);
181 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
191 wxChar
* WXDLLEXPORT
wxSetlocale(int category
, const wxChar
*locale
)
193 setlocale(category
, wxConv_libc
.cWX2MB(locale
));
195 return (wxChar
*)NULL
;
199 #ifdef wxNEED_WX_STDIO_H
200 int WXDLLEXPORT
wxPrintf(const wxChar
*fmt
, ...)
205 va_start(argptr
, fmt
);
206 ret
= wxVprintf(fmt
, argptr
);
211 int WXDLLEXPORT
wxVprintf(const wxChar
*fmt
, va_list argptr
)
214 str
.PrintfV(fmt
,argptr
);
215 printf("%s", (const char*)str
.mb_str());
219 int WXDLLEXPORT
wxFprintf(FILE *stream
, const wxChar
*fmt
, ...)
224 va_start(argptr
, fmt
);
225 ret
= wxVfprintf(stream
, fmt
, argptr
);
230 int WXDLLEXPORT
wxVfprintf(FILE *stream
, const wxChar
*fmt
, va_list argptr
)
233 str
.PrintfV(fmt
,argptr
);
234 fprintf(stream
, "%s", (const char*)str
.mb_str());
238 int WXDLLEXPORT
wxSprintf(wxChar
*buf
, const wxChar
*fmt
, ...)
243 va_start(argptr
, fmt
);
244 ret
= wxVsprintf(buf
, fmt
, argptr
);
249 int WXDLLEXPORT
wxVsprintf(wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
251 // this might be sort of inefficient, but it doesn't matter since
252 // we'd prefer people to use wxString::Printf directly instead anyway
254 str
.PrintfV(fmt
,argptr
);
255 wxStrcpy(buf
,str
.c_str());
259 int WXDLLEXPORT
wxSscanf(const wxChar
*buf
, const wxChar
*fmt
, ...)
264 va_start(argptr
, fmt
);
265 ret
= wxVsscanf(buf
, fmt
, argptr
);
270 int WXDLLEXPORT
wxVsscanf(const wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
273 // this will work only for numeric conversion! Strings will not be converted correctly
274 // hopefully this is all we'll need
275 ret
= vsscanf(wxConv_libc
.cWX2MB(buf
), wxConv_libc
.cWX2MB(fmt
), argptr
);
281 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
283 return atof(wxConv_libc
.cWX2MB(psz
));
287 #ifdef wxNEED_WX_STDLIB_H
288 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
290 return atoi(wxConv_libc
.cWX2MB(psz
));
293 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
295 return atol(wxConv_libc
.cWX2MB(psz
));
298 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
300 static wxHashTable env
;
301 // check if we already have stored the converted env var
302 wxObject
*data
= env
.Get(name
);
304 // nope, retrieve it,
305 const char *val
= getenv(wxConv_libc
.cWX2MB(name
));
306 if (!val
) return (wxChar
*)NULL
;
308 data
= (wxObject
*)new wxString(val
);
312 // return converted env var
313 return (wxChar
*)((wxString
*)data
)->c_str();
316 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
318 return system(wxConv_libc
.cWX2MB(psz
));