]> git.saurik.com Git - wxWidgets.git/blame - src/common/wxchar.cpp
Implemented wxTextAttrEx::CombineEx and wxRichTextAttr::Combine
[wxWidgets.git] / src / common / wxchar.cpp
CommitLineData
c9e089e9 1/////////////////////////////////////////////////////////////////////////////
41e155b4 2// Name: src/common/wxchar.cpp
c9e089e9 3// Purpose: wxChar implementation
247c23b4
VZ
4// Author: Ove Kaven
5// Modified by: Ron Lee, Francesco Montorsi
c9e089e9
OK
6// Created: 09/04/99
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets copyright
65571936 9// Licence: wxWindows licence
c9e089e9
OK
10/////////////////////////////////////////////////////////////////////////////
11
c9e089e9
OK
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__
8898456d 20 #pragma hdrstop
c9e089e9
OK
21#endif
22
4400c1a4
OK
23#define _ISOC9X_SOURCE 1 // to get vsscanf()
24#define _BSD_SOURCE 1 // to still get strdup()
25
84fff0b3 26#include <stdio.h>
c9e089e9
OK
27#include <stdlib.h>
28#include <string.h>
1c193821
JS
29
30#ifndef __WXWINCE__
8898456d
WS
31 #include <time.h>
32 #include <locale.h>
1c193821 33#else
8898456d 34 #include "wx/msw/wince/time.h"
1c193821 35#endif
c9e089e9
OK
36
37#ifndef WX_PRECOMP
8898456d
WS
38 #include "wx/wxchar.h"
39 #include "wx/string.h"
40 #include "wx/hash.h"
c9e089e9 41#endif
7a828c7f 42 #include "wx/utils.h" // for wxMin and wxMax
c9e089e9 43
57f6da0d 44#if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
b3e8d00a 45 #include <windef.h>
8898456d
WS
46 #include <winbase.h>
47 #include <winnls.h>
48 #include <winnt.h>
57f6da0d
OK
49#endif
50
31907d03 51#if defined(__MWERKS__) && __MSL__ >= 0x6000
52cbcda3 52namespace std {}
31907d03
SC
53using namespace std ;
54#endif
55
d0bdc3ca 56#if wxUSE_WCHAR_T
434d2cb3 57size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n)
c9e089e9 58{
2b5f62a0 59 // assume that we have mbsrtowcs() too if we have wcsrtombs()
fe190003 60#ifdef HAVE_WCSRTOMBS
2b5f62a0
VZ
61 mbstate_t mbstate;
62 memset(&mbstate, 0, sizeof(mbstate_t));
63#endif
64
c9e089e9 65 if (buf) {
923d3156 66 if (!n || !*psz) {
223d09f6 67 if (n) *buf = wxT('\0');
923d3156
OK
68 return 0;
69 }
2b5f62a0
VZ
70#ifdef HAVE_WCSRTOMBS
71 return mbsrtowcs(buf, &psz, n, &mbstate);
72#else
1e96e503 73 return wxMbstowcs(buf, psz, n);
2b5f62a0 74#endif
c9e089e9
OK
75 }
76
0d8c57c0
VZ
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
b3e8d00a 83#ifdef HAVE_WCSRTOMBS
c9e089e9 84 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
2b5f62a0 85#else
1e96e503 86 return wxMbstowcs((wchar_t *) NULL, psz, 0);
2b5f62a0 87#endif
c9e089e9
OK
88}
89
434d2cb3 90size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
c9e089e9 91{
fe190003 92#ifdef HAVE_WCSRTOMBS
2b5f62a0
VZ
93 mbstate_t mbstate;
94 memset(&mbstate, 0, sizeof(mbstate_t));
95#endif
96
c9e089e9 97 if (buf) {
923d3156
OK
98 if (!n || !*pwz) {
99 // glibc2.1 chokes on null input
100 if (n) *buf = '\0';
101 return 0;
102 }
fe190003 103#ifdef HAVE_WCSRTOMBS
2b5f62a0
VZ
104 return wcsrtombs(buf, &pwz, n, &mbstate);
105#else
1e96e503 106 return wxWcstombs(buf, pwz, n);
2b5f62a0 107#endif
c9e089e9
OK
108 }
109
fe190003 110#ifdef HAVE_WCSRTOMBS
c9e089e9 111 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
2b5f62a0 112#else
1e96e503 113 return wxWcstombs((char *) NULL, pwz, 0);
2b5f62a0 114#endif
c9e089e9 115}
b3e8d00a 116#endif // wxUSE_WCHAR_T
c9e089e9 117
434d2cb3
OK
118bool WXDLLEXPORT wxOKlibc()
119{
5283098e 120#if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
66b3ec7f
OK
121 // glibc 2.0 uses UTF-8 even when it shouldn't
122 wchar_t res = 0;
434d2cb3 123 if ((MB_CUR_MAX == 2) &&
66b3ec7f 124 (wxMB2WC(&res, "\xdd\xa5", 1) == 1) &&
434d2cb3
OK
125 (res==0x765)) {
126 // this is UTF-8 allright, check whether that's what we want
66b3ec7f 127 char *cur_locale = setlocale(LC_CTYPE, NULL);
434d2cb3 128 if ((strlen(cur_locale) < 4) ||
f6f5941b
VZ
129 (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) ||
130 (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) {
434d2cb3 131 // nope, don't use libc conversion
cab1a605 132 return false;
434d2cb3
OK
133 }
134 }
135#endif
cab1a605 136 return true;
434d2cb3
OK
137}
138
f6f5941b
VZ
139// ============================================================================
140// printf() functions business
141// ============================================================================
142
df17b887
VZ
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
f6f5941b
VZ
160// ----------------------------------------------------------------------------
161// implement [v]snprintf() if the system doesn't provide a safe one
7a828c7f
VZ
162// or if the system's one does not support positional parameters
163// (very useful for i18n purposes)
f6f5941b
VZ
164// ----------------------------------------------------------------------------
165
166#if !defined(wxVsnprintf_)
7a828c7f
VZ
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_
190enum 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_
217typedef 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...
246class wxPrintfConvSpec
f6f5941b 247{
7a828c7f 248public:
f6f5941b 249
7a828c7f
VZ
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;
5f1d3069 260
7a828c7f
VZ
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
280public:
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
300private:
301 // An helper function of LoadArg() which is used to handle the '*' flag
302 void ReplaceAsteriskWith(int w);
303};
304
305void 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
316bool 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
f6f5941b 328 {
7a828c7f
VZ
329#define CHECK_PREC \
330 if (in_prec && !prec_dot) \
331 { \
332 szFlags[flagofs++] = '.'; \
333 prec_dot = true; \
334 }
df17b887 335
7a828c7f
VZ
336 // what follows '%'?
337 const wxChar ch = *(++argend);
338 switch ( ch )
df17b887 339 {
7a828c7f
VZ
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
f6f5941b 399
7a828c7f
VZ
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
f6f5941b 436 }
7a828c7f
VZ
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
581void 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
603bool 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
686int 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;
f6f5941b
VZ
691
692#define APPEND_CH(ch) \
210f4bcd
VS
693 { \
694 if ( lenCur == lenMax ) \
695 return -1; \
696 \
697 buf[lenCur++] = ch; \
698 }
f6f5941b
VZ
699
700#define APPEND_STR(s) \
f6f5941b 701 { \
1af48460
VZ
702 for ( const wxChar *p = s; *p; p++ ) \
703 { \
704 APPEND_CH(*p); \
705 } \
f6f5941b
VZ
706 }
707
7a828c7f
VZ
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
f6f5941b 718#if SIZEOF_LONG_LONG
7a828c7f
VZ
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 =
210f4bcd 744#if wxUSE_UNICODE
7a828c7f
VZ
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
34deaa93 755#if wxUSE_WCHAR_T
7a828c7f
VZ
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 }
34deaa93 761#endif
210f4bcd 762#endif
210f4bcd 763
7a828c7f 764 size_t i;
210f4bcd 765
7a828c7f
VZ
766 if (!adj_left)
767 for (i = 1; i < (size_t)min_width; i++)
768 APPEND_CH(_T(' '));
f6f5941b 769
7a828c7f 770 APPEND_CH(val);
210f4bcd 771
7a828c7f
VZ
772 if (adj_left)
773 for (i = 1; i < (size_t)min_width; i++)
774 APPEND_CH(_T(' '));
775 }
776 break;
f6f5941b 777
7a828c7f
VZ
778 case wxPAT_PCHAR:
779 case wxPAT_PWCHAR:
780 {
781 wxString s;
782 const wxChar *val =
f6f5941b 783#if wxUSE_UNICODE
7a828c7f
VZ
784 p->pad_pwchar;
785
786 if (type == wxPAT_PCHAR) {
787 // user passed a string explicitely indicated as ANSI...
a31746c7 788 val = s = wxString(p->pad_pchar, wxConvLibc);
7a828c7f
VZ
789 }
790#else
791 p->pad_pchar;
792
34deaa93 793#if wxUSE_WCHAR_T
7a828c7f
VZ
794 if (type == wxPAT_PWCHAR) {
795 // user passed a string explicitely indicated as Unicode...
a31746c7 796 val = s = wxString(p->pad_pwchar, wxConvLibc);
7a828c7f 797 }
34deaa93 798#endif
7a828c7f
VZ
799#endif
800 int len;
801
802 if (val)
803 {
804#if wxUSE_STRUTILS
a31746c7
VZ
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));
7a828c7f
VZ
808#else
809 for ( len = 0; val[len] && (len < max_width); len++ )
810 ;
f6f5941b 811#endif
7a828c7f
VZ
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
a31746c7
VZ
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);
7a828c7f
VZ
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(' '));
f6f5941b 847 }
df17b887 848 }
7a828c7f
VZ
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
247c23b4
VZ
904// differences from standard strncpy:
905// 1) copies everything from 'source' except for '%%' sequence which is copied as '%'
906// 2) returns the number of written characters in 'dest' as it could differ from given 'n'
907// 3) much less optimized, unfortunately...
908static int wxCopyStrWithPercents(wxChar *dest, const wxChar *source, size_t n)
909{
910 size_t written = 0;
911
912 if (n == 0)
913 return 0;
914
915 size_t i;
916 for ( i = 0; i < n-1; source++, i++)
917 {
918 dest[written++] = *source;
919 if (*(source+1) == wxT('%'))
920 {
921 // skip this additional '%' character
922 source++;
923 i++;
924 }
925 }
926
927 if (i < n)
928 // copy last character inconditionally
929 dest[written++] = *source;
930
931 return written;
932}
933
7a828c7f
VZ
934int WXDLLEXPORT wxVsnprintf_(wxChar *buf, size_t lenMax,
935 const wxChar *format, va_list argptr)
936{
937 // cached data
938 static wxPrintfConvSpec arg[wxMAX_SVNPRINTF_ARGUMENTS];
939 static wxPrintfArg argdata[wxMAX_SVNPRINTF_ARGUMENTS];
940 static wxPrintfConvSpec *pspec[wxMAX_SVNPRINTF_ARGUMENTS] = { NULL };
941
942 size_t i;
943
944 // number of characters in the buffer so far, must be less than lenMax
945 size_t lenCur = 0;
946
947 size_t nargs = 0;
948 const wxChar *toparse = format;
949
950 // parse the format string
951 bool posarg_present = false, nonposarg_present = false;
952 for (; *toparse != wxT('\0'); toparse++)
953 {
954 if (*toparse == wxT('%') )
f6f5941b 955 {
7a828c7f
VZ
956 arg[nargs].Init();
957
958 // let's see if this is a (valid) conversion specifier...
959 if (arg[nargs].Parse(toparse))
960 {
961 // ...yes it is
962 wxPrintfConvSpec *current = &arg[nargs];
963
964 // make toparse point to the end of this specifier
965 toparse = current->argend;
966
967 if (current->pos > 0) {
968 // the positionals start from number 1... adjust the index
969 current->pos--;
970 posarg_present = true;
971 } else {
972 // not a positional argument...
973 current->pos = nargs;
974 nonposarg_present = true;
975 }
976
977 // this conversion specifier is tied to the pos-th argument...
978 pspec[current->pos] = current;
979 nargs++;
980
981 if (nargs == wxMAX_SVNPRINTF_ARGUMENTS)
982 break; // cannot handle any additional conv spec
983 }
f6f5941b 984 }
7a828c7f 985 }
df17b887 986
7a828c7f
VZ
987 if (posarg_present && nonposarg_present)
988 return -1; // format strings with both positional and
989 // non-positional conversion specifier are unsupported !!
990
3aa077ce
MW
991 // on platforms where va_list is an array type, it is necessary to make a
992 // copy to be able to pass it to LoadArg as a reference.
993 bool ok = true;
994 va_list ap;
995 wxVaCopy(ap, argptr);
996
7a828c7f 997 // now load arguments from stack
3aa077ce
MW
998 for (i=0; i < nargs && ok; i++) {
999 // !pspec[i] if user forgot a positional parameter (e.g. %$1s %$3s) ?
1000 // or LoadArg false if wxPrintfConvSpec::Parse failed to set its 'type'
1001 // to a valid value...
1002 ok = pspec[i] && pspec[i]->LoadArg(&argdata[i], ap);
7a828c7f
VZ
1003 }
1004
3aa077ce
MW
1005 va_end(ap);
1006
247c23b4 1007 // something failed while loading arguments from the variable list...
3aa077ce
MW
1008 if (!ok)
1009 return -1;
1010
7a828c7f
VZ
1011 // finally, process each conversion specifier with its own argument
1012 toparse = format;
1013 for (i=0; i < nargs; i++)
1014 {
1015 // copy in the output buffer the portion of the format string between
1016 // last specifier and the current one
1017 size_t tocopy = ( arg[i].argpos - toparse );
1018 if (lenCur+tocopy >= lenMax)
1019 return -1; // not enough space in the output buffer !
1020
247c23b4 1021 lenCur += wxCopyStrWithPercents(buf+lenCur, toparse, tocopy);
7a828c7f
VZ
1022
1023 // process this specifier directly in the output buffer
1024 int n = arg[i].Process(buf+lenCur, lenMax - lenCur, &argdata[arg[i].pos]);
1025 if (n == -1)
1026 return -1; // not enough space in the output buffer !
1027 lenCur += n;
1028
1029 // the +1 is because wxPrintfConvSpec::argend points to the last character
1030 // of the format specifier, but we are not interested to it...
1031 toparse = arg[i].argend + 1;
5f1d3069
RR
1032 }
1033
7a828c7f
VZ
1034 // copy portion of the format string after last specifier
1035 // NOTE: toparse is pointing to the character just after the last processed
1036 // conversion specifier
1037 // NOTE2: the +1 is because we want to copy also the '\0'
1038 size_t tocopy = wxStrlen(format) + 1 - ( toparse - format ) ;
1039 if (lenCur+tocopy >= lenMax)
1040 return -1; // not enough space in the output buffer !
247c23b4
VZ
1041
1042 // the -1 is because of the '\0'
1043 lenCur += wxCopyStrWithPercents(buf+lenCur, toparse, tocopy) - 1;
7a828c7f
VZ
1044
1045 // clean the static array portion used...
1046 // NOTE: other arrays do not need cleanup!
1047 memset(pspec, 0, sizeof(wxPrintfConvSpec*)*nargs);
1048
1049 wxASSERT(lenCur == wxStrlen(buf));
f6f5941b
VZ
1050 return lenCur;
1051}
5f1d3069 1052
f6f5941b
VZ
1053#undef APPEND_CH
1054#undef APPEND_STR
1055#undef CHECK_PREC
5f1d3069 1056
f6f5941b
VZ
1057#endif // !wxVsnprintfA
1058
1059#if !defined(wxSnprintf_)
1060int WXDLLEXPORT wxSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...)
1061{
5f1d3069
RR
1062 va_list argptr;
1063 va_start(argptr, format);
1064
f6f5941b 1065 int iLen = wxVsnprintf_(buf, len, format, argptr);
5f1d3069
RR
1066
1067 va_end(argptr);
f6f5941b
VZ
1068
1069 return iLen;
5f1d3069 1070}
f6f5941b 1071#endif // wxSnprintf_
5f1d3069 1072
8dfb846e
CE
1073#if defined(__DMC__)
1074 /* Digital Mars adds count to _stprintf (C99) so convert */
1075 #if wxUSE_UNICODE
1076 int wxSprintf (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... )
1077 {
1078 va_list arglist;
1079
1080 va_start( arglist, format );
1081 int iLen = swprintf ( s, -1, format, arglist );
1082 va_end( arglist );
1083 return iLen ;
1084 }
1085
1086 #endif // wxUSE_UNICODE
1087
1088#endif //__DMC__
1089
f6f5941b
VZ
1090// ----------------------------------------------------------------------------
1091// implement the standard IO functions for wide char if libc doesn't have them
1092// ----------------------------------------------------------------------------
5f1d3069 1093
fbe47c7b 1094#ifdef wxNEED_FPUTS
f6f5941b
VZ
1095int wxFputs(const wchar_t *ws, FILE *stream)
1096{
1097 // counting the number of wide characters written isn't worth the trouble,
1098 // simply distinguish between ok and error
1099 return fputs(wxConvLibc.cWC2MB(ws), stream) == -1 ? -1 : 0;
1100}
fbe47c7b 1101#endif // wxNEED_FPUTS
5f1d3069 1102
19b65a30
VZ
1103#ifdef wxNEED_PUTS
1104int wxPuts(const wxChar *ws)
1105{
1106 int rc = wxFputs(ws, stdout);
1107 if ( rc != -1 )
1108 {
1109 if ( wxFputs(L"\n", stdout) == -1 )
1110 return -1;
1111
1112 rc++;
1113 }
1114
1115 return rc;
1116}
1117#endif // wxNEED_PUTS
1118
fbe47c7b 1119#ifdef wxNEED_PUTC
f6f5941b
VZ
1120int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream)
1121{
1122 wchar_t ws[2] = { wc, L'\0' };
5f1d3069 1123
f6f5941b 1124 return wxFputs(ws, stream);
5f1d3069 1125}
fbe47c7b 1126#endif // wxNEED_PUTC
f6f5941b
VZ
1127
1128// NB: we only implement va_list functions here, the ones taking ... are
1129// defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
1130// the definitions there to avoid duplicating them here
1131#ifdef wxNEED_WPRINTF
1132
1133// TODO: implement the scanf() functions
df17b887 1134int vwscanf(const wxChar *format, va_list argptr)
5f1d3069 1135{
f6f5941b 1136 wxFAIL_MSG( _T("TODO") );
5f1d3069 1137
f6f5941b
VZ
1138 return -1;
1139}
1140
df17b887 1141int vswscanf(const wxChar *ws, const wxChar *format, va_list argptr)
f6f5941b 1142{
63de666d
VS
1143 // The best we can do without proper Unicode support in glibc is to
1144 // convert the strings into MB representation and run ANSI version
1145 // of the function. This doesn't work with %c and %s because of difference
1146 // in size of char and wchar_t, though.
525d8583 1147
63de666d
VS
1148 wxCHECK_MSG( wxStrstr(format, _T("%s")) == NULL, -1,
1149 _T("incomplete vswscanf implementation doesn't allow %s") );
1150 wxCHECK_MSG( wxStrstr(format, _T("%c")) == NULL, -1,
1151 _T("incomplete vswscanf implementation doesn't allow %c") );
525d8583 1152
63de666d
VS
1153 va_list argcopy;
1154 wxVaCopy(argcopy, argptr);
1155 return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argcopy);
f6f5941b 1156}
5f1d3069 1157
df17b887 1158int vfwscanf(FILE *stream, const wxChar *format, va_list argptr)
f6f5941b
VZ
1159{
1160 wxFAIL_MSG( _T("TODO") );
5f1d3069 1161
f6f5941b 1162 return -1;
5f1d3069
RR
1163}
1164
f6f5941b
VZ
1165#define vswprintf wxVsnprintf_
1166
df17b887 1167int vfwprintf(FILE *stream, const wxChar *format, va_list argptr)
5f1d3069 1168{
f6f5941b
VZ
1169 wxString s;
1170 int rc = s.PrintfV(format, argptr);
5f1d3069 1171
f6f5941b
VZ
1172 if ( rc != -1 )
1173 {
1174 // we can't do much better without Unicode support in libc...
50b079e5 1175 if ( fprintf(stream, "%s", (const char*)s.mb_str() ) == -1 )
f6f5941b
VZ
1176 return -1;
1177 }
5f1d3069 1178
f6f5941b 1179 return rc;
5f1d3069
RR
1180}
1181
df17b887 1182int vwprintf(const wxChar *format, va_list argptr)
5f1d3069 1183{
f6f5941b
VZ
1184 return wxVfprintf(stdout, format, argptr);
1185}
5f1d3069 1186
f6f5941b 1187#endif // wxNEED_WPRINTF
5f1d3069 1188
f6f5941b 1189#ifdef wxNEED_PRINTF_CONVERSION
5f1d3069 1190
f6f5941b
VZ
1191// ----------------------------------------------------------------------------
1192// wxFormatConverter: class doing the "%s" -> "%ls" conversion
1193// ----------------------------------------------------------------------------
5f1d3069 1194
f6f5941b
VZ
1195/*
1196 Here are the gory details. We want to follow the Windows/MS conventions,
1197 that is to have
1198
1199 In ANSI mode:
1200
1201 format specifier results in
1202 -----------------------------------
1203 %c, %hc, %hC char
1204 %lc, %C, %lC wchar_t
1205
1206 In Unicode mode:
1207
1208 format specifier results in
1209 -----------------------------------
1210 %hc, %C, %hC char
1211 %c, %lc, %lC wchar_t
1212
1213
1214 while on POSIX systems we have %C identical to %lc and %c always means char
1215 (in any mode) while %lc always means wchar_t,
1216
1217 So to use native functions in order to get our semantics we must do the
1218 following translations in Unicode mode (nothing to do in ANSI mode):
1219
77ffb593 1220 wxWidgets specifier POSIX specifier
f6f5941b
VZ
1221 ----------------------------------------
1222
1223 %hc, %C, %hC %c
1224 %c %lc
1225
1226
1227 And, of course, the same should be done for %s as well.
1228*/
1229
1230class wxFormatConverter
5f1d3069 1231{
f6f5941b
VZ
1232public:
1233 wxFormatConverter(const wxChar *format);
1234
c03d0035
VZ
1235 // notice that we only translated the string if m_fmtOrig == NULL (as set
1236 // by CopyAllBefore()), otherwise we should simply use the original format
1237 operator const wxChar *() const
1238 { return m_fmtOrig ? m_fmtOrig : m_fmt.c_str(); }
f6f5941b
VZ
1239
1240private:
1241 // copy another character to the translated format: this function does the
1242 // copy if we are translating but doesn't do anything at all if we don't,
1243 // so we don't create the translated format string at all unless we really
1244 // need to (i.e. InsertFmtChar() is called)
1245 wxChar CopyFmtChar(wxChar ch)
1246 {
1247 if ( !m_fmtOrig )
1248 {
c03d0035 1249 // we're translating, do copy
f6f5941b
VZ
1250 m_fmt += ch;
1251 }
1252 else
1253 {
1254 // simply increase the count which should be copied by
1255 // CopyAllBefore() later if needed
1256 m_nCopied++;
1257 }
1258
1259 return ch;
1260 }
1261
1262 // insert an extra character
1263 void InsertFmtChar(wxChar ch)
1264 {
1265 if ( m_fmtOrig )
1266 {
1267 // so far we haven't translated anything yet
1268 CopyAllBefore();
1269 }
1270
1271 m_fmt += ch;
1272 }
1273
1274 void CopyAllBefore()
1275 {
1276 wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") );
1277
1278 m_fmt = wxString(m_fmtOrig, m_nCopied);
1279
1280 // we won't need it any longer
1281 m_fmtOrig = NULL;
1282 }
5f1d3069 1283
f6f5941b
VZ
1284 static bool IsFlagChar(wxChar ch)
1285 {
1286 return ch == _T('-') || ch == _T('+') ||
1287 ch == _T('0') || ch == _T(' ') || ch == _T('#');
1288 }
1289
41d9940d 1290 void SkipDigits(const wxChar **ptpc)
f6f5941b 1291 {
41d9940d
SC
1292 while ( **ptpc >= _T('0') && **ptpc <= _T('9') )
1293 CopyFmtChar(*(*ptpc)++);
f6f5941b
VZ
1294 }
1295
1296 // the translated format
1297 wxString m_fmt;
1298
1299 // the original format
1300 const wxChar *m_fmtOrig;
1301
1302 // the number of characters already copied
1303 size_t m_nCopied;
1304};
1305
1306wxFormatConverter::wxFormatConverter(const wxChar *format)
1307{
1308 m_fmtOrig = format;
1309 m_nCopied = 0;
1310
1311 while ( *format )
1312 {
1313 if ( CopyFmtChar(*format++) == _T('%') )
1314 {
1315 // skip any flags
1316 while ( IsFlagChar(*format) )
1317 CopyFmtChar(*format++);
1318
1319 // and possible width
1320 if ( *format == _T('*') )
1321 CopyFmtChar(*format++);
1322 else
1323 SkipDigits(&format);
1324
1325 // precision?
1326 if ( *format == _T('.') )
1327 {
cfee166d
JS
1328 CopyFmtChar(*format++);
1329 if ( *format == _T('*') )
1330 CopyFmtChar(*format++);
1331 else
1332 SkipDigits(&format);
f6f5941b
VZ
1333 }
1334
1335 // next we can have a size modifier
1336 enum
1337 {
1338 Default,
1339 Short,
1340 Long
1341 } size;
1342
1343 switch ( *format )
1344 {
1345 case _T('h'):
1346 size = Short;
1347 format++;
1348 break;
1349
1350 case _T('l'):
1351 // "ll" has a different meaning!
1352 if ( format[1] != _T('l') )
1353 {
1354 size = Long;
1355 format++;
1356 break;
1357 }
1358 //else: fall through
1359
1360 default:
1361 size = Default;
1362 }
1363
1364 // and finally we should have the type
1365 switch ( *format )
1366 {
1367 case _T('C'):
1368 case _T('S'):
1369 // %C and %hC -> %c and %lC -> %lc
1370 if ( size == Long )
1371 CopyFmtChar(_T('l'));
1372
1373 InsertFmtChar(*format++ == _T('C') ? _T('c') : _T('s'));
1374 break;
1375
1376 case _T('c'):
1377 case _T('s'):
1378 // %c -> %lc but %hc stays %hc and %lc is still %lc
cfee166d
JS
1379 if ( size == Default)
1380 InsertFmtChar(_T('l'));
f6f5941b
VZ
1381 // fall through
1382
1383 default:
1384 // nothing special to do
cfee166d
JS
1385 if ( size != Default )
1386 CopyFmtChar(*(format - 1));
f6f5941b
VZ
1387 CopyFmtChar(*format++);
1388 }
1389 }
1390 }
1391}
1392
1393#else // !wxNEED_PRINTF_CONVERSION
1394 // no conversion necessary
1395 #define wxFormatConverter(x) (x)
1396#endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
1397
cfee166d
JS
1398#ifdef __WXDEBUG__
1399// For testing the format converter
1400wxString wxConvertFormat(const wxChar *format)
1401{
1402 return wxString(wxFormatConverter(format));
1403}
1404#endif
1405
f6f5941b
VZ
1406// ----------------------------------------------------------------------------
1407// wxPrintf(), wxScanf() and relatives
1408// ----------------------------------------------------------------------------
1409
1410#if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
1411
df17b887 1412int wxScanf( const wxChar *format, ... )
f6f5941b 1413{
5f1d3069
RR
1414 va_list argptr;
1415 va_start(argptr, format);
1416
f6f5941b 1417 int ret = vwscanf(wxFormatConverter(format), argptr );
5f1d3069
RR
1418
1419 va_end(argptr);
1420
1421 return ret;
1422}
1423
df17b887 1424int wxSscanf( const wxChar *str, const wxChar *format, ... )
5f1d3069 1425{
5f1d3069
RR
1426 va_list argptr;
1427 va_start(argptr, format);
1428
f6f5941b 1429 int ret = vswscanf( str, wxFormatConverter(format), argptr );
5f1d3069
RR
1430
1431 va_end(argptr);
1432
1433 return ret;
1434}
1435
df17b887 1436int wxFscanf( FILE *stream, const wxChar *format, ... )
5f1d3069 1437{
5f1d3069 1438 va_list argptr;
f6f5941b 1439 va_start(argptr, format);
f6f5941b 1440 int ret = vfwscanf(stream, wxFormatConverter(format), argptr);
5f1d3069
RR
1441
1442 va_end(argptr);
1443
1444 return ret;
1445}
1446
df17b887 1447int wxPrintf( const wxChar *format, ... )
5f1d3069 1448{
f6f5941b
VZ
1449 va_list argptr;
1450 va_start(argptr, format);
df17b887 1451
f6f5941b 1452 int ret = vwprintf( wxFormatConverter(format), argptr );
5f1d3069 1453
f6f5941b 1454 va_end(argptr);
5f1d3069
RR
1455
1456 return ret;
1457}
1458
f6f5941b 1459#ifndef wxSnprintf
df17b887 1460int wxSnprintf( wxChar *str, size_t size, const wxChar *format, ... )
5f1d3069 1461{
f6f5941b
VZ
1462 va_list argptr;
1463 va_start(argptr, format);
5f1d3069 1464
f6f5941b 1465 int ret = vswprintf( str, size, wxFormatConverter(format), argptr );
5f1d3069 1466
f6f5941b 1467 va_end(argptr);
5f1d3069
RR
1468
1469 return ret;
1470}
f6f5941b 1471#endif // wxSnprintf
5f1d3069 1472
df17b887 1473int wxSprintf( wxChar *str, const wxChar *format, ... )
5f1d3069 1474{
f6f5941b
VZ
1475 va_list argptr;
1476 va_start(argptr, format);
5f1d3069 1477
56413ebf 1478 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
f8991003 1479 // it's safe to implement this one in terms of it
56413ebf 1480 wxString s(wxString::FormatV(format, argptr));
a4e0917b 1481 wxStrcpy(str, s);
5f1d3069 1482
f6f5941b 1483 va_end(argptr);
5f1d3069 1484
a4e0917b 1485 return s.length();
5f1d3069
RR
1486}
1487
df17b887 1488int wxFprintf( FILE *stream, const wxChar *format, ... )
5f1d3069 1489{
f6f5941b
VZ
1490 va_list argptr;
1491 va_start( argptr, format );
5f1d3069 1492
f6f5941b 1493 int ret = vfwprintf( stream, wxFormatConverter(format), argptr );
5f1d3069 1494
f6f5941b 1495 va_end(argptr);
5f1d3069
RR
1496
1497 return ret;
1498}
f06e3075 1499
f6f5941b 1500int wxVsscanf( const wxChar *str, const wxChar *format, va_list argptr )
5f1d3069 1501{
f6f5941b
VZ
1502 return vswscanf( str, wxFormatConverter(format), argptr );
1503}
5f1d3069 1504
f6f5941b
VZ
1505int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr )
1506{
1507 return vfwprintf( stream, wxFormatConverter(format), argptr );
1508}
5f1d3069 1509
f6f5941b
VZ
1510int wxVprintf( const wxChar *format, va_list argptr )
1511{
1512 return vwprintf( wxFormatConverter(format), argptr );
5f1d3069 1513}
5f1d3069 1514
f6f5941b
VZ
1515#ifndef wxVsnprintf
1516int wxVsnprintf( wxChar *str, size_t size, const wxChar *format, va_list argptr )
5f1d3069 1517{
f6f5941b
VZ
1518 return vswprintf( str, size, wxFormatConverter(format), argptr );
1519}
1520#endif // wxVsnprintf
5f1d3069 1521
f6f5941b
VZ
1522int wxVsprintf( wxChar *str, const wxChar *format, va_list argptr )
1523{
1524 // same as for wxSprintf()
b63b07a8 1525 return vswprintf(str, INT_MAX / 4, wxFormatConverter(format), argptr);
f6f5941b 1526}
5f1d3069 1527
f6f5941b 1528#endif // wxNEED_PRINTF_CONVERSION
5f1d3069 1529
33a7c3dd
VZ
1530#if wxUSE_WCHAR_T
1531
f6f5941b
VZ
1532// ----------------------------------------------------------------------------
1533// ctype.h stuff (currently unused)
1534// ----------------------------------------------------------------------------
5f1d3069 1535
57f6da0d
OK
1536#if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
1537inline WORD wxMSW_ctype(wxChar ch)
1538{
1539 WORD ret;
1540 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
1541 return ret;
1542}
1543
b0655a87
OK
1544WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); }
1545WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); }
4eb7c4b1 1546WXDLLEXPORT int wxIscntrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; }
b0655a87
OK
1547WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; }
1548WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); }
1549WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); }
1550WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); }
1551WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; }
1552WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; }
1553WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); }
1554WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; }
1555WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
1556WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
57f6da0d
OK
1557#endif
1558
dbf9aa46 1559#ifdef wxNEED_WX_MBSTOWCS
dcb68102 1560
1e96e503 1561WXDLLEXPORT size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen)
dcb68102
RN
1562{
1563 if (!out)
1564 {
1565 size_t outsize = 0;
1566 while(*in++)
1567 outsize++;
1568 return outsize;
1569 }
525d8583 1570
dcb68102 1571 const char* origin = in;
525d8583 1572
dcb68102
RN
1573 while (outlen-- && *in)
1574 {
1575 *out++ = (wchar_t) *in++;
1576 }
525d8583 1577
dcb68102 1578 *out = '\0';
525d8583 1579
dcb68102
RN
1580 return in - origin;
1581}
1582
41e155b4 1583WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen)
dcb68102
RN
1584{
1585 if (!out)
1586 {
1587 size_t outsize = 0;
1588 while(*in++)
1589 outsize++;
1590 return outsize;
1591 }
525d8583 1592
dcb68102 1593 const wchar_t* origin = in;
525d8583 1594
dcb68102
RN
1595 while (outlen-- && *in)
1596 {
1597 *out++ = (char) *in++;
1598 }
525d8583 1599
dcb68102 1600 *out = '\0';
525d8583 1601
dcb68102
RN
1602 return in - origin;
1603}
525d8583 1604
dbf9aa46
VZ
1605#endif // wxNEED_WX_MBSTOWCS
1606
dcb68102
RN
1607#if defined(wxNEED_WX_CTYPE_H)
1608
1609#include <CoreFoundation/CoreFoundation.h>
1610
eaceebf6
RN
1611#define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
1612#define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
1613#define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
1614#define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
dcb68102 1615//CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
eaceebf6 1616#define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
dcb68102 1617//CFCharacterSetRef cfprintset = !kCFCharacterSetControl
eaceebf6
RN
1618#define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
1619#define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
1620#define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
dcb68102
RN
1621
1622WXDLLEXPORT int wxIsalnum(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalnumset, ch); }
1623WXDLLEXPORT int wxIsalpha(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalphaset, ch); }
1624WXDLLEXPORT int wxIscntrl(wxChar ch) { return CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
1625WXDLLEXPORT int wxIsdigit(wxChar ch) { return CFCharacterSetIsCharacterMember(cfdigitset, ch); }
1626WXDLLEXPORT int wxIsgraph(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch) && ch != ' '; }
1627WXDLLEXPORT int wxIslower(wxChar ch) { return CFCharacterSetIsCharacterMember(cflowerset, ch); }
1628WXDLLEXPORT int wxIsprint(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
1629WXDLLEXPORT int wxIspunct(wxChar ch) { return CFCharacterSetIsCharacterMember(cfpunctset, ch); }
1630WXDLLEXPORT int wxIsspace(wxChar ch) { return CFCharacterSetIsCharacterMember(cfspaceset, ch); }
1631WXDLLEXPORT int wxIsupper(wxChar ch) { return CFCharacterSetIsCharacterMember(cfupperset, ch); }
1632WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxIsdigit(ch) || (ch>='a' && ch<='f') || (ch>='A' && ch<='F'); }
1633WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)tolower((char)(ch)); }
1634WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)toupper((char)(ch)); }
dcb68102 1635
8a9c20b0
RN
1636#endif // wxNEED_WX_CTYPE_H
1637
07243717
VZ
1638#ifndef wxStrdupA
1639
1640WXDLLEXPORT char *wxStrdupA(const char *s)
1641{
1642 return strcpy((char *)malloc(strlen(s) + 1), s);
1643}
1644
1645#endif // wxStrdupA
1646
1647#ifndef wxStrdupW
1648
1649WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz)
c9e089e9 1650{
07243717
VZ
1651 size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t);
1652 wchar_t *ret = (wchar_t *) malloc(size);
1653 memcpy(ret, pwz, size);
c9e089e9
OK
1654 return ret;
1655}
07243717
VZ
1656
1657#endif // wxStrdupW
c9e089e9 1658
d0bdc3ca
OK
1659#ifndef wxStricmp
1660int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
1661{
1662 register wxChar c1, c2;
1663 do {
1664 c1 = wxTolower(*psz1++);
1665 c2 = wxTolower(*psz2++);
1666 } while ( c1 && (c1 == c2) );
1667 return c1 - c2;
1668}
1669#endif
1670
e766c8a9
SC
1671#ifndef wxStricmp
1672int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n)
1673{
a400a823
VZ
1674 // initialize the variables just to suppress stupid gcc warning
1675 register wxChar c1 = 0, c2 = 0;
e766c8a9
SC
1676 while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
1677 if (n) {
1678 if (c1 < c2) return -1;
1679 if (c1 > c2) return 1;
1680 }
1681 return 0;
1682}
1683#endif
1684
c9e089e9 1685#ifndef wxSetlocale
191ab39a 1686WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
c9e089e9 1687{
ddf14c13 1688 char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale));
929eed47 1689
ddf14c13 1690 return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld));
c9e089e9
OK
1691}
1692#endif
1693
30261041
RN
1694#if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
1695WXDLLEXPORT size_t wxWcslen(const wchar_t *s)
1696{
1697 size_t n = 0;
1698 while ( *s++ )
1699 n++;
1700
1701 return n;
1702}
1703#endif
1704
f6f5941b
VZ
1705// ----------------------------------------------------------------------------
1706// string.h functions
1707// ----------------------------------------------------------------------------
1708
b0655a87 1709#ifdef wxNEED_WX_STRING_H
30261041
RN
1710
1711// RN: These need to be c externed for the regex lib
1712#ifdef __cplusplus
1713extern "C" {
1714#endif
1715
b0655a87
OK
1716WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
1717{
1718 wxChar *ret = dest;
1719 while (*dest) dest++;
1720 while ((*dest++ = *src++));
1721 return ret;
1722}
1723
109c7768 1724WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c)
b0655a87 1725{
109c7768
VZ
1726 // be careful here as the terminating NUL makes part of the string
1727 while ( *s != c )
1728 {
1729 if ( !*s++ )
1730 return NULL;
1731 }
1732
1733 return s;
b0655a87
OK
1734}
1735
1736WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
1737{
1738 while ((*s1 == *s2) && *s1) s1++, s2++;
1739 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
1740 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
1741 return 0;
1742}
1743
1744WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
1745{
1746 wxChar *ret = dest;
1747 while ((*dest++ = *src++));
1748 return ret;
1749}
1750
dcb68102
RN
1751WXDLLEXPORT size_t wxStrlen_(const wxChar *s)
1752{
1753 size_t n = 0;
1754 while ( *s++ )
1755 n++;
525d8583 1756
dcb68102
RN
1757 return n;
1758}
1759
1760
b0655a87
OK
1761WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
1762{
1763 wxChar *ret = dest;
1764 while (*dest) dest++;
1765 while (n && (*dest++ = *src++)) n--;
1766 return ret;
1767}
1768
a5e0b1e8
OK
1769WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
1770{
1771 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
1772 if (n) {
1773 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
1774 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
1775 }
1776 return 0;
1777}
1778
b0655a87
OK
1779WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
1780{
1781 wxChar *ret = dest;
1782 while (n && (*dest++ = *src++)) n--;
1783 while (n) *dest++=0, n--; // the docs specify padding with zeroes
1784 return ret;
1785}
1786
109c7768 1787WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
b0655a87 1788{
109c7768
VZ
1789 while (*s && !wxStrchr(accept, *s))
1790 s++;
1791
1792 return *s ? s : NULL;
b0655a87
OK
1793}
1794
109c7768 1795WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c)
b0655a87 1796{
109c7768
VZ
1797 const wxChar *ret = NULL;
1798 do
1799 {
1800 if ( *s == c )
1801 ret = s;
1802 s++;
1803 }
1804 while ( *s );
1805
1806 return ret;
b0655a87
OK
1807}
1808
1809WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
1810{
1811 size_t len = 0;
1812 while (wxStrchr(accept, *s++)) len++;
1813 return len;
1814}
1815
109c7768 1816WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle)
b0655a87 1817{
dcb68102 1818 wxASSERT_MSG( needle != NULL, _T("NULL argument in wxStrstr") );
9f6fe288 1819
109c7768
VZ
1820 // VZ: this is not exactly the most efficient string search algorithm...
1821
9f6fe288
VZ
1822 const size_t len = wxStrlen(needle);
1823
109c7768 1824 while ( const wxChar *fnd = wxStrchr(haystack, *needle) )
9f6fe288
VZ
1825 {
1826 if ( !wxStrncmp(fnd, needle, len) )
1827 return fnd;
1828
1829 haystack = fnd + 1;
1830 }
1831
109c7768 1832 return NULL;
b0655a87
OK
1833}
1834
30261041
RN
1835#ifdef __cplusplus
1836}
1837#endif
1838
b0655a87
OK
1839WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
1840{
1841 const wxChar *start = nptr;
1842
1843 // FIXME: only correct for C locale
1844 while (wxIsspace(*nptr)) nptr++;
223d09f6 1845 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
b0655a87 1846 while (wxIsdigit(*nptr)) nptr++;
223d09f6 1847 if (*nptr == wxT('.')) {
b0655a87
OK
1848 nptr++;
1849 while (wxIsdigit(*nptr)) nptr++;
1850 }
223d09f6 1851 if (*nptr == wxT('E') || *nptr == wxT('e')) {
b0655a87 1852 nptr++;
223d09f6 1853 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
b0655a87
OK
1854 while (wxIsdigit(*nptr)) nptr++;
1855 }
1856
1857 wxString data(nptr, nptr-start);
ddf14c13 1858 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
e90c1d2a 1859 char *rdat = wxMBSTRINGCAST dat;
b0655a87
OK
1860 double ret = strtod(dat, &rdat);
1861
1862 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
1863
1864 return ret;
1865}
1866
1867WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
1868{
1869 const wxChar *start = nptr;
1870
1871 // FIXME: only correct for C locale
1872 while (wxIsspace(*nptr)) nptr++;
223d09f6 1873 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
b0655a87 1874 if (((base == 0) || (base == 16)) &&
223d09f6 1875 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
b0655a87
OK
1876 nptr += 2;
1877 base = 16;
1878 }
223d09f6 1879 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
b0655a87
OK
1880 else if (base == 0) base = 10;
1881
223d09f6
KB
1882 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
1883 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
b0655a87 1884
dcb68102 1885 wxString data(start, nptr-start);
ddf14c13 1886 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
e90c1d2a 1887 char *rdat = wxMBSTRINGCAST dat;
b0655a87
OK
1888 long int ret = strtol(dat, &rdat, base);
1889
1890 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
1891
1892 return ret;
1893}
dcb68102
RN
1894
1895WXDLLEXPORT unsigned long int wxStrtoul(const wxChar *nptr, wxChar **endptr, int base)
1896{
1897 return (unsigned long int) wxStrtol(nptr, endptr, base);
1898}
1899
f6f5941b 1900#endif // wxNEED_WX_STRING_H
b0655a87 1901
c9e089e9 1902#ifdef wxNEED_WX_STDIO_H
e7b3d6ba
OK
1903WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
1904{
b1ac3b56
RR
1905 char mode_buffer[10];
1906 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
1907 mode_buffer[i] = (char) mode[i];
f6f5941b 1908
b1ac3b56 1909 return fopen( wxConvFile.cWX2MB(path), mode_buffer );
e7b3d6ba
OK
1910}
1911
1912WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
1913{
b1ac3b56
RR
1914 char mode_buffer[10];
1915 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
1916 mode_buffer[i] = (char) mode[i];
f6f5941b 1917
b1ac3b56 1918 return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream );
e7b3d6ba
OK
1919}
1920
f6bcfd97
BP
1921WXDLLEXPORT int wxRemove(const wxChar *path)
1922{
92980e90 1923 return remove( wxConvFile.cWX2MB(path) );
f6bcfd97
BP
1924}
1925
1926WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
1927{
92980e90 1928 return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) );
f6bcfd97 1929}
c9e089e9
OK
1930#endif
1931
b7a5d6ca 1932#ifndef wxAtof
c9e089e9
OK
1933double WXDLLEXPORT wxAtof(const wxChar *psz)
1934{
4676948b
JS
1935#ifdef __WXWINCE__
1936 double d;
1937 wxString str(psz);
1938 if (str.ToDouble(& d))
1939 return d;
41e155b4
WS
1940
1941 return 0.0;
4676948b 1942#else
ddf14c13 1943 return atof(wxConvLibc.cWX2MB(psz));
4676948b 1944#endif
c9e089e9 1945}
b7a5d6ca 1946#endif
c9e089e9 1947
b7a5d6ca 1948#ifdef wxNEED_WX_STDLIB_H
c9e089e9
OK
1949int WXDLLEXPORT wxAtoi(const wxChar *psz)
1950{
ddf14c13 1951 return atoi(wxConvLibc.cWX2MB(psz));
c9e089e9
OK
1952}
1953
1954long WXDLLEXPORT wxAtol(const wxChar *psz)
1955{
ddf14c13 1956 return atol(wxConvLibc.cWX2MB(psz));
c9e089e9
OK
1957}
1958
1959wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
1960{
78868257 1961#if wxUSE_UNICODE
4772f8e1
VS
1962 // NB: buffer returned by getenv() is allowed to be overwritten next
1963 // time getenv() is called, so it is OK to use static string
1964 // buffer to hold the data.
1965 static wxWCharBuffer value((wxChar*)NULL);
ddf14c13 1966 value = wxConvLibc.cMB2WX(getenv(wxConvLibc.cWX2MB(name)));
4772f8e1 1967 return value.data();
92980e90 1968#else
4772f8e1 1969 return getenv(name);
92980e90 1970#endif
c9e089e9
OK
1971}
1972
b1ac3b56 1973int WXDLLEXPORT wxSystem(const wxChar *psz)
c9e089e9 1974{
ddf14c13 1975 return system(wxConvLibc.cWX2MB(psz));
c9e089e9
OK
1976}
1977
33a7c3dd 1978#endif // wxNEED_WX_STDLIB_H
e7b3d6ba
OK
1979
1980#ifdef wxNEED_WX_TIME_H
26378b41
VZ
1981WXDLLEXPORT size_t
1982wxStrftime(wxChar *s, size_t maxsize, const wxChar *fmt, const struct tm *tm)
e7b3d6ba 1983{
26378b41
VZ
1984 if ( !maxsize )
1985 return 0;
f6f5941b 1986
26378b41
VZ
1987 wxCharBuffer buf(maxsize);
1988
ddf14c13 1989 wxCharBuffer bufFmt(wxConvLibc.cWX2MB(fmt));
26378b41 1990 if ( !bufFmt )
b1ac3b56 1991 return 0;
26378b41
VZ
1992
1993 size_t ret = strftime(buf.data(), maxsize, bufFmt, tm);
1994 if ( !ret )
1995 return 0;
1996
ddf14c13 1997 wxWCharBuffer wbuf = wxConvLibc.cMB2WX(buf);
26378b41
VZ
1998 if ( !wbuf )
1999 return 0;
2000
2001 wxStrncpy(s, wbuf, maxsize);
2002 return wxStrlen(s);
e7b3d6ba 2003}
33a7c3dd
VZ
2004#endif // wxNEED_WX_TIME_H
2005
b63b07a8
RL
2006#ifndef wxCtime
2007WXDLLEXPORT wxChar *wxCtime(const time_t *timep)
2008{
9348da2f
VZ
2009 // normally the string is 26 chars but give one more in case some broken
2010 // DOS compiler decides to use "\r\n" instead of "\n" at the end
2011 static wxChar buf[27];
2012
2013 // ctime() is guaranteed to return a string containing only ASCII
2014 // characters, as its format is always the same for any locale
2015 wxStrncpy(buf, wxString::FromAscii(ctime(timep)), WXSIZEOF(buf));
2016 buf[WXSIZEOF(buf) - 1] = _T('\0');
b63b07a8
RL
2017
2018 return buf;
2019}
2020#endif // wxCtime
2021
33a7c3dd
VZ
2022#endif // wxUSE_WCHAR_T
2023
2024// ----------------------------------------------------------------------------
2025// functions which we may need even if !wxUSE_WCHAR_T
2026// ----------------------------------------------------------------------------
2027
2028#ifndef wxStrtok
2029
2030WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
2031{
2032 if (!psz)
2033 {
2034 psz = *save_ptr;
2035 if ( !psz )
2036 return NULL;
2037 }
2038
2039 psz += wxStrspn(psz, delim);
2040 if (!*psz)
2041 {
2042 *save_ptr = (wxChar *)NULL;
2043 return (wxChar *)NULL;
2044 }
2045
2046 wxChar *ret = psz;
2047 psz = wxStrpbrk(psz, delim);
2048 if (!psz)
2049 {
2050 *save_ptr = (wxChar*)NULL;
2051 }
2052 else
2053 {
2054 *psz = wxT('\0');
2055 *save_ptr = psz + 1;
2056 }
2057
2058 return ret;
2059}
2060
2061#endif // wxStrtok
2062
2bb1e1f4
SC
2063// ----------------------------------------------------------------------------
2064// missing C RTL functions
2065// ----------------------------------------------------------------------------
2066
896e226a 2067#ifdef wxNEED_STRDUP
27db4210 2068
2bb1e1f4
SC
2069char *strdup(const char *s)
2070{
d314acc0
SC
2071 char *dest = (char*) malloc( strlen( s ) + 1 ) ;
2072 if ( dest )
2073 strcpy( dest , s ) ;
2074 return dest ;
2bb1e1f4 2075}
27db4210 2076#endif // wxNEED_STRDUP
0be9ace2 2077
eae4425d 2078#if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
27db4210 2079
0be9ace2
JS
2080void *calloc( size_t num, size_t size )
2081{
2082 void** ptr = (void **)malloc(num * size);
2083 memset( ptr, 0, num * size);
2084 return ptr;
2085}
42d11c8e 2086
27db4210 2087#endif // __WXWINCE__ <= 211
4c663122 2088
619be6d0
JS
2089#ifdef __WXWINCE__
2090
2091int wxRemove(const wxChar *path)
2092{
2093 return ::DeleteFile(path) == 0;
2094}
2095
2096#endif