added our own implementation of strto[u]ll() if the system doesn't have one (patch...
[wxWidgets.git] / src / common / wxcrt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wxcrt.cpp
3 // Purpose: wxChar CRT wrappers implementation
4 // Author: Ove Kaven
5 // Modified by: Ron Lee, Francesco Montorsi
6 // Created: 09/04/99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
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__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/wxchar.h"
24
25 #define _ISOC9X_SOURCE 1 // to get vsscanf()
26 #define _BSD_SOURCE 1 // to still get strdup()
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifndef __WXWINCE__
33 #include <time.h>
34 #include <locale.h>
35 #else
36 #include "wx/msw/wince/time.h"
37 #endif
38
39 #ifndef WX_PRECOMP
40 #include "wx/string.h"
41 #include "wx/hash.h"
42 #include "wx/utils.h" // for wxMin and wxMax
43 #include "wx/log.h"
44 #endif
45
46 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
47 #include <windef.h>
48 #include <winbase.h>
49 #include <winnls.h>
50 #include <winnt.h>
51 #endif
52
53 #ifndef wxStrtoll
54 #ifdef __WXWINCE__
55 // there is no errno.h under CE apparently
56 #define wxSET_ERRNO(value)
57 #else
58 #include <errno.h>
59
60 #define wxSET_ERRNO(value) errno = value
61 #endif
62 #endif
63
64 #if defined(__MWERKS__) && __MSL__ >= 0x6000
65 namespace std {}
66 using namespace std ;
67 #endif
68
69 #if wxUSE_WCHAR_T
70 size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n)
71 {
72 // assume that we have mbsrtowcs() too if we have wcsrtombs()
73 #ifdef HAVE_WCSRTOMBS
74 mbstate_t mbstate;
75 memset(&mbstate, 0, sizeof(mbstate_t));
76 #endif
77
78 if (buf) {
79 if (!n || !*psz) {
80 if (n) *buf = wxT('\0');
81 return 0;
82 }
83 #ifdef HAVE_WCSRTOMBS
84 return mbsrtowcs(buf, &psz, n, &mbstate);
85 #else
86 return wxMbstowcs(buf, psz, n);
87 #endif
88 }
89
90 // note that we rely on common (and required by Unix98 but unfortunately not
91 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
92 // to just get the size of the needed buffer -- this is needed as otherwise
93 // we have no idea about how much space we need and if the CRT doesn't
94 // support it (the only currently known example being Metrowerks, see
95 // wx/wxchar.h) we don't use its mbstowcs() at all
96 #ifdef HAVE_WCSRTOMBS
97 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
98 #else
99 return wxMbstowcs((wchar_t *) NULL, psz, 0);
100 #endif
101 }
102
103 size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
104 {
105 #ifdef HAVE_WCSRTOMBS
106 mbstate_t mbstate;
107 memset(&mbstate, 0, sizeof(mbstate_t));
108 #endif
109
110 if (buf) {
111 if (!n || !*pwz) {
112 // glibc2.1 chokes on null input
113 if (n) *buf = '\0';
114 return 0;
115 }
116 #ifdef HAVE_WCSRTOMBS
117 return wcsrtombs(buf, &pwz, n, &mbstate);
118 #else
119 return wxWcstombs(buf, pwz, n);
120 #endif
121 }
122
123 #ifdef HAVE_WCSRTOMBS
124 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
125 #else
126 return wxWcstombs((char *) NULL, pwz, 0);
127 #endif
128 }
129 #endif // wxUSE_WCHAR_T
130
131 bool WXDLLEXPORT wxOKlibc()
132 {
133 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
134 // glibc 2.0 uses UTF-8 even when it shouldn't
135 wchar_t res = 0;
136 if ((MB_CUR_MAX == 2) &&
137 (wxMB2WC(&res, "\xdd\xa5", 1) == 1) &&
138 (res==0x765)) {
139 // this is UTF-8 allright, check whether that's what we want
140 char *cur_locale = setlocale(LC_CTYPE, NULL);
141 if ((strlen(cur_locale) < 4) ||
142 (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) ||
143 (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) {
144 // nope, don't use libc conversion
145 return false;
146 }
147 }
148 #endif
149 return true;
150 }
151
152 // ============================================================================
153 // printf() functions business
154 // ============================================================================
155
156 // special test mode: define all functions below even if we don't really need
157 // them to be able to test them
158 #ifdef wxTEST_PRINTF
159 #undef wxFprintf
160 #undef wxPrintf
161 #undef wxSprintf
162 #undef wxVfprintf
163 #undef wxVsprintf
164 #undef wxVprintf
165 #undef wxVsnprintf_
166 #undef wxSnprintf_
167
168 #define wxNEED_WPRINTF
169
170 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr );
171 #endif
172
173 #if !defined(wxSnprintf_)
174 int WXDLLEXPORT wxDoSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...)
175 {
176 va_list argptr;
177 va_start(argptr, format);
178
179 int iLen = wxVsnprintf_(buf, len, format, argptr);
180
181 va_end(argptr);
182
183 return iLen;
184 }
185 #endif // wxSnprintf_
186
187 #if defined(__DMC__)
188 /* Digital Mars adds count to _stprintf (C99) so convert */
189 #if wxUSE_UNICODE
190 int wxDoSprintf (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... )
191 {
192 va_list arglist;
193
194 va_start( arglist, format );
195 int iLen = swprintf ( s, -1, format, arglist );
196 va_end( arglist );
197 return iLen ;
198 }
199
200 #endif // wxUSE_UNICODE
201
202 #endif //__DMC__
203
204 // ----------------------------------------------------------------------------
205 // implement the standard IO functions for wide char if libc doesn't have them
206 // ----------------------------------------------------------------------------
207
208 #ifdef wxNEED_FPUTS
209 int wxFputs(const wchar_t *ws, FILE *stream)
210 {
211 wxCharBuffer buf(wxConvLibc.cWC2MB(ws));
212 if ( !buf )
213 return -1;
214
215 // counting the number of wide characters written isn't worth the trouble,
216 // simply distinguish between ok and error
217 return fputs(buf, stream) == -1 ? -1 : 0;
218 }
219 #endif // wxNEED_FPUTS
220
221 #ifdef wxNEED_PUTS
222 int wxPuts(const wxChar *ws)
223 {
224 int rc = wxFputs(ws, stdout);
225 if ( rc != -1 )
226 {
227 if ( wxFputs(L"\n", stdout) == -1 )
228 return -1;
229
230 rc++;
231 }
232
233 return rc;
234 }
235 #endif // wxNEED_PUTS
236
237 #ifdef wxNEED_PUTC
238 int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream)
239 {
240 wchar_t ws[2] = { wc, L'\0' };
241
242 return wxFputs(ws, stream);
243 }
244 #endif // wxNEED_PUTC
245
246 // NB: we only implement va_list functions here, the ones taking ... are
247 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
248 // the definitions there to avoid duplicating them here
249 #ifdef wxNEED_WPRINTF
250
251 // TODO: implement the scanf() functions
252 int vwscanf(const wxChar *format, va_list argptr)
253 {
254 wxFAIL_MSG( _T("TODO") );
255
256 return -1;
257 }
258
259 int vswscanf(const wxChar *ws, const wxChar *format, va_list argptr)
260 {
261 // The best we can do without proper Unicode support in glibc is to
262 // convert the strings into MB representation and run ANSI version
263 // of the function. This doesn't work with %c and %s because of difference
264 // in size of char and wchar_t, though.
265
266 wxCHECK_MSG( wxStrstr(format, _T("%s")) == NULL, -1,
267 _T("incomplete vswscanf implementation doesn't allow %s") );
268 wxCHECK_MSG( wxStrstr(format, _T("%c")) == NULL, -1,
269 _T("incomplete vswscanf implementation doesn't allow %c") );
270
271 va_list argcopy;
272 wxVaCopy(argcopy, argptr);
273 return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argcopy);
274 }
275
276 int vfwscanf(FILE *stream, const wxChar *format, va_list argptr)
277 {
278 wxFAIL_MSG( _T("TODO") );
279
280 return -1;
281 }
282
283 #define vswprintf wxVsnprintf_
284
285 int vfwprintf(FILE *stream, const wxChar *format, va_list argptr)
286 {
287 wxString s;
288 int rc = s.PrintfV(format, argptr);
289
290 if ( rc != -1 )
291 {
292 // we can't do much better without Unicode support in libc...
293 if ( fprintf(stream, "%s", (const char*)s.mb_str() ) == -1 )
294 return -1;
295 }
296
297 return rc;
298 }
299
300 int vwprintf(const wxChar *format, va_list argptr)
301 {
302 return wxVfprintf(stdout, format, argptr);
303 }
304
305 #endif // wxNEED_WPRINTF
306
307 #ifdef wxNEED_PRINTF_CONVERSION
308
309 // ----------------------------------------------------------------------------
310 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
311 // ----------------------------------------------------------------------------
312
313 /*
314 Here are the gory details. We want to follow the Windows/MS conventions,
315 that is to have
316
317 In ANSI mode:
318
319 format specifier results in
320 -----------------------------------
321 %c, %hc, %hC char
322 %lc, %C, %lC wchar_t
323
324 In Unicode mode:
325
326 format specifier results in
327 -----------------------------------
328 %hc, %C, %hC char
329 %c, %lc, %lC wchar_t
330
331
332 while on POSIX systems we have %C identical to %lc and %c always means char
333 (in any mode) while %lc always means wchar_t,
334
335 So to use native functions in order to get our semantics we must do the
336 following translations in Unicode mode (nothing to do in ANSI mode):
337
338 wxWidgets specifier POSIX specifier
339 ----------------------------------------
340
341 %hc, %C, %hC %c
342 %c %lc
343
344
345 And, of course, the same should be done for %s as well.
346 */
347
348 class wxFormatConverter
349 {
350 public:
351 wxFormatConverter(const wxChar *format);
352
353 // notice that we only translated the string if m_fmtOrig == NULL (as set
354 // by CopyAllBefore()), otherwise we should simply use the original format
355 operator const wxChar *() const
356 { return m_fmtOrig ? m_fmtOrig : m_fmt.c_str(); }
357
358 private:
359 // copy another character to the translated format: this function does the
360 // copy if we are translating but doesn't do anything at all if we don't,
361 // so we don't create the translated format string at all unless we really
362 // need to (i.e. InsertFmtChar() is called)
363 wxChar CopyFmtChar(wxChar ch)
364 {
365 if ( !m_fmtOrig )
366 {
367 // we're translating, do copy
368 m_fmt += ch;
369 }
370 else
371 {
372 // simply increase the count which should be copied by
373 // CopyAllBefore() later if needed
374 m_nCopied++;
375 }
376
377 return ch;
378 }
379
380 // insert an extra character
381 void InsertFmtChar(wxChar ch)
382 {
383 if ( m_fmtOrig )
384 {
385 // so far we haven't translated anything yet
386 CopyAllBefore();
387 }
388
389 m_fmt += ch;
390 }
391
392 void CopyAllBefore()
393 {
394 wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") );
395
396 m_fmt = wxString(m_fmtOrig, m_nCopied);
397
398 // we won't need it any longer
399 m_fmtOrig = NULL;
400 }
401
402 static bool IsFlagChar(wxChar ch)
403 {
404 return ch == _T('-') || ch == _T('+') ||
405 ch == _T('0') || ch == _T(' ') || ch == _T('#');
406 }
407
408 void SkipDigits(const wxChar **ptpc)
409 {
410 while ( **ptpc >= _T('0') && **ptpc <= _T('9') )
411 CopyFmtChar(*(*ptpc)++);
412 }
413
414 // the translated format
415 wxString m_fmt;
416
417 // the original format
418 const wxChar *m_fmtOrig;
419
420 // the number of characters already copied
421 size_t m_nCopied;
422 };
423
424 wxFormatConverter::wxFormatConverter(const wxChar *format)
425 {
426 m_fmtOrig = format;
427 m_nCopied = 0;
428
429 while ( *format )
430 {
431 if ( CopyFmtChar(*format++) == _T('%') )
432 {
433 // skip any flags
434 while ( IsFlagChar(*format) )
435 CopyFmtChar(*format++);
436
437 // and possible width
438 if ( *format == _T('*') )
439 CopyFmtChar(*format++);
440 else
441 SkipDigits(&format);
442
443 // precision?
444 if ( *format == _T('.') )
445 {
446 CopyFmtChar(*format++);
447 if ( *format == _T('*') )
448 CopyFmtChar(*format++);
449 else
450 SkipDigits(&format);
451 }
452
453 // next we can have a size modifier
454 enum
455 {
456 Default,
457 Short,
458 Long
459 } size;
460
461 switch ( *format )
462 {
463 case _T('h'):
464 size = Short;
465 format++;
466 break;
467
468 case _T('l'):
469 // "ll" has a different meaning!
470 if ( format[1] != _T('l') )
471 {
472 size = Long;
473 format++;
474 break;
475 }
476 //else: fall through
477
478 default:
479 size = Default;
480 }
481
482 // and finally we should have the type
483 switch ( *format )
484 {
485 case _T('C'):
486 case _T('S'):
487 // %C and %hC -> %c and %lC -> %lc
488 if ( size == Long )
489 CopyFmtChar(_T('l'));
490
491 InsertFmtChar(*format++ == _T('C') ? _T('c') : _T('s'));
492 break;
493
494 case _T('c'):
495 case _T('s'):
496 // %c -> %lc but %hc stays %hc and %lc is still %lc
497 if ( size == Default)
498 InsertFmtChar(_T('l'));
499 // fall through
500
501 default:
502 // nothing special to do
503 if ( size != Default )
504 CopyFmtChar(*(format - 1));
505 CopyFmtChar(*format++);
506 }
507 }
508 }
509 }
510
511 #else // !wxNEED_PRINTF_CONVERSION
512 // no conversion necessary
513 #define wxFormatConverter(x) (x)
514 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
515
516 #ifdef __WXDEBUG__
517 // For testing the format converter
518 wxString wxConvertFormat(const wxChar *format)
519 {
520 return wxString(wxFormatConverter(format));
521 }
522 #endif
523
524 // ----------------------------------------------------------------------------
525 // wxPrintf(), wxScanf() and relatives
526 // ----------------------------------------------------------------------------
527
528 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
529
530 int wxDoScanf( const wxChar *format, ... )
531 {
532 va_list argptr;
533 va_start(argptr, format);
534
535 int ret = vwscanf(wxFormatConverter(format), argptr );
536
537 va_end(argptr);
538
539 return ret;
540 }
541
542 int wxDoSscanf( const wxChar *str, const wxChar *format, ... )
543 {
544 va_list argptr;
545 va_start(argptr, format);
546
547 int ret = vswscanf( str, wxFormatConverter(format), argptr );
548
549 va_end(argptr);
550
551 return ret;
552 }
553
554 int wxDoFscanf( FILE *stream, const wxChar *format, ... )
555 {
556 va_list argptr;
557 va_start(argptr, format);
558 int ret = vfwscanf(stream, wxFormatConverter(format), argptr);
559
560 va_end(argptr);
561
562 return ret;
563 }
564
565 int wxDoPrintf( const wxChar *format, ... )
566 {
567 va_list argptr;
568 va_start(argptr, format);
569
570 int ret = vwprintf( wxFormatConverter(format), argptr );
571
572 va_end(argptr);
573
574 return ret;
575 }
576
577 #ifndef wxSnprintf
578 int wxDoSnprintf( wxChar *str, size_t size, const wxChar *format, ... )
579 {
580 va_list argptr;
581 va_start(argptr, format);
582
583 int ret = vswprintf( str, size, wxFormatConverter(format), argptr );
584
585 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
586 // doesn't nul terminate on truncation.
587 str[size - 1] = 0;
588
589 va_end(argptr);
590
591 return ret;
592 }
593 #endif // wxSnprintf
594
595 int wxDoSprintf( wxChar *str, const wxChar *format, ... )
596 {
597 va_list argptr;
598 va_start(argptr, format);
599
600 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
601 // it's safe to implement this one in terms of it
602 wxString s(wxString::FormatV(format, argptr));
603 wxStrcpy(str, s);
604
605 va_end(argptr);
606
607 return s.length();
608 }
609
610 int wxDoFprintf( FILE *stream, const wxChar *format, ... )
611 {
612 va_list argptr;
613 va_start( argptr, format );
614
615 int ret = vfwprintf( stream, wxFormatConverter(format), argptr );
616
617 va_end(argptr);
618
619 return ret;
620 }
621
622 int wxVsscanf( const wxChar *str, const wxChar *format, va_list argptr )
623 {
624 return vswscanf( str, wxFormatConverter(format), argptr );
625 }
626
627 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr )
628 {
629 return vfwprintf( stream, wxFormatConverter(format), argptr );
630 }
631
632 int wxVprintf( const wxChar *format, va_list argptr )
633 {
634 return vwprintf( wxFormatConverter(format), argptr );
635 }
636
637 #ifndef wxVsnprintf
638 int wxVsnprintf( wxChar *str, size_t size, const wxChar *format, va_list argptr )
639 {
640 return vswprintf( str, size, wxFormatConverter(format), argptr );
641 }
642 #endif // wxVsnprintf
643
644 int wxVsprintf( wxChar *str, const wxChar *format, va_list argptr )
645 {
646 // same as for wxSprintf()
647 return vswprintf(str, INT_MAX / 4, wxFormatConverter(format), argptr);
648 }
649
650 #endif // wxNEED_PRINTF_CONVERSION
651
652 #if wxUSE_WCHAR_T
653
654 // ----------------------------------------------------------------------------
655 // ctype.h stuff (currently unused)
656 // ----------------------------------------------------------------------------
657
658 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
659 inline WORD wxMSW_ctype(wxChar ch)
660 {
661 WORD ret;
662 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
663 return ret;
664 }
665
666 WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); }
667 WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); }
668 WXDLLEXPORT int wxIscntrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; }
669 WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; }
670 WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); }
671 WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); }
672 WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); }
673 WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; }
674 WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; }
675 WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); }
676 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; }
677 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
678 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
679 #endif
680
681 #ifdef wxNEED_WX_MBSTOWCS
682
683 WXDLLEXPORT size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen)
684 {
685 if (!out)
686 {
687 size_t outsize = 0;
688 while(*in++)
689 outsize++;
690 return outsize;
691 }
692
693 const char* origin = in;
694
695 while (outlen-- && *in)
696 {
697 *out++ = (wchar_t) *in++;
698 }
699
700 *out = '\0';
701
702 return in - origin;
703 }
704
705 WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen)
706 {
707 if (!out)
708 {
709 size_t outsize = 0;
710 while(*in++)
711 outsize++;
712 return outsize;
713 }
714
715 const wchar_t* origin = in;
716
717 while (outlen-- && *in)
718 {
719 *out++ = (char) *in++;
720 }
721
722 *out = '\0';
723
724 return in - origin;
725 }
726
727 #endif // wxNEED_WX_MBSTOWCS
728
729 #if defined(wxNEED_WX_CTYPE_H)
730
731 #include <CoreFoundation/CoreFoundation.h>
732
733 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
734 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
735 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
736 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
737 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
738 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
739 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
740 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
741 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
742 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
743
744 WXDLLEXPORT int wxIsalnum(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalnumset, ch); }
745 WXDLLEXPORT int wxIsalpha(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalphaset, ch); }
746 WXDLLEXPORT int wxIscntrl(wxChar ch) { return CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
747 WXDLLEXPORT int wxIsdigit(wxChar ch) { return CFCharacterSetIsCharacterMember(cfdigitset, ch); }
748 WXDLLEXPORT int wxIsgraph(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch) && ch != ' '; }
749 WXDLLEXPORT int wxIslower(wxChar ch) { return CFCharacterSetIsCharacterMember(cflowerset, ch); }
750 WXDLLEXPORT int wxIsprint(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
751 WXDLLEXPORT int wxIspunct(wxChar ch) { return CFCharacterSetIsCharacterMember(cfpunctset, ch); }
752 WXDLLEXPORT int wxIsspace(wxChar ch) { return CFCharacterSetIsCharacterMember(cfspaceset, ch); }
753 WXDLLEXPORT int wxIsupper(wxChar ch) { return CFCharacterSetIsCharacterMember(cfupperset, ch); }
754 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxIsdigit(ch) || (ch>='a' && ch<='f') || (ch>='A' && ch<='F'); }
755 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)tolower((char)(ch)); }
756 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)toupper((char)(ch)); }
757
758 #endif // wxNEED_WX_CTYPE_H
759
760 #ifndef wxStrdupA
761
762 WXDLLEXPORT char *wxStrdupA(const char *s)
763 {
764 return strcpy((char *)malloc(strlen(s) + 1), s);
765 }
766
767 #endif // wxStrdupA
768
769 #ifndef wxStrdupW
770
771 WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz)
772 {
773 size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t);
774 wchar_t *ret = (wchar_t *) malloc(size);
775 memcpy(ret, pwz, size);
776 return ret;
777 }
778
779 #endif // wxStrdupW
780
781 #ifndef wxStricmp
782 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
783 {
784 register wxChar c1, c2;
785 do {
786 c1 = wxTolower(*psz1++);
787 c2 = wxTolower(*psz2++);
788 } while ( c1 && (c1 == c2) );
789 return c1 - c2;
790 }
791 #endif
792
793 #ifndef wxStricmp
794 int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n)
795 {
796 // initialize the variables just to suppress stupid gcc warning
797 register wxChar c1 = 0, c2 = 0;
798 while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
799 if (n) {
800 if (c1 < c2) return -1;
801 if (c1 > c2) return 1;
802 }
803 return 0;
804 }
805 #endif
806
807 #ifndef wxSetlocale
808 WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
809 {
810 char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale));
811
812 return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld));
813 }
814 #endif
815
816 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
817 WXDLLEXPORT size_t wxWcslen(const wchar_t *s)
818 {
819 size_t n = 0;
820 while ( *s++ )
821 n++;
822
823 return n;
824 }
825 #endif
826
827 // ----------------------------------------------------------------------------
828 // string.h functions
829 // ----------------------------------------------------------------------------
830
831 #ifdef wxNEED_WX_STRING_H
832
833 // RN: These need to be c externed for the regex lib
834 #ifdef __cplusplus
835 extern "C" {
836 #endif
837
838 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
839 {
840 wxChar *ret = dest;
841 while (*dest) dest++;
842 while ((*dest++ = *src++));
843 return ret;
844 }
845
846 WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c)
847 {
848 // be careful here as the terminating NUL makes part of the string
849 while ( *s != c )
850 {
851 if ( !*s++ )
852 return NULL;
853 }
854
855 return s;
856 }
857
858 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
859 {
860 while ((*s1 == *s2) && *s1) s1++, s2++;
861 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
862 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
863 return 0;
864 }
865
866 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
867 {
868 wxChar *ret = dest;
869 while ((*dest++ = *src++));
870 return ret;
871 }
872
873 WXDLLEXPORT size_t wxStrlen_(const wxChar *s)
874 {
875 size_t n = 0;
876 while ( *s++ )
877 n++;
878
879 return n;
880 }
881
882
883 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
884 {
885 wxChar *ret = dest;
886 while (*dest) dest++;
887 while (n && (*dest++ = *src++)) n--;
888 return ret;
889 }
890
891 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
892 {
893 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
894 if (n) {
895 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
896 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
897 }
898 return 0;
899 }
900
901 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
902 {
903 wxChar *ret = dest;
904 while (n && (*dest++ = *src++)) n--;
905 while (n) *dest++=0, n--; // the docs specify padding with zeroes
906 return ret;
907 }
908
909 WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
910 {
911 while (*s && !wxStrchr(accept, *s))
912 s++;
913
914 return *s ? s : NULL;
915 }
916
917 WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c)
918 {
919 const wxChar *ret = NULL;
920 do
921 {
922 if ( *s == c )
923 ret = s;
924 s++;
925 }
926 while ( *s );
927
928 return ret;
929 }
930
931 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
932 {
933 size_t len = 0;
934 while (wxStrchr(accept, *s++)) len++;
935 return len;
936 }
937
938 WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle)
939 {
940 wxASSERT_MSG( needle != NULL, _T("NULL argument in wxStrstr") );
941
942 // VZ: this is not exactly the most efficient string search algorithm...
943
944 const size_t len = wxStrlen(needle);
945
946 while ( const wxChar *fnd = wxStrchr(haystack, *needle) )
947 {
948 if ( !wxStrncmp(fnd, needle, len) )
949 return fnd;
950
951 haystack = fnd + 1;
952 }
953
954 return NULL;
955 }
956
957 #ifdef __cplusplus
958 }
959 #endif
960
961 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
962 {
963 const wxChar *start = nptr;
964
965 // FIXME: only correct for C locale
966 while (wxIsspace(*nptr)) nptr++;
967 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
968 while (wxIsdigit(*nptr)) nptr++;
969 if (*nptr == wxT('.')) {
970 nptr++;
971 while (wxIsdigit(*nptr)) nptr++;
972 }
973 if (*nptr == wxT('E') || *nptr == wxT('e')) {
974 nptr++;
975 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
976 while (wxIsdigit(*nptr)) nptr++;
977 }
978
979 wxString data(nptr, nptr-start);
980 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
981 char *rdat = wxMBSTRINGCAST dat;
982 double ret = strtod(dat, &rdat);
983
984 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
985
986 return ret;
987 }
988
989 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
990 {
991 const wxChar *start = nptr;
992
993 // FIXME: only correct for C locale
994 while (wxIsspace(*nptr)) nptr++;
995 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
996 if (((base == 0) || (base == 16)) &&
997 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
998 nptr += 2;
999 base = 16;
1000 }
1001 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
1002 else if (base == 0) base = 10;
1003
1004 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
1005 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
1006
1007 wxString data(start, nptr-start);
1008 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
1009 char *rdat = wxMBSTRINGCAST dat;
1010 long int ret = strtol(dat, &rdat, base);
1011
1012 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
1013
1014 return ret;
1015 }
1016
1017 WXDLLEXPORT unsigned long int wxStrtoul(const wxChar *nptr, wxChar **endptr, int base)
1018 {
1019 return (unsigned long int) wxStrtol(nptr, endptr, base);
1020 }
1021
1022 #endif // wxNEED_WX_STRING_H
1023
1024 #ifdef wxNEED_WX_STDIO_H
1025 WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
1026 {
1027 char mode_buffer[10];
1028 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
1029 mode_buffer[i] = (char) mode[i];
1030
1031 return fopen( wxConvFile.cWX2MB(path), mode_buffer );
1032 }
1033
1034 WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
1035 {
1036 char mode_buffer[10];
1037 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
1038 mode_buffer[i] = (char) mode[i];
1039
1040 return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream );
1041 }
1042
1043 WXDLLEXPORT int wxRemove(const wxChar *path)
1044 {
1045 return remove( wxConvFile.cWX2MB(path) );
1046 }
1047
1048 WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
1049 {
1050 return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) );
1051 }
1052 #endif
1053
1054 #ifndef wxAtof
1055 double WXDLLEXPORT wxAtof(const wxChar *psz)
1056 {
1057 #ifdef __WXWINCE__
1058 double d;
1059 wxString str(psz);
1060 if (str.ToDouble(& d))
1061 return d;
1062
1063 return 0.0;
1064 #else
1065 return atof(wxConvLibc.cWX2MB(psz));
1066 #endif
1067 }
1068 #endif
1069
1070 #ifdef wxNEED_WX_STDLIB_H
1071 int WXDLLEXPORT wxAtoi(const wxChar *psz)
1072 {
1073 return atoi(wxConvLibc.cWX2MB(psz));
1074 }
1075
1076 long WXDLLEXPORT wxAtol(const wxChar *psz)
1077 {
1078 return atol(wxConvLibc.cWX2MB(psz));
1079 }
1080
1081 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
1082 {
1083 #if wxUSE_UNICODE
1084 // NB: buffer returned by getenv() is allowed to be overwritten next
1085 // time getenv() is called, so it is OK to use static string
1086 // buffer to hold the data.
1087 static wxWCharBuffer value((wxChar*)NULL);
1088 value = wxConvLibc.cMB2WX(getenv(wxConvLibc.cWX2MB(name)));
1089 return value.data();
1090 #else
1091 return getenv(name);
1092 #endif
1093 }
1094
1095 int WXDLLEXPORT wxSystem(const wxChar *psz)
1096 {
1097 return system(wxConvLibc.cWX2MB(psz));
1098 }
1099
1100 #endif // wxNEED_WX_STDLIB_H
1101
1102 #ifdef wxNEED_WX_TIME_H
1103 WXDLLEXPORT size_t
1104 wxStrftime(wxChar *s, size_t maxsize, const wxChar *fmt, const struct tm *tm)
1105 {
1106 if ( !maxsize )
1107 return 0;
1108
1109 wxCharBuffer buf(maxsize);
1110
1111 wxCharBuffer bufFmt(wxConvLibc.cWX2MB(fmt));
1112 if ( !bufFmt )
1113 return 0;
1114
1115 size_t ret = strftime(buf.data(), maxsize, bufFmt, tm);
1116 if ( !ret )
1117 return 0;
1118
1119 wxWCharBuffer wbuf = wxConvLibc.cMB2WX(buf);
1120 if ( !wbuf )
1121 return 0;
1122
1123 wxStrncpy(s, wbuf, maxsize);
1124 return wxStrlen(s);
1125 }
1126 #endif // wxNEED_WX_TIME_H
1127
1128 #ifndef wxCtime
1129 WXDLLEXPORT wxChar *wxCtime(const time_t *timep)
1130 {
1131 // normally the string is 26 chars but give one more in case some broken
1132 // DOS compiler decides to use "\r\n" instead of "\n" at the end
1133 static wxChar buf[27];
1134
1135 // ctime() is guaranteed to return a string containing only ASCII
1136 // characters, as its format is always the same for any locale
1137 wxStrncpy(buf, wxString::FromAscii(ctime(timep)), WXSIZEOF(buf));
1138 buf[WXSIZEOF(buf) - 1] = _T('\0');
1139
1140 return buf;
1141 }
1142 #endif // wxCtime
1143
1144 #endif // wxUSE_WCHAR_T
1145
1146 #ifndef wxStrtoll
1147 static wxULongLong_t wxStrtoullBase(const wxChar* nptr, wxChar** endptr, int base, wxChar* sign)
1148 {
1149 wxULongLong_t sum = 0;
1150 wxString wxstr(nptr);
1151 wxString::const_iterator i = wxstr.begin();
1152 wxString::const_iterator end = wxstr.end();
1153
1154 // Skip spaces
1155 while ( i != end && wxIsspace(*i) ) i++;
1156
1157 // Starts with sign?
1158 *sign = wxT(' ');
1159 if ( i != end )
1160 {
1161 wxChar c = *i;
1162 if ( c == wxT('+') || c == wxT('-') )
1163 {
1164 *sign = c;
1165 i++;
1166 }
1167 }
1168
1169 // Starts with 0x?
1170 if ( i != end && *i == wxT('0') )
1171 {
1172 i++;
1173 if ( i != end )
1174 {
1175 if ( *i == wxT('x') && (base == 16 || base == 0) )
1176 {
1177 base = 16;
1178 i++;
1179 }
1180 else
1181 {
1182 if ( endptr )
1183 *endptr = (wxChar*) nptr;
1184 wxSET_ERRNO(EINVAL);
1185 return sum;
1186 }
1187 }
1188 else
1189 i--;
1190 }
1191
1192 if ( base == 0 )
1193 base = 10;
1194
1195 for ( ; i != end; i++ )
1196 {
1197 unsigned int n;
1198
1199 wxChar c = *i;
1200 if ( c >= wxT('0') )
1201 {
1202 if ( c <= wxT('9') )
1203 n = c - wxT('0');
1204 else
1205 n = wxTolower(c) - wxT('a') + 10;
1206 }
1207 else
1208 break;
1209
1210 if ( n >= (unsigned int)base )
1211 // Invalid character (for this base)
1212 break;
1213
1214 wxULongLong_t prevsum = sum;
1215 sum = (sum * base) + n;
1216
1217 if ( sum < prevsum )
1218 {
1219 wxSET_ERRNO(ERANGE);
1220 break;
1221 }
1222 }
1223
1224 if ( endptr )
1225 {
1226 const wxChar& endref = *i;
1227 *endptr = &(wxChar&)endref;
1228 }
1229
1230 return sum;
1231 }
1232
1233 wxULongLong_t wxStrtoull(const wxChar* nptr, wxChar** endptr, int base)
1234 {
1235 wxChar sign;
1236 wxULongLong_t uval = wxStrtoullBase(nptr, endptr, base, &sign);
1237
1238 if ( sign == wxT('-') )
1239 {
1240 wxSET_ERRNO(ERANGE);
1241 uval = 0;
1242 }
1243
1244 return uval;
1245 }
1246
1247 wxLongLong_t wxStrtoll(const wxChar* nptr, wxChar** endptr, int base)
1248 {
1249 wxChar sign;
1250 wxULongLong_t uval = wxStrtoullBase(nptr, endptr, base, &sign);
1251 wxLongLong_t val = 0;
1252
1253 if ( sign == wxT('-') )
1254 {
1255 if ( uval <= wxULL(wxINT64_MAX+1) )
1256 {
1257 if ( uval == wxULL(wxINT64_MAX+1))
1258 val = -((wxLongLong_t)wxINT64_MAX) - 1;
1259 else
1260 val = -((wxLongLong_t)uval);
1261 }
1262 else
1263 {
1264 wxSET_ERRNO(ERANGE);
1265 }
1266 }
1267 else if ( uval <= wxINT64_MAX )
1268 {
1269 val = uval;
1270 }
1271 else
1272 {
1273 wxSET_ERRNO(ERANGE);
1274 }
1275
1276 return val;
1277 }
1278 #endif // wxStrtoll
1279
1280 // ----------------------------------------------------------------------------
1281 // functions which we may need even if !wxUSE_WCHAR_T
1282 // ----------------------------------------------------------------------------
1283
1284 #ifndef wxStrtok
1285
1286 WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
1287 {
1288 if (!psz)
1289 {
1290 psz = *save_ptr;
1291 if ( !psz )
1292 return NULL;
1293 }
1294
1295 psz += wxStrspn(psz, delim);
1296 if (!*psz)
1297 {
1298 *save_ptr = (wxChar *)NULL;
1299 return (wxChar *)NULL;
1300 }
1301
1302 wxChar *ret = psz;
1303 psz = wxStrpbrk(psz, delim);
1304 if (!psz)
1305 {
1306 *save_ptr = (wxChar*)NULL;
1307 }
1308 else
1309 {
1310 *psz = wxT('\0');
1311 *save_ptr = psz + 1;
1312 }
1313
1314 return ret;
1315 }
1316
1317 #endif // wxStrtok
1318
1319 // ----------------------------------------------------------------------------
1320 // missing C RTL functions
1321 // ----------------------------------------------------------------------------
1322
1323 #ifdef wxNEED_STRDUP
1324
1325 char *strdup(const char *s)
1326 {
1327 char *dest = (char*) malloc( strlen( s ) + 1 ) ;
1328 if ( dest )
1329 strcpy( dest , s ) ;
1330 return dest ;
1331 }
1332 #endif // wxNEED_STRDUP
1333
1334 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1335
1336 void *calloc( size_t num, size_t size )
1337 {
1338 void** ptr = (void **)malloc(num * size);
1339 memset( ptr, 0, num * size);
1340 return ptr;
1341 }
1342
1343 #endif // __WXWINCE__ <= 211
1344
1345 #ifdef __WXWINCE__
1346
1347 int wxRemove(const wxChar *path)
1348 {
1349 return ::DeleteFile(path) == 0;
1350 }
1351
1352 #endif