]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
* Some more CopyObject()
[wxWidgets.git] / src / common / wxchar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxchar.cpp
3 // Purpose: wxChar implementation
4 // Author: Ove Kåven
5 // Modified by:
6 // Created: 09/04/99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows copyright
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "wxchar.h"
14 #endif
15
16 // ===========================================================================
17 // headers, declarations, constants
18 // ===========================================================================
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #define _ISOC9X_SOURCE 1 // to get vsscanf()
28 #define _BSD_SOURCE 1 // to still get strdup()
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <locale.h>
34
35 #ifndef WX_PRECOMP
36 #include "wx/defs.h"
37 #include "wx/wxchar.h"
38 #include "wx/string.h"
39 #include "wx/hash.h"
40 #endif
41
42 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
43 #include <windef.h>
44 #include <winbase.h>
45 #include <winnls.h>
46 #include <winnt.h>
47 #endif
48
49 #if wxUSE_WCHAR_T
50 size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n)
51 {
52 if (buf) {
53 if (!n || !*psz) {
54 if (n) *buf = _T('\0');
55 return 0;
56 }
57 return mbstowcs(buf, psz, n);
58 }
59
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).
62 #if wxUSE_WCSRTOMBS
63 // don't know if it's really needed (or if we can pass NULL), but better safe
64 // than quick
65 mbstate_t mbstate;
66 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
67 #else // !GNU libc
68 return mbstowcs((wchar_t *) NULL, psz, 0);
69 #endif // GNU
70 }
71
72 size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
73 {
74 if (buf) {
75 if (!n || !*pwz) {
76 // glibc2.1 chokes on null input
77 if (n) *buf = '\0';
78 return 0;
79 }
80 return wcstombs(buf, pwz, n);
81 }
82
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).
85 #if wxUSE_WCSRTOMBS
86 // don't know if it's really needed (or if we can pass NULL), but better safe
87 // than quick
88 mbstate_t mbstate;
89 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
90 #else // !GNU libc
91 return wcstombs((char *) NULL, pwz, 0);
92 #endif // GNU
93 }
94 #endif
95
96 bool WXDLLEXPORT wxOKlibc()
97 {
98 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
99 // GNU libc uses UTF-8 even when it shouldn't
100 wchar_t res;
101 if ((MB_CUR_MAX == 2) &&
102 (wxMB2WC(&res, "\xdd\xa5", 1)>0) &&
103 (res==0x765)) {
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
109 return FALSE;
110 }
111 }
112 #endif
113 return TRUE;
114 }
115
116 #ifndef HAVE_WCSLEN
117 size_t WXDLLEXPORT wcslen(const wchar_t *s)
118 {
119 size_t len = 0;
120 while (s[len]) len++;
121 return len;
122 }
123 #endif
124
125 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
126 inline WORD wxMSW_ctype(wxChar ch)
127 {
128 WORD ret;
129 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
130 return ret;
131 }
132
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)); }
146 #endif
147
148 #ifndef wxStrdup
149 wxChar * WXDLLEXPORT wxStrdup(const wxChar *psz)
150 {
151 size_t size = (wxStrlen(psz) + 1) * sizeof(wxChar);
152 wxChar *ret = (wxChar *) malloc(size);
153 memcpy(ret, psz, size);
154 return ret;
155 }
156 #endif
157
158 #ifndef wxStricmp
159 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
160 {
161 register wxChar c1, c2;
162 do {
163 c1 = wxTolower(*psz1++);
164 c2 = wxTolower(*psz2++);
165 } while ( c1 && (c1 == c2) );
166 return c1 - c2;
167 }
168 #endif
169
170 #ifndef wxStrtok
171 wxChar * WXDLLEXPORT wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
172 {
173 if (!psz) psz = *save_ptr;
174 psz += wxStrspn(psz, delim);
175 if (!*psz) {
176 *save_ptr = (wxChar *)NULL;
177 return (wxChar *)NULL;
178 }
179 wxChar *ret = psz;
180 psz = wxStrpbrk(psz, delim);
181 if (!psz) *save_ptr = (wxChar*)NULL;
182 else {
183 *psz = _T('\0');
184 *save_ptr = psz + 1;
185 }
186 return ret;
187 }
188 #endif
189
190 #ifndef wxSetlocale
191 wxChar * WXDLLEXPORT wxSetlocale(int category, const wxChar *locale)
192 {
193 setlocale(category, wxConv_libc.cWX2MB(locale));
194 // FIXME
195 return (wxChar *)NULL;
196 }
197 #endif
198
199 #ifdef wxNEED_WX_STDIO_H
200 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...)
201 {
202 va_list argptr;
203 int ret;
204
205 va_start(argptr, fmt);
206 ret = wxVprintf(fmt, argptr);
207 va_end(argptr);
208 return ret;
209 }
210
211 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr)
212 {
213 wxString str;
214 str.PrintfV(fmt,argptr);
215 printf("%s", (const char*)str.mb_str());
216 return str.Len();
217 }
218
219 int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...)
220 {
221 va_list argptr;
222 int ret;
223
224 va_start(argptr, fmt);
225 ret = wxVfprintf(stream, fmt, argptr);
226 va_end(argptr);
227 return ret;
228 }
229
230 int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr)
231 {
232 wxString str;
233 str.PrintfV(fmt,argptr);
234 fprintf(stream, "%s", (const char*)str.mb_str());
235 return str.Len();
236 }
237
238 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
239 {
240 va_list argptr;
241 int ret;
242
243 va_start(argptr, fmt);
244 ret = wxVsprintf(buf, fmt, argptr);
245 va_end(argptr);
246 return ret;
247 }
248
249 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
250 {
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
253 wxString str;
254 str.PrintfV(fmt,argptr);
255 wxStrcpy(buf,str.c_str());
256 return str.Len();
257 }
258
259 int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...)
260 {
261 va_list argptr;
262 int ret;
263
264 va_start(argptr, fmt);
265 ret = wxVsscanf(buf, fmt, argptr);
266 va_end(argptr);
267 return ret;
268 }
269
270 int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr)
271 {
272 int ret;
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);
276 return ret;
277 }
278 #endif
279
280 #ifndef wxAtof
281 double WXDLLEXPORT wxAtof(const wxChar *psz)
282 {
283 return atof(wxConv_libc.cWX2MB(psz));
284 }
285 #endif
286
287 #ifdef wxNEED_WX_STDLIB_H
288 int WXDLLEXPORT wxAtoi(const wxChar *psz)
289 {
290 return atoi(wxConv_libc.cWX2MB(psz));
291 }
292
293 long WXDLLEXPORT wxAtol(const wxChar *psz)
294 {
295 return atol(wxConv_libc.cWX2MB(psz));
296 }
297
298 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
299 {
300 static wxHashTable env;
301 // check if we already have stored the converted env var
302 wxObject *data = env.Get(name);
303 if (!data) {
304 // nope, retrieve it,
305 const char *val = getenv(wxConv_libc.cWX2MB(name));
306 if (!val) return (wxChar *)NULL;
307 // convert it,
308 data = (wxObject *)new wxString(val);
309 // and store it
310 env.Put(name, data);
311 }
312 // return converted env var
313 return (wxChar *)((wxString *)data)->c_str();
314 }
315
316 int WXDLLEXPORT wxSystem(const wxChar *psz)
317 {
318 return system(wxConv_libc.cWX2MB(psz));
319 }
320
321 #endif