Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /smlnj-lib/trunk/Util/io-util.sml
ViewVC logotype

Annotation of /smlnj-lib/trunk/Util/io-util.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2144 - (view) (download)

1 : monnier 2 (* io-util.sml
2 :     *
3 :     * COPYRIGHT (c) 1997 AT&T Labs Research.
4 :     *)
5 :    
6 :     structure IOUtil : IO_UTIL =
7 :     struct
8 :    
9 :     type instream = TextIO.instream
10 :     type outstream = TextIO.outstream
11 :    
12 :     fun swapInstrm (s, s') =
13 :     TextIO.getInstream s before TextIO.setInstream(s, s')
14 :    
15 :     fun withInputFile (s, f) x = let
16 :     val oldStrm = swapInstrm(TextIO.stdIn, TextIO.getInstream(TextIO.openIn s))
17 :     fun cleanUp () =
18 :     TextIO.StreamIO.closeIn(swapInstrm(TextIO.stdIn, oldStrm))
19 :     val res = (f x) handle ex => (cleanUp(); raise ex)
20 :     in
21 :     cleanUp();
22 :     res
23 :     end
24 :    
25 :     fun withInstream (strm, f) x = let
26 :     val oldStrm = swapInstrm(TextIO.stdIn, TextIO.getInstream strm)
27 :     fun cleanUp () =
28 :     TextIO.setInstream(strm, swapInstrm(TextIO.stdIn, oldStrm))
29 :     val res = (f x) handle ex => (cleanUp(); raise ex)
30 :     in
31 :     cleanUp();
32 :     res
33 :     end
34 :    
35 :     fun swapOutstrm (s, s') =
36 :     TextIO.getOutstream s before TextIO.setOutstream(s, s')
37 :    
38 :     fun withOutputFile (s, f) x = let
39 :     val oldStrm = swapOutstrm(TextIO.stdOut, TextIO.getOutstream(TextIO.openOut s))
40 :     fun cleanUp () =
41 :     TextIO.StreamIO.closeOut(swapOutstrm(TextIO.stdOut, oldStrm))
42 :     val res = (f x) handle ex => (cleanUp(); raise ex)
43 :     in
44 :     cleanUp();
45 :     res
46 :     end
47 :    
48 :     fun withOutstream (strm, f) x = let
49 :     val oldStrm = swapOutstrm(TextIO.stdOut, TextIO.getOutstream strm)
50 :     fun cleanUp () =
51 :     TextIO.setOutstream(strm, swapOutstrm(TextIO.stdOut, oldStrm))
52 :     val res = (f x) handle ex => (cleanUp(); raise ex)
53 :     in
54 :     cleanUp();
55 :     res
56 :     end
57 :    
58 :     end (* IOUtil *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0