aboutsummaryrefslogtreecommitdiffstats
path: root/src/encoding.cpp
diff options
context:
space:
mode:
authorVincent Richard <[email protected]>2004-10-05 10:28:21 +0000
committerVincent Richard <[email protected]>2004-10-05 10:28:21 +0000
commita3229a051381e8f6b6df0fd423186166d20c898f (patch)
tree29dab66e608651e50a9b6f4bf9ce28f2ee897c87 /src/encoding.cpp
downloadvmime-a3229a051381e8f6b6df0fd423186166d20c898f.tar.gz
vmime-a3229a051381e8f6b6df0fd423186166d20c898f.zip
Initial import.
Diffstat (limited to 'src/encoding.cpp')
-rw-r--r--src/encoding.cpp161
1 files changed, 161 insertions, 0 deletions
diff --git a/src/encoding.cpp b/src/encoding.cpp
new file mode 100644
index 00000000..c797cf18
--- /dev/null
+++ b/src/encoding.cpp
@@ -0,0 +1,161 @@
+//
+// VMime library (http://vmime.sourceforge.net)
+// Copyright (C) 2002-2004 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
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+
+#include "encoding.hpp"
+#include "encoderFactory.hpp"
+#include "contentHandler.hpp"
+
+#include <algorithm>
+
+
+namespace vmime
+{
+
+
+encoding::encoding()
+ : m_name(encodingTypes::SEVEN_BIT)
+{
+}
+
+
+encoding::encoding(const string& name)
+ : m_name(toLower(name))
+{
+}
+
+
+encoding::encoding(const encoding& enc)
+ : component(), m_name(enc.m_name)
+{
+}
+
+
+void encoding::parse(const string& buffer, const string::size_type position,
+ const string::size_type end, string::size_type* newPosition)
+{
+ m_name = toLower(string(buffer.begin() + position, buffer.begin() + end));
+
+ if (newPosition)
+ *newPosition = end;
+}
+
+
+void encoding::generate(utility::outputStream& os, const string::size_type /* maxLineLength */,
+ const string::size_type curLinePos, string::size_type* newLinePos) const
+{
+ os << m_name;
+
+ if (newLinePos)
+ *newLinePos = curLinePos + m_name.length();
+}
+
+
+encoder* encoding::getEncoder() const
+{
+ return (encoderFactory::getInstance()->create(generate()));
+}
+
+
+encoding& encoding::operator=(const encoding& source)
+{
+ m_name = source.m_name;
+ return (*this);
+}
+
+
+encoding& encoding::operator=(const string& name)
+{
+ m_name = toLower(name);
+ return (*this);
+}
+
+
+const bool encoding::operator==(const encoding& value) const
+{
+ return (toLower(m_name) == value.m_name);
+}
+
+
+const bool encoding::operator!=(const encoding& value) const
+{
+ return !(*this == value);
+}
+
+
+const encoding encoding::decide
+ (const string::const_iterator begin, const string::const_iterator end)
+{
+ const string::difference_type length = end - begin;
+ const string::difference_type count = std::count_if
+ (begin, end, std::bind2nd(std::less<unsigned char>(), 127));
+
+ // All is in 7-bit US-ASCII --> 7-bit (or Quoted-Printable...)
+ if (length == count)
+ {
+ // Now, we check if there is any line with more than
+ // "lineLengthLimits::convenient" characters (7-bit requires that)
+ string::const_iterator p = begin;
+
+ const string::size_type maxLen = lineLengthLimits::convenient;
+ string::size_type len = 0;
+
+ for ( ; p != end && len <= maxLen ; )
+ {
+ if (*p == '\n')
+ {
+ len = 0;
+ ++p;
+
+ // May or may not need to be encoded, we don't take
+ // any risk (avoid problems with SMTP)
+ if (p != end && *p == '.')
+ len = maxLen + 1;
+ }
+ else
+ {
+ ++len;
+ ++p;
+ }
+ }
+
+ if (len > maxLen)
+ return (encoding(encodingTypes::QUOTED_PRINTABLE));
+ else
+ return (encoding(encodingTypes::SEVEN_BIT));
+ }
+ // Less than 20% non US-ASCII --> Quoted-Printable
+ else if ((length - count) <= length / 5)
+ {
+ return (encoding(encodingTypes::QUOTED_PRINTABLE));
+ }
+ // Otherwise --> Base64
+ else
+ {
+ return (encoding(encodingTypes::BASE64));
+ }
+}
+
+
+const encoding encoding::decide(const contentHandler& /* data */)
+{
+ return (encoding(encodingTypes::BASE64));
+}
+
+
+} // vmime