shithub: mc

ref: 73518d58457d543258e344b96a8f4b13ccd16882
dir: /lib/bio/geti.myr/

View raw version
use std

use "bio"

pkg bio =
	/* unsigned big endian */
	generic getbe8	: (f : file# -> status(@a::(numeric,integral)))
	generic getbe16	: (f : file# -> status(@a::(numeric,integral)))
	generic getbe32	: (f : file# -> status(@a::(numeric,integral)))
	generic getbe64	: (f : file# -> status(@a::(numeric,integral)))

	/* signed big endian */
	generic getle8	: (f : file# -> status(@a::(numeric,integral)))
	generic getle16	: (f : file# -> status(@a::(numeric,integral)))
	generic getle32	: (f : file# -> status(@a::(numeric,integral)))
	generic getle64	: (f : file# -> status(@a::(numeric,integral)))
;;

/*
  reads a single integer-like value to the output stream, in
  little endian format
*/
generic getle = {f, n -> status(@a::(numeric,integral))
	var v, i

	v = 0
	match ensureread(f, n)
	| `Eof:	-> `Eof
	| `Err e :	-> `Err e
	| `Ok _:
		for i = 0; i < n; i++
			v |= (f.rbuf[f.rstart++] : uint64) << (8*(i : uint64))
		;;
		-> `Ok (v : @a::(numeric,integral))
	;;
}

/*
  reads a single integer-like value to the output stream, in
  big endian format
*/
generic getbe = {f, n -> status(@a::(numeric,integral))
	var v, i

	v = 0
	match ensureread(f, n)
	| `Eof:	-> `Eof
	| `Err e :	-> `Err e
	| `Ok _:
		for i = 0; i < n; i++
			v <<= 8
			v |= (f.rbuf[f.rstart++] : uint64)
		;;
		-> `Ok (v : @a::(numeric,integral))
	;;
}

generic getbe8  = {f; -> getbe(f, 1)}
generic getbe16 = {f; -> getbe(f, 2)}
generic getbe32 = {f; -> getbe(f, 4)}
generic getbe64 = {f; -> getbe(f, 8)}

generic getle8  = {f; -> getle(f, 1)}
generic getle16 = {f; -> getle(f, 2)}
generic getle32 = {f; -> getle(f, 4)}
generic getle64 = {f; -> getle(f, 8)}