]>
Commit | Line | Data |
---|---|---|
dd0ef332 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/wxcrt.cpp | |
3 | // Purpose: wxChar CRT wrappers implementation | |
4 | // Author: Ove Kaven | |
5 | // Modified by: Ron Lee, Francesco Montorsi | |
6 | // Created: 09/04/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) wxWidgets copyright | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // =========================================================================== | |
13 | // headers, declarations, constants | |
14 | // =========================================================================== | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
3a3dde0d | 23 | #include "wx/crt.h" |
dd0ef332 VS |
24 | |
25 | #define _ISOC9X_SOURCE 1 // to get vsscanf() | |
26 | #define _BSD_SOURCE 1 // to still get strdup() | |
27 | ||
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | ||
32 | #ifndef __WXWINCE__ | |
33 | #include <time.h> | |
34 | #include <locale.h> | |
35 | #else | |
36 | #include "wx/msw/wince/time.h" | |
37 | #endif | |
38 | ||
39 | #ifndef WX_PRECOMP | |
40 | #include "wx/string.h" | |
41 | #include "wx/hash.h" | |
42 | #include "wx/utils.h" // for wxMin and wxMax | |
43 | #include "wx/log.h" | |
44 | #endif | |
45 | ||
52de37c7 VS |
46 | #ifdef __WXWINCE__ |
47 | // there is no errno.h under CE apparently | |
48 | #define wxSET_ERRNO(value) | |
49 | #else | |
50 | #include <errno.h> | |
17482893 | 51 | |
52de37c7 | 52 | #define wxSET_ERRNO(value) errno = value |
17482893 VZ |
53 | #endif |
54 | ||
dd0ef332 VS |
55 | #if defined(__MWERKS__) && __MSL__ >= 0x6000 |
56 | namespace std {} | |
57 | using namespace std ; | |
58 | #endif | |
59 | ||
60 | #if wxUSE_WCHAR_T | |
37140a71 | 61 | WXDLLIMPEXP_BASE size_t wxMB2WC(wchar_t *buf, const char *psz, size_t n) |
dd0ef332 VS |
62 | { |
63 | // assume that we have mbsrtowcs() too if we have wcsrtombs() | |
64 | #ifdef HAVE_WCSRTOMBS | |
65 | mbstate_t mbstate; | |
66 | memset(&mbstate, 0, sizeof(mbstate_t)); | |
67 | #endif | |
68 | ||
69 | if (buf) { | |
70 | if (!n || !*psz) { | |
71 | if (n) *buf = wxT('\0'); | |
72 | return 0; | |
73 | } | |
74 | #ifdef HAVE_WCSRTOMBS | |
75 | return mbsrtowcs(buf, &psz, n, &mbstate); | |
76 | #else | |
77 | return wxMbstowcs(buf, psz, n); | |
78 | #endif | |
79 | } | |
80 | ||
81 | // note that we rely on common (and required by Unix98 but unfortunately not | |
82 | // C99) extension which allows to call mbs(r)towcs() with NULL output pointer | |
83 | // to just get the size of the needed buffer -- this is needed as otherwise | |
84 | // we have no idea about how much space we need and if the CRT doesn't | |
85 | // support it (the only currently known example being Metrowerks, see | |
3a3dde0d | 86 | // wx/crt.h) we don't use its mbstowcs() at all |
dd0ef332 VS |
87 | #ifdef HAVE_WCSRTOMBS |
88 | return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate); | |
89 | #else | |
90 | return wxMbstowcs((wchar_t *) NULL, psz, 0); | |
91 | #endif | |
92 | } | |
93 | ||
37140a71 | 94 | WXDLLIMPEXP_BASE size_t wxWC2MB(char *buf, const wchar_t *pwz, size_t n) |
dd0ef332 VS |
95 | { |
96 | #ifdef HAVE_WCSRTOMBS | |
97 | mbstate_t mbstate; | |
98 | memset(&mbstate, 0, sizeof(mbstate_t)); | |
99 | #endif | |
100 | ||
101 | if (buf) { | |
102 | if (!n || !*pwz) { | |
103 | // glibc2.1 chokes on null input | |
104 | if (n) *buf = '\0'; | |
105 | return 0; | |
106 | } | |
107 | #ifdef HAVE_WCSRTOMBS | |
108 | return wcsrtombs(buf, &pwz, n, &mbstate); | |
109 | #else | |
110 | return wxWcstombs(buf, pwz, n); | |
111 | #endif | |
112 | } | |
113 | ||
114 | #ifdef HAVE_WCSRTOMBS | |
115 | return wcsrtombs((char *) NULL, &pwz, 0, &mbstate); | |
116 | #else | |
117 | return wxWcstombs((char *) NULL, pwz, 0); | |
118 | #endif | |
119 | } | |
120 | #endif // wxUSE_WCHAR_T | |
121 | ||
37140a71 | 122 | WXDLLIMPEXP_BASE bool wxOKlibc() |
dd0ef332 VS |
123 | { |
124 | #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__) | |
125 | // glibc 2.0 uses UTF-8 even when it shouldn't | |
126 | wchar_t res = 0; | |
127 | if ((MB_CUR_MAX == 2) && | |
128 | (wxMB2WC(&res, "\xdd\xa5", 1) == 1) && | |
129 | (res==0x765)) { | |
130 | // this is UTF-8 allright, check whether that's what we want | |
131 | char *cur_locale = setlocale(LC_CTYPE, NULL); | |
132 | if ((strlen(cur_locale) < 4) || | |
133 | (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) || | |
134 | (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) { | |
135 | // nope, don't use libc conversion | |
136 | return false; | |
137 | } | |
138 | } | |
139 | #endif | |
140 | return true; | |
141 | } | |
142 | ||
52de37c7 VS |
143 | char* wxSetlocale(int category, const char *locale) |
144 | { | |
d6f2a891 VZ |
145 | #ifdef __WXWINCE__ |
146 | // FIXME-CE: there is no setlocale() in CE CRT, use SetThreadLocale()? | |
147 | wxUnusedVar(category); | |
148 | wxUnusedVar(locale); | |
149 | ||
150 | return NULL; | |
151 | #else // !__WXWINCE__ | |
52de37c7 VS |
152 | char *rv = setlocale(category, locale); |
153 | if ( locale != NULL /* setting locale, not querying */ && | |
154 | rv /* call was successful */ ) | |
155 | { | |
156 | wxUpdateLocaleIsUtf8(); | |
157 | } | |
158 | return rv; | |
d6f2a891 | 159 | #endif // __WXWINCE__/!__WXWINCE__ |
52de37c7 VS |
160 | } |
161 | ||
dd0ef332 VS |
162 | // ============================================================================ |
163 | // printf() functions business | |
164 | // ============================================================================ | |
165 | ||
166 | // special test mode: define all functions below even if we don't really need | |
167 | // them to be able to test them | |
168 | #ifdef wxTEST_PRINTF | |
169 | #undef wxFprintf | |
170 | #undef wxPrintf | |
171 | #undef wxSprintf | |
172 | #undef wxVfprintf | |
173 | #undef wxVsprintf | |
174 | #undef wxVprintf | |
175 | #undef wxVsnprintf_ | |
dd0ef332 VS |
176 | |
177 | #define wxNEED_WPRINTF | |
178 | ||
52de37c7 | 179 | int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list argptr ); |
dd0ef332 VS |
180 | #endif |
181 | ||
dd0ef332 | 182 | #if defined(__DMC__) |
52de37c7 VS |
183 | /* Digital Mars adds count to _stprintf (C99) so convert */ |
184 | int wxCRT_SprintfW (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... ) | |
185 | { | |
186 | va_list arglist; | |
dd0ef332 | 187 | |
52de37c7 VS |
188 | va_start( arglist, format ); |
189 | int iLen = swprintf ( s, -1, format, arglist ); | |
190 | va_end( arglist ); | |
191 | return iLen ; | |
192 | } | |
dd0ef332 VS |
193 | #endif //__DMC__ |
194 | ||
195 | // ---------------------------------------------------------------------------- | |
196 | // implement the standard IO functions for wide char if libc doesn't have them | |
197 | // ---------------------------------------------------------------------------- | |
198 | ||
52de37c7 VS |
199 | #ifndef wxCRT_FputsW |
200 | int wxCRT_FputsW(const wchar_t *ws, FILE *stream) | |
dd0ef332 VS |
201 | { |
202 | wxCharBuffer buf(wxConvLibc.cWC2MB(ws)); | |
203 | if ( !buf ) | |
204 | return -1; | |
205 | ||
206 | // counting the number of wide characters written isn't worth the trouble, | |
207 | // simply distinguish between ok and error | |
52de37c7 | 208 | return wxCRT_FputsA(buf, stream) == -1 ? -1 : 0; |
dd0ef332 | 209 | } |
52de37c7 | 210 | #endif // !wxCRT_FputsW |
dd0ef332 | 211 | |
52de37c7 VS |
212 | #ifndef wxCRT_PutsW |
213 | int wxCRT_PutsW(const wchar_t *ws) | |
dd0ef332 | 214 | { |
52de37c7 | 215 | int rc = wxCRT_FputsW(ws, stdout); |
dd0ef332 VS |
216 | if ( rc != -1 ) |
217 | { | |
52de37c7 | 218 | if ( wxCRT_FputsW(L"\n", stdout) == -1 ) |
dd0ef332 VS |
219 | return -1; |
220 | ||
221 | rc++; | |
222 | } | |
223 | ||
224 | return rc; | |
225 | } | |
52de37c7 | 226 | #endif // !wxCRT_PutsW |
dd0ef332 | 227 | |
52de37c7 VS |
228 | #ifndef wxCRT_FputcW |
229 | int /* not wint_t */ wxCRT_FputcW(wchar_t wc, FILE *stream) | |
dd0ef332 VS |
230 | { |
231 | wchar_t ws[2] = { wc, L'\0' }; | |
232 | ||
52de37c7 | 233 | return wxCRT_FputsW(ws, stream); |
dd0ef332 | 234 | } |
52de37c7 | 235 | #endif // !wxCRT_FputcW |
dd0ef332 VS |
236 | |
237 | // NB: we only implement va_list functions here, the ones taking ... are | |
238 | // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse | |
239 | // the definitions there to avoid duplicating them here | |
240 | #ifdef wxNEED_WPRINTF | |
241 | ||
242 | // TODO: implement the scanf() functions | |
8cb1060f | 243 | static int vwscanf(const wchar_t *format, va_list argptr) |
dd0ef332 VS |
244 | { |
245 | wxFAIL_MSG( _T("TODO") ); | |
246 | ||
247 | return -1; | |
248 | } | |
249 | ||
8cb1060f | 250 | static int vswscanf(const wchar_t *ws, const wchar_t *format, va_list argptr) |
dd0ef332 VS |
251 | { |
252 | // The best we can do without proper Unicode support in glibc is to | |
253 | // convert the strings into MB representation and run ANSI version | |
254 | // of the function. This doesn't work with %c and %s because of difference | |
255 | // in size of char and wchar_t, though. | |
256 | ||
257 | wxCHECK_MSG( wxStrstr(format, _T("%s")) == NULL, -1, | |
258 | _T("incomplete vswscanf implementation doesn't allow %s") ); | |
259 | wxCHECK_MSG( wxStrstr(format, _T("%c")) == NULL, -1, | |
260 | _T("incomplete vswscanf implementation doesn't allow %c") ); | |
261 | ||
c6255a6e | 262 | return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argptr); |
dd0ef332 VS |
263 | } |
264 | ||
8cb1060f | 265 | static int vfwscanf(FILE *stream, const wchar_t *format, va_list argptr) |
dd0ef332 VS |
266 | { |
267 | wxFAIL_MSG( _T("TODO") ); | |
268 | ||
269 | return -1; | |
270 | } | |
271 | ||
50e27899 | 272 | #define vswprintf wxCRT_VsnprintfW |
dd0ef332 | 273 | |
8cb1060f | 274 | static int vfwprintf(FILE *stream, const wchar_t *format, va_list argptr) |
dd0ef332 VS |
275 | { |
276 | wxString s; | |
277 | int rc = s.PrintfV(format, argptr); | |
278 | ||
279 | if ( rc != -1 ) | |
280 | { | |
281 | // we can't do much better without Unicode support in libc... | |
282 | if ( fprintf(stream, "%s", (const char*)s.mb_str() ) == -1 ) | |
283 | return -1; | |
284 | } | |
285 | ||
286 | return rc; | |
287 | } | |
288 | ||
8cb1060f | 289 | static int vwprintf(const wchar_t *format, va_list argptr) |
dd0ef332 | 290 | { |
52de37c7 | 291 | return wxCRT_VfprintfW(stdout, format, argptr); |
dd0ef332 VS |
292 | } |
293 | ||
294 | #endif // wxNEED_WPRINTF | |
295 | ||
dd0ef332 VS |
296 | // ---------------------------------------------------------------------------- |
297 | // wxPrintf(), wxScanf() and relatives | |
298 | // ---------------------------------------------------------------------------- | |
299 | ||
eb6cb207 VS |
300 | // FIXME-UTF8: do format conversion using (modified) wxFormatConverter in |
301 | // template wrappers, not here; note that it will needed to | |
302 | // translate all forms of string specifiers to %(l)s for wxPrintf(), | |
303 | // but it only should do what it did in 2.8 for wxScanf()! | |
304 | ||
52de37c7 VS |
305 | #ifndef wxCRT_PrintfW |
306 | int wxCRT_PrintfW( const wchar_t *format, ... ) | |
dd0ef332 VS |
307 | { |
308 | va_list argptr; | |
309 | va_start(argptr, format); | |
310 | ||
50e27899 | 311 | int ret = vwprintf( format, argptr ); |
dd0ef332 VS |
312 | |
313 | va_end(argptr); | |
314 | ||
315 | return ret; | |
316 | } | |
52de37c7 | 317 | #endif |
dd0ef332 | 318 | |
52de37c7 VS |
319 | #ifndef wxCRT_FprintfW |
320 | int wxCRT_FprintfW( FILE *stream, const wchar_t *format, ... ) | |
dd0ef332 VS |
321 | { |
322 | va_list argptr; | |
2523e9b7 | 323 | va_start( argptr, format ); |
dd0ef332 | 324 | |
50e27899 | 325 | int ret = vfwprintf( stream, format, argptr ); |
dd0ef332 VS |
326 | |
327 | va_end(argptr); | |
328 | ||
329 | return ret; | |
330 | } | |
52de37c7 | 331 | #endif |
dd0ef332 | 332 | |
52de37c7 VS |
333 | #ifndef wxCRT_VfprintfW |
334 | int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list argptr ) | |
2523e9b7 | 335 | { |
50e27899 | 336 | return vfwprintf( stream, format, argptr ); |
2523e9b7 | 337 | } |
52de37c7 | 338 | #endif |
2523e9b7 | 339 | |
52de37c7 VS |
340 | #ifndef wxCRT_VprintfW |
341 | int wxCRT_VprintfW( const wchar_t *format, va_list argptr ) | |
2523e9b7 | 342 | { |
50e27899 | 343 | return vwprintf( format, argptr ); |
2523e9b7 | 344 | } |
52de37c7 | 345 | #endif |
2523e9b7 | 346 | |
52de37c7 VS |
347 | #ifndef wxCRT_VsprintfW |
348 | int wxCRT_VsprintfW( wchar_t *str, const wchar_t *format, va_list argptr ) | |
2523e9b7 VS |
349 | { |
350 | // same as for wxSprintf() | |
50e27899 | 351 | return vswprintf(str, INT_MAX / 4, format, argptr); |
2523e9b7 | 352 | } |
52de37c7 | 353 | #endif |
2523e9b7 | 354 | |
52de37c7 | 355 | #ifndef wxCRT_ScanfW |
eb6cb207 VS |
356 | int wxCRT_ScanfW(const wchar_t *format, ...) |
357 | { | |
358 | va_list argptr; | |
359 | va_start(argptr, format); | |
360 | ||
28ffb1f2 JJ |
361 | #ifdef __VMS |
362 | #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD ) | |
50e27899 | 363 | int ret = std::vwscanf(format, argptr); |
28ffb1f2 | 364 | #else |
50e27899 | 365 | int ret = vwscanf(format, argptr); |
28ffb1f2 JJ |
366 | #endif |
367 | #else | |
50e27899 | 368 | int ret = vwscanf(format, argptr); |
28ffb1f2 | 369 | #endif |
b2dd2f28 | 370 | |
eb6cb207 VS |
371 | va_end(argptr); |
372 | ||
373 | return ret; | |
374 | } | |
52de37c7 | 375 | #endif |
eb6cb207 | 376 | |
52de37c7 | 377 | #ifndef wxCRT_SscanfW |
eb6cb207 VS |
378 | int wxCRT_SscanfW(const wchar_t *str, const wchar_t *format, ...) |
379 | { | |
380 | va_list argptr; | |
381 | va_start(argptr, format); | |
382 | ||
28ffb1f2 JJ |
383 | #ifdef __VMS |
384 | #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD ) | |
50e27899 | 385 | int ret = std::vswscanf(str, format, argptr); |
28ffb1f2 | 386 | #else |
50e27899 | 387 | int ret = vswscanf(str, format, argptr); |
28ffb1f2 JJ |
388 | #endif |
389 | #else | |
50e27899 | 390 | int ret = vswscanf(str, format, argptr); |
28ffb1f2 | 391 | #endif |
eb6cb207 VS |
392 | |
393 | va_end(argptr); | |
394 | ||
395 | return ret; | |
396 | } | |
52de37c7 | 397 | #endif |
eb6cb207 | 398 | |
52de37c7 | 399 | #ifndef wxCRT_FscanfW |
eb6cb207 VS |
400 | int wxCRT_FscanfW(FILE *stream, const wchar_t *format, ...) |
401 | { | |
402 | va_list argptr; | |
403 | va_start(argptr, format); | |
28ffb1f2 JJ |
404 | #ifdef __VMS |
405 | #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD ) | |
50e27899 | 406 | int ret = std::vfwscanf(stream, format, argptr); |
28ffb1f2 | 407 | #else |
50e27899 | 408 | int ret = vfwscanf(stream, format, argptr); |
28ffb1f2 JJ |
409 | #endif |
410 | #else | |
50e27899 | 411 | int ret = vfwscanf(stream, format, argptr); |
28ffb1f2 | 412 | #endif |
eb6cb207 VS |
413 | |
414 | va_end(argptr); | |
415 | ||
416 | return ret; | |
417 | } | |
52de37c7 | 418 | #endif |
eb6cb207 | 419 | |
52de37c7 | 420 | #ifndef wxCRT_VsscanfW |
eb6cb207 VS |
421 | int wxCRT_VsscanfW(const wchar_t *str, const wchar_t *format, va_list argptr) |
422 | { | |
28ffb1f2 JJ |
423 | #ifdef __VMS |
424 | #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD ) | |
50e27899 | 425 | return std::vswscanf(str, format, argptr); |
28ffb1f2 | 426 | #else |
50e27899 | 427 | return vswscanf(str, format, argptr); |
28ffb1f2 JJ |
428 | #endif |
429 | #else | |
50e27899 | 430 | return vswscanf(str, format, argptr); |
28ffb1f2 | 431 | #endif |
eb6cb207 | 432 | } |
52de37c7 | 433 | #endif |
2523e9b7 VS |
434 | |
435 | ||
436 | // ---------------------------------------------------------------------------- | |
437 | // wrappers to printf and scanf function families | |
438 | // ---------------------------------------------------------------------------- | |
439 | ||
d1f6e2cf VS |
440 | #if !wxUSE_UTF8_LOCALE_ONLY |
441 | int wxDoSprintfWchar(char *str, const wxChar *format, ...) | |
dd0ef332 VS |
442 | { |
443 | va_list argptr; | |
444 | va_start(argptr, format); | |
445 | ||
2523e9b7 | 446 | int rv = wxVsprintf(str, format, argptr); |
dd0ef332 VS |
447 | |
448 | va_end(argptr); | |
2523e9b7 VS |
449 | return rv; |
450 | } | |
d1f6e2cf VS |
451 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
452 | ||
453 | #if wxUSE_UNICODE_UTF8 | |
454 | int wxDoSprintfUtf8(char *str, const char *format, ...) | |
455 | { | |
456 | va_list argptr; | |
457 | va_start(argptr, format); | |
458 | ||
459 | int rv = wxVsprintf(str, format, argptr); | |
460 | ||
461 | va_end(argptr); | |
462 | return rv; | |
463 | } | |
464 | #endif // wxUSE_UNICODE_UTF8 | |
2523e9b7 VS |
465 | |
466 | #if wxUSE_UNICODE | |
d1f6e2cf VS |
467 | |
468 | #if !wxUSE_UTF8_LOCALE_ONLY | |
469 | int wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...) | |
2523e9b7 VS |
470 | { |
471 | va_list argptr; | |
472 | va_start(argptr, format); | |
dd0ef332 | 473 | |
2523e9b7 VS |
474 | int rv = wxVsprintf(str, format, argptr); |
475 | ||
476 | va_end(argptr); | |
477 | return rv; | |
dd0ef332 | 478 | } |
d1f6e2cf VS |
479 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
480 | ||
481 | #if wxUSE_UNICODE_UTF8 | |
482 | int wxDoSprintfUtf8(wchar_t *str, const char *format, ...) | |
483 | { | |
484 | va_list argptr; | |
485 | va_start(argptr, format); | |
486 | ||
487 | int rv = wxVsprintf(str, format, argptr); | |
488 | ||
489 | va_end(argptr); | |
490 | return rv; | |
491 | } | |
492 | #endif // wxUSE_UNICODE_UTF8 | |
493 | ||
494 | #endif // wxUSE_UNICODE | |
495 | ||
496 | #if !wxUSE_UTF8_LOCALE_ONLY | |
497 | int wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...) | |
498 | { | |
499 | va_list argptr; | |
500 | va_start(argptr, format); | |
dd0ef332 | 501 | |
d1f6e2cf VS |
502 | int rv = wxVsnprintf(str, size, format, argptr); |
503 | ||
504 | va_end(argptr); | |
505 | return rv; | |
506 | } | |
507 | #endif // !wxUSE_UTF8_LOCALE_ONLY | |
508 | ||
509 | #if wxUSE_UNICODE_UTF8 | |
510 | int wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...) | |
dd0ef332 VS |
511 | { |
512 | va_list argptr; | |
2523e9b7 | 513 | va_start(argptr, format); |
dd0ef332 | 514 | |
2523e9b7 | 515 | int rv = wxVsnprintf(str, size, format, argptr); |
dd0ef332 VS |
516 | |
517 | va_end(argptr); | |
2523e9b7 VS |
518 | return rv; |
519 | } | |
d1f6e2cf | 520 | #endif // wxUSE_UNICODE_UTF8 |
dd0ef332 | 521 | |
2523e9b7 | 522 | #if wxUSE_UNICODE |
d1f6e2cf VS |
523 | |
524 | #if !wxUSE_UTF8_LOCALE_ONLY | |
525 | int wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...) | |
2523e9b7 VS |
526 | { |
527 | va_list argptr; | |
528 | va_start(argptr, format); | |
529 | ||
530 | int rv = wxVsnprintf(str, size, format, argptr); | |
531 | ||
532 | va_end(argptr); | |
533 | return rv; | |
dd0ef332 | 534 | } |
d1f6e2cf VS |
535 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
536 | ||
537 | #if wxUSE_UNICODE_UTF8 | |
538 | int wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...) | |
539 | { | |
540 | va_list argptr; | |
541 | va_start(argptr, format); | |
542 | ||
543 | int rv = wxVsnprintf(str, size, format, argptr); | |
544 | ||
545 | va_end(argptr); | |
546 | return rv; | |
547 | } | |
548 | #endif // wxUSE_UNICODE_UTF8 | |
549 | ||
550 | #endif // wxUSE_UNICODE | |
dd0ef332 | 551 | |
2523e9b7 VS |
552 | |
553 | #ifdef HAVE_BROKEN_VSNPRINTF_DECL | |
554 | #define vsnprintf wx_fixed_vsnprintf | |
555 | #endif | |
556 | ||
557 | #if wxUSE_UNICODE | |
d1f6e2cf | 558 | |
80f8355d VZ |
559 | namespace |
560 | { | |
561 | ||
d1f6e2cf | 562 | #if !wxUSE_UTF8_LOCALE_ONLY |
ec873c94 | 563 | int ConvertStringToBuf(const wxString& s, char *out, size_t outsize) |
dd0ef332 | 564 | { |
2523e9b7 VS |
565 | const wxWX2WCbuf buf = s.wc_str(); |
566 | ||
567 | size_t len = wxConvLibc.FromWChar(out, outsize, buf); | |
568 | if ( len != wxCONV_FAILED ) | |
569 | return len-1; | |
570 | else | |
571 | return wxConvLibc.FromWChar(NULL, 0, buf); | |
dd0ef332 | 572 | } |
d1f6e2cf | 573 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
dd0ef332 | 574 | |
2523e9b7 | 575 | #if wxUSE_UNICODE_UTF8 |
ec873c94 | 576 | int ConvertStringToBuf(const wxString& s, wchar_t *out, size_t outsize) |
dd0ef332 | 577 | { |
2523e9b7 | 578 | const wxWX2WCbuf buf(s.wc_str()); |
ec873c94 | 579 | size_t len = s.length(); // same as buf length for wchar_t* |
2523e9b7 | 580 | if ( outsize > len ) |
ec873c94 | 581 | { |
2523e9b7 | 582 | memcpy(out, buf, (len+1) * sizeof(wchar_t)); |
ec873c94 VS |
583 | } |
584 | else // not enough space | |
585 | { | |
586 | memcpy(out, buf, (outsize-1) * sizeof(wchar_t)); | |
587 | out[outsize-1] = 0; | |
588 | } | |
2523e9b7 | 589 | return len; |
dd0ef332 | 590 | } |
d1f6e2cf | 591 | #endif // wxUSE_UNICODE_UTF8 |
dd0ef332 | 592 | |
80f8355d VZ |
593 | } // anonymous namespace |
594 | ||
2523e9b7 VS |
595 | template<typename T> |
596 | static size_t PrintfViaString(T *out, size_t outsize, | |
597 | const wxString& format, va_list argptr) | |
dd0ef332 | 598 | { |
2523e9b7 | 599 | wxString s; |
c6255a6e | 600 | s.PrintfV(format, argptr); |
2523e9b7 | 601 | |
ec873c94 | 602 | return ConvertStringToBuf(s, out, outsize); |
dd0ef332 | 603 | } |
2523e9b7 | 604 | #endif // wxUSE_UNICODE |
dd0ef332 | 605 | |
2523e9b7 | 606 | int wxVsprintf(char *str, const wxString& format, va_list argptr) |
dd0ef332 | 607 | { |
2523e9b7 | 608 | #if wxUSE_UTF8_LOCALE_ONLY |
04fd66c9 | 609 | return wxCRT_VsprintfA(str, format.wx_str(), argptr); |
2523e9b7 VS |
610 | #else |
611 | #if wxUSE_UNICODE_UTF8 | |
612 | if ( wxLocaleIsUtf8 ) | |
04fd66c9 | 613 | return wxCRT_VsprintfA(str, format.wx_str(), argptr); |
2523e9b7 VS |
614 | else |
615 | #endif | |
616 | #if wxUSE_UNICODE | |
c6255a6e | 617 | return PrintfViaString(str, wxNO_LEN, format, argptr); |
2523e9b7 | 618 | #else |
52de37c7 | 619 | return wxCRT_VsprintfA(str, format.mb_str(), argptr); |
2523e9b7 VS |
620 | #endif |
621 | #endif | |
dd0ef332 | 622 | } |
dd0ef332 | 623 | |
2523e9b7 VS |
624 | #if wxUSE_UNICODE |
625 | int wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) | |
dd0ef332 | 626 | { |
2523e9b7 | 627 | #if wxUSE_UNICODE_WCHAR |
91a151d3 CE |
628 | #ifdef __DMC__ |
629 | /* | |
630 | This fails with a bug similar to | |
631 | http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=c++.beta&artnum=680 | |
632 | in DMC 8.49 and 8.50 | |
633 | I don't see it being used in the wxWidgets sources at present (oct 2007) CE | |
634 | */ | |
635 | #pragma message ( "warning ::::: wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) not yet implemented" ) | |
636 | wxFAIL_MSG( _T("TODO") ); | |
637 | ||
638 | return -1; | |
639 | #else | |
52de37c7 | 640 | return wxCRT_VsprintfW(str, format.wc_str(), argptr); |
91a151d3 | 641 | #endif //DMC |
2523e9b7 VS |
642 | #else // wxUSE_UNICODE_UTF8 |
643 | #if !wxUSE_UTF8_LOCALE_ONLY | |
644 | if ( !wxLocaleIsUtf8 ) | |
52de37c7 | 645 | return wxCRT_VsprintfW(str, format.wc_str(), argptr); |
2523e9b7 VS |
646 | else |
647 | #endif | |
c6255a6e | 648 | return PrintfViaString(str, wxNO_LEN, format, argptr); |
2523e9b7 | 649 | #endif // wxUSE_UNICODE_UTF8 |
dd0ef332 | 650 | } |
2523e9b7 | 651 | #endif // wxUSE_UNICODE |
dd0ef332 | 652 | |
2523e9b7 VS |
653 | int wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr) |
654 | { | |
655 | int rv; | |
2523e9b7 | 656 | #if wxUSE_UTF8_LOCALE_ONLY |
52de37c7 | 657 | rv = wxCRT_VsnprintfA(str, size, format.wx_str(), argptr); |
2523e9b7 VS |
658 | #else |
659 | #if wxUSE_UNICODE_UTF8 | |
660 | if ( wxLocaleIsUtf8 ) | |
52de37c7 | 661 | rv = wxCRT_VsnprintfA(str, size, format.wx_str(), argptr); |
2523e9b7 VS |
662 | else |
663 | #endif | |
664 | #if wxUSE_UNICODE | |
665 | { | |
666 | // NB: if this code is called, then wxString::PrintV() would use the | |
667 | // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV() | |
668 | // from here | |
c6255a6e | 669 | rv = PrintfViaString(str, size, format, argptr); |
2523e9b7 VS |
670 | } |
671 | #else | |
52de37c7 | 672 | rv = wxCRT_VsnprintfA(str, size, format.mb_str(), argptr); |
2523e9b7 VS |
673 | #endif |
674 | #endif | |
675 | ||
676 | // VsnprintfTestCase reveals that glibc's implementation of vswprintf | |
677 | // doesn't nul terminate on truncation. | |
678 | str[size - 1] = 0; | |
679 | ||
680 | return rv; | |
681 | } | |
682 | ||
683 | #if wxUSE_UNICODE | |
684 | int wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr) | |
685 | { | |
686 | int rv; | |
2523e9b7 VS |
687 | |
688 | #if wxUSE_UNICODE_WCHAR | |
52de37c7 | 689 | rv = wxCRT_VsnprintfW(str, size, format.wc_str(), argptr); |
2523e9b7 VS |
690 | #else // wxUSE_UNICODE_UTF8 |
691 | #if !wxUSE_UTF8_LOCALE_ONLY | |
692 | if ( !wxLocaleIsUtf8 ) | |
52de37c7 | 693 | rv = wxCRT_VsnprintfW(str, size, format.wc_str(), argptr); |
2523e9b7 VS |
694 | else |
695 | #endif | |
696 | { | |
697 | // NB: if this code is called, then wxString::PrintV() would use the | |
698 | // char* version of wxVsnprintf(), so it's safe to use PrintV() | |
699 | // from here | |
c6255a6e | 700 | rv = PrintfViaString(str, size, format, argptr); |
2523e9b7 VS |
701 | } |
702 | #endif // wxUSE_UNICODE_UTF8 | |
703 | ||
704 | // VsnprintfTestCase reveals that glibc's implementation of vswprintf | |
705 | // doesn't nul terminate on truncation. | |
706 | str[size - 1] = 0; | |
707 | ||
708 | return rv; | |
709 | } | |
710 | #endif // wxUSE_UNICODE | |
dd0ef332 VS |
711 | |
712 | #if wxUSE_WCHAR_T | |
713 | ||
714 | // ---------------------------------------------------------------------------- | |
715 | // ctype.h stuff (currently unused) | |
716 | // ---------------------------------------------------------------------------- | |
717 | ||
dd0ef332 VS |
718 | #ifdef wxNEED_WX_MBSTOWCS |
719 | ||
37140a71 | 720 | WXDLLIMPEXP_BASE size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen) |
dd0ef332 VS |
721 | { |
722 | if (!out) | |
723 | { | |
724 | size_t outsize = 0; | |
725 | while(*in++) | |
726 | outsize++; | |
727 | return outsize; | |
728 | } | |
729 | ||
730 | const char* origin = in; | |
731 | ||
732 | while (outlen-- && *in) | |
733 | { | |
734 | *out++ = (wchar_t) *in++; | |
735 | } | |
736 | ||
737 | *out = '\0'; | |
738 | ||
739 | return in - origin; | |
740 | } | |
741 | ||
37140a71 | 742 | WXDLLIMPEXP_BASE size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen) |
dd0ef332 VS |
743 | { |
744 | if (!out) | |
745 | { | |
746 | size_t outsize = 0; | |
747 | while(*in++) | |
748 | outsize++; | |
749 | return outsize; | |
750 | } | |
751 | ||
752 | const wchar_t* origin = in; | |
753 | ||
754 | while (outlen-- && *in) | |
755 | { | |
756 | *out++ = (char) *in++; | |
757 | } | |
758 | ||
759 | *out = '\0'; | |
760 | ||
761 | return in - origin; | |
762 | } | |
763 | ||
764 | #endif // wxNEED_WX_MBSTOWCS | |
765 | ||
52de37c7 | 766 | #ifndef wxCRT_StrdupA |
37140a71 | 767 | WXDLLIMPEXP_BASE char *wxCRT_StrdupA(const char *s) |
dd0ef332 VS |
768 | { |
769 | return strcpy((char *)malloc(strlen(s) + 1), s); | |
770 | } | |
52de37c7 | 771 | #endif // wxCRT_StrdupA |
dd0ef332 | 772 | |
52de37c7 | 773 | #ifndef wxCRT_StrdupW |
37140a71 | 774 | WXDLLIMPEXP_BASE wchar_t * wxCRT_StrdupW(const wchar_t *pwz) |
dd0ef332 VS |
775 | { |
776 | size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t); | |
777 | wchar_t *ret = (wchar_t *) malloc(size); | |
778 | memcpy(ret, pwz, size); | |
779 | return ret; | |
780 | } | |
52de37c7 | 781 | #endif // wxCRT_StrdupW |
dd0ef332 | 782 | |
52de37c7 | 783 | #ifndef wxCRT_StricmpA |
37140a71 | 784 | WXDLLIMPEXP_BASE int wxCRT_StricmpA(const char *psz1, const char *psz2) |
52de37c7 VS |
785 | { |
786 | register char c1, c2; | |
787 | do { | |
788 | c1 = wxTolower(*psz1++); | |
789 | c2 = wxTolower(*psz2++); | |
790 | } while ( c1 && (c1 == c2) ); | |
791 | return c1 - c2; | |
792 | } | |
793 | #endif // !defined(wxCRT_StricmpA) | |
dd0ef332 | 794 | |
52de37c7 | 795 | #ifndef wxCRT_StricmpW |
37140a71 | 796 | WXDLLIMPEXP_BASE int wxCRT_StricmpW(const wchar_t *psz1, const wchar_t *psz2) |
dd0ef332 | 797 | { |
52de37c7 | 798 | register wchar_t c1, c2; |
dd0ef332 VS |
799 | do { |
800 | c1 = wxTolower(*psz1++); | |
801 | c2 = wxTolower(*psz2++); | |
802 | } while ( c1 && (c1 == c2) ); | |
803 | return c1 - c2; | |
804 | } | |
52de37c7 | 805 | #endif // !defined(wxCRT_StricmpW) |
dd0ef332 | 806 | |
52de37c7 | 807 | #ifndef wxCRT_StrnicmpA |
37140a71 | 808 | WXDLLIMPEXP_BASE int wxCRT_StrnicmpA(const char *s1, const char *s2, size_t n) |
dd0ef332 VS |
809 | { |
810 | // initialize the variables just to suppress stupid gcc warning | |
52de37c7 | 811 | register char c1 = 0, c2 = 0; |
dd0ef332 VS |
812 | while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++; |
813 | if (n) { | |
814 | if (c1 < c2) return -1; | |
815 | if (c1 > c2) return 1; | |
816 | } | |
817 | return 0; | |
818 | } | |
52de37c7 | 819 | #endif // !defined(wxCRT_StrnicmpA) |
cb352236 | 820 | |
52de37c7 | 821 | #ifndef wxCRT_StrnicmpW |
37140a71 | 822 | WXDLLIMPEXP_BASE int wxCRT_StrnicmpW(const wchar_t *s1, const wchar_t *s2, size_t n) |
cb352236 | 823 | { |
52de37c7 VS |
824 | // initialize the variables just to suppress stupid gcc warning |
825 | register wchar_t c1 = 0, c2 = 0; | |
826 | while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++; | |
827 | if (n) { | |
828 | if (c1 < c2) return -1; | |
829 | if (c1 > c2) return 1; | |
830 | } | |
831 | return 0; | |
dd0ef332 | 832 | } |
52de37c7 | 833 | #endif // !defined(wxCRT_StrnicmpW) |
dd0ef332 VS |
834 | |
835 | // ---------------------------------------------------------------------------- | |
836 | // string.h functions | |
837 | // ---------------------------------------------------------------------------- | |
838 | ||
410390cf VS |
839 | // this (and wxCRT_StrncmpW below) are extern "C" because they are needed |
840 | // by regex code, the rest isn't needed, so it's not declared as extern "C" | |
841 | #ifndef wxCRT_StrlenW | |
37140a71 | 842 | extern "C" WXDLLIMPEXP_BASE size_t wxCRT_StrlenW(const wchar_t *s) |
dd0ef332 VS |
843 | { |
844 | size_t n = 0; | |
845 | while ( *s++ ) | |
846 | n++; | |
847 | ||
848 | return n; | |
849 | } | |
52de37c7 | 850 | #endif |
dd0ef332 | 851 | |
410390cf VS |
852 | // ---------------------------------------------------------------------------- |
853 | // stdlib.h functions | |
854 | // ---------------------------------------------------------------------------- | |
dd0ef332 | 855 | |
52de37c7 | 856 | #ifndef wxCRT_GetenvW |
37140a71 | 857 | WXDLLIMPEXP_BASE wchar_t* wxCRT_GetenvW(const wchar_t *name) |
dd0ef332 | 858 | { |
dd0ef332 VS |
859 | // NB: buffer returned by getenv() is allowed to be overwritten next |
860 | // time getenv() is called, so it is OK to use static string | |
861 | // buffer to hold the data. | |
f62262aa VS |
862 | static wxWCharBuffer value((wchar_t*)NULL); |
863 | value = wxConvLibc.cMB2WC(getenv(wxConvLibc.cWC2MB(name))); | |
dd0ef332 | 864 | return value.data(); |
dd0ef332 | 865 | } |
52de37c7 | 866 | #endif // !wxCRT_GetenvW |
dd0ef332 | 867 | |
52de37c7 | 868 | #ifndef wxCRT_StrftimeW |
37140a71 | 869 | WXDLLIMPEXP_BASE size_t |
52de37c7 | 870 | wxCRT_StrftimeW(wchar_t *s, size_t maxsize, const wchar_t *fmt, const struct tm *tm) |
dd0ef332 VS |
871 | { |
872 | if ( !maxsize ) | |
873 | return 0; | |
874 | ||
875 | wxCharBuffer buf(maxsize); | |
876 | ||
877 | wxCharBuffer bufFmt(wxConvLibc.cWX2MB(fmt)); | |
878 | if ( !bufFmt ) | |
879 | return 0; | |
880 | ||
881 | size_t ret = strftime(buf.data(), maxsize, bufFmt, tm); | |
882 | if ( !ret ) | |
883 | return 0; | |
884 | ||
885 | wxWCharBuffer wbuf = wxConvLibc.cMB2WX(buf); | |
886 | if ( !wbuf ) | |
887 | return 0; | |
888 | ||
52de37c7 VS |
889 | wxCRT_StrncpyW(s, wbuf, maxsize); |
890 | return wxCRT_StrlenW(s); | |
dd0ef332 | 891 | } |
52de37c7 | 892 | #endif // !wxCRT_StrftimeW |
dd0ef332 VS |
893 | |
894 | #endif // wxUSE_WCHAR_T | |
895 | ||
bdcb2137 | 896 | #ifdef wxLongLong_t |
52de37c7 VS |
897 | template<typename T> |
898 | static wxULongLong_t | |
899 | wxCRT_StrtoullBase(const T* nptr, T** endptr, int base, T* sign) | |
17482893 VZ |
900 | { |
901 | wxULongLong_t sum = 0; | |
902 | wxString wxstr(nptr); | |
903 | wxString::const_iterator i = wxstr.begin(); | |
904 | wxString::const_iterator end = wxstr.end(); | |
905 | ||
906 | // Skip spaces | |
b2dd2f28 | 907 | while ( i != end && wxIsspace(*i) ) ++i; |
17482893 VZ |
908 | |
909 | // Starts with sign? | |
910 | *sign = wxT(' '); | |
911 | if ( i != end ) | |
912 | { | |
52de37c7 | 913 | T c = *i; |
17482893 VZ |
914 | if ( c == wxT('+') || c == wxT('-') ) |
915 | { | |
916 | *sign = c; | |
b2dd2f28 | 917 | ++i; |
17482893 VZ |
918 | } |
919 | } | |
920 | ||
921 | // Starts with 0x? | |
922 | if ( i != end && *i == wxT('0') ) | |
923 | { | |
b2dd2f28 | 924 | ++i; |
17482893 VZ |
925 | if ( i != end ) |
926 | { | |
927 | if ( *i == wxT('x') && (base == 16 || base == 0) ) | |
928 | { | |
929 | base = 16; | |
b2dd2f28 | 930 | ++i; |
17482893 VZ |
931 | } |
932 | else | |
933 | { | |
934 | if ( endptr ) | |
52de37c7 | 935 | *endptr = (T*) nptr; |
17482893 VZ |
936 | wxSET_ERRNO(EINVAL); |
937 | return sum; | |
938 | } | |
939 | } | |
940 | else | |
b2dd2f28 | 941 | --i; |
17482893 VZ |
942 | } |
943 | ||
944 | if ( base == 0 ) | |
945 | base = 10; | |
946 | ||
b2dd2f28 | 947 | for ( ; i != end; ++i ) |
17482893 VZ |
948 | { |
949 | unsigned int n; | |
950 | ||
52de37c7 | 951 | T c = *i; |
f5851311 | 952 | if ( c >= '0' ) |
17482893 | 953 | { |
f5851311 | 954 | if ( c <= '9' ) |
17482893 VZ |
955 | n = c - wxT('0'); |
956 | else | |
957 | n = wxTolower(c) - wxT('a') + 10; | |
958 | } | |
959 | else | |
960 | break; | |
961 | ||
962 | if ( n >= (unsigned int)base ) | |
963 | // Invalid character (for this base) | |
964 | break; | |
965 | ||
966 | wxULongLong_t prevsum = sum; | |
967 | sum = (sum * base) + n; | |
968 | ||
969 | if ( sum < prevsum ) | |
970 | { | |
971 | wxSET_ERRNO(ERANGE); | |
972 | break; | |
973 | } | |
974 | } | |
975 | ||
976 | if ( endptr ) | |
977 | { | |
52de37c7 | 978 | *endptr = (T*)(nptr + (i - wxstr.begin())); |
17482893 VZ |
979 | } |
980 | ||
981 | return sum; | |
982 | } | |
983 | ||
52de37c7 VS |
984 | template<typename T> |
985 | static wxULongLong_t wxCRT_DoStrtoull(const T* nptr, T** endptr, int base) | |
17482893 | 986 | { |
52de37c7 | 987 | T sign; |
8fd7108e | 988 | wxULongLong_t uval = ::wxCRT_StrtoullBase(nptr, endptr, base, &sign); |
17482893 VZ |
989 | |
990 | if ( sign == wxT('-') ) | |
991 | { | |
992 | wxSET_ERRNO(ERANGE); | |
993 | uval = 0; | |
994 | } | |
995 | ||
996 | return uval; | |
997 | } | |
998 | ||
52de37c7 VS |
999 | template<typename T> |
1000 | static wxLongLong_t wxCRT_DoStrtoll(const T* nptr, T** endptr, int base) | |
17482893 | 1001 | { |
52de37c7 | 1002 | T sign; |
8fd7108e | 1003 | wxULongLong_t uval = ::wxCRT_StrtoullBase(nptr, endptr, base, &sign); |
17482893 VZ |
1004 | wxLongLong_t val = 0; |
1005 | ||
1006 | if ( sign == wxT('-') ) | |
1007 | { | |
1008 | if ( uval <= wxULL(wxINT64_MAX+1) ) | |
1009 | { | |
1010 | if ( uval == wxULL(wxINT64_MAX+1)) | |
1011 | val = -((wxLongLong_t)wxINT64_MAX) - 1; | |
1012 | else | |
1013 | val = -((wxLongLong_t)uval); | |
1014 | } | |
1015 | else | |
1016 | { | |
1017 | wxSET_ERRNO(ERANGE); | |
1018 | } | |
1019 | } | |
1020 | else if ( uval <= wxINT64_MAX ) | |
1021 | { | |
1022 | val = uval; | |
1023 | } | |
1024 | else | |
1025 | { | |
1026 | wxSET_ERRNO(ERANGE); | |
1027 | } | |
1028 | ||
1029 | return val; | |
1030 | } | |
52de37c7 VS |
1031 | |
1032 | #ifndef wxCRT_StrtollA | |
1033 | wxLongLong_t wxCRT_StrtollA(const char* nptr, char** endptr, int base) | |
1034 | { return wxCRT_DoStrtoll(nptr, endptr, base); } | |
1035 | #endif | |
1036 | #ifndef wxCRT_StrtollW | |
1037 | wxLongLong_t wxCRT_StrtollW(const wchar_t* nptr, wchar_t** endptr, int base) | |
1038 | { return wxCRT_DoStrtoll(nptr, endptr, base); } | |
1039 | #endif | |
1040 | ||
1041 | #ifndef wxCRT_StrtoullA | |
1042 | wxULongLong_t wxCRT_StrtoullA(const char* nptr, char** endptr, int base) | |
1043 | { return wxCRT_DoStrtoull(nptr, endptr, base); } | |
1044 | #endif | |
1045 | #ifndef wxCRT_StrtoullW | |
1046 | wxULongLong_t wxCRT_StrtoullW(const wchar_t* nptr, wchar_t** endptr, int base) | |
1047 | { return wxCRT_DoStrtoull(nptr, endptr, base); } | |
1048 | #endif | |
17482893 | 1049 | |
bdcb2137 VS |
1050 | #endif // wxLongLong_t |
1051 | ||
dd0ef332 VS |
1052 | // ---------------------------------------------------------------------------- |
1053 | // functions which we may need even if !wxUSE_WCHAR_T | |
1054 | // ---------------------------------------------------------------------------- | |
1055 | ||
52de37c7 VS |
1056 | template<typename T> |
1057 | static T *wxCRT_DoStrtok(T *psz, const T *delim, T **save_ptr) | |
dd0ef332 VS |
1058 | { |
1059 | if (!psz) | |
1060 | { | |
1061 | psz = *save_ptr; | |
1062 | if ( !psz ) | |
1063 | return NULL; | |
1064 | } | |
1065 | ||
1066 | psz += wxStrspn(psz, delim); | |
1067 | if (!*psz) | |
1068 | { | |
52de37c7 VS |
1069 | *save_ptr = (T *)NULL; |
1070 | return (T *)NULL; | |
dd0ef332 VS |
1071 | } |
1072 | ||
52de37c7 | 1073 | T *ret = psz; |
dd0ef332 VS |
1074 | psz = wxStrpbrk(psz, delim); |
1075 | if (!psz) | |
1076 | { | |
52de37c7 | 1077 | *save_ptr = (T*)NULL; |
dd0ef332 VS |
1078 | } |
1079 | else | |
1080 | { | |
1081 | *psz = wxT('\0'); | |
1082 | *save_ptr = psz + 1; | |
1083 | } | |
1084 | ||
1085 | return ret; | |
1086 | } | |
1087 | ||
52de37c7 VS |
1088 | #ifndef wxCRT_StrtokA |
1089 | char *wxCRT_StrtokA(char *psz, const char *delim, char **save_ptr) | |
1090 | { return wxCRT_DoStrtok(psz, delim, save_ptr); } | |
1091 | #endif | |
1092 | #ifndef wxCRT_StrtokW | |
1093 | wchar_t *wxCRT_StrtokW(wchar_t *psz, const wchar_t *delim, wchar_t **save_ptr) | |
1094 | { return wxCRT_DoStrtok(psz, delim, save_ptr); } | |
1095 | #endif | |
dd0ef332 VS |
1096 | |
1097 | // ---------------------------------------------------------------------------- | |
1098 | // missing C RTL functions | |
1099 | // ---------------------------------------------------------------------------- | |
1100 | ||
1101 | #ifdef wxNEED_STRDUP | |
1102 | ||
1103 | char *strdup(const char *s) | |
1104 | { | |
1105 | char *dest = (char*) malloc( strlen( s ) + 1 ) ; | |
1106 | if ( dest ) | |
1107 | strcpy( dest , s ) ; | |
1108 | return dest ; | |
1109 | } | |
1110 | #endif // wxNEED_STRDUP | |
1111 | ||
1112 | #if defined(__WXWINCE__) && (_WIN32_WCE <= 211) | |
1113 | ||
1114 | void *calloc( size_t num, size_t size ) | |
1115 | { | |
1116 | void** ptr = (void **)malloc(num * size); | |
1117 | memset( ptr, 0, num * size); | |
1118 | return ptr; | |
1119 | } | |
1120 | ||
1121 | #endif // __WXWINCE__ <= 211 | |
1122 | ||
52de37c7 | 1123 | // ============================================================================ |
cb352236 | 1124 | // wxLocaleIsUtf8 |
52de37c7 | 1125 | // ============================================================================ |
cb352236 VS |
1126 | |
1127 | #if wxUSE_UNICODE_UTF8 | |
1128 | ||
1129 | #if !wxUSE_UTF8_LOCALE_ONLY | |
1130 | bool wxLocaleIsUtf8 = false; // the safer setting if not known | |
1131 | #endif | |
1132 | ||
1133 | static bool wxIsLocaleUtf8() | |
1134 | { | |
1135 | // NB: we intentionally don't use wxLocale::GetSystemEncodingName(), | |
1136 | // because a) it may be unavailable in some builds and b) has slightly | |
1137 | // different semantics (default locale instead of current) | |
1138 | ||
1139 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) | |
1140 | // GNU libc provides current character set this way (this conforms to | |
1141 | // Unix98) | |
1142 | const char *charset = nl_langinfo(CODESET); | |
1143 | if ( charset ) | |
1144 | { | |
1145 | // "UTF-8" is used by modern glibc versions, but test other variants | |
1146 | // as well, just in case: | |
e40dfb3a VS |
1147 | if ( strcmp(charset, "UTF-8") == 0 || |
1148 | strcmp(charset, "utf-8") == 0 || | |
1149 | strcmp(charset, "UTF8") == 0 || | |
1150 | strcmp(charset, "utf8") == 0 ) | |
1151 | { | |
1152 | return true; | |
1153 | } | |
cb352236 | 1154 | } |
cb352236 | 1155 | #endif |
e40dfb3a VS |
1156 | |
1157 | // check if we're running under the "C" locale: it is 7bit subset | |
1158 | // of UTF-8, so it can be safely used with the UTF-8 build: | |
1159 | const char *lc_ctype = setlocale(LC_CTYPE, NULL); | |
1160 | if ( lc_ctype && | |
1161 | (strcmp(lc_ctype, "C") == 0 || strcmp(lc_ctype, "POSIX") == 0) ) | |
cb352236 | 1162 | { |
e40dfb3a | 1163 | return true; |
cb352236 | 1164 | } |
e40dfb3a VS |
1165 | |
1166 | // we don't know what charset libc is using, so assume the worst | |
1167 | // to be safe: | |
1168 | return false; | |
cb352236 VS |
1169 | } |
1170 | ||
1171 | void wxUpdateLocaleIsUtf8() | |
1172 | { | |
1173 | #if wxUSE_UTF8_LOCALE_ONLY | |
1174 | if ( !wxIsLocaleUtf8() ) | |
1175 | { | |
1176 | wxLogFatalError(_T("This program requires UTF-8 locale to run.")); | |
1177 | } | |
1178 | #else // !wxUSE_UTF8_LOCALE_ONLY | |
1179 | wxLocaleIsUtf8 = wxIsLocaleUtf8(); | |
1180 | #endif | |
1181 | } | |
1182 | ||
c49f8879 VS |
1183 | #endif // wxUSE_UNICODE_UTF8 |
1184 | ||
1185 | // ============================================================================ | |
1186 | // wx wrappers for CRT functions | |
1187 | // ============================================================================ | |
1188 | ||
52de37c7 | 1189 | #if wxUSE_UNICODE_WCHAR |
19984725 | 1190 | #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW |
52de37c7 | 1191 | #elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY |
19984725 VS |
1192 | #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \ |
1193 | return_kw wxLocaleIsUtf8 ? callA : callW | |
52de37c7 | 1194 | #else // ANSI or UTF8 only |
19984725 | 1195 | #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA |
52de37c7 VS |
1196 | #endif |
1197 | ||
1198 | int wxPuts(const wxString& s) | |
1199 | { | |
19984725 VS |
1200 | CALL_ANSI_OR_UNICODE(return, |
1201 | wxCRT_PutsA(s.mb_str()), | |
52de37c7 VS |
1202 | wxCRT_PutsW(s.wc_str())); |
1203 | } | |
1204 | ||
1205 | int wxFputs(const wxString& s, FILE *stream) | |
1206 | { | |
19984725 VS |
1207 | CALL_ANSI_OR_UNICODE(return, |
1208 | wxCRT_FputsA(s.mb_str(), stream), | |
52de37c7 VS |
1209 | wxCRT_FputsW(s.wc_str(), stream)); |
1210 | } | |
1211 | ||
1212 | int wxFputc(const wxUniChar& c, FILE *stream) | |
1213 | { | |
1214 | #if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build | |
1215 | return wxCRT_FputcA((char)c, stream); | |
1216 | #else | |
19984725 VS |
1217 | CALL_ANSI_OR_UNICODE(return, |
1218 | wxCRT_FputsA(c.AsUTF8(), stream), | |
52de37c7 VS |
1219 | wxCRT_FputcW((wchar_t)c, stream)); |
1220 | #endif | |
1221 | } | |
1222 | ||
d6f2a891 VZ |
1223 | #ifdef wxCRT_PerrorA |
1224 | ||
52de37c7 VS |
1225 | void wxPerror(const wxString& s) |
1226 | { | |
1227 | #ifdef wxCRT_PerrorW | |
19984725 VS |
1228 | CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE, |
1229 | wxCRT_PerrorA(s.mb_str()), | |
1230 | wxCRT_PerrorW(s.wc_str())); | |
52de37c7 VS |
1231 | #else |
1232 | wxCRT_PerrorA(s.mb_str()); | |
1233 | #endif | |
1234 | } | |
1235 | ||
d6f2a891 VZ |
1236 | #endif // wxCRT_PerrorA |
1237 | ||
52de37c7 VS |
1238 | wchar_t *wxFgets(wchar_t *s, int size, FILE *stream) |
1239 | { | |
1240 | wxCHECK_MSG( s, NULL, "empty buffer passed to wxFgets()" ); | |
1241 | ||
1242 | wxCharBuffer buf(size - 1); | |
1243 | // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide | |
1244 | // characters may be encoded by up to 'size'*4 bytes), but what | |
1245 | // else can we do? | |
1246 | if ( wxFgets(buf.data(), size, stream) == NULL ) | |
1247 | return NULL; | |
1248 | ||
1249 | if ( wxConvLibc.ToWChar(s, size, buf, wxNO_LEN) == wxCONV_FAILED ) | |
1250 | return NULL; | |
1251 | ||
1252 | return s; | |
1253 | } | |
c49f8879 VS |
1254 | |
1255 | // ---------------------------------------------------------------------------- | |
1256 | // wxScanf() and friends | |
1257 | // ---------------------------------------------------------------------------- | |
1258 | ||
91a151d3 | 1259 | #ifndef HAVE_NO_VSSCANF // __VISUALC__ and __DMC__ see wx/crt.h |
c49f8879 VS |
1260 | int wxVsscanf(const char *str, const char *format, va_list ap) |
1261 | { return wxCRT_VsscanfA(str, format, ap); } | |
1262 | int wxVsscanf(const wchar_t *str, const wchar_t *format, va_list ap) | |
eb6cb207 | 1263 | { return wxCRT_VsscanfW(str, format, ap); } |
c49f8879 VS |
1264 | int wxVsscanf(const wxCharBuffer& str, const char *format, va_list ap) |
1265 | { return wxCRT_VsscanfA(str, format, ap); } | |
1266 | int wxVsscanf(const wxWCharBuffer& str, const wchar_t *format, va_list ap) | |
eb6cb207 | 1267 | { return wxCRT_VsscanfW(str, format, ap); } |
c49f8879 VS |
1268 | int wxVsscanf(const wxString& str, const char *format, va_list ap) |
1269 | { return wxCRT_VsscanfA(str.mb_str(), format, ap); } | |
1270 | int wxVsscanf(const wxString& str, const wchar_t *format, va_list ap) | |
eb6cb207 | 1271 | { return wxCRT_VsscanfW(str.wc_str(), format, ap); } |
c49f8879 VS |
1272 | int wxVsscanf(const wxCStrData& str, const char *format, va_list ap) |
1273 | { return wxCRT_VsscanfA(str.AsCharBuf(), format, ap); } | |
1274 | int wxVsscanf(const wxCStrData& str, const wchar_t *format, va_list ap) | |
eb6cb207 | 1275 | { return wxCRT_VsscanfW(str.AsWCharBuf(), format, ap); } |
91a151d3 | 1276 | #endif // HAVE_NO_VSSCANF |