75615
|
1 |
(* Title: Pure/General/file_stream.ML
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
File-system stream operations.
|
|
5 |
*)
|
|
6 |
|
|
7 |
signature FILE_STREAM =
|
|
8 |
sig
|
|
9 |
val open_dir: (OS.FileSys.dirstream -> 'a) -> Path.T -> 'a
|
|
10 |
val open_input: (BinIO.instream -> 'a) -> Path.T -> 'a
|
|
11 |
val open_output: (BinIO.outstream -> 'a) -> Path.T -> 'a
|
|
12 |
val open_append: (BinIO.outstream -> 'a) -> Path.T -> 'a
|
|
13 |
val input: BinIO.instream -> string
|
|
14 |
val input_size: int -> BinIO.instream -> string
|
|
15 |
val input_all: BinIO.instream -> string
|
|
16 |
val output: BinIO.outstream -> string -> unit
|
|
17 |
val outputs: BinIO.outstream -> string list -> unit
|
|
18 |
end;
|
|
19 |
|
|
20 |
structure File_Stream: FILE_STREAM =
|
|
21 |
struct
|
|
22 |
|
|
23 |
(* open streams *)
|
|
24 |
|
|
25 |
local
|
|
26 |
|
|
27 |
val platform_path = ML_System.platform_path o Path.implode o Path.expand;
|
|
28 |
|
|
29 |
fun with_file open_file close_file f =
|
78716
|
30 |
Thread_Attributes.uninterruptible (fn run => fn path =>
|
75615
|
31 |
let
|
|
32 |
val file = open_file path;
|
78716
|
33 |
val result = Exn.capture (run f) file;
|
78718
|
34 |
val _ = close_file file;
|
|
35 |
in Exn.release result end);
|
75615
|
36 |
|
|
37 |
in
|
|
38 |
|
|
39 |
fun open_dir f = with_file OS.FileSys.openDir OS.FileSys.closeDir f o platform_path;
|
|
40 |
fun open_input f = with_file BinIO.openIn BinIO.closeIn f o platform_path;
|
|
41 |
fun open_output f = with_file BinIO.openOut BinIO.closeOut f o platform_path;
|
|
42 |
fun open_append f = with_file BinIO.openAppend BinIO.closeOut f o platform_path;
|
|
43 |
|
|
44 |
end;
|
|
45 |
|
|
46 |
|
|
47 |
(* input *)
|
|
48 |
|
|
49 |
val input = Byte.bytesToString o BinIO.input;
|
|
50 |
fun input_size n stream = Byte.bytesToString (BinIO.inputN (stream, n));
|
|
51 |
val input_all = Byte.bytesToString o BinIO.inputAll;
|
|
52 |
|
|
53 |
|
|
54 |
(* output *)
|
|
55 |
|
|
56 |
fun output file txt = BinIO.output (file, Byte.stringToBytes txt);
|
|
57 |
val outputs = List.app o output;
|
|
58 |
|
|
59 |
end;
|