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