Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / instream.sml
CommitLineData
7f918cf1
CE
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
8structure Instream: INSTREAM =
9struct
10
11open Instream0
12
13structure String = ZString
14
15val input =
16 Trace.trace ("Instream.input", layout, String.layout) input
17
18fun 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
28val inputLine =
29 Trace.trace ("Instream.inputLine", layout, Option.layout String.layout) inputLine
30
31fun 'a withClose (ins: t, f: t -> 'a): 'a =
32 Exn.finally (fn () => f ins, fn () => close ins)
33
34fun '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
38fun withNull f = withIn ("/dev/zero", f)
39
40fun lines ins = rev (foldLines (ins, [], op ::))
41
42end
43
44structure In = Instream