6118
|
1 |
(* Title: Pure/General/file.ML
|
5009
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
File system operations.
|
6182
|
6 |
|
|
7 |
TODO:
|
|
8 |
- close in case of error (!?);
|
5009
|
9 |
*)
|
|
10 |
|
|
11 |
signature FILE =
|
|
12 |
sig
|
6182
|
13 |
val sys_path_fn: (Path.T -> string) ref
|
|
14 |
val tmp_path: Path.T -> Path.T
|
|
15 |
eqtype info
|
|
16 |
val info: Path.T -> info option
|
|
17 |
val exists: Path.T -> bool
|
|
18 |
val read: Path.T -> string
|
|
19 |
val write: Path.T -> string -> unit
|
|
20 |
val append: Path.T -> string -> unit
|
|
21 |
val copy: Path.T -> Path.T -> unit
|
|
22 |
val use: Path.T -> unit
|
|
23 |
val cd: Path.T -> unit
|
|
24 |
val rm: Path.T -> unit
|
5009
|
25 |
end;
|
|
26 |
|
|
27 |
structure File: FILE =
|
|
28 |
struct
|
|
29 |
|
|
30 |
|
6182
|
31 |
(* sys_path (default for Unix) *)
|
|
32 |
|
|
33 |
val sys_path_fn = ref Path.pack;
|
|
34 |
fun sysify path = ! sys_path_fn (Path.expand path);
|
5009
|
35 |
|
6182
|
36 |
|
|
37 |
(* tmp_path *)
|
|
38 |
|
|
39 |
fun tmp_path path =
|
|
40 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
|
5009
|
41 |
|
|
42 |
|
6182
|
43 |
(* info / exists *)
|
|
44 |
|
|
45 |
datatype info = Info of string;
|
5009
|
46 |
|
6182
|
47 |
fun info path =
|
|
48 |
let val name = sysify path in
|
|
49 |
(case file_info name of
|
|
50 |
"" => None
|
|
51 |
| s => Some (Info (name ^ ": " ^ s))) (* FIXME include full path (!?) *)
|
|
52 |
end;
|
|
53 |
|
|
54 |
val exists = is_some o info;
|
5009
|
55 |
|
|
56 |
|
|
57 |
(* read / write files *)
|
|
58 |
|
6182
|
59 |
fun read path =
|
5009
|
60 |
let
|
6182
|
61 |
val instream = TextIO.openIn (sysify path);
|
5009
|
62 |
val intext = TextIO.inputAll instream;
|
|
63 |
in
|
|
64 |
TextIO.closeIn instream;
|
|
65 |
intext
|
|
66 |
end;
|
|
67 |
|
6182
|
68 |
fun write path txt =
|
|
69 |
let val outstream = TextIO.openOut (sysify path) in
|
|
70 |
TextIO.output (outstream, txt);
|
|
71 |
TextIO.closeOut outstream
|
|
72 |
end;
|
|
73 |
|
|
74 |
fun append path txt =
|
|
75 |
let val outstream = TextIO.openAppend (sysify path) in
|
5009
|
76 |
TextIO.output (outstream, txt);
|
|
77 |
TextIO.closeOut outstream
|
|
78 |
end;
|
|
79 |
|
6182
|
80 |
fun copy inpath outpath = write outpath (read inpath);
|
|
81 |
|
5009
|
82 |
|
6182
|
83 |
(* misc operations *)
|
|
84 |
|
|
85 |
val use = use o sysify;
|
|
86 |
val cd = Library.cd o sysify;
|
|
87 |
val rm = OS.FileSys.remove o sysify;
|
5009
|
88 |
|
|
89 |
|
|
90 |
end;
|