summaryrefslogtreecommitdiff
path: root/lib/bio/test/bio-read.myr
blob: 3e477c2cb2238d16d5cab81d18857dfcf9267bcb (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std
use bio

const main = {
	var f
	/* Must be bigger than a bio buffer (ie, > 64k) */
	var buf : byte[64*1024]
	var b

	match bio.open("data/datafile", bio.Rd)
	| `std.Ok bio:	f = bio
	| `std.Err m:	std.fatal("Unable to open data file: {}\n", m)
	;;
	
	/* read a 4 byte chunk j*/
	b = r(f, buf[:4])
	std.write(1, b)
	std.write(1, "\n")

	/* read the next 32 bytes */
	b = r(f, buf[:32])
	std.write(1, b)
	std.write(1, "\n")

	/* read a 64k chunk */
	b = r(f, buf[:])
	std.write(1, b)
	std.write(1, "\n")

	/* read to EOF */
	b = r(f, buf[:])
	std.write(1, b)
	std.write(1, "\n")

	/* and fail */
	b = r(f, buf[:])

	bio.close(f)
}

const r = {f, buf
	match bio.read(f, buf)
	| `std.Ok b:
		-> b
	| `std.Err `bio.Eof:
		std.put("eof\n")
		-> ""
	| `std.Err e:
		std.put("err\n")
		-> ""
	;;
}