summaryrefslogtreecommitdiff
path: root/project2/files/writeStream.cpp
blob: 485c13851c60ba368e2d1e111d600a6d286d4065 (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
37
#include "scriptLoader.h"
#include "task.h"
#include "stream.h"
#include "variables.h"
#include <scriptStorage.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<StreamFactory>(&stream));
		}

		void execute(ExecContext * ec) const
		{
			std::fstream ddd(path(ec).as<std::string>(), std::fstream::trunc | std::fstream::out);
			if (!ddd.good()) {
				throw OpenTargetFile(path(ec));
			}
			stream->runStream([&ddd](const char * data, size_t len) -> size_t {
					ddd.write(data, len);
					return len;
					}, ec);
		}

		const Variable path;
		StreamPtr stream;
};

NAMEDFACTORY("writestream", WriteStream, TaskFactory);