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