From 6a2b6dd1a74b7886da8ba569f623cde7db513fd1 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Fri, 5 Oct 2007 15:17:28 +0000 Subject: [PATCH] generate, libi2ncommon: (tomj) fix custom spam folders with umlauts (#1106) --- src/stringfunc.cpp | 32 ++++++++++++++++++++++++++++++++ src/stringfunc.hxx | 1 + test/stringfunc.cpp | 14 ++++++++++++++ 3 files changed, 47 insertions(+), 0 deletions(-) diff --git a/src/stringfunc.cpp b/src/stringfunc.cpp index c041609..8c67b97 100644 --- a/src/stringfunc.cpp +++ b/src/stringfunc.cpp @@ -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 > &tokenized, const std::string &input) { diff --git a/src/stringfunc.hxx b/src/stringfunc.hxx index d33b679..196ae61 100644 --- a/src/stringfunc.hxx +++ b/src/stringfunc.hxx @@ -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); diff --git a/test/stringfunc.cpp b/test/stringfunc.cpp index e6b3efc..c2a1c96 100644 --- a/test/stringfunc.cpp +++ b/test/stringfunc.cpp @@ -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("Ď ༱ 肈"), 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); -- 1.7.1