summaryrefslogtreecommitdiff
path: root/lib/fileutil/walk.myr
blob: 851c1f12ccf2a3c9eaf939a4050e8caed9ed6619 (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
63
64
use std

pkg fileutil =
	type walkiter = struct
		dirstk	: std.dir#[:]
		curdir	: byte[:][:]
	;;

	impl iterable walkiter -> byte[:]
	const bywalk	: (dir : byte[:] -> walkiter)
;;

const bywalk = {p
	match std.diropen(p)
	| `std.Ok d:
		-> [.dirstk = std.sldup([d][:]), .curdir = std.sldup([std.sldup(p)][:])]
	| `std.Err e:
		-> [.dirstk = [][:], .curdir = [][:]]
	;;
}

impl iterable walkiter -> byte[:] =
	__iternext__ = {itp, valp
		var cur, p

:nextfile
		if itp.dirstk.len < 1
			-> false
		;;
		cur = itp.dirstk[itp.dirstk.len - 1]
		match std.dirread(cur)
		| `std.Some ".":	goto nextfile
		| `std.Some "..":	goto nextfile
		| `std.Some ent:
			p = std.pathcat(itp.curdir[itp.curdir.len - 1], ent)
			if std.fisdir(p)
				match std.diropen(p)
				| `std.Ok d:
					std.slpush(&itp.dirstk, d)
					std.slpush(&itp.curdir, p)
				| `std.Err e:	/* ? */
					std.slfree(p)
				;;
				goto nextfile
			;;
			valp# = p
			-> true
		| `std.None:
			std.dirclose(cur)
			std.slfree(itp.curdir[itp.curdir.len - 1])
			std.slpop(&itp.curdir)
			std.slpop(&itp.dirstk)
			goto nextfile
		;;
	}

	__iterfin__ = {itp, valp
		std.slfree(valp#)
	}
;;

generic last = {sl : @a[:]
	-> sl[sl.len - 1]
}