ref: 894bed9a153ab56763627975f9e109281bec297b
parent: 89363fe7fc174052ede24bcec2e28eacf26e39ed
author: Ori Bernstein <[email protected]>
date: Fri Aug 29 13:10:34 EDT 2014
Split slurp into a variant that also takes an FD
--- a/libstd/slurp.myr
+++ b/libstd/slurp.myr
@@ -8,6 +8,7 @@
pkg std =
const slurp : (path : byte[:] -> result(byte[:], byte[:]))
+ const fslurp : (path : fd -> result(byte[:], byte[:]))
;;
const Bufinc = 4096
@@ -14,20 +15,23 @@
const slurp = {path
var fd
- var n
- var len
- var buf
-
fd = open(path, Ordonly)
if fd < 0
-> `Fail "Could not open file"
;;
+ -> fslurp(fd)
+}
+const fslurp = {fd
+ var n
+ var len
+ var buf
+
len = 0
buf = slalloc(Bufinc)
while true
n = read(fd, buf[len:])
- if n == 0
+ if n <= 0
goto done
;;
len += n castto(size)