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