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