]> Joshua Wise's Git repositories - snipe.git/blame_incremental - util/safe-io.sml
Un-size optimize
[snipe.git] / util / safe-io.sml
... / ...
CommitLineData
1(* L1 Compiler
2 * Safe(r) I/O functions
3 * Author: Frank Pfenning <fp@cs.cmu.edu>
4 *)
5
6signature SAFE_IO =
7sig
8 (* withOpenIn fileName (fn instream => body) = result
9 opens fileName for input to obtain instream and evaluates body.
10 The file is closed during normal and abnormal exit of body.
11 *)
12 val withOpenIn : string -> (TextIO.instream -> 'a) -> 'a
13
14 (* withOpenOut fileName (fn outstream => body) = result
15 opens fileName for output to obtain outstream and evaluates body.
16 The file is closed during normal and abnormal exit of body.
17 *)
18 val withOpenOut : string -> (TextIO.outstream -> 'a) -> 'a
19end
20
21structure SafeIO :> SAFE_IO =
22struct
23
24 (* result of a computation *)
25 datatype 'a Result = Value of 'a | Exception of exn
26
27
28 (* withOpenIn fileName (fn instream => body) = result
29 opens fileName for input to obtain instream and evaluates body.
30 The file is closed during normal and abnormal exit of body.
31 *)
32 fun withOpenIn (fileName) (scope) =
33 let
34 val instream = TextIO.openIn fileName
35 (* val _ = fileOpenMsg (fileName) *)
36 val result = Value (scope instream) handle exn => Exception (exn)
37 (* val _ = fileCloseMsg (fileName) *)
38 val _ = TextIO.closeIn instream
39 in
40 case result
41 of Value (x) => x
42 | Exception (exn) => raise exn
43 end
44
45 (* withOpenOut fileName (fn outstream => body) = result
46 opens fileName for input to obtain outstream and evaluates body.
47 The file is closed during normal and abnormal exit of body.
48 *)
49 fun withOpenOut (fileName) (scope) =
50 let
51 val outstream = TextIO.openOut fileName
52 (* val _ = fileOpenMsg (fileName) *)
53 val result = Value (scope outstream) handle exn => Exception (exn)
54 (* val _ = fileCloseMsg (fileName) *)
55 val _ = TextIO.closeOut outstream
56 in
57 case result
58 of Value (x) => x
59 | Exception (exn) => raise exn
60 end
61end
This page took 0.023696 seconds and 4 git commands to generate.