generate, libi2ncommon: (tomj) fix custom spam folders with umlauts (#1106)
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Fri, 5 Oct 2007 15:17:28 +0000 (15:17 +0000)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Fri, 5 Oct 2007 15:17:28 +0000 (15:17 +0000)
src/stringfunc.cpp
src/stringfunc.hxx
test/stringfunc.cpp

index c041609..8c67b97 100644 (file)
@@ -136,6 +136,38 @@ std::string utf7imap_to_utf8(const std::string& utf7imapstring)
     return result;
 }
 
+std::string utf8_to_utf7imap(const std::string& utf8string)
+{
+    string result;
+
+    iconv_t utf82utf7imap = iconv_open ("UTF-7-IMAP", "UTF-8");
+
+    if (utf82utf7imap == (iconv_t)-1)
+        throw runtime_error("iconv can't convert from UTF-7-IMAP to UTF-8");
+
+    // UTF-7 is base64 encoded, a buffer 10x as large
+    // as the utf-8 buffer should be enough. If not the string will be truncated.
+    size_t in_size=utf8string.size();
+    size_t out_size=in_size*10;
+
+    char *buf = (char *)malloc(out_size+1);
+    if (buf == NULL)
+        throw runtime_error("out of memory for iconv buffer");
+
+    const char *in = utf8string.c_str();
+    char *out = buf;
+    iconv (utf82utf7imap, &in, &in_size, &out, &out_size);
+
+    buf[utf8string.size()*10-out_size]=0;
+
+    result=buf;
+
+    free(buf);
+    iconv_close (utf82utf7imap);
+
+    return result;
+}
+
 // Tokenize string by (html) tags
 void tokenize_by_tag(vector<pair<string,bool> > &tokenized, const std::string &input)
 {
index d33b679..196ae61 100644 (file)
@@ -23,6 +23,7 @@ bool replace_all(std::string &base, const std::string &ist, const std::string &s
 std::string iso_to_utf8(const std::string& isostring);
 std::string utf8_to_iso(const std::string& utf8string);
 std::string utf7imap_to_utf8(const std::string &utf7imapstring);
+std::string utf8_to_utf7imap(const std::string &utf8string);
 
 std::string strip_html_tags(const std::string &input);
 std::string smart_html_entities(const std::string &input);
index e6b3efc..c2a1c96 100644 (file)
@@ -34,6 +34,8 @@ class stringfunc : public TestFixture
     CPPUNIT_TEST(html_entities1);
     CPPUNIT_TEST(html_entities2);
     CPPUNIT_TEST(html_entities3);
+    CPPUNIT_TEST(imaputf7_to_utf8);
+    CPPUNIT_TEST(utf8_to_imaputf7);
 
     CPPUNIT_TEST_SUITE_END();
 
@@ -118,6 +120,18 @@ class stringfunc : public TestFixture
             string output = html_entities("\xC4\x8E \xE0\xBC\xB1 \xE8\x82\x88");
             CPPUNIT_ASSERT_EQUAL(string("&#270; &#3889; &#32904;"), output);
         }
+
+        void imaputf7_to_utf8()
+        {
+            string output = utf7imap_to_utf8("Sp&AOQ-m");
+            CPPUNIT_ASSERT_EQUAL(string("Späm"), output);
+        }
+
+        void utf8_to_imaputf7()
+        {
+            string output = utf8_to_utf7imap("Späm");
+            CPPUNIT_ASSERT_EQUAL(string("Sp&AOQ-m"), output);
+        }
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(stringfunc);