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