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