Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / instream.sml
1 (* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8 structure Instream: INSTREAM =
9 struct
10
11 open Instream0
12
13 structure String = ZString
14
15 val input =
16 Trace.trace ("Instream.input", layout, String.layout) input
17
18 fun outputAll (ins: t, out: Out.t): unit =
19 let
20 fun loop () =
21 case input ins of
22 "" => ()
23 | s => (Out.output (out, s); loop ())
24 in
25 loop ()
26 end
27
28 val inputLine =
29 Trace.trace ("Instream.inputLine", layout, Option.layout String.layout) inputLine
30
31 fun 'a withClose (ins: t, f: t -> 'a): 'a =
32 Exn.finally (fn () => f ins, fn () => close ins)
33
34 fun 'a withIn (f: string, g: t -> 'a): 'a =
35 withClose (openIn f handle IO.Io _ =>
36 Error.bug (concat ["Instream.withIn: cannot open ", f]), g)
37
38 fun withNull f = withIn ("/dev/zero", f)
39
40 fun lines ins = rev (foldLines (ins, [], op ::))
41
42 end
43
44 structure In = Instream