diff options
Diffstat (limited to 'src/utility')
-rw-r--r-- | src/utility/encoder/defaultEncoder.cpp | 2 | ||||
-rw-r--r-- | src/utility/inputStream.cpp | 33 | ||||
-rw-r--r-- | src/utility/inputStreamAdapter.cpp | 70 | ||||
-rw-r--r-- | src/utility/inputStreamByteBufferAdapter.cpp | 90 | ||||
-rw-r--r-- | src/utility/inputStreamPointerAdapter.cpp | 46 | ||||
-rw-r--r-- | src/utility/inputStreamSocketAdapter.cpp | 82 | ||||
-rw-r--r-- | src/utility/inputStreamStringAdapter.cpp | 94 | ||||
-rw-r--r-- | src/utility/inputStreamStringProxyAdapter.cpp | 89 | ||||
-rw-r--r-- | src/utility/outputStream.cpp | 33 | ||||
-rw-r--r-- | src/utility/outputStreamAdapter.cpp | 54 | ||||
-rw-r--r-- | src/utility/outputStreamByteArrayAdapter.cpp | 51 | ||||
-rw-r--r-- | src/utility/outputStreamSocketAdapter.cpp | 68 | ||||
-rw-r--r-- | src/utility/outputStreamStringAdapter.cpp | 51 | ||||
-rw-r--r-- | src/utility/stream.cpp | 485 | ||||
-rw-r--r-- | src/utility/streamUtils.cpp | 92 | ||||
-rw-r--r-- | src/utility/stringProxy.cpp | 2 |
16 files changed, 857 insertions, 485 deletions
diff --git a/src/utility/encoder/defaultEncoder.cpp b/src/utility/encoder/defaultEncoder.cpp index 4d0ffb5d..e2d226e6 100644 --- a/src/utility/encoder/defaultEncoder.cpp +++ b/src/utility/encoder/defaultEncoder.cpp @@ -23,6 +23,8 @@ #include "vmime/utility/encoder/defaultEncoder.hpp" +#include "vmime/utility/streamUtils.hpp" + namespace vmime { namespace utility { diff --git a/src/utility/inputStream.cpp b/src/utility/inputStream.cpp new file mode 100644 index 00000000..dd0adf4d --- /dev/null +++ b/src/utility/inputStream.cpp @@ -0,0 +1,33 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/inputStream.hpp" + + +namespace vmime { +namespace utility { + + +} // utility +} // vmime + diff --git a/src/utility/inputStreamAdapter.cpp b/src/utility/inputStreamAdapter.cpp new file mode 100644 index 00000000..b44b0841 --- /dev/null +++ b/src/utility/inputStreamAdapter.cpp @@ -0,0 +1,70 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/inputStreamAdapter.hpp" + + +namespace vmime { +namespace utility { + + +inputStreamAdapter::inputStreamAdapter(std::istream& is) + : m_stream(is) +{ +} + + +bool inputStreamAdapter::eof() const +{ + return (m_stream.eof()); +} + + +void inputStreamAdapter::reset() +{ + m_stream.exceptions(std::ios_base::badbit); + m_stream.seekg(0, std::ios::beg); + m_stream.clear(); +} + + +stream::size_type inputStreamAdapter::read + (value_type* const data, const size_type count) +{ + m_stream.exceptions(std::ios_base::badbit); + m_stream.read(data, count); + return (m_stream.gcount()); +} + + +stream::size_type inputStreamAdapter::skip(const size_type count) +{ + m_stream.exceptions(std::ios_base::badbit); + m_stream.ignore(count); + return (m_stream.gcount()); +} + + +} // utility +} // vmime + diff --git a/src/utility/inputStreamByteBufferAdapter.cpp b/src/utility/inputStreamByteBufferAdapter.cpp new file mode 100644 index 00000000..92e779fe --- /dev/null +++ b/src/utility/inputStreamByteBufferAdapter.cpp @@ -0,0 +1,90 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/inputStreamByteBufferAdapter.hpp" + + +namespace vmime { +namespace utility { + + +inputStreamByteBufferAdapter::inputStreamByteBufferAdapter(const byte_t* buffer, const size_type length) + : m_buffer(buffer), m_length(length), m_pos(0) +{ +} + + +bool inputStreamByteBufferAdapter::eof() const +{ + return m_pos >= m_length; +} + + +void inputStreamByteBufferAdapter::reset() +{ + m_pos = 0; +} + + +stream::size_type inputStreamByteBufferAdapter::read + (value_type* const data, const size_type count) +{ + const size_type remaining = m_length - m_pos; + + if (remaining < count) + { + std::copy(m_buffer + m_pos, m_buffer + m_pos + remaining, data); + m_pos += remaining; + + return remaining; + } + else + { + std::copy(m_buffer + m_pos, m_buffer + m_pos + count, data); + m_pos += count; + + return count; + } +} + + +stream::size_type inputStreamByteBufferAdapter::skip(const size_type count) +{ + const size_type remaining = m_length - m_pos; + + if (remaining < count) + { + m_pos += remaining; + return remaining; + } + else + { + m_pos += count; + return count; + } +} + + +} // utility +} // vmime + diff --git a/src/utility/inputStreamPointerAdapter.cpp b/src/utility/inputStreamPointerAdapter.cpp new file mode 100644 index 00000000..4d03e306 --- /dev/null +++ b/src/utility/inputStreamPointerAdapter.cpp @@ -0,0 +1,46 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/inputStreamPointerAdapter.hpp" + + +namespace vmime { +namespace utility { + + +inputStreamPointerAdapter::inputStreamPointerAdapter(std::istream* is, const bool own) + : inputStreamAdapter(*is), m_stream(is), m_own(own) +{ +} + + +inputStreamPointerAdapter::~inputStreamPointerAdapter() +{ + if (m_own) + delete (m_stream); +} + + +} // utility +} // vmime + diff --git a/src/utility/inputStreamSocketAdapter.cpp b/src/utility/inputStreamSocketAdapter.cpp new file mode 100644 index 00000000..b93cc3cd --- /dev/null +++ b/src/utility/inputStreamSocketAdapter.cpp @@ -0,0 +1,82 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/inputStreamSocketAdapter.hpp" + + +#if VMIME_HAVE_MESSAGING_FEATURES + + +#include "vmime/net/socket.hpp" + + +namespace vmime { +namespace utility { + + +inputStreamSocketAdapter::inputStreamSocketAdapter(net::socket& sok) + : m_socket(sok) +{ +} + + +bool inputStreamSocketAdapter::eof() const +{ + // Can't know... + return false; +} + + +void inputStreamSocketAdapter::reset() +{ + // Not supported +} + + +stream::size_type inputStreamSocketAdapter::read + (value_type* const data, const size_type count) +{ + return m_socket.receiveRaw(data, count); +} + + +stream::size_type inputStreamSocketAdapter::skip + (const size_type /* count */) +{ + // Not supported + return 0; +} + + +stream::size_type inputStreamSocketAdapter::getBlockSize() +{ + return m_socket.getBlockSize(); +} + + +} // utility +} // vmime + + +#endif // VMIME_HAVE_MESSAGING_FEATURES + diff --git a/src/utility/inputStreamStringAdapter.cpp b/src/utility/inputStreamStringAdapter.cpp new file mode 100644 index 00000000..31c9fdaf --- /dev/null +++ b/src/utility/inputStreamStringAdapter.cpp @@ -0,0 +1,94 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/inputStreamStringAdapter.hpp" + + +namespace vmime { +namespace utility { + + +inputStreamStringAdapter::inputStreamStringAdapter(const string& buffer) + : m_buffer(buffer), m_begin(0), m_end(buffer.length()), m_pos(0) +{ +} + + +inputStreamStringAdapter::inputStreamStringAdapter(const string& buffer, + const string::size_type begin, const string::size_type end) + : m_buffer(buffer), m_begin(begin), m_end(end), m_pos(begin) +{ +} + + +bool inputStreamStringAdapter::eof() const +{ + return (m_pos >= m_end); +} + + +void inputStreamStringAdapter::reset() +{ + m_pos = m_begin; +} + + +stream::size_type inputStreamStringAdapter::read + (value_type* const data, const size_type count) +{ + if (m_pos + count >= m_end) + { + const size_type remaining = m_end - m_pos; + + std::copy(m_buffer.begin() + m_pos, m_buffer.end(), data); + m_pos = m_end; + return (remaining); + } + else + { + std::copy(m_buffer.begin() + m_pos, m_buffer.begin() + m_pos + count, data); + m_pos += count; + return (count); + } +} + + +stream::size_type inputStreamStringAdapter::skip(const size_type count) +{ + if (m_pos + count >= m_end) + { + const size_type remaining = m_end - m_pos; + m_pos = m_end; + return (remaining); + } + else + { + m_pos += count; + return (count); + } +} + + +} // utility +} // vmime + diff --git a/src/utility/inputStreamStringProxyAdapter.cpp b/src/utility/inputStreamStringProxyAdapter.cpp new file mode 100644 index 00000000..5e4b60b9 --- /dev/null +++ b/src/utility/inputStreamStringProxyAdapter.cpp @@ -0,0 +1,89 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/inputStreamStringProxyAdapter.hpp" +#include "vmime/utility/stringProxy.hpp" + + +namespace vmime { +namespace utility { + + +inputStreamStringProxyAdapter::inputStreamStringProxyAdapter(const stringProxy& buffer) + : m_buffer(buffer), m_pos(0) +{ +} + + +bool inputStreamStringProxyAdapter::eof() const +{ + return (m_pos >= m_buffer.length()); +} + + +void inputStreamStringProxyAdapter::reset() +{ + m_pos = 0; +} + + +stream::size_type inputStreamStringProxyAdapter::read + (value_type* const data, const size_type count) +{ + const size_type remaining = m_buffer.length() - m_pos; + + if (count > remaining) + { + std::copy(m_buffer.it_begin() + m_pos, m_buffer.it_end(), data); + m_pos = m_buffer.length(); + return (remaining); + } + else + { + std::copy(m_buffer.it_begin() + m_pos, m_buffer.it_begin() + m_pos + count, data); + m_pos += count; + return (count); + } +} + + +stream::size_type inputStreamStringProxyAdapter::skip(const size_type count) +{ + const size_type remaining = m_buffer.length() - m_pos; + + if (count > remaining) + { + m_pos = m_buffer.length(); + return (remaining); + } + else + { + m_pos += count; + return (count); + } +} + + +} // utility +} // vmime + diff --git a/src/utility/outputStream.cpp b/src/utility/outputStream.cpp new file mode 100644 index 00000000..8a65db53 --- /dev/null +++ b/src/utility/outputStream.cpp @@ -0,0 +1,33 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/outputStream.hpp" + + +namespace vmime { +namespace utility { + + +} // utility +} // vmime + diff --git a/src/utility/outputStreamAdapter.cpp b/src/utility/outputStreamAdapter.cpp new file mode 100644 index 00000000..2da94f16 --- /dev/null +++ b/src/utility/outputStreamAdapter.cpp @@ -0,0 +1,54 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/outputStreamAdapter.hpp" + + +namespace vmime { +namespace utility { + + +outputStreamAdapter::outputStreamAdapter(std::ostream& os) + : m_stream(os) +{ +} + + +void outputStreamAdapter::write + (const value_type* const data, const size_type count) +{ + m_stream.exceptions(std::ios_base::badbit); + m_stream.write(data, count); +} + + +void outputStreamAdapter::flush() +{ + m_stream.exceptions(std::ios_base::badbit); + m_stream.flush(); +} + + +} // utility +} // vmime + diff --git a/src/utility/outputStreamByteArrayAdapter.cpp b/src/utility/outputStreamByteArrayAdapter.cpp new file mode 100644 index 00000000..97b27d2b --- /dev/null +++ b/src/utility/outputStreamByteArrayAdapter.cpp @@ -0,0 +1,51 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/outputStreamByteArrayAdapter.hpp" + + +namespace vmime { +namespace utility { + + +outputStreamByteArrayAdapter::outputStreamByteArrayAdapter(byteArray& array) + : m_array(array) +{ +} + + +void outputStreamByteArrayAdapter::write(const value_type* const data, const size_type count) +{ + m_array.insert(m_array.end(), data, data + count); +} + + +void outputStreamByteArrayAdapter::flush() +{ + // Do nothing +} + + +} // utility +} // vmime + diff --git a/src/utility/outputStreamSocketAdapter.cpp b/src/utility/outputStreamSocketAdapter.cpp new file mode 100644 index 00000000..d933e73d --- /dev/null +++ b/src/utility/outputStreamSocketAdapter.cpp @@ -0,0 +1,68 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/outputStreamSocketAdapter.hpp" + + +#if VMIME_HAVE_MESSAGING_FEATURES + + +#include "vmime/net/socket.hpp" + + +namespace vmime { +namespace utility { + + +outputStreamSocketAdapter::outputStreamSocketAdapter(net::socket& sok) + : m_socket(sok) +{ +} + + +void outputStreamSocketAdapter::write + (const value_type* const data, const size_type count) +{ + m_socket.sendRaw(data, count); +} + + +void outputStreamSocketAdapter::flush() +{ + // Do nothing +} + + +stream::size_type outputStreamSocketAdapter::getBlockSize() +{ + return m_socket.getBlockSize(); +} + + + +} // utility +} // vmime + + +#endif // VMIME_HAVE_MESSAGING_FEATURES + diff --git a/src/utility/outputStreamStringAdapter.cpp b/src/utility/outputStreamStringAdapter.cpp new file mode 100644 index 00000000..62b2a72c --- /dev/null +++ b/src/utility/outputStreamStringAdapter.cpp @@ -0,0 +1,51 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/outputStreamStringAdapter.hpp" + + +namespace vmime { +namespace utility { + + +outputStreamStringAdapter::outputStreamStringAdapter(string& buffer) + : m_buffer(buffer) +{ +} + + +void outputStreamStringAdapter::write(const value_type* const data, const size_type count) +{ + m_buffer.append(data, count); +} + + +void outputStreamStringAdapter::flush() +{ + // Do nothing +} + + +} // utility +} // vmime + diff --git a/src/utility/stream.cpp b/src/utility/stream.cpp index ec30b7d3..1c940c2a 100644 --- a/src/utility/stream.cpp +++ b/src/utility/stream.cpp @@ -22,503 +22,18 @@ // #include "vmime/utility/stream.hpp" -#include "vmime/utility/stringProxy.hpp" -#include <algorithm> // for std::copy -#include <iterator> // for std::back_inserter - -#if VMIME_HAVE_MESSAGING_FEATURES - #include "vmime/net/socket.hpp" -#endif namespace vmime { namespace utility { -// stream - stream::size_type stream::getBlockSize() { return 32768; // 32 KB } -// Helpers - -outputStream& operator<<(outputStream& os, const stream::value_type c) -{ - os.write(&c, 1); - return (os); -} - - -outputStream& operator<<(outputStream& os, const string& str) -{ - os.write(str.data(), str.length()); - return (os); -} - - -stream::size_type bufferedStreamCopy(inputStream& is, outputStream& os) -{ - return bufferedStreamCopy(is, os, 0, NULL); -} - - -stream::size_type bufferedStreamCopy(inputStream& is, outputStream& os, - const stream::size_type length, progressListener* progress) -{ - const stream::size_type blockSize = - std::min(is.getBlockSize(), os.getBlockSize()); - - std::vector <stream::value_type> vbuffer(blockSize); - - stream::value_type* buffer = &vbuffer.front(); - stream::size_type total = 0; - - if (progress != NULL) - progress->start(length); - - while (!is.eof()) - { - const stream::size_type read = is.read(buffer, blockSize); - - if (read != 0) - { - os.write(buffer, read); - total += read; - - if (progress != NULL) - progress->progress(total, std::max(total, length)); - } - } - - if (progress != NULL) - progress->stop(total); - - return (total); -} - - - -// outputStreamAdapter - -outputStreamAdapter::outputStreamAdapter(std::ostream& os) - : m_stream(os) -{ -} - - -void outputStreamAdapter::write - (const value_type* const data, const size_type count) -{ - m_stream.exceptions(std::ios_base::badbit); - m_stream.write(data, count); -} - - -void outputStreamAdapter::flush() -{ - m_stream.exceptions(std::ios_base::badbit); - m_stream.flush(); -} - - - -// outputStreamStringAdapter - -outputStreamStringAdapter::outputStreamStringAdapter(string& buffer) - : m_buffer(buffer) -{ -} - - -void outputStreamStringAdapter::write(const value_type* const data, const size_type count) -{ - m_buffer.append(data, count); -} - - -void outputStreamStringAdapter::flush() -{ - // Do nothing -} - - - -// outputStreamByteArrayAdapter - -outputStreamByteArrayAdapter::outputStreamByteArrayAdapter(byteArray& array) - : m_array(array) -{ -} - - -void outputStreamByteArrayAdapter::write(const value_type* const data, const size_type count) -{ - m_array.insert(m_array.end(), data, data + count); -} - - -void outputStreamByteArrayAdapter::flush() -{ - // Do nothing -} - - - -// inputStreamAdapter - -inputStreamAdapter::inputStreamAdapter(std::istream& is) - : m_stream(is) -{ -} - - -bool inputStreamAdapter::eof() const -{ - return (m_stream.eof()); -} - - -void inputStreamAdapter::reset() -{ - m_stream.exceptions(std::ios_base::badbit); - m_stream.seekg(0, std::ios::beg); - m_stream.clear(); -} - - -stream::size_type inputStreamAdapter::read - (value_type* const data, const size_type count) -{ - m_stream.exceptions(std::ios_base::badbit); - m_stream.read(data, count); - return (m_stream.gcount()); -} - - -stream::size_type inputStreamAdapter::skip(const size_type count) -{ - m_stream.exceptions(std::ios_base::badbit); - m_stream.ignore(count); - return (m_stream.gcount()); -} - - - -// inputStreamStringAdapter - -inputStreamStringAdapter::inputStreamStringAdapter(const string& buffer) - : m_buffer(buffer), m_begin(0), m_end(buffer.length()), m_pos(0) -{ -} - - -inputStreamStringAdapter::inputStreamStringAdapter(const string& buffer, - const string::size_type begin, const string::size_type end) - : m_buffer(buffer), m_begin(begin), m_end(end), m_pos(begin) -{ -} - - -bool inputStreamStringAdapter::eof() const -{ - return (m_pos >= m_end); -} - - -void inputStreamStringAdapter::reset() -{ - m_pos = m_begin; -} - - -stream::size_type inputStreamStringAdapter::read - (value_type* const data, const size_type count) -{ - if (m_pos + count >= m_end) - { - const size_type remaining = m_end - m_pos; - - std::copy(m_buffer.begin() + m_pos, m_buffer.end(), data); - m_pos = m_end; - return (remaining); - } - else - { - std::copy(m_buffer.begin() + m_pos, m_buffer.begin() + m_pos + count, data); - m_pos += count; - return (count); - } -} - - -stream::size_type inputStreamStringAdapter::skip(const size_type count) -{ - if (m_pos + count >= m_end) - { - const size_type remaining = m_end - m_pos; - m_pos = m_end; - return (remaining); - } - else - { - m_pos += count; - return (count); - } -} - - - -// inputStreamStringProxyAdapter - -inputStreamStringProxyAdapter::inputStreamStringProxyAdapter(const stringProxy& buffer) - : m_buffer(buffer), m_pos(0) -{ -} - - -bool inputStreamStringProxyAdapter::eof() const -{ - return (m_pos >= m_buffer.length()); -} - - -void inputStreamStringProxyAdapter::reset() -{ - m_pos = 0; -} - - -stream::size_type inputStreamStringProxyAdapter::read - (value_type* const data, const size_type count) -{ - const size_type remaining = m_buffer.length() - m_pos; - - if (count > remaining) - { - std::copy(m_buffer.it_begin() + m_pos, m_buffer.it_end(), data); - m_pos = m_buffer.length(); - return (remaining); - } - else - { - std::copy(m_buffer.it_begin() + m_pos, m_buffer.it_begin() + m_pos + count, data); - m_pos += count; - return (count); - } -} - - -stream::size_type inputStreamStringProxyAdapter::skip(const size_type count) -{ - const size_type remaining = m_buffer.length() - m_pos; - - if (count > remaining) - { - m_pos = m_buffer.length(); - return (remaining); - } - else - { - m_pos += count; - return (count); - } -} - - - -// inputStreamPointerAdapter - -inputStreamPointerAdapter::inputStreamPointerAdapter(std::istream* is, const bool own) - : m_stream(is), m_own(own) -{ -} - - -inputStreamPointerAdapter::inputStreamPointerAdapter(const inputStreamPointerAdapter&) - : inputStream(), m_stream(NULL), m_own(false) -{ - // Not copiable -} - - -inputStreamPointerAdapter::~inputStreamPointerAdapter() -{ - if (m_own) - delete (m_stream); -} - - -bool inputStreamPointerAdapter::eof() const -{ - return (m_stream->eof()); -} - - -void inputStreamPointerAdapter::reset() -{ - m_stream->exceptions(std::ios_base::badbit); - m_stream->seekg(0, std::ios::beg); - m_stream->clear(); -} - - -stream::size_type inputStreamPointerAdapter::read - (value_type* const data, const size_type count) -{ - m_stream->exceptions(std::ios_base::badbit); - m_stream->read(data, count); - return (m_stream->gcount()); -} - - -stream::size_type inputStreamPointerAdapter::skip(const size_type count) -{ - m_stream->exceptions(std::ios_base::badbit); - m_stream->ignore(count); - return (m_stream->gcount()); -} - - - -// inputStreamByteBufferAdapter - -inputStreamByteBufferAdapter::inputStreamByteBufferAdapter(const byte_t* buffer, const size_type length) - : m_buffer(buffer), m_length(length), m_pos(0) -{ -} - - -bool inputStreamByteBufferAdapter::eof() const -{ - return m_pos >= m_length; -} - - -void inputStreamByteBufferAdapter::reset() -{ - m_pos = 0; -} - - -stream::size_type inputStreamByteBufferAdapter::read - (value_type* const data, const size_type count) -{ - const size_type remaining = m_length - m_pos; - - if (remaining < count) - { - std::copy(m_buffer + m_pos, m_buffer + m_pos + remaining, data); - m_pos += remaining; - - return remaining; - } - else - { - std::copy(m_buffer + m_pos, m_buffer + m_pos + count, data); - m_pos += count; - - return count; - } -} - - -stream::size_type inputStreamByteBufferAdapter::skip(const size_type count) -{ - const size_type remaining = m_length - m_pos; - - if (remaining < count) - { - m_pos += remaining; - return remaining; - } - else - { - m_pos += count; - return count; - } -} - - - -#ifdef VMIME_HAVE_MESSAGING_FEATURES - - -// outputStreamSocketAdapter - -outputStreamSocketAdapter::outputStreamSocketAdapter(net::socket& sok) - : m_socket(sok) -{ -} - - -void outputStreamSocketAdapter::write - (const value_type* const data, const size_type count) -{ - m_socket.sendRaw(data, count); -} - - -void outputStreamSocketAdapter::flush() -{ - // Do nothing -} - - -stream::size_type outputStreamSocketAdapter::getBlockSize() -{ - return m_socket.getBlockSize(); -} - - - -// inputStreamSocketAdapter - -inputStreamSocketAdapter::inputStreamSocketAdapter(net::socket& sok) - : m_socket(sok) -{ -} - - -bool inputStreamSocketAdapter::eof() const -{ - // Can't know... - return false; -} - - -void inputStreamSocketAdapter::reset() -{ - // Not supported -} - - -stream::size_type inputStreamSocketAdapter::read - (value_type* const data, const size_type count) -{ - return m_socket.receiveRaw(data, count); -} - - -stream::size_type inputStreamSocketAdapter::skip - (const size_type /* count */) -{ - // Not supported - return 0; -} - - -stream::size_type inputStreamSocketAdapter::getBlockSize() -{ - return m_socket.getBlockSize(); -} - - -#endif // VMIME_HAVE_MESSAGING_FEATURES - - } // utility } // vmime diff --git a/src/utility/streamUtils.cpp b/src/utility/streamUtils.cpp new file mode 100644 index 00000000..f1d3b9de --- /dev/null +++ b/src/utility/streamUtils.cpp @@ -0,0 +1,92 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002-2012 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 3 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., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "vmime/utility/streamUtils.hpp" + +#include <algorithm> // for std::copy +#include <iterator> // for std::back_inserter + + + +namespace vmime { +namespace utility { + + +outputStream& operator<<(outputStream& os, const stream::value_type c) +{ + os.write(&c, 1); + return (os); +} + + +outputStream& operator<<(outputStream& os, const string& str) +{ + os.write(str.data(), str.length()); + return (os); +} + + +stream::size_type bufferedStreamCopy(inputStream& is, outputStream& os) +{ + return bufferedStreamCopy(is, os, 0, NULL); +} + + +stream::size_type bufferedStreamCopy(inputStream& is, outputStream& os, + const stream::size_type length, progressListener* progress) +{ + const stream::size_type blockSize = + std::min(is.getBlockSize(), os.getBlockSize()); + + std::vector <stream::value_type> vbuffer(blockSize); + + stream::value_type* buffer = &vbuffer.front(); + stream::size_type total = 0; + + if (progress != NULL) + progress->start(length); + + while (!is.eof()) + { + const stream::size_type read = is.read(buffer, blockSize); + + if (read != 0) + { + os.write(buffer, read); + total += read; + + if (progress != NULL) + progress->progress(total, std::max(total, length)); + } + } + + if (progress != NULL) + progress->stop(total); + + return (total); +} + + +} // utility +} // vmime + diff --git a/src/utility/stringProxy.cpp b/src/utility/stringProxy.cpp index a4ba6d22..74344b57 100644 --- a/src/utility/stringProxy.cpp +++ b/src/utility/stringProxy.cpp @@ -23,6 +23,8 @@ #include "vmime/utility/stringProxy.hpp" +#include "vmime/utility/outputStreamAdapter.hpp" + #include <iterator> #include <algorithm> |