From b24e034bca84b2935dd5079d832ba88d5d104011 Mon Sep 17 00:00:00 2001 From: randomdan Date: Tue, 3 Apr 2012 19:00:52 +0000 Subject: View to stream... and stream to file --- project2/files/writeStream.cpp | 35 +++++++++++++++++++++++++++ project2/streams/viewStream.cpp | 52 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 project2/files/writeStream.cpp create mode 100644 project2/streams/viewStream.cpp diff --git a/project2/files/writeStream.cpp b/project2/files/writeStream.cpp new file mode 100644 index 0000000..921804f --- /dev/null +++ b/project2/files/writeStream.cpp @@ -0,0 +1,35 @@ +#include "scriptLoader.h" +#include "task.h" +#include "stream.h" +#include + +SimpleMessageException(OpenTargetFile); + +class WriteStream : public Task { + public: + WriteStream(const ScriptNodePtr & s) : + SourceObject(s), + Task(s), + path(s, "path") + { + s->script->loader.addLoadTarget(s, Storer::into(&stream)); + } + + void execute() const + { + std::fstream ddd(path().as(), std::fstream::trunc | std::fstream::out); + if (!ddd.good()) { + throw OpenTargetFile(path()); + } + stream->runStream([&ddd](const char * data, size_t len) -> size_t { + ddd.write(data, len); + return len; + }); + } + + const Variable path; + StreamPtr stream; +}; + +DECLARE_LOADER("writestream", WriteStream); + diff --git a/project2/streams/viewStream.cpp b/project2/streams/viewStream.cpp new file mode 100644 index 0000000..77ecf45 --- /dev/null +++ b/project2/streams/viewStream.cpp @@ -0,0 +1,52 @@ +#include "scriptLoader.h" +#include "stream.h" +#include "scopeObject.h" +#include "ostreamWrapper.h" +#include "viewHost.h" +#include +#include + +class SinkStream : public boost::iostreams::sink { + public: + SinkStream(const Stream::Sink & s) : + sink(s) + { + } + std::streamsize write(const char * data, std::streamsize len) + { + return sink(data, len); + } + + const Stream::Sink & sink; +}; + +class ViewStream : public Stream, public ViewHost { + public: + ViewStream(ScriptNodePtr p) : + CheckHost(p), + Stream(p), + ViewHost(p) + { + p->script->loader.addLoadTarget(p, Storer::into(&presenter)); + } + void runStream(const Stream::Sink & s) const + { + TransformSourcePtr t = boost::dynamic_pointer_cast(getPresenter()); + if (t) { + boost::iostreams::stream strm(s); + ostreamWrapper * o = new ostreamWrapper(strm); + executeViews(); + t->addTarget(o); + ScopeObject remove([&t] { t->clearTargets(); }); + doTransforms(); + } + } + + private: + MultiRowSetPresenterPtr getPresenter() const { + return presenter; + } + MultiRowSetPresenterPtr presenter; +}; + +DECLARE_LOADER("viewstream", ViewStream); -- cgit v1.2.3