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