File: MemStream.cpp

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (97 lines) | stat: -rw-r--r-- 2,020 bytes parent folder | download | duplicates (2)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "stdafx.h"
#include "MemStream.h"
#include "Core/CloneEnv.h"
#include "Core/StrBuf.h"

namespace storm {

	MemIStream::MemIStream(Buffer b) : data(buffer(engine(), b.filled())), pos(0) {
		// Copy the filled data.
		memcpy(data.dataPtr(), b.dataPtr(), b.filled());
		data.filled(b.filled());
	}

	MemIStream::MemIStream(const MemIStream &o) : data(o.data) {}

	void MemIStream::deepCopy(CloneEnv *env) {
		// We never change 'data', so we do not need to clone it here.
	}

	Bool MemIStream::more() {
		return pos < data.count();
	}

	Buffer MemIStream::read(Buffer to) {
		Nat start = to.filled();
		to = peek(to);
		pos += to.filled() - start;
		return to;
	}

	Buffer MemIStream::peek(Buffer to) {
		Nat start = to.filled();
		Nat copy = min(to.count() - start, data.count() - pos);
		memcpy(to.dataPtr() + start, data.dataPtr() + pos, copy);
		to.filled(copy + start);
		return to;
	}

	void MemIStream::seek(Word to) {
		pos = min(nat(to), data.count());
	}

	Word MemIStream::tell() {
		return pos;
	}

	Word MemIStream::length() {
		return data.count();
	}

	RIStream *MemIStream::randomAccess() {
		return this;
	}

	void MemIStream::toS(StrBuf *to) const {
		data.outputMark(to, pos);
	}


	MemOStream::MemOStream() {}

	MemOStream::MemOStream(Buffer appendTo) : data(appendTo) {}

	MemOStream::MemOStream(const MemOStream &o) : data(o.data) {
		data.deepCopy(null);
	}

	void MemOStream::deepCopy(CloneEnv *env) {
		// Nothing to do...
	}

	Nat MemOStream::write(Buffer src, Nat start) {
		start = min(start, src.filled());
		Nat copy = src.filled() - start;
		Nat filled = data.filled();
		if (filled + copy >= data.count()) {
			// Grow the buffer...
			Nat growTo = max(max(data.count() * 2, copy), Nat(1024));
			data = grow(engine(), data, growTo);
		}

		// Copy data.
		memcpy(data.dataPtr() + filled, src.dataPtr() + start, copy);
		data.filled(filled + copy);

		return copy;
	}

	Buffer MemOStream::buffer() {
		return data;
	}

	void MemOStream::toS(StrBuf *to) const {
		*to << data;
	}

}