shithub: mc

ref: cef2d4ebd48ab554330e49f8961547e05df47127
dir: /lib/bio/test/bio-delim.myr/

View raw version
use std
use bio

const main = {
	var f
	var d

	match bio.open("data/lines", bio.Rd)
	| `std.Ok bio:	f = bio
	| `std.Err m:	std.fatal("Unable to open data file: {}\n", m)
	;;

	/* read first line */
	d = readln(f)
	std.write(1, d)
	std.write(1, "\n")
	std.slfree(d)

	/* read second line, should not include \n */
	d = readln(f)
	std.write(1, d)
	std.write(1, "\n")
	std.slfree(d)

	/* read second line, should not include \r\n */
	d = readln(f)
	std.write(1, d)
	std.write(1, "\n")
	std.slfree(d)

	/* read second line, should not include \r */
	d = readln(f)
	std.write(1, d)
	std.write(1, "\n")
	std.slfree(d)

	/* read to ';' */
	d = readto(f, ";")
	std.write(1, d)
	std.write(1, "\n")
	std.slfree(d)

	/* read to ';'  again */
	d = readto(f, ";")
	std.write(1, d)
	std.write(1, "\n")
	std.slfree(d)

	/* '--'  this time */
	d = readto(f, "--")
	std.write(1, d)
	std.write(1, "\n")
	std.slfree(d)

	/* and without the terminator, we should get the remaining text */
	d = readto(f, "not-there")
	std.write(1, d)
	std.write(1, "\n")
	std.slfree(d)

	/* and now, eof */
	d = readln(f)
	d = readto(f, "actually, eof")
	
	bio.close(f)
}

const readln = {f
	match bio.readln(f)
	| `std.Ok d:	-> d
	| `std.Err `bio.Eof:
		std.put("eof\n")
		-> [][:]
	| `std.Err e:
		std.put("err\n")
		-> [][:]
	;;
}

const readto = {f, delim
	match bio.readto(f, delim)
	| `std.Ok d:	-> d
	| `std.Err `bio.Eof:
		std.put("eof\n")
		-> [][:]
	| `std.Err e:
		std.put("err\n")
		-> [][:]
	;;
}