]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
added missing wxSTD
[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 #include <time.h>
35
36 #ifndef WX_PRECOMP
37 #include "wx/defs.h"
38 #include "wx/wxchar.h"
39 #include "wx/string.h"
40 #include "wx/hash.h"
41 #endif
42
43 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
44 #include <windef.h>
45 #include <winbase.h>
46 #include <winnls.h>
47 #include <winnt.h>
48 #endif
49
50 #if wxUSE_WCHAR_T
51 size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n)
52 {
53 if (buf) {
54 if (!n || !*psz) {
55 if (n) *buf = wxT('\0');
56 return 0;
57 }
58 return mbstowcs(buf, psz, n);
59 }
60
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).
63 #if wxUSE_WCSRTOMBS
64 // don't know if it's really needed (or if we can pass NULL), but better safe
65 // than quick
66 mbstate_t mbstate;
67 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
68 #else // !GNU libc
69 return mbstowcs((wchar_t *) NULL, psz, 0);
70 #endif // GNU
71 }
72
73 size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
74 {
75 if (buf) {
76 if (!n || !*pwz) {
77 // glibc2.1 chokes on null input
78 if (n) *buf = '\0';
79 return 0;
80 }
81 return wcstombs(buf, pwz, n);
82 }
83
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).
86 #if wxUSE_WCSRTOMBS
87 // don't know if it's really needed (or if we can pass NULL), but better safe
88 // than quick
89 mbstate_t mbstate;
90 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
91 #else // !GNU libc
92 return wcstombs((char *) NULL, pwz, 0);
93 #endif // GNU
94 }
95 #endif
96
97 bool WXDLLEXPORT wxOKlibc()
98 {
99 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
100 // glibc 2.0 uses UTF-8 even when it shouldn't
101 wchar_t res = 0;
102 if ((MB_CUR_MAX == 2) &&
103 (wxMB2WC(&res, "\xdd\xa5", 1) == 1) &&
104 (res==0x765)) {
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 // nope, don't use libc conversion
110 return FALSE;
111 }
112 }
113 #endif
114 return TRUE;
115 }
116
117 #ifndef HAVE_WCSLEN
118 size_t WXDLLEXPORT wcslen(const wchar_t *s)
119 {
120 size_t len = 0;
121 while (s[len]) len++;
122 return len;
123 }
124 #endif
125
126 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
127 inline WORD wxMSW_ctype(wxChar ch)
128 {
129 WORD ret;
130 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
131 return ret;
132 }
133
134 WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); }
135 WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); }
136 WXDLLEXPORT int wxIsctrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; }
137 WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; }
138 WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); }
139 WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); }
140 WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); }
141 WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; }
142 WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; }
143 WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); }
144 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; }
145 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
146 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
147 #endif
148
149 #ifndef wxStrdup
150 WXDLLEXPORT wxChar * wxStrdup(const wxChar *psz)
151 {
152 size_t size = (wxStrlen(psz) + 1) * sizeof(wxChar);
153 wxChar *ret = (wxChar *) malloc(size);
154 memcpy(ret, psz, size);
155 return ret;
156 }
157 #endif
158
159 #ifndef wxStricmp
160 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
161 {
162 register wxChar c1, c2;
163 do {
164 c1 = wxTolower(*psz1++);
165 c2 = wxTolower(*psz2++);
166 } while ( c1 && (c1 == c2) );
167 return c1 - c2;
168 }
169 #endif
170
171 #ifndef wxStricmp
172 int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n)
173 {
174 register wxChar c1, c2;
175 while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
176 if (n) {
177 if (c1 < c2) return -1;
178 if (c1 > c2) return 1;
179 }
180 return 0;
181 }
182 #endif
183
184 #ifndef wxStrtok
185 WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
186 {
187 if (!psz) psz = *save_ptr;
188 psz += wxStrspn(psz, delim);
189 if (!*psz) {
190 *save_ptr = (wxChar *)NULL;
191 return (wxChar *)NULL;
192 }
193 wxChar *ret = psz;
194 psz = wxStrpbrk(psz, delim);
195 if (!psz) *save_ptr = (wxChar*)NULL;
196 else {
197 *psz = wxT('\0');
198 *save_ptr = psz + 1;
199 }
200 return ret;
201 }
202 #endif
203
204 #ifndef wxSetlocale
205 WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
206 {
207 char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale));
208
209 return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld));
210 }
211 #endif
212
213 #ifdef wxNEED_WX_STRING_H
214 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
215 {
216 wxChar *ret = dest;
217 while (*dest) dest++;
218 while ((*dest++ = *src++));
219 return ret;
220 }
221
222 WXDLLEXPORT wxChar * wxStrchr(const wxChar *s, wxChar c)
223 {
224 while (*s && *s != c) s++;
225 return (*s) ? (wxChar *)s : (wxChar *)NULL;
226 }
227
228 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
229 {
230 while ((*s1 == *s2) && *s1) s1++, s2++;
231 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
232 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
233 return 0;
234 }
235
236 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
237 {
238 wxChar *ret = dest;
239 while ((*dest++ = *src++));
240 return ret;
241 }
242
243 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
244 {
245 wxChar *ret = dest;
246 while (*dest) dest++;
247 while (n && (*dest++ = *src++)) n--;
248 return ret;
249 }
250
251 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
252 {
253 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
254 if (n) {
255 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
256 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
257 }
258 return 0;
259 }
260
261 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
262 {
263 wxChar *ret = dest;
264 while (n && (*dest++ = *src++)) n--;
265 while (n) *dest++=0, n--; // the docs specify padding with zeroes
266 return ret;
267 }
268
269 WXDLLEXPORT wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
270 {
271 while (*s && !wxStrchr(accept, *s)) s++;
272 return (*s) ? (wxChar *)s : (wxChar *)NULL;
273 }
274
275 WXDLLEXPORT wxChar * wxStrrchr(const wxChar *s, wxChar c)
276 {
277 wxChar *ret = (wxChar *)NULL;
278 while (*s) {
279 if (*s == c) ret = (wxChar *)s;
280 s++;
281 }
282 return ret;
283 }
284
285 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
286 {
287 size_t len = 0;
288 while (wxStrchr(accept, *s++)) len++;
289 return len;
290 }
291
292 WXDLLEXPORT wxChar * wxStrstr(const wxChar *haystack, const wxChar *needle)
293 {
294 wxChar *fnd;
295 while ((fnd = wxStrchr(haystack, *needle))) {
296 if (!wxStrcmp(fnd, needle)) return fnd;
297 haystack = fnd + 1;
298 }
299 return (wxChar *)NULL;
300 }
301
302 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
303 {
304 const wxChar *start = nptr;
305
306 // FIXME: only correct for C locale
307 while (wxIsspace(*nptr)) nptr++;
308 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
309 while (wxIsdigit(*nptr)) nptr++;
310 if (*nptr == wxT('.')) {
311 nptr++;
312 while (wxIsdigit(*nptr)) nptr++;
313 }
314 if (*nptr == wxT('E') || *nptr == wxT('e')) {
315 nptr++;
316 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
317 while (wxIsdigit(*nptr)) nptr++;
318 }
319
320 wxString data(nptr, nptr-start);
321 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
322 char *rdat = wxMBSTRINGCAST dat;
323 double ret = strtod(dat, &rdat);
324
325 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
326
327 return ret;
328 }
329
330 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
331 {
332 const wxChar *start = nptr;
333
334 // FIXME: only correct for C locale
335 while (wxIsspace(*nptr)) nptr++;
336 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
337 if (((base == 0) || (base == 16)) &&
338 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
339 nptr += 2;
340 base = 16;
341 }
342 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
343 else if (base == 0) base = 10;
344
345 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
346 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
347
348 wxString data(nptr, nptr-start);
349 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
350 char *rdat = wxMBSTRINGCAST dat;
351 long int ret = strtol(dat, &rdat, base);
352
353 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
354
355 return ret;
356 }
357 #endif
358
359 #ifdef wxNEED_WX_STDIO_H
360 WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
361 {
362 return fopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode));
363 }
364
365 WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
366 {
367 return freopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode), stream);
368 }
369
370 WXDLLEXPORT int wxRemove(const wxChar *path)
371 {
372 return remove(wxConvFile.cWX2MB(path));
373 }
374
375 WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
376 {
377 return rename(wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath));
378 }
379
380 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...)
381 {
382 va_list argptr;
383 int ret;
384
385 va_start(argptr, fmt);
386 ret = wxVprintf(fmt, argptr);
387 va_end(argptr);
388 return ret;
389 }
390
391 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr)
392 {
393 wxString str;
394 str.PrintfV(fmt,argptr);
395 printf("%s", (const char*)str.mb_str());
396 return str.Len();
397 }
398
399 int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...)
400 {
401 va_list argptr;
402 int ret;
403
404 va_start(argptr, fmt);
405 ret = wxVfprintf(stream, fmt, argptr);
406 va_end(argptr);
407 return ret;
408 }
409
410 int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr)
411 {
412 wxString str;
413 str.PrintfV(fmt,argptr);
414 fprintf(stream, "%s", (const char*)str.mb_str());
415 return str.Len();
416 }
417
418 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
419 {
420 va_list argptr;
421 int ret;
422
423 va_start(argptr, fmt);
424 ret = wxVsprintf(buf, fmt, argptr);
425 va_end(argptr);
426 return ret;
427 }
428
429 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
430 {
431 // this might be sort of inefficient, but it doesn't matter since
432 // we'd prefer people to use wxString::Printf directly instead anyway
433 wxString str;
434 str.PrintfV(fmt,argptr);
435 wxStrcpy(buf,str.c_str());
436 return str.Len();
437 }
438
439 int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...)
440 {
441 va_list argptr;
442 int ret;
443
444 va_start(argptr, fmt);
445 ret = wxVsscanf(buf, fmt, argptr);
446 va_end(argptr);
447 return ret;
448 }
449
450 int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr)
451 {
452 int ret;
453 // this will work only for numeric conversion! Strings will not be converted correctly
454 // hopefully this is all we'll need
455 ret = vsscanf(wxConvLibc.cWX2MB(buf), wxConvLibc.cWX2MB(fmt), argptr);
456 return ret;
457 }
458 #endif
459
460 #ifndef wxAtof
461 double WXDLLEXPORT wxAtof(const wxChar *psz)
462 {
463 return atof(wxConvLibc.cWX2MB(psz));
464 }
465 #endif
466
467 #ifdef wxNEED_WX_STDLIB_H
468 int WXDLLEXPORT wxAtoi(const wxChar *psz)
469 {
470 return atoi(wxConvLibc.cWX2MB(psz));
471 }
472
473 long WXDLLEXPORT wxAtol(const wxChar *psz)
474 {
475 return atol(wxConvLibc.cWX2MB(psz));
476 }
477
478 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
479 {
480 static wxHashTable env;
481 // check if we already have stored the converted env var
482 wxObject *data = env.Get(name);
483 if (!data) {
484 // nope, retrieve it,
485 const char *val = getenv(wxConvLibc.cWX2MB(name));
486 if (!val) return (wxChar *)NULL;
487 // convert it,
488 data = (wxObject *)new wxString(val);
489 // and store it
490 env.Put(name, data);
491 }
492 // return converted env var
493 return (wxChar *)((wxString *)data)->c_str();
494 }
495
496 int WXDLLEXPORT wxSystem(const wxChar *psz)
497 {
498 return system(wxConvLibc.cWX2MB(psz));
499 }
500
501 #endif
502
503 #ifdef wxNEED_WX_TIME_H
504 WXDLLEXPORT size_t wxStrftime(wxChar *s, size_t max, const wxChar *fmt, const struct tm *tm)
505 {
506 if (!max) return 0;
507 char *buf = (char *)malloc(max);
508 size_t ret = strftime(buf, max, wxConvLibc.cWX2MB(fmt), tm);
509 if (ret) {
510 wxStrcpy(s, wxConvLibc.cMB2WX(buf));
511 free(buf);
512 return wxStrlen(s);
513 } else {
514 free(buf);
515 *s = 0;
516 return 0;
517 }
518 }
519 #endif