diff options
Diffstat (limited to 'src/utility')
-rw-r--r-- | src/utility/filteredStream.cpp | 115 | ||||
-rw-r--r-- | src/utility/stream.cpp | 43 |
2 files changed, 158 insertions, 0 deletions
diff --git a/src/utility/filteredStream.cpp b/src/utility/filteredStream.cpp new file mode 100644 index 00000000..9586f165 --- /dev/null +++ b/src/utility/filteredStream.cpp @@ -0,0 +1,115 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2005 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 "vmime/utility/filteredStream.hpp" + + +namespace vmime { +namespace utility { + + +// dotFilteredOutputStream + +dotFilteredOutputStream::dotFilteredOutputStream(outputStream& os) + : m_stream(os) +{ +} + + +outputStream& dotFilteredOutputStream::getNextOutputStream() +{ + return (m_stream); +} + + +void dotFilteredOutputStream::write + (const value_type* const data, const size_type count) +{ + const value_type* pos = data; + const value_type* end = data + count; + const value_type* start = data; + + // Replace "\n." with "\n.." + while ((pos = std::find(pos, end, '.')) != end) + { + const value_type previousChar = + (pos == data ? m_previousChar : *(pos - 1)); + + if (previousChar == '\n') + { + m_stream.write(start, pos - data); + m_stream.write("..", 2); + + start = pos + 1; + } + + ++pos; + } + + m_stream.write(start, end - start); + m_previousChar = data[count - 1]; +} + + +// CRLFToLFFilteredOutputStream + +CRLFToLFFilteredOutputStream::CRLFToLFFilteredOutputStream(outputStream& os) + : m_stream(os) +{ +} + + +outputStream& CRLFToLFFilteredOutputStream::getNextOutputStream() +{ + return (m_stream); +} + + +void CRLFToLFFilteredOutputStream::write + (const value_type* const data, const size_type count) +{ + const value_type* pos = data; + const value_type* end = data + count; + const value_type* start = data; + + // Replace "\r\n" (CRLF) with "\n" (LF) + while ((pos = std::find(pos, end, '\n')) != end) + { + const value_type previousChar = + (pos == data ? m_previousChar : *(pos - 1)); + + if (previousChar == '\r') + { + m_stream.write(start, pos - 1 - data); // do not write \r + m_stream.write("\n", 1); + + start = pos + 1; + } + + ++pos; + } + + m_stream.write(start, end - start); + m_previousChar = data[count - 1]; +} + + +} // utility +} // vmime + diff --git a/src/utility/stream.cpp b/src/utility/stream.cpp index 397bf20b..3899eb52 100644 --- a/src/utility/stream.cpp +++ b/src/utility/stream.cpp @@ -23,6 +23,10 @@ #include <algorithm> // for std::copy #include <iterator> // for std::back_inserter +#if VMIME_HAVE_MESSAGING_FEATURES + #include "vmime/messaging/socket.hpp" +#endif + namespace vmime { namespace utility { @@ -46,9 +50,19 @@ outputStream& operator<<(outputStream& os, const string& str) const stream::size_type bufferedStreamCopy(inputStream& is, outputStream& os) { + return bufferedStreamCopy(is, os, 0, NULL); +} + + +const stream::size_type bufferedStreamCopy(inputStream& is, outputStream& os, + const stream::size_type length, progressionListener* progress) +{ stream::value_type buffer[65536]; stream::size_type total = 0; + if (progress != NULL) + progress->start(length); + while (!is.eof()) { const stream::size_type read = is.read(buffer, sizeof(buffer)); @@ -57,9 +71,15 @@ const stream::size_type bufferedStreamCopy(inputStream& is, outputStream& os) { os.write(buffer, read); total += read; + + if (progress != NULL) + progress->progress(total, std::max(total, length)); } } + if (progress != NULL) + progress->stop(total); + return (total); } @@ -299,5 +319,28 @@ const stream::size_type inputStreamPointerAdapter::skip(const size_type count) } +// outputStreamSocketAdapter + +#ifdef VMIME_HAVE_MESSAGING_FEATURES + + +outputStreamSocketAdapter::outputStreamSocketAdapter(messaging::socket& sok) + : m_socket(sok) +{ +} + + +void outputStreamSocketAdapter::write + (const value_type* const data, const size_type count) +{ + m_socket.sendRaw(data, count); +} + + +#endif // VMIME_HAVE_MESSAGING_FEATURES + + + + } // utility } // vmime |