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