summaryrefslogtreecommitdiff
path: root/lib/bio/test/bio-peek.myr
blob: a0f475243e55e6ac210092aecf78b1765c56f414 (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
use std
use bio

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

	match bio.open("data/datafile", bio.Rd)
	| `std.Ok bio:	f = bio
	| `std.Err m:	std.fatal("Unable to open data file: {}\n", m)
	;;
	
	std.assert(peekb(f) == 0x30, "wrong byte value read from datafile\n")
	std.assert(peekc(f) == '0', "wrong char value read from datafile\n")

	bio.read(f, buf[:4])	/* skip ahead 4 bytes */
	std.assert(peekb(f) == 0x34, "wrong byte value read from datafile\n")
	std.assert(peekc(f) == '4', "wrong char value read from datafile\n")

	bio.read(f, buf[:])	/* skip ahead 64k */
	std.assert(peekb(f) == 0x30, "wrong byte value read from datafile\n")
	std.assert(peekc(f) == '0', "wrong char value read from datafile\n")

	bio.close(f);
	std.put("Succeded peeeking values\n")
}

const peekc = {f
	match bio.peekc(f)
	| `std.Ok c:	-> c
	| `std.Err `bio.Eof:
		std.put("eof\n")
		-> -1
	| `std.Err e:
		std.fatal("error reading\n")
		-> -1
	;;
}

const peekb = {f
	match bio.peekb(f)
	| `std.Ok b:	-> b
	| `std.Err `bio.Eof:
		std.put("eof\n")
		-> -1
	| `std.Err e:
		std.fatal("error reading\n")
		-> -1
	;;
}