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