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