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 #define _ISOC9X_SOURCE 1 // to get vsscanf()
24 #define _BSD_SOURCE 1 // to still get strdup()
34 #include "wx/msw/wince/time.h"
38 #include "wx/wxchar.h"
39 #include "wx/string.h"
42 #include "wx/utils.h" // for wxMin and wxMax
44 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
51 #if defined(__MWERKS__) && __MSL__ >= 0x6000
57 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
59 // assume that we have mbsrtowcs() too if we have wcsrtombs()
62 memset(&mbstate
, 0, sizeof(mbstate_t));
67 if (n
) *buf
= wxT('\0');
71 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
73 return wxMbstowcs(buf
, psz
, n
);
77 // note that we rely on common (and required by Unix98 but unfortunately not
78 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
79 // to just get the size of the needed buffer -- this is needed as otherwise
80 // we have no idea about how much space we need and if the CRT doesn't
81 // support it (the only currently known example being Metrowerks, see
82 // wx/wxchar.h) we don't use its mbstowcs() at all
84 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
86 return wxMbstowcs((wchar_t *) NULL
, psz
, 0);
90 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
94 memset(&mbstate
, 0, sizeof(mbstate_t));
99 // glibc2.1 chokes on null input
103 #ifdef HAVE_WCSRTOMBS
104 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
106 return wxWcstombs(buf
, pwz
, n
);
110 #ifdef HAVE_WCSRTOMBS
111 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
113 return wxWcstombs((char *) NULL
, pwz
, 0);
116 #endif // wxUSE_WCHAR_T
118 bool WXDLLEXPORT
wxOKlibc()
120 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
121 // glibc 2.0 uses UTF-8 even when it shouldn't
123 if ((MB_CUR_MAX
== 2) &&
124 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
126 // this is UTF-8 allright, check whether that's what we want
127 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
128 if ((strlen(cur_locale
) < 4) ||
129 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
130 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
131 // nope, don't use libc conversion
139 // ============================================================================
140 // printf() functions business
141 // ============================================================================
143 // special test mode: define all functions below even if we don't really need
144 // them to be able to test them
155 #define wxNEED_WPRINTF
157 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
160 // ----------------------------------------------------------------------------
161 // implement [v]snprintf() if the system doesn't provide a safe one
162 // or if the system's one does not support positional parameters
163 // (very useful for i18n purposes)
164 // ----------------------------------------------------------------------------
166 #if !defined(wxVsnprintf_)
168 // wxUSE_STRUTILS says our wxVsnprintf_ implementation to use or not to
169 // use wxStrlen and wxStrncpy functions over one-char processing loops.
171 // Some benchmarking revealed that wxUSE_STRUTILS == 1 has the following
174 // when in ANSI mode, this setting does not change almost anything
175 // when in Unicode mode, it gives ~ 50% of slowdown !
177 // both in ANSI and Unicode mode it gives ~ 60% of speedup !
179 #if defined(WIN32) && wxUSE_UNICODE
180 #define wxUSE_STRUTILS 0
182 #define wxUSE_STRUTILS 1
185 // some limits of our implementation
186 #define wxMAX_SVNPRINTF_ARGUMENTS 64
187 #define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32
189 // the conversion specifiers accepted by wxVsnprintf_
190 enum wxPrintfArgType
{
193 wxPAT_INT
, // %d, %i, %o, %u, %x, %X
194 wxPAT_LONGINT
, // %ld, etc
196 wxPAT_LONGLONGINT
, // %Ld, etc
198 wxPAT_SIZET
, // %Zd, etc
200 wxPAT_DOUBLE
, // %e, %E, %f, %g, %G
201 wxPAT_LONGDOUBLE
, // %le, etc
205 wxPAT_CHAR
, // %hc (in ANSI mode: %c, too)
206 wxPAT_WCHAR
, // %lc (in Unicode mode: %c, too)
208 wxPAT_PCHAR
, // %s (related to a char *)
209 wxPAT_PWCHAR
, // %s (related to a wchar_t *)
212 wxPAT_NSHORTINT
, // %hn
213 wxPAT_NLONGINT
// %ln
216 // an argument passed to wxVsnprintf_
218 int pad_int
; // %d, %i, %o, %u, %x, %X
219 long int pad_longint
; // %ld, etc
221 long long int pad_longlongint
; // %Ld, etc
223 size_t pad_sizet
; // %Zd, etc
225 double pad_double
; // %e, %E, %f, %g, %G
226 long double pad_longdouble
; // %le, etc
228 void *pad_pointer
; // %p
230 char pad_char
; // %hc (in ANSI mode: %c, too)
231 wchar_t pad_wchar
; // %lc (in Unicode mode: %c, too)
233 char *pad_pchar
; // %s (related to a char *)
234 wchar_t *pad_pwchar
; // %s (related to a wchar_t *)
237 short int *pad_nshortint
; // %hn
238 long int *pad_nlongint
; // %ln
242 // Contains parsed data relative to a conversion specifier given to
243 // wxVsnprintf_ and parsed from the format string
244 // NOTE: in C++ there is almost no difference between struct & classes thus
245 // there is no performance gain by using a struct here...
246 class wxPrintfConvSpec
250 // the position of the argument relative to this conversion specifier
253 // the type of this conversion specifier
254 wxPrintfArgType type
;
256 // the minimum and maximum width
257 // when one of this var is set to -1 it means: use the following argument
258 // in the stack as minimum/maximum width for this conversion specifier
259 int min_width
, max_width
;
261 // does the argument need to the be aligned to left ?
264 // pointer to the '%' of this conversion specifier in the format string
265 // NOTE: this points somewhere in the string given to the Parse() function -
266 // it's task of the caller ensure that memory is still valid !
267 const wxChar
*argpos
;
269 // pointer to the last character of this conversion specifier in the
271 // NOTE: this points somewhere in the string given to the Parse() function -
272 // it's task of the caller ensure that memory is still valid !
273 const wxChar
*argend
;
275 // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse()
276 // for use in Process()
277 // NB: this buffer can be safely a char buffer instead of a wchar_t buffer
278 // since it's used only for numeric conversion specifier and always
280 char szFlags
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
285 // we don't declare this as a constructor otherwise it would be called
286 // automatically and we don't want this: to be optimized, wxVsnprintf_
287 // calls this function only on really-used instances of this class.
290 // Parses the first conversion specifier in the given string, which must
291 // begin with a '%'. Returns false if the first '%' does not introduce a
292 // (valid) conversion specifier and thus should be ignored.
293 bool Parse(const wxChar
*format
);
295 // Process this conversion specifier and puts the result in the given
296 // buffer. Returns the number of characters written in 'buf' or -1 if
297 // there's not enough space.
298 int Process(wxChar
*buf
, size_t lenMax
, wxPrintfArg
*p
);
300 // Loads the argument of this conversion specifier from given va_list.
301 bool LoadArg(wxPrintfArg
*p
, va_list &argptr
);
304 // An helper function of LoadArg() which is used to handle the '*' flag
305 void ReplaceAsteriskWith(int w
);
308 void wxPrintfConvSpec::Init()
314 argpos
= argend
= NULL
;
315 type
= wxPAT_INVALID
;
317 // this character will never be removed from szFlags array and
318 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
322 bool wxPrintfConvSpec::Parse(const wxChar
*format
)
326 // temporary parse data
328 bool in_prec
, prec_dot
;
331 adj_left
= in_prec
= prec_dot
= false;
332 argpos
= argend
= format
;
336 if (in_prec && !prec_dot) \
338 szFlags[flagofs++] = (char)'.'; \
343 const wxChar ch
= *(++argend
);
347 return false; // not really an argument
350 return false; // not really an argument
358 szFlags
[flagofs
++] = (char)ch
;
364 szFlags
[flagofs
++] = (char)ch
;
372 // dot will be auto-added to szFlags if non-negative
379 szFlags
[flagofs
++] = (char)ch
;
385 szFlags
[flagofs
++] = (char)ch
;
392 szFlags
[flagofs
++] = (char)ch
;
398 szFlags
[flagofs
++] = (char)ch
;
406 // tell Process() to use the next argument
407 // in the stack as maxwidth...
412 // tell Process() to use the next argument
413 // in the stack as minwidth...
417 // save the * in our formatting buffer...
418 // will be replaced later by Process()
419 szFlags
[flagofs
++] = (char)ch
;
422 case wxT('1'): case wxT('2'): case wxT('3'):
423 case wxT('4'): case wxT('5'): case wxT('6'):
424 case wxT('7'): case wxT('8'): case wxT('9'):
428 while ( (*argend
>= wxT('0')) &&
429 (*argend
<= wxT('9')) )
431 szFlags
[flagofs
++] = (char)(*argend
);
432 len
= len
*10 + (*argend
- wxT('0'));
441 argend
--; // the main loop pre-increments n again
445 case wxT('$'): // a positional parameter (e.g. %2$s) ?
448 break; // ignore this formatting flag as no
449 // numbers are preceding it
451 // remove from szFlags all digits previously added
454 } while (szFlags
[flagofs
] >= '1' &&
455 szFlags
[flagofs
] <= '9');
457 // re-adjust the offset making it point to the
458 // next free char of szFlags
473 szFlags
[flagofs
++] = (char)ch
;
474 szFlags
[flagofs
] = (char)'\0';
478 // NB: 'short int' value passed through '...'
479 // is promoted to 'int', so we have to get
480 // an int from stack even if we need a short
483 type
= wxPAT_LONGINT
;
486 type
= wxPAT_LONGLONGINT
;
488 type
= wxPAT_LONGINT
;
489 #endif // long long/!long long
501 szFlags
[flagofs
++] = (char)ch
;
502 szFlags
[flagofs
] = (char)'\0';
504 type
= wxPAT_LONGDOUBLE
;
511 type
= wxPAT_POINTER
;
518 // in Unicode mode %hc == ANSI character
519 // and in ANSI mode, %hc == %c == ANSI...
524 // in ANSI mode %lc == Unicode character
525 // and in Unicode mode, %lc == %c == Unicode...
531 // in Unicode mode, %c == Unicode character
534 // in ANSI mode, %c == ANSI character
544 // Unicode mode wx extension: we'll let %hs mean non-Unicode
545 // strings (when in ANSI mode, %s == %hs == ANSI string)
550 // in Unicode mode, %ls == %s == Unicode string
551 // in ANSI mode, %ls == Unicode string
569 type
= wxPAT_NSHORTINT
;
571 type
= wxPAT_NLONGINT
;
576 // bad format, don't consider this an argument;
577 // leave it unchanged
583 return true; // parsing was successful
587 void wxPrintfConvSpec::ReplaceAsteriskWith(int w
)
589 char temp
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
591 // find the first * in our flag buffer
592 char *pwidth
= strchr(szFlags
, '*');
595 // save what follows the * (the +1 is to skip it!)
596 strcpy(temp
, pwidth
+1);
602 // replace * with the actual integer given as width
603 int offset
= ::sprintf(pwidth
,"%d",abs(w
));
605 // restore after the expanded * what was following it
606 strcpy(pwidth
+offset
, temp
);
609 bool wxPrintfConvSpec::LoadArg(wxPrintfArg
*p
, va_list &argptr
)
611 // did the '*' width/precision specifier was used ?
614 // take the maxwidth specifier from the stack
615 max_width
= va_arg(argptr
, int);
619 ReplaceAsteriskWith(max_width
);
624 // take the minwidth specifier from the stack
625 min_width
= va_arg(argptr
, int);
627 ReplaceAsteriskWith(min_width
);
630 adj_left
= !adj_left
;
631 min_width
= -min_width
;
637 p
->pad_int
= va_arg(argptr
, int);
640 p
->pad_longint
= va_arg(argptr
, long int);
643 case wxPAT_LONGLONGINT
:
644 p
->pad_longlongint
= va_arg(argptr
, long long int);
648 p
->pad_sizet
= va_arg(argptr
, size_t);
651 p
->pad_double
= va_arg(argptr
, double);
653 case wxPAT_LONGDOUBLE
:
654 p
->pad_longdouble
= va_arg(argptr
, long double);
657 p
->pad_pointer
= va_arg(argptr
, void *);
661 p
->pad_char
= (char)va_arg(argptr
, int); // char is promoted to int when passed through '...'
664 p
->pad_wchar
= (wchar_t)va_arg(argptr
, int); // char is promoted to int when passed through '...'
668 p
->pad_pchar
= va_arg(argptr
, char *);
671 p
->pad_pwchar
= va_arg(argptr
, wchar_t *);
675 p
->pad_nint
= va_arg(argptr
, int *);
677 case wxPAT_NSHORTINT
:
678 p
->pad_nshortint
= va_arg(argptr
, short int *);
681 p
->pad_nlongint
= va_arg(argptr
, long int *);
689 return true; // loading was successful
692 int wxPrintfConvSpec::Process(wxChar
*buf
, size_t lenMax
, wxPrintfArg
*p
)
694 // buffer to avoid dynamic memory allocation each time for small strings
695 static char szScratch
[1024];
698 #define APPEND_CH(ch) \
700 if ( lenCur == lenMax ) \
703 buf[lenCur++] = ch; \
706 #define APPEND_STR(s) \
708 for ( const wxChar *p = s; *p; p++ ) \
717 ::sprintf(szScratch
, szFlags
, p
->pad_int
);
721 ::sprintf(szScratch
, szFlags
, p
->pad_longint
);
725 case wxPAT_LONGLONGINT
:
726 ::sprintf(szScratch
, szFlags
, p
->pad_longlongint
);
728 #endif // SIZEOF_LONG_LONG
731 ::sprintf(szScratch
, szFlags
, p
->pad_sizet
);
734 case wxPAT_LONGDOUBLE
:
735 ::sprintf(szScratch
, szFlags
, p
->pad_longdouble
);
739 ::sprintf(szScratch
, szFlags
, p
->pad_double
);
743 ::sprintf(szScratch
, szFlags
, p
->pad_pointer
);
753 if (type
== wxPAT_CHAR
) {
754 // user passed a character explicitely indicated as ANSI...
755 const char buf
[2] = { p
->pad_char
, 0 };
756 val
= wxString(buf
, wxConvLibc
)[0u];
762 if (type
== wxPAT_WCHAR
) {
763 // user passed a character explicitely indicated as Unicode...
764 const wchar_t buf
[2] = { p
->pad_wchar
, 0 };
765 val
= wxString(buf
, wxConvLibc
)[0u];
773 for (i
= 1; i
< (size_t)min_width
; i
++)
779 for (i
= 1; i
< (size_t)min_width
; i
++)
792 if (type
== wxPAT_PCHAR
) {
793 // user passed a string explicitely indicated as ANSI...
794 val
= s
= wxString(p
->pad_pchar
, wxConvLibc
);
800 if (type
== wxPAT_PWCHAR
) {
801 // user passed a string explicitely indicated as Unicode...
802 val
= s
= wxString(p
->pad_pwchar
, wxConvLibc
);
811 // at this point we are sure that max_width is positive or null
812 // (see top of wxPrintfConvSpec::LoadArg)
813 len
= wxMin((unsigned int)max_width
, wxStrlen(val
));
815 for ( len
= 0; val
[len
] && (len
< max_width
); len
++ )
819 else if (max_width
>= 6)
834 for (i
= len
; i
< min_width
; i
++)
839 // at this point we are sure that max_width is positive or null
840 // (see top of wxPrintfConvSpec::LoadArg)
841 len
= wxMin((unsigned int)len
, lenMax
-lenCur
);
842 wxStrncpy(buf
+lenCur
, val
, len
);
845 for (i
= 0; i
< len
; i
++)
851 for (i
= len
; i
< min_width
; i
++)
858 *p
->pad_nint
= lenCur
;
861 case wxPAT_NSHORTINT
:
862 *p
->pad_nshortint
= (short int)lenCur
;
866 *p
->pad_nlongint
= lenCur
;
874 // if we used system's sprintf() then we now need to append the s_szScratch
875 // buffer to the given one...
881 case wxPAT_LONGLONGINT
:
884 case wxPAT_LONGDOUBLE
:
889 const wxMB2WXbuf tmp
= wxConvLibc
.cMB2WX(szScratch
);
890 size_t len
= wxMin(lenMax
, wxStrlen(tmp
));
891 wxStrncpy(buf
, tmp
, len
);
896 const wxMB2WXbuf tmp
=
897 wxConvLibc
.cMB2WX(szScratch
);
904 break; // all other cases were completed previously
910 // differences from standard strncpy:
911 // 1) copies everything from 'source' except for '%%' sequence which is copied as '%'
912 // 2) returns the number of written characters in 'dest' as it could differ from given 'n'
913 // 3) much less optimized, unfortunately...
914 static int wxCopyStrWithPercents(wxChar
*dest
, const wxChar
*source
, size_t n
)
922 for ( i
= 0; i
< n
-1; source
++, i
++)
924 dest
[written
++] = *source
;
925 if (*(source
+1) == wxT('%'))
927 // skip this additional '%' character
934 // copy last character inconditionally
935 dest
[written
++] = *source
;
940 int WXDLLEXPORT
wxVsnprintf_(wxChar
*buf
, size_t lenMax
,
941 const wxChar
*format
, va_list argptr
)
944 static wxPrintfConvSpec arg
[wxMAX_SVNPRINTF_ARGUMENTS
];
945 static wxPrintfArg argdata
[wxMAX_SVNPRINTF_ARGUMENTS
];
946 static wxPrintfConvSpec
*pspec
[wxMAX_SVNPRINTF_ARGUMENTS
] = { NULL
};
950 // number of characters in the buffer so far, must be less than lenMax
954 const wxChar
*toparse
= format
;
956 // parse the format string
957 bool posarg_present
= false, nonposarg_present
= false;
958 for (; *toparse
!= wxT('\0'); toparse
++)
960 if (*toparse
== wxT('%') )
964 // let's see if this is a (valid) conversion specifier...
965 if (arg
[nargs
].Parse(toparse
))
968 wxPrintfConvSpec
*current
= &arg
[nargs
];
970 // make toparse point to the end of this specifier
971 toparse
= current
->argend
;
973 if (current
->pos
> 0) {
974 // the positionals start from number 1... adjust the index
976 posarg_present
= true;
978 // not a positional argument...
979 current
->pos
= nargs
;
980 nonposarg_present
= true;
983 // this conversion specifier is tied to the pos-th argument...
984 pspec
[current
->pos
] = current
;
987 if (nargs
== wxMAX_SVNPRINTF_ARGUMENTS
)
988 break; // cannot handle any additional conv spec
993 if (posarg_present
&& nonposarg_present
)
994 return -1; // format strings with both positional and
995 // non-positional conversion specifier are unsupported !!
997 // on platforms where va_list is an array type, it is necessary to make a
998 // copy to be able to pass it to LoadArg as a reference.
1001 wxVaCopy(ap
, argptr
);
1003 // now load arguments from stack
1004 for (i
=0; i
< nargs
&& ok
; i
++) {
1005 // !pspec[i] if user forgot a positional parameter (e.g. %$1s %$3s) ?
1006 // or LoadArg false if wxPrintfConvSpec::Parse failed to set its 'type'
1007 // to a valid value...
1008 ok
= pspec
[i
] && pspec
[i
]->LoadArg(&argdata
[i
], ap
);
1013 // something failed while loading arguments from the variable list...
1017 // finally, process each conversion specifier with its own argument
1019 for (i
=0; i
< nargs
; i
++)
1021 // copy in the output buffer the portion of the format string between
1022 // last specifier and the current one
1023 size_t tocopy
= ( arg
[i
].argpos
- toparse
);
1024 if (lenCur
+tocopy
>= lenMax
)
1025 return -1; // not enough space in the output buffer !
1027 lenCur
+= wxCopyStrWithPercents(buf
+lenCur
, toparse
, tocopy
);
1029 // process this specifier directly in the output buffer
1030 int n
= arg
[i
].Process(buf
+lenCur
, lenMax
- lenCur
, &argdata
[arg
[i
].pos
]);
1032 return -1; // not enough space in the output buffer !
1035 // the +1 is because wxPrintfConvSpec::argend points to the last character
1036 // of the format specifier, but we are not interested to it...
1037 toparse
= arg
[i
].argend
+ 1;
1040 // copy portion of the format string after last specifier
1041 // NOTE: toparse is pointing to the character just after the last processed
1042 // conversion specifier
1043 // NOTE2: the +1 is because we want to copy also the '\0'
1044 size_t tocopy
= wxStrlen(format
) + 1 - ( toparse
- format
) ;
1045 if (lenCur
+tocopy
>= lenMax
)
1046 return -1; // not enough space in the output buffer !
1048 // the -1 is because of the '\0'
1049 lenCur
+= wxCopyStrWithPercents(buf
+lenCur
, toparse
, tocopy
) - 1;
1051 // clean the static array portion used...
1052 // NOTE: other arrays do not need cleanup!
1053 memset(pspec
, 0, sizeof(wxPrintfConvSpec
*)*nargs
);
1055 wxASSERT(lenCur
== wxStrlen(buf
));
1063 #endif // !wxVsnprintfA
1065 #if !defined(wxSnprintf_)
1066 int WXDLLEXPORT
wxSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
1069 va_start(argptr
, format
);
1071 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
1077 #endif // wxSnprintf_
1079 #if defined(__DMC__)
1080 /* Digital Mars adds count to _stprintf (C99) so convert */
1082 int wxSprintf (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
1086 va_start( arglist
, format
);
1087 int iLen
= swprintf ( s
, -1, format
, arglist
);
1092 #endif // wxUSE_UNICODE
1096 // ----------------------------------------------------------------------------
1097 // implement the standard IO functions for wide char if libc doesn't have them
1098 // ----------------------------------------------------------------------------
1101 int wxFputs(const wchar_t *ws
, FILE *stream
)
1103 // counting the number of wide characters written isn't worth the trouble,
1104 // simply distinguish between ok and error
1105 return fputs(wxConvLibc
.cWC2MB(ws
), stream
) == -1 ? -1 : 0;
1107 #endif // wxNEED_FPUTS
1110 int wxPuts(const wxChar
*ws
)
1112 int rc
= wxFputs(ws
, stdout
);
1115 if ( wxFputs(L
"\n", stdout
) == -1 )
1123 #endif // wxNEED_PUTS
1126 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
1128 wchar_t ws
[2] = { wc
, L
'\0' };
1130 return wxFputs(ws
, stream
);
1132 #endif // wxNEED_PUTC
1134 // NB: we only implement va_list functions here, the ones taking ... are
1135 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
1136 // the definitions there to avoid duplicating them here
1137 #ifdef wxNEED_WPRINTF
1139 // TODO: implement the scanf() functions
1140 int vwscanf(const wxChar
*format
, va_list argptr
)
1142 wxFAIL_MSG( _T("TODO") );
1147 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
1149 // The best we can do without proper Unicode support in glibc is to
1150 // convert the strings into MB representation and run ANSI version
1151 // of the function. This doesn't work with %c and %s because of difference
1152 // in size of char and wchar_t, though.
1154 wxCHECK_MSG( wxStrstr(format
, _T("%s")) == NULL
, -1,
1155 _T("incomplete vswscanf implementation doesn't allow %s") );
1156 wxCHECK_MSG( wxStrstr(format
, _T("%c")) == NULL
, -1,
1157 _T("incomplete vswscanf implementation doesn't allow %c") );
1160 wxVaCopy(argcopy
, argptr
);
1161 return vsscanf(wxConvLibc
.cWX2MB(ws
), wxConvLibc
.cWX2MB(format
), argcopy
);
1164 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
1166 wxFAIL_MSG( _T("TODO") );
1171 #define vswprintf wxVsnprintf_
1173 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
1176 int rc
= s
.PrintfV(format
, argptr
);
1180 // we can't do much better without Unicode support in libc...
1181 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
1188 int vwprintf(const wxChar
*format
, va_list argptr
)
1190 return wxVfprintf(stdout
, format
, argptr
);
1193 #endif // wxNEED_WPRINTF
1195 #ifdef wxNEED_PRINTF_CONVERSION
1197 // ----------------------------------------------------------------------------
1198 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
1199 // ----------------------------------------------------------------------------
1202 Here are the gory details. We want to follow the Windows/MS conventions,
1207 format specifier results in
1208 -----------------------------------
1210 %lc, %C, %lC wchar_t
1214 format specifier results in
1215 -----------------------------------
1217 %c, %lc, %lC wchar_t
1220 while on POSIX systems we have %C identical to %lc and %c always means char
1221 (in any mode) while %lc always means wchar_t,
1223 So to use native functions in order to get our semantics we must do the
1224 following translations in Unicode mode (nothing to do in ANSI mode):
1226 wxWidgets specifier POSIX specifier
1227 ----------------------------------------
1233 And, of course, the same should be done for %s as well.
1236 class wxFormatConverter
1239 wxFormatConverter(const wxChar
*format
);
1241 // notice that we only translated the string if m_fmtOrig == NULL (as set
1242 // by CopyAllBefore()), otherwise we should simply use the original format
1243 operator const wxChar
*() const
1244 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
1247 // copy another character to the translated format: this function does the
1248 // copy if we are translating but doesn't do anything at all if we don't,
1249 // so we don't create the translated format string at all unless we really
1250 // need to (i.e. InsertFmtChar() is called)
1251 wxChar
CopyFmtChar(wxChar ch
)
1255 // we're translating, do copy
1260 // simply increase the count which should be copied by
1261 // CopyAllBefore() later if needed
1268 // insert an extra character
1269 void InsertFmtChar(wxChar ch
)
1273 // so far we haven't translated anything yet
1280 void CopyAllBefore()
1282 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
1284 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
1286 // we won't need it any longer
1290 static bool IsFlagChar(wxChar ch
)
1292 return ch
== _T('-') || ch
== _T('+') ||
1293 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
1296 void SkipDigits(const wxChar
**ptpc
)
1298 while ( **ptpc
>= _T('0') && **ptpc
<= _T('9') )
1299 CopyFmtChar(*(*ptpc
)++);
1302 // the translated format
1305 // the original format
1306 const wxChar
*m_fmtOrig
;
1308 // the number of characters already copied
1312 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
1319 if ( CopyFmtChar(*format
++) == _T('%') )
1322 while ( IsFlagChar(*format
) )
1323 CopyFmtChar(*format
++);
1325 // and possible width
1326 if ( *format
== _T('*') )
1327 CopyFmtChar(*format
++);
1329 SkipDigits(&format
);
1332 if ( *format
== _T('.') )
1334 CopyFmtChar(*format
++);
1335 if ( *format
== _T('*') )
1336 CopyFmtChar(*format
++);
1338 SkipDigits(&format
);
1341 // next we can have a size modifier
1357 // "ll" has a different meaning!
1358 if ( format
[1] != _T('l') )
1364 //else: fall through
1370 // and finally we should have the type
1375 // %C and %hC -> %c and %lC -> %lc
1377 CopyFmtChar(_T('l'));
1379 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
1384 // %c -> %lc but %hc stays %hc and %lc is still %lc
1385 if ( size
== Default
)
1386 InsertFmtChar(_T('l'));
1390 // nothing special to do
1391 if ( size
!= Default
)
1392 CopyFmtChar(*(format
- 1));
1393 CopyFmtChar(*format
++);
1399 #else // !wxNEED_PRINTF_CONVERSION
1400 // no conversion necessary
1401 #define wxFormatConverter(x) (x)
1402 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
1405 // For testing the format converter
1406 wxString
wxConvertFormat(const wxChar
*format
)
1408 return wxString(wxFormatConverter(format
));
1412 // ----------------------------------------------------------------------------
1413 // wxPrintf(), wxScanf() and relatives
1414 // ----------------------------------------------------------------------------
1416 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
1418 int wxScanf( const wxChar
*format
, ... )
1421 va_start(argptr
, format
);
1423 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
1430 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... )
1433 va_start(argptr
, format
);
1435 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
1442 int wxFscanf( FILE *stream
, const wxChar
*format
, ... )
1445 va_start(argptr
, format
);
1446 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
1453 int wxPrintf( const wxChar
*format
, ... )
1456 va_start(argptr
, format
);
1458 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
1466 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... )
1469 va_start(argptr
, format
);
1471 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
1477 #endif // wxSnprintf
1479 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... )
1482 va_start(argptr
, format
);
1484 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
1485 // it's safe to implement this one in terms of it
1486 wxString
s(wxString::FormatV(format
, argptr
));
1494 int wxFprintf( FILE *stream
, const wxChar
*format
, ... )
1497 va_start( argptr
, format
);
1499 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
1506 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
1508 return vswscanf( str
, wxFormatConverter(format
), argptr
);
1511 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
1513 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
1516 int wxVprintf( const wxChar
*format
, va_list argptr
)
1518 return vwprintf( wxFormatConverter(format
), argptr
);
1522 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
1524 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
1526 #endif // wxVsnprintf
1528 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
1530 // same as for wxSprintf()
1531 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
1534 #endif // wxNEED_PRINTF_CONVERSION
1538 // ----------------------------------------------------------------------------
1539 // ctype.h stuff (currently unused)
1540 // ----------------------------------------------------------------------------
1542 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
1543 inline WORD
wxMSW_ctype(wxChar ch
)
1546 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
1550 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
1551 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
1552 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
1553 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
1554 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
1555 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
1556 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
1557 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
1558 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
1559 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
1560 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
1561 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
1562 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
1565 #ifdef wxNEED_WX_MBSTOWCS
1567 WXDLLEXPORT
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
1577 const char* origin
= in
;
1579 while (outlen
-- && *in
)
1581 *out
++ = (wchar_t) *in
++;
1589 WXDLLEXPORT
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
1599 const wchar_t* origin
= in
;
1601 while (outlen
-- && *in
)
1603 *out
++ = (char) *in
++;
1611 #endif // wxNEED_WX_MBSTOWCS
1613 #if defined(wxNEED_WX_CTYPE_H)
1615 #include <CoreFoundation/CoreFoundation.h>
1617 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
1618 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
1619 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
1620 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
1621 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
1622 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
1623 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
1624 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
1625 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
1626 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
1628 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalnumset
, ch
); }
1629 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalphaset
, ch
); }
1630 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
1631 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfdigitset
, ch
); }
1632 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
) && ch
!= ' '; }
1633 WXDLLEXPORT
int wxIslower(wxChar ch
) { return CFCharacterSetIsCharacterMember(cflowerset
, ch
); }
1634 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
1635 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfpunctset
, ch
); }
1636 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfspaceset
, ch
); }
1637 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfupperset
, ch
); }
1638 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxIsdigit(ch
) || (ch
>='a' && ch
<='f') || (ch
>='A' && ch
<='F'); }
1639 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)tolower((char)(ch
)); }
1640 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)toupper((char)(ch
)); }
1642 #endif // wxNEED_WX_CTYPE_H
1646 WXDLLEXPORT
char *wxStrdupA(const char *s
)
1648 return strcpy((char *)malloc(strlen(s
) + 1), s
);
1655 WXDLLEXPORT
wchar_t * wxStrdupW(const wchar_t *pwz
)
1657 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
1658 wchar_t *ret
= (wchar_t *) malloc(size
);
1659 memcpy(ret
, pwz
, size
);
1666 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
1668 register wxChar c1
, c2
;
1670 c1
= wxTolower(*psz1
++);
1671 c2
= wxTolower(*psz2
++);
1672 } while ( c1
&& (c1
== c2
) );
1678 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1680 // initialize the variables just to suppress stupid gcc warning
1681 register wxChar c1
= 0, c2
= 0;
1682 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1684 if (c1
< c2
) return -1;
1685 if (c1
> c2
) return 1;
1692 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
1694 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
1696 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
1700 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
1701 WXDLLEXPORT
size_t wxWcslen(const wchar_t *s
)
1711 // ----------------------------------------------------------------------------
1712 // string.h functions
1713 // ----------------------------------------------------------------------------
1715 #ifdef wxNEED_WX_STRING_H
1717 // RN: These need to be c externed for the regex lib
1722 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
1725 while (*dest
) dest
++;
1726 while ((*dest
++ = *src
++));
1730 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
1732 // be careful here as the terminating NUL makes part of the string
1742 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
1744 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
1745 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1746 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1750 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
1753 while ((*dest
++ = *src
++));
1757 WXDLLEXPORT
size_t wxStrlen_(const wxChar
*s
)
1767 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1770 while (*dest
) dest
++;
1771 while (n
&& (*dest
++ = *src
++)) n
--;
1775 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1777 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1779 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1780 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1785 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1788 while (n
&& (*dest
++ = *src
++)) n
--;
1789 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1793 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1795 while (*s
&& !wxStrchr(accept
, *s
))
1798 return *s
? s
: NULL
;
1801 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1803 const wxChar
*ret
= NULL
;
1815 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1818 while (wxStrchr(accept
, *s
++)) len
++;
1822 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1824 wxASSERT_MSG( needle
!= NULL
, _T("NULL argument in wxStrstr") );
1826 // VZ: this is not exactly the most efficient string search algorithm...
1828 const size_t len
= wxStrlen(needle
);
1830 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1832 if ( !wxStrncmp(fnd
, needle
, len
) )
1845 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1847 const wxChar
*start
= nptr
;
1849 // FIXME: only correct for C locale
1850 while (wxIsspace(*nptr
)) nptr
++;
1851 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1852 while (wxIsdigit(*nptr
)) nptr
++;
1853 if (*nptr
== wxT('.')) {
1855 while (wxIsdigit(*nptr
)) nptr
++;
1857 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1859 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1860 while (wxIsdigit(*nptr
)) nptr
++;
1863 wxString
data(nptr
, nptr
-start
);
1864 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1865 char *rdat
= wxMBSTRINGCAST dat
;
1866 double ret
= strtod(dat
, &rdat
);
1868 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1873 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1875 const wxChar
*start
= nptr
;
1877 // FIXME: only correct for C locale
1878 while (wxIsspace(*nptr
)) nptr
++;
1879 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1880 if (((base
== 0) || (base
== 16)) &&
1881 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1885 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1886 else if (base
== 0) base
= 10;
1888 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1889 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1891 wxString
data(start
, nptr
-start
);
1892 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1893 char *rdat
= wxMBSTRINGCAST dat
;
1894 long int ret
= strtol(dat
, &rdat
, base
);
1896 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1901 WXDLLEXPORT
unsigned long int wxStrtoul(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1903 return (unsigned long int) wxStrtol(nptr
, endptr
, base
);
1906 #endif // wxNEED_WX_STRING_H
1908 #ifdef wxNEED_WX_STDIO_H
1909 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1911 char mode_buffer
[10];
1912 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1913 mode_buffer
[i
] = (char) mode
[i
];
1915 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1918 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1920 char mode_buffer
[10];
1921 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1922 mode_buffer
[i
] = (char) mode
[i
];
1924 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1927 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1929 return remove( wxConvFile
.cWX2MB(path
) );
1932 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1934 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1939 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1944 if (str
.ToDouble(& d
))
1949 return atof(wxConvLibc
.cWX2MB(psz
));
1954 #ifdef wxNEED_WX_STDLIB_H
1955 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1957 return atoi(wxConvLibc
.cWX2MB(psz
));
1960 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1962 return atol(wxConvLibc
.cWX2MB(psz
));
1965 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1968 // NB: buffer returned by getenv() is allowed to be overwritten next
1969 // time getenv() is called, so it is OK to use static string
1970 // buffer to hold the data.
1971 static wxWCharBuffer
value((wxChar
*)NULL
);
1972 value
= wxConvLibc
.cMB2WX(getenv(wxConvLibc
.cWX2MB(name
)));
1973 return value
.data();
1975 return getenv(name
);
1979 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1981 return system(wxConvLibc
.cWX2MB(psz
));
1984 #endif // wxNEED_WX_STDLIB_H
1986 #ifdef wxNEED_WX_TIME_H
1988 wxStrftime(wxChar
*s
, size_t maxsize
, const wxChar
*fmt
, const struct tm
*tm
)
1993 wxCharBuffer
buf(maxsize
);
1995 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
1999 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
2003 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
2007 wxStrncpy(s
, wbuf
, maxsize
);
2010 #endif // wxNEED_WX_TIME_H
2013 WXDLLEXPORT wxChar
*wxCtime(const time_t *timep
)
2015 // normally the string is 26 chars but give one more in case some broken
2016 // DOS compiler decides to use "\r\n" instead of "\n" at the end
2017 static wxChar buf
[27];
2019 // ctime() is guaranteed to return a string containing only ASCII
2020 // characters, as its format is always the same for any locale
2021 wxStrncpy(buf
, wxString::FromAscii(ctime(timep
)), WXSIZEOF(buf
));
2022 buf
[WXSIZEOF(buf
) - 1] = _T('\0');
2028 #endif // wxUSE_WCHAR_T
2030 // ----------------------------------------------------------------------------
2031 // functions which we may need even if !wxUSE_WCHAR_T
2032 // ----------------------------------------------------------------------------
2036 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
2045 psz
+= wxStrspn(psz
, delim
);
2048 *save_ptr
= (wxChar
*)NULL
;
2049 return (wxChar
*)NULL
;
2053 psz
= wxStrpbrk(psz
, delim
);
2056 *save_ptr
= (wxChar
*)NULL
;
2061 *save_ptr
= psz
+ 1;
2069 // ----------------------------------------------------------------------------
2070 // missing C RTL functions
2071 // ----------------------------------------------------------------------------
2073 #ifdef wxNEED_STRDUP
2075 char *strdup(const char *s
)
2077 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
2079 strcpy( dest
, s
) ;
2082 #endif // wxNEED_STRDUP
2084 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
2086 void *calloc( size_t num
, size_t size
)
2088 void** ptr
= (void **)malloc(num
* size
);
2089 memset( ptr
, 0, num
* size
);
2093 #endif // __WXWINCE__ <= 211
2097 int wxRemove(const wxChar
*path
)
2099 return ::DeleteFile(path
) == 0;