shithub: mc

ref: 2a0ea983cd6365e3212abe5a5ce3cf74a554daf9
dir: /lib/bio/test/bio-peek.myr/

View raw version
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: {}", m)
	;;
	
	std.assert(peekb(f) == 0x30, "wrong byte value read from datafile")
	std.assert(peekc(f) == '0', "wrong char value read from datafile")

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

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

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

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

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