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