From 97638ba32447a5184b3bd0a48fe1a678c334648e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 26 May 2019 18:39:20 +0100 Subject: Add MemStream A wrapper around open_memstream --- libadhocutil/memstream.cpp | 51 ++++++++++++++++++++++++++++++++ libadhocutil/memstream.h | 36 ++++++++++++++++++++++ libadhocutil/unittests/Jamfile.jam | 8 +++++ libadhocutil/unittests/testMemStream.cpp | 34 +++++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 libadhocutil/memstream.cpp create mode 100644 libadhocutil/memstream.h create mode 100644 libadhocutil/unittests/testMemStream.cpp diff --git a/libadhocutil/memstream.cpp b/libadhocutil/memstream.cpp new file mode 100644 index 0000000..35d42ad --- /dev/null +++ b/libadhocutil/memstream.cpp @@ -0,0 +1,51 @@ +#include "memstream.h" +#include + +namespace AdHoc { + MemStream::MemStream() : + buf(nullptr), + len(0), + strm(open_memstream(&buf, &len)) + { + } + + MemStream::~MemStream() + { + fclose(strm); + free(buf); + } + + MemStream::operator FILE * () + { + return strm; + } + + MemStream::operator const char * () const + { + fflush(strm); + return buf; + } + + MemStream::operator std::string_view() const + { + fflush(strm); + return { buf, len }; + } + + const char * MemStream::buffer() const + { + return *this; + } + + std::string_view MemStream::sv() const + { + return *this; + } + + size_t MemStream::length() const + { + fflush(strm); + return len; + } +} + diff --git a/libadhocutil/memstream.h b/libadhocutil/memstream.h new file mode 100644 index 0000000..b807d8b --- /dev/null +++ b/libadhocutil/memstream.h @@ -0,0 +1,36 @@ +#ifndef LIBADHOC_MEMSTREAM_H +#define LIBADHOC_MEMSTREAM_H + +#include +#include +#include + +namespace AdHoc { + class DLL_PUBLIC MemStream { + public: + MemStream(); + ~MemStream(); + + MemStream(const MemStream &) = delete; + MemStream(MemStream &&) = delete; + + void operator=(const MemStream &) = delete; + void operator=(MemStream &&) = delete; + + operator FILE * (); + operator const char * () const; + operator std::string_view () const; + + const char * buffer() const; + std::string_view sv() const; + size_t length() const; + + private: + char * buf; + size_t len; + FILE * strm; + }; +} + +#endif + diff --git a/libadhocutil/unittests/Jamfile.jam b/libadhocutil/unittests/Jamfile.jam index 6b04540..0b749a8 100644 --- a/libadhocutil/unittests/Jamfile.jam +++ b/libadhocutil/unittests/Jamfile.jam @@ -287,3 +287,11 @@ run boost_utf ; +run + testMemStream.cpp + : : : + BOOST_TEST_DYN_LINK + ..//adhocutil + boost_utf + ; + diff --git a/libadhocutil/unittests/testMemStream.cpp b/libadhocutil/unittests/testMemStream.cpp new file mode 100644 index 0000000..b244b5e --- /dev/null +++ b/libadhocutil/unittests/testMemStream.cpp @@ -0,0 +1,34 @@ +#define BOOST_TEST_MODULE NvpParse +#include +#include + +#include "memstream.h" + +using namespace AdHoc; + +BOOST_FIXTURE_TEST_SUITE(s, MemStream); + +BOOST_AUTO_TEST_CASE(empty) +{ + BOOST_CHECK_EQUAL("", this->sv()); + BOOST_CHECK_EQUAL("", this->buffer()); + BOOST_CHECK_EQUAL(0, this->sv().length()); + BOOST_CHECK_EQUAL(0, this->length()); +} + +using cast_types = boost::mpl::list; +BOOST_AUTO_TEST_CASE_TEMPLATE(casts, T, cast_types ) +{ + auto dummy = [](const T &) { }; + dummy(*this); +} + +BOOST_AUTO_TEST_CASE(simple) +{ + auto len = fprintf(*this, "Some %s write.", "simple"); + BOOST_CHECK_EQUAL("Some simple write.", this->buffer()); + BOOST_CHECK_EQUAL(len, this->length()); +} + +BOOST_AUTO_TEST_SUITE_END(); + -- cgit v1.2.3