summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2012-04-03 19:00:52 +0000
committerrandomdan <randomdan@localhost>2012-04-03 19:00:52 +0000
commitfaf2241c925901459e640d4ce1f27d19788cf8e8 (patch)
tree846c9724fcd31c57eb69bcd082c6ff7d5d43238e
parentCreate app engine on the heap as SourceObjects are now self registering (smar... (diff)
downloadproject2-faf2241c925901459e640d4ce1f27d19788cf8e8.tar.bz2
project2-faf2241c925901459e640d4ce1f27d19788cf8e8.tar.xz
project2-faf2241c925901459e640d4ce1f27d19788cf8e8.zip
View to stream... and stream to file
-rw-r--r--project2/files/writeStream.cpp35
-rw-r--r--project2/streams/viewStream.cpp52
2 files changed, 87 insertions, 0 deletions
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 <fstream>
+
+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<ElementLoader>(&stream));
+ }
+
+ void execute() const
+ {
+ std::fstream ddd(path().as<std::string>(), 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 <boost/iostreams/stream.hpp>
+#include <boost/iostreams/concepts.hpp>
+
+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<PresenterLoader>(&presenter));
+ }
+ void runStream(const Stream::Sink & s) const
+ {
+ TransformSourcePtr t = boost::dynamic_pointer_cast<TransformSource>(getPresenter());
+ if (t) {
+ boost::iostreams::stream<SinkStream> 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);