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