]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
Attempt to implement a bunch of wx string.h equivalents.
[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 wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
238 {
239 wxChar *ret = dest;
240 while (n && (*dest++ = *src++)) n--;
241 while (n) *dest++=0, n--; // the docs specify padding with zeroes
242 return ret;
243 }
244
245 WXDLLEXPORT wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
246 {
247 while (*s && !wxStrchr(accept, *s)) s++;
248 return (*s) ? (wxChar *)s : (wxChar *)NULL;
249 }
250
251 WXDLLEXPORT wxChar * wxStrrchr(const wxChar *s, wxChar c)
252 {
253 wxChar *ret = (wxChar *)NULL;
254 while (*s) {
255 if (*s == c) ret = (wxChar *)s;
256 s++;
257 }
258 return ret;
259 }
260
261 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
262 {
263 size_t len = 0;
264 while (wxStrchr(accept, *s++)) len++;
265 return len;
266 }
267
268 WXDLLEXPORT wxChar * wxStrstr(const wxChar *haystack, const wxChar *needle)
269 {
270 wxChar *fnd;
271 while ((fnd = wxStrchr(haystack, *needle))) {
272 if (!wxStrcmp(fnd, needle)) return fnd;
273 haystack = fnd + 1;
274 }
275 return (wxChar *)NULL;
276 }
277
278 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
279 {
280 const wxChar *start = nptr;
281
282 // FIXME: only correct for C locale
283 while (wxIsspace(*nptr)) nptr++;
284 if (*nptr == _T('+') || *nptr == _T('-')) nptr++;
285 while (wxIsdigit(*nptr)) nptr++;
286 if (*nptr == _T('.')) {
287 nptr++;
288 while (wxIsdigit(*nptr)) nptr++;
289 }
290 if (*nptr == _T('E') || *nptr == _T('e')) {
291 nptr++;
292 if (*nptr == _T('+') || *nptr == _T('-')) nptr++;
293 while (wxIsdigit(*nptr)) nptr++;
294 }
295
296 wxString data(nptr, nptr-start);
297 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
298 char *rdat = MBSTRINGCAST dat;
299 double ret = strtod(dat, &rdat);
300
301 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
302
303 return ret;
304 }
305
306 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
307 {
308 const wxChar *start = nptr;
309
310 // FIXME: only correct for C locale
311 while (wxIsspace(*nptr)) nptr++;
312 if (*nptr == _T('+') || *nptr == _T('-')) nptr++;
313 if (((base == 0) || (base == 16)) &&
314 (nptr[0] == _T('0') && nptr[1] == _T('x'))) {
315 nptr += 2;
316 base = 16;
317 }
318 else if ((base == 0) && (nptr[0] == _T('0'))) base = 8;
319 else if (base == 0) base = 10;
320
321 while ((wxIsdigit(*nptr) && (*nptr - _T('0') < base)) ||
322 (wxIsalpha(*nptr) && (wxToupper(*nptr) - _T('A') + 10 < base))) nptr++;
323
324 wxString data(nptr, nptr-start);
325 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
326 char *rdat = MBSTRINGCAST dat;
327 long int ret = strtol(dat, &rdat, base);
328
329 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
330
331 return ret;
332 }
333 #endif
334
335 #ifdef wxNEED_WX_STDIO_H
336 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...)
337 {
338 va_list argptr;
339 int ret;
340
341 va_start(argptr, fmt);
342 ret = wxVprintf(fmt, argptr);
343 va_end(argptr);
344 return ret;
345 }
346
347 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr)
348 {
349 wxString str;
350 str.PrintfV(fmt,argptr);
351 printf("%s", (const char*)str.mb_str());
352 return str.Len();
353 }
354
355 int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...)
356 {
357 va_list argptr;
358 int ret;
359
360 va_start(argptr, fmt);
361 ret = wxVfprintf(stream, fmt, argptr);
362 va_end(argptr);
363 return ret;
364 }
365
366 int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr)
367 {
368 wxString str;
369 str.PrintfV(fmt,argptr);
370 fprintf(stream, "%s", (const char*)str.mb_str());
371 return str.Len();
372 }
373
374 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
375 {
376 va_list argptr;
377 int ret;
378
379 va_start(argptr, fmt);
380 ret = wxVsprintf(buf, fmt, argptr);
381 va_end(argptr);
382 return ret;
383 }
384
385 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
386 {
387 // this might be sort of inefficient, but it doesn't matter since
388 // we'd prefer people to use wxString::Printf directly instead anyway
389 wxString str;
390 str.PrintfV(fmt,argptr);
391 wxStrcpy(buf,str.c_str());
392 return str.Len();
393 }
394
395 int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...)
396 {
397 va_list argptr;
398 int ret;
399
400 va_start(argptr, fmt);
401 ret = wxVsscanf(buf, fmt, argptr);
402 va_end(argptr);
403 return ret;
404 }
405
406 int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr)
407 {
408 int ret;
409 // this will work only for numeric conversion! Strings will not be converted correctly
410 // hopefully this is all we'll need
411 ret = vsscanf(wxConvLibc.cWX2MB(buf), wxConvLibc.cWX2MB(fmt), argptr);
412 return ret;
413 }
414 #endif
415
416 #ifndef wxAtof
417 double WXDLLEXPORT wxAtof(const wxChar *psz)
418 {
419 return atof(wxConvLibc.cWX2MB(psz));
420 }
421 #endif
422
423 #ifdef wxNEED_WX_STDLIB_H
424 int WXDLLEXPORT wxAtoi(const wxChar *psz)
425 {
426 return atoi(wxConvLibc.cWX2MB(psz));
427 }
428
429 long WXDLLEXPORT wxAtol(const wxChar *psz)
430 {
431 return atol(wxConvLibc.cWX2MB(psz));
432 }
433
434 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
435 {
436 static wxHashTable env;
437 // check if we already have stored the converted env var
438 wxObject *data = env.Get(name);
439 if (!data) {
440 // nope, retrieve it,
441 const char *val = getenv(wxConvLibc.cWX2MB(name));
442 if (!val) return (wxChar *)NULL;
443 // convert it,
444 data = (wxObject *)new wxString(val);
445 // and store it
446 env.Put(name, data);
447 }
448 // return converted env var
449 return (wxChar *)((wxString *)data)->c_str();
450 }
451
452 int WXDLLEXPORT wxSystem(const wxChar *psz)
453 {
454 return system(wxConvLibc.cWX2MB(psz));
455 }
456
457 #endif