From 0f0298b10cc3d4f862e764fcabffce6f426c91a8 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Thu, 18 Jan 2007 20:39:10 +0000
Subject: [PATCH] added wxCSConv::IsOk() (patch 1637944)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44251 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/changes.txt          |  1 +
 docs/latex/wx/csconv.tex  | 18 ++++++++++++++++--
 include/wx/strconv.h      |  5 +++++
 src/common/strconv.cpp    | 13 +++++++++++++
 tests/strings/unicode.cpp | 13 +++++++++++++
 5 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/docs/changes.txt b/docs/changes.txt
index 26f7d97cdc..4e21d3a8f2 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -94,6 +94,7 @@ Major new features in 2.8 release
 All:
 
 - Added wxSizerFlags::Shaped() and FixedMinSize() methods
+- Added wxCSConv::IsOk() (Manuel Martin)
 - Added wxDateTime::GetDateOnly()
 - Made wxTextFile work with unseekable files again (David Hart)
 
diff --git a/docs/latex/wx/csconv.tex b/docs/latex/wx/csconv.tex
index cfd32f7882..42e503e75a 100644
--- a/docs/latex/wx/csconv.tex
+++ b/docs/latex/wx/csconv.tex
@@ -32,8 +32,8 @@ default user character set.
 \func{}{wxCSConv}{\param{wxFontEncoding }{encoding}}
 
 Constructor. You may specify either the name of the character set you want to
-convert from/to or an encoding constant. If the character set name is not
-recognized, ISO 8859-1 is used as fall back.
+convert from/to or an encoding constant. If the character set name (or the
+encoding) is not recognized, ISO 8859-1 is used as fall back.
 
 
 \membersection{wxCSConv::\destruct{wxCSConv}}\label{wxcsconvdtor}
@@ -43,12 +43,26 @@ recognized, ISO 8859-1 is used as fall back.
 Destructor frees any resources needed to perform the conversion.
 
 
+\membersection{wxCSConv::IsOk}\label{wxcsconvisok}
+
+\constfunc{bool}{IsOk}{\void}
+
+Returns \true if the charset (or the encoding) given at constructor is really
+available to use. Returns \false if ISO 8859-1 will be used instead.
+
+Note this does \emph{not} mean that a given string will be correctly converted.
+A malformed string may still make conversion functions return \texttt{wxCONV\_FAILED}.
+
+\newsince{2.8.2}
+
+
 \membersection{wxCSConv::MB2WC}\label{wxcsconvmb2wc}
 
 \constfunc{size\_t}{MB2WC}{\param{wchar\_t* }{buf}, \param{const char* }{psz}, \param{size\_t }{n}}
 
 Converts from the selected character set to Unicode. Returns length of string written to destination buffer.
 
+
 \membersection{wxCSConv::WC2MB}\label{wxcsconvwc2mb}
 
 \constfunc{size\_t}{WC2MB}{\param{char* }{buf}, \param{const wchar\_t* }{psz}, \param{size\_t }{n}}
diff --git a/include/wx/strconv.h b/include/wx/strconv.h
index 947efad799..58ba99ac0c 100644
--- a/include/wx/strconv.h
+++ b/include/wx/strconv.h
@@ -385,6 +385,11 @@ public:
 
     void Clear();
 
+#if wxABI_VERSION >= 20802
+    // return true if the conversion could be initilized successfully
+    bool IsOk() const;
+#endif // wx 2.8.2+
+
 private:
     // common part of all ctors
     void Init();
diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp
index f442e54dcd..bd0a0926c3 100644
--- a/src/common/strconv.cpp
+++ b/src/common/strconv.cpp
@@ -3518,6 +3518,19 @@ void wxCSConv::CreateConvIfNeeded() const
     }
 }
 
+bool wxCSConv::IsOk() const
+{
+    CreateConvIfNeeded();
+
+    // special case: no convReal created for wxFONTENCODING_ISO8859_1
+    if ( m_encoding == wxFONTENCODING_ISO8859_1 )
+        return true; // always ok as we do it ourselves
+
+    // m_convReal->IsOk() is called at its own creation, so we know it must
+    // be ok if m_convReal is non-NULL
+    return m_convReal != NULL;
+}
+
 size_t wxCSConv::ToWChar(wchar_t *dst, size_t dstLen,
                          const char *src, size_t srcLen) const
 {
diff --git a/tests/strings/unicode.cpp b/tests/strings/unicode.cpp
index 7a0303bd89..26686bdd59 100644
--- a/tests/strings/unicode.cpp
+++ b/tests/strings/unicode.cpp
@@ -59,6 +59,7 @@ private:
         CPPUNIT_TEST( ConversionUTF8 );
         CPPUNIT_TEST( ConversionUTF16 );
         CPPUNIT_TEST( ConversionUTF32 );
+        CPPUNIT_TEST( IsConvOk );
 #endif // wxUSE_WCHAR_T
     CPPUNIT_TEST_SUITE_END();
 
@@ -71,6 +72,7 @@ private:
     void ConversionUTF8();
     void ConversionUTF16();
     void ConversionUTF32();
+    void IsConvOk();
 
     // test if converting s using the given encoding gives ws and vice versa
     //
@@ -317,5 +319,16 @@ void UnicodeTestCase::ConversionUTF32()
     CPPUNIT_ASSERT_EQUAL( (size_t)3, len );
 }
 
+void UnicodeTestCase::IsConvOk()
+{
+    CPPUNIT_ASSERT( wxCSConv(wxFONTENCODING_SYSTEM).IsOk() );
+    CPPUNIT_ASSERT( wxCSConv(_T("UTF-8")).IsOk() );
+    CPPUNIT_ASSERT( !wxCSConv(_T("NoSuchConversion")).IsOk() );
+
+#ifdef __WINDOWS__
+    CPPUNIT_ASSERT( wxCSConv(_T("WINDOWS-437")).IsOk() );
+#endif
+}
+
 #endif // wxUSE_WCHAR_T
 
-- 
2.50.0

