]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
Typos.
[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 WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); }
134 WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); }
135 WXDLLEXPORT int wxIsctrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; }
136 WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; }
137 WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); }
138 WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); }
139 WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); }
140 WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; }
141 WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; }
142 WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); }
143 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; }
144 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
145 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
146 #endif
147
148 #ifndef wxStrdup
149 WXDLLEXPORT wxChar * 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 WXDLLEXPORT wxChar * 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 WXDLLEXPORT wxChar * wxSetlocale(int category, const wxChar *locale)
192 {
193 setlocale(category, wxConvLibc.cWX2MB(locale));
194 // FIXME
195 return (wxChar *)NULL;
196 }
197 #endif
198
199 #ifdef wxNEED_WX_STRING_H
200 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
201 {
202 wxChar *ret = dest;
203 while (*dest) dest++;
204 while ((*dest++ = *src++));
205 return ret;
206 }
207
208 WXDLLEXPORT wxChar * wxStrchr(const wxChar *s, wxChar c)
209 {
210 while (*s && *s != c) s++;
211 return (*s) ? (wxChar *)s : (wxChar *)NULL;
212 }
213
214 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
215 {
216 while ((*s1 == *s2) && *s1) s1++, s2++;
217 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
218 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
219 return 0;
220 }
221
222 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
223 {
224 wxChar *ret = dest;
225 while ((*dest++ = *src++));
226 return ret;
227 }
228
229 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
230 {
231 wxChar *ret = dest;
232 while (*dest) dest++;
233 while (n && (*dest++ = *src++)) n--;
234 return ret;
235 }
236
237 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
238 {
239 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
240 if (n) {
241 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
242 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
243 }
244 return 0;
245 }
246
247 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
248 {
249 wxChar *ret = dest;
250 while (n && (*dest++ = *src++)) n--;
251 while (n) *dest++=0, n--; // the docs specify padding with zeroes
252 return ret;
253 }
254
255 WXDLLEXPORT wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
256 {
257 while (*s && !wxStrchr(accept, *s)) s++;
258 return (*s) ? (wxChar *)s : (wxChar *)NULL;
259 }
260
261 WXDLLEXPORT wxChar * wxStrrchr(const wxChar *s, wxChar c)
262 {
263 wxChar *ret = (wxChar *)NULL;
264 while (*s) {
265 if (*s == c) ret = (wxChar *)s;
266 s++;
267 }
268 return ret;
269 }
270
271 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
272 {
273 size_t len = 0;
274 while (wxStrchr(accept, *s++)) len++;
275 return len;
276 }
277
278 WXDLLEXPORT wxChar * wxStrstr(const wxChar *haystack, const wxChar *needle)
279 {
280 wxChar *fnd;
281 while ((fnd = wxStrchr(haystack, *needle))) {
282 if (!wxStrcmp(fnd, needle)) return fnd;
283 haystack = fnd + 1;
284 }
285 return (wxChar *)NULL;
286 }
287
288 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
289 {
290 const wxChar *start = nptr;
291
292 // FIXME: only correct for C locale
293 while (wxIsspace(*nptr)) nptr++;
294 if (*nptr == _T('+') || *nptr == _T('-')) nptr++;
295 while (wxIsdigit(*nptr)) nptr++;
296 if (*nptr == _T('.')) {
297 nptr++;
298 while (wxIsdigit(*nptr)) nptr++;
299 }
300 if (*nptr == _T('E') || *nptr == _T('e')) {
301 nptr++;
302 if (*nptr == _T('+') || *nptr == _T('-')) nptr++;
303 while (wxIsdigit(*nptr)) nptr++;
304 }
305
306 wxString data(nptr, nptr-start);
307 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
308 char *rdat = MBSTRINGCAST dat;
309 double ret = strtod(dat, &rdat);
310
311 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
312
313 return ret;
314 }
315
316 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
317 {
318 const wxChar *start = nptr;
319
320 // FIXME: only correct for C locale
321 while (wxIsspace(*nptr)) nptr++;
322 if (*nptr == _T('+') || *nptr == _T('-')) nptr++;
323 if (((base == 0) || (base == 16)) &&
324 (nptr[0] == _T('0') && nptr[1] == _T('x'))) {
325 nptr += 2;
326 base = 16;
327 }
328 else if ((base == 0) && (nptr[0] == _T('0'))) base = 8;
329 else if (base == 0) base = 10;
330
331 while ((wxIsdigit(*nptr) && (*nptr - _T('0') < base)) ||
332 (wxIsalpha(*nptr) && (wxToupper(*nptr) - _T('A') + 10 < base))) nptr++;
333
334 wxString data(nptr, nptr-start);
335 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
336 char *rdat = MBSTRINGCAST dat;
337 long int ret = strtol(dat, &rdat, base);
338
339 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
340
341 return ret;
342 }
343 #endif
344
345 #ifdef wxNEED_WX_STDIO_H
346 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...)
347 {
348 va_list argptr;
349 int ret;
350
351 va_start(argptr, fmt);
352 ret = wxVprintf(fmt, argptr);
353 va_end(argptr);
354 return ret;
355 }
356
357 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr)
358 {
359 wxString str;
360 str.PrintfV(fmt,argptr);
361 printf("%s", (const char*)str.mb_str());
362 return str.Len();
363 }
364
365 int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...)
366 {
367 va_list argptr;
368 int ret;
369
370 va_start(argptr, fmt);
371 ret = wxVfprintf(stream, fmt, argptr);
372 va_end(argptr);
373 return ret;
374 }
375
376 int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr)
377 {
378 wxString str;
379 str.PrintfV(fmt,argptr);
380 fprintf(stream, "%s", (const char*)str.mb_str());
381 return str.Len();
382 }
383
384 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
385 {
386 va_list argptr;
387 int ret;
388
389 va_start(argptr, fmt);
390 ret = wxVsprintf(buf, fmt, argptr);
391 va_end(argptr);
392 return ret;
393 }
394
395 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
396 {
397 // this might be sort of inefficient, but it doesn't matter since
398 // we'd prefer people to use wxString::Printf directly instead anyway
399 wxString str;
400 str.PrintfV(fmt,argptr);
401 wxStrcpy(buf,str.c_str());
402 return str.Len();
403 }
404
405 int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...)
406 {
407 va_list argptr;
408 int ret;
409
410 va_start(argptr, fmt);
411 ret = wxVsscanf(buf, fmt, argptr);
412 va_end(argptr);
413 return ret;
414 }
415
416 int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr)
417 {
418 int ret;
419 // this will work only for numeric conversion! Strings will not be converted correctly
420 // hopefully this is all we'll need
421 ret = vsscanf(wxConvLibc.cWX2MB(buf), wxConvLibc.cWX2MB(fmt), argptr);
422 return ret;
423 }
424 #endif
425
426 #ifndef wxAtof
427 double WXDLLEXPORT wxAtof(const wxChar *psz)
428 {
429 return atof(wxConvLibc.cWX2MB(psz));
430 }
431 #endif
432
433 #ifdef wxNEED_WX_STDLIB_H
434 int WXDLLEXPORT wxAtoi(const wxChar *psz)
435 {
436 return atoi(wxConvLibc.cWX2MB(psz));
437 }
438
439 long WXDLLEXPORT wxAtol(const wxChar *psz)
440 {
441 return atol(wxConvLibc.cWX2MB(psz));
442 }
443
444 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
445 {
446 static wxHashTable env;
447 // check if we already have stored the converted env var
448 wxObject *data = env.Get(name);
449 if (!data) {
450 // nope, retrieve it,
451 const char *val = getenv(wxConvLibc.cWX2MB(name));
452 if (!val) return (wxChar *)NULL;
453 // convert it,
454 data = (wxObject *)new wxString(val);
455 // and store it
456 env.Put(name, data);
457 }
458 // return converted env var
459 return (wxChar *)((wxString *)data)->c_str();
460 }
461
462 int WXDLLEXPORT wxSystem(const wxChar *psz)
463 {
464 return system(wxConvLibc.cWX2MB(psz));
465 }
466
467 #endif