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