6118
|
1 |
(* Title: Pure/General/file.ML
|
5009
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
8806
|
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE)
|
5009
|
5 |
|
|
6 |
File system operations.
|
|
7 |
*)
|
|
8 |
|
|
9 |
signature FILE =
|
|
10 |
sig
|
6224
|
11 |
val sys_pack_fn: (Path.T -> string) ref
|
|
12 |
val sys_unpack_fn: (string -> Path.T) ref
|
7129
|
13 |
val sysify_path: Path.T -> string
|
9784
|
14 |
val quote_sysify_path: Path.T -> string
|
6224
|
15 |
val rm: Path.T -> unit
|
|
16 |
val cd: Path.T -> unit
|
|
17 |
val pwd: unit -> Path.T
|
|
18 |
val full_path: Path.T -> Path.T
|
6182
|
19 |
val tmp_path: Path.T -> Path.T
|
|
20 |
val read: Path.T -> string
|
|
21 |
val write: Path.T -> string -> unit
|
|
22 |
val append: Path.T -> string -> unit
|
9046
|
23 |
val system_command: string -> unit
|
6182
|
24 |
val copy: Path.T -> Path.T -> unit
|
6224
|
25 |
eqtype info
|
|
26 |
val info: Path.T -> info option
|
|
27 |
val exists: Path.T -> bool
|
6318
|
28 |
val mkdir: Path.T -> unit
|
12116
|
29 |
val use: Path.T -> unit
|
5009
|
30 |
end;
|
|
31 |
|
|
32 |
structure File: FILE =
|
|
33 |
struct
|
|
34 |
|
|
35 |
|
6224
|
36 |
(* system path representations (default: Unix) *)
|
|
37 |
|
|
38 |
val sys_pack_fn = ref Path.pack;
|
|
39 |
val sys_unpack_fn = ref Path.unpack;
|
|
40 |
|
7129
|
41 |
fun sysify_path path = ! sys_pack_fn (Path.expand path);
|
9784
|
42 |
val quote_sysify_path = enclose "'" "'" o sysify_path;
|
7129
|
43 |
fun unsysify_path s = ! sys_unpack_fn s;
|
6224
|
44 |
|
7129
|
45 |
val rm = OS.FileSys.remove o sysify_path;
|
6224
|
46 |
|
|
47 |
|
|
48 |
(* current path *)
|
|
49 |
|
7129
|
50 |
val cd = Library.cd o sysify_path;
|
|
51 |
val pwd = unsysify_path o Library.pwd;
|
6224
|
52 |
|
|
53 |
fun full_path path =
|
|
54 |
if Path.is_absolute path then path
|
|
55 |
else Path.append (pwd ()) path;
|
5009
|
56 |
|
6182
|
57 |
|
|
58 |
(* tmp_path *)
|
|
59 |
|
|
60 |
fun tmp_path path =
|
|
61 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
|
5009
|
62 |
|
|
63 |
|
6224
|
64 |
(* read / write files *)
|
|
65 |
|
|
66 |
fun fail_safe f g x =
|
|
67 |
let val y = f x handle exn => (g x; raise exn)
|
|
68 |
in g x; y end;
|
|
69 |
|
|
70 |
|
|
71 |
fun output txt stream = TextIO.output (stream, txt);
|
6182
|
72 |
|
7129
|
73 |
fun read path = fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (sysify_path path));
|
|
74 |
fun write path txt = fail_safe (output txt) TextIO.closeOut (TextIO.openOut (sysify_path path));
|
7716
|
75 |
fun append path txt =
|
|
76 |
fail_safe (output txt) TextIO.closeOut (TextIO.openAppend (sysify_path path));
|
6218
|
77 |
|
6224
|
78 |
fun copy inpath outpath = write outpath (read inpath);
|
|
79 |
|
|
80 |
|
|
81 |
(* file info *)
|
|
82 |
|
|
83 |
datatype info = Info of string;
|
5009
|
84 |
|
6182
|
85 |
fun info path =
|
7129
|
86 |
let val name = sysify_path path in
|
6182
|
87 |
(case file_info name of
|
|
88 |
"" => None
|
6224
|
89 |
| s => Some (Info s))
|
6182
|
90 |
end;
|
|
91 |
|
|
92 |
val exists = is_some o info;
|
5009
|
93 |
|
|
94 |
|
7716
|
95 |
(* directories *)
|
6318
|
96 |
|
7850
|
97 |
fun system_command cmd =
|
|
98 |
if system cmd <> 0 then error ("System command failed: " ^ cmd)
|
|
99 |
else ();
|
12894
|
100 |
|
9784
|
101 |
fun mkdir path = system_command ("mkdir -p " ^ quote_sysify_path path);
|
6640
|
102 |
|
|
103 |
|
12116
|
104 |
(* use ML files *)
|
6640
|
105 |
|
12116
|
106 |
val use = use o sysify_path;
|
6640
|
107 |
|
5009
|
108 |
end;
|