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