]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
cleaning up image mess for os/2
[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 // GNU libc uses UTF-8 even when it shouldn't
101 wchar_t res;
102 if ((MB_CUR_MAX == 2) &&
103 (wxMB2WC(&res, "\xdd\xa5", 1)>0) &&
104 (res==0x765)) {
105 // this is UTF-8 allright, check whether that's what we want
106 char *cur_locale = setlocale(LC_ALL, 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 wxStrtok
172 WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
173 {
174 if (!psz) psz = *save_ptr;
175 psz += wxStrspn(psz, delim);
176 if (!*psz) {
177 *save_ptr = (wxChar *)NULL;
178 return (wxChar *)NULL;
179 }
180 wxChar *ret = psz;
181 psz = wxStrpbrk(psz, delim);
182 if (!psz) *save_ptr = (wxChar*)NULL;
183 else {
184 *psz = wxT('\0');
185 *save_ptr = psz + 1;
186 }
187 return ret;
188 }
189 #endif
190
191 #ifndef wxSetlocale
192 WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
193 {
194 char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale));
195
196 return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld));
197 }
198 #endif
199
200 #ifdef wxNEED_WX_STRING_H
201 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
202 {
203 wxChar *ret = dest;
204 while (*dest) dest++;
205 while ((*dest++ = *src++));
206 return ret;
207 }
208
209 WXDLLEXPORT wxChar * wxStrchr(const wxChar *s, wxChar c)
210 {
211 while (*s && *s != c) s++;
212 return (*s) ? (wxChar *)s : (wxChar *)NULL;
213 }
214
215 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
216 {
217 while ((*s1 == *s2) && *s1) s1++, s2++;
218 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
219 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
220 return 0;
221 }
222
223 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
224 {
225 wxChar *ret = dest;
226 while ((*dest++ = *src++));
227 return ret;
228 }
229
230 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
231 {
232 wxChar *ret = dest;
233 while (*dest) dest++;
234 while (n && (*dest++ = *src++)) n--;
235 return ret;
236 }
237
238 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
239 {
240 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
241 if (n) {
242 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
243 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
244 }
245 return 0;
246 }
247
248 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
249 {
250 wxChar *ret = dest;
251 while (n && (*dest++ = *src++)) n--;
252 while (n) *dest++=0, n--; // the docs specify padding with zeroes
253 return ret;
254 }
255
256 WXDLLEXPORT wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
257 {
258 while (*s && !wxStrchr(accept, *s)) s++;
259 return (*s) ? (wxChar *)s : (wxChar *)NULL;
260 }
261
262 WXDLLEXPORT wxChar * wxStrrchr(const wxChar *s, wxChar c)
263 {
264 wxChar *ret = (wxChar *)NULL;
265 while (*s) {
266 if (*s == c) ret = (wxChar *)s;
267 s++;
268 }
269 return ret;
270 }
271
272 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
273 {
274 size_t len = 0;
275 while (wxStrchr(accept, *s++)) len++;
276 return len;
277 }
278
279 WXDLLEXPORT wxChar * wxStrstr(const wxChar *haystack, const wxChar *needle)
280 {
281 wxChar *fnd;
282 while ((fnd = wxStrchr(haystack, *needle))) {
283 if (!wxStrcmp(fnd, needle)) return fnd;
284 haystack = fnd + 1;
285 }
286 return (wxChar *)NULL;
287 }
288
289 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
290 {
291 const wxChar *start = nptr;
292
293 // FIXME: only correct for C locale
294 while (wxIsspace(*nptr)) nptr++;
295 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
296 while (wxIsdigit(*nptr)) nptr++;
297 if (*nptr == wxT('.')) {
298 nptr++;
299 while (wxIsdigit(*nptr)) nptr++;
300 }
301 if (*nptr == wxT('E') || *nptr == wxT('e')) {
302 nptr++;
303 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
304 while (wxIsdigit(*nptr)) nptr++;
305 }
306
307 wxString data(nptr, nptr-start);
308 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
309 char *rdat = wxMBSTRINGCAST dat;
310 double ret = strtod(dat, &rdat);
311
312 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
313
314 return ret;
315 }
316
317 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
318 {
319 const wxChar *start = nptr;
320
321 // FIXME: only correct for C locale
322 while (wxIsspace(*nptr)) nptr++;
323 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
324 if (((base == 0) || (base == 16)) &&
325 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
326 nptr += 2;
327 base = 16;
328 }
329 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
330 else if (base == 0) base = 10;
331
332 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
333 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
334
335 wxString data(nptr, nptr-start);
336 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
337 char *rdat = wxMBSTRINGCAST dat;
338 long int ret = strtol(dat, &rdat, base);
339
340 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
341
342 return ret;
343 }
344 #endif
345
346 #ifdef wxNEED_WX_STDIO_H
347 WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
348 {
349 return fopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode));
350 }
351
352 WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
353 {
354 return freopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode), stream);
355 }
356
357 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...)
358 {
359 va_list argptr;
360 int ret;
361
362 va_start(argptr, fmt);
363 ret = wxVprintf(fmt, argptr);
364 va_end(argptr);
365 return ret;
366 }
367
368 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr)
369 {
370 wxString str;
371 str.PrintfV(fmt,argptr);
372 printf("%s", (const char*)str.mb_str());
373 return str.Len();
374 }
375
376 int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...)
377 {
378 va_list argptr;
379 int ret;
380
381 va_start(argptr, fmt);
382 ret = wxVfprintf(stream, fmt, argptr);
383 va_end(argptr);
384 return ret;
385 }
386
387 int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr)
388 {
389 wxString str;
390 str.PrintfV(fmt,argptr);
391 fprintf(stream, "%s", (const char*)str.mb_str());
392 return str.Len();
393 }
394
395 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
396 {
397 va_list argptr;
398 int ret;
399
400 va_start(argptr, fmt);
401 ret = wxVsprintf(buf, fmt, argptr);
402 va_end(argptr);
403 return ret;
404 }
405
406 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
407 {
408 // this might be sort of inefficient, but it doesn't matter since
409 // we'd prefer people to use wxString::Printf directly instead anyway
410 wxString str;
411 str.PrintfV(fmt,argptr);
412 wxStrcpy(buf,str.c_str());
413 return str.Len();
414 }
415
416 int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...)
417 {
418 va_list argptr;
419 int ret;
420
421 va_start(argptr, fmt);
422 ret = wxVsscanf(buf, fmt, argptr);
423 va_end(argptr);
424 return ret;
425 }
426
427 int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr)
428 {
429 int ret;
430 // this will work only for numeric conversion! Strings will not be converted correctly
431 // hopefully this is all we'll need
432 ret = vsscanf(wxConvLibc.cWX2MB(buf), wxConvLibc.cWX2MB(fmt), argptr);
433 return ret;
434 }
435 #endif
436
437 #ifndef wxAtof
438 double WXDLLEXPORT wxAtof(const wxChar *psz)
439 {
440 return atof(wxConvLibc.cWX2MB(psz));
441 }
442 #endif
443
444 #ifdef wxNEED_WX_STDLIB_H
445 int WXDLLEXPORT wxAtoi(const wxChar *psz)
446 {
447 return atoi(wxConvLibc.cWX2MB(psz));
448 }
449
450 long WXDLLEXPORT wxAtol(const wxChar *psz)
451 {
452 return atol(wxConvLibc.cWX2MB(psz));
453 }
454
455 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
456 {
457 static wxHashTable env;
458 // check if we already have stored the converted env var
459 wxObject *data = env.Get(name);
460 if (!data) {
461 // nope, retrieve it,
462 const char *val = getenv(wxConvLibc.cWX2MB(name));
463 if (!val) return (wxChar *)NULL;
464 // convert it,
465 data = (wxObject *)new wxString(val);
466 // and store it
467 env.Put(name, data);
468 }
469 // return converted env var
470 return (wxChar *)((wxString *)data)->c_str();
471 }
472
473 int WXDLLEXPORT wxSystem(const wxChar *psz)
474 {
475 return system(wxConvLibc.cWX2MB(psz));
476 }
477
478 #endif
479
480 #ifdef wxNEED_WX_TIME_H
481 WXDLLEXPORT size_t wxStrftime(wxChar *s, size_t max, const wxChar *fmt, const struct tm *tm)
482 {
483 if (!max) return 0;
484 char *buf = (char *)malloc(max);
485 size_t ret = strftime(buf, max, wxConvLibc.cWX2MB(fmt), tm);
486 if (ret) {
487 wxStrcpy(s, wxConvLibc.cMB2WX(buf));
488 free(buf);
489 return wxStrlen(s);
490 } else {
491 free(buf);
492 *s = 0;
493 return 0;
494 }
495 }
496 #endif