author | wenzelm |
Thu, 02 Jun 2005 18:29:52 +0200 | |
changeset 16192 | 733267a60e32 |
parent 16002 | e0557c452138 |
child 16261 | 28803c418b59 |
permissions | -rw-r--r-- |
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 |
9784 | 13 |
val quote_sysify_path: Path.T -> string |
6224 | 14 |
val rm: Path.T -> unit |
15 |
val cd: Path.T -> unit |
|
16 |
val pwd: unit -> Path.T |
|
17 |
val full_path: Path.T -> Path.T |
|
6182 | 18 |
val tmp_path: Path.T -> Path.T |
19 |
val read: Path.T -> string |
|
20 |
val write: Path.T -> string -> unit |
|
21 |
val append: Path.T -> string -> unit |
|
9046 | 22 |
val system_command: string -> unit |
6182 | 23 |
val copy: Path.T -> Path.T -> unit |
6224 | 24 |
eqtype info |
25 |
val info: Path.T -> info option |
|
26 |
val exists: Path.T -> bool |
|
6318 | 27 |
val mkdir: Path.T -> unit |
12116 | 28 |
val 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); |
9784 | 41 |
val quote_sysify_path = enclose "'" "'" o sysify_path; |
7129 | 42 |
fun unsysify_path s = ! sys_unpack_fn s; |
6224 | 43 |
|
7129 | 44 |
val rm = OS.FileSys.remove o sysify_path; |
6224 | 45 |
|
46 |
||
47 |
(* current path *) |
|
48 |
||
7129 | 49 |
val cd = Library.cd o sysify_path; |
50 |
val pwd = unsysify_path o Library.pwd; |
|
6224 | 51 |
|
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
52 |
fun norm_absolute p = |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
53 |
let |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
54 |
val p' = pwd (); |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
55 |
fun norm p = if can cd p then pwd () |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
56 |
else Path.append (norm (Path.dir p)) (Path.base p); |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
57 |
val p'' = norm p |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
58 |
in (cd p'; p'') end |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
59 |
|
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
60 |
fun full_path path = norm_absolute |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
61 |
(if Path.is_absolute path then path |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
62 |
else Path.append (pwd ()) path); |
5009 | 63 |
|
6182 | 64 |
|
65 |
(* tmp_path *) |
|
66 |
||
67 |
fun tmp_path path = |
|
68 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path); |
|
5009 | 69 |
|
70 |
||
6224 | 71 |
(* read / write files *) |
72 |
||
73 |
fun fail_safe f g x = |
|
74 |
let val y = f x handle exn => (g x; raise exn) |
|
75 |
in g x; y end; |
|
76 |
||
77 |
||
78 |
fun output txt stream = TextIO.output (stream, txt); |
|
6182 | 79 |
|
7129 | 80 |
fun read path = fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (sysify_path path)); |
81 |
fun write path txt = fail_safe (output txt) TextIO.closeOut (TextIO.openOut (sysify_path path)); |
|
7716 | 82 |
fun append path txt = |
83 |
fail_safe (output txt) TextIO.closeOut (TextIO.openAppend (sysify_path path)); |
|
6218 | 84 |
|
6224 | 85 |
fun copy inpath outpath = write outpath (read inpath); |
86 |
||
87 |
||
88 |
(* file info *) |
|
89 |
||
90 |
datatype info = Info of string; |
|
5009 | 91 |
|
6182 | 92 |
fun info path = |
7129 | 93 |
let val name = sysify_path path in |
6182 | 94 |
(case file_info name of |
15531 | 95 |
"" => NONE |
96 |
| s => SOME (Info s)) |
|
6182 | 97 |
end; |
98 |
||
16002 | 99 |
val exists = is_some o info; |
5009 | 100 |
|
101 |
||
7716 | 102 |
(* directories *) |
6318 | 103 |
|
7850 | 104 |
fun system_command cmd = |
105 |
if system cmd <> 0 then error ("System command failed: " ^ cmd) |
|
106 |
else (); |
|
12894 | 107 |
|
9784 | 108 |
fun mkdir path = system_command ("mkdir -p " ^ quote_sysify_path path); |
6640 | 109 |
|
110 |
||
12116 | 111 |
(* use ML files *) |
6640 | 112 |
|
12116 | 113 |
val use = use o sysify_path; |
6640 | 114 |
|
5009 | 115 |
end; |