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