author | wenzelm |
Sun, 05 Jun 2005 11:31:24 +0200 | |
changeset 16261 | 28803c418b59 |
parent 16002 | e0557c452138 |
child 16304 | 5e845b75f94f |
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 |
|
16261 | 10 |
val unpack_platform_path_fn: (string -> Path.T) ref |
11 |
val unpack_platform_path: string -> Path.T |
|
12 |
val platform_path_fn: (Path.T -> string) ref |
|
13 |
val platform_path: Path.T -> string |
|
14 |
val shell_path_fn: (Path.T -> string) ref |
|
15 |
val shell_path: Path.T -> string |
|
6224 | 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 |
16261 | 20 |
val isatool: string -> int |
21 |
val system_command: string -> unit |
|
6182 | 22 |
val read: Path.T -> string |
23 |
val write: Path.T -> string -> unit |
|
24 |
val append: Path.T -> string -> unit |
|
25 |
val copy: Path.T -> Path.T -> unit |
|
16261 | 26 |
val copy_dir: Path.T -> Path.T -> unit |
6224 | 27 |
eqtype info |
28 |
val info: Path.T -> info option |
|
29 |
val exists: Path.T -> bool |
|
16261 | 30 |
val rm: Path.T -> unit |
6318 | 31 |
val mkdir: Path.T -> unit |
12116 | 32 |
val use: Path.T -> unit |
5009 | 33 |
end; |
34 |
||
35 |
structure File: FILE = |
|
36 |
struct |
|
37 |
||
16261 | 38 |
(* platform specific path representations *) |
6224 | 39 |
|
16261 | 40 |
val unpack_platform_path_fn = ref Path.unpack; |
41 |
fun unpack_platform_path s = ! unpack_platform_path_fn s; |
|
6224 | 42 |
|
16261 | 43 |
val platform_path_fn = ref (Path.pack o Path.expand); |
44 |
fun platform_path path = ! platform_path_fn path; |
|
6224 | 45 |
|
16261 | 46 |
val shell_path_fn = ref (enclose "'" "'" o Path.pack o Path.expand); |
47 |
fun shell_path path = ! shell_path_fn path; |
|
6224 | 48 |
|
49 |
||
50 |
(* current path *) |
|
51 |
||
16261 | 52 |
val cd = Library.cd o platform_path; |
53 |
val pwd = unpack_platform_path o Library.pwd; |
|
6224 | 54 |
|
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
55 |
fun norm_absolute p = |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
56 |
let |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
57 |
val p' = pwd (); |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
58 |
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
|
59 |
else Path.append (norm (Path.dir p)) (Path.base p); |
16261 | 60 |
val p'' = norm p; |
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
61 |
in (cd p'; p'') end |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
62 |
|
16261 | 63 |
fun full_path path = |
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
64 |
(if Path.is_absolute path then path |
16261 | 65 |
else Path.append (pwd ()) path) |
66 |
|> norm_absolute; |
|
5009 | 67 |
|
6182 | 68 |
|
69 |
(* tmp_path *) |
|
70 |
||
71 |
fun tmp_path path = |
|
72 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path); |
|
5009 | 73 |
|
74 |
||
16261 | 75 |
(* system commands *) |
76 |
||
77 |
fun isatool cmd = system ("\"$ISATOOL\" " ^ cmd); |
|
78 |
||
79 |
fun system_command cmd = |
|
80 |
if system cmd <> 0 then error ("System command failed: " ^ cmd) |
|
81 |
else (); |
|
82 |
||
83 |
||
84 |
(* directory entries *) |
|
85 |
||
86 |
datatype info = Info of string; |
|
87 |
||
88 |
fun info path = |
|
89 |
Option.map (Info o Time.toString) (try OS.FileSys.modTime (platform_path path)); |
|
90 |
||
91 |
val exists = is_some o info; |
|
92 |
||
93 |
val rm = OS.FileSys.remove o platform_path; |
|
94 |
||
95 |
fun mkdir path = system_command ("mkdir -p " ^ shell_path path); |
|
96 |
||
97 |
fun is_dir path = |
|
98 |
if_none (try OS.FileSys.isDir (platform_path path)) false; |
|
99 |
||
100 |
||
6224 | 101 |
(* read / write files *) |
102 |
||
16261 | 103 |
local |
104 |
||
6224 | 105 |
fun fail_safe f g x = |
106 |
let val y = f x handle exn => (g x; raise exn) |
|
107 |
in g x; y end; |
|
108 |
||
109 |
fun output txt stream = TextIO.output (stream, txt); |
|
6182 | 110 |
|
16261 | 111 |
in |
6218 | 112 |
|
16261 | 113 |
fun read path = |
114 |
fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (platform_path path)); |
|
6224 | 115 |
|
16261 | 116 |
fun write path txt = |
117 |
fail_safe (output txt) TextIO.closeOut (TextIO.openOut (platform_path path)); |
|
5009 | 118 |
|
16261 | 119 |
fun append path txt = |
120 |
fail_safe (output txt) TextIO.closeOut (TextIO.openAppend (platform_path path)); |
|
6182 | 121 |
|
16261 | 122 |
end; |
5009 | 123 |
|
16261 | 124 |
fun copy from to = |
125 |
let val target = if is_dir to then Path.append to (Path.base from) else to |
|
126 |
in write target (read from) end; |
|
6318 | 127 |
|
16261 | 128 |
fun copy_dir from to = |
129 |
system_command ("cp -r " ^ shell_path from ^ "/. " ^ shell_path to); |
|
6640 | 130 |
|
131 |
||
12116 | 132 |
(* use ML files *) |
6640 | 133 |
|
16261 | 134 |
val use = use o platform_path; |
6640 | 135 |
|
5009 | 136 |
end; |