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