File: BufferedOStream.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 (77 lines) | stat: -rw-r--r-- 2,032 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
#include "stdafx.h"
#include "BufferedOStream.h"

namespace storm {

	BufferedOStream::BufferedOStream(OStream *output) : output(output) {
		init(4096);
	}

	BufferedOStream::BufferedOStream(OStream *output, Nat bufferSize) : output(output) {
		init(bufferSize);
	}

	void BufferedOStream::init(Nat bufferSize) {
		buffer = storm::buffer(engine(), bufferSize);
	}

	Nat BufferedOStream::write(Buffer input, Nat start) {
		if (start >= input.filled())
			return 0;

		// 0: Fast path: if our buffer is empty, and number of bytes are larger than the buffer,
		// just output the buffer as is.
		Nat inputCount = input.filled() - start;
		if (buffer.empty() && inputCount >= buffer.count()) {
			return output->write(input, start);
		}

		// 1: Fill the buffer.
		Nat consumed = 0;
		Nat toCopy = min(inputCount, buffer.free());
		memcpy(buffer.dataPtr() + buffer.filled(), input.dataPtr() + start, toCopy);
		buffer.filled(buffer.filled() + toCopy);
		start += toCopy;
		inputCount -= toCopy;
		consumed += toCopy;

		// 2: Time to flush? If not, then we are done.
		if (!buffer.full())
			return consumed;
		output->write(buffer);
		buffer.filled(0);

		// 3: More data. Either, we can copy the remaining data to the buffer, or it is enough that
		// we can just output the rest as it is (as in the fast-path).
		if (inputCount >= buffer.count()) {
			// Output everything.
			consumed += output->write(input, start);
		} else if (inputCount > 0) {
			// Copy the remaining data. Note: 'inputCount' is less than 'buffer.count()', and the
			// buffer is flushed.
			memcpy(buffer.dataPtr(), input.dataPtr() + start, inputCount);
			buffer.filled(inputCount);
			consumed += inputCount;
		}
		return consumed;
	}

	Bool BufferedOStream::flush() {
		Bool ok = true;
		if (buffer.filled() > 0) {
			ok = output->write(buffer) == buffer.filled();
			buffer.filled(0);
		}
		return ok;
	}

	void BufferedOStream::close() {
		flush();
		output->close();
	}

	sys::ErrorCode BufferedOStream::error() const {
		return output->error();
	}

}