File: util.ha

package info (click to toggle)
hare 0.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,948 kB
  • sloc: asm: 1,264; makefile: 123; sh: 114; lisp: 101
file content (41 lines) | stat: -rw-r--r-- 1,118 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
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

// Reads an entire buffer, perhaps issuing several [[read]] calls to do so. If
// EOF is immediately encountered, it is returned; if [[EOF]] is encountered
// partway through reading the buffer, [[underread]] is returned.
export fn readall(in: handle, buf: []u8) (size | EOF | error) = {
	let z: size = 0;
	for (z < len(buf)) {
		match (read(in, buf[z..])?) {
		case EOF =>
			if (z == 0) {
				return EOF;
			};
			return z: underread: error;
		case let n: size =>
			z += n;
		};
	};
	return z;
};

// Writes an entire buffer, perhaps issuing several [[write]] calls to do so.
// Aborts on errors after partial writes. Hence it should only be used if it is
// certain that the underlying writes will not fail after an initial write.
export fn writeall(out: handle, buf: const []u8) (size | error) = {
	let z: size = 0;
	for (z < len(buf)) {
		z += match (write(out, buf[z..])) {
		case let s: size =>
			yield s;
		case let e: error =>
			if (z == 0) {
				return e;
			};

			abort("error after partial write");
		};
	};
	return z;
};