shithub: mc

ref: ddb327c92ebe3f96c5c816ec61996440551de401
dir: /lib/bio/test/bio-read.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]
	var b

	match bio.open("data/datafile", bio.Rd)
	| `std.Ok bio:	f = bio
	| `std.Err m:	std.fatal("Unable to open data file: {}", 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)
	| `bio.Ok b:
		-> b
	| `bio.Eof:
		std.put("eof\n")
		-> ""
	| `bio.Err e:
		std.put("err\n")
		-> ""
	;;
}