1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wxchar.cpp
3 // Purpose: wxChar implementation
5 // Modified by: Ron Lee, Francesco Montorsi
8 // Copyright: (c) wxWidgets copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
13 // headers, declarations, constants
14 // ===========================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/wxchar.h"
25 #define _ISOC9X_SOURCE 1 // to get vsscanf()
26 #define _BSD_SOURCE 1 // to still get strdup()
36 #include "wx/msw/wince/time.h"
40 #include "wx/string.h"
42 #include "wx/utils.h" // for wxMin and wxMax
46 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
53 #if defined(__MWERKS__) && __MSL__ >= 0x6000
59 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
61 // assume that we have mbsrtowcs() too if we have wcsrtombs()
64 memset(&mbstate
, 0, sizeof(mbstate_t));
69 if (n
) *buf
= wxT('\0');
73 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
75 return wxMbstowcs(buf
, psz
, n
);
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
86 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
88 return wxMbstowcs((wchar_t *) NULL
, psz
, 0);
92 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
96 memset(&mbstate
, 0, sizeof(mbstate_t));
101 // glibc2.1 chokes on null input
105 #ifdef HAVE_WCSRTOMBS
106 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
108 return wxWcstombs(buf
, pwz
, n
);
112 #ifdef HAVE_WCSRTOMBS
113 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
115 return wxWcstombs((char *) NULL
, pwz
, 0);
118 #endif // wxUSE_WCHAR_T
120 bool WXDLLEXPORT
wxOKlibc()
122 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
123 // glibc 2.0 uses UTF-8 even when it shouldn't
125 if ((MB_CUR_MAX
== 2) &&
126 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
128 // this is UTF-8 allright, check whether that's what we want
129 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
130 if ((strlen(cur_locale
) < 4) ||
131 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
132 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
133 // nope, don't use libc conversion
141 // ============================================================================
142 // printf() functions business
143 // ============================================================================
145 // special test mode: define all functions below even if we don't really need
146 // them to be able to test them
157 #define wxNEED_WPRINTF
159 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
162 // ----------------------------------------------------------------------------
163 // implement [v]snprintf() if the system doesn't provide a safe one
164 // or if the system's one does not support positional parameters
165 // (very useful for i18n purposes)
166 // ----------------------------------------------------------------------------
168 #if !defined(wxVsnprintf_)
170 // wxUSE_STRUTILS says our wxVsnprintf_ implementation to use or not to
171 // use wxStrlen and wxStrncpy functions over one-char processing loops.
173 // Some benchmarking revealed that wxUSE_STRUTILS == 1 has the following
176 // when in ANSI mode, this setting does not change almost anything
177 // when in Unicode mode, it gives ~ 50% of slowdown !
179 // both in ANSI and Unicode mode it gives ~ 60% of speedup !
181 #if defined(WIN32) && wxUSE_UNICODE
182 #define wxUSE_STRUTILS 0
184 #define wxUSE_STRUTILS 1
187 // some limits of our implementation
188 #define wxMAX_SVNPRINTF_ARGUMENTS 16
189 #define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32
190 #define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512
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
199 #if defined(__WINDOWS__) && !defined(HAVE_SWPRINTF)
200 // all compilers under Windows should have swprintf()
201 #define HAVE_SWPRINTF
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__)
212 #ifndef HAVE_BROKEN_SWPRINTF_DECL
213 #define HAVE_BROKEN_SWPRINTF_DECL
217 // problem: on some systems swprintf takes the 'max' argument while on
219 #if defined(HAVE_BROKEN_SWPRINTF_DECL)
220 // like when using sprintf(), since 'max' is not used, wxVsnprintf()
221 // should always ensure that 'buff' is big enough for all common needs
222 #define system_sprintf(buff, max, flags, data) \
223 ::swprintf(buff, flags, data)
225 #define SYSTEM_SPRINTF_IS_UNSAFE
227 #if !defined(HAVE_SWPRINTF)
228 #error wxVsnprintf() needs a system swprintf() implementation!
231 #define system_sprintf(buff, max, flags, data) \
232 ::swprintf(buff, max, flags, data)
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)
240 #define system_sprintf(buff, max, flags, data) \
241 ::snprintf(buff, max, flags, data)
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
245 // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN)
246 #define system_sprintf(buff, max, flags, data) \
247 ::sprintf(buff, flags, data)
249 #define SYSTEM_SPRINTF_IS_UNSAFE
251 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
255 // the conversion specifiers accepted by wxVsnprintf_
256 enum wxPrintfArgType
{
259 wxPAT_INT
, // %d, %i, %o, %u, %x, %X
260 wxPAT_LONGINT
, // %ld, etc
262 wxPAT_LONGLONGINT
, // %Ld, etc
264 wxPAT_SIZET
, // %Zd, etc
266 wxPAT_DOUBLE
, // %e, %E, %f, %g, %G
267 wxPAT_LONGDOUBLE
, // %le, etc
271 wxPAT_CHAR
, // %hc (in ANSI mode: %c, too)
272 wxPAT_WCHAR
, // %lc (in Unicode mode: %c, too)
274 wxPAT_PCHAR
, // %s (related to a char *)
275 wxPAT_PWCHAR
, // %s (related to a wchar_t *)
278 wxPAT_NSHORTINT
, // %hn
279 wxPAT_NLONGINT
// %ln
282 // an argument passed to wxVsnprintf_
284 int pad_int
; // %d, %i, %o, %u, %x, %X
285 long int pad_longint
; // %ld, etc
287 long long int pad_longlongint
; // %Ld, etc
289 size_t pad_sizet
; // %Zd, etc
291 double pad_double
; // %e, %E, %f, %g, %G
292 long double pad_longdouble
; // %le, etc
294 void *pad_pointer
; // %p
296 char pad_char
; // %hc (in ANSI mode: %c, too)
297 wchar_t pad_wchar
; // %lc (in Unicode mode: %c, too)
299 char *pad_pchar
; // %s (related to a char *)
300 wchar_t *pad_pwchar
; // %s (related to a wchar_t *)
303 short int *pad_nshortint
; // %hn
304 long int *pad_nlongint
; // %ln
308 // Contains parsed data relative to a conversion specifier given to
309 // wxVsnprintf_ and parsed from the format string
310 // NOTE: in C++ there is almost no difference between struct & classes thus
311 // there is no performance gain by using a struct here...
312 class wxPrintfConvSpec
316 // the position of the argument relative to this conversion specifier
319 // the type of this conversion specifier
320 wxPrintfArgType m_type
;
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
325 int m_nMinWidth
, m_nMaxWidth
;
327 // does the argument need to the be aligned to left ?
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 !
333 const wxChar
*m_pArgPos
;
335 // pointer to the last character of this conversion specifier in the
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 !
339 const wxChar
*m_pArgEnd
;
341 // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse()
342 // for use in Process()
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
];
352 // we don't declare this as a constructor otherwise it would be called
353 // automatically and we don't want this: to be optimized, wxVsnprintf_
354 // calls this function only on really-used instances of this class.
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
);
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
);
367 // Loads the argument of this conversion specifier from given va_list.
368 bool LoadArg(wxPrintfArg
*p
, va_list &argptr
);
371 // An helper function of LoadArg() which is used to handle the '*' flag
372 void ReplaceAsteriskWith(int w
);
375 void wxPrintfConvSpec::Init()
378 m_nMaxWidth
= 0xFFFF;
380 m_bAlignLeft
= false;
381 m_pArgPos
= m_pArgEnd
= NULL
;
382 m_type
= wxPAT_INVALID
;
384 // this character will never be removed from m_szFlags array and
385 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
386 m_szFlags
[0] = wxT('%');
389 bool wxPrintfConvSpec::Parse(const wxChar
*format
)
393 // temporary parse data
395 bool in_prec
, prec_dot
;
398 m_bAlignLeft
= in_prec
= prec_dot
= false;
399 m_pArgPos
= m_pArgEnd
= format
;
403 if (in_prec && !prec_dot) \
405 m_szFlags[flagofs++] = wxT('.'); \
410 const wxChar ch
= *(++m_pArgEnd
);
414 return false; // not really an argument
417 return false; // not really an argument
425 m_szFlags
[flagofs
++] = ch
;
431 m_szFlags
[flagofs
++] = ch
;
439 // dot will be auto-added to m_szFlags if non-negative
446 m_szFlags
[flagofs
++] = ch
;
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'
456 m_szFlags
[flagofs
++] = ch
;
463 m_szFlags
[flagofs
++] = ch
;
469 m_szFlags
[flagofs
++] = ch
;
477 // tell Process() to use the next argument
478 // in the stack as maxwidth...
483 // tell Process() to use the next argument
484 // in the stack as minwidth...
488 // save the * in our formatting buffer...
489 // will be replaced later by Process()
490 m_szFlags
[flagofs
++] = ch
;
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'):
499 while ( (*m_pArgEnd
>= wxT('0')) &&
500 (*m_pArgEnd
<= wxT('9')) )
502 m_szFlags
[flagofs
++] = (*m_pArgEnd
);
503 len
= len
*10 + (*m_pArgEnd
- wxT('0'));
512 m_pArgEnd
--; // the main loop pre-increments n again
516 case wxT('$'): // a positional parameter (e.g. %2$s) ?
518 if (m_nMinWidth
<= 0)
519 break; // ignore this formatting flag as no
520 // numbers are preceding it
522 // remove from m_szFlags all digits previously added
525 } while (m_szFlags
[flagofs
] >= '1' &&
526 m_szFlags
[flagofs
] <= '9');
528 // re-adjust the offset making it point to the
529 // next free char of m_szFlags
544 m_szFlags
[flagofs
++] = ch
;
545 m_szFlags
[flagofs
] = wxT('\0');
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
554 m_type
= wxPAT_LONGINT
;
557 m_type
= wxPAT_LONGLONGINT
;
559 m_type
= wxPAT_LONGINT
;
560 #endif // long long/!long long
562 m_type
= wxPAT_SIZET
;
572 m_szFlags
[flagofs
++] = ch
;
573 m_szFlags
[flagofs
] = wxT('\0');
575 m_type
= wxPAT_LONGDOUBLE
;
577 m_type
= wxPAT_DOUBLE
;
582 m_type
= wxPAT_POINTER
;
583 m_szFlags
[flagofs
++] = ch
;
584 m_szFlags
[flagofs
] = '\0';
591 // in Unicode mode %hc == ANSI character
592 // and in ANSI mode, %hc == %c == ANSI...
597 // in ANSI mode %lc == Unicode character
598 // and in Unicode mode, %lc == %c == Unicode...
599 m_type
= wxPAT_WCHAR
;
604 // in Unicode mode, %c == Unicode character
605 m_type
= wxPAT_WCHAR
;
607 // in ANSI mode, %c == ANSI character
617 // Unicode mode wx extension: we'll let %hs mean non-Unicode
618 // strings (when in ANSI mode, %s == %hs == ANSI string)
619 m_type
= wxPAT_PCHAR
;
623 // in Unicode mode, %ls == %s == Unicode string
624 // in ANSI mode, %ls == Unicode string
625 m_type
= wxPAT_PWCHAR
;
630 m_type
= wxPAT_PWCHAR
;
632 m_type
= wxPAT_PCHAR
;
642 m_type
= wxPAT_NSHORTINT
;
644 m_type
= wxPAT_NLONGINT
;
649 // bad format, don't consider this an argument;
650 // leave it unchanged
654 if (flagofs
== wxMAX_SVNPRINTF_FLAGBUFFER_LEN
)
656 wxLogDebug(wxT("Too many flags specified for a single conversion specifier!"));
662 return true; // parsing was successful
666 void wxPrintfConvSpec::ReplaceAsteriskWith(int width
)
668 wxChar temp
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
670 // find the first * in our flag buffer
671 wxChar
*pwidth
= wxStrchr(m_szFlags
, wxT('*'));
674 // save what follows the * (the +1 is to skip the asterisk itself!)
675 wxStrcpy(temp
, pwidth
+1);
678 pwidth
[0] = wxT('-');
682 // replace * with the actual integer given as width
683 #ifndef SYSTEM_SPRINTF_IS_UNSAFE
684 int maxlen
= (m_szFlags
+ wxMAX_SVNPRINTF_FLAGBUFFER_LEN
- pwidth
) /
687 int offset
= system_sprintf(pwidth
, maxlen
, wxT("%d"), abs(width
));
689 // restore after the expanded * what was following it
690 wxStrcpy(pwidth
+offset
, temp
);
693 bool wxPrintfConvSpec::LoadArg(wxPrintfArg
*p
, va_list &argptr
)
695 // did the '*' width/precision specifier was used ?
696 if (m_nMaxWidth
== -1)
698 // take the maxwidth specifier from the stack
699 m_nMaxWidth
= va_arg(argptr
, int);
703 ReplaceAsteriskWith(m_nMaxWidth
);
706 if (m_nMinWidth
== -1)
708 // take the minwidth specifier from the stack
709 m_nMinWidth
= va_arg(argptr
, int);
711 ReplaceAsteriskWith(m_nMinWidth
);
714 m_bAlignLeft
= !m_bAlignLeft
;
715 m_nMinWidth
= -m_nMinWidth
;
721 p
->pad_int
= va_arg(argptr
, int);
724 p
->pad_longint
= va_arg(argptr
, long int);
727 case wxPAT_LONGLONGINT
:
728 p
->pad_longlongint
= va_arg(argptr
, long long int);
732 p
->pad_sizet
= va_arg(argptr
, size_t);
735 p
->pad_double
= va_arg(argptr
, double);
737 case wxPAT_LONGDOUBLE
:
738 p
->pad_longdouble
= va_arg(argptr
, long double);
741 p
->pad_pointer
= va_arg(argptr
, void *);
745 p
->pad_char
= (char)va_arg(argptr
, int); // char is promoted to int when passed through '...'
748 p
->pad_wchar
= (wchar_t)va_arg(argptr
, int); // char is promoted to int when passed through '...'
752 p
->pad_pchar
= va_arg(argptr
, char *);
755 p
->pad_pwchar
= va_arg(argptr
, wchar_t *);
759 p
->pad_nint
= va_arg(argptr
, int *);
761 case wxPAT_NSHORTINT
:
762 p
->pad_nshortint
= va_arg(argptr
, short int *);
765 p
->pad_nlongint
= va_arg(argptr
, long int *);
773 return true; // loading was successful
776 int wxPrintfConvSpec::Process(wxChar
*buf
, size_t lenMax
, wxPrintfArg
*p
)
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
781 wxChar szScratch
[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
];
782 size_t lenScratch
= 0, lenCur
= 0;
784 #define APPEND_CH(ch) \
786 if ( lenCur == lenMax ) \
789 buf[lenCur++] = ch; \
792 #define APPEND_STR(s) \
794 for ( const wxChar *p = s; *p; p++ ) \
803 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_int
);
807 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longint
);
811 case wxPAT_LONGLONGINT
:
812 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longlongint
);
814 #endif // SIZEOF_LONG_LONG
817 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_sizet
);
820 case wxPAT_LONGDOUBLE
:
821 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longdouble
);
825 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_double
);
829 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_pointer
);
839 if (m_type
== wxPAT_CHAR
)
841 // user passed a character explicitely indicated as ANSI...
842 const char buf
[2] = { p
->pad_char
, 0 };
843 val
= wxString(buf
, wxConvLibc
)[0u];
845 //wprintf(L"converting ANSI=>Unicode"); // for debug
851 if (m_type
== wxPAT_WCHAR
)
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];
857 //printf("converting Unicode=>ANSI"); // for debug
865 for (i
= 1; i
< (size_t)m_nMinWidth
; i
++)
871 for (i
= 1; i
< (size_t)m_nMinWidth
; i
++)
884 if (m_type
== wxPAT_PCHAR
)
886 // user passed a string explicitely indicated as ANSI...
887 val
= s
= wxString(p
->pad_pchar
, wxConvLibc
);
889 //wprintf(L"converting ANSI=>Unicode"); // for debug
895 if (m_type
== wxPAT_PWCHAR
)
897 // user passed a string explicitely indicated as Unicode...
898 val
= s
= wxString(p
->pad_pwchar
, wxConvLibc
);
900 //printf("converting Unicode=>ANSI"); // for debug
909 // at this point we are sure that m_nMaxWidth is positive or null
910 // (see top of wxPrintfConvSpec::LoadArg)
911 len
= wxMin((unsigned int)m_nMaxWidth
, wxStrlen(val
));
913 for ( len
= 0; val
[len
] && (len
< m_nMaxWidth
); len
++ )
917 else if (m_nMaxWidth
>= 6)
932 for (i
= len
; i
< m_nMinWidth
; i
++)
937 len
= wxMin((unsigned int)len
, lenMax
-lenCur
);
938 wxStrncpy(buf
+lenCur
, val
, len
);
941 for (i
= 0; i
< len
; i
++)
947 for (i
= len
; i
< m_nMinWidth
; i
++)
954 *p
->pad_nint
= lenCur
;
957 case wxPAT_NSHORTINT
:
958 *p
->pad_nshortint
= (short int)lenCur
;
962 *p
->pad_nlongint
= lenCur
;
970 #ifdef HAVE_BROKEN_SWPRINTF_DECL
971 wxUnusedVar(lenScratch
); // avoid dummy warnings
974 // if we used system's sprintf() then we now need to append the s_szScratch
975 // buffer to the given one...
981 case wxPAT_LONGLONGINT
:
984 case wxPAT_LONGDOUBLE
:
989 wxASSERT( /* lenScratch >= 0 && */ lenScratch
< wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
);
990 if (lenMax
< lenScratch
)
992 // fill output buffer and then return -1
993 wxStrncpy(buf
, szScratch
, lenMax
);
996 wxStrncpy(buf
, szScratch
, lenScratch
);
997 lenCur
+= lenScratch
;
1001 APPEND_STR(szScratch
);
1007 break; // all other cases were completed previously
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...
1017 static int wxCopyStrWithPercents(wxChar
*dest
, const wxChar
*source
, size_t n
)
1025 for ( i
= 0; i
< n
-1; source
++, i
++)
1027 dest
[written
++] = *source
;
1028 if (*(source
+1) == wxT('%'))
1030 // skip this additional '%' character
1037 // copy last character inconditionally
1038 dest
[written
++] = *source
;
1043 int WXDLLEXPORT
wxVsnprintf_(wxChar
*buf
, size_t lenMax
,
1044 const wxChar
*format
, va_list argptr
)
1046 // useful for debugging, to understand if we are really using this function
1047 // rather than the system implementation
1049 wprintf(L
"Using wxVsnprintf_\n");
1053 wxPrintfConvSpec arg
[wxMAX_SVNPRINTF_ARGUMENTS
];
1054 wxPrintfArg argdata
[wxMAX_SVNPRINTF_ARGUMENTS
];
1055 wxPrintfConvSpec
*pspec
[wxMAX_SVNPRINTF_ARGUMENTS
] = { NULL
};
1059 // number of characters in the buffer so far, must be less than lenMax
1063 const wxChar
*toparse
= format
;
1065 // parse the format string
1066 bool posarg_present
= false, nonposarg_present
= false;
1067 for (; *toparse
!= wxT('\0'); toparse
++)
1069 if (*toparse
== wxT('%') )
1073 // let's see if this is a (valid) conversion specifier...
1074 if (arg
[nargs
].Parse(toparse
))
1077 wxPrintfConvSpec
*current
= &arg
[nargs
];
1079 // make toparse point to the end of this specifier
1080 toparse
= current
->m_pArgEnd
;
1082 if (current
->m_pos
> 0)
1084 // the positionals start from number 1... adjust the index
1086 posarg_present
= true;
1090 // not a positional argument...
1091 current
->m_pos
= nargs
;
1092 nonposarg_present
= true;
1095 // this conversion specifier is tied to the pos-th argument...
1096 pspec
[current
->m_pos
] = current
;
1099 if (nargs
== wxMAX_SVNPRINTF_ARGUMENTS
)
1101 wxLogDebug(wxT("A single call to wxVsnprintf() has more than %d arguments; ")
1102 wxT("ignoring all remaining arguments."), wxMAX_SVNPRINTF_ARGUMENTS
);
1103 break; // cannot handle any additional conv spec
1108 // it's safe to look in the next character of toparse as at worst
1110 if (*(toparse
+1) == wxT('%'))
1111 toparse
++; // the Parse() returned false because we've found a %%
1116 if (posarg_present
&& nonposarg_present
)
1117 return -1; // format strings with both positional and
1118 // non-positional conversion specifier are unsupported !!
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.
1124 wxVaCopy(ap
, argptr
);
1126 // now load arguments from stack
1127 for (i
=0; i
< nargs
&& ok
; i
++)
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...
1132 ok
= pspec
[i
] && pspec
[i
]->LoadArg(&argdata
[i
], ap
);
1137 // something failed while loading arguments from the variable list...
1141 // finally, process each conversion specifier with its own argument
1143 for (i
=0; i
< nargs
; i
++)
1145 // copy in the output buffer the portion of the format string between
1146 // last specifier and the current one
1147 size_t tocopy
= ( arg
[i
].m_pArgPos
- toparse
);
1148 if (lenCur
+tocopy
>= lenMax
)
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');
1157 lenCur
+= wxCopyStrWithPercents(buf
+lenCur
, toparse
, tocopy
);
1159 // process this specifier directly in the output buffer
1160 int n
= arg
[i
].Process(buf
+lenCur
, lenMax
- lenCur
, &argdata
[arg
[i
].m_pos
]);
1163 buf
[lenMax
-1] = wxT('\0'); // be sure to always NUL-terminate the string
1164 return -1; // not enough space in the output buffer !
1168 // the +1 is because wxPrintfConvSpec::m_pArgEnd points to the last character
1169 // of the format specifier, but we are not interested to it...
1170 toparse
= arg
[i
].m_pArgEnd
+ 1;
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 !
1181 // the -1 is because of the '\0'
1182 lenCur
+= wxCopyStrWithPercents(buf
+lenCur
, toparse
, tocopy
) - 1;
1184 wxASSERT(lenCur
== wxStrlen(buf
));
1192 #endif // !wxVsnprintfA
1194 #if !defined(wxSnprintf_)
1195 int WXDLLEXPORT
wxSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
1198 va_start(argptr
, format
);
1200 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
1206 #endif // wxSnprintf_
1208 #if defined(__DMC__)
1209 /* Digital Mars adds count to _stprintf (C99) so convert */
1211 int wxSprintf (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
1215 va_start( arglist
, format
);
1216 int iLen
= swprintf ( s
, -1, format
, arglist
);
1221 #endif // wxUSE_UNICODE
1225 // ----------------------------------------------------------------------------
1226 // implement the standard IO functions for wide char if libc doesn't have them
1227 // ----------------------------------------------------------------------------
1230 int wxFputs(const wchar_t *ws
, FILE *stream
)
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;
1236 #endif // wxNEED_FPUTS
1239 int wxPuts(const wxChar
*ws
)
1241 int rc
= wxFputs(ws
, stdout
);
1244 if ( wxFputs(L
"\n", stdout
) == -1 )
1252 #endif // wxNEED_PUTS
1255 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
1257 wchar_t ws
[2] = { wc
, L
'\0' };
1259 return wxFputs(ws
, stream
);
1261 #endif // wxNEED_PUTC
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
1268 // TODO: implement the scanf() functions
1269 int vwscanf(const wxChar
*format
, va_list argptr
)
1271 wxFAIL_MSG( _T("TODO") );
1276 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
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.
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") );
1289 wxVaCopy(argcopy
, argptr
);
1290 return vsscanf(wxConvLibc
.cWX2MB(ws
), wxConvLibc
.cWX2MB(format
), argcopy
);
1293 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
1295 wxFAIL_MSG( _T("TODO") );
1300 #define vswprintf wxVsnprintf_
1302 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
1305 int rc
= s
.PrintfV(format
, argptr
);
1309 // we can't do much better without Unicode support in libc...
1310 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
1317 int vwprintf(const wxChar
*format
, va_list argptr
)
1319 return wxVfprintf(stdout
, format
, argptr
);
1322 #endif // wxNEED_WPRINTF
1324 #ifdef wxNEED_PRINTF_CONVERSION
1326 // ----------------------------------------------------------------------------
1327 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
1328 // ----------------------------------------------------------------------------
1331 Here are the gory details. We want to follow the Windows/MS conventions,
1336 format specifier results in
1337 -----------------------------------
1339 %lc, %C, %lC wchar_t
1343 format specifier results in
1344 -----------------------------------
1346 %c, %lc, %lC wchar_t
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,
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):
1355 wxWidgets specifier POSIX specifier
1356 ----------------------------------------
1362 And, of course, the same should be done for %s as well.
1365 class wxFormatConverter
1368 wxFormatConverter(const wxChar
*format
);
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(); }
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
)
1384 // we're translating, do copy
1389 // simply increase the count which should be copied by
1390 // CopyAllBefore() later if needed
1397 // insert an extra character
1398 void InsertFmtChar(wxChar ch
)
1402 // so far we haven't translated anything yet
1409 void CopyAllBefore()
1411 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
1413 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
1415 // we won't need it any longer
1419 static bool IsFlagChar(wxChar ch
)
1421 return ch
== _T('-') || ch
== _T('+') ||
1422 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
1425 void SkipDigits(const wxChar
**ptpc
)
1427 while ( **ptpc
>= _T('0') && **ptpc
<= _T('9') )
1428 CopyFmtChar(*(*ptpc
)++);
1431 // the translated format
1434 // the original format
1435 const wxChar
*m_fmtOrig
;
1437 // the number of characters already copied
1441 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
1448 if ( CopyFmtChar(*format
++) == _T('%') )
1451 while ( IsFlagChar(*format
) )
1452 CopyFmtChar(*format
++);
1454 // and possible width
1455 if ( *format
== _T('*') )
1456 CopyFmtChar(*format
++);
1458 SkipDigits(&format
);
1461 if ( *format
== _T('.') )
1463 CopyFmtChar(*format
++);
1464 if ( *format
== _T('*') )
1465 CopyFmtChar(*format
++);
1467 SkipDigits(&format
);
1470 // next we can have a size modifier
1486 // "ll" has a different meaning!
1487 if ( format
[1] != _T('l') )
1493 //else: fall through
1499 // and finally we should have the type
1504 // %C and %hC -> %c and %lC -> %lc
1506 CopyFmtChar(_T('l'));
1508 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
1513 // %c -> %lc but %hc stays %hc and %lc is still %lc
1514 if ( size
== Default
)
1515 InsertFmtChar(_T('l'));
1519 // nothing special to do
1520 if ( size
!= Default
)
1521 CopyFmtChar(*(format
- 1));
1522 CopyFmtChar(*format
++);
1528 #else // !wxNEED_PRINTF_CONVERSION
1529 // no conversion necessary
1530 #define wxFormatConverter(x) (x)
1531 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
1534 // For testing the format converter
1535 wxString
wxConvertFormat(const wxChar
*format
)
1537 return wxString(wxFormatConverter(format
));
1541 // ----------------------------------------------------------------------------
1542 // wxPrintf(), wxScanf() and relatives
1543 // ----------------------------------------------------------------------------
1545 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
1547 int wxScanf( const wxChar
*format
, ... )
1550 va_start(argptr
, format
);
1552 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
1559 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... )
1562 va_start(argptr
, format
);
1564 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
1571 int wxFscanf( FILE *stream
, const wxChar
*format
, ... )
1574 va_start(argptr
, format
);
1575 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
1582 int wxPrintf( const wxChar
*format
, ... )
1585 va_start(argptr
, format
);
1587 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
1595 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... )
1598 va_start(argptr
, format
);
1600 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
1602 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
1603 // doesn't nul terminate on truncation.
1610 #endif // wxSnprintf
1612 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... )
1615 va_start(argptr
, format
);
1617 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
1618 // it's safe to implement this one in terms of it
1619 wxString
s(wxString::FormatV(format
, argptr
));
1627 int wxFprintf( FILE *stream
, const wxChar
*format
, ... )
1630 va_start( argptr
, format
);
1632 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
1639 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
1641 return vswscanf( str
, wxFormatConverter(format
), argptr
);
1644 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
1646 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
1649 int wxVprintf( const wxChar
*format
, va_list argptr
)
1651 return vwprintf( wxFormatConverter(format
), argptr
);
1655 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
1657 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
1659 #endif // wxVsnprintf
1661 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
1663 // same as for wxSprintf()
1664 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
1667 #endif // wxNEED_PRINTF_CONVERSION
1671 // ----------------------------------------------------------------------------
1672 // ctype.h stuff (currently unused)
1673 // ----------------------------------------------------------------------------
1675 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
1676 inline WORD
wxMSW_ctype(wxChar ch
)
1679 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
1683 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
1684 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
1685 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
1686 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
1687 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
1688 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
1689 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
1690 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
1691 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
1692 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
1693 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
1694 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
1695 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
1698 #ifdef wxNEED_WX_MBSTOWCS
1700 WXDLLEXPORT
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
1710 const char* origin
= in
;
1712 while (outlen
-- && *in
)
1714 *out
++ = (wchar_t) *in
++;
1722 WXDLLEXPORT
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
1732 const wchar_t* origin
= in
;
1734 while (outlen
-- && *in
)
1736 *out
++ = (char) *in
++;
1744 #endif // wxNEED_WX_MBSTOWCS
1746 #if defined(wxNEED_WX_CTYPE_H)
1748 #include <CoreFoundation/CoreFoundation.h>
1750 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
1751 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
1752 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
1753 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
1754 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
1755 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
1756 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
1757 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
1758 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
1759 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
1761 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalnumset
, ch
); }
1762 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalphaset
, ch
); }
1763 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
1764 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfdigitset
, ch
); }
1765 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
) && ch
!= ' '; }
1766 WXDLLEXPORT
int wxIslower(wxChar ch
) { return CFCharacterSetIsCharacterMember(cflowerset
, ch
); }
1767 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
1768 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfpunctset
, ch
); }
1769 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfspaceset
, ch
); }
1770 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfupperset
, ch
); }
1771 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxIsdigit(ch
) || (ch
>='a' && ch
<='f') || (ch
>='A' && ch
<='F'); }
1772 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)tolower((char)(ch
)); }
1773 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)toupper((char)(ch
)); }
1775 #endif // wxNEED_WX_CTYPE_H
1779 WXDLLEXPORT
char *wxStrdupA(const char *s
)
1781 return strcpy((char *)malloc(strlen(s
) + 1), s
);
1788 WXDLLEXPORT
wchar_t * wxStrdupW(const wchar_t *pwz
)
1790 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
1791 wchar_t *ret
= (wchar_t *) malloc(size
);
1792 memcpy(ret
, pwz
, size
);
1799 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
1801 register wxChar c1
, c2
;
1803 c1
= wxTolower(*psz1
++);
1804 c2
= wxTolower(*psz2
++);
1805 } while ( c1
&& (c1
== c2
) );
1811 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1813 // initialize the variables just to suppress stupid gcc warning
1814 register wxChar c1
= 0, c2
= 0;
1815 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1817 if (c1
< c2
) return -1;
1818 if (c1
> c2
) return 1;
1825 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
1827 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
1829 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
1833 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
1834 WXDLLEXPORT
size_t wxWcslen(const wchar_t *s
)
1844 // ----------------------------------------------------------------------------
1845 // string.h functions
1846 // ----------------------------------------------------------------------------
1848 #ifdef wxNEED_WX_STRING_H
1850 // RN: These need to be c externed for the regex lib
1855 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
1858 while (*dest
) dest
++;
1859 while ((*dest
++ = *src
++));
1863 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
1865 // be careful here as the terminating NUL makes part of the string
1875 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
1877 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
1878 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1879 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1883 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
1886 while ((*dest
++ = *src
++));
1890 WXDLLEXPORT
size_t wxStrlen_(const wxChar
*s
)
1900 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1903 while (*dest
) dest
++;
1904 while (n
&& (*dest
++ = *src
++)) n
--;
1908 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1910 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1912 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1913 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1918 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1921 while (n
&& (*dest
++ = *src
++)) n
--;
1922 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1926 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1928 while (*s
&& !wxStrchr(accept
, *s
))
1931 return *s
? s
: NULL
;
1934 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1936 const wxChar
*ret
= NULL
;
1948 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1951 while (wxStrchr(accept
, *s
++)) len
++;
1955 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1957 wxASSERT_MSG( needle
!= NULL
, _T("NULL argument in wxStrstr") );
1959 // VZ: this is not exactly the most efficient string search algorithm...
1961 const size_t len
= wxStrlen(needle
);
1963 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1965 if ( !wxStrncmp(fnd
, needle
, len
) )
1978 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1980 const wxChar
*start
= nptr
;
1982 // FIXME: only correct for C locale
1983 while (wxIsspace(*nptr
)) nptr
++;
1984 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1985 while (wxIsdigit(*nptr
)) nptr
++;
1986 if (*nptr
== wxT('.')) {
1988 while (wxIsdigit(*nptr
)) nptr
++;
1990 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1992 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1993 while (wxIsdigit(*nptr
)) nptr
++;
1996 wxString
data(nptr
, nptr
-start
);
1997 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1998 char *rdat
= wxMBSTRINGCAST dat
;
1999 double ret
= strtod(dat
, &rdat
);
2001 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
2006 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
2008 const wxChar
*start
= nptr
;
2010 // FIXME: only correct for C locale
2011 while (wxIsspace(*nptr
)) nptr
++;
2012 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
2013 if (((base
== 0) || (base
== 16)) &&
2014 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
2018 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
2019 else if (base
== 0) base
= 10;
2021 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
2022 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
2024 wxString
data(start
, nptr
-start
);
2025 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
2026 char *rdat
= wxMBSTRINGCAST dat
;
2027 long int ret
= strtol(dat
, &rdat
, base
);
2029 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
2034 WXDLLEXPORT
unsigned long int wxStrtoul(const wxChar
*nptr
, wxChar
**endptr
, int base
)
2036 return (unsigned long int) wxStrtol(nptr
, endptr
, base
);
2039 #endif // wxNEED_WX_STRING_H
2041 #ifdef wxNEED_WX_STDIO_H
2042 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
2044 char mode_buffer
[10];
2045 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
2046 mode_buffer
[i
] = (char) mode
[i
];
2048 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
2051 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
2053 char mode_buffer
[10];
2054 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
2055 mode_buffer
[i
] = (char) mode
[i
];
2057 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
2060 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
2062 return remove( wxConvFile
.cWX2MB(path
) );
2065 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
2067 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
2072 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
2077 if (str
.ToDouble(& d
))
2082 return atof(wxConvLibc
.cWX2MB(psz
));
2087 #ifdef wxNEED_WX_STDLIB_H
2088 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
2090 return atoi(wxConvLibc
.cWX2MB(psz
));
2093 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
2095 return atol(wxConvLibc
.cWX2MB(psz
));
2098 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
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
);
2105 value
= wxConvLibc
.cMB2WX(getenv(wxConvLibc
.cWX2MB(name
)));
2106 return value
.data();
2108 return getenv(name
);
2112 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
2114 return system(wxConvLibc
.cWX2MB(psz
));
2117 #endif // wxNEED_WX_STDLIB_H
2119 #ifdef wxNEED_WX_TIME_H
2121 wxStrftime(wxChar
*s
, size_t maxsize
, const wxChar
*fmt
, const struct tm
*tm
)
2126 wxCharBuffer
buf(maxsize
);
2128 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
2132 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
2136 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
2140 wxStrncpy(s
, wbuf
, maxsize
);
2143 #endif // wxNEED_WX_TIME_H
2146 WXDLLEXPORT wxChar
*wxCtime(const time_t *timep
)
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];
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');
2161 #endif // wxUSE_WCHAR_T
2163 // ----------------------------------------------------------------------------
2164 // functions which we may need even if !wxUSE_WCHAR_T
2165 // ----------------------------------------------------------------------------
2169 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
2178 psz
+= wxStrspn(psz
, delim
);
2181 *save_ptr
= (wxChar
*)NULL
;
2182 return (wxChar
*)NULL
;
2186 psz
= wxStrpbrk(psz
, delim
);
2189 *save_ptr
= (wxChar
*)NULL
;
2194 *save_ptr
= psz
+ 1;
2202 // ----------------------------------------------------------------------------
2203 // missing C RTL functions
2204 // ----------------------------------------------------------------------------
2206 #ifdef wxNEED_STRDUP
2208 char *strdup(const char *s
)
2210 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
2212 strcpy( dest
, s
) ;
2215 #endif // wxNEED_STRDUP
2217 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
2219 void *calloc( size_t num
, size_t size
)
2221 void** ptr
= (void **)malloc(num
* size
);
2222 memset( ptr
, 0, num
* size
);
2226 #endif // __WXWINCE__ <= 211
2230 int wxRemove(const wxChar
*path
)
2232 return ::DeleteFile(path
) == 0;