# HG changeset patch
# User Michael Stahl <mst@openoffice.org>
# Date 1303140562 0
# Node ID 8e3529d5590ef9912f2273a8d291aebcb371b505
# Parent  414c34ef3984cc11b259821f49c474c6f80d8705
rtftok01: writerfilter: support for various text encodings

diff --git a/writerfilter/source/rtftok/RTFDocumentImpl.cxx b/writerfilter/source/rtftok/RTFDocumentImpl.cxx
--- a/writerfilter/source/rtftok/RTFDocumentImpl.cxx
+++ b/writerfilter/source/rtftok/RTFDocumentImpl.cxx
@@ -33,6 +33,8 @@
 
 #include <rtl/strbuf.hxx>
 #include <rtl/ustrbuf.hxx>
+#include <rtl/tencinfo.h>
+#include <osl/thread.h>
 
 #include <comphelper/stlunosequence.hxx>
 
@@ -311,6 +313,7 @@
 public:
     explicit RTFScannerHandlerImpl(Stream & rStream)
         : m_rStream(rStream)
+        , m_TextEncoding(osl_getThreadTextEncoding())
     {
         m_Destinations.push( ::std::make_pair(0,
             RTFDestination::Pointer_t(createRootDestination(*this))) );
@@ -352,7 +355,7 @@
     virtual void addSpaces(unsigned int const count)
     {
         checkUString();
-        for (int i = 0; i < count; ++i) {
+        for (unsigned int i = 0; i < count; ++i) {
             m_String.append(" ");
         }
     }
@@ -378,6 +381,12 @@
     }
     Stream & GetStream() { return m_rStream; }
 
+    rtl_TextEncoding GetEncoding() const { return m_TextEncoding; }
+    void SetEncoding(rtl_TextEncoding const e) {
+fprintf(stderr, "RTFScannerHandlerImpl::SetEncoding: %d\n", e);
+        m_TextEncoding = e;
+    }
+
 private:
     void checkStrings()
     {
@@ -404,6 +413,7 @@
     ::rtl::OStringBuffer m_String;
     ::rtl::OUStringBuffer m_UString;
     ::std::stack< ::std::pair<int, RTFDestination::Pointer_t> > m_Destinations;
+    rtl_TextEncoding m_TextEncoding;
 };
 
 
@@ -445,8 +455,7 @@
         {
             ::rtl::OUString name(
                 ::rtl::OStringToOUString(m_Entry.m_Name,
-            //FIXME encoding?
-                    RTL_TEXTENCODING_ASCII_US).trim() );
+                    GetHandler().GetEncoding()).trim() );
             if (name.endsWithAsciiL(";", 1)) { // strip trailing ;
                 name = name.copy(0, name.getLength() - 1);
             }
@@ -510,9 +519,42 @@
         }
         return RTFDestination::dest(token, value);
     }
-    virtual void ctrl(char const*const token, char const*const /*value*/)
+    virtual void ctrl(char const*const token, char const*const value)
     {
-        if (!strcasecmp(token, "par")) {
+        if (!strcasecmp(token, "rtf")) {
+            if (!strcmp(value, "1")) {
+                OSL_TRACE("RTFDestinationRTF: warning: \\rtf version not 1 ?");
+            }
+        }
+        else if (!strcasecmp(token, "pwd")) {
+            if (!strcmp(value, "1")) {
+                OSL_TRACE("RTFDestinationRTF: warning: \\pwd version not 1 ?");
+            }
+        }
+        else if (!strcasecmp(token, "urtf")) {
+            if (!strcmp(value, "1")) {
+                OSL_TRACE("RTFDestinationRTF: warning: \\urtf version not 1 ?");
+            }
+            GetHandler().SetEncoding(RTL_TEXTENCODING_UTF8);
+        }
+        else if (!strcasecmp(token, "ansi")) {
+            GetHandler().SetEncoding(RTL_TEXTENCODING_MS_1252);
+        }
+        else if (!strcasecmp(token, "mac")) {
+            GetHandler().SetEncoding(RTL_TEXTENCODING_APPLE_ROMAN);
+        }
+        else if (!strcasecmp(token, "pc")) {
+            GetHandler().SetEncoding(RTL_TEXTENCODING_IBM_437);
+        }
+        else if (!strcasecmp(token, "pca")) {
+            GetHandler().SetEncoding(RTL_TEXTENCODING_IBM_850);
+        }
+        else if (!strcasecmp(token, "ansicpg")) {
+            sal_Int32 const n( ::rtl::OString(value).toInt32() );
+            rtl_TextEncoding const e(rtl_getTextEncodingFromWindowsCodePage(n));
+            GetHandler().SetEncoding(e);
+        }
+        else if (!strcasecmp(token, "par")) {
             sal_uInt8 const nl(0x0d);
             GetHandler().GetStream().text(& nl, sizeof(nl));
         }
@@ -528,9 +570,11 @@
 #endif
     virtual void HandleString (::rtl::OStringBuffer  & rBuf)
     {
-        GetHandler().GetStream().text(
-            reinterpret_cast<sal_uInt8 const*>(rBuf.getStr()),
-            rBuf.getLength());
+        ::rtl::OUString const s(::rtl::OStringToOUString(
+                    rBuf.makeStringAndClear(), GetHandler().GetEncoding()));
+        GetHandler().GetStream().utext(
+            reinterpret_cast<sal_uInt8 const*>(s.getStr()),
+            s.getLength());
     }
     virtual void HandleStringU(::rtl::OUStringBuffer & rBuf)
     {
