author | wenzelm |
Sun, 30 Nov 2014 13:15:04 +0100 | |
changeset 59066 | 45ab32a542fe |
parent 59058 | a78612c67ec0 |
child 60970 | e08d868ceca9 |
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 |
|
42003
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
14 |
val full_path: Path.T -> Path.T -> Path.T |
6182 | 15 |
val tmp_path: Path.T -> Path.T |
17826 | 16 |
val exists: Path.T -> bool |
17 |
val rm: Path.T -> unit |
|
40785 | 18 |
val is_dir: Path.T -> bool |
42003
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
19 |
val check_dir: Path.T -> Path.T |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
20 |
val check_file: Path.T -> Path.T |
43848 | 21 |
val open_dir: (OS.FileSys.dirstream -> 'a) -> Path.T -> 'a |
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
22 |
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
|
23 |
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
|
24 |
val open_append: (TextIO.outstream -> 'a) -> Path.T -> 'a |
43848 | 25 |
val fold_dir: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a |
26 |
val read_dir: Path.T -> string list |
|
28028 | 27 |
val fold_lines: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a |
43845
d89353d17f54
added File.fold_pages for streaming of large files;
wenzelm
parents:
43616
diff
changeset
|
28 |
val fold_pages: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a |
43848 | 29 |
val read_lines: Path.T -> string list |
44879 | 30 |
val read_pages: Path.T -> string list |
6182 | 31 |
val read: Path.T -> string |
32 |
val write: Path.T -> string -> unit |
|
33 |
val append: Path.T -> string -> unit |
|
16713 | 34 |
val write_list: Path.T -> string list -> unit |
35 |
val append_list: Path.T -> string list -> unit |
|
28028 | 36 |
val write_buffer: Path.T -> Buffer.T -> unit |
16603 | 37 |
val eq: Path.T * Path.T -> bool |
5009 | 38 |
end; |
39 |
||
40 |
structure File: FILE = |
|
41 |
struct |
|
42 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
43 |
(* system path representations *) |
6224 | 44 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
45 |
val platform_path = Path.implode o Path.expand; |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
46 |
|
48446 | 47 |
val shell_quote = enclose "'" "'"; |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
48 |
val shell_path = shell_quote o platform_path; |
6224 | 49 |
|
50 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
51 |
(* current working directory *) |
6224 | 52 |
|
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22145
diff
changeset
|
53 |
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
|
54 |
val pwd = Path.explode o pwd; |
6224 | 55 |
|
42003
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
56 |
|
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
57 |
(* full_path *) |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
58 |
|
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
59 |
fun full_path dir path = |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
60 |
let |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
61 |
val path' = Path.expand path; |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
62 |
val _ = Path.is_current path' andalso error "Bad file specification"; |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
63 |
val path'' = Path.append dir path'; |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
64 |
in |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
65 |
if Path.is_absolute path'' then path'' |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
66 |
else Path.append (pwd ()) path'' |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
67 |
end; |
5009 | 68 |
|
6182 | 69 |
|
70 |
(* tmp_path *) |
|
71 |
||
72 |
fun tmp_path path = |
|
73 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path); |
|
5009 | 74 |
|
75 |
||
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
76 |
(* directory entries *) |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
77 |
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
78 |
val exists = can OS.FileSys.modTime o platform_path; |
16261 | 79 |
|
80 |
val rm = OS.FileSys.remove o platform_path; |
|
81 |
||
40785 | 82 |
fun is_dir path = |
83 |
the_default false (try OS.FileSys.isDir (platform_path path)); |
|
84 |
||
42003
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
85 |
fun check_dir path = |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
86 |
if exists path andalso is_dir path then path |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
87 |
else error ("No such directory: " ^ Path.print path); |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
88 |
|
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
89 |
fun check_file path = |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
90 |
if exists path andalso not (is_dir path) then path |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
91 |
else error ("No such file: " ^ Path.print path); |
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
92 |
|
16261 | 93 |
|
43848 | 94 |
(* open streams *) |
6224 | 95 |
|
16261 | 96 |
local |
97 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
98 |
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
|
99 |
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
|
100 |
in Exn.release (Exn.capture f file before close_file file) end; |
6224 | 101 |
|
16261 | 102 |
in |
6218 | 103 |
|
43848 | 104 |
fun open_dir f = with_file OS.FileSys.openDir OS.FileSys.closeDir f o platform_path; |
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
105 |
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
|
106 |
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
|
107 |
fun open_append f = with_file TextIO.openAppend TextIO.closeOut f o platform_path; |
6224 | 108 |
|
28028 | 109 |
end; |
110 |
||
111 |
||
43848 | 112 |
(* directory content *) |
113 |
||
114 |
fun fold_dir f path a = open_dir (fn stream => |
|
115 |
let |
|
116 |
fun read x = |
|
117 |
(case OS.FileSys.readDir stream of |
|
118 |
NONE => x |
|
119 |
| SOME entry => read (f entry x)); |
|
120 |
in read a end) path; |
|
121 |
||
122 |
fun read_dir path = rev (fold_dir cons path []); |
|
123 |
||
124 |
||
28028 | 125 |
(* input *) |
126 |
||
44879 | 127 |
(* |
128 |
scalable iterator: |
|
129 |
. avoid size limit of TextIO.inputAll and overhead of many TextIO.inputLine |
|
48911
5debc3e4fa81
tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents:
48446
diff
changeset
|
130 |
. optional terminator at end-of-input |
44879 | 131 |
*) |
43845
d89353d17f54
added File.fold_pages for streaming of large files;
wenzelm
parents:
43616
diff
changeset
|
132 |
fun fold_chunks terminator f path a = open_input (fn file => |
28028 | 133 |
let |
28510
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
134 |
fun read buf x = |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
135 |
(case TextIO.input file of |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
136 |
"" => (case Buffer.content buf of "" => x | line => f line x) |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
137 |
| input => |
43845
d89353d17f54
added File.fold_pages for streaming of large files;
wenzelm
parents:
43616
diff
changeset
|
138 |
(case String.fields (fn c => c = terminator) input of |
28510
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
139 |
[rest] => read (Buffer.add rest buf) x |
43616
9e237a9dc1fd
reverted 782991e4180d: fold_fields was never used
noschinl
parents:
42329
diff
changeset
|
140 |
| line :: more => read_lines more (f (Buffer.content (Buffer.add line buf)) x))) |
9e237a9dc1fd
reverted 782991e4180d: fold_fields was never used
noschinl
parents:
42329
diff
changeset
|
141 |
and read_lines [rest] x = read (Buffer.add rest Buffer.empty) x |
9e237a9dc1fd
reverted 782991e4180d: fold_fields was never used
noschinl
parents:
42329
diff
changeset
|
142 |
| read_lines (line :: more) x = read_lines more (f line x); |
28028 | 143 |
in read Buffer.empty a end) path; |
144 |
||
43845
d89353d17f54
added File.fold_pages for streaming of large files;
wenzelm
parents:
43616
diff
changeset
|
145 |
fun fold_lines f = fold_chunks #"\n" f; |
d89353d17f54
added File.fold_pages for streaming of large files;
wenzelm
parents:
43616
diff
changeset
|
146 |
fun fold_pages f = fold_chunks #"\f" f; |
d89353d17f54
added File.fold_pages for streaming of large files;
wenzelm
parents:
43616
diff
changeset
|
147 |
|
43848 | 148 |
fun read_lines path = rev (fold_lines cons path []); |
44879 | 149 |
fun read_pages path = rev (fold_pages cons path []); |
43848 | 150 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
151 |
val read = open_input TextIO.inputAll; |
5009 | 152 |
|
28028 | 153 |
|
154 |
(* output *) |
|
155 |
||
156 |
fun output txts file = List.app (fn txt => TextIO.output (file, txt)) txts; |
|
157 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
158 |
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
|
159 |
fun append_list path txts = open_append (output txts) path; |
16713 | 160 |
|
161 |
fun write path txt = write_list path [txt]; |
|
162 |
fun append path txt = append_list path [txt]; |
|
6182 | 163 |
|
28028 | 164 |
fun write_buffer path buf = open_output (Buffer.output buf) path; |
165 |
||
166 |
||
56533
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
48911
diff
changeset
|
167 |
(* eq *) |
5009 | 168 |
|
16603 | 169 |
fun eq paths = |
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
56533
diff
changeset
|
170 |
(case try (apply2 (OS.FileSys.fileId o platform_path)) paths of |
26656 | 171 |
SOME ids => is_equal (OS.FileSys.compare ids) |
16603 | 172 |
| NONE => false); |
173 |
||
5009 | 174 |
end; |