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