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