about summary refs log tree commit diff stats
path: root/088file.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-08-16 17:13:26 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-08-16 17:13:26 -0700
commitda925d0697d61db6d265fd78cab3d1adf214950c (patch)
tree24fcbd533c7c4fb1393b02497b62964178094fe7 /088file.mu
parent78a12c9d706f6197d3710ece04e9bd8d4eebe713 (diff)
downloadmu-da925d0697d61db6d265fd78cab3d1adf214950c.tar.gz
3203 - testable interface for reading a file
This commit was written by Stephen Malina. Thanks also to Stephen for
running into the bug of commit 3202.
Diffstat (limited to '088file.mu')
-rw-r--r--088file.mu31
1 files changed, 31 insertions, 0 deletions
diff --git a/088file.mu b/088file.mu
new file mode 100644
index 00000000..25263fa9
--- /dev/null
+++ b/088file.mu
@@ -0,0 +1,31 @@
+# Wrappers around file-system primitives that take a 'filesystem' object and
+# are thus easier to test.
+
+container filesystem [
+  {data: (address table (address array character) (address array character))}
+]
+
+def start-reading fs:address:filesystem, filename:address:array:character -> contents:address:source:character [
+  local-scope
+  load-ingredients
+  x:number/file <- $open-file-for-reading filename
+  contents:address:source:character, sink:address:sink:character <- new-channel 30
+  $print [sink: ], sink, 10/newline
+  chan:address:channel:character <- get *sink, chan:offset
+  $print [chan in start-reading: ], chan, 10/newline
+  start-running transmit x, sink
+]
+
+def transmit file:number, sink:address:sink:character -> file:number, sink:address:sink:character [
+  local-scope
+  load-ingredients
+  {
+    c:character <- $read-from-file file
+    break-unless c
+    sink <- write sink, c
+    loop
+  }
+  $print [closing chan after reading file], 10/newline
+  sink <- close sink
+  $print [returning from 'transmit'], 10/newline
+]