+ default:
+ size = Size_Default;
+ }
+
+ CharType outConv = *format;
+ SizeModifier outSize = size;
+
+ // and finally we should have the type
+ switch ( *format )
+ {
+ case wxT('S'):
+ case wxT('s'):
+ // all strings were converted into the same form by
+ // wxArgNormalizer<T>, this form depends on the context
+ // in which the value is used (scanf/printf/wprintf):
+ HandleString(*format, size, outConv, outSize);
+ break;
+
+ case wxT('C'):
+ case wxT('c'):
+ HandleChar(*format, size, outConv, outSize);
+ break;
+
+ default:
+ // nothing special to do
+ break;
+ }
+
+ if ( outConv == *format && outSize == size ) // no change
+ {
+ if ( size != Size_Default )
+ CopyFmtChar(*(format - 1));
+ CopyFmtChar(*format);
+ }
+ else // something changed
+ {
+ switch ( outSize )
+ {
+ case Size_Long:
+ InsertFmtChar(wxT('l'));
+ break;
+
+ case Size_Short:
+ InsertFmtChar(wxT('h'));
+ break;
+
+ case Size_Default:
+ // nothing to do
+ break;
+ }
+ InsertFmtChar(outConv);
+ }
+
+ format++;
+ }
+ }
+
+ // notice that we only translated the string if m_fmtOrig == NULL (as
+ // set by CopyAllBefore()), otherwise we should simply use the original
+ // format
+ if ( m_fmtOrig )
+ {
+ return wxScopedCharTypeBuffer<CharType>::CreateNonOwned(m_fmtOrig);
+ }
+ else
+ {
+ // shrink converted format string to actual size (instead of
+ // over-sized allocation from CopyAllBefore()) and NUL-terminate
+ // it:
+ m_fmt.shrink(m_fmtLast - m_fmt.data());
+ return m_fmt;
+ }
+ }
+
+ virtual ~wxFormatConverterBase() {}
+
+protected:
+ enum SizeModifier
+ {
+ Size_Default,
+ Size_Short,
+ Size_Long
+ };
+
+ // called to handle %S or %s; 'conv' is conversion specifier ('S' or 's'
+ // respectively), 'size' is the preceding size modifier; the new values of
+ // conversion and size specifiers must be written to outConv and outSize
+ virtual void HandleString(CharType conv, SizeModifier size,
+ CharType& outConv, SizeModifier& outSize) = 0;
+
+ // ditto for %C or %c
+ virtual void HandleChar(CharType conv, SizeModifier size,
+ CharType& outConv, SizeModifier& outSize) = 0;
+
+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)
+ CharType CopyFmtChar(CharType ch)
+ {
+ if ( !m_fmtOrig )
+ {
+ // we're translating, do copy
+ *(m_fmtLast++) = 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(CharType ch)
+ {
+ if ( m_fmtOrig )
+ {
+ // so far we haven't translated anything yet
+ CopyAllBefore();
+ }
+
+ *(m_fmtLast++) = ch;
+ }
+
+ void CopyAllBefore()
+ {
+ wxASSERT_MSG( m_fmtOrig && m_fmt.data() == NULL, "logic error" );
+
+ // the modified format string is guaranteed to be no longer than
+ // 3/2 of the original (worst case: the entire format string consists
+ // of "%s" repeated and is expanded to "%ls" on Unix), so we can
+ // allocate the buffer now and not worry about running out of space if
+ // we over-allocate a bit:
+ size_t fmtLen = wxStrlen(m_fmtOrig);
+ // worst case is of even length, so there's no rounding error in *3/2:
+ m_fmt.extend(fmtLen * 3 / 2);
+
+ if ( m_nCopied > 0 )
+ wxStrncpy(m_fmt.data(), m_fmtOrig, m_nCopied);
+ m_fmtLast = m_fmt.data() + m_nCopied;
+
+ // we won't need it any longer and resetting it also indicates that we
+ // modified the format
+ m_fmtOrig = NULL;
+ }
+
+ static bool IsFlagChar(CharType ch)
+ {
+ return ch == wxT('-') || ch == wxT('+') ||
+ ch == wxT('0') || ch == wxT(' ') || ch == wxT('#');
+ }
+
+ void SkipDigits(const CharType **ptpc)
+ {
+ while ( **ptpc >= wxT('0') && **ptpc <= wxT('9') )
+ CopyFmtChar(*(*ptpc)++);
+ }
+
+ // the translated format
+ wxCharTypeBuffer<CharType> m_fmt;
+ CharType *m_fmtLast;
+
+ // the original format
+ const CharType *m_fmtOrig;
+
+ // the number of characters already copied (i.e. already parsed, but left
+ // unmodified)
+ size_t m_nCopied;
+};
+
+#if defined(__WINDOWS__) && !defined(__CYGWIN__)
+
+// on Windows, we should use %s and %c regardless of the build:
+class wxPrintfFormatConverterWchar : public wxFormatConverterBase<wchar_t>
+{
+ virtual void HandleString(CharType WXUNUSED(conv),
+ SizeModifier WXUNUSED(size),
+ CharType& outConv, SizeModifier& outSize)
+ {
+ outConv = 's';
+ outSize = Size_Default;
+ }
+
+ virtual void HandleChar(CharType WXUNUSED(conv),
+ SizeModifier WXUNUSED(size),
+ CharType& outConv, SizeModifier& outSize)
+ {
+ outConv = 'c';
+ outSize = Size_Default;
+ }
+};
+
+#else // !__WINDOWS__
+
+// on Unix, it's %s for ANSI functions and %ls for widechar:
+
+#if !wxUSE_UTF8_LOCALE_ONLY
+class wxPrintfFormatConverterWchar : public wxFormatConverterBase<wchar_t>