5009
|
1 |
(* Title: Pure/Thy/file.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
File system operations.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature FILE =
|
|
9 |
sig
|
|
10 |
val tmp_name: string -> string
|
|
11 |
val exists: string -> bool
|
|
12 |
val info: string -> string
|
|
13 |
val read: string -> string
|
|
14 |
val write: string -> string -> unit
|
|
15 |
val append: string -> string -> unit
|
|
16 |
val copy: string -> string -> unit
|
|
17 |
end;
|
|
18 |
|
|
19 |
structure File: FILE =
|
|
20 |
struct
|
|
21 |
|
|
22 |
|
|
23 |
(* tmp_name *)
|
|
24 |
|
|
25 |
fun tmp_name name =
|
|
26 |
Path.pack (Path.evaluate (Path.unpack o getenv)
|
|
27 |
(Path.append (Path.variable "ISABELLE_TMP") (Path.unpack name)));
|
|
28 |
|
|
29 |
|
|
30 |
(* exists / info *)
|
|
31 |
|
|
32 |
fun exists name = (file_info name <> "");
|
|
33 |
val info = file_info;
|
|
34 |
|
|
35 |
|
|
36 |
(* read / write files *)
|
|
37 |
|
|
38 |
fun read name =
|
|
39 |
let
|
|
40 |
val instream = TextIO.openIn name;
|
|
41 |
val intext = TextIO.inputAll instream;
|
|
42 |
in
|
|
43 |
TextIO.closeIn instream;
|
|
44 |
intext
|
|
45 |
end;
|
|
46 |
|
|
47 |
fun write name txt =
|
|
48 |
let val outstream = TextIO.openOut name in
|
|
49 |
TextIO.output (outstream, txt);
|
|
50 |
TextIO.closeOut outstream
|
|
51 |
end;
|
|
52 |
|
|
53 |
fun append name txt =
|
|
54 |
let val outstream = TextIO.openAppend name in
|
|
55 |
TextIO.output (outstream, txt);
|
|
56 |
TextIO.closeOut outstream
|
|
57 |
end;
|
|
58 |
|
|
59 |
fun copy infile outfile =
|
|
60 |
if not (exists infile) then error ("File not found: " ^ quote infile)
|
|
61 |
else write outfile (read infile);
|
|
62 |
|
|
63 |
|
|
64 |
end;
|