diff options
Diffstat (limited to 'src/vmime/charset.cpp')
-rw-r--r-- | src/vmime/charset.cpp | 171 |
1 files changed, 96 insertions, 75 deletions
diff --git a/src/vmime/charset.cpp b/src/vmime/charset.cpp index 1a291106..8828c563 100644 --- a/src/vmime/charset.cpp +++ b/src/vmime/charset.cpp @@ -1,6 +1,6 @@ // // VMime library (http://www.vmime.org) -// Copyright (C) 2002-2013 Vincent Richard <[email protected]> +// Copyright (C) 2002 Vincent Richard <[email protected]> // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as @@ -32,74 +32,93 @@ -namespace vmime -{ +namespace vmime { charset::charset() - : m_name(charsets::US_ASCII) -{ + : m_name(charsets::US_ASCII) { + } charset::charset(const string& name) - : m_name(name) -{ + : m_name(name) { + // If we receive this rfc-1642 valid MIME charset, convert it to something usefull for iconv - if (utility::stringUtils::isStringEqualNoCase(m_name, "unicode-1-1-utf-7")) + if (utility::stringUtils::isStringEqualNoCase(m_name, "unicode-1-1-utf-7")) { m_name = "utf-7"; + } } charset::charset(const char* name) - : m_name(name) -{ + : m_name(name) { + } -void charset::parseImpl - (const parsingContext& /* ctx */, const string& buffer, const size_t position, - const size_t end, size_t* newPosition) -{ - m_name = utility::stringUtils::trim - (string(buffer.begin() + position, buffer.begin() + end)); +void charset::parseImpl( + const parsingContext& /* ctx */, + const string& buffer, + const size_t position, + const size_t end, + size_t* newPosition +) { + + m_name = utility::stringUtils::trim( + string(buffer.begin() + position, buffer.begin() + end) + ); // If we parsed this rfc-1642 valid MIME charset, convert it to something usefull for iconv - if (utility::stringUtils::isStringEqualNoCase(m_name, "unicode-1-1-utf-7")) + if (utility::stringUtils::isStringEqualNoCase(m_name, "unicode-1-1-utf-7")) { m_name = "utf-7"; + } setParsedBounds(position, end); - if (newPosition) + if (newPosition) { *newPosition = end; + } } -void charset::generateImpl - (const generationContext& /* ctx */, utility::outputStream& os, - const size_t curLinePos, size_t* newLinePos) const -{ +void charset::generateImpl( + const generationContext& /* ctx */, + utility::outputStream& os, + const size_t curLinePos, + size_t* newLinePos +) const { + os << m_name; - if (newLinePos) + if (newLinePos) { *newLinePos = curLinePos + m_name.length(); + } } -void charset::convert(utility::inputStream& in, utility::outputStream& out, - const charset& source, const charset& dest, - const charsetConverterOptions& opts) -{ +void charset::convert( + utility::inputStream& in, + utility::outputStream& out, + const charset& source, + const charset& dest, + const charsetConverterOptions& opts +) { + shared_ptr <charsetConverter> conv = charsetConverter::create(source, dest, opts); conv->convert(in, out); } -void charset::convert(const string& in, string& out, const charset& source, const charset& dest, - const charsetConverterOptions& opts) -{ - if (source == dest) - { +void charset::convert( + const string& in, + string& out, + const charset& source, + const charset& dest, + const charsetConverterOptions& opts +) { + + if (source == dest) { out = in; return; } @@ -109,27 +128,26 @@ void charset::convert(const string& in, string& out, const charset& source, cons } -bool charset::isValidText - (const string& text, string::size_type* firstInvalidByte) const -{ +bool charset::isValidText(const string& text, string::size_type* firstInvalidByte) const { + charsetConverterOptions opts; opts.silentlyReplaceInvalidSequences = false; charsetConverter::status st; - try - { + try { + std::string out; // Try converting to UTF-8 shared_ptr <charsetConverter> conv = charsetConverter::create(*this, vmime::charset("utf-8"), opts); conv->convert(text, out, &st); - } - catch (exceptions::illegal_byte_sequence_for_charset& e) - { + + } catch (exceptions::illegal_byte_sequence_for_charset& e) { + // An illegal byte sequence was found in the input buffer - if (firstInvalidByte) - { + if (firstInvalidByte) { + if (st.inputBytesRead < text.length()) *firstInvalidByte = st.inputBytesRead; else @@ -139,77 +157,79 @@ bool charset::isValidText return false; } - if (firstInvalidByte) + if (firstInvalidByte) { *firstInvalidByte = text.length(); + } return true; } -const charset charset::getLocalCharset() -{ - return (platform::getHandler()->getLocalCharset()); +const charset charset::getLocalCharset() { + + return platform::getHandler()->getLocalCharset(); } -charset& charset::operator=(const charset& other) -{ +charset& charset::operator=(const charset& other) { + copyFrom(other); - return (*this); + return *this; } -bool charset::operator==(const charset& value) const -{ - return (utility::stringUtils::isStringEqualNoCase(m_name, value.m_name)); +bool charset::operator==(const charset& value) const { + + return utility::stringUtils::isStringEqualNoCase(m_name, value.m_name); } -bool charset::operator!=(const charset& value) const -{ +bool charset::operator!=(const charset& value) const { + return !(*this == value); } -shared_ptr <component> charset::clone() const -{ +shared_ptr <component> charset::clone() const { + return make_shared <charset>(m_name); } -const string& charset::getName() const -{ - return (m_name); +const string& charset::getName() const { + + return m_name; } -void charset::copyFrom(const component& other) -{ +void charset::copyFrom(const component& other) { + m_name = dynamic_cast <const charset&>(other).m_name; } -const std::vector <shared_ptr <component> > charset::getChildComponents() -{ +const std::vector <shared_ptr <component> > charset::getChildComponents() { + return std::vector <shared_ptr <component> >(); } // Explicitly force encoding for some charsets -struct CharsetEncodingEntry -{ +struct CharsetEncodingEntry { + CharsetEncodingEntry(const string& charset_, const string& encoding_) - : charset(charset_), encoding(encoding_) - { + : charset(charset_), encoding(encoding_) { + } const string charset; const string encoding; }; -CharsetEncodingEntry g_charsetEncodingMap[] = -{ + +CharsetEncodingEntry g_charsetEncodingMap[] = { + // Use QP encoding for ISO-8859-x charsets CharsetEncodingEntry("iso-8859", encodingTypes::QUOTED_PRINTABLE), CharsetEncodingEntry("iso8859", encodingTypes::QUOTED_PRINTABLE), @@ -226,15 +246,16 @@ CharsetEncodingEntry g_charsetEncodingMap[] = }; -bool charset::getRecommendedEncoding(encoding& enc) const -{ +bool charset::getRecommendedEncoding(encoding& enc) const { + // Special treatment for some charsets const string cset = utility::stringUtils::toLower(getName()); - for (unsigned int i = 0 ; i < (sizeof(g_charsetEncodingMap) / sizeof(g_charsetEncodingMap[0])) - 1 ; ++i) - { - if (cset.find(g_charsetEncodingMap[i].charset) != string::npos) - { + for (unsigned int i = 0 ; + i < (sizeof(g_charsetEncodingMap) / sizeof(g_charsetEncodingMap[0])) - 1 ; + ++i) { + + if (cset.find(g_charsetEncodingMap[i].charset) != string::npos) { enc = g_charsetEncodingMap[i].encoding; return true; } |