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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2144 - (download) (annotate)
Thu Nov 2 16:23:11 2006 UTC (12 years, 8 months ago) by blume
File size: 1564 byte(s)
moved smlnj-lib to toplevel
(* io-util.sml
 *
 * COPYRIGHT (c) 1997 AT&T Labs Research.
 *)

structure IOUtil : IO_UTIL =
  struct

    type instream = TextIO.instream
    type outstream = TextIO.outstream

    fun swapInstrm (s, s') =
	  TextIO.getInstream s before TextIO.setInstream(s, s')

    fun withInputFile (s, f) x = let
	  val oldStrm = swapInstrm(TextIO.stdIn, TextIO.getInstream(TextIO.openIn s))
	  fun cleanUp () =
		TextIO.StreamIO.closeIn(swapInstrm(TextIO.stdIn, oldStrm))
	  val res = (f x) handle ex => (cleanUp(); raise ex)
	  in
	    cleanUp();
	    res
	  end

    fun withInstream (strm, f) x = let
	    val oldStrm = swapInstrm(TextIO.stdIn, TextIO.getInstream strm)
	    fun cleanUp () =
		  TextIO.setInstream(strm, swapInstrm(TextIO.stdIn, oldStrm))
	    val res = (f x) handle ex => (cleanUp(); raise ex)
	    in
	      cleanUp();
	      res
	    end

    fun swapOutstrm (s, s') =
	  TextIO.getOutstream s before TextIO.setOutstream(s, s')

    fun withOutputFile (s, f) x = let
	  val oldStrm = swapOutstrm(TextIO.stdOut, TextIO.getOutstream(TextIO.openOut s))
	  fun cleanUp () =
		TextIO.StreamIO.closeOut(swapOutstrm(TextIO.stdOut, oldStrm))
	  val res = (f x) handle ex => (cleanUp(); raise ex)
	  in
	    cleanUp();
	    res
	  end

    fun withOutstream (strm, f) x = let
	    val oldStrm = swapOutstrm(TextIO.stdOut, TextIO.getOutstream strm)
	    fun cleanUp () =
		  TextIO.setOutstream(strm, swapOutstrm(TextIO.stdOut, oldStrm))
	    val res = (f x) handle ex => (cleanUp(); raise ex)
	    in
	      cleanUp();
	      res
	    end

  end (* IOUtil *)

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