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