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