# HG changeset patch
# User Michael Stahl <mst@openoffice.org>
# Date 1303140562 0
# Node ID 6d5ee77161481fd3b40778f1b3ce3ca4f5823a89
# Parent  0e216ed13561ca17180661ab6ff06c44a79a2a70
rtftok01: writerfilter: add support for text import

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
@@ -29,6 +29,9 @@
 
 #include <algorithm>
 
+#include <rtl/strbuf.hxx>
+#include <rtl/ustrbuf.hxx>
+
 #include <comphelper/stlunosequence.hxx>
 
 #include <resourcemodel/TagLogger.hxx>
@@ -73,6 +76,20 @@
     uno::Reference<io::XInputStream> m_xInputStream;
 };
 
+static char
+lcl_HexDigit(char const c)
+{
+    if (('0' <= c) && (c <= '9')) {
+        return c - '0';
+    }
+    char const l( tolower(c) );
+    if (('a' <= l) && (l <= 'z')) {
+        return 10 + (l - 'a');
+    }
+    OSL_ASSERT(false);
+    return '\0';
+}
+
 class RTFScannerHandlerImpl
     : public RTFScannerHandler
 {
@@ -82,25 +99,83 @@
     { }
     virtual ~RTFScannerHandlerImpl() { }
     virtual void dest(char const*const token, char const*const value)
-    { }
+    {
+        checkStrings();
+    }
     virtual void ctrl(char const*const token, char const*const value)
-    { }
+    {
+        checkStrings();
+        if (!strcasecmp(token, "par")) {
+            sal_uInt8 const nl(0x0d);
+            m_rStream.text(& nl, sizeof(nl));
+        }
+    }
     virtual void lbrace()
-    { }
+    {
+        checkStrings();
+    }
     virtual void rbrace()
-    { }
+    {
+        checkStrings();
+    }
     virtual void addSpaces(unsigned int const count)
-    { }
+    {
+        checkUString();
+        for (int i = 0; i < count; ++i) {
+            m_String.append(" ");
+        }
+    }
     virtual void addBinData(unsigned char const data)
-    { }
+    {
+        checkStrings();
+    }
     virtual void addChar(char const ch)
-    { }
+    {
+        checkUString();
+        m_String.append(ch);
+    }
     virtual void addCharU(sal_Unicode const ch)
-    { }
+    {
+        checkString();
+        m_UString.append(ch);
+    }
     virtual void addHexChar(char const hexch[2])
-    { }
+    {
+        checkUString();
+        char const c( (lcl_HexDigit(hexch[0]) << 4) | lcl_HexDigit(hexch[1]) );
+        m_String.append(c);
+    }
+    Stream & GetStream() { return m_rStream; }
+
 private:
+    void checkStrings()
+    {
+        checkString();
+        checkUString();
+    }
+    void checkString()
+    {
+        if (m_String.getLength()) {
+            m_rStream.text(
+                reinterpret_cast<sal_uInt8 const*>(m_String.getStr()),
+                m_String.getLength());
+            m_String.makeStringAndClear();
+        }
+    }
+    void checkUString()
+    {
+        if (m_UString.getLength()) {
+            m_rStream.utext(
+                reinterpret_cast<sal_uInt8 const*>(m_UString.getStr()),
+                m_UString.getLength());
+            m_UString.makeStringAndClear();
+        }
+    }
     Stream & m_rStream;
+    /* use 2 buffers here, because it's not possible to convert 8-bit char
+       to unicode individually (could be mult-byte enconding!) */
+    ::rtl::OStringBuffer m_String;
+    ::rtl::OUStringBuffer m_UString;
 };
 
 
