+ }
+ done = true;
+ break;
+
+ case wxT('s'):
+ if (ilen == -1)
+ {
+ // Unicode mode wx extension: we'll let %hs mean non-Unicode
+ // strings (when in ANSI mode, %s == %hs == ANSI string)
+ type = wxPAT_PCHAR;
+ }
+ else if (ilen == 1)
+ {
+ // in Unicode mode, %ls == %s == Unicode string
+ // in ANSI mode, %ls == Unicode string
+ type = wxPAT_PWCHAR;
+ }
+ else
+ {
+#if wxUSE_UNICODE
+ type = wxPAT_PWCHAR;
+#else
+ type = wxPAT_PCHAR;
+#endif
+ }
+ done = true;
+ break;
+
+ case wxT('n'):
+ if (ilen == 0)
+ type = wxPAT_NINT;
+ else if (ilen == -1)
+ type = wxPAT_NSHORTINT;
+ else if (ilen >= 1)
+ type = wxPAT_NLONGINT;
+ done = true;
+ break;
+
+ default:
+ // bad format, don't consider this an argument;
+ // leave it unchanged
+ return false;
+ }
+ }
+ while (!done);
+
+ return true; // parsing was successful
+}
+
+
+void wxPrintfConvSpec::ReplaceAsteriskWith(int w)
+{
+ char temp[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
+
+ // find the first * in our flag buffer
+ char *pwidth = strchr(szFlags, '*');
+ wxASSERT(pwidth);
+
+ // save what follows the * (the +1 is to skip it!)
+ strcpy(temp, pwidth+1);
+ if (w < 0) {
+ pwidth[0] = '-';
+ pwidth++;
+ }
+
+ // replace * with the actual integer given as width
+ int offset = ::sprintf(pwidth,"%d",abs(w));
+
+ // restore after the expanded * what was following it
+ strcpy(pwidth+offset, temp);
+}
+
+bool wxPrintfConvSpec::LoadArg(wxPrintfArg *p, va_list &argptr)
+{
+ // did the '*' width/precision specifier was used ?
+ if (max_width == -1)
+ {
+ // take the maxwidth specifier from the stack
+ max_width = va_arg(argptr, int);
+ if (max_width < 0)
+ max_width = 0;
+ else
+ ReplaceAsteriskWith(max_width);
+ }
+
+ if (min_width == -1)
+ {
+ // take the minwidth specifier from the stack
+ min_width = va_arg(argptr, int);
+
+ ReplaceAsteriskWith(min_width);
+ if (min_width < 0)
+ {
+ adj_left = !adj_left;
+ min_width = -min_width;
+ }
+ }
+
+ switch (type) {
+ case wxPAT_INT:
+ p->pad_int = va_arg(argptr, int);
+ break;
+ case wxPAT_LONGINT:
+ p->pad_longint = va_arg(argptr, long int);
+ break;
+#if SIZEOF_LONG_LONG
+ case wxPAT_LONGLONGINT:
+ p->pad_longlongint = va_arg(argptr, long long int);
+ break;
+#endif
+ case wxPAT_SIZET:
+ p->pad_sizet = va_arg(argptr, size_t);
+ break;
+ case wxPAT_DOUBLE:
+ p->pad_double = va_arg(argptr, double);
+ break;
+ case wxPAT_LONGDOUBLE:
+ p->pad_longdouble = va_arg(argptr, long double);
+ break;
+ case wxPAT_POINTER:
+ p->pad_pointer = va_arg(argptr, void *);
+ break;
+
+ case wxPAT_CHAR:
+ p->pad_char = (char)va_arg(argptr, int); // char is promoted to int when passed through '...'
+ break;
+ case wxPAT_WCHAR:
+ p->pad_wchar = (wchar_t)va_arg(argptr, int); // char is promoted to int when passed through '...'
+ break;
+
+ case wxPAT_PCHAR:
+ p->pad_pchar = va_arg(argptr, char *);
+ break;
+ case wxPAT_PWCHAR:
+ p->pad_pwchar = va_arg(argptr, wchar_t *);
+ break;
+
+ case wxPAT_NINT:
+ p->pad_nint = va_arg(argptr, int *);
+ break;
+ case wxPAT_NSHORTINT:
+ p->pad_nshortint = va_arg(argptr, short int *);
+ break;
+ case wxPAT_NLONGINT:
+ p->pad_nlongint = va_arg(argptr, long int *);
+ break;
+
+ case wxPAT_INVALID:
+ default:
+ return false;
+ }
+
+ return true; // loading was successful
+}
+
+int wxPrintfConvSpec::Process(wxChar *buf, size_t lenMax, wxPrintfArg *p)
+{
+ // buffer to avoid dynamic memory allocation each time for small strings
+ static char szScratch[1024];
+ size_t lenCur = 0;
+
+#define APPEND_CH(ch) \
+ { \
+ if ( lenCur == lenMax ) \
+ return -1; \
+ \
+ buf[lenCur++] = ch; \
+ }
+
+#define APPEND_STR(s) \
+ { \
+ for ( const wxChar *p = s; *p; p++ ) \
+ { \
+ APPEND_CH(*p); \
+ } \
+ }
+
+ switch ( type )
+ {
+ case wxPAT_INT:
+ ::sprintf(szScratch, szFlags, p->pad_int);
+ break;
+
+ case wxPAT_LONGINT:
+ ::sprintf(szScratch, szFlags, p->pad_longint);
+ break;
+
+#if SIZEOF_LONG_LONG
+ case wxPAT_LONGLONGINT:
+ ::sprintf(szScratch, szFlags, p->pad_longlongint);
+ break;
+#endif // SIZEOF_LONG_LONG
+
+ case wxPAT_SIZET:
+ ::sprintf(szScratch, szFlags, p->pad_sizet);
+ break;
+
+ case wxPAT_LONGDOUBLE:
+ ::sprintf(szScratch, szFlags, p->pad_longdouble);
+ break;
+
+ case wxPAT_DOUBLE:
+ ::sprintf(szScratch, szFlags, p->pad_double);
+ break;
+
+ case wxPAT_POINTER:
+ ::sprintf(szScratch, szFlags, p->pad_pointer);
+ break;
+
+ case wxPAT_CHAR:
+ case wxPAT_WCHAR:
+ {
+ wxChar val =
+#if wxUSE_UNICODE
+ p->pad_wchar;
+
+ if (type == wxPAT_CHAR) {
+ // user passed a character explicitely indicated as ANSI...
+ const char buf[2] = { p->pad_char, 0 };
+ val = wxString(buf, wxConvLibc)[0u];
+ }
+#else
+ p->pad_char;
+
+#if wxUSE_WCHAR_T
+ if (type == wxPAT_WCHAR) {
+ // user passed a character explicitely indicated as Unicode...
+ const wchar_t buf[2] = { p->pad_wchar, 0 };
+ val = wxString(buf, wxConvLibc)[0u];
+ }
+#endif
+#endif
+
+ size_t i;
+
+ if (!adj_left)
+ for (i = 1; i < (size_t)min_width; i++)
+ APPEND_CH(_T(' '));
+
+ APPEND_CH(val);
+
+ if (adj_left)
+ for (i = 1; i < (size_t)min_width; i++)
+ APPEND_CH(_T(' '));
+ }
+ break;
+
+ case wxPAT_PCHAR:
+ case wxPAT_PWCHAR:
+ {
+ wxString s;
+ const wxChar *val =
+#if wxUSE_UNICODE
+ p->pad_pwchar;
+
+ if (type == wxPAT_PCHAR) {
+ // user passed a string explicitely indicated as ANSI...
+ val = s = wxString(p->pad_pchar, wxConvLibc);
+ }
+#else
+ p->pad_pchar;
+
+#if wxUSE_WCHAR_T
+ if (type == wxPAT_PWCHAR) {
+ // user passed a string explicitely indicated as Unicode...
+ val = s = wxString(p->pad_pwchar, wxConvLibc);
+ }
+#endif
+#endif
+ int len;
+
+ if (val)
+ {
+#if wxUSE_STRUTILS
+ // at this point we are sure that max_width is positive or null
+ // (see top of wxPrintfConvSpec::LoadArg)
+ len = wxMin((unsigned int)max_width, wxStrlen(val));
+#else
+ for ( len = 0; val[len] && (len < max_width); len++ )
+ ;
+#endif
+ }
+ else if (max_width >= 6)
+ {
+ val = wxT("(null)");
+ len = 6;
+ }
+ else
+ {
+ val = wxEmptyString;
+ len = 0;
+ }
+
+ int i;
+
+ if (!adj_left)
+ {
+ for (i = len; i < min_width; i++)
+ APPEND_CH(_T(' '));
+ }
+
+#if wxUSE_STRUTILS
+ // at this point we are sure that max_width is positive or null
+ // (see top of wxPrintfConvSpec::LoadArg)
+ len = wxMin((unsigned int)len, lenMax-lenCur);
+ wxStrncpy(buf+lenCur, val, len);
+ lenCur += len;
+#else
+ for (i = 0; i < len; i++)
+ APPEND_CH(val[i]);
+#endif
+
+ if (adj_left)
+ {
+ for (i = len; i < min_width; i++)
+ APPEND_CH(_T(' '));
+ }
+ }
+ break;
+
+ case wxPAT_NINT:
+ *p->pad_nint = lenCur;
+ break;
+
+ case wxPAT_NSHORTINT:
+ *p->pad_nshortint = (short int)lenCur;
+ break;
+
+ case wxPAT_NLONGINT:
+ *p->pad_nlongint = lenCur;
+ break;
+
+ case wxPAT_INVALID:
+ default:
+ return -1;
+ }
+
+ // if we used system's sprintf() then we now need to append the s_szScratch
+ // buffer to the given one...
+ switch (type)
+ {
+ case wxPAT_INT:
+ case wxPAT_LONGINT:
+#if SIZEOF_LONG_LONG
+ case wxPAT_LONGLONGINT:
+#endif
+ case wxPAT_SIZET:
+ case wxPAT_LONGDOUBLE:
+ case wxPAT_DOUBLE:
+ case wxPAT_POINTER:
+#if wxUSE_STRUTILS
+ {
+ const wxMB2WXbuf tmp = wxConvLibc.cMB2WX(szScratch);
+ size_t len = wxMin(lenMax, wxStrlen(tmp));
+ wxStrncpy(buf, tmp, len);
+ lenCur += len;
+ }
+#else
+ {
+ const wxMB2WXbuf tmp =
+ wxConvLibc.cMB2WX(szScratch);
+ APPEND_STR(tmp);
+ }
+#endif
+ break;
+
+ default:
+ break; // all other cases were completed previously
+ }
+
+ return lenCur;
+}
+
+// differences from standard strncpy:
+// 1) copies everything from 'source' except for '%%' sequence which is copied as '%'
+// 2) returns the number of written characters in 'dest' as it could differ from given 'n'
+// 3) much less optimized, unfortunately...
+static int wxCopyStrWithPercents(wxChar *dest, const wxChar *source, size_t n)
+{
+ size_t written = 0;
+
+ if (n == 0)
+ return 0;
+
+ size_t i;
+ for ( i = 0; i < n-1; source++, i++)
+ {
+ dest[written++] = *source;
+ if (*(source+1) == wxT('%'))
+ {
+ // skip this additional '%' character
+ source++;
+ i++;
+ }
+ }
+
+ if (i < n)
+ // copy last character inconditionally
+ dest[written++] = *source;
+
+ return written;
+}
+
+int WXDLLEXPORT wxVsnprintf_(wxChar *buf, size_t lenMax,
+ const wxChar *format, va_list argptr)
+{
+ // cached data
+ static wxPrintfConvSpec arg[wxMAX_SVNPRINTF_ARGUMENTS];
+ static wxPrintfArg argdata[wxMAX_SVNPRINTF_ARGUMENTS];
+ static wxPrintfConvSpec *pspec[wxMAX_SVNPRINTF_ARGUMENTS] = { NULL };
+
+ size_t i;
+
+ // number of characters in the buffer so far, must be less than lenMax
+ size_t lenCur = 0;
+
+ size_t nargs = 0;
+ const wxChar *toparse = format;
+
+ // parse the format string
+ bool posarg_present = false, nonposarg_present = false;
+ for (; *toparse != wxT('\0'); toparse++)
+ {
+ if (*toparse == wxT('%') )
+ {
+ arg[nargs].Init();
+
+ // let's see if this is a (valid) conversion specifier...
+ if (arg[nargs].Parse(toparse))
+ {
+ // ...yes it is
+ wxPrintfConvSpec *current = &arg[nargs];
+
+ // make toparse point to the end of this specifier
+ toparse = current->argend;
+
+ if (current->pos > 0) {
+ // the positionals start from number 1... adjust the index
+ current->pos--;
+ posarg_present = true;
+ } else {
+ // not a positional argument...
+ current->pos = nargs;
+ nonposarg_present = true;
+ }
+
+ // this conversion specifier is tied to the pos-th argument...
+ pspec[current->pos] = current;
+ nargs++;
+
+ if (nargs == wxMAX_SVNPRINTF_ARGUMENTS)
+ break; // cannot handle any additional conv spec
+ }
+ }
+ }
+
+ if (posarg_present && nonposarg_present)
+ return -1; // format strings with both positional and
+ // non-positional conversion specifier are unsupported !!
+
+ // on platforms where va_list is an array type, it is necessary to make a
+ // copy to be able to pass it to LoadArg as a reference.
+ bool ok = true;
+ va_list ap;
+ wxVaCopy(ap, argptr);
+
+ // now load arguments from stack
+ for (i=0; i < nargs && ok; i++) {
+ // !pspec[i] if user forgot a positional parameter (e.g. %$1s %$3s) ?
+ // or LoadArg false if wxPrintfConvSpec::Parse failed to set its 'type'
+ // to a valid value...
+ ok = pspec[i] && pspec[i]->LoadArg(&argdata[i], ap);
+ }
+
+ va_end(ap);
+
+ // something failed while loading arguments from the variable list...
+ if (!ok)
+ return -1;
+
+ // finally, process each conversion specifier with its own argument
+ toparse = format;
+ for (i=0; i < nargs; i++)
+ {
+ // copy in the output buffer the portion of the format string between
+ // last specifier and the current one
+ size_t tocopy = ( arg[i].argpos - toparse );
+ if (lenCur+tocopy >= lenMax)
+ return -1; // not enough space in the output buffer !
+
+ lenCur += wxCopyStrWithPercents(buf+lenCur, toparse, tocopy);
+
+ // process this specifier directly in the output buffer
+ int n = arg[i].Process(buf+lenCur, lenMax - lenCur, &argdata[arg[i].pos]);
+ if (n == -1)
+ return -1; // not enough space in the output buffer !
+ lenCur += n;
+
+ // the +1 is because wxPrintfConvSpec::argend points to the last character
+ // of the format specifier, but we are not interested to it...
+ toparse = arg[i].argend + 1;
+ }
+
+ // copy portion of the format string after last specifier
+ // NOTE: toparse is pointing to the character just after the last processed
+ // conversion specifier
+ // NOTE2: the +1 is because we want to copy also the '\0'
+ size_t tocopy = wxStrlen(format) + 1 - ( toparse - format ) ;
+ if (lenCur+tocopy >= lenMax)
+ return -1; // not enough space in the output buffer !
+
+ // the -1 is because of the '\0'
+ lenCur += wxCopyStrWithPercents(buf+lenCur, toparse, tocopy) - 1;
+
+ // clean the static array portion used...
+ // NOTE: other arrays do not need cleanup!
+ memset(pspec, 0, sizeof(wxPrintfConvSpec*)*nargs);
+
+ wxASSERT(lenCur == wxStrlen(buf));
+ return lenCur;
+}
+
+#undef APPEND_CH
+#undef APPEND_STR
+#undef CHECK_PREC
+
+#endif // !wxVsnprintfA
+
+#if !defined(wxSnprintf_)
+int WXDLLEXPORT wxSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...)
+{
+ va_list argptr;
+ va_start(argptr, format);
+
+ int iLen = wxVsnprintf_(buf, len, format, argptr);
+
+ va_end(argptr);
+
+ return iLen;
+}
+#endif // wxSnprintf_
+
+#if defined(__DMC__)
+ /* Digital Mars adds count to _stprintf (C99) so convert */
+ #if wxUSE_UNICODE
+ int wxSprintf (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)
+{
+ // counting the number of wide characters written isn't worth the trouble,
+ // simply distinguish between ok and error
+ return fputs(wxConvLibc.cWC2MB(ws), stream) == -1 ? -1 : 0;
+}
+#endif // wxNEED_FPUTS
+
+#ifdef wxNEED_PUTS
+int wxPuts(const wxChar *ws)
+{
+ int rc = wxFputs(ws, stdout);
+ if ( rc != -1 )
+ {
+ if ( wxFputs(L"\n", stdout) == -1 )
+ return -1;
+
+ rc++;
+ }
+
+ return rc;
+}
+#endif // wxNEED_PUTS
+
+#ifdef wxNEED_PUTC
+int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream)
+{
+ wchar_t ws[2] = { wc, L'\0' };
+
+ return wxFputs(ws, stream);
+}
+#endif // wxNEED_PUTC
+
+// NB: we only implement va_list functions here, the ones taking ... are
+// defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
+// the definitions there to avoid duplicating them here
+#ifdef wxNEED_WPRINTF
+
+// TODO: implement the scanf() functions
+int vwscanf(const wxChar *format, va_list argptr)
+{
+ wxFAIL_MSG( _T("TODO") );
+
+ return -1;
+}
+
+int vswscanf(const wxChar *ws, const wxChar *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") );
+
+ return -1;
+}
+
+#define vswprintf wxVsnprintf_
+
+int vfwprintf(FILE *stream, const wxChar *format, va_list argptr)
+{
+ wxString s;
+ int rc = s.PrintfV(format, argptr);
+
+ if ( rc != -1 )
+ {
+ // we can't do much better without Unicode support in libc...
+ if ( fprintf(stream, "%s", (const char*)s.mb_str() ) == -1 )
+ return -1;
+ }
+
+ return rc;
+}
+
+int vwprintf(const wxChar *format, va_list argptr)
+{
+ return wxVfprintf(stdout, format, argptr);
+}
+
+#endif // wxNEED_WPRINTF
+
+#ifdef wxNEED_PRINTF_CONVERSION
+
+// ----------------------------------------------------------------------------
+// wxFormatConverter: class doing the "%s" -> "%ls" conversion
+// ----------------------------------------------------------------------------
+
+/*
+ Here are the gory details. We want to follow the Windows/MS conventions,
+ that is to have
+
+ In ANSI mode:
+
+ format specifier results in
+ -----------------------------------
+ %c, %hc, %hC char
+ %lc, %C, %lC wchar_t
+
+ In Unicode mode:
+
+ format specifier results in
+ -----------------------------------
+ %hc, %C, %hC char
+ %c, %lc, %lC wchar_t
+
+
+ while on POSIX systems we have %C identical to %lc and %c always means char
+ (in any mode) while %lc always means wchar_t,
+
+ 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):
+
+ wxWidgets specifier POSIX specifier
+ ----------------------------------------
+
+ %hc, %C, %hC %c
+ %c %lc
+
+
+ And, of course, the same should be done for %s as well.
+*/
+
+class wxFormatConverter
+{
+public:
+ wxFormatConverter(const wxChar *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(); }
+
+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++;
+ }
+
+ return ch;
+ }
+
+ // insert an extra character
+ void InsertFmtChar(wxChar ch)
+ {
+ if ( m_fmtOrig )
+ {
+ // so far we haven't translated anything yet
+ CopyAllBefore();
+ }
+
+ m_fmt += ch;
+ }
+
+ void CopyAllBefore()
+ {
+ wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") );
+
+ m_fmt = wxString(m_fmtOrig, m_nCopied);
+
+ // we won't need it any longer
+ m_fmtOrig = NULL;
+ }
+
+ static bool IsFlagChar(wxChar ch)
+ {
+ return ch == _T('-') || ch == _T('+') ||
+ ch == _T('0') || ch == _T(' ') || ch == _T('#');
+ }
+
+ void SkipDigits(const wxChar **ptpc)
+ {
+ while ( **ptpc >= _T('0') && **ptpc <= _T('9') )
+ CopyFmtChar(*(*ptpc)++);
+ }
+
+ // the translated format
+ wxString m_fmt;