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