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