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)
{
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);
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();
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);