// Author: Ove Kaven
// Modified by: Ron Lee, Francesco Montorsi
// Created: 09/04/99
-// RCS-ID: $Id$
// Copyright: (c) wxWidgets copyright
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#pragma hdrstop
#endif
-#include "wx/wxchar.h"
+#include "wx/crt.h"
+#include "wx/strconv.h" // wxMBConv::cWC2MB()
#define _ISOC9X_SOURCE 1 // to get vsscanf()
#define _BSD_SOURCE 1 // to still get strdup()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <wchar.h>
+
+#ifdef __SGI__
+ // wide character functions are declared in std namespace under IRIX
+ using namespace std;
+
+ // and this one is only declared if __c99 is defined which is not the case
+ // for C++ builds, so declare it ourselves
+ extern "C" int vswscanf(const wchar_t *, const wchar_t *, va_list);
+#endif
#ifndef __WXWINCE__
#include <time.h>
#include "wx/log.h"
#endif
-#if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
- #include <windef.h>
- #include <winbase.h>
- #include <winnls.h>
- #include <winnt.h>
+#ifdef HAVE_LANGINFO_H
+ #include <langinfo.h>
#endif
-#ifndef wxStrtoll
- #ifdef __WXWINCE__
- // there is no errno.h under CE apparently
- #define wxSET_ERRNO(value)
- #else
- #include <errno.h>
+#ifdef __WXWINCE__
+ // there is no errno.h under CE apparently
+ #define wxSET_ERRNO(value)
+#else
+ #include <errno.h>
- #define wxSET_ERRNO(value) errno = value
- #endif
+ #define wxSET_ERRNO(value) errno = value
#endif
-#if defined(__MWERKS__) && __MSL__ >= 0x6000
-namespace std {}
-using namespace std ;
+#if defined(__DARWIN__)
+ #include "wx/osx/core/cfref.h"
+ #include <CoreFoundation/CFLocale.h>
+ #include "wx/osx/core/cfstring.h"
+ #include <xlocale.h>
#endif
-#if wxUSE_WCHAR_T
-size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n)
+WXDLLIMPEXP_BASE size_t wxMB2WC(wchar_t *buf, const char *psz, size_t n)
{
// assume that we have mbsrtowcs() too if we have wcsrtombs()
#ifdef HAVE_WCSRTOMBS
#endif
}
- // note that we rely on common (and required by Unix98 but unfortunately not
+ // Note that we rely on common (and required by Unix98 but unfortunately not
// C99) extension which allows to call mbs(r)towcs() with NULL output pointer
// to just get the size of the needed buffer -- this is needed as otherwise
- // we have no idea about how much space we need and if the CRT doesn't
- // support it (the only currently known example being Metrowerks, see
- // wx/wxchar.h) we don't use its mbstowcs() at all
+ // we have no idea about how much space we need. Currently all supported
+ // compilers do provide it and if they don't, HAVE_WCSRTOMBS shouldn't be
+ // defined at all.
#ifdef HAVE_WCSRTOMBS
- return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
+ return mbsrtowcs(NULL, &psz, 0, &mbstate);
#else
- return wxMbstowcs((wchar_t *) NULL, psz, 0);
+ return wxMbstowcs(NULL, psz, 0);
#endif
}
-size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
+WXDLLIMPEXP_BASE size_t wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
{
#ifdef HAVE_WCSRTOMBS
mbstate_t mbstate;
}
#ifdef HAVE_WCSRTOMBS
- return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
+ return wcsrtombs(NULL, &pwz, 0, &mbstate);
#else
- return wxWcstombs((char *) NULL, pwz, 0);
+ return wxWcstombs(NULL, pwz, 0);
#endif
}
-#endif // wxUSE_WCHAR_T
-
-bool WXDLLEXPORT wxOKlibc()
-{
-#if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
- // glibc 2.0 uses UTF-8 even when it shouldn't
- wchar_t res = 0;
- if ((MB_CUR_MAX == 2) &&
- (wxMB2WC(&res, "\xdd\xa5", 1) == 1) &&
- (res==0x765)) {
- // this is UTF-8 allright, check whether that's what we want
- char *cur_locale = setlocale(LC_CTYPE, NULL);
- if ((strlen(cur_locale) < 4) ||
- (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) ||
- (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) {
- // nope, don't use libc conversion
- return false;
+
+char* wxSetlocale(int category, const char *locale)
+{
+#ifdef __WXWINCE__
+ // FIXME-CE: there is no setlocale() in CE CRT, use SetThreadLocale()?
+ wxUnusedVar(category);
+ wxUnusedVar(locale);
+
+ return NULL;
+#else // !__WXWINCE__
+#ifdef __WXMAC__
+ char *rv = NULL ;
+ if ( locale != NULL && locale[0] == 0 )
+ {
+ // the attempt to use newlocale(LC_ALL_MASK, "", NULL);
+ // here in order to deduce the language along the environment vars rules
+ // lead to strange crashes later...
+
+ // we have to emulate the behaviour under OS X
+ wxCFRef<CFLocaleRef> userLocaleRef(CFLocaleCopyCurrent());
+ wxCFStringRef str(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleLanguageCode)));
+ wxString langFull = str.AsString()+"_";
+ str.reset(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleCountryCode)));
+ langFull += str.AsString();
+ rv = setlocale(category, langFull.c_str());
}
- }
+ else
+ rv = setlocale(category, locale);
+#else
+ char *rv = setlocale(category, locale);
#endif
- return true;
+ if ( locale != NULL /* setting locale, not querying */ &&
+ rv /* call was successful */ )
+ {
+ wxUpdateLocaleIsUtf8();
+ }
+ return rv;
+#endif // __WXWINCE__/!__WXWINCE__
}
// ============================================================================
#undef wxVsprintf
#undef wxVprintf
#undef wxVsnprintf_
- #undef wxSnprintf_
#define wxNEED_WPRINTF
- int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr );
+ int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list argptr );
#endif
-#if !defined(wxSnprintf_)
-int WXDLLEXPORT wxDoSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...)
+#if defined(__DMC__)
+/* Digital Mars adds count to _stprintf (C99) so convert */
+int wxCRT_SprintfW (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... )
{
- va_list argptr;
- va_start(argptr, format);
-
- int iLen = wxVsnprintf_(buf, len, format, argptr);
-
- va_end(argptr);
+ va_list arglist;
- return iLen;
+ va_start( arglist, format );
+ int iLen = swprintf ( s, -1, format, arglist );
+ va_end( arglist );
+ return iLen ;
}
-#endif // wxSnprintf_
-
-#if defined(__DMC__)
- /* Digital Mars adds count to _stprintf (C99) so convert */
- #if wxUSE_UNICODE
- int wxDoSprintf (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... )
- {
- va_list arglist;
-
- va_start( arglist, format );
- int iLen = swprintf ( s, -1, format, arglist );
- va_end( arglist );
- return iLen ;
- }
-
- #endif // wxUSE_UNICODE
-
#endif //__DMC__
// ----------------------------------------------------------------------------
// implement the standard IO functions for wide char if libc doesn't have them
// ----------------------------------------------------------------------------
-#ifdef wxNEED_FPUTS
-int wxFputs(const wchar_t *ws, FILE *stream)
+#ifndef wxCRT_FputsW
+int wxCRT_FputsW(const wchar_t *ws, FILE *stream)
{
wxCharBuffer buf(wxConvLibc.cWC2MB(ws));
if ( !buf )
// counting the number of wide characters written isn't worth the trouble,
// simply distinguish between ok and error
- return fputs(buf, stream) == -1 ? -1 : 0;
+ return wxCRT_FputsA(buf, stream) == -1 ? -1 : 0;
}
-#endif // wxNEED_FPUTS
+#endif // !wxCRT_FputsW
-#ifdef wxNEED_PUTS
-int wxPuts(const wxChar *ws)
+#ifndef wxCRT_PutsW
+int wxCRT_PutsW(const wchar_t *ws)
{
- int rc = wxFputs(ws, stdout);
+ int rc = wxCRT_FputsW(ws, stdout);
if ( rc != -1 )
{
- if ( wxFputs(L"\n", stdout) == -1 )
+ if ( wxCRT_FputsW(L"\n", stdout) == -1 )
return -1;
rc++;
return rc;
}
-#endif // wxNEED_PUTS
+#endif // !wxCRT_PutsW
-#ifdef wxNEED_PUTC
-int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream)
+#ifndef wxCRT_FputcW
+int /* not wint_t */ wxCRT_FputcW(wchar_t wc, FILE *stream)
{
wchar_t ws[2] = { wc, L'\0' };
- return wxFputs(ws, stream);
+ return wxCRT_FputsW(ws, stream);
}
-#endif // wxNEED_PUTC
+#endif // !wxCRT_FputcW
// NB: we only implement va_list functions here, the ones taking ... are
// defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
#ifdef wxNEED_WPRINTF
// TODO: implement the scanf() functions
-int vwscanf(const wxChar *format, va_list argptr)
+static int vwscanf(const wchar_t *format, va_list argptr)
{
- wxFAIL_MSG( _T("TODO") );
+ wxFAIL_MSG( wxT("TODO") );
return -1;
}
-int vswscanf(const wxChar *ws, const wxChar *format, va_list argptr)
+static int vfwscanf(FILE *stream, const wchar_t *format, va_list argptr)
{
- // The best we can do without proper Unicode support in glibc is to
- // convert the strings into MB representation and run ANSI version
- // of the function. This doesn't work with %c and %s because of difference
- // in size of char and wchar_t, though.
-
- wxCHECK_MSG( wxStrstr(format, _T("%s")) == NULL, -1,
- _T("incomplete vswscanf implementation doesn't allow %s") );
- wxCHECK_MSG( wxStrstr(format, _T("%c")) == NULL, -1,
- _T("incomplete vswscanf implementation doesn't allow %c") );
-
- va_list argcopy;
- wxVaCopy(argcopy, argptr);
- return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argcopy);
-}
-
-int vfwscanf(FILE *stream, const wxChar *format, va_list argptr)
-{
- wxFAIL_MSG( _T("TODO") );
+ wxFAIL_MSG( wxT("TODO") );
return -1;
}
-#define vswprintf wxVsnprintf_
+#define vswprintf wxCRT_VsnprintfW
-int vfwprintf(FILE *stream, const wxChar *format, va_list argptr)
+static int vfwprintf(FILE *stream, const wchar_t *format, va_list argptr)
{
wxString s;
int rc = s.PrintfV(format, argptr);
return rc;
}
-int vwprintf(const wxChar *format, va_list argptr)
+static int vwprintf(const wchar_t *format, va_list argptr)
{
- return wxVfprintf(stdout, format, argptr);
+ return wxCRT_VfprintfW(stdout, format, argptr);
}
#endif // wxNEED_WPRINTF
-#ifdef wxNEED_PRINTF_CONVERSION
+#ifdef wxNEED_VSWSCANF
+static int vswscanf(const wchar_t *ws, const wchar_t *format, va_list argptr)
+{
+ // The best we can do without proper Unicode support in glibc is to
+ // convert the strings into MB representation and run ANSI version
+ // of the function. This doesn't work with %c and %s because of difference
+ // in size of char and wchar_t, though.
+
+ wxCHECK_MSG( wxStrstr(format, wxT("%s")) == NULL, -1,
+ wxT("incomplete vswscanf implementation doesn't allow %s") );
+ wxCHECK_MSG( wxStrstr(format, wxT("%c")) == NULL, -1,
+ wxT("incomplete vswscanf implementation doesn't allow %c") );
+
+ return vsscanf(static_cast<const char*>(wxConvLibc.cWX2MB(ws)),
+ wxConvLibc.cWX2MB(format), argptr);
+}
+#endif
// ----------------------------------------------------------------------------
-// wxFormatConverter: class doing the "%s" -> "%ls" conversion
+// wxPrintf(), wxScanf() and relatives
// ----------------------------------------------------------------------------
-/*
- Here are the gory details. We want to follow the Windows/MS conventions,
- that is to have
+// FIXME-UTF8: do format conversion using (modified) wxFormatConverter in
+// template wrappers, not here; note that it will needed to
+// translate all forms of string specifiers to %(l)s for wxPrintf(),
+// but it only should do what it did in 2.8 for wxScanf()!
- In ANSI mode:
+#ifndef wxCRT_PrintfW
+int wxCRT_PrintfW( const wchar_t *format, ... )
+{
+ va_list argptr;
+ va_start(argptr, format);
- format specifier results in
- -----------------------------------
- %c, %hc, %hC char
- %lc, %C, %lC wchar_t
+ int ret = vwprintf( format, argptr );
- In Unicode mode:
+ va_end(argptr);
- format specifier results in
- -----------------------------------
- %hc, %C, %hC char
- %c, %lc, %lC wchar_t
+ return ret;
+}
+#endif
+#ifndef wxCRT_FprintfW
+int wxCRT_FprintfW( FILE *stream, const wchar_t *format, ... )
+{
+ va_list argptr;
+ va_start( argptr, format );
- while on POSIX systems we have %C identical to %lc and %c always means char
- (in any mode) while %lc always means wchar_t,
+ int ret = vfwprintf( stream, format, argptr );
- So to use native functions in order to get our semantics we must do the
- following translations in Unicode mode (nothing to do in ANSI mode):
+ va_end(argptr);
- wxWidgets specifier POSIX specifier
- ----------------------------------------
+ return ret;
+}
+#endif
- %hc, %C, %hC %c
- %c %lc
+#ifndef wxCRT_VfprintfW
+int wxCRT_VfprintfW( FILE *stream, const wchar_t *format, va_list argptr )
+{
+ return vfwprintf( stream, format, argptr );
+}
+#endif
+#ifndef wxCRT_VprintfW
+int wxCRT_VprintfW( const wchar_t *format, va_list argptr )
+{
+ return vwprintf( format, argptr );
+}
+#endif
- And, of course, the same should be done for %s as well.
-*/
+#ifndef wxCRT_VsprintfW
+int wxCRT_VsprintfW( wchar_t *str, const wchar_t *format, va_list argptr )
+{
+ // same as for wxSprintf()
+ return vswprintf(str, INT_MAX / 4, format, argptr);
+}
+#endif
-class wxFormatConverter
+#ifndef wxCRT_ScanfW
+int wxCRT_ScanfW(const wchar_t *format, ...)
{
-public:
- wxFormatConverter(const wxChar *format);
+ va_list argptr;
+ va_start(argptr, format);
- // notice that we only translated the string if m_fmtOrig == NULL (as set
- // by CopyAllBefore()), otherwise we should simply use the original format
- operator const wxChar *() const
- { return m_fmtOrig ? m_fmtOrig : m_fmt.c_str(); }
+#ifdef __VMS
+#if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
+ int ret = std::vwscanf(format, argptr);
+#else
+ int ret = vwscanf(format, argptr);
+#endif
+#else
+ int ret = vwscanf(format, argptr);
+#endif
-private:
- // copy another character to the translated format: this function does the
- // copy if we are translating but doesn't do anything at all if we don't,
- // so we don't create the translated format string at all unless we really
- // need to (i.e. InsertFmtChar() is called)
- wxChar CopyFmtChar(wxChar ch)
- {
- if ( !m_fmtOrig )
- {
- // we're translating, do copy
- m_fmt += ch;
- }
- else
- {
- // simply increase the count which should be copied by
- // CopyAllBefore() later if needed
- m_nCopied++;
- }
+ va_end(argptr);
- return ch;
- }
+ return ret;
+}
+#endif
- // insert an extra character
- void InsertFmtChar(wxChar ch)
- {
- if ( m_fmtOrig )
- {
- // so far we haven't translated anything yet
- CopyAllBefore();
- }
+#ifndef wxCRT_SscanfW
+int wxCRT_SscanfW(const wchar_t *str, const wchar_t *format, ...)
+{
+ va_list argptr;
+ va_start(argptr, format);
- m_fmt += ch;
- }
+#ifdef __VMS
+#if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
+ int ret = std::vswscanf(str, format, argptr);
+#else
+ int ret = vswscanf(str, format, argptr);
+#endif
+#else
+ int ret = vswscanf(str, format, argptr);
+#endif
- void CopyAllBefore()
- {
- wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") );
+ va_end(argptr);
- m_fmt = wxString(m_fmtOrig, m_nCopied);
+ return ret;
+}
+#endif
- // we won't need it any longer
- m_fmtOrig = NULL;
- }
+#ifndef wxCRT_FscanfW
+int wxCRT_FscanfW(FILE *stream, const wchar_t *format, ...)
+{
+ va_list argptr;
+ va_start(argptr, format);
+#ifdef __VMS
+#if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
+ int ret = std::vfwscanf(stream, format, argptr);
+#else
+ int ret = vfwscanf(stream, format, argptr);
+#endif
+#else
+ int ret = vfwscanf(stream, format, argptr);
+#endif
- static bool IsFlagChar(wxChar ch)
- {
- return ch == _T('-') || ch == _T('+') ||
- ch == _T('0') || ch == _T(' ') || ch == _T('#');
- }
+ va_end(argptr);
- void SkipDigits(const wxChar **ptpc)
- {
- while ( **ptpc >= _T('0') && **ptpc <= _T('9') )
- CopyFmtChar(*(*ptpc)++);
- }
+ return ret;
+}
+#endif
- // the translated format
- wxString m_fmt;
+#ifndef wxCRT_VsscanfW
+int wxCRT_VsscanfW(const wchar_t *str, const wchar_t *format, va_list argptr)
+{
+#ifdef __VMS
+#if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
+ return std::vswscanf(str, format, argptr);
+#else
+ return vswscanf(str, format, argptr);
+#endif
+#else
+ return vswscanf(str, format, argptr);
+#endif
+}
+#endif
- // the original format
- const wxChar *m_fmtOrig;
- // the number of characters already copied
- size_t m_nCopied;
-};
+// ----------------------------------------------------------------------------
+// wrappers to printf and scanf function families
+// ----------------------------------------------------------------------------
-wxFormatConverter::wxFormatConverter(const wxChar *format)
+#if !wxUSE_UTF8_LOCALE_ONLY
+int wxDoSprintfWchar(char *str, const wxChar *format, ...)
{
- m_fmtOrig = format;
- m_nCopied = 0;
-
- while ( *format )
- {
- if ( CopyFmtChar(*format++) == _T('%') )
- {
- // skip any flags
- while ( IsFlagChar(*format) )
- CopyFmtChar(*format++);
+ va_list argptr;
+ va_start(argptr, format);
- // and possible width
- if ( *format == _T('*') )
- CopyFmtChar(*format++);
- else
- SkipDigits(&format);
+ int rv = wxVsprintf(str, format, argptr);
- // precision?
- if ( *format == _T('.') )
- {
- CopyFmtChar(*format++);
- if ( *format == _T('*') )
- CopyFmtChar(*format++);
- else
- SkipDigits(&format);
- }
+ va_end(argptr);
+ return rv;
+}
+#endif // !wxUSE_UTF8_LOCALE_ONLY
- // next we can have a size modifier
- enum
- {
- Default,
- Short,
- Long
- } size;
+#if wxUSE_UNICODE_UTF8
+int wxDoSprintfUtf8(char *str, const char *format, ...)
+{
+ va_list argptr;
+ va_start(argptr, format);
- switch ( *format )
- {
- case _T('h'):
- size = Short;
- format++;
- break;
-
- case _T('l'):
- // "ll" has a different meaning!
- if ( format[1] != _T('l') )
- {
- size = Long;
- format++;
- break;
- }
- //else: fall through
-
- default:
- size = Default;
- }
+ int rv = wxVsprintf(str, format, argptr);
- // and finally we should have the type
- switch ( *format )
- {
- case _T('C'):
- case _T('S'):
- // %C and %hC -> %c and %lC -> %lc
- if ( size == Long )
- CopyFmtChar(_T('l'));
-
- InsertFmtChar(*format++ == _T('C') ? _T('c') : _T('s'));
- break;
-
- case _T('c'):
- case _T('s'):
- // %c -> %lc but %hc stays %hc and %lc is still %lc
- if ( size == Default)
- InsertFmtChar(_T('l'));
- // fall through
-
- default:
- // nothing special to do
- if ( size != Default )
- CopyFmtChar(*(format - 1));
- CopyFmtChar(*format++);
- }
- }
- }
+ va_end(argptr);
+ return rv;
}
+#endif // wxUSE_UNICODE_UTF8
-#else // !wxNEED_PRINTF_CONVERSION
- // no conversion necessary
- #define wxFormatConverter(x) (x)
-#endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
+#if wxUSE_UNICODE
-#ifdef __WXDEBUG__
-// For testing the format converter
-wxString wxConvertFormat(const wxChar *format)
+#if !wxUSE_UTF8_LOCALE_ONLY
+int wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...)
{
- return wxString(wxFormatConverter(format));
-}
-#endif
+ va_list argptr;
+ va_start(argptr, format);
-// ----------------------------------------------------------------------------
-// wxPrintf(), wxScanf() and relatives
-// ----------------------------------------------------------------------------
+ int rv = wxVsprintf(str, format, argptr);
-#if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
+ va_end(argptr);
+ return rv;
+}
+#endif // !wxUSE_UTF8_LOCALE_ONLY
-int wxDoScanf( const wxChar *format, ... )
+#if wxUSE_UNICODE_UTF8
+int wxDoSprintfUtf8(wchar_t *str, const char *format, ...)
{
va_list argptr;
va_start(argptr, format);
- int ret = vwscanf(wxFormatConverter(format), argptr );
+ int rv = wxVsprintf(str, format, argptr);
va_end(argptr);
-
- return ret;
+ return rv;
}
+#endif // wxUSE_UNICODE_UTF8
+
+#endif // wxUSE_UNICODE
-int wxDoSscanf( const wxChar *str, const wxChar *format, ... )
+#if !wxUSE_UTF8_LOCALE_ONLY
+int wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...)
{
va_list argptr;
va_start(argptr, format);
- int ret = vswscanf( str, wxFormatConverter(format), argptr );
+ int rv = wxVsnprintf(str, size, format, argptr);
va_end(argptr);
-
- return ret;
+ return rv;
}
+#endif // !wxUSE_UTF8_LOCALE_ONLY
-int wxDoFscanf( FILE *stream, const wxChar *format, ... )
+#if wxUSE_UNICODE_UTF8
+int wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...)
{
va_list argptr;
va_start(argptr, format);
- int ret = vfwscanf(stream, wxFormatConverter(format), argptr);
- va_end(argptr);
+ int rv = wxVsnprintf(str, size, format, argptr);
- return ret;
+ va_end(argptr);
+ return rv;
}
+#endif // wxUSE_UNICODE_UTF8
+
+#if wxUSE_UNICODE
-int wxDoPrintf( const wxChar *format, ... )
+#if !wxUSE_UTF8_LOCALE_ONLY
+int wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...)
{
va_list argptr;
va_start(argptr, format);
- int ret = vwprintf( wxFormatConverter(format), argptr );
+ int rv = wxVsnprintf(str, size, format, argptr);
va_end(argptr);
-
- return ret;
+ return rv;
}
+#endif // !wxUSE_UTF8_LOCALE_ONLY
-#ifndef wxSnprintf
-int wxDoSnprintf( wxChar *str, size_t size, const wxChar *format, ... )
+#if wxUSE_UNICODE_UTF8
+int wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...)
{
va_list argptr;
va_start(argptr, format);
- int ret = vswprintf( str, size, wxFormatConverter(format), argptr );
-
- // VsnprintfTestCase reveals that glibc's implementation of vswprintf
- // doesn't nul terminate on truncation.
- str[size - 1] = 0;
+ int rv = wxVsnprintf(str, size, format, argptr);
va_end(argptr);
-
- return ret;
+ return rv;
}
-#endif // wxSnprintf
+#endif // wxUSE_UNICODE_UTF8
+
+#endif // wxUSE_UNICODE
+
-int wxDoSprintf( wxChar *str, const wxChar *format, ... )
+#ifdef HAVE_BROKEN_VSNPRINTF_DECL
+ #define vsnprintf wx_fixed_vsnprintf
+#endif
+
+#if wxUSE_UNICODE
+
+namespace
{
- va_list argptr;
- va_start(argptr, format);
- // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
- // it's safe to implement this one in terms of it
- wxString s(wxString::FormatV(format, argptr));
- wxStrcpy(str, s);
+#if !wxUSE_UTF8_LOCALE_ONLY
+int ConvertStringToBuf(const wxString& s, char *out, size_t outsize)
+{
+ const wxCharBuffer buf(s.mb_str());
- va_end(argptr);
+ const size_t len = buf.length();
+ if ( outsize > len )
+ {
+ memcpy(out, buf, len+1);
+ }
+ else // not enough space
+ {
+ memcpy(out, buf, outsize-1);
+ out[outsize-1] = '\0';
+ }
- return s.length();
+ return len;
}
+#endif // !wxUSE_UTF8_LOCALE_ONLY
-int wxDoFprintf( FILE *stream, const wxChar *format, ... )
+#if wxUSE_UNICODE_UTF8
+int ConvertStringToBuf(const wxString& s, wchar_t *out, size_t outsize)
{
- va_list argptr;
- va_start( argptr, format );
+ const wxWX2WCbuf buf(s.wc_str());
+ size_t len = s.length(); // same as buf length for wchar_t*
+ if ( outsize > len )
+ {
+ memcpy(out, buf, (len+1) * sizeof(wchar_t));
+ }
+ else // not enough space
+ {
+ memcpy(out, buf, (outsize-1) * sizeof(wchar_t));
+ out[outsize-1] = 0;
+ }
+ return len;
+}
+#endif // wxUSE_UNICODE_UTF8
- int ret = vfwprintf( stream, wxFormatConverter(format), argptr );
+} // anonymous namespace
- va_end(argptr);
+template<typename T>
+static size_t PrintfViaString(T *out, size_t outsize,
+ const wxString& format, va_list argptr)
+{
+ wxString s;
+ s.PrintfV(format, argptr);
- return ret;
+ return ConvertStringToBuf(s, out, outsize);
}
+#endif // wxUSE_UNICODE
-int wxVsscanf( const wxChar *str, const wxChar *format, va_list argptr )
+int wxVsprintf(char *str, const wxString& format, va_list argptr)
{
- return vswscanf( str, wxFormatConverter(format), argptr );
+#if wxUSE_UTF8_LOCALE_ONLY
+ return wxCRT_VsprintfA(str, format.wx_str(), argptr);
+#else
+ #if wxUSE_UNICODE_UTF8
+ if ( wxLocaleIsUtf8 )
+ return wxCRT_VsprintfA(str, format.wx_str(), argptr);
+ else
+ #endif
+ #if wxUSE_UNICODE
+ return PrintfViaString(str, wxNO_LEN, format, argptr);
+ #else
+ return wxCRT_VsprintfA(str, format.mb_str(), argptr);
+ #endif
+#endif
}
-int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr )
+#if wxUSE_UNICODE
+int wxVsprintf(wchar_t *str, const wxString& format, va_list argptr)
{
- return vfwprintf( stream, wxFormatConverter(format), argptr );
+#if wxUSE_UNICODE_WCHAR
+#ifdef __DMC__
+/*
+This fails with a bug similar to
+http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=c++.beta&artnum=680
+in DMC 8.49 and 8.50
+I don't see it being used in the wxWidgets sources at present (oct 2007) CE
+*/
+#pragma message ( "warning ::::: wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) not yet implemented" )
+ wxFAIL_MSG( wxT("TODO") );
+
+ return -1;
+#else
+ return wxCRT_VsprintfW(str, format.wc_str(), argptr);
+#endif //DMC
+#else // wxUSE_UNICODE_UTF8
+ #if !wxUSE_UTF8_LOCALE_ONLY
+ if ( !wxLocaleIsUtf8 )
+ return wxCRT_VsprintfW(str, format.wc_str(), argptr);
+ else
+ #endif
+ return PrintfViaString(str, wxNO_LEN, format, argptr);
+#endif // wxUSE_UNICODE_UTF8
}
+#endif // wxUSE_UNICODE
-int wxVprintf( const wxChar *format, va_list argptr )
+int wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr)
{
- return vwprintf( wxFormatConverter(format), argptr );
+ int rv;
+#if wxUSE_UTF8_LOCALE_ONLY
+ rv = wxCRT_VsnprintfA(str, size, format.wx_str(), argptr);
+#else
+ #if wxUSE_UNICODE_UTF8
+ if ( wxLocaleIsUtf8 )
+ rv = wxCRT_VsnprintfA(str, size, format.wx_str(), argptr);
+ else
+ #endif
+ #if wxUSE_UNICODE
+ {
+ // NB: if this code is called, then wxString::PrintV() would use the
+ // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
+ // from here
+ rv = PrintfViaString(str, size, format, argptr);
+ }
+ #else
+ rv = wxCRT_VsnprintfA(str, size, format.mb_str(), argptr);
+ #endif
+#endif
+
+ // VsnprintfTestCase reveals that glibc's implementation of vswprintf
+ // doesn't nul terminate on truncation.
+ str[size - 1] = 0;
+
+ return rv;
}
-#ifndef wxVsnprintf
-int wxVsnprintf( wxChar *str, size_t size, const wxChar *format, va_list argptr )
+#if wxUSE_UNICODE
+int wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr)
{
- return vswprintf( str, size, wxFormatConverter(format), argptr );
-}
-#endif // wxVsnprintf
+ int rv;
+
+#if wxUSE_UNICODE_WCHAR
+ rv = wxCRT_VsnprintfW(str, size, format.wc_str(), argptr);
+#else // wxUSE_UNICODE_UTF8
+ #if !wxUSE_UTF8_LOCALE_ONLY
+ if ( !wxLocaleIsUtf8 )
+ rv = wxCRT_VsnprintfW(str, size, format.wc_str(), argptr);
+ else
+ #endif
+ {
+ // NB: if this code is called, then wxString::PrintV() would use the
+ // char* version of wxVsnprintf(), so it's safe to use PrintV()
+ // from here
+ rv = PrintfViaString(str, size, format, argptr);
+ }
+#endif // wxUSE_UNICODE_UTF8
+
+ // VsnprintfTestCase reveals that glibc's implementation of vswprintf
+ // doesn't nul terminate on truncation.
+ str[size - 1] = 0;
-int wxVsprintf( wxChar *str, const wxChar *format, va_list argptr )
-{
- // same as for wxSprintf()
- return vswprintf(str, INT_MAX / 4, wxFormatConverter(format), argptr);
+ return rv;
}
+#endif // wxUSE_UNICODE
-#endif // wxNEED_PRINTF_CONVERSION
-
-#if wxUSE_WCHAR_T
// ----------------------------------------------------------------------------
// ctype.h stuff (currently unused)
// ----------------------------------------------------------------------------
-#if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
-inline WORD wxMSW_ctype(wxChar ch)
-{
- WORD ret;
- GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
- return ret;
-}
-
-WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); }
-WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); }
-WXDLLEXPORT int wxIscntrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; }
-WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; }
-WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); }
-WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); }
-WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); }
-WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; }
-WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; }
-WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); }
-WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; }
-WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
-WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
-#endif
-
#ifdef wxNEED_WX_MBSTOWCS
-WXDLLEXPORT size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen)
+WXDLLIMPEXP_BASE size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen)
{
if (!out)
{
return in - origin;
}
-WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen)
+WXDLLIMPEXP_BASE size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen)
{
if (!out)
{
#endif // wxNEED_WX_MBSTOWCS
-#if defined(wxNEED_WX_CTYPE_H)
-
-#include <CoreFoundation/CoreFoundation.h>
-
-#define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
-#define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
-#define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
-#define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
-//CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
-#define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
-//CFCharacterSetRef cfprintset = !kCFCharacterSetControl
-#define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
-#define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
-#define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
-
-WXDLLEXPORT int wxIsalnum(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalnumset, ch); }
-WXDLLEXPORT int wxIsalpha(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalphaset, ch); }
-WXDLLEXPORT int wxIscntrl(wxChar ch) { return CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
-WXDLLEXPORT int wxIsdigit(wxChar ch) { return CFCharacterSetIsCharacterMember(cfdigitset, ch); }
-WXDLLEXPORT int wxIsgraph(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch) && ch != ' '; }
-WXDLLEXPORT int wxIslower(wxChar ch) { return CFCharacterSetIsCharacterMember(cflowerset, ch); }
-WXDLLEXPORT int wxIsprint(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
-WXDLLEXPORT int wxIspunct(wxChar ch) { return CFCharacterSetIsCharacterMember(cfpunctset, ch); }
-WXDLLEXPORT int wxIsspace(wxChar ch) { return CFCharacterSetIsCharacterMember(cfspaceset, ch); }
-WXDLLEXPORT int wxIsupper(wxChar ch) { return CFCharacterSetIsCharacterMember(cfupperset, ch); }
-WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxIsdigit(ch) || (ch>='a' && ch<='f') || (ch>='A' && ch<='F'); }
-WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)tolower((char)(ch)); }
-WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)toupper((char)(ch)); }
-
-#endif // wxNEED_WX_CTYPE_H
-
-#ifndef wxStrdupA
-
-WXDLLEXPORT char *wxStrdupA(const char *s)
+#ifndef wxCRT_StrdupA
+WXDLLIMPEXP_BASE char *wxCRT_StrdupA(const char *s)
{
return strcpy((char *)malloc(strlen(s) + 1), s);
}
+#endif // wxCRT_StrdupA
-#endif // wxStrdupA
-
-#ifndef wxStrdupW
-
-WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz)
+#ifndef wxCRT_StrdupW
+WXDLLIMPEXP_BASE wchar_t * wxCRT_StrdupW(const wchar_t *pwz)
{
size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t);
wchar_t *ret = (wchar_t *) malloc(size);
memcpy(ret, pwz, size);
return ret;
}
+#endif // wxCRT_StrdupW
-#endif // wxStrdupW
-
-#ifndef wxStricmp
-int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
-{
- register wxChar c1, c2;
- do {
- c1 = wxTolower(*psz1++);
- c2 = wxTolower(*psz2++);
- } while ( c1 && (c1 == c2) );
- return c1 - c2;
-}
-#endif
-
-#ifndef wxStricmp
-int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n)
-{
- // initialize the variables just to suppress stupid gcc warning
- register wxChar c1 = 0, c2 = 0;
- while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
- if (n) {
- if (c1 < c2) return -1;
- if (c1 > c2) return 1;
- }
- return 0;
-}
-#endif
-
-#ifndef wxSetlocale_
-wxWCharBuffer wxSetlocale_(int category, const wxChar *locale)
-{
- char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale));
-
- return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld));
-}
-
-wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
-{
- wxWCharBuffer rv = wxSetlocale_(category, locale);
- if ( rv )
- wxUpdateLocaleIsUtf8();
- return rv;
-}
-#else // defined(wxSetlocale_)
-const wxChar *wxSetlocale(int category, const wxChar *locale)
-{
- const wxChar *rv = wxSetlocale_(category, locale);
- if ( rv )
- wxUpdateLocaleIsUtf8();
- return rv;
-}
-#endif // wxSetlocale_ defined or not
-
-#if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
-WXDLLEXPORT size_t wxWcslen(const wchar_t *s)
+#ifndef wxWCHAR_T_IS_WXCHAR16
+size_t wxStrlen(const wxChar16 *s )
{
- size_t n = 0;
- while ( *s++ )
- n++;
-
- return n;
+ if (!s) return 0;
+ size_t i=0;
+ while (*s!=0) { ++i; ++s; };
+ return i;
}
-#endif
-
-// ----------------------------------------------------------------------------
-// string.h functions
-// ----------------------------------------------------------------------------
-#ifdef wxNEED_WX_STRING_H
-
-// RN: These need to be c externed for the regex lib
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
+wxChar16* wxStrdup(const wxChar16* s)
{
- wxChar *ret = dest;
- while (*dest) dest++;
- while ((*dest++ = *src++));
+ size_t size = (wxStrlen(s) + 1) * sizeof(wxChar16);
+ wxChar16 *ret = (wxChar16*) malloc(size);
+ memcpy(ret, s, size);
return ret;
}
+#endif
-WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c)
-{
- // be careful here as the terminating NUL makes part of the string
- while ( *s != c )
- {
- if ( !*s++ )
- return NULL;
- }
-
- return s;
-}
-
-WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
+#ifndef wxWCHAR_T_IS_WXCHAR32
+size_t wxStrlen(const wxChar32 *s )
{
- while ((*s1 == *s2) && *s1) s1++, s2++;
- if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
- if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
- return 0;
+ if (!s) return 0;
+ size_t i=0;
+ while (*s!=0) { ++i; ++s; };
+ return i;
}
-WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
+wxChar32* wxStrdup(const wxChar32* s)
{
- wxChar *ret = dest;
- while ((*dest++ = *src++));
+ size_t size = (wxStrlen(s) + 1) * sizeof(wxChar32);
+ wxChar32 *ret = (wxChar32*) malloc(size);
+ if ( ret )
+ memcpy(ret, s, size);
return ret;
}
+#endif
-WXDLLEXPORT size_t wxStrlen_(const wxChar *s)
+#ifndef wxCRT_StricmpA
+WXDLLIMPEXP_BASE int wxCRT_StricmpA(const char *psz1, const char *psz2)
{
- size_t n = 0;
- while ( *s++ )
- n++;
-
- return n;
+ register char c1, c2;
+ do {
+ c1 = wxTolower(*psz1++);
+ c2 = wxTolower(*psz2++);
+ } while ( c1 && (c1 == c2) );
+ return c1 - c2;
}
+#endif // !defined(wxCRT_StricmpA)
-
-WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
+#ifndef wxCRT_StricmpW
+WXDLLIMPEXP_BASE int wxCRT_StricmpW(const wchar_t *psz1, const wchar_t *psz2)
{
- wxChar *ret = dest;
- while (*dest) dest++;
- while (n && (*dest++ = *src++)) n--;
- return ret;
+ register wchar_t c1, c2;
+ do {
+ c1 = wxTolower(*psz1++);
+ c2 = wxTolower(*psz2++);
+ } while ( c1 && (c1 == c2) );
+ return c1 - c2;
}
+#endif // !defined(wxCRT_StricmpW)
-WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
+#ifndef wxCRT_StrnicmpA
+WXDLLIMPEXP_BASE int wxCRT_StrnicmpA(const char *s1, const char *s2, size_t n)
{
- while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
+ // initialize the variables just to suppress stupid gcc warning
+ register char c1 = 0, c2 = 0;
+ while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
if (n) {
- if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
- if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
+ if (c1 < c2) return -1;
+ if (c1 > c2) return 1;
}
return 0;
}
+#endif // !defined(wxCRT_StrnicmpA)
-WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
-{
- wxChar *ret = dest;
- while (n && (*dest++ = *src++)) n--;
- while (n) *dest++=0, n--; // the docs specify padding with zeroes
- return ret;
-}
-
-WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
-{
- while (*s && !wxStrchr(accept, *s))
- s++;
-
- return *s ? s : NULL;
-}
-
-WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c)
-{
- const wxChar *ret = NULL;
- do
- {
- if ( *s == c )
- ret = s;
- s++;
- }
- while ( *s );
-
- return ret;
-}
-
-WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
-{
- size_t len = 0;
- while (wxStrchr(accept, *s++)) len++;
- return len;
-}
-
-WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle)
-{
- wxASSERT_MSG( needle != NULL, _T("NULL argument in wxStrstr") );
-
- // VZ: this is not exactly the most efficient string search algorithm...
-
- const size_t len = wxStrlen(needle);
-
- while ( const wxChar *fnd = wxStrchr(haystack, *needle) )
- {
- if ( !wxStrncmp(fnd, needle, len) )
- return fnd;
-
- haystack = fnd + 1;
- }
-
- return NULL;
-}
-
-#ifdef __cplusplus
-}
-#endif
-
-WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
-{
- const wxChar *start = nptr;
-
- // FIXME: only correct for C locale
- while (wxIsspace(*nptr)) nptr++;
- if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
- while (wxIsdigit(*nptr)) nptr++;
- if (*nptr == wxT('.')) {
- nptr++;
- while (wxIsdigit(*nptr)) nptr++;
- }
- if (*nptr == wxT('E') || *nptr == wxT('e')) {
- nptr++;
- if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
- while (wxIsdigit(*nptr)) nptr++;
- }
-
- wxString data(nptr, nptr-start);
- wxWX2MBbuf dat = data.mb_str(wxConvLibc);
- char *rdat = wxMBSTRINGCAST dat;
- double ret = strtod(dat, &rdat);
-
- if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
-
- return ret;
-}
-
-WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
+#ifndef wxCRT_StrnicmpW
+WXDLLIMPEXP_BASE int wxCRT_StrnicmpW(const wchar_t *s1, const wchar_t *s2, size_t n)
{
- const wxChar *start = nptr;
-
- // FIXME: only correct for C locale
- while (wxIsspace(*nptr)) nptr++;
- if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
- if (((base == 0) || (base == 16)) &&
- (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
- nptr += 2;
- base = 16;
+ // initialize the variables just to suppress stupid gcc warning
+ register wchar_t c1 = 0, c2 = 0;
+ while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
+ if (n) {
+ if (c1 < c2) return -1;
+ if (c1 > c2) return 1;
}
- else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
- else if (base == 0) base = 10;
-
- while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
- (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
-
- wxString data(start, nptr-start);
- wxWX2MBbuf dat = data.mb_str(wxConvLibc);
- char *rdat = wxMBSTRINGCAST dat;
- long int ret = strtol(dat, &rdat, base);
-
- if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
-
- return ret;
-}
-
-WXDLLEXPORT unsigned long int wxStrtoul(const wxChar *nptr, wxChar **endptr, int base)
-{
- return (unsigned long int) wxStrtol(nptr, endptr, base);
-}
-
-#endif // wxNEED_WX_STRING_H
-
-#ifdef wxNEED_WX_STDIO_H
-WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
-{
- char mode_buffer[10];
- for (size_t i = 0; i < wxStrlen(mode)+1; i++)
- mode_buffer[i] = (char) mode[i];
-
- return fopen( wxConvFile.cWX2MB(path), mode_buffer );
-}
-
-WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
-{
- char mode_buffer[10];
- for (size_t i = 0; i < wxStrlen(mode)+1; i++)
- mode_buffer[i] = (char) mode[i];
-
- return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream );
-}
-
-WXDLLEXPORT int wxRemove(const wxChar *path)
-{
- return remove( wxConvFile.cWX2MB(path) );
+ return 0;
}
+#endif // !defined(wxCRT_StrnicmpW)
-WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
-{
- return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) );
-}
-#endif
+// ----------------------------------------------------------------------------
+// string.h functions
+// ----------------------------------------------------------------------------
-#ifndef wxAtof
-double WXDLLEXPORT wxAtof(const wxChar *psz)
+// this (and wxCRT_StrncmpW below) are extern "C" because they are needed
+// by regex code, the rest isn't needed, so it's not declared as extern "C"
+#ifndef wxCRT_StrlenW
+extern "C" WXDLLIMPEXP_BASE size_t wxCRT_StrlenW(const wchar_t *s)
{
-#ifdef __WXWINCE__
- double d;
- wxString str(psz);
- if (str.ToDouble(& d))
- return d;
+ size_t n = 0;
+ while ( *s++ )
+ n++;
- return 0.0;
-#else
- return atof(wxConvLibc.cWX2MB(psz));
-#endif
+ return n;
}
#endif
-#ifdef wxNEED_WX_STDLIB_H
-int WXDLLEXPORT wxAtoi(const wxChar *psz)
-{
- return atoi(wxConvLibc.cWX2MB(psz));
-}
-
-long WXDLLEXPORT wxAtol(const wxChar *psz)
-{
- return atol(wxConvLibc.cWX2MB(psz));
-}
+// ----------------------------------------------------------------------------
+// stdlib.h functions
+// ----------------------------------------------------------------------------
-wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
+#ifndef wxCRT_GetenvW
+WXDLLIMPEXP_BASE wchar_t* wxCRT_GetenvW(const wchar_t *name)
{
-#if wxUSE_UNICODE
// NB: buffer returned by getenv() is allowed to be overwritten next
// time getenv() is called, so it is OK to use static string
// buffer to hold the data.
- static wxWCharBuffer value((wxChar*)NULL);
- value = wxConvLibc.cMB2WX(getenv(wxConvLibc.cWX2MB(name)));
+ static wxWCharBuffer value;
+ value = wxConvLibc.cMB2WC(getenv(wxConvLibc.cWC2MB(name)));
return value.data();
-#else
- return getenv(name);
-#endif
-}
-
-int WXDLLEXPORT wxSystem(const wxChar *psz)
-{
- return system(wxConvLibc.cWX2MB(psz));
}
+#endif // !wxCRT_GetenvW
-#endif // wxNEED_WX_STDLIB_H
-
-#ifdef wxNEED_WX_TIME_H
-WXDLLEXPORT size_t
-wxStrftime(wxChar *s, size_t maxsize, const wxChar *fmt, const struct tm *tm)
+#ifndef wxCRT_StrftimeW
+WXDLLIMPEXP_BASE size_t
+wxCRT_StrftimeW(wchar_t *s, size_t maxsize, const wchar_t *fmt, const struct tm *tm)
{
if ( !maxsize )
return 0;
if ( !wbuf )
return 0;
- wxStrncpy(s, wbuf, maxsize);
- return wxStrlen(s);
-}
-#endif // wxNEED_WX_TIME_H
-
-#ifndef wxCtime
-WXDLLEXPORT wxChar *wxCtime(const time_t *timep)
-{
- // normally the string is 26 chars but give one more in case some broken
- // DOS compiler decides to use "\r\n" instead of "\n" at the end
- static wxChar buf[27];
-
- // ctime() is guaranteed to return a string containing only ASCII
- // characters, as its format is always the same for any locale
- wxStrncpy(buf, wxString::FromAscii(ctime(timep)), WXSIZEOF(buf));
- buf[WXSIZEOF(buf) - 1] = _T('\0');
-
- return buf;
+ wxCRT_StrncpyW(s, wbuf, maxsize);
+ return wxCRT_StrlenW(s);
}
-#endif // wxCtime
+#endif // !wxCRT_StrftimeW
-#endif // wxUSE_WCHAR_T
-
-#ifndef wxStrtoll
-static wxULongLong_t wxStrtoullBase(const wxChar* nptr, wxChar** endptr, int base, wxChar* sign)
+#ifdef wxLongLong_t
+template<typename T>
+static wxULongLong_t
+wxCRT_StrtoullBase(const T* nptr, T** endptr, int base, T* sign)
{
wxULongLong_t sum = 0;
wxString wxstr(nptr);
wxString::const_iterator end = wxstr.end();
// Skip spaces
- while ( i != end && wxIsspace(*i) ) i++;
+ while ( i != end && wxIsspace(*i) ) ++i;
// Starts with sign?
*sign = wxT(' ');
if ( i != end )
{
- wxChar c = *i;
+ T c = *i;
if ( c == wxT('+') || c == wxT('-') )
{
*sign = c;
- i++;
+ ++i;
}
}
- // Starts with 0x?
+ // Starts with octal or hexadecimal prefix?
if ( i != end && *i == wxT('0') )
{
- i++;
+ ++i;
if ( i != end )
{
- if ( *i == wxT('x') && (base == 16 || base == 0) )
+ if ( (*i == wxT('x')) || (*i == wxT('X')) )
{
- base = 16;
- i++;
+ // Hexadecimal prefix: use base 16 if auto-detecting.
+ if ( base == 0 )
+ base = 16;
+
+ // If we do use base 16, just skip "x" as well.
+ if ( base == 16 )
+ {
+ ++i;
+ }
+ else // Not using base 16
+ {
+ // Then it's an error.
+ if ( endptr )
+ *endptr = (T*) nptr;
+ wxSET_ERRNO(EINVAL);
+ return sum;
+ }
}
- else
+ else if ( base == 0 )
{
- if ( endptr )
- *endptr = (wxChar*) nptr;
- wxSET_ERRNO(EINVAL);
- return sum;
+ base = 8;
}
}
else
- i--;
+ --i;
}
if ( base == 0 )
base = 10;
- for ( ; i != end; i++ )
+ for ( ; i != end; ++i )
{
unsigned int n;
- wxChar c = *i;
- if ( c >= wxT('0') )
+ T c = *i;
+ if ( c >= '0' )
{
- if ( c <= wxT('9') )
+ if ( c <= '9' )
n = c - wxT('0');
else
n = wxTolower(c) - wxT('a') + 10;
if ( endptr )
{
- const wxChar& endref = *i;
- *endptr = &(wxChar&)endref;
+ *endptr = (T*)(nptr + (i - wxstr.begin()));
}
return sum;
}
-wxULongLong_t wxStrtoull(const wxChar* nptr, wxChar** endptr, int base)
+template<typename T>
+static wxULongLong_t wxCRT_DoStrtoull(const T* nptr, T** endptr, int base)
{
- wxChar sign;
- wxULongLong_t uval = wxStrtoullBase(nptr, endptr, base, &sign);
+ T sign;
+ wxULongLong_t uval = ::wxCRT_StrtoullBase(nptr, endptr, base, &sign);
if ( sign == wxT('-') )
{
return uval;
}
-wxLongLong_t wxStrtoll(const wxChar* nptr, wxChar** endptr, int base)
+template<typename T>
+static wxLongLong_t wxCRT_DoStrtoll(const T* nptr, T** endptr, int base)
{
- wxChar sign;
- wxULongLong_t uval = wxStrtoullBase(nptr, endptr, base, &sign);
+ T sign;
+ wxULongLong_t uval = ::wxCRT_StrtoullBase(nptr, endptr, base, &sign);
wxLongLong_t val = 0;
if ( sign == wxT('-') )
{
- if ( uval <= wxULL(wxINT64_MAX+1) )
+ if (uval <= (wxULongLong_t)wxINT64_MAX + 1)
{
- if ( uval == wxULL(wxINT64_MAX+1))
- val = -((wxLongLong_t)wxINT64_MAX) - 1;
- else
- val = -((wxLongLong_t)uval);
+ val = -(wxLongLong_t)uval;
}
else
{
return val;
}
-#endif // wxStrtoll
+
+#ifndef wxCRT_StrtollA
+wxLongLong_t wxCRT_StrtollA(const char* nptr, char** endptr, int base)
+ { return wxCRT_DoStrtoll(nptr, endptr, base); }
+#endif
+#ifndef wxCRT_StrtollW
+wxLongLong_t wxCRT_StrtollW(const wchar_t* nptr, wchar_t** endptr, int base)
+ { return wxCRT_DoStrtoll(nptr, endptr, base); }
+#endif
+
+#ifndef wxCRT_StrtoullA
+wxULongLong_t wxCRT_StrtoullA(const char* nptr, char** endptr, int base)
+ { return wxCRT_DoStrtoull(nptr, endptr, base); }
+#endif
+#ifndef wxCRT_StrtoullW
+wxULongLong_t wxCRT_StrtoullW(const wchar_t* nptr, wchar_t** endptr, int base)
+ { return wxCRT_DoStrtoull(nptr, endptr, base); }
+#endif
+
+#endif // wxLongLong_t
// ----------------------------------------------------------------------------
-// functions which we may need even if !wxUSE_WCHAR_T
+// strtok() functions
// ----------------------------------------------------------------------------
-#ifndef wxStrtok
-
-WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
+template<typename T>
+static T *wxCRT_DoStrtok(T *psz, const T *delim, T **save_ptr)
{
if (!psz)
{
psz += wxStrspn(psz, delim);
if (!*psz)
{
- *save_ptr = (wxChar *)NULL;
- return (wxChar *)NULL;
+ *save_ptr = NULL;
+ return NULL;
}
- wxChar *ret = psz;
+ T *ret = psz;
psz = wxStrpbrk(psz, delim);
if (!psz)
{
- *save_ptr = (wxChar*)NULL;
+ *save_ptr = NULL;
}
else
{
return ret;
}
-#endif // wxStrtok
+#ifndef wxCRT_StrtokA
+char *wxCRT_StrtokA(char *psz, const char *delim, char **save_ptr)
+ { return wxCRT_DoStrtok(psz, delim, save_ptr); }
+#endif
+#ifndef wxCRT_StrtokW
+wchar_t *wxCRT_StrtokW(wchar_t *psz, const wchar_t *delim, wchar_t **save_ptr)
+ { return wxCRT_DoStrtok(psz, delim, save_ptr); }
+#endif
// ----------------------------------------------------------------------------
// missing C RTL functions
#endif // __WXWINCE__ <= 211
-#ifdef __WXWINCE__
-
-int wxRemove(const wxChar *path)
-{
- return ::DeleteFile(path) == 0;
-}
-
-#endif
-
-
-// ----------------------------------------------------------------------------
+// ============================================================================
// wxLocaleIsUtf8
-// ----------------------------------------------------------------------------
+// ============================================================================
#if wxUSE_UNICODE_UTF8
{
// "UTF-8" is used by modern glibc versions, but test other variants
// as well, just in case:
- return strcmp(charset, "UTF-8") == 0 ||
- strcmp(charset, "utf-8") == 0 ||
- strcmp(charset, "UTF8") == 0 ||
- strcmp(charset, "utf8") == 0;
+ if ( strcmp(charset, "UTF-8") == 0 ||
+ strcmp(charset, "utf-8") == 0 ||
+ strcmp(charset, "UTF8") == 0 ||
+ strcmp(charset, "utf8") == 0 )
+ {
+ return true;
+ }
}
- else // nl_langinfo() failed
-#endif
+#endif // HAVE_LANGINFO_H
+
+ // check if we're running under the "C" locale: it is 7bit subset
+ // of UTF-8, so it can be safely used with the UTF-8 build:
+ const char *lc_ctype = setlocale(LC_CTYPE, NULL);
+ if ( lc_ctype &&
+ (strcmp(lc_ctype, "C") == 0 || strcmp(lc_ctype, "POSIX") == 0) )
{
- // we don't know what charset libc is using, so assume the worst
- // to be safe:
- return false;
+ return true;
}
+
+ // we don't know what charset libc is using, so assume the worst
+ // to be safe:
+ return false;
}
void wxUpdateLocaleIsUtf8()
#if wxUSE_UTF8_LOCALE_ONLY
if ( !wxIsLocaleUtf8() )
{
- wxLogFatalError(_T("This program requires UTF-8 locale to run."));
+ wxLogFatalError(wxT("This program requires UTF-8 locale to run."));
}
#else // !wxUSE_UTF8_LOCALE_ONLY
wxLocaleIsUtf8 = wxIsLocaleUtf8();
#endif
}
-#endif // wxUSE_UTF8_LOCALE_ONLY
+#endif // wxUSE_UNICODE_UTF8
+
+// ============================================================================
+// wx wrappers for CRT functions
+// ============================================================================
+
+#if wxUSE_UNICODE_WCHAR
+ #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW
+#elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
+ #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \
+ return_kw wxLocaleIsUtf8 ? callA : callW
+#else // ANSI or UTF8 only
+ #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA
+#endif
+
+int wxPuts(const wxString& s)
+{
+ // under IRIX putws() takes a non-const argument so use wchar_str() instead
+ // of wc_str()
+ CALL_ANSI_OR_UNICODE(return,
+ wxCRT_PutsA(s.mb_str()),
+ wxCRT_PutsW(s.wchar_str()));
+}
+
+int wxFputs(const wxString& s, FILE *stream)
+{
+ CALL_ANSI_OR_UNICODE(return,
+ wxCRT_FputsA(s.mb_str(), stream),
+ wxCRT_FputsW(s.wc_str(), stream));
+}
+
+int wxFputc(const wxUniChar& c, FILE *stream)
+{
+#if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build
+ return wxCRT_FputcA((char)c, stream);
+#else
+ CALL_ANSI_OR_UNICODE(return,
+ wxCRT_FputsA(c.AsUTF8(), stream),
+ wxCRT_FputcW((wchar_t)c, stream));
+#endif
+}
+
+#ifdef wxCRT_PerrorA
+
+void wxPerror(const wxString& s)
+{
+#ifdef wxCRT_PerrorW
+ CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE,
+ wxCRT_PerrorA(s.mb_str()),
+ wxCRT_PerrorW(s.wc_str()));
+#else
+ wxCRT_PerrorA(s.mb_str());
+#endif
+}
+
+#endif // wxCRT_PerrorA
+
+wchar_t *wxFgets(wchar_t *s, int size, FILE *stream)
+{
+ wxCHECK_MSG( s, NULL, "empty buffer passed to wxFgets()" );
+
+ wxCharBuffer buf(size - 1);
+ // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide
+ // characters may be encoded by up to 'size'*4 bytes), but what
+ // else can we do?
+ if ( wxFgets(buf.data(), size, stream) == NULL )
+ return NULL;
+
+ if ( wxConvLibc.ToWChar(s, size, buf, wxNO_LEN) == wxCONV_FAILED )
+ return NULL;
+
+ return s;
+}
+
+// ----------------------------------------------------------------------------
+// wxScanf() and friends
+// ----------------------------------------------------------------------------
+
+#ifdef HAVE_VSSCANF // __VISUALC__ and __DMC__ see wx/crt.h
+int wxVsscanf(const char *str, const char *format, va_list ap)
+ { return wxCRT_VsscanfA(str, format, ap); }
+int wxVsscanf(const wchar_t *str, const wchar_t *format, va_list ap)
+ { return wxCRT_VsscanfW(str, format, ap); }
+int wxVsscanf(const wxCharBuffer& str, const char *format, va_list ap)
+ { return wxCRT_VsscanfA(static_cast<const char*>(str), format, ap); }
+int wxVsscanf(const wxWCharBuffer& str, const wchar_t *format, va_list ap)
+ { return wxCRT_VsscanfW(str, format, ap); }
+int wxVsscanf(const wxString& str, const char *format, va_list ap)
+ { return wxCRT_VsscanfA(static_cast<const char*>(str.mb_str()), format, ap); }
+int wxVsscanf(const wxString& str, const wchar_t *format, va_list ap)
+ { return wxCRT_VsscanfW(str.wc_str(), format, ap); }
+int wxVsscanf(const wxCStrData& str, const char *format, va_list ap)
+ { return wxCRT_VsscanfA(static_cast<const char*>(str.AsCharBuf()), format, ap); }
+int wxVsscanf(const wxCStrData& str, const wchar_t *format, va_list ap)
+ { return wxCRT_VsscanfW(str.AsWCharBuf(), format, ap); }
+#endif // HAVE_NO_VSSCANF