6118
|
1 |
(* Title: Pure/General/file.ML
|
5009
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
File system operations.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature FILE =
|
|
9 |
sig
|
6224
|
10 |
val sys_pack_fn: (Path.T -> string) ref
|
|
11 |
val sys_unpack_fn: (string -> Path.T) ref
|
7129
|
12 |
val sysify_path: Path.T -> string
|
6224
|
13 |
val rm: Path.T -> unit
|
|
14 |
val cd: Path.T -> unit
|
|
15 |
val pwd: unit -> Path.T
|
|
16 |
val full_path: Path.T -> Path.T
|
6182
|
17 |
val tmp_path: Path.T -> Path.T
|
|
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
|
6224
|
22 |
eqtype info
|
|
23 |
val info: Path.T -> info option
|
|
24 |
val exists: Path.T -> bool
|
6318
|
25 |
val mkdir: Path.T -> unit
|
7716
|
26 |
val copy_all: Path.T -> Path.T -> unit
|
6640
|
27 |
val plain_use: Path.T -> unit
|
|
28 |
val symbol_use: Path.T -> unit
|
5009
|
29 |
end;
|
|
30 |
|
|
31 |
structure File: FILE =
|
|
32 |
struct
|
|
33 |
|
|
34 |
|
6224
|
35 |
(* system path representations (default: Unix) *)
|
|
36 |
|
|
37 |
val sys_pack_fn = ref Path.pack;
|
|
38 |
val sys_unpack_fn = ref Path.unpack;
|
|
39 |
|
7129
|
40 |
fun sysify_path path = ! sys_pack_fn (Path.expand path);
|
|
41 |
fun unsysify_path s = ! sys_unpack_fn s;
|
6224
|
42 |
|
7129
|
43 |
val rm = OS.FileSys.remove o sysify_path;
|
6224
|
44 |
|
|
45 |
|
|
46 |
(* current path *)
|
|
47 |
|
7129
|
48 |
val cd = Library.cd o sysify_path;
|
|
49 |
val pwd = unsysify_path o Library.pwd;
|
6224
|
50 |
|
|
51 |
fun full_path path =
|
|
52 |
if Path.is_absolute path then path
|
|
53 |
else Path.append (pwd ()) path;
|
5009
|
54 |
|
6182
|
55 |
|
|
56 |
(* tmp_path *)
|
|
57 |
|
|
58 |
fun tmp_path path =
|
|
59 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
|
5009
|
60 |
|
|
61 |
|
6224
|
62 |
(* read / write files *)
|
|
63 |
|
|
64 |
fun fail_safe f g x =
|
|
65 |
let val y = f x handle exn => (g x; raise exn)
|
|
66 |
in g x; y end;
|
|
67 |
|
|
68 |
|
|
69 |
fun output txt stream = TextIO.output (stream, txt);
|
6182
|
70 |
|
7129
|
71 |
fun read path = fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (sysify_path path));
|
|
72 |
fun write path txt = fail_safe (output txt) TextIO.closeOut (TextIO.openOut (sysify_path path));
|
7716
|
73 |
fun append path txt =
|
|
74 |
fail_safe (output txt) TextIO.closeOut (TextIO.openAppend (sysify_path path));
|
6218
|
75 |
|
6224
|
76 |
fun copy inpath outpath = write outpath (read inpath);
|
|
77 |
|
|
78 |
|
|
79 |
(* file info *)
|
|
80 |
|
|
81 |
datatype info = Info of string;
|
5009
|
82 |
|
6182
|
83 |
fun info path =
|
7129
|
84 |
let val name = sysify_path path in
|
6182
|
85 |
(case file_info name of
|
|
86 |
"" => None
|
6224
|
87 |
| s => Some (Info s))
|
6182
|
88 |
end;
|
|
89 |
|
|
90 |
val exists = is_some o info;
|
5009
|
91 |
|
|
92 |
|
7716
|
93 |
(* directories *)
|
6318
|
94 |
|
7716
|
95 |
val quote_sysify = enclose "'" "'" o sysify_path;
|
6318
|
96 |
|
7716
|
97 |
fun mkdir path = (execute ("mkdir -p " ^ quote_sysify path); ());
|
6640
|
98 |
|
7716
|
99 |
fun copy_all path1 path2 =
|
|
100 |
(execute ("cp -R " ^ quote_sysify path1 ^ " " ^ quote_sysify path2); ());
|
6640
|
101 |
|
|
102 |
|
|
103 |
(* symbol_use *)
|
|
104 |
|
7129
|
105 |
val plain_use = use o sysify_path;
|
6640
|
106 |
|
|
107 |
(* version of 'use' with input filtering *)
|
|
108 |
|
|
109 |
val symbol_use =
|
|
110 |
if not needs_filtered_use then plain_use (*ML system (wrongly!) accepts non-ASCII*)
|
|
111 |
else
|
|
112 |
fn path =>
|
|
113 |
let
|
|
114 |
val txt = read path;
|
|
115 |
val txt_out = Symbol.input txt;
|
|
116 |
in
|
|
117 |
if txt = txt_out then plain_use path
|
|
118 |
else
|
|
119 |
let val tmp_path = tmp_path path in
|
|
120 |
write tmp_path txt_out;
|
|
121 |
plain_use tmp_path handle exn => (rm tmp_path; raise exn);
|
|
122 |
rm tmp_path
|
|
123 |
end
|
|
124 |
end;
|
|
125 |
|
5009
|
126 |
end;
|