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