]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
only use PS printing in wxUniv (the test was accidentally broken by last commit)
[wxWidgets.git] / src / common / wxchar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wxchar.cpp
3 // Purpose: wxChar 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
23 #include "wx/wxchar.h"
24
25 #define _ISOC9X_SOURCE 1 // to get vsscanf()
26 #define _BSD_SOURCE 1 // to still get strdup()
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifndef __WXWINCE__
33 #include <time.h>
34 #include <locale.h>
35 #else
36 #include "wx/msw/wince/time.h"
37 #endif
38
39 #ifndef WX_PRECOMP
40 #include "wx/string.h"
41 #include "wx/hash.h"
42 #include "wx/utils.h" // for wxMin and wxMax
43 #include "wx/log.h"
44 #endif
45
46 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
47 #include <windef.h>
48 #include <winbase.h>
49 #include <winnls.h>
50 #include <winnt.h>
51 #endif
52
53 #if defined(__MWERKS__) && __MSL__ >= 0x6000
54 namespace std {}
55 using namespace std ;
56 #endif
57
58 #if wxUSE_WCHAR_T
59 size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n)
60 {
61 // assume that we have mbsrtowcs() too if we have wcsrtombs()
62 #ifdef HAVE_WCSRTOMBS
63 mbstate_t mbstate;
64 memset(&mbstate, 0, sizeof(mbstate_t));
65 #endif
66
67 if (buf) {
68 if (!n || !*psz) {
69 if (n) *buf = wxT('\0');
70 return 0;
71 }
72 #ifdef HAVE_WCSRTOMBS
73 return mbsrtowcs(buf, &psz, n, &mbstate);
74 #else
75 return wxMbstowcs(buf, psz, n);
76 #endif
77 }
78
79 // note that we rely on common (and required by Unix98 but unfortunately not
80 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
81 // to just get the size of the needed buffer -- this is needed as otherwise
82 // we have no idea about how much space we need and if the CRT doesn't
83 // support it (the only currently known example being Metrowerks, see
84 // wx/wxchar.h) we don't use its mbstowcs() at all
85 #ifdef HAVE_WCSRTOMBS
86 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
87 #else
88 return wxMbstowcs((wchar_t *) NULL, psz, 0);
89 #endif
90 }
91
92 size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
93 {
94 #ifdef HAVE_WCSRTOMBS
95 mbstate_t mbstate;
96 memset(&mbstate, 0, sizeof(mbstate_t));
97 #endif
98
99 if (buf) {
100 if (!n || !*pwz) {
101 // glibc2.1 chokes on null input
102 if (n) *buf = '\0';
103 return 0;
104 }
105 #ifdef HAVE_WCSRTOMBS
106 return wcsrtombs(buf, &pwz, n, &mbstate);
107 #else
108 return wxWcstombs(buf, pwz, n);
109 #endif
110 }
111
112 #ifdef HAVE_WCSRTOMBS
113 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
114 #else
115 return wxWcstombs((char *) NULL, pwz, 0);
116 #endif
117 }
118 #endif // wxUSE_WCHAR_T
119
120 bool WXDLLEXPORT wxOKlibc()
121 {
122 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
123 // glibc 2.0 uses UTF-8 even when it shouldn't
124 wchar_t res = 0;
125 if ((MB_CUR_MAX == 2) &&
126 (wxMB2WC(&res, "\xdd\xa5", 1) == 1) &&
127 (res==0x765)) {
128 // this is UTF-8 allright, check whether that's what we want
129 char *cur_locale = setlocale(LC_CTYPE, NULL);
130 if ((strlen(cur_locale) < 4) ||
131 (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) ||
132 (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) {
133 // nope, don't use libc conversion
134 return false;
135 }
136 }
137 #endif
138 return true;
139 }
140
141 // ============================================================================
142 // printf() functions business
143 // ============================================================================
144
145 // special test mode: define all functions below even if we don't really need
146 // them to be able to test them
147 #ifdef wxTEST_PRINTF
148 #undef wxFprintf
149 #undef wxPrintf
150 #undef wxSprintf
151 #undef wxVfprintf
152 #undef wxVsprintf
153 #undef wxVprintf
154 #undef wxVsnprintf_
155 #undef wxSnprintf_
156
157 #define wxNEED_WPRINTF
158
159 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr );
160 #endif
161
162 // ----------------------------------------------------------------------------
163 // implement [v]snprintf() if the system doesn't provide a safe one
164 // or if the system's one does not support positional parameters
165 // (very useful for i18n purposes)
166 // ----------------------------------------------------------------------------
167
168 #if !defined(wxVsnprintf_)
169
170 // wxUSE_STRUTILS says our wxVsnprintf_ implementation to use or not to
171 // use wxStrlen and wxStrncpy functions over one-char processing loops.
172 //
173 // Some benchmarking revealed that wxUSE_STRUTILS == 1 has the following
174 // effects:
175 // -> on Windows:
176 // when in ANSI mode, this setting does not change almost anything
177 // when in Unicode mode, it gives ~ 50% of slowdown !
178 // -> on Linux:
179 // both in ANSI and Unicode mode it gives ~ 60% of speedup !
180 //
181 #if defined(WIN32) && wxUSE_UNICODE
182 #define wxUSE_STRUTILS 0
183 #else
184 #define wxUSE_STRUTILS 1
185 #endif
186
187 // some limits of our implementation
188 #define wxMAX_SVNPRINTF_ARGUMENTS 16
189 #define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32
190 #define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512
191
192
193 // wxVsnprintf() needs to use a *system* implementation of swnprintf()
194 // in order to perform some internal tasks.
195 // NB: we cannot just use wxSnprintf() because for some systems it maybe
196 // implemented later in this file using wxVsnprintf() and that would
197 // result in an endless recursion and thus in a stack overflow
198 #if wxUSE_UNICODE
199 #if defined(__WINDOWS__)
200 // all compilers under Windows should have swprintf()
201 #define HAVE_SWPRINTF
202 #endif
203
204 // NB: MSVC 6 has only non-standard swprintf() declaration and while MSVC 7
205 // and 7.1 do have the standard one, it's completely broken unless
206 // /Zc:wchar_t is used while the other one works so use it instead, and
207 // only VC8 has a working standard-compliant swprintf()
208 #if defined(__WXWINCE__) || \
209 (defined(__VISUALC__) && __VISUALC__ < 1400) || \
210 defined(__GNUWIN32__) || \
211 defined(__BORLANDC__)
212 #define HAVE_BROKEN_SWPRINTF_DECL
213 #endif
214
215 // problem: on some systems swprintf takes the 'max' argument while on
216 // others it doesn't
217 #if defined(HAVE_BROKEN_SWPRINTF_DECL)
218 // like when using sprintf(), since 'max' is not used, wxVsnprintf()
219 // should always ensure that 'buff' is big enough for all common needs
220 #define system_sprintf(buff, max, flags, data) \
221 ::swprintf(buff, flags, data)
222
223 #define SYSTEM_SPRINTF_IS_UNSAFE
224 #else
225 #if !defined(HAVE_SWPRINTF)
226 #error wxVsnprintf() needs a system swprintf() implementation!
227 #endif
228
229 #define system_sprintf(buff, max, flags, data) \
230 ::swprintf(buff, max, flags, data)
231 #endif
232 #else // !wxUSE_UNICODE
233 #if defined(__VISUALC__) || \
234 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
235 #define system_sprintf(buff, max, flags, data) \
236 ::_snprintf(buff, max, flags, data)
237 #elif defined(HAVE_SNPRINTF)
238 #define system_sprintf(buff, max, flags, data) \
239 ::snprintf(buff, max, flags, data)
240 #else // NB: at least sprintf() should always be available
241 // since 'max' is not used in this case, wxVsnprintf() should always
242 // ensure that 'buff' is big enough for all common needs
243 // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN)
244 #define system_sprintf(buff, max, flags, data) \
245 ::sprintf(buff, flags, data)
246
247 #define SYSTEM_SPRINTF_IS_UNSAFE
248 #endif
249 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
250
251
252
253 // the conversion specifiers accepted by wxVsnprintf_
254 enum wxPrintfArgType {
255 wxPAT_INVALID = -1,
256
257 wxPAT_INT, // %d, %i, %o, %u, %x, %X
258 wxPAT_LONGINT, // %ld, etc
259 #if SIZEOF_LONG_LONG
260 wxPAT_LONGLONGINT, // %Ld, etc
261 #endif
262 wxPAT_SIZET, // %Zd, etc
263
264 wxPAT_DOUBLE, // %e, %E, %f, %g, %G
265 wxPAT_LONGDOUBLE, // %le, etc
266
267 wxPAT_POINTER, // %p
268
269 wxPAT_CHAR, // %hc (in ANSI mode: %c, too)
270 wxPAT_WCHAR, // %lc (in Unicode mode: %c, too)
271
272 wxPAT_PCHAR, // %s (related to a char *)
273 wxPAT_PWCHAR, // %s (related to a wchar_t *)
274
275 wxPAT_NINT, // %n
276 wxPAT_NSHORTINT, // %hn
277 wxPAT_NLONGINT // %ln
278 };
279
280 // an argument passed to wxVsnprintf_
281 typedef union {
282 int pad_int; // %d, %i, %o, %u, %x, %X
283 long int pad_longint; // %ld, etc
284 #if SIZEOF_LONG_LONG
285 long long int pad_longlongint; // %Ld, etc
286 #endif
287 size_t pad_sizet; // %Zd, etc
288
289 double pad_double; // %e, %E, %f, %g, %G
290 long double pad_longdouble; // %le, etc
291
292 void *pad_pointer; // %p
293
294 char pad_char; // %hc (in ANSI mode: %c, too)
295 wchar_t pad_wchar; // %lc (in Unicode mode: %c, too)
296
297 char *pad_pchar; // %s (related to a char *)
298 wchar_t *pad_pwchar; // %s (related to a wchar_t *)
299
300 int *pad_nint; // %n
301 short int *pad_nshortint; // %hn
302 long int *pad_nlongint; // %ln
303 } wxPrintfArg;
304
305
306 // Contains parsed data relative to a conversion specifier given to
307 // wxVsnprintf_ and parsed from the format string
308 // NOTE: in C++ there is almost no difference between struct & classes thus
309 // there is no performance gain by using a struct here...
310 class wxPrintfConvSpec
311 {
312 public:
313
314 // the position of the argument relative to this conversion specifier
315 size_t m_pos;
316
317 // the type of this conversion specifier
318 wxPrintfArgType m_type;
319
320 // the minimum and maximum width
321 // when one of this var is set to -1 it means: use the following argument
322 // in the stack as minimum/maximum width for this conversion specifier
323 int m_nMinWidth, m_nMaxWidth;
324
325 // does the argument need to the be aligned to left ?
326 bool m_bAlignLeft;
327
328 // pointer to the '%' of this conversion specifier in the format string
329 // NOTE: this points somewhere in the string given to the Parse() function -
330 // it's task of the caller ensure that memory is still valid !
331 const wxChar *m_pArgPos;
332
333 // pointer to the last character of this conversion specifier in the
334 // format string
335 // NOTE: this points somewhere in the string given to the Parse() function -
336 // it's task of the caller ensure that memory is still valid !
337 const wxChar *m_pArgEnd;
338
339 // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse()
340 // for use in Process()
341 // NB: even if this buffer is used only for numeric conversion specifiers and
342 // thus could be safely declared as a char[] buffer, we want it to be wxChar
343 // so that in Unicode builds we can avoid to convert its contents to Unicode
344 // chars when copying it in user's buffer.
345 wxChar m_szFlags[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
346
347
348 public:
349
350 // we don't declare this as a constructor otherwise it would be called
351 // automatically and we don't want this: to be optimized, wxVsnprintf_
352 // calls this function only on really-used instances of this class.
353 void Init();
354
355 // Parses the first conversion specifier in the given string, which must
356 // begin with a '%'. Returns false if the first '%' does not introduce a
357 // (valid) conversion specifier and thus should be ignored.
358 bool Parse(const wxChar *format);
359
360 // Process this conversion specifier and puts the result in the given
361 // buffer. Returns the number of characters written in 'buf' or -1 if
362 // there's not enough space.
363 int Process(wxChar *buf, size_t lenMax, wxPrintfArg *p);
364
365 // Loads the argument of this conversion specifier from given va_list.
366 bool LoadArg(wxPrintfArg *p, va_list &argptr);
367
368 private:
369 // An helper function of LoadArg() which is used to handle the '*' flag
370 void ReplaceAsteriskWith(int w);
371 };
372
373 void wxPrintfConvSpec::Init()
374 {
375 m_nMinWidth = 0;
376 m_nMaxWidth = 0xFFFF;
377 m_pos = 0;
378 m_bAlignLeft = false;
379 m_pArgPos = m_pArgEnd = NULL;
380 m_type = wxPAT_INVALID;
381
382 // this character will never be removed from m_szFlags array and
383 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
384 m_szFlags[0] = wxT('%');
385 }
386
387 bool wxPrintfConvSpec::Parse(const wxChar *format)
388 {
389 bool done = false;
390
391 // temporary parse data
392 size_t flagofs = 1;
393 bool in_prec, prec_dot;
394 int ilen = 0;
395
396 m_bAlignLeft = in_prec = prec_dot = false;
397 m_pArgPos = m_pArgEnd = format;
398 do
399 {
400 #define CHECK_PREC \
401 if (in_prec && !prec_dot) \
402 { \
403 m_szFlags[flagofs++] = wxT('.'); \
404 prec_dot = true; \
405 }
406
407 // what follows '%'?
408 const wxChar ch = *(++m_pArgEnd);
409 switch ( ch )
410 {
411 case wxT('\0'):
412 return false; // not really an argument
413
414 case wxT('%'):
415 return false; // not really an argument
416
417 case wxT('#'):
418 case wxT('0'):
419 case wxT(' '):
420 case wxT('+'):
421 case wxT('\''):
422 CHECK_PREC
423 m_szFlags[flagofs++] = ch;
424 break;
425
426 case wxT('-'):
427 CHECK_PREC
428 m_bAlignLeft = true;
429 m_szFlags[flagofs++] = ch;
430 break;
431
432 case wxT('.'):
433 CHECK_PREC
434 in_prec = true;
435 prec_dot = false;
436 m_nMaxWidth = 0;
437 // dot will be auto-added to m_szFlags if non-negative
438 // number follows
439 break;
440
441 case wxT('h'):
442 ilen = -1;
443 CHECK_PREC
444 m_szFlags[flagofs++] = ch;
445 break;
446
447 case wxT('l'):
448 // NB: it's safe to use flagofs-1 as flagofs always start from 1
449 if (m_szFlags[flagofs-1] == 'l') // 'll' modifier is the same as 'L' or 'q'
450 ilen = 2;
451 else
452 ilen = 1;
453 CHECK_PREC
454 m_szFlags[flagofs++] = ch;
455 break;
456
457 case wxT('q'):
458 case wxT('L'):
459 ilen = 2;
460 CHECK_PREC
461 m_szFlags[flagofs++] = ch;
462 break;
463
464 case wxT('Z'):
465 ilen = 3;
466 CHECK_PREC
467 m_szFlags[flagofs++] = ch;
468 break;
469
470 case wxT('*'):
471 if (in_prec)
472 {
473 CHECK_PREC
474
475 // tell Process() to use the next argument
476 // in the stack as maxwidth...
477 m_nMaxWidth = -1;
478 }
479 else
480 {
481 // tell Process() to use the next argument
482 // in the stack as minwidth...
483 m_nMinWidth = -1;
484 }
485
486 // save the * in our formatting buffer...
487 // will be replaced later by Process()
488 m_szFlags[flagofs++] = ch;
489 break;
490
491 case wxT('1'): case wxT('2'): case wxT('3'):
492 case wxT('4'): case wxT('5'): case wxT('6'):
493 case wxT('7'): case wxT('8'): case wxT('9'):
494 {
495 int len = 0;
496 CHECK_PREC
497 while ( (*m_pArgEnd >= wxT('0')) &&
498 (*m_pArgEnd <= wxT('9')) )
499 {
500 m_szFlags[flagofs++] = (*m_pArgEnd);
501 len = len*10 + (*m_pArgEnd - wxT('0'));
502 m_pArgEnd++;
503 }
504
505 if (in_prec)
506 m_nMaxWidth = len;
507 else
508 m_nMinWidth = len;
509
510 m_pArgEnd--; // the main loop pre-increments n again
511 }
512 break;
513
514 case wxT('$'): // a positional parameter (e.g. %2$s) ?
515 {
516 if (m_nMinWidth <= 0)
517 break; // ignore this formatting flag as no
518 // numbers are preceding it
519
520 // remove from m_szFlags all digits previously added
521 do {
522 flagofs--;
523 } while (m_szFlags[flagofs] >= '1' &&
524 m_szFlags[flagofs] <= '9');
525
526 // re-adjust the offset making it point to the
527 // next free char of m_szFlags
528 flagofs++;
529
530 m_pos = m_nMinWidth;
531 m_nMinWidth = 0;
532 }
533 break;
534
535 case wxT('d'):
536 case wxT('i'):
537 case wxT('o'):
538 case wxT('u'):
539 case wxT('x'):
540 case wxT('X'):
541 CHECK_PREC
542 m_szFlags[flagofs++] = ch;
543 m_szFlags[flagofs] = wxT('\0');
544 if (ilen == 0)
545 m_type = wxPAT_INT;
546 else if (ilen == -1)
547 // NB: 'short int' value passed through '...'
548 // is promoted to 'int', so we have to get
549 // an int from stack even if we need a short
550 m_type = wxPAT_INT;
551 else if (ilen == 1)
552 m_type = wxPAT_LONGINT;
553 else if (ilen == 2)
554 #if SIZEOF_LONG_LONG
555 m_type = wxPAT_LONGLONGINT;
556 #else // !long long
557 m_type = wxPAT_LONGINT;
558 #endif // long long/!long long
559 else if (ilen == 3)
560 m_type = wxPAT_SIZET;
561 done = true;
562 break;
563
564 case wxT('e'):
565 case wxT('E'):
566 case wxT('f'):
567 case wxT('g'):
568 case wxT('G'):
569 CHECK_PREC
570 m_szFlags[flagofs++] = ch;
571 m_szFlags[flagofs] = wxT('\0');
572 if (ilen == 2)
573 m_type = wxPAT_LONGDOUBLE;
574 else
575 m_type = wxPAT_DOUBLE;
576 done = true;
577 break;
578
579 case wxT('p'):
580 m_type = wxPAT_POINTER;
581 done = true;
582 break;
583
584 case wxT('c'):
585 if (ilen == -1)
586 {
587 // in Unicode mode %hc == ANSI character
588 // and in ANSI mode, %hc == %c == ANSI...
589 m_type = wxPAT_CHAR;
590 }
591 else if (ilen == 1)
592 {
593 // in ANSI mode %lc == Unicode character
594 // and in Unicode mode, %lc == %c == Unicode...
595 m_type = wxPAT_WCHAR;
596 }
597 else
598 {
599 #if wxUSE_UNICODE
600 // in Unicode mode, %c == Unicode character
601 m_type = wxPAT_WCHAR;
602 #else
603 // in ANSI mode, %c == ANSI character
604 m_type = wxPAT_CHAR;
605 #endif
606 }
607 done = true;
608 break;
609
610 case wxT('s'):
611 if (ilen == -1)
612 {
613 // Unicode mode wx extension: we'll let %hs mean non-Unicode
614 // strings (when in ANSI mode, %s == %hs == ANSI string)
615 m_type = wxPAT_PCHAR;
616 }
617 else if (ilen == 1)
618 {
619 // in Unicode mode, %ls == %s == Unicode string
620 // in ANSI mode, %ls == Unicode string
621 m_type = wxPAT_PWCHAR;
622 }
623 else
624 {
625 #if wxUSE_UNICODE
626 m_type = wxPAT_PWCHAR;
627 #else
628 m_type = wxPAT_PCHAR;
629 #endif
630 }
631 done = true;
632 break;
633
634 case wxT('n'):
635 if (ilen == 0)
636 m_type = wxPAT_NINT;
637 else if (ilen == -1)
638 m_type = wxPAT_NSHORTINT;
639 else if (ilen >= 1)
640 m_type = wxPAT_NLONGINT;
641 done = true;
642 break;
643
644 default:
645 // bad format, don't consider this an argument;
646 // leave it unchanged
647 return false;
648 }
649
650 if (flagofs == wxMAX_SVNPRINTF_FLAGBUFFER_LEN)
651 {
652 wxLogDebug(wxT("Too many flags specified for a single conversion specifier!"));
653 return false;
654 }
655 }
656 while (!done);
657
658 return true; // parsing was successful
659 }
660
661
662 void wxPrintfConvSpec::ReplaceAsteriskWith(int width)
663 {
664 wxChar temp[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
665
666 // find the first * in our flag buffer
667 wxChar *pwidth = wxStrchr(m_szFlags, wxT('*'));
668 wxASSERT(pwidth);
669
670 // save what follows the * (the +1 is to skip the asterisk itself!)
671 wxStrcpy(temp, pwidth+1);
672 if (width < 0)
673 {
674 pwidth[0] = wxT('-');
675 pwidth++;
676 }
677
678 // replace * with the actual integer given as width
679 #ifndef SYSTEM_SPRINTF_IS_UNSAFE
680 int maxlen = (m_szFlags + wxMAX_SVNPRINTF_FLAGBUFFER_LEN - pwidth) /
681 sizeof(wxChar);
682 #endif
683 int offset = system_sprintf(pwidth, maxlen, wxT("%d"), abs(width));
684
685 // restore after the expanded * what was following it
686 wxStrcpy(pwidth+offset, temp);
687 }
688
689 bool wxPrintfConvSpec::LoadArg(wxPrintfArg *p, va_list &argptr)
690 {
691 // did the '*' width/precision specifier was used ?
692 if (m_nMaxWidth == -1)
693 {
694 // take the maxwidth specifier from the stack
695 m_nMaxWidth = va_arg(argptr, int);
696 if (m_nMaxWidth < 0)
697 m_nMaxWidth = 0;
698 else
699 ReplaceAsteriskWith(m_nMaxWidth);
700 }
701
702 if (m_nMinWidth == -1)
703 {
704 // take the minwidth specifier from the stack
705 m_nMinWidth = va_arg(argptr, int);
706
707 ReplaceAsteriskWith(m_nMinWidth);
708 if (m_nMinWidth < 0)
709 {
710 m_bAlignLeft = !m_bAlignLeft;
711 m_nMinWidth = -m_nMinWidth;
712 }
713 }
714
715 switch (m_type) {
716 case wxPAT_INT:
717 p->pad_int = va_arg(argptr, int);
718 break;
719 case wxPAT_LONGINT:
720 p->pad_longint = va_arg(argptr, long int);
721 break;
722 #if SIZEOF_LONG_LONG
723 case wxPAT_LONGLONGINT:
724 p->pad_longlongint = va_arg(argptr, long long int);
725 break;
726 #endif
727 case wxPAT_SIZET:
728 p->pad_sizet = va_arg(argptr, size_t);
729 break;
730 case wxPAT_DOUBLE:
731 p->pad_double = va_arg(argptr, double);
732 break;
733 case wxPAT_LONGDOUBLE:
734 p->pad_longdouble = va_arg(argptr, long double);
735 break;
736 case wxPAT_POINTER:
737 p->pad_pointer = va_arg(argptr, void *);
738 break;
739
740 case wxPAT_CHAR:
741 p->pad_char = (char)va_arg(argptr, int); // char is promoted to int when passed through '...'
742 break;
743 case wxPAT_WCHAR:
744 p->pad_wchar = (wchar_t)va_arg(argptr, int); // char is promoted to int when passed through '...'
745 break;
746
747 case wxPAT_PCHAR:
748 p->pad_pchar = va_arg(argptr, char *);
749 break;
750 case wxPAT_PWCHAR:
751 p->pad_pwchar = va_arg(argptr, wchar_t *);
752 break;
753
754 case wxPAT_NINT:
755 p->pad_nint = va_arg(argptr, int *);
756 break;
757 case wxPAT_NSHORTINT:
758 p->pad_nshortint = va_arg(argptr, short int *);
759 break;
760 case wxPAT_NLONGINT:
761 p->pad_nlongint = va_arg(argptr, long int *);
762 break;
763
764 case wxPAT_INVALID:
765 default:
766 return false;
767 }
768
769 return true; // loading was successful
770 }
771
772 int wxPrintfConvSpec::Process(wxChar *buf, size_t lenMax, wxPrintfArg *p)
773 {
774 // buffer to avoid dynamic memory allocation each time for small strings;
775 // note that this buffer is used only to hold results of number formatting,
776 // %s directly writes user's string in buf, without using szScratch
777 wxChar szScratch[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN];
778 size_t lenScratch = 0, lenCur = 0;
779
780 #define APPEND_CH(ch) \
781 { \
782 if ( lenCur == lenMax ) \
783 return -1; \
784 \
785 buf[lenCur++] = ch; \
786 }
787
788 #define APPEND_STR(s) \
789 { \
790 for ( const wxChar *p = s; *p; p++ ) \
791 { \
792 APPEND_CH(*p); \
793 } \
794 }
795
796 switch ( m_type )
797 {
798 case wxPAT_INT:
799 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_int);
800 break;
801
802 case wxPAT_LONGINT:
803 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longint);
804 break;
805
806 #if SIZEOF_LONG_LONG
807 case wxPAT_LONGLONGINT:
808 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longlongint);
809 break;
810 #endif // SIZEOF_LONG_LONG
811
812 case wxPAT_SIZET:
813 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_sizet);
814 break;
815
816 case wxPAT_LONGDOUBLE:
817 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longdouble);
818 break;
819
820 case wxPAT_DOUBLE:
821 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_double);
822 break;
823
824 case wxPAT_POINTER:
825 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_pointer);
826 break;
827
828 case wxPAT_CHAR:
829 case wxPAT_WCHAR:
830 {
831 wxChar val =
832 #if wxUSE_UNICODE
833 p->pad_wchar;
834
835 if (m_type == wxPAT_CHAR)
836 {
837 // user passed a character explicitely indicated as ANSI...
838 const char buf[2] = { p->pad_char, 0 };
839 val = wxString(buf, wxConvLibc)[0u];
840
841 //wprintf(L"converting ANSI=>Unicode"); // for debug
842 }
843 #else
844 p->pad_char;
845
846 #if wxUSE_WCHAR_T
847 if (m_type == wxPAT_WCHAR)
848 {
849 // user passed a character explicitely indicated as Unicode...
850 const wchar_t buf[2] = { p->pad_wchar, 0 };
851 val = wxString(buf, wxConvLibc)[0u];
852
853 //printf("converting Unicode=>ANSI"); // for debug
854 }
855 #endif
856 #endif
857
858 size_t i;
859
860 if (!m_bAlignLeft)
861 for (i = 1; i < (size_t)m_nMinWidth; i++)
862 APPEND_CH(_T(' '));
863
864 APPEND_CH(val);
865
866 if (m_bAlignLeft)
867 for (i = 1; i < (size_t)m_nMinWidth; i++)
868 APPEND_CH(_T(' '));
869 }
870 break;
871
872 case wxPAT_PCHAR:
873 case wxPAT_PWCHAR:
874 {
875 wxString s;
876 const wxChar *val =
877 #if wxUSE_UNICODE
878 p->pad_pwchar;
879
880 if (m_type == wxPAT_PCHAR)
881 {
882 // user passed a string explicitely indicated as ANSI...
883 val = s = wxString(p->pad_pchar, wxConvLibc);
884
885 //wprintf(L"converting ANSI=>Unicode"); // for debug
886 }
887 #else
888 p->pad_pchar;
889
890 #if wxUSE_WCHAR_T
891 if (m_type == wxPAT_PWCHAR)
892 {
893 // user passed a string explicitely indicated as Unicode...
894 val = s = wxString(p->pad_pwchar, wxConvLibc);
895
896 //printf("converting Unicode=>ANSI"); // for debug
897 }
898 #endif
899 #endif
900 int len;
901
902 if (val)
903 {
904 #if wxUSE_STRUTILS
905 // at this point we are sure that m_nMaxWidth is positive or null
906 // (see top of wxPrintfConvSpec::LoadArg)
907 len = wxMin((unsigned int)m_nMaxWidth, wxStrlen(val));
908 #else
909 for ( len = 0; val[len] && (len < m_nMaxWidth); len++ )
910 ;
911 #endif
912 }
913 else if (m_nMaxWidth >= 6)
914 {
915 val = wxT("(null)");
916 len = 6;
917 }
918 else
919 {
920 val = wxEmptyString;
921 len = 0;
922 }
923
924 int i;
925
926 if (!m_bAlignLeft)
927 {
928 for (i = len; i < m_nMinWidth; i++)
929 APPEND_CH(_T(' '));
930 }
931
932 #if wxUSE_STRUTILS
933 len = wxMin((unsigned int)len, lenMax-lenCur);
934 wxStrncpy(buf+lenCur, val, len);
935 lenCur += len;
936 #else
937 for (i = 0; i < len; i++)
938 APPEND_CH(val[i]);
939 #endif
940
941 if (m_bAlignLeft)
942 {
943 for (i = len; i < m_nMinWidth; i++)
944 APPEND_CH(_T(' '));
945 }
946 }
947 break;
948
949 case wxPAT_NINT:
950 *p->pad_nint = lenCur;
951 break;
952
953 case wxPAT_NSHORTINT:
954 *p->pad_nshortint = (short int)lenCur;
955 break;
956
957 case wxPAT_NLONGINT:
958 *p->pad_nlongint = lenCur;
959 break;
960
961 case wxPAT_INVALID:
962 default:
963 return -1;
964 }
965
966 #ifdef HAVE_BROKEN_SWPRINTF_DECL
967 wxUnusedVar(lenScratch); // avoid dummy warnings
968 #endif
969
970 // if we used system's sprintf() then we now need to append the s_szScratch
971 // buffer to the given one...
972 switch (m_type)
973 {
974 case wxPAT_INT:
975 case wxPAT_LONGINT:
976 #if SIZEOF_LONG_LONG
977 case wxPAT_LONGLONGINT:
978 #endif
979 case wxPAT_SIZET:
980 case wxPAT_LONGDOUBLE:
981 case wxPAT_DOUBLE:
982 case wxPAT_POINTER:
983 #if wxUSE_STRUTILS
984 {
985 wxASSERT(lenScratch >= 0 && lenScratch < wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN);
986 if (lenMax < lenScratch)
987 {
988 // fill output buffer and then return -1
989 wxStrncpy(buf, szScratch, lenMax);
990 return -1;
991 }
992 wxStrncpy(buf, szScratch, lenScratch);
993 lenCur += lenScratch;
994 }
995 #else
996 {
997 APPEND_STR(szScratch);
998 }
999 #endif
1000 break;
1001
1002 default:
1003 break; // all other cases were completed previously
1004 }
1005
1006 return lenCur;
1007 }
1008
1009 // differences from standard strncpy:
1010 // 1) copies everything from 'source' except for '%%' sequence which is copied as '%'
1011 // 2) returns the number of written characters in 'dest' as it could differ from given 'n'
1012 // 3) much less optimized, unfortunately...
1013 static int wxCopyStrWithPercents(wxChar *dest, const wxChar *source, size_t n)
1014 {
1015 size_t written = 0;
1016
1017 if (n == 0)
1018 return 0;
1019
1020 size_t i;
1021 for ( i = 0; i < n-1; source++, i++)
1022 {
1023 dest[written++] = *source;
1024 if (*(source+1) == wxT('%'))
1025 {
1026 // skip this additional '%' character
1027 source++;
1028 i++;
1029 }
1030 }
1031
1032 if (i < n)
1033 // copy last character inconditionally
1034 dest[written++] = *source;
1035
1036 return written;
1037 }
1038
1039 int WXDLLEXPORT wxVsnprintf_(wxChar *buf, size_t lenMax,
1040 const wxChar *format, va_list argptr)
1041 {
1042 // useful for debugging, to understand if we are really using this function
1043 // rather than the system implementation
1044 #if 0
1045 wprintf(L"Using wxVsnprintf_\n");
1046 #endif
1047
1048 // required memory:
1049 wxPrintfConvSpec arg[wxMAX_SVNPRINTF_ARGUMENTS];
1050 wxPrintfArg argdata[wxMAX_SVNPRINTF_ARGUMENTS];
1051 wxPrintfConvSpec *pspec[wxMAX_SVNPRINTF_ARGUMENTS] = { NULL };
1052
1053 size_t i;
1054
1055 // number of characters in the buffer so far, must be less than lenMax
1056 size_t lenCur = 0;
1057
1058 size_t nargs = 0;
1059 const wxChar *toparse = format;
1060
1061 // parse the format string
1062 bool posarg_present = false, nonposarg_present = false;
1063 for (; *toparse != wxT('\0'); toparse++)
1064 {
1065 if (*toparse == wxT('%') )
1066 {
1067 arg[nargs].Init();
1068
1069 // let's see if this is a (valid) conversion specifier...
1070 if (arg[nargs].Parse(toparse))
1071 {
1072 // ...yes it is
1073 wxPrintfConvSpec *current = &arg[nargs];
1074
1075 // make toparse point to the end of this specifier
1076 toparse = current->m_pArgEnd;
1077
1078 if (current->m_pos > 0)
1079 {
1080 // the positionals start from number 1... adjust the index
1081 current->m_pos--;
1082 posarg_present = true;
1083 }
1084 else
1085 {
1086 // not a positional argument...
1087 current->m_pos = nargs;
1088 nonposarg_present = true;
1089 }
1090
1091 // this conversion specifier is tied to the pos-th argument...
1092 pspec[current->m_pos] = current;
1093 nargs++;
1094
1095 if (nargs == wxMAX_SVNPRINTF_ARGUMENTS)
1096 {
1097 wxLogDebug(wxT("A single call to wxVsnprintf() has more than %d arguments; ")
1098 wxT("ignoring all remaining arguments."), wxMAX_SVNPRINTF_ARGUMENTS);
1099 break; // cannot handle any additional conv spec
1100 }
1101 }
1102 else
1103 {
1104 // it's safe to look in the next character of toparse as at worst
1105 // we'll hit its \0
1106 if (*(toparse+1) == wxT('%'))
1107 toparse++; // the Parse() returned false because we've found a %%
1108 }
1109 }
1110 }
1111
1112 if (posarg_present && nonposarg_present)
1113 return -1; // format strings with both positional and
1114 // non-positional conversion specifier are unsupported !!
1115
1116 // on platforms where va_list is an array type, it is necessary to make a
1117 // copy to be able to pass it to LoadArg as a reference.
1118 bool ok = true;
1119 va_list ap;
1120 wxVaCopy(ap, argptr);
1121
1122 // now load arguments from stack
1123 for (i=0; i < nargs && ok; i++)
1124 {
1125 // !pspec[i] means that the user forgot a positional parameter (e.g. %$1s %$3s);
1126 // LoadArg == false means that wxPrintfConvSpec::Parse failed to set the
1127 // conversion specifier 'type' to a valid value...
1128 ok = pspec[i] && pspec[i]->LoadArg(&argdata[i], ap);
1129 }
1130
1131 va_end(ap);
1132
1133 // something failed while loading arguments from the variable list...
1134 if (!ok)
1135 return -1;
1136
1137 // finally, process each conversion specifier with its own argument
1138 toparse = format;
1139 for (i=0; i < nargs; i++)
1140 {
1141 // copy in the output buffer the portion of the format string between
1142 // last specifier and the current one
1143 size_t tocopy = ( arg[i].m_pArgPos - toparse );
1144 if (lenCur+tocopy >= lenMax)
1145 {
1146 // not enough space in the output buffer !
1147 // copy until the end of remaining space and then stop
1148 wxCopyStrWithPercents(buf+lenCur, toparse, lenMax - lenCur - 1);
1149 buf[lenMax-1] = wxT('\0');
1150 return -1;
1151 }
1152
1153 lenCur += wxCopyStrWithPercents(buf+lenCur, toparse, tocopy);
1154
1155 // process this specifier directly in the output buffer
1156 int n = arg[i].Process(buf+lenCur, lenMax - lenCur, &argdata[arg[i].m_pos]);
1157 if (n == -1)
1158 {
1159 buf[lenMax-1] = wxT('\0'); // be sure to always NUL-terminate the string
1160 return -1; // not enough space in the output buffer !
1161 }
1162 lenCur += n;
1163
1164 // the +1 is because wxPrintfConvSpec::m_pArgEnd points to the last character
1165 // of the format specifier, but we are not interested to it...
1166 toparse = arg[i].m_pArgEnd + 1;
1167 }
1168
1169 // copy portion of the format string after last specifier
1170 // NOTE: toparse is pointing to the character just after the last processed
1171 // conversion specifier
1172 // NOTE2: the +1 is because we want to copy also the '\0'
1173 size_t tocopy = wxStrlen(format) + 1 - ( toparse - format ) ;
1174 if (lenCur+tocopy >= lenMax)
1175 return -1; // not enough space in the output buffer !
1176
1177 // the -1 is because of the '\0'
1178 lenCur += wxCopyStrWithPercents(buf+lenCur, toparse, tocopy) - 1;
1179
1180 wxASSERT(lenCur == wxStrlen(buf));
1181 return lenCur;
1182 }
1183
1184 #undef APPEND_CH
1185 #undef APPEND_STR
1186 #undef CHECK_PREC
1187
1188 #endif // !wxVsnprintfA
1189
1190 #if !defined(wxSnprintf_)
1191 int WXDLLEXPORT wxSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...)
1192 {
1193 va_list argptr;
1194 va_start(argptr, format);
1195
1196 int iLen = wxVsnprintf_(buf, len, format, argptr);
1197
1198 va_end(argptr);
1199
1200 return iLen;
1201 }
1202 #endif // wxSnprintf_
1203
1204 #if defined(__DMC__)
1205 /* Digital Mars adds count to _stprintf (C99) so convert */
1206 #if wxUSE_UNICODE
1207 int wxSprintf (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... )
1208 {
1209 va_list arglist;
1210
1211 va_start( arglist, format );
1212 int iLen = swprintf ( s, -1, format, arglist );
1213 va_end( arglist );
1214 return iLen ;
1215 }
1216
1217 #endif // wxUSE_UNICODE
1218
1219 #endif //__DMC__
1220
1221 // ----------------------------------------------------------------------------
1222 // implement the standard IO functions for wide char if libc doesn't have them
1223 // ----------------------------------------------------------------------------
1224
1225 #ifdef wxNEED_FPUTS
1226 int wxFputs(const wchar_t *ws, FILE *stream)
1227 {
1228 // counting the number of wide characters written isn't worth the trouble,
1229 // simply distinguish between ok and error
1230 return fputs(wxConvLibc.cWC2MB(ws), stream) == -1 ? -1 : 0;
1231 }
1232 #endif // wxNEED_FPUTS
1233
1234 #ifdef wxNEED_PUTS
1235 int wxPuts(const wxChar *ws)
1236 {
1237 int rc = wxFputs(ws, stdout);
1238 if ( rc != -1 )
1239 {
1240 if ( wxFputs(L"\n", stdout) == -1 )
1241 return -1;
1242
1243 rc++;
1244 }
1245
1246 return rc;
1247 }
1248 #endif // wxNEED_PUTS
1249
1250 #ifdef wxNEED_PUTC
1251 int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream)
1252 {
1253 wchar_t ws[2] = { wc, L'\0' };
1254
1255 return wxFputs(ws, stream);
1256 }
1257 #endif // wxNEED_PUTC
1258
1259 // NB: we only implement va_list functions here, the ones taking ... are
1260 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
1261 // the definitions there to avoid duplicating them here
1262 #ifdef wxNEED_WPRINTF
1263
1264 // TODO: implement the scanf() functions
1265 int vwscanf(const wxChar *format, va_list argptr)
1266 {
1267 wxFAIL_MSG( _T("TODO") );
1268
1269 return -1;
1270 }
1271
1272 int vswscanf(const wxChar *ws, const wxChar *format, va_list argptr)
1273 {
1274 // The best we can do without proper Unicode support in glibc is to
1275 // convert the strings into MB representation and run ANSI version
1276 // of the function. This doesn't work with %c and %s because of difference
1277 // in size of char and wchar_t, though.
1278
1279 wxCHECK_MSG( wxStrstr(format, _T("%s")) == NULL, -1,
1280 _T("incomplete vswscanf implementation doesn't allow %s") );
1281 wxCHECK_MSG( wxStrstr(format, _T("%c")) == NULL, -1,
1282 _T("incomplete vswscanf implementation doesn't allow %c") );
1283
1284 va_list argcopy;
1285 wxVaCopy(argcopy, argptr);
1286 return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argcopy);
1287 }
1288
1289 int vfwscanf(FILE *stream, const wxChar *format, va_list argptr)
1290 {
1291 wxFAIL_MSG( _T("TODO") );
1292
1293 return -1;
1294 }
1295
1296 #define vswprintf wxVsnprintf_
1297
1298 int vfwprintf(FILE *stream, const wxChar *format, va_list argptr)
1299 {
1300 wxString s;
1301 int rc = s.PrintfV(format, argptr);
1302
1303 if ( rc != -1 )
1304 {
1305 // we can't do much better without Unicode support in libc...
1306 if ( fprintf(stream, "%s", (const char*)s.mb_str() ) == -1 )
1307 return -1;
1308 }
1309
1310 return rc;
1311 }
1312
1313 int vwprintf(const wxChar *format, va_list argptr)
1314 {
1315 return wxVfprintf(stdout, format, argptr);
1316 }
1317
1318 #endif // wxNEED_WPRINTF
1319
1320 #ifdef wxNEED_PRINTF_CONVERSION
1321
1322 // ----------------------------------------------------------------------------
1323 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
1324 // ----------------------------------------------------------------------------
1325
1326 /*
1327 Here are the gory details. We want to follow the Windows/MS conventions,
1328 that is to have
1329
1330 In ANSI mode:
1331
1332 format specifier results in
1333 -----------------------------------
1334 %c, %hc, %hC char
1335 %lc, %C, %lC wchar_t
1336
1337 In Unicode mode:
1338
1339 format specifier results in
1340 -----------------------------------
1341 %hc, %C, %hC char
1342 %c, %lc, %lC wchar_t
1343
1344
1345 while on POSIX systems we have %C identical to %lc and %c always means char
1346 (in any mode) while %lc always means wchar_t,
1347
1348 So to use native functions in order to get our semantics we must do the
1349 following translations in Unicode mode (nothing to do in ANSI mode):
1350
1351 wxWidgets specifier POSIX specifier
1352 ----------------------------------------
1353
1354 %hc, %C, %hC %c
1355 %c %lc
1356
1357
1358 And, of course, the same should be done for %s as well.
1359 */
1360
1361 class wxFormatConverter
1362 {
1363 public:
1364 wxFormatConverter(const wxChar *format);
1365
1366 // notice that we only translated the string if m_fmtOrig == NULL (as set
1367 // by CopyAllBefore()), otherwise we should simply use the original format
1368 operator const wxChar *() const
1369 { return m_fmtOrig ? m_fmtOrig : m_fmt.c_str(); }
1370
1371 private:
1372 // copy another character to the translated format: this function does the
1373 // copy if we are translating but doesn't do anything at all if we don't,
1374 // so we don't create the translated format string at all unless we really
1375 // need to (i.e. InsertFmtChar() is called)
1376 wxChar CopyFmtChar(wxChar ch)
1377 {
1378 if ( !m_fmtOrig )
1379 {
1380 // we're translating, do copy
1381 m_fmt += ch;
1382 }
1383 else
1384 {
1385 // simply increase the count which should be copied by
1386 // CopyAllBefore() later if needed
1387 m_nCopied++;
1388 }
1389
1390 return ch;
1391 }
1392
1393 // insert an extra character
1394 void InsertFmtChar(wxChar ch)
1395 {
1396 if ( m_fmtOrig )
1397 {
1398 // so far we haven't translated anything yet
1399 CopyAllBefore();
1400 }
1401
1402 m_fmt += ch;
1403 }
1404
1405 void CopyAllBefore()
1406 {
1407 wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") );
1408
1409 m_fmt = wxString(m_fmtOrig, m_nCopied);
1410
1411 // we won't need it any longer
1412 m_fmtOrig = NULL;
1413 }
1414
1415 static bool IsFlagChar(wxChar ch)
1416 {
1417 return ch == _T('-') || ch == _T('+') ||
1418 ch == _T('0') || ch == _T(' ') || ch == _T('#');
1419 }
1420
1421 void SkipDigits(const wxChar **ptpc)
1422 {
1423 while ( **ptpc >= _T('0') && **ptpc <= _T('9') )
1424 CopyFmtChar(*(*ptpc)++);
1425 }
1426
1427 // the translated format
1428 wxString m_fmt;
1429
1430 // the original format
1431 const wxChar *m_fmtOrig;
1432
1433 // the number of characters already copied
1434 size_t m_nCopied;
1435 };
1436
1437 wxFormatConverter::wxFormatConverter(const wxChar *format)
1438 {
1439 m_fmtOrig = format;
1440 m_nCopied = 0;
1441
1442 while ( *format )
1443 {
1444 if ( CopyFmtChar(*format++) == _T('%') )
1445 {
1446 // skip any flags
1447 while ( IsFlagChar(*format) )
1448 CopyFmtChar(*format++);
1449
1450 // and possible width
1451 if ( *format == _T('*') )
1452 CopyFmtChar(*format++);
1453 else
1454 SkipDigits(&format);
1455
1456 // precision?
1457 if ( *format == _T('.') )
1458 {
1459 CopyFmtChar(*format++);
1460 if ( *format == _T('*') )
1461 CopyFmtChar(*format++);
1462 else
1463 SkipDigits(&format);
1464 }
1465
1466 // next we can have a size modifier
1467 enum
1468 {
1469 Default,
1470 Short,
1471 Long
1472 } size;
1473
1474 switch ( *format )
1475 {
1476 case _T('h'):
1477 size = Short;
1478 format++;
1479 break;
1480
1481 case _T('l'):
1482 // "ll" has a different meaning!
1483 if ( format[1] != _T('l') )
1484 {
1485 size = Long;
1486 format++;
1487 break;
1488 }
1489 //else: fall through
1490
1491 default:
1492 size = Default;
1493 }
1494
1495 // and finally we should have the type
1496 switch ( *format )
1497 {
1498 case _T('C'):
1499 case _T('S'):
1500 // %C and %hC -> %c and %lC -> %lc
1501 if ( size == Long )
1502 CopyFmtChar(_T('l'));
1503
1504 InsertFmtChar(*format++ == _T('C') ? _T('c') : _T('s'));
1505 break;
1506
1507 case _T('c'):
1508 case _T('s'):
1509 // %c -> %lc but %hc stays %hc and %lc is still %lc
1510 if ( size == Default)
1511 InsertFmtChar(_T('l'));
1512 // fall through
1513
1514 default:
1515 // nothing special to do
1516 if ( size != Default )
1517 CopyFmtChar(*(format - 1));
1518 CopyFmtChar(*format++);
1519 }
1520 }
1521 }
1522 }
1523
1524 #else // !wxNEED_PRINTF_CONVERSION
1525 // no conversion necessary
1526 #define wxFormatConverter(x) (x)
1527 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
1528
1529 #ifdef __WXDEBUG__
1530 // For testing the format converter
1531 wxString wxConvertFormat(const wxChar *format)
1532 {
1533 return wxString(wxFormatConverter(format));
1534 }
1535 #endif
1536
1537 // ----------------------------------------------------------------------------
1538 // wxPrintf(), wxScanf() and relatives
1539 // ----------------------------------------------------------------------------
1540
1541 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
1542
1543 int wxScanf( const wxChar *format, ... )
1544 {
1545 va_list argptr;
1546 va_start(argptr, format);
1547
1548 int ret = vwscanf(wxFormatConverter(format), argptr );
1549
1550 va_end(argptr);
1551
1552 return ret;
1553 }
1554
1555 int wxSscanf( const wxChar *str, const wxChar *format, ... )
1556 {
1557 va_list argptr;
1558 va_start(argptr, format);
1559
1560 int ret = vswscanf( str, wxFormatConverter(format), argptr );
1561
1562 va_end(argptr);
1563
1564 return ret;
1565 }
1566
1567 int wxFscanf( FILE *stream, const wxChar *format, ... )
1568 {
1569 va_list argptr;
1570 va_start(argptr, format);
1571 int ret = vfwscanf(stream, wxFormatConverter(format), argptr);
1572
1573 va_end(argptr);
1574
1575 return ret;
1576 }
1577
1578 int wxPrintf( const wxChar *format, ... )
1579 {
1580 va_list argptr;
1581 va_start(argptr, format);
1582
1583 int ret = vwprintf( wxFormatConverter(format), argptr );
1584
1585 va_end(argptr);
1586
1587 return ret;
1588 }
1589
1590 #ifndef wxSnprintf
1591 int wxSnprintf( wxChar *str, size_t size, const wxChar *format, ... )
1592 {
1593 va_list argptr;
1594 va_start(argptr, format);
1595
1596 int ret = vswprintf( str, size, wxFormatConverter(format), argptr );
1597
1598 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
1599 // doesn't nul terminate on truncation.
1600 str[size - 1] = 0;
1601
1602 va_end(argptr);
1603
1604 return ret;
1605 }
1606 #endif // wxSnprintf
1607
1608 int wxSprintf( wxChar *str, const wxChar *format, ... )
1609 {
1610 va_list argptr;
1611 va_start(argptr, format);
1612
1613 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
1614 // it's safe to implement this one in terms of it
1615 wxString s(wxString::FormatV(format, argptr));
1616 wxStrcpy(str, s);
1617
1618 va_end(argptr);
1619
1620 return s.length();
1621 }
1622
1623 int wxFprintf( FILE *stream, const wxChar *format, ... )
1624 {
1625 va_list argptr;
1626 va_start( argptr, format );
1627
1628 int ret = vfwprintf( stream, wxFormatConverter(format), argptr );
1629
1630 va_end(argptr);
1631
1632 return ret;
1633 }
1634
1635 int wxVsscanf( const wxChar *str, const wxChar *format, va_list argptr )
1636 {
1637 return vswscanf( str, wxFormatConverter(format), argptr );
1638 }
1639
1640 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr )
1641 {
1642 return vfwprintf( stream, wxFormatConverter(format), argptr );
1643 }
1644
1645 int wxVprintf( const wxChar *format, va_list argptr )
1646 {
1647 return vwprintf( wxFormatConverter(format), argptr );
1648 }
1649
1650 #ifndef wxVsnprintf
1651 int wxVsnprintf( wxChar *str, size_t size, const wxChar *format, va_list argptr )
1652 {
1653 return vswprintf( str, size, wxFormatConverter(format), argptr );
1654 }
1655 #endif // wxVsnprintf
1656
1657 int wxVsprintf( wxChar *str, const wxChar *format, va_list argptr )
1658 {
1659 // same as for wxSprintf()
1660 return vswprintf(str, INT_MAX / 4, wxFormatConverter(format), argptr);
1661 }
1662
1663 #endif // wxNEED_PRINTF_CONVERSION
1664
1665 #if wxUSE_WCHAR_T
1666
1667 // ----------------------------------------------------------------------------
1668 // ctype.h stuff (currently unused)
1669 // ----------------------------------------------------------------------------
1670
1671 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
1672 inline WORD wxMSW_ctype(wxChar ch)
1673 {
1674 WORD ret;
1675 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
1676 return ret;
1677 }
1678
1679 WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); }
1680 WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); }
1681 WXDLLEXPORT int wxIscntrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; }
1682 WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; }
1683 WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); }
1684 WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); }
1685 WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); }
1686 WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; }
1687 WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; }
1688 WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); }
1689 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; }
1690 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
1691 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
1692 #endif
1693
1694 #ifdef wxNEED_WX_MBSTOWCS
1695
1696 WXDLLEXPORT size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen)
1697 {
1698 if (!out)
1699 {
1700 size_t outsize = 0;
1701 while(*in++)
1702 outsize++;
1703 return outsize;
1704 }
1705
1706 const char* origin = in;
1707
1708 while (outlen-- && *in)
1709 {
1710 *out++ = (wchar_t) *in++;
1711 }
1712
1713 *out = '\0';
1714
1715 return in - origin;
1716 }
1717
1718 WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen)
1719 {
1720 if (!out)
1721 {
1722 size_t outsize = 0;
1723 while(*in++)
1724 outsize++;
1725 return outsize;
1726 }
1727
1728 const wchar_t* origin = in;
1729
1730 while (outlen-- && *in)
1731 {
1732 *out++ = (char) *in++;
1733 }
1734
1735 *out = '\0';
1736
1737 return in - origin;
1738 }
1739
1740 #endif // wxNEED_WX_MBSTOWCS
1741
1742 #if defined(wxNEED_WX_CTYPE_H)
1743
1744 #include <CoreFoundation/CoreFoundation.h>
1745
1746 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
1747 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
1748 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
1749 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
1750 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
1751 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
1752 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
1753 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
1754 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
1755 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
1756
1757 WXDLLEXPORT int wxIsalnum(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalnumset, ch); }
1758 WXDLLEXPORT int wxIsalpha(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalphaset, ch); }
1759 WXDLLEXPORT int wxIscntrl(wxChar ch) { return CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
1760 WXDLLEXPORT int wxIsdigit(wxChar ch) { return CFCharacterSetIsCharacterMember(cfdigitset, ch); }
1761 WXDLLEXPORT int wxIsgraph(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch) && ch != ' '; }
1762 WXDLLEXPORT int wxIslower(wxChar ch) { return CFCharacterSetIsCharacterMember(cflowerset, ch); }
1763 WXDLLEXPORT int wxIsprint(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
1764 WXDLLEXPORT int wxIspunct(wxChar ch) { return CFCharacterSetIsCharacterMember(cfpunctset, ch); }
1765 WXDLLEXPORT int wxIsspace(wxChar ch) { return CFCharacterSetIsCharacterMember(cfspaceset, ch); }
1766 WXDLLEXPORT int wxIsupper(wxChar ch) { return CFCharacterSetIsCharacterMember(cfupperset, ch); }
1767 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxIsdigit(ch) || (ch>='a' && ch<='f') || (ch>='A' && ch<='F'); }
1768 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)tolower((char)(ch)); }
1769 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)toupper((char)(ch)); }
1770
1771 #endif // wxNEED_WX_CTYPE_H
1772
1773 #ifndef wxStrdupA
1774
1775 WXDLLEXPORT char *wxStrdupA(const char *s)
1776 {
1777 return strcpy((char *)malloc(strlen(s) + 1), s);
1778 }
1779
1780 #endif // wxStrdupA
1781
1782 #ifndef wxStrdupW
1783
1784 WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz)
1785 {
1786 size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t);
1787 wchar_t *ret = (wchar_t *) malloc(size);
1788 memcpy(ret, pwz, size);
1789 return ret;
1790 }
1791
1792 #endif // wxStrdupW
1793
1794 #ifndef wxStricmp
1795 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
1796 {
1797 register wxChar c1, c2;
1798 do {
1799 c1 = wxTolower(*psz1++);
1800 c2 = wxTolower(*psz2++);
1801 } while ( c1 && (c1 == c2) );
1802 return c1 - c2;
1803 }
1804 #endif
1805
1806 #ifndef wxStricmp
1807 int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n)
1808 {
1809 // initialize the variables just to suppress stupid gcc warning
1810 register wxChar c1 = 0, c2 = 0;
1811 while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
1812 if (n) {
1813 if (c1 < c2) return -1;
1814 if (c1 > c2) return 1;
1815 }
1816 return 0;
1817 }
1818 #endif
1819
1820 #ifndef wxSetlocale
1821 WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
1822 {
1823 char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale));
1824
1825 return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld));
1826 }
1827 #endif
1828
1829 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
1830 WXDLLEXPORT size_t wxWcslen(const wchar_t *s)
1831 {
1832 size_t n = 0;
1833 while ( *s++ )
1834 n++;
1835
1836 return n;
1837 }
1838 #endif
1839
1840 // ----------------------------------------------------------------------------
1841 // string.h functions
1842 // ----------------------------------------------------------------------------
1843
1844 #ifdef wxNEED_WX_STRING_H
1845
1846 // RN: These need to be c externed for the regex lib
1847 #ifdef __cplusplus
1848 extern "C" {
1849 #endif
1850
1851 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
1852 {
1853 wxChar *ret = dest;
1854 while (*dest) dest++;
1855 while ((*dest++ = *src++));
1856 return ret;
1857 }
1858
1859 WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c)
1860 {
1861 // be careful here as the terminating NUL makes part of the string
1862 while ( *s != c )
1863 {
1864 if ( !*s++ )
1865 return NULL;
1866 }
1867
1868 return s;
1869 }
1870
1871 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
1872 {
1873 while ((*s1 == *s2) && *s1) s1++, s2++;
1874 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
1875 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
1876 return 0;
1877 }
1878
1879 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
1880 {
1881 wxChar *ret = dest;
1882 while ((*dest++ = *src++));
1883 return ret;
1884 }
1885
1886 WXDLLEXPORT size_t wxStrlen_(const wxChar *s)
1887 {
1888 size_t n = 0;
1889 while ( *s++ )
1890 n++;
1891
1892 return n;
1893 }
1894
1895
1896 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
1897 {
1898 wxChar *ret = dest;
1899 while (*dest) dest++;
1900 while (n && (*dest++ = *src++)) n--;
1901 return ret;
1902 }
1903
1904 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
1905 {
1906 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
1907 if (n) {
1908 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
1909 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
1910 }
1911 return 0;
1912 }
1913
1914 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
1915 {
1916 wxChar *ret = dest;
1917 while (n && (*dest++ = *src++)) n--;
1918 while (n) *dest++=0, n--; // the docs specify padding with zeroes
1919 return ret;
1920 }
1921
1922 WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
1923 {
1924 while (*s && !wxStrchr(accept, *s))
1925 s++;
1926
1927 return *s ? s : NULL;
1928 }
1929
1930 WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c)
1931 {
1932 const wxChar *ret = NULL;
1933 do
1934 {
1935 if ( *s == c )
1936 ret = s;
1937 s++;
1938 }
1939 while ( *s );
1940
1941 return ret;
1942 }
1943
1944 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
1945 {
1946 size_t len = 0;
1947 while (wxStrchr(accept, *s++)) len++;
1948 return len;
1949 }
1950
1951 WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle)
1952 {
1953 wxASSERT_MSG( needle != NULL, _T("NULL argument in wxStrstr") );
1954
1955 // VZ: this is not exactly the most efficient string search algorithm...
1956
1957 const size_t len = wxStrlen(needle);
1958
1959 while ( const wxChar *fnd = wxStrchr(haystack, *needle) )
1960 {
1961 if ( !wxStrncmp(fnd, needle, len) )
1962 return fnd;
1963
1964 haystack = fnd + 1;
1965 }
1966
1967 return NULL;
1968 }
1969
1970 #ifdef __cplusplus
1971 }
1972 #endif
1973
1974 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
1975 {
1976 const wxChar *start = nptr;
1977
1978 // FIXME: only correct for C locale
1979 while (wxIsspace(*nptr)) nptr++;
1980 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1981 while (wxIsdigit(*nptr)) nptr++;
1982 if (*nptr == wxT('.')) {
1983 nptr++;
1984 while (wxIsdigit(*nptr)) nptr++;
1985 }
1986 if (*nptr == wxT('E') || *nptr == wxT('e')) {
1987 nptr++;
1988 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1989 while (wxIsdigit(*nptr)) nptr++;
1990 }
1991
1992 wxString data(nptr, nptr-start);
1993 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
1994 char *rdat = wxMBSTRINGCAST dat;
1995 double ret = strtod(dat, &rdat);
1996
1997 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
1998
1999 return ret;
2000 }
2001
2002 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
2003 {
2004 const wxChar *start = nptr;
2005
2006 // FIXME: only correct for C locale
2007 while (wxIsspace(*nptr)) nptr++;
2008 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
2009 if (((base == 0) || (base == 16)) &&
2010 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
2011 nptr += 2;
2012 base = 16;
2013 }
2014 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
2015 else if (base == 0) base = 10;
2016
2017 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
2018 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
2019
2020 wxString data(start, nptr-start);
2021 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
2022 char *rdat = wxMBSTRINGCAST dat;
2023 long int ret = strtol(dat, &rdat, base);
2024
2025 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
2026
2027 return ret;
2028 }
2029
2030 WXDLLEXPORT unsigned long int wxStrtoul(const wxChar *nptr, wxChar **endptr, int base)
2031 {
2032 return (unsigned long int) wxStrtol(nptr, endptr, base);
2033 }
2034
2035 #endif // wxNEED_WX_STRING_H
2036
2037 #ifdef wxNEED_WX_STDIO_H
2038 WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
2039 {
2040 char mode_buffer[10];
2041 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
2042 mode_buffer[i] = (char) mode[i];
2043
2044 return fopen( wxConvFile.cWX2MB(path), mode_buffer );
2045 }
2046
2047 WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
2048 {
2049 char mode_buffer[10];
2050 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
2051 mode_buffer[i] = (char) mode[i];
2052
2053 return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream );
2054 }
2055
2056 WXDLLEXPORT int wxRemove(const wxChar *path)
2057 {
2058 return remove( wxConvFile.cWX2MB(path) );
2059 }
2060
2061 WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
2062 {
2063 return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) );
2064 }
2065 #endif
2066
2067 #ifndef wxAtof
2068 double WXDLLEXPORT wxAtof(const wxChar *psz)
2069 {
2070 #ifdef __WXWINCE__
2071 double d;
2072 wxString str(psz);
2073 if (str.ToDouble(& d))
2074 return d;
2075
2076 return 0.0;
2077 #else
2078 return atof(wxConvLibc.cWX2MB(psz));
2079 #endif
2080 }
2081 #endif
2082
2083 #ifdef wxNEED_WX_STDLIB_H
2084 int WXDLLEXPORT wxAtoi(const wxChar *psz)
2085 {
2086 return atoi(wxConvLibc.cWX2MB(psz));
2087 }
2088
2089 long WXDLLEXPORT wxAtol(const wxChar *psz)
2090 {
2091 return atol(wxConvLibc.cWX2MB(psz));
2092 }
2093
2094 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
2095 {
2096 #if wxUSE_UNICODE
2097 // NB: buffer returned by getenv() is allowed to be overwritten next
2098 // time getenv() is called, so it is OK to use static string
2099 // buffer to hold the data.
2100 static wxWCharBuffer value((wxChar*)NULL);
2101 value = wxConvLibc.cMB2WX(getenv(wxConvLibc.cWX2MB(name)));
2102 return value.data();
2103 #else
2104 return getenv(name);
2105 #endif
2106 }
2107
2108 int WXDLLEXPORT wxSystem(const wxChar *psz)
2109 {
2110 return system(wxConvLibc.cWX2MB(psz));
2111 }
2112
2113 #endif // wxNEED_WX_STDLIB_H
2114
2115 #ifdef wxNEED_WX_TIME_H
2116 WXDLLEXPORT size_t
2117 wxStrftime(wxChar *s, size_t maxsize, const wxChar *fmt, const struct tm *tm)
2118 {
2119 if ( !maxsize )
2120 return 0;
2121
2122 wxCharBuffer buf(maxsize);
2123
2124 wxCharBuffer bufFmt(wxConvLibc.cWX2MB(fmt));
2125 if ( !bufFmt )
2126 return 0;
2127
2128 size_t ret = strftime(buf.data(), maxsize, bufFmt, tm);
2129 if ( !ret )
2130 return 0;
2131
2132 wxWCharBuffer wbuf = wxConvLibc.cMB2WX(buf);
2133 if ( !wbuf )
2134 return 0;
2135
2136 wxStrncpy(s, wbuf, maxsize);
2137 return wxStrlen(s);
2138 }
2139 #endif // wxNEED_WX_TIME_H
2140
2141 #ifndef wxCtime
2142 WXDLLEXPORT wxChar *wxCtime(const time_t *timep)
2143 {
2144 // normally the string is 26 chars but give one more in case some broken
2145 // DOS compiler decides to use "\r\n" instead of "\n" at the end
2146 static wxChar buf[27];
2147
2148 // ctime() is guaranteed to return a string containing only ASCII
2149 // characters, as its format is always the same for any locale
2150 wxStrncpy(buf, wxString::FromAscii(ctime(timep)), WXSIZEOF(buf));
2151 buf[WXSIZEOF(buf) - 1] = _T('\0');
2152
2153 return buf;
2154 }
2155 #endif // wxCtime
2156
2157 #endif // wxUSE_WCHAR_T
2158
2159 // ----------------------------------------------------------------------------
2160 // functions which we may need even if !wxUSE_WCHAR_T
2161 // ----------------------------------------------------------------------------
2162
2163 #ifndef wxStrtok
2164
2165 WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
2166 {
2167 if (!psz)
2168 {
2169 psz = *save_ptr;
2170 if ( !psz )
2171 return NULL;
2172 }
2173
2174 psz += wxStrspn(psz, delim);
2175 if (!*psz)
2176 {
2177 *save_ptr = (wxChar *)NULL;
2178 return (wxChar *)NULL;
2179 }
2180
2181 wxChar *ret = psz;
2182 psz = wxStrpbrk(psz, delim);
2183 if (!psz)
2184 {
2185 *save_ptr = (wxChar*)NULL;
2186 }
2187 else
2188 {
2189 *psz = wxT('\0');
2190 *save_ptr = psz + 1;
2191 }
2192
2193 return ret;
2194 }
2195
2196 #endif // wxStrtok
2197
2198 // ----------------------------------------------------------------------------
2199 // missing C RTL functions
2200 // ----------------------------------------------------------------------------
2201
2202 #ifdef wxNEED_STRDUP
2203
2204 char *strdup(const char *s)
2205 {
2206 char *dest = (char*) malloc( strlen( s ) + 1 ) ;
2207 if ( dest )
2208 strcpy( dest , s ) ;
2209 return dest ;
2210 }
2211 #endif // wxNEED_STRDUP
2212
2213 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
2214
2215 void *calloc( size_t num, size_t size )
2216 {
2217 void** ptr = (void **)malloc(num * size);
2218 memset( ptr, 0, num * size);
2219 return ptr;
2220 }
2221
2222 #endif // __WXWINCE__ <= 211
2223
2224 #ifdef __WXWINCE__
2225
2226 int wxRemove(const wxChar *path)
2227 {
2228 return ::DeleteFile(path) == 0;
2229 }
2230
2231 #endif