author | wenzelm |
Sun, 30 Jan 2011 13:02:18 +0100 | |
changeset 41648 | 6d736d983d5c |
parent 40785 | c755df0f7062 |
child 41944 | b97091ae583a |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/file.ML |
5009 | 2 |
Author: Markus Wenzel, TU Muenchen |
3 |
||
4 |
File system operations. |
|
5 |
*) |
|
6 |
||
7 |
signature FILE = |
|
8 |
sig |
|
16261 | 9 |
val platform_path: Path.T -> string |
32943 | 10 |
val shell_quote: string -> string |
16261 | 11 |
val shell_path: Path.T -> string |
6224 | 12 |
val cd: Path.T -> unit |
13 |
val pwd: unit -> Path.T |
|
14 |
val full_path: Path.T -> Path.T |
|
6182 | 15 |
val tmp_path: Path.T -> Path.T |
17826 | 16 |
val exists: Path.T -> bool |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
17 |
val check: Path.T -> unit |
17826 | 18 |
val rm: Path.T -> unit |
40785 | 19 |
val is_dir: Path.T -> bool |
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
20 |
val open_input: (TextIO.instream -> 'a) -> Path.T -> 'a |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
21 |
val open_output: (TextIO.outstream -> 'a) -> Path.T -> 'a |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
22 |
val open_append: (TextIO.outstream -> 'a) -> Path.T -> 'a |
28028 | 23 |
val fold_lines: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a |
6182 | 24 |
val read: Path.T -> string |
25 |
val write: Path.T -> string -> unit |
|
26 |
val append: Path.T -> string -> unit |
|
16713 | 27 |
val write_list: Path.T -> string list -> unit |
28 |
val append_list: Path.T -> string list -> unit |
|
28028 | 29 |
val write_buffer: Path.T -> Buffer.T -> unit |
16603 | 30 |
val eq: Path.T * Path.T -> bool |
6182 | 31 |
val copy: Path.T -> Path.T -> unit |
5009 | 32 |
end; |
33 |
||
34 |
structure File: FILE = |
|
35 |
struct |
|
36 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
37 |
(* system path representations *) |
6224 | 38 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
39 |
val platform_path = Path.implode o Path.expand; |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
40 |
|
40746 | 41 |
fun shell_quote s = |
42 |
if exists_string (fn c => c = "'") s then |
|
43 |
error ("Illegal single quote in path specification: " ^ quote s) |
|
44 |
else enclose "'" "'" s; |
|
45 |
||
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
46 |
val shell_path = shell_quote o platform_path; |
6224 | 47 |
|
48 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
49 |
(* current working directory *) |
6224 | 50 |
|
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22145
diff
changeset
|
51 |
val cd = cd o platform_path; |
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
52 |
val pwd = Path.explode o pwd; |
6224 | 53 |
|
16261 | 54 |
fun full_path path = |
26946
0b6eff8c088d
removed norm_absolute (not thread safe; chdir does not guarantee normalization anyway);
wenzelm
parents:
26656
diff
changeset
|
55 |
if Path.is_absolute path then path |
0b6eff8c088d
removed norm_absolute (not thread safe; chdir does not guarantee normalization anyway);
wenzelm
parents:
26656
diff
changeset
|
56 |
else Path.append (pwd ()) path; |
5009 | 57 |
|
6182 | 58 |
|
59 |
(* tmp_path *) |
|
60 |
||
61 |
fun tmp_path path = |
|
62 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path); |
|
5009 | 63 |
|
64 |
||
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
65 |
(* directory entries *) |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
66 |
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
67 |
val exists = can OS.FileSys.modTime o platform_path; |
16261 | 68 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
69 |
fun check path = |
17826 | 70 |
if exists path then () |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
71 |
else error ("No such file or directory: " ^ quote (Path.implode path)); |
17826 | 72 |
|
16261 | 73 |
val rm = OS.FileSys.remove o platform_path; |
74 |
||
40785 | 75 |
fun is_dir path = |
76 |
the_default false (try OS.FileSys.isDir (platform_path path)); |
|
77 |
||
16261 | 78 |
|
28028 | 79 |
(* open files *) |
6224 | 80 |
|
16261 | 81 |
local |
82 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
83 |
fun with_file open_file close_file f path = |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
84 |
let val file = open_file path |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
85 |
in Exn.release (Exn.capture f file before close_file file) end; |
6224 | 86 |
|
16261 | 87 |
in |
6218 | 88 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
89 |
fun open_input f = with_file TextIO.openIn TextIO.closeIn f o platform_path; |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
90 |
fun open_output f = with_file TextIO.openOut TextIO.closeOut f o platform_path; |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
91 |
fun open_append f = with_file TextIO.openAppend TextIO.closeOut f o platform_path; |
6224 | 92 |
|
28028 | 93 |
end; |
94 |
||
95 |
||
96 |
(* input *) |
|
97 |
||
98 |
(*scalable iterator -- avoid size limit of TextIO.inputAll, and overhead of many TextIO.inputLine*) |
|
99 |
fun fold_lines f path a = open_input (fn file => |
|
100 |
let |
|
28510
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
101 |
fun read buf x = |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
102 |
(case TextIO.input file of |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
103 |
"" => (case Buffer.content buf of "" => x | line => f line x) |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
104 |
| input => |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
105 |
(case String.fields (fn c => c = #"\n") input of |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
106 |
[rest] => read (Buffer.add rest buf) x |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
107 |
| line :: more => read_lines more (f (Buffer.content (Buffer.add line buf)) x))) |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
108 |
and read_lines [rest] x = read (Buffer.add rest Buffer.empty) x |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
109 |
| read_lines (line :: more) x = read_lines more (f line x); |
28028 | 110 |
in read Buffer.empty a end) path; |
111 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
112 |
val read = open_input TextIO.inputAll; |
5009 | 113 |
|
28028 | 114 |
|
115 |
(* output *) |
|
116 |
||
117 |
fun output txts file = List.app (fn txt => TextIO.output (file, txt)) txts; |
|
118 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
119 |
fun write_list path txts = open_output (output txts) path; |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
120 |
fun append_list path txts = open_append (output txts) path; |
16713 | 121 |
|
122 |
fun write path txt = write_list path [txt]; |
|
123 |
fun append path txt = append_list path [txt]; |
|
6182 | 124 |
|
28028 | 125 |
fun write_buffer path buf = open_output (Buffer.output buf) path; |
126 |
||
127 |
||
128 |
(* copy *) |
|
5009 | 129 |
|
16603 | 130 |
fun eq paths = |
131 |
(case try (pairself (OS.FileSys.fileId o platform_path)) paths of |
|
26656 | 132 |
SOME ids => is_equal (OS.FileSys.compare ids) |
16603 | 133 |
| NONE => false); |
134 |
||
21962 | 135 |
fun copy src dst = |
136 |
if eq (src, dst) then () |
|
137 |
else |
|
138 |
let val target = if is_dir dst then Path.append dst (Path.base src) else dst |
|
139 |
in write target (read src) end; |
|
6318 | 140 |
|
5009 | 141 |
end; |