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