File: Stream.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 (255 lines) | stat: -rw-r--r-- 4,727 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "stdafx.h"
#include "Stream.h"
#include "Exception.h"
#include "LazyMemStream.h"

namespace storm {

	/**
	 * IStream.
	 */

	IStream::IStream() {}

	Bool IStream::more() {
		return false;
	}

	Buffer IStream::read(Nat c) {
		return read(buffer(engine(), c));
	}

	Buffer IStream::read(Buffer to) {
		return to;
	}

	Buffer IStream::peek(Nat c) {
		return peek(buffer(engine(), c));
	}

	Buffer IStream::peek(Buffer to) {
		return to;
	}

	Buffer IStream::fill(Nat c) {
		return fill(buffer(engine(), c));
	}

	Buffer IStream::fill(Buffer b) {
		b.filled(0);
		while (!b.full()) {
			Nat old = b.filled();
			b = read(b);

			// End of stream reached?
			if (b.filled() == old)
				break;
		}
		return b;
	}

	RIStream *IStream::randomAccess() {
		return new (this) LazyMemIStream(this);
	}

	void IStream::close() {}

	sys::ErrorCode IStream::error() const {
		return sys::none;
	}

	/**
	 * RIStream.
	 */

	RIStream::RIStream() {}

	void RIStream::seek(Word to) {}

	Word RIStream::tell() { return 0; }

	Word RIStream::length() { return 0; }

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


	/**
	 * OStream.
	 */

	OStream::OStream() {}

	Nat OStream::write(Buffer buf) {
		return write(buf, 0);
	}

	Nat OStream::write(Buffer buf, Nat start) {
		return 0;
	}

	Bool OStream::flush() {
		return true;
	}

	void OStream::close() {}

	sys::ErrorCode OStream::error() const {
		return sys::none;
	}

	/**
	 * Read/write primitive types.
	 */

	static void checkBuffer(Engine &e, const Buffer &b) {
		if (!b.full())
			throw new (e) IoError(S("Not enough data to read primitive."));
	}

	Bool IStream::readBool() {
		return readByte() != 0;
	}

	Byte IStream::readByte() {
		GcPreArray<Byte, 1> d;
		Buffer b = fill(emptyBuffer(d));
		checkBuffer(engine(), b);
		return d.v[0];
	}

	Int IStream::readInt() {
		GcPreArray<Byte, 4> d;
		Buffer b = fill(emptyBuffer(d));
		checkBuffer(engine(), b);
		Int r = Int(b[0]) << 24;
		r |= Int(b[1]) << 16;
		r |= Int(b[2]) << 8;
		r |= Int(b[3]) << 0;
		return r;
	}

	Nat IStream::readNat() {
		GcPreArray<Byte, 4> d;
		Buffer b = fill(emptyBuffer(d));
		checkBuffer(engine(), b);
		Nat r = Nat(b[0]) << 24;
		r |= Nat(b[1]) << 16;
		r |= Nat(b[2]) << 8;
		r |= Nat(b[3]) << 0;
		return r;
	}

	Long IStream::readLong() {
		GcPreArray<Byte, 8> d;
		Buffer b = fill(emptyBuffer(d));
		checkBuffer(engine(), b);
		Long r = Long(b[0]) << 56;
		r |= Long(b[1]) << 48;
		r |= Long(b[2]) << 40;
		r |= Long(b[3]) << 32;
		r |= Long(b[4]) << 24;
		r |= Long(b[5]) << 16;
		r |= Long(b[6]) << 8;
		r |= Long(b[7]) << 0;
		return r;
	}

	Word IStream::readWord() {
		GcPreArray<Byte, 8> d;
		Buffer b = fill(emptyBuffer(d));
		checkBuffer(engine(), b);
		Long r = Long(b[0]) << 56;
		r |= Long(b[1]) << 48;
		r |= Long(b[2]) << 40;
		r |= Long(b[3]) << 32;
		r |= Long(b[4]) << 24;
		r |= Long(b[5]) << 16;
		r |= Long(b[6]) << 8;
		r |= Long(b[7]) << 0;
		return r;
	}

	Float IStream::readFloat() {
		Nat repr = readNat();
		Float r;
		memcpy(&r, &repr, sizeof(repr));
		return r;
	}

	Double IStream::readDouble() {
		Word repr = readWord();
		Double r;
		memcpy(&r, &repr, sizeof(repr));
		return r;
	}


	void OStream::writeBool(Bool v) {
		writeByte(v ? 1 : 0);
	}

	void OStream::writeByte(Byte v) {
		GcPreArray<Byte, 1> d;
		d.v[0] = v;
		write(fullBuffer(d));
	}

	void OStream::writeInt(Int v) {
		GcPreArray<Byte, 4> d;
		d.v[0] = Byte((v >> 24) & 0xFF);
		d.v[1] = Byte((v >> 16) & 0xFF);
		d.v[2] = Byte((v >>  8) & 0xFF);
		d.v[3] = Byte((v >>  0) & 0xFF);
		write(fullBuffer(d));
	}

	void OStream::writeNat(Nat v) {
		GcPreArray<Byte, 4> d;
		d.v[0] = Byte((v >> 24) & 0xFF);
		d.v[1] = Byte((v >> 16) & 0xFF);
		d.v[2] = Byte((v >>  8) & 0xFF);
		d.v[3] = Byte((v >>  0) & 0xFF);
		write(fullBuffer(d));
	}

	void OStream::writeLong(Long v) {
		GcPreArray<Byte, 8> d;
		d.v[0] = Byte((v >> 56) & 0xFF);
		d.v[1] = Byte((v >> 48) & 0xFF);
		d.v[2] = Byte((v >> 40) & 0xFF);
		d.v[3] = Byte((v >> 32) & 0xFF);
		d.v[4] = Byte((v >> 24) & 0xFF);
		d.v[5] = Byte((v >> 16) & 0xFF);
		d.v[6] = Byte((v >>  8) & 0xFF);
		d.v[7] = Byte((v >>  0) & 0xFF);
		write(fullBuffer(d));
	}

	void OStream::writeWord(Word v) {
		GcPreArray<Byte, 8> d;
		d.v[0] = Byte((v >> 56) & 0xFF);
		d.v[1] = Byte((v >> 48) & 0xFF);
		d.v[2] = Byte((v >> 40) & 0xFF);
		d.v[3] = Byte((v >> 32) & 0xFF);
		d.v[4] = Byte((v >> 24) & 0xFF);
		d.v[5] = Byte((v >> 16) & 0xFF);
		d.v[6] = Byte((v >>  8) & 0xFF);
		d.v[7] = Byte((v >>  0) & 0xFF);
		write(fullBuffer(d));
	}

	void OStream::writeFloat(Float v) {
		Nat repr;
		memcpy(&repr, &v, sizeof(v));
		writeNat(repr);
	}

	void OStream::writeDouble(Double v) {
		Word repr;
		memcpy(&repr, &v, sizeof(v));
		writeWord(repr);
	}

}