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