## io-with.pkg
# Compiled by:
#
src/lib/std/standard.lib### "My method is to take the utmost trouble
### to find the right thing to say.
###
### "And then say it with the utmost levity."
###
### -- George Bernard Shaw
stipulate
package fil = file__premicrothread; # file__premicrothread is from
src/lib/std/src/posix/file--premicrothread.pkgherein
package io_with
: (weak) Io_With # Io_With is from
src/lib/src/io-with.api {
Input_Stream = fil::Input_Stream;
Output_Stream = fil::Output_Stream;
fun swap_instream (s, s')
=
fil::get_instream s
then
fil::set_instream (s, s');
fun with_input_file (s, f) x
=
result
where
old_strm
=
swap_instream
( fil::stdin,
fil::get_instream (fil::open_for_read s)
);
fun clean_up ()
=
fil::pur::close_input
#
(swap_instream (fil::stdin, old_strm));
result
=
(f x)
except
x = { clean_up ();
raise exception x;
};
clean_up();
end;
#
fun with_instream (stream, f) x
=
{ old_strm = swap_instream (fil::stdin, fil::get_instream stream);
#
fun clean_up ()
=
fil::set_instream (stream, swap_instream (fil::stdin, old_strm));
result
=
(f x)
except
ex = { clean_up ();
raise exception ex;
};
clean_up();
result;
};
fun swap_outstrm (s, s')
=
fil::get_outstream s
then
fil::set_outstream (s, s');
fun with_output_file (s, f) x
=
{ old_strm
=
swap_outstrm
( fil::stdout,
fil::get_outstream (fil::open_for_write s)
);
fun clean_up ()
=
fil::pur::close_output (swap_outstrm (fil::stdout, old_strm));
result = (f x)
except
x = { clean_up ();
raise exception x;
};
clean_up();
result;
};
fun with_outstream (stream, f) x
=
{ old_strm
=
swap_outstrm
( fil::stdout,
fil::get_outstream stream
);
fun clean_up ()
=
fil::set_outstream (stream, swap_outstrm (fil::stdout, old_strm));
result = f x
except
x = { clean_up ();
raise exception x;
};
clean_up ();
result;
};
}; # package io_with
end;