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
|
|
12 |
val use: Path.T -> unit
|
|
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
|
5009
|
25 |
end;
|
|
26 |
|
|
27 |
structure File: FILE =
|
|
28 |
struct
|
|
29 |
|
|
30 |
|
6224
|
31 |
(* system path representations (default: Unix) *)
|
|
32 |
|
|
33 |
val sys_pack_fn = ref Path.pack;
|
|
34 |
val sys_unpack_fn = ref Path.unpack;
|
|
35 |
|
|
36 |
fun sysify path = ! sys_pack_fn (Path.expand path);
|
|
37 |
fun unsysify s = ! sys_unpack_fn s;
|
|
38 |
|
6182
|
39 |
|
6224
|
40 |
val use = use o sysify;
|
|
41 |
val rm = OS.FileSys.remove o sysify;
|
|
42 |
|
|
43 |
|
|
44 |
(* current path *)
|
|
45 |
|
|
46 |
val cd = Library.cd o sysify;
|
|
47 |
val pwd = unsysify o Library.pwd;
|
|
48 |
|
|
49 |
fun full_path path =
|
|
50 |
if Path.is_absolute path then path
|
|
51 |
else Path.append (pwd ()) path;
|
5009
|
52 |
|
6182
|
53 |
|
|
54 |
(* tmp_path *)
|
|
55 |
|
|
56 |
fun tmp_path path =
|
|
57 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
|
5009
|
58 |
|
|
59 |
|
6224
|
60 |
(* read / write files *)
|
|
61 |
|
|
62 |
fun fail_safe f g x =
|
|
63 |
let val y = f x handle exn => (g x; raise exn)
|
|
64 |
in g x; y end;
|
|
65 |
|
|
66 |
|
|
67 |
fun output txt stream = TextIO.output (stream, txt);
|
6182
|
68 |
|
6224
|
69 |
fun read path = fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (sysify path));
|
|
70 |
fun write path txt = fail_safe (output txt) TextIO.closeOut (TextIO.openOut (sysify path));
|
|
71 |
fun append path txt = fail_safe (output txt) TextIO.closeOut (TextIO.openAppend (sysify path));
|
6218
|
72 |
|
6224
|
73 |
fun copy inpath outpath = write outpath (read inpath);
|
|
74 |
|
|
75 |
|
|
76 |
(* file info *)
|
|
77 |
|
|
78 |
datatype info = Info of string;
|
5009
|
79 |
|
6182
|
80 |
fun info path =
|
|
81 |
let val name = sysify path in
|
|
82 |
(case file_info name of
|
|
83 |
"" => None
|
6224
|
84 |
| s => Some (Info s))
|
6182
|
85 |
end;
|
|
86 |
|
|
87 |
val exists = is_some o info;
|
5009
|
88 |
|
|
89 |
|
|
90 |
end;
|