]>
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 | |
61 | size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n) | |
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 | ||
94 | size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n) | |
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 | ||
122 | bool WXDLLEXPORT wxOKlibc() | |
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 | { | |
145 | char *rv = setlocale(category, locale); | |
146 | if ( locale != NULL /* setting locale, not querying */ && | |
147 | rv /* call was successful */ ) | |
148 | { | |
149 | wxUpdateLocaleIsUtf8(); | |
150 | } | |
151 | return rv; | |
152 | } | |
153 | ||
dd0ef332 VS |
154 | // ============================================================================ |
155 | // printf() functions business | |
156 | // ============================================================================ | |
157 | ||
158 | // special test mode: define all functions below even if we don't really need | |
159 | // them to be able to test them | |
160 | #ifdef wxTEST_PRINTF | |
161 | #undef wxFprintf | |
162 | #undef wxPrintf | |
163 | #undef wxSprintf | |
164 | #undef wxVfprintf | |
165 | #undef wxVsprintf | |
166 | #undef wxVprintf | |
167 | #undef wxVsnprintf_ | |
dd0ef332 VS |
168 | |
169 | #define wxNEED_WPRINTF | |
170 | ||
52de37c7 | 171 | int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list argptr ); |
dd0ef332 VS |
172 | #endif |
173 | ||
dd0ef332 | 174 | #if defined(__DMC__) |
52de37c7 VS |
175 | /* Digital Mars adds count to _stprintf (C99) so convert */ |
176 | int wxCRT_SprintfW (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... ) | |
177 | { | |
178 | va_list arglist; | |
dd0ef332 | 179 | |
52de37c7 VS |
180 | va_start( arglist, format ); |
181 | int iLen = swprintf ( s, -1, format, arglist ); | |
182 | va_end( arglist ); | |
183 | return iLen ; | |
184 | } | |
dd0ef332 VS |
185 | #endif //__DMC__ |
186 | ||
187 | // ---------------------------------------------------------------------------- | |
188 | // implement the standard IO functions for wide char if libc doesn't have them | |
189 | // ---------------------------------------------------------------------------- | |
190 | ||
52de37c7 VS |
191 | #ifndef wxCRT_FputsW |
192 | int wxCRT_FputsW(const wchar_t *ws, FILE *stream) | |
dd0ef332 VS |
193 | { |
194 | wxCharBuffer buf(wxConvLibc.cWC2MB(ws)); | |
195 | if ( !buf ) | |
196 | return -1; | |
197 | ||
198 | // counting the number of wide characters written isn't worth the trouble, | |
199 | // simply distinguish between ok and error | |
52de37c7 | 200 | return wxCRT_FputsA(buf, stream) == -1 ? -1 : 0; |
dd0ef332 | 201 | } |
52de37c7 | 202 | #endif // !wxCRT_FputsW |
dd0ef332 | 203 | |
52de37c7 VS |
204 | #ifndef wxCRT_PutsW |
205 | int wxCRT_PutsW(const wchar_t *ws) | |
dd0ef332 | 206 | { |
52de37c7 | 207 | int rc = wxCRT_FputsW(ws, stdout); |
dd0ef332 VS |
208 | if ( rc != -1 ) |
209 | { | |
52de37c7 | 210 | if ( wxCRT_FputsW(L"\n", stdout) == -1 ) |
dd0ef332 VS |
211 | return -1; |
212 | ||
213 | rc++; | |
214 | } | |
215 | ||
216 | return rc; | |
217 | } | |
52de37c7 | 218 | #endif // !wxCRT_PutsW |
dd0ef332 | 219 | |
52de37c7 VS |
220 | #ifndef wxCRT_FputcW |
221 | int /* not wint_t */ wxCRT_FputcW(wchar_t wc, FILE *stream) | |
dd0ef332 VS |
222 | { |
223 | wchar_t ws[2] = { wc, L'\0' }; | |
224 | ||
52de37c7 | 225 | return wxCRT_FputsW(ws, stream); |
dd0ef332 | 226 | } |
52de37c7 | 227 | #endif // !wxCRT_FputcW |
dd0ef332 VS |
228 | |
229 | // NB: we only implement va_list functions here, the ones taking ... are | |
230 | // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse | |
231 | // the definitions there to avoid duplicating them here | |
232 | #ifdef wxNEED_WPRINTF | |
233 | ||
234 | // TODO: implement the scanf() functions | |
52de37c7 | 235 | static int vwscanf(const wchar_t *format, va_list argptr) |
dd0ef332 VS |
236 | { |
237 | wxFAIL_MSG( _T("TODO") ); | |
238 | ||
239 | return -1; | |
240 | } | |
241 | ||
52de37c7 | 242 | static int vswscanf(const wchar_t *ws, const wchar_t *format, va_list argptr) |
dd0ef332 VS |
243 | { |
244 | // The best we can do without proper Unicode support in glibc is to | |
245 | // convert the strings into MB representation and run ANSI version | |
246 | // of the function. This doesn't work with %c and %s because of difference | |
247 | // in size of char and wchar_t, though. | |
248 | ||
249 | wxCHECK_MSG( wxStrstr(format, _T("%s")) == NULL, -1, | |
250 | _T("incomplete vswscanf implementation doesn't allow %s") ); | |
251 | wxCHECK_MSG( wxStrstr(format, _T("%c")) == NULL, -1, | |
252 | _T("incomplete vswscanf implementation doesn't allow %c") ); | |
253 | ||
c6255a6e | 254 | return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argptr); |
dd0ef332 VS |
255 | } |
256 | ||
52de37c7 | 257 | static int vfwscanf(FILE *stream, const wchar_t *format, va_list argptr) |
dd0ef332 VS |
258 | { |
259 | wxFAIL_MSG( _T("TODO") ); | |
260 | ||
261 | return -1; | |
262 | } | |
263 | ||
50e27899 | 264 | #define vswprintf wxCRT_VsnprintfW |
dd0ef332 | 265 | |
52de37c7 | 266 | static int vfwprintf(FILE *stream, const wchar_t *format, va_list argptr) |
dd0ef332 VS |
267 | { |
268 | wxString s; | |
269 | int rc = s.PrintfV(format, argptr); | |
270 | ||
271 | if ( rc != -1 ) | |
272 | { | |
273 | // we can't do much better without Unicode support in libc... | |
274 | if ( fprintf(stream, "%s", (const char*)s.mb_str() ) == -1 ) | |
275 | return -1; | |
276 | } | |
277 | ||
278 | return rc; | |
279 | } | |
280 | ||
52de37c7 | 281 | static int vwprintf(const wchar_t *format, va_list argptr) |
dd0ef332 | 282 | { |
52de37c7 | 283 | return wxCRT_VfprintfW(stdout, format, argptr); |
dd0ef332 VS |
284 | } |
285 | ||
286 | #endif // wxNEED_WPRINTF | |
287 | ||
dd0ef332 VS |
288 | // ---------------------------------------------------------------------------- |
289 | // wxPrintf(), wxScanf() and relatives | |
290 | // ---------------------------------------------------------------------------- | |
291 | ||
eb6cb207 VS |
292 | // FIXME-UTF8: do format conversion using (modified) wxFormatConverter in |
293 | // template wrappers, not here; note that it will needed to | |
294 | // translate all forms of string specifiers to %(l)s for wxPrintf(), | |
295 | // but it only should do what it did in 2.8 for wxScanf()! | |
296 | ||
52de37c7 VS |
297 | #ifndef wxCRT_PrintfW |
298 | int wxCRT_PrintfW( const wchar_t *format, ... ) | |
dd0ef332 VS |
299 | { |
300 | va_list argptr; | |
301 | va_start(argptr, format); | |
302 | ||
50e27899 | 303 | int ret = vwprintf( format, argptr ); |
dd0ef332 VS |
304 | |
305 | va_end(argptr); | |
306 | ||
307 | return ret; | |
308 | } | |
52de37c7 | 309 | #endif |
dd0ef332 | 310 | |
52de37c7 VS |
311 | #ifndef wxCRT_FprintfW |
312 | int wxCRT_FprintfW( FILE *stream, const wchar_t *format, ... ) | |
dd0ef332 VS |
313 | { |
314 | va_list argptr; | |
2523e9b7 | 315 | va_start( argptr, format ); |
dd0ef332 | 316 | |
50e27899 | 317 | int ret = vfwprintf( stream, format, argptr ); |
dd0ef332 VS |
318 | |
319 | va_end(argptr); | |
320 | ||
321 | return ret; | |
322 | } | |
52de37c7 | 323 | #endif |
dd0ef332 | 324 | |
52de37c7 VS |
325 | #ifndef wxCRT_VfprintfW |
326 | int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list argptr ) | |
2523e9b7 | 327 | { |
50e27899 | 328 | return vfwprintf( stream, format, argptr ); |
2523e9b7 | 329 | } |
52de37c7 | 330 | #endif |
2523e9b7 | 331 | |
52de37c7 VS |
332 | #ifndef wxCRT_VprintfW |
333 | int wxCRT_VprintfW( const wchar_t *format, va_list argptr ) | |
2523e9b7 | 334 | { |
50e27899 | 335 | return vwprintf( format, argptr ); |
2523e9b7 | 336 | } |
52de37c7 | 337 | #endif |
2523e9b7 | 338 | |
52de37c7 VS |
339 | #ifndef wxCRT_VsprintfW |
340 | int wxCRT_VsprintfW( wchar_t *str, const wchar_t *format, va_list argptr ) | |
2523e9b7 VS |
341 | { |
342 | // same as for wxSprintf() | |
50e27899 | 343 | return vswprintf(str, INT_MAX / 4, format, argptr); |
2523e9b7 | 344 | } |
52de37c7 | 345 | #endif |
2523e9b7 | 346 | |
52de37c7 | 347 | #ifndef wxCRT_ScanfW |
eb6cb207 VS |
348 | int wxCRT_ScanfW(const wchar_t *format, ...) |
349 | { | |
350 | va_list argptr; | |
351 | va_start(argptr, format); | |
352 | ||
28ffb1f2 JJ |
353 | #ifdef __VMS |
354 | #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD ) | |
50e27899 | 355 | int ret = std::vwscanf(format, argptr); |
28ffb1f2 | 356 | #else |
50e27899 | 357 | int ret = vwscanf(format, argptr); |
28ffb1f2 JJ |
358 | #endif |
359 | #else | |
50e27899 | 360 | int ret = vwscanf(format, argptr); |
28ffb1f2 JJ |
361 | #endif |
362 | ||
eb6cb207 VS |
363 | va_end(argptr); |
364 | ||
365 | return ret; | |
366 | } | |
52de37c7 | 367 | #endif |
eb6cb207 | 368 | |
52de37c7 | 369 | #ifndef wxCRT_SscanfW |
eb6cb207 VS |
370 | int wxCRT_SscanfW(const wchar_t *str, const wchar_t *format, ...) |
371 | { | |
372 | va_list argptr; | |
373 | va_start(argptr, format); | |
374 | ||
28ffb1f2 JJ |
375 | #ifdef __VMS |
376 | #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD ) | |
50e27899 | 377 | int ret = std::vswscanf(str, format, argptr); |
28ffb1f2 | 378 | #else |
50e27899 | 379 | int ret = vswscanf(str, format, argptr); |
28ffb1f2 JJ |
380 | #endif |
381 | #else | |
50e27899 | 382 | int ret = vswscanf(str, format, argptr); |
28ffb1f2 | 383 | #endif |
eb6cb207 VS |
384 | |
385 | va_end(argptr); | |
386 | ||
387 | return ret; | |
388 | } | |
52de37c7 | 389 | #endif |
eb6cb207 | 390 | |
52de37c7 | 391 | #ifndef wxCRT_FscanfW |
eb6cb207 VS |
392 | int wxCRT_FscanfW(FILE *stream, const wchar_t *format, ...) |
393 | { | |
394 | va_list argptr; | |
395 | va_start(argptr, format); | |
28ffb1f2 JJ |
396 | #ifdef __VMS |
397 | #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD ) | |
50e27899 | 398 | int ret = std::vfwscanf(stream, format, argptr); |
28ffb1f2 | 399 | #else |
50e27899 | 400 | int ret = vfwscanf(stream, format, argptr); |
28ffb1f2 JJ |
401 | #endif |
402 | #else | |
50e27899 | 403 | int ret = vfwscanf(stream, format, argptr); |
28ffb1f2 | 404 | #endif |
eb6cb207 VS |
405 | |
406 | va_end(argptr); | |
407 | ||
408 | return ret; | |
409 | } | |
52de37c7 | 410 | #endif |
eb6cb207 | 411 | |
52de37c7 | 412 | #ifndef wxCRT_VsscanfW |
eb6cb207 VS |
413 | int wxCRT_VsscanfW(const wchar_t *str, const wchar_t *format, va_list argptr) |
414 | { | |
28ffb1f2 JJ |
415 | #ifdef __VMS |
416 | #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD ) | |
50e27899 | 417 | return std::vswscanf(str, format, argptr); |
28ffb1f2 | 418 | #else |
50e27899 | 419 | return vswscanf(str, format, argptr); |
28ffb1f2 JJ |
420 | #endif |
421 | #else | |
50e27899 | 422 | return vswscanf(str, format, argptr); |
28ffb1f2 | 423 | #endif |
eb6cb207 | 424 | } |
52de37c7 | 425 | #endif |
2523e9b7 VS |
426 | |
427 | ||
428 | // ---------------------------------------------------------------------------- | |
429 | // wrappers to printf and scanf function families | |
430 | // ---------------------------------------------------------------------------- | |
431 | ||
d1f6e2cf VS |
432 | #if !wxUSE_UTF8_LOCALE_ONLY |
433 | int wxDoSprintfWchar(char *str, const wxChar *format, ...) | |
dd0ef332 VS |
434 | { |
435 | va_list argptr; | |
436 | va_start(argptr, format); | |
437 | ||
2523e9b7 | 438 | int rv = wxVsprintf(str, format, argptr); |
dd0ef332 VS |
439 | |
440 | va_end(argptr); | |
2523e9b7 VS |
441 | return rv; |
442 | } | |
d1f6e2cf VS |
443 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
444 | ||
445 | #if wxUSE_UNICODE_UTF8 | |
446 | int wxDoSprintfUtf8(char *str, const char *format, ...) | |
447 | { | |
448 | va_list argptr; | |
449 | va_start(argptr, format); | |
450 | ||
451 | int rv = wxVsprintf(str, format, argptr); | |
452 | ||
453 | va_end(argptr); | |
454 | return rv; | |
455 | } | |
456 | #endif // wxUSE_UNICODE_UTF8 | |
2523e9b7 VS |
457 | |
458 | #if wxUSE_UNICODE | |
d1f6e2cf VS |
459 | |
460 | #if !wxUSE_UTF8_LOCALE_ONLY | |
461 | int wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...) | |
2523e9b7 VS |
462 | { |
463 | va_list argptr; | |
464 | va_start(argptr, format); | |
dd0ef332 | 465 | |
2523e9b7 VS |
466 | int rv = wxVsprintf(str, format, argptr); |
467 | ||
468 | va_end(argptr); | |
469 | return rv; | |
dd0ef332 | 470 | } |
d1f6e2cf VS |
471 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
472 | ||
473 | #if wxUSE_UNICODE_UTF8 | |
474 | int wxDoSprintfUtf8(wchar_t *str, const char *format, ...) | |
475 | { | |
476 | va_list argptr; | |
477 | va_start(argptr, format); | |
478 | ||
479 | int rv = wxVsprintf(str, format, argptr); | |
480 | ||
481 | va_end(argptr); | |
482 | return rv; | |
483 | } | |
484 | #endif // wxUSE_UNICODE_UTF8 | |
485 | ||
486 | #endif // wxUSE_UNICODE | |
487 | ||
488 | #if !wxUSE_UTF8_LOCALE_ONLY | |
489 | int wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...) | |
490 | { | |
491 | va_list argptr; | |
492 | va_start(argptr, format); | |
dd0ef332 | 493 | |
d1f6e2cf VS |
494 | int rv = wxVsnprintf(str, size, format, argptr); |
495 | ||
496 | va_end(argptr); | |
497 | return rv; | |
498 | } | |
499 | #endif // !wxUSE_UTF8_LOCALE_ONLY | |
500 | ||
501 | #if wxUSE_UNICODE_UTF8 | |
502 | int wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...) | |
dd0ef332 VS |
503 | { |
504 | va_list argptr; | |
2523e9b7 | 505 | va_start(argptr, format); |
dd0ef332 | 506 | |
2523e9b7 | 507 | int rv = wxVsnprintf(str, size, format, argptr); |
dd0ef332 VS |
508 | |
509 | va_end(argptr); | |
2523e9b7 VS |
510 | return rv; |
511 | } | |
d1f6e2cf | 512 | #endif // wxUSE_UNICODE_UTF8 |
dd0ef332 | 513 | |
2523e9b7 | 514 | #if wxUSE_UNICODE |
d1f6e2cf VS |
515 | |
516 | #if !wxUSE_UTF8_LOCALE_ONLY | |
517 | int wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...) | |
2523e9b7 VS |
518 | { |
519 | va_list argptr; | |
520 | va_start(argptr, format); | |
521 | ||
522 | int rv = wxVsnprintf(str, size, format, argptr); | |
523 | ||
524 | va_end(argptr); | |
525 | return rv; | |
dd0ef332 | 526 | } |
d1f6e2cf VS |
527 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
528 | ||
529 | #if wxUSE_UNICODE_UTF8 | |
530 | int wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...) | |
531 | { | |
532 | va_list argptr; | |
533 | va_start(argptr, format); | |
534 | ||
535 | int rv = wxVsnprintf(str, size, format, argptr); | |
536 | ||
537 | va_end(argptr); | |
538 | return rv; | |
539 | } | |
540 | #endif // wxUSE_UNICODE_UTF8 | |
541 | ||
542 | #endif // wxUSE_UNICODE | |
dd0ef332 | 543 | |
2523e9b7 VS |
544 | |
545 | #ifdef HAVE_BROKEN_VSNPRINTF_DECL | |
546 | #define vsnprintf wx_fixed_vsnprintf | |
547 | #endif | |
548 | ||
549 | #if wxUSE_UNICODE | |
d1f6e2cf | 550 | |
80f8355d VZ |
551 | namespace |
552 | { | |
553 | ||
d1f6e2cf | 554 | #if !wxUSE_UTF8_LOCALE_ONLY |
ec873c94 | 555 | int ConvertStringToBuf(const wxString& s, char *out, size_t outsize) |
dd0ef332 | 556 | { |
2523e9b7 VS |
557 | const wxWX2WCbuf buf = s.wc_str(); |
558 | ||
559 | size_t len = wxConvLibc.FromWChar(out, outsize, buf); | |
560 | if ( len != wxCONV_FAILED ) | |
561 | return len-1; | |
562 | else | |
563 | return wxConvLibc.FromWChar(NULL, 0, buf); | |
dd0ef332 | 564 | } |
d1f6e2cf | 565 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
dd0ef332 | 566 | |
2523e9b7 | 567 | #if wxUSE_UNICODE_UTF8 |
ec873c94 | 568 | int ConvertStringToBuf(const wxString& s, wchar_t *out, size_t outsize) |
dd0ef332 | 569 | { |
2523e9b7 | 570 | const wxWX2WCbuf buf(s.wc_str()); |
ec873c94 | 571 | size_t len = s.length(); // same as buf length for wchar_t* |
2523e9b7 | 572 | if ( outsize > len ) |
ec873c94 | 573 | { |
2523e9b7 | 574 | memcpy(out, buf, (len+1) * sizeof(wchar_t)); |
ec873c94 VS |
575 | } |
576 | else // not enough space | |
577 | { | |
578 | memcpy(out, buf, (outsize-1) * sizeof(wchar_t)); | |
579 | out[outsize-1] = 0; | |
580 | } | |
2523e9b7 | 581 | return len; |
dd0ef332 | 582 | } |
d1f6e2cf | 583 | #endif // wxUSE_UNICODE_UTF8 |
dd0ef332 | 584 | |
80f8355d VZ |
585 | } // anonymous namespace |
586 | ||
2523e9b7 VS |
587 | template<typename T> |
588 | static size_t PrintfViaString(T *out, size_t outsize, | |
589 | const wxString& format, va_list argptr) | |
dd0ef332 | 590 | { |
2523e9b7 | 591 | wxString s; |
c6255a6e | 592 | s.PrintfV(format, argptr); |
2523e9b7 | 593 | |
ec873c94 | 594 | return ConvertStringToBuf(s, out, outsize); |
dd0ef332 | 595 | } |
2523e9b7 | 596 | #endif // wxUSE_UNICODE |
dd0ef332 | 597 | |
2523e9b7 | 598 | int wxVsprintf(char *str, const wxString& format, va_list argptr) |
dd0ef332 | 599 | { |
2523e9b7 | 600 | #if wxUSE_UTF8_LOCALE_ONLY |
04fd66c9 | 601 | return wxCRT_VsprintfA(str, format.wx_str(), argptr); |
2523e9b7 VS |
602 | #else |
603 | #if wxUSE_UNICODE_UTF8 | |
604 | if ( wxLocaleIsUtf8 ) | |
04fd66c9 | 605 | return wxCRT_VsprintfA(str, format.wx_str(), argptr); |
2523e9b7 VS |
606 | else |
607 | #endif | |
608 | #if wxUSE_UNICODE | |
c6255a6e | 609 | return PrintfViaString(str, wxNO_LEN, format, argptr); |
2523e9b7 | 610 | #else |
52de37c7 | 611 | return wxCRT_VsprintfA(str, format.mb_str(), argptr); |
2523e9b7 VS |
612 | #endif |
613 | #endif | |
dd0ef332 | 614 | } |
dd0ef332 | 615 | |
2523e9b7 VS |
616 | #if wxUSE_UNICODE |
617 | int wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) | |
dd0ef332 | 618 | { |
2523e9b7 | 619 | #if wxUSE_UNICODE_WCHAR |
52de37c7 | 620 | return wxCRT_VsprintfW(str, format.wc_str(), argptr); |
2523e9b7 VS |
621 | #else // wxUSE_UNICODE_UTF8 |
622 | #if !wxUSE_UTF8_LOCALE_ONLY | |
623 | if ( !wxLocaleIsUtf8 ) | |
52de37c7 | 624 | return wxCRT_VsprintfW(str, format.wc_str(), argptr); |
2523e9b7 VS |
625 | else |
626 | #endif | |
c6255a6e | 627 | return PrintfViaString(str, wxNO_LEN, format, argptr); |
2523e9b7 | 628 | #endif // wxUSE_UNICODE_UTF8 |
dd0ef332 | 629 | } |
2523e9b7 | 630 | #endif // wxUSE_UNICODE |
dd0ef332 | 631 | |
2523e9b7 VS |
632 | int wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr) |
633 | { | |
634 | int rv; | |
2523e9b7 | 635 | #if wxUSE_UTF8_LOCALE_ONLY |
52de37c7 | 636 | rv = wxCRT_VsnprintfA(str, size, format.wx_str(), argptr); |
2523e9b7 VS |
637 | #else |
638 | #if wxUSE_UNICODE_UTF8 | |
639 | if ( wxLocaleIsUtf8 ) | |
52de37c7 | 640 | rv = wxCRT_VsnprintfA(str, size, format.wx_str(), argptr); |
2523e9b7 VS |
641 | else |
642 | #endif | |
643 | #if wxUSE_UNICODE | |
644 | { | |
645 | // NB: if this code is called, then wxString::PrintV() would use the | |
646 | // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV() | |
647 | // from here | |
c6255a6e | 648 | rv = PrintfViaString(str, size, format, argptr); |
2523e9b7 VS |
649 | } |
650 | #else | |
52de37c7 | 651 | rv = wxCRT_VsnprintfA(str, size, format.mb_str(), argptr); |
2523e9b7 VS |
652 | #endif |
653 | #endif | |
654 | ||
655 | // VsnprintfTestCase reveals that glibc's implementation of vswprintf | |
656 | // doesn't nul terminate on truncation. | |
657 | str[size - 1] = 0; | |
658 | ||
659 | return rv; | |
660 | } | |
661 | ||
662 | #if wxUSE_UNICODE | |
663 | int wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr) | |
664 | { | |
665 | int rv; | |
2523e9b7 VS |
666 | |
667 | #if wxUSE_UNICODE_WCHAR | |
52de37c7 | 668 | rv = wxCRT_VsnprintfW(str, size, format.wc_str(), argptr); |
2523e9b7 VS |
669 | #else // wxUSE_UNICODE_UTF8 |
670 | #if !wxUSE_UTF8_LOCALE_ONLY | |
671 | if ( !wxLocaleIsUtf8 ) | |
52de37c7 | 672 | rv = wxCRT_VsnprintfW(str, size, format.wc_str(), argptr); |
2523e9b7 VS |
673 | else |
674 | #endif | |
675 | { | |
676 | // NB: if this code is called, then wxString::PrintV() would use the | |
677 | // char* version of wxVsnprintf(), so it's safe to use PrintV() | |
678 | // from here | |
c6255a6e | 679 | rv = PrintfViaString(str, size, format, argptr); |
2523e9b7 VS |
680 | } |
681 | #endif // wxUSE_UNICODE_UTF8 | |
682 | ||
683 | // VsnprintfTestCase reveals that glibc's implementation of vswprintf | |
684 | // doesn't nul terminate on truncation. | |
685 | str[size - 1] = 0; | |
686 | ||
687 | return rv; | |
688 | } | |
689 | #endif // wxUSE_UNICODE | |
dd0ef332 VS |
690 | |
691 | #if wxUSE_WCHAR_T | |
692 | ||
693 | // ---------------------------------------------------------------------------- | |
694 | // ctype.h stuff (currently unused) | |
695 | // ---------------------------------------------------------------------------- | |
696 | ||
dd0ef332 VS |
697 | #ifdef wxNEED_WX_MBSTOWCS |
698 | ||
699 | WXDLLEXPORT size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen) | |
700 | { | |
701 | if (!out) | |
702 | { | |
703 | size_t outsize = 0; | |
704 | while(*in++) | |
705 | outsize++; | |
706 | return outsize; | |
707 | } | |
708 | ||
709 | const char* origin = in; | |
710 | ||
711 | while (outlen-- && *in) | |
712 | { | |
713 | *out++ = (wchar_t) *in++; | |
714 | } | |
715 | ||
716 | *out = '\0'; | |
717 | ||
718 | return in - origin; | |
719 | } | |
720 | ||
721 | WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen) | |
722 | { | |
723 | if (!out) | |
724 | { | |
725 | size_t outsize = 0; | |
726 | while(*in++) | |
727 | outsize++; | |
728 | return outsize; | |
729 | } | |
730 | ||
731 | const wchar_t* origin = in; | |
732 | ||
733 | while (outlen-- && *in) | |
734 | { | |
735 | *out++ = (char) *in++; | |
736 | } | |
737 | ||
738 | *out = '\0'; | |
739 | ||
740 | return in - origin; | |
741 | } | |
742 | ||
743 | #endif // wxNEED_WX_MBSTOWCS | |
744 | ||
52de37c7 VS |
745 | #ifndef wxCRT_StrdupA |
746 | WXDLLEXPORT char *wxCRT_StrdupA(const char *s) | |
dd0ef332 VS |
747 | { |
748 | return strcpy((char *)malloc(strlen(s) + 1), s); | |
749 | } | |
52de37c7 | 750 | #endif // wxCRT_StrdupA |
dd0ef332 | 751 | |
52de37c7 VS |
752 | #ifndef wxCRT_StrdupW |
753 | WXDLLEXPORT wchar_t * wxCRT_StrdupW(const wchar_t *pwz) | |
dd0ef332 VS |
754 | { |
755 | size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t); | |
756 | wchar_t *ret = (wchar_t *) malloc(size); | |
757 | memcpy(ret, pwz, size); | |
758 | return ret; | |
759 | } | |
52de37c7 | 760 | #endif // wxCRT_StrdupW |
dd0ef332 | 761 | |
52de37c7 VS |
762 | #ifndef wxCRT_StricmpA |
763 | int WXDLLEXPORT wxCRT_StricmpA(const char *psz1, const char *psz2) | |
764 | { | |
765 | register char c1, c2; | |
766 | do { | |
767 | c1 = wxTolower(*psz1++); | |
768 | c2 = wxTolower(*psz2++); | |
769 | } while ( c1 && (c1 == c2) ); | |
770 | return c1 - c2; | |
771 | } | |
772 | #endif // !defined(wxCRT_StricmpA) | |
dd0ef332 | 773 | |
52de37c7 VS |
774 | #ifndef wxCRT_StricmpW |
775 | int WXDLLEXPORT wxCRT_StricmpW(const wchar_t *psz1, const wchar_t *psz2) | |
dd0ef332 | 776 | { |
52de37c7 | 777 | register wchar_t c1, c2; |
dd0ef332 VS |
778 | do { |
779 | c1 = wxTolower(*psz1++); | |
780 | c2 = wxTolower(*psz2++); | |
781 | } while ( c1 && (c1 == c2) ); | |
782 | return c1 - c2; | |
783 | } | |
52de37c7 | 784 | #endif // !defined(wxCRT_StricmpW) |
dd0ef332 | 785 | |
52de37c7 VS |
786 | #ifndef wxCRT_StrnicmpA |
787 | int WXDLLEXPORT wxCRT_StrnicmpA(const char *s1, const char *s2, size_t n) | |
dd0ef332 VS |
788 | { |
789 | // initialize the variables just to suppress stupid gcc warning | |
52de37c7 | 790 | register char c1 = 0, c2 = 0; |
dd0ef332 VS |
791 | while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++; |
792 | if (n) { | |
793 | if (c1 < c2) return -1; | |
794 | if (c1 > c2) return 1; | |
795 | } | |
796 | return 0; | |
797 | } | |
52de37c7 | 798 | #endif // !defined(wxCRT_StrnicmpA) |
cb352236 | 799 | |
52de37c7 VS |
800 | #ifndef wxCRT_StrnicmpW |
801 | int WXDLLEXPORT wxCRT_StrnicmpW(const wchar_t *s1, const wchar_t *s2, size_t n) | |
cb352236 | 802 | { |
52de37c7 VS |
803 | // initialize the variables just to suppress stupid gcc warning |
804 | register wchar_t c1 = 0, c2 = 0; | |
805 | while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++; | |
806 | if (n) { | |
807 | if (c1 < c2) return -1; | |
808 | if (c1 > c2) return 1; | |
809 | } | |
810 | return 0; | |
dd0ef332 | 811 | } |
52de37c7 | 812 | #endif // !defined(wxCRT_StrnicmpW) |
dd0ef332 VS |
813 | |
814 | // ---------------------------------------------------------------------------- | |
815 | // string.h functions | |
816 | // ---------------------------------------------------------------------------- | |
817 | ||
410390cf VS |
818 | // this (and wxCRT_StrncmpW below) are extern "C" because they are needed |
819 | // by regex code, the rest isn't needed, so it's not declared as extern "C" | |
820 | #ifndef wxCRT_StrlenW | |
821 | extern "C" WXDLLEXPORT size_t wxCRT_StrlenW(const wchar_t *s) | |
dd0ef332 VS |
822 | { |
823 | size_t n = 0; | |
824 | while ( *s++ ) | |
825 | n++; | |
826 | ||
827 | return n; | |
828 | } | |
52de37c7 | 829 | #endif |
dd0ef332 | 830 | |
410390cf VS |
831 | // ---------------------------------------------------------------------------- |
832 | // stdlib.h functions | |
833 | // ---------------------------------------------------------------------------- | |
dd0ef332 | 834 | |
52de37c7 VS |
835 | #ifndef wxCRT_GetenvW |
836 | wchar_t* WXDLLEXPORT wxCRT_GetenvW(const wchar_t *name) | |
dd0ef332 | 837 | { |
dd0ef332 VS |
838 | // NB: buffer returned by getenv() is allowed to be overwritten next |
839 | // time getenv() is called, so it is OK to use static string | |
840 | // buffer to hold the data. | |
f62262aa VS |
841 | static wxWCharBuffer value((wchar_t*)NULL); |
842 | value = wxConvLibc.cMB2WC(getenv(wxConvLibc.cWC2MB(name))); | |
dd0ef332 | 843 | return value.data(); |
dd0ef332 | 844 | } |
52de37c7 | 845 | #endif // !wxCRT_GetenvW |
dd0ef332 | 846 | |
52de37c7 | 847 | #ifndef wxCRT_StrftimeW |
dd0ef332 | 848 | WXDLLEXPORT size_t |
52de37c7 | 849 | wxCRT_StrftimeW(wchar_t *s, size_t maxsize, const wchar_t *fmt, const struct tm *tm) |
dd0ef332 VS |
850 | { |
851 | if ( !maxsize ) | |
852 | return 0; | |
853 | ||
854 | wxCharBuffer buf(maxsize); | |
855 | ||
856 | wxCharBuffer bufFmt(wxConvLibc.cWX2MB(fmt)); | |
857 | if ( !bufFmt ) | |
858 | return 0; | |
859 | ||
860 | size_t ret = strftime(buf.data(), maxsize, bufFmt, tm); | |
861 | if ( !ret ) | |
862 | return 0; | |
863 | ||
864 | wxWCharBuffer wbuf = wxConvLibc.cMB2WX(buf); | |
865 | if ( !wbuf ) | |
866 | return 0; | |
867 | ||
52de37c7 VS |
868 | wxCRT_StrncpyW(s, wbuf, maxsize); |
869 | return wxCRT_StrlenW(s); | |
dd0ef332 | 870 | } |
52de37c7 | 871 | #endif // !wxCRT_StrftimeW |
dd0ef332 VS |
872 | |
873 | #endif // wxUSE_WCHAR_T | |
874 | ||
bdcb2137 | 875 | #ifdef wxLongLong_t |
52de37c7 VS |
876 | template<typename T> |
877 | static wxULongLong_t | |
878 | wxCRT_StrtoullBase(const T* nptr, T** endptr, int base, T* sign) | |
17482893 VZ |
879 | { |
880 | wxULongLong_t sum = 0; | |
881 | wxString wxstr(nptr); | |
882 | wxString::const_iterator i = wxstr.begin(); | |
883 | wxString::const_iterator end = wxstr.end(); | |
884 | ||
885 | // Skip spaces | |
886 | while ( i != end && wxIsspace(*i) ) i++; | |
887 | ||
888 | // Starts with sign? | |
889 | *sign = wxT(' '); | |
890 | if ( i != end ) | |
891 | { | |
52de37c7 | 892 | T c = *i; |
17482893 VZ |
893 | if ( c == wxT('+') || c == wxT('-') ) |
894 | { | |
895 | *sign = c; | |
896 | i++; | |
897 | } | |
898 | } | |
899 | ||
900 | // Starts with 0x? | |
901 | if ( i != end && *i == wxT('0') ) | |
902 | { | |
903 | i++; | |
904 | if ( i != end ) | |
905 | { | |
906 | if ( *i == wxT('x') && (base == 16 || base == 0) ) | |
907 | { | |
908 | base = 16; | |
909 | i++; | |
910 | } | |
911 | else | |
912 | { | |
913 | if ( endptr ) | |
52de37c7 | 914 | *endptr = (T*) nptr; |
17482893 VZ |
915 | wxSET_ERRNO(EINVAL); |
916 | return sum; | |
917 | } | |
918 | } | |
919 | else | |
920 | i--; | |
921 | } | |
922 | ||
923 | if ( base == 0 ) | |
924 | base = 10; | |
925 | ||
926 | for ( ; i != end; i++ ) | |
927 | { | |
928 | unsigned int n; | |
929 | ||
52de37c7 | 930 | T c = *i; |
17482893 VZ |
931 | if ( c >= wxT('0') ) |
932 | { | |
933 | if ( c <= wxT('9') ) | |
934 | n = c - wxT('0'); | |
935 | else | |
936 | n = wxTolower(c) - wxT('a') + 10; | |
937 | } | |
938 | else | |
939 | break; | |
940 | ||
941 | if ( n >= (unsigned int)base ) | |
942 | // Invalid character (for this base) | |
943 | break; | |
944 | ||
945 | wxULongLong_t prevsum = sum; | |
946 | sum = (sum * base) + n; | |
947 | ||
948 | if ( sum < prevsum ) | |
949 | { | |
950 | wxSET_ERRNO(ERANGE); | |
951 | break; | |
952 | } | |
953 | } | |
954 | ||
955 | if ( endptr ) | |
956 | { | |
52de37c7 | 957 | *endptr = (T*)(nptr + (i - wxstr.begin())); |
17482893 VZ |
958 | } |
959 | ||
960 | return sum; | |
961 | } | |
962 | ||
52de37c7 VS |
963 | template<typename T> |
964 | static wxULongLong_t wxCRT_DoStrtoull(const T* nptr, T** endptr, int base) | |
17482893 | 965 | { |
52de37c7 VS |
966 | T sign; |
967 | wxULongLong_t uval = wxCRT_StrtoullBase(nptr, endptr, base, &sign); | |
17482893 VZ |
968 | |
969 | if ( sign == wxT('-') ) | |
970 | { | |
971 | wxSET_ERRNO(ERANGE); | |
972 | uval = 0; | |
973 | } | |
974 | ||
975 | return uval; | |
976 | } | |
977 | ||
52de37c7 VS |
978 | template<typename T> |
979 | static wxLongLong_t wxCRT_DoStrtoll(const T* nptr, T** endptr, int base) | |
17482893 | 980 | { |
52de37c7 VS |
981 | T sign; |
982 | wxULongLong_t uval = wxCRT_StrtoullBase(nptr, endptr, base, &sign); | |
17482893 VZ |
983 | wxLongLong_t val = 0; |
984 | ||
985 | if ( sign == wxT('-') ) | |
986 | { | |
987 | if ( uval <= wxULL(wxINT64_MAX+1) ) | |
988 | { | |
989 | if ( uval == wxULL(wxINT64_MAX+1)) | |
990 | val = -((wxLongLong_t)wxINT64_MAX) - 1; | |
991 | else | |
992 | val = -((wxLongLong_t)uval); | |
993 | } | |
994 | else | |
995 | { | |
996 | wxSET_ERRNO(ERANGE); | |
997 | } | |
998 | } | |
999 | else if ( uval <= wxINT64_MAX ) | |
1000 | { | |
1001 | val = uval; | |
1002 | } | |
1003 | else | |
1004 | { | |
1005 | wxSET_ERRNO(ERANGE); | |
1006 | } | |
1007 | ||
1008 | return val; | |
1009 | } | |
52de37c7 VS |
1010 | |
1011 | #ifndef wxCRT_StrtollA | |
1012 | wxLongLong_t wxCRT_StrtollA(const char* nptr, char** endptr, int base) | |
1013 | { return wxCRT_DoStrtoll(nptr, endptr, base); } | |
1014 | #endif | |
1015 | #ifndef wxCRT_StrtollW | |
1016 | wxLongLong_t wxCRT_StrtollW(const wchar_t* nptr, wchar_t** endptr, int base) | |
1017 | { return wxCRT_DoStrtoll(nptr, endptr, base); } | |
1018 | #endif | |
1019 | ||
1020 | #ifndef wxCRT_StrtoullA | |
1021 | wxULongLong_t wxCRT_StrtoullA(const char* nptr, char** endptr, int base) | |
1022 | { return wxCRT_DoStrtoull(nptr, endptr, base); } | |
1023 | #endif | |
1024 | #ifndef wxCRT_StrtoullW | |
1025 | wxULongLong_t wxCRT_StrtoullW(const wchar_t* nptr, wchar_t** endptr, int base) | |
1026 | { return wxCRT_DoStrtoull(nptr, endptr, base); } | |
1027 | #endif | |
17482893 | 1028 | |
bdcb2137 VS |
1029 | #endif // wxLongLong_t |
1030 | ||
dd0ef332 VS |
1031 | // ---------------------------------------------------------------------------- |
1032 | // functions which we may need even if !wxUSE_WCHAR_T | |
1033 | // ---------------------------------------------------------------------------- | |
1034 | ||
52de37c7 VS |
1035 | template<typename T> |
1036 | static T *wxCRT_DoStrtok(T *psz, const T *delim, T **save_ptr) | |
dd0ef332 VS |
1037 | { |
1038 | if (!psz) | |
1039 | { | |
1040 | psz = *save_ptr; | |
1041 | if ( !psz ) | |
1042 | return NULL; | |
1043 | } | |
1044 | ||
1045 | psz += wxStrspn(psz, delim); | |
1046 | if (!*psz) | |
1047 | { | |
52de37c7 VS |
1048 | *save_ptr = (T *)NULL; |
1049 | return (T *)NULL; | |
dd0ef332 VS |
1050 | } |
1051 | ||
52de37c7 | 1052 | T *ret = psz; |
dd0ef332 VS |
1053 | psz = wxStrpbrk(psz, delim); |
1054 | if (!psz) | |
1055 | { | |
52de37c7 | 1056 | *save_ptr = (T*)NULL; |
dd0ef332 VS |
1057 | } |
1058 | else | |
1059 | { | |
1060 | *psz = wxT('\0'); | |
1061 | *save_ptr = psz + 1; | |
1062 | } | |
1063 | ||
1064 | return ret; | |
1065 | } | |
1066 | ||
52de37c7 VS |
1067 | #ifndef wxCRT_StrtokA |
1068 | char *wxCRT_StrtokA(char *psz, const char *delim, char **save_ptr) | |
1069 | { return wxCRT_DoStrtok(psz, delim, save_ptr); } | |
1070 | #endif | |
1071 | #ifndef wxCRT_StrtokW | |
1072 | wchar_t *wxCRT_StrtokW(wchar_t *psz, const wchar_t *delim, wchar_t **save_ptr) | |
1073 | { return wxCRT_DoStrtok(psz, delim, save_ptr); } | |
1074 | #endif | |
dd0ef332 VS |
1075 | |
1076 | // ---------------------------------------------------------------------------- | |
1077 | // missing C RTL functions | |
1078 | // ---------------------------------------------------------------------------- | |
1079 | ||
1080 | #ifdef wxNEED_STRDUP | |
1081 | ||
1082 | char *strdup(const char *s) | |
1083 | { | |
1084 | char *dest = (char*) malloc( strlen( s ) + 1 ) ; | |
1085 | if ( dest ) | |
1086 | strcpy( dest , s ) ; | |
1087 | return dest ; | |
1088 | } | |
1089 | #endif // wxNEED_STRDUP | |
1090 | ||
1091 | #if defined(__WXWINCE__) && (_WIN32_WCE <= 211) | |
1092 | ||
1093 | void *calloc( size_t num, size_t size ) | |
1094 | { | |
1095 | void** ptr = (void **)malloc(num * size); | |
1096 | memset( ptr, 0, num * size); | |
1097 | return ptr; | |
1098 | } | |
1099 | ||
1100 | #endif // __WXWINCE__ <= 211 | |
1101 | ||
1102 | #ifdef __WXWINCE__ | |
52de37c7 | 1103 | int wxCRT_RemoveW(const wchar_t *path) |
dd0ef332 VS |
1104 | { |
1105 | return ::DeleteFile(path) == 0; | |
1106 | } | |
52de37c7 VS |
1107 | #endif |
1108 | ||
1109 | #ifndef wxCRT_TmpnamW | |
1110 | wchar_t *wxCRT_TmpnamW(wchar_t *s) | |
1111 | { | |
1112 | // tmpnam_r() returns NULL if s=NULL, do the same | |
1113 | wxCHECK_MSG( s, NULL, "wxTmpnam must be called with a buffer" ); | |
dd0ef332 | 1114 | |
52de37c7 VS |
1115 | #ifndef L_tmpnam |
1116 | #define L_tmpnam 1024 | |
dd0ef332 | 1117 | #endif |
52de37c7 VS |
1118 | wxCharBuffer buf(L_tmpnam); |
1119 | tmpnam(buf.data()); | |
1120 | ||
1121 | wxConvLibc.ToWChar(s, L_tmpnam+1, buf.data()); | |
1122 | return s; | |
1123 | } | |
1124 | #endif // !wxCRT_TmpnamW | |
cb352236 VS |
1125 | |
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 | } |
cb352236 | 1159 | #endif |
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 | ||
1227 | void wxPerror(const wxString& s) | |
1228 | { | |
1229 | #ifdef wxCRT_PerrorW | |
19984725 VS |
1230 | CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE, |
1231 | wxCRT_PerrorA(s.mb_str()), | |
1232 | wxCRT_PerrorW(s.wc_str())); | |
52de37c7 VS |
1233 | #else |
1234 | wxCRT_PerrorA(s.mb_str()); | |
1235 | #endif | |
1236 | } | |
1237 | ||
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 | ||
eb6cb207 | 1259 | #ifndef __VISUALC__ |
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 VS |
1275 | { return wxCRT_VsscanfW(str.AsWCharBuf(), format, ap); } |
1276 | #endif // !__VISUALC__ |