summaryrefslogtreecommitdiff
path: root/lib/bio/geti.myr
blob: 4e9facaf4a77e9038fd47d8ecd882fdf5cee404e (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
52
53
54
55
56
57
58
59
60
61
62
use std

use "bio"
use "types"

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

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

/*
  reads a single integer-like value to the output stream, in
  little endian format
*/
generic getle = {f, n -> std.result(@a, err) :: numeric,integral @a
	match ensureread(f, n)
	| `std.Err e :	-> `std.Err e
	| `std.Ok _:
		var v = 0
		for var i = 0; i < n; i++
			v |= (f.rbuf[f.rstart++] : uint64) << (8*(i : uint64))
		;;
		-> `std.Ok (v : @a)
	;;
}

/*
  reads a single integer-like value to the output stream, in
  big endian format
*/
generic getbe = {f, n -> std.result(@a, err) :: numeric,integral @a
	match ensureread(f, n)
	| `std.Err e :	-> `std.Err e
	| `std.Ok _:
		var v = 0
		for var i = 0; i < n; i++
			v <<= 8
			v |= (f.rbuf[f.rstart++] : uint64)
		;;
		-> `std.Ok (v : @a)
	;;
}

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)}