summaryrefslogtreecommitdiff
path: root/project2/files/writeStream.cpp
blob: 6e49e10dc5c6910cef6b02d4e9d082d2a8194206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "scriptLoader.h"
#include "task.h"
#include "stream.h"
#include "variables.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);