Removed 'stringProxy' since COW std::string is no longer valid in C++11.

This commit is contained in:
Vincent Richard 2018-09-15 07:41:26 +02:00
parent 0c81108cc8
commit df135b5a8b
11 changed files with 26 additions and 738 deletions

View File

@ -28,7 +28,6 @@
#include <limits>
#include "vmime/base.hpp"
#include "vmime/utility/stringProxy.hpp"
#include "vmime/utility/progressListener.hpp"
#include "vmime/encoding.hpp"
#include "vmime/mediaType.hpp"

View File

@ -34,7 +34,7 @@
#include "vmime/net/imap/IMAPStore.hpp"
#include "vmime/utility/outputStreamAdapter.hpp"
#include "vmime/utility/inputStreamStringProxyAdapter.hpp"
#include "vmime/utility/inputStreamStringAdapter.hpp"
namespace vmime {
@ -87,7 +87,7 @@ void IMAPMessagePartContentHandler::generate(
msg->extractPart(part, tmp, NULL);
// Decode to another temporary buffer
utility::inputStreamStringProxyAdapter in(oss.str());
utility::inputStreamStringAdapter in(oss.str());
std::ostringstream oss2;
utility::outputStreamAdapter tmp2(oss2);

View File

@ -24,8 +24,8 @@
#include "vmime/stringContentHandler.hpp"
#include "vmime/utility/inputStreamStringAdapter.hpp"
#include "vmime/utility/inputStreamStringProxyAdapter.hpp"
#include "vmime/utility/outputStreamAdapter.hpp"
#include "vmime/utility/streamUtils.hpp"
namespace vmime {
@ -57,28 +57,6 @@ stringContentHandler::stringContentHandler(
}
stringContentHandler::stringContentHandler(
const utility::stringProxy& str,
const vmime::encoding& enc
)
: m_encoding(enc),
m_string(str) {
}
stringContentHandler::stringContentHandler(
const string& buffer,
const size_t start,
const size_t end,
const vmime::encoding& enc
)
: m_encoding(enc),
m_string(buffer, start, end) {
}
stringContentHandler::~stringContentHandler() {
}
@ -100,29 +78,10 @@ stringContentHandler& stringContentHandler::operator=(const stringContentHandler
}
void stringContentHandler::setData(const utility::stringProxy& str, const vmime::encoding& enc) {
m_encoding = enc;
m_string = str;
}
void stringContentHandler::setData(const string& buffer, const vmime::encoding& enc) {
m_encoding = enc;
m_string.set(buffer);
}
void stringContentHandler::setData(
const string& buffer,
const size_t start,
const size_t end,
const vmime::encoding& enc
) {
m_encoding = enc;
m_string.set(buffer, start, end);
m_string = buffer;
}
@ -154,7 +113,7 @@ void stringContentHandler::generate(
theEncoder->getProperties()["maxlinelength"] = maxLineLength;
theEncoder->getProperties()["text"] = (m_contentType.getType() == mediaTypes::TEXT);
utility::inputStreamStringProxyAdapter in(m_string);
utility::inputStreamStringAdapter in(m_string);
std::ostringstream oss;
utility::outputStreamAdapter tempOut(oss);
@ -169,7 +128,7 @@ void stringContentHandler::generate(
// No encoding to perform
} else {
m_string.extract(os);
os.write(m_string.data(), m_string.length());
}
// Need to encode data before
@ -179,7 +138,7 @@ void stringContentHandler::generate(
theEncoder->getProperties()["maxlinelength"] = maxLineLength;
theEncoder->getProperties()["text"] = (m_contentType.getType() == mediaTypes::TEXT);
utility::inputStreamStringProxyAdapter in(m_string);
utility::inputStreamStringAdapter in(m_string);
theEncoder->encode(in, os);
}
@ -194,14 +153,17 @@ void stringContentHandler::extract(
// No decoding to perform
if (!isEncoded()) {
m_string.extract(os, 0, m_string.length(), progress);
utility::inputStreamStringAdapter in(m_string);
utility::progressListenerSizeAdapter plsa(progress, getLength());
utility::bufferedStreamCopy(in, os, m_string.length(), progress);
// Need to decode data
} else {
shared_ptr <utility::encoder::encoder> theDecoder = m_encoding.getEncoder();
utility::inputStreamStringProxyAdapter in(m_string);
utility::inputStreamStringAdapter in(m_string);
utility::progressListenerSizeAdapter plsa(progress, getLength());
theDecoder->decode(in, os, &plsa);
@ -214,7 +176,10 @@ void stringContentHandler::extractRaw(
utility::progressListener* progress
) const {
m_string.extract(os, 0, m_string.length(), progress);
utility::inputStreamStringAdapter in(m_string);
utility::progressListenerSizeAdapter plsa(progress, getLength());
utility::bufferedStreamCopy(in, os, m_string.length(), progress);
}

View File

@ -42,18 +42,6 @@ public:
const vmime::encoding& enc = NO_ENCODING
);
stringContentHandler(
const utility::stringProxy& str,
const vmime::encoding& enc = NO_ENCODING
);
stringContentHandler(
const string& buffer,
const size_t start,
const size_t end,
const vmime::encoding& enc = NO_ENCODING
);
~stringContentHandler();
stringContentHandler(const stringContentHandler& cts);
@ -61,32 +49,20 @@ public:
shared_ptr <contentHandler> clone() const;
// Set the data contained in the body.
//
// The two first functions take advantage of the COW (copy-on-write) system that
// might be implemented into std::string. This is done using "stringProxy" object.
//
// Set "enc" parameter to anything other than NO_ENCODING if the data managed by
// this content handler is already encoded with the specified encoding (so, no
// encoding/decoding will be performed on generate()/extract()). Note that the
// data may be re-encoded (that is, decoded and encoded) if the encoding passed
// to generate() is different from this one...
void setData(
const utility::stringProxy& str,
const vmime::encoding& enc = NO_ENCODING
);
/** Set data contained in this object.
*
* @param buffer buffer containing data
* @param enc set to anything other than NO_ENCODING if the data managed by
* this content handler is already encoded with the specified encoding (so, no
* encoding/decoding will be performed on generate()/extract()). Note that the
* data may be re-encoded (that is, decoded and encoded) if the encoding passed
* to generate() is different from this one.
*/
void setData(
const string& buffer,
const vmime::encoding& enc = NO_ENCODING
);
void setData(
const string& buffer,
const size_t start,
const size_t end,
const vmime::encoding& enc = NO_ENCODING
);
stringContentHandler& operator=(const string& buffer);
@ -121,7 +97,7 @@ private:
vmime::encoding m_encoding;
// The actual data
utility::stringProxy m_string;
string m_string;
};

View File

@ -1,104 +0,0 @@
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// 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;
}
size_t inputStreamStringProxyAdapter::read(byte_t* const data, const size_t count) {
const size_t 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;
}
}
size_t inputStreamStringProxyAdapter::skip(const size_t count) {
const size_t remaining = m_buffer.length() - m_pos;
if (count > remaining) {
m_pos = m_buffer.length();
return remaining;
} else {
m_pos += count;
return count;
}
}
size_t inputStreamStringProxyAdapter::getPosition() const {
return m_pos;
}
void inputStreamStringProxyAdapter::seek(const size_t pos) {
if (pos <= m_buffer.length()) {
m_pos = pos;
}
}
} // utility
} // vmime

View File

@ -1,68 +0,0 @@
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// 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.
//
#ifndef VMIME_UTILITY_INPUTSTREAMSTRINGPROXYADAPTER_HPP_INCLUDED
#define VMIME_UTILITY_INPUTSTREAMSTRINGPROXYADAPTER_HPP_INCLUDED
#include "vmime/utility/seekableInputStream.hpp"
namespace vmime {
namespace utility {
class stringProxy;
/** An adapter class for stringProxy input.
*/
class VMIME_EXPORT inputStreamStringProxyAdapter : public seekableInputStream {
public:
/** @param buffer stringProxy object to wrap
*/
inputStreamStringProxyAdapter(const stringProxy& buffer);
bool eof() const;
void reset();
size_t read(byte_t* const data, const size_t count);
size_t skip(const size_t count);
size_t getPosition() const;
void seek(const size_t pos);
private:
inputStreamStringProxyAdapter(const inputStreamStringProxyAdapter&);
const stringProxy& m_buffer;
size_t m_pos;
};
} // utility
} // vmime
#endif // VMIME_UTILITY_INPUTSTREAMSTRINGPROXYADAPTER_HPP_INCLUDED

View File

@ -1,163 +0,0 @@
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// 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/stringProxy.hpp"
#include "vmime/utility/outputStreamAdapter.hpp"
#include <iterator>
#include <algorithm>
namespace vmime {
namespace utility {
stringProxy::stringProxy()
: m_start(0),
m_end(0) {
}
stringProxy::stringProxy(const stringProxy& s)
: m_buffer(s.m_buffer),
m_start(s.m_start),
m_end(s.m_end) {
}
stringProxy::stringProxy(const string& s, const size_t start, const size_t end)
: m_buffer(s),
m_start(start),
m_end(end == std::numeric_limits <size_t>::max() ? s.length() : end) {
}
void stringProxy::set(const string& s, const size_t start, const size_t end) {
m_buffer = s;
m_start = start;
if (end == std::numeric_limits <size_t>::max()) {
m_end = s.length();
} else {
m_end = end;
}
}
void stringProxy::detach() {
m_buffer.clear();
m_start = m_end = 0;
}
stringProxy& stringProxy::operator=(const stringProxy& s) {
m_buffer = s.m_buffer;
m_start = s.m_start;
m_end = s.m_end;
return *this;
}
stringProxy& stringProxy::operator=(const string& s) {
m_buffer = s;
m_start = 0;
m_end = s.length();
return *this;
}
void stringProxy::extract(
outputStream& os,
const size_t start,
const size_t end,
utility::progressListener* progress
) const {
size_t len = 0;
if (end == std::numeric_limits <size_t>::max()) {
len = m_end - start - m_start;
} else if (end > start) {
len = end - start;
}
if (progress) {
progress->start(len);
}
os.write(m_buffer.data() + m_start + start, len);
if (progress) {
progress->progress(len, len);
progress->stop(len);
}
}
size_t stringProxy::length() const {
return m_end - m_start;
}
size_t stringProxy::start() const {
return m_start;
}
size_t stringProxy::end() const {
return m_end;
}
std::ostream& operator<<(std::ostream& os, const stringProxy& s) {
outputStreamAdapter adapter(os);
s.extract(adapter);
return os;
}
outputStream& operator<<(outputStream& os, const stringProxy& s) {
s.extract(os);
return os;
}
} // utility
} // vmime

View File

@ -1,96 +0,0 @@
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// 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.
//
#ifndef VMIME_UTILITY_STRINGPROXY_HPP_INCLUDED
#define VMIME_UTILITY_STRINGPROXY_HPP_INCLUDED
#include <limits>
#include "vmime/types.hpp"
#include "vmime/utility/stream.hpp"
#include "vmime/utility/outputStream.hpp"
#include "vmime/utility/progressListener.hpp"
namespace vmime {
namespace utility {
/** This class is a proxy for the string class. This takes
* advantage of the COW (copy-on-write) system that might
* be used in "std::string" implementation.
*/
class VMIME_EXPORT stringProxy {
public:
// Consruction
stringProxy();
stringProxy(const stringProxy& s);
stringProxy(const string& s, const size_t start = 0, const size_t end = std::numeric_limits <size_t>::max());
// Assignment
void set(const string& s, const size_t start = 0, const size_t end = std::numeric_limits <size_t>::max());
void detach();
stringProxy& operator=(const stringProxy& s);
stringProxy& operator=(const string& s);
// Extract some portion (or whole) of the string
// and output it into a stream.
void extract(
outputStream& os,
const size_t start = 0,
const size_t end = std::numeric_limits <size_t>::max(),
utility::progressListener* progress = NULL
) const;
// Return the "virtual" length of the string
size_t length() const;
// Return the boundaries of the "virtual" string
size_t start() const;
size_t end() const;
string::const_iterator it_begin() const { return (m_buffer.begin() + m_start); }
string::const_iterator it_end() const { return (m_buffer.begin() + m_end); }
private:
string m_buffer;
size_t m_start;
size_t m_end;
};
VMIME_EXPORT std::ostream& operator<<(std::ostream& os, const stringProxy& s);
VMIME_EXPORT outputStream& operator<<(outputStream& os, const stringProxy& s);
} // utility
} // vmime
#endif // VMIME_UTILITY_STRINGPROXY_HPP_INCLUDED

View File

@ -80,7 +80,6 @@
#include "utility/inputStreamPointerAdapter.hpp"
#include "utility/inputStreamSocketAdapter.hpp"
#include "utility/inputStreamStringAdapter.hpp"
#include "utility/inputStreamStringProxyAdapter.hpp"
#include "utility/outputStream.hpp"
#include "utility/outputStreamAdapter.hpp"
#include "utility/outputStreamByteArrayAdapter.hpp"

View File

@ -38,7 +38,6 @@ VMIME_TEST_SUITE_BEGIN(stringContentHandlerTest)
VMIME_TEST(testExtractRaw_Encoded)
VMIME_TEST(testGenerate)
VMIME_TEST(testGenerate_Encoded)
VMIME_TEST(testStringProxy)
VMIME_TEST_LIST_END
@ -163,34 +162,4 @@ VMIME_TEST_SUITE_BEGIN(stringContentHandlerTest)
VASSERT_EQ("generate", "Zm9vEjRWYmFy", oss.str());
}
void testStringProxy() {
// With 'stringProxy' object
vmime::utility::stringProxy str("This is the data buffer", 12, 12 + 4);
vmime::stringContentHandler cth(str);
VASSERT_FALSE("empty", cth.isEmpty());
VASSERT_EQ("length", 4, cth.getLength());
std::ostringstream oss;
vmime::utility::outputStreamAdapter osa(oss);
cth.extract(osa);
VASSERT_EQ("extract", "data", oss.str());
// Directly with constructor
vmime::stringContentHandler cth2("This is the data buffer", 12, 12 + 4);
VASSERT_FALSE("empty", cth2.isEmpty());
VASSERT_EQ("length", 4, cth2.getLength());
std::ostringstream oss2;
vmime::utility::outputStreamAdapter osa2(oss2);
cth2.extract(osa2);
VASSERT_EQ("extract", "data", oss2.str());
}
VMIME_TEST_SUITE_END

View File

@ -1,189 +0,0 @@
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// 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 "tests/testUtils.hpp"
VMIME_TEST_SUITE_BEGIN(stringProxyTest)
VMIME_TEST_LIST_BEGIN
VMIME_TEST(testConstruct)
VMIME_TEST(testConstruct2)
VMIME_TEST(testDetach)
VMIME_TEST(testSet)
VMIME_TEST(testExtract)
VMIME_TEST(testOperatorLTLT1)
VMIME_TEST(testOperatorLTLT2)
VMIME_TEST_LIST_END
void testConstruct() {
vmime::utility::stringProxy s;
VASSERT_EQ("1", static_cast <vmime::size_t>(0), s.length());
VASSERT_EQ("2", static_cast <vmime::size_t>(0), s.start());
VASSERT_EQ("3", static_cast <vmime::size_t>(0), s.end());
}
void testConstruct2() {
vmime::string str("This is a test string.");
vmime::utility::stringProxy s1(str);
VASSERT_EQ("1", str.length(), s1.length());
VASSERT_EQ("2", static_cast <vmime::size_t>(0), s1.start());
VASSERT_EQ("3", str.length(), s1.end());
vmime::utility::stringProxy s2(str, 10);
VASSERT_EQ("4", str.length() - 10, s2.length());
VASSERT_EQ("5", static_cast <vmime::size_t>(10), s2.start());
VASSERT_EQ("6", str.length(), s2.end());
vmime::utility::stringProxy s3(str, 10, 14);
VASSERT_EQ("7", static_cast <vmime::size_t>(4), s3.length());
VASSERT_EQ("8", static_cast <vmime::size_t>(10), s3.start());
VASSERT_EQ("9", static_cast <vmime::size_t>(14), s3.end());
VASSERT_EQ("10", 't', *s3.it_begin());
VASSERT_EQ("11", 'e', *(s3.it_begin() + 1));
VASSERT_EQ("12", 's', *(s3.it_begin() + 2));
VASSERT_EQ("13", 't', *(s3.it_begin() + 3));
}
void testDetach() {
vmime::utility::stringProxy s;
s = "foo";
s.detach();
VASSERT_EQ("1", static_cast <vmime::size_t>(0), s.length());
VASSERT_EQ("2", static_cast <vmime::size_t>(0), s.start());
VASSERT_EQ("3", static_cast <vmime::size_t>(0), s.end());
}
void testSet() {
vmime::string str("This is a test string.");
vmime::utility::stringProxy s1;
s1.set(str);
VASSERT_EQ("1", str.length(), s1.length());
VASSERT_EQ("2", static_cast <vmime::size_t>(0), s1.start());
VASSERT_EQ("3", str.length(), s1.end());
vmime::utility::stringProxy s2;
s2.set(str, 10);
VASSERT_EQ("4", str.length() - 10, s2.length());
VASSERT_EQ("5", static_cast <vmime::size_t>(10), s2.start());
VASSERT_EQ("6", str.length(), s2.end());
vmime::utility::stringProxy s3;
s3.set(str, 10, 14);
VASSERT_EQ("7", static_cast <vmime::size_t>(4), s3.length());
VASSERT_EQ("8", static_cast <vmime::size_t>(10), s3.start());
VASSERT_EQ("9", static_cast <vmime::size_t>(14), s3.end());
VASSERT_EQ("10", 't', *s3.it_begin());
VASSERT_EQ("11", 'e', *(s3.it_begin() + 1));
VASSERT_EQ("12", 's', *(s3.it_begin() + 2));
VASSERT_EQ("13", 't', *(s3.it_begin() + 3));
}
void testExtract() {
vmime::string str("This is a test string.");
vmime::utility::stringProxy s1(str, 10, 14);
std::ostringstream oss1;
vmime::utility::outputStreamAdapter osa1(oss1);
s1.extract(osa1);
VASSERT_EQ("1", "test", oss1.str());
vmime::utility::stringProxy s2(str);
std::ostringstream oss2;
vmime::utility::outputStreamAdapter osa2(oss2);
s2.extract(osa2);
VASSERT_EQ("2", str, oss2.str());
}
void testOperatorLTLT1() {
vmime::string str("This is a test string.");
vmime::utility::stringProxy s1(str, 10, 14);
std::ostringstream oss1;
oss1 << s1;
VASSERT_EQ("1", "test", oss1.str());
vmime::utility::stringProxy s2(str);
std::ostringstream oss2;
oss2 << s2;
VASSERT_EQ("2", str, oss2.str());
}
void testOperatorLTLT2() {
vmime::string str("This is a test string.");
vmime::utility::stringProxy s1(str, 10, 14);
std::ostringstream oss1;
vmime::utility::outputStreamAdapter osa1(oss1);
osa1 << s1;
VASSERT_EQ("1", "test", oss1.str());
vmime::utility::stringProxy s2(str);
std::ostringstream oss2;
vmime::utility::outputStreamAdapter osa2(oss2);
osa2 << s2;
VASSERT_EQ("2", str, oss2.str());
}
VMIME_TEST_SUITE_END