| author | wenzelm | 
| Sun, 20 Mar 2011 17:40:45 +0100 | |
| changeset 42003 | 6e45dc518ebb | 
| parent 41944 | b97091ae583a | 
| child 42329 | 782991e4180d | 
| 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  | 
| 
26503
 
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
 
wenzelm 
parents: 
26220 
diff
changeset
 | 
21  | 
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
 | 
22  | 
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
 | 
23  | 
val open_append: (TextIO.outstream -> 'a) -> Path.T -> 'a  | 
| 28028 | 24  | 
val fold_lines: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a  | 
| 6182 | 25  | 
val read: Path.T -> string  | 
26  | 
val write: Path.T -> string -> unit  | 
|
27  | 
val append: Path.T -> string -> unit  | 
|
| 16713 | 28  | 
val write_list: Path.T -> string list -> unit  | 
29  | 
val append_list: Path.T -> string list -> unit  | 
|
| 28028 | 30  | 
val write_buffer: Path.T -> Buffer.T -> unit  | 
| 16603 | 31  | 
val eq: Path.T * Path.T -> bool  | 
| 6182 | 32  | 
val copy: Path.T -> Path.T -> unit  | 
| 5009 | 33  | 
end;  | 
34  | 
||
35  | 
structure File: FILE =  | 
|
36  | 
struct  | 
|
37  | 
||
| 
26503
 
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
 
wenzelm 
parents: 
26220 
diff
changeset
 | 
38  | 
(* system path representations *)  | 
| 6224 | 39  | 
|
| 
26503
 
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
 
wenzelm 
parents: 
26220 
diff
changeset
 | 
40  | 
val platform_path = Path.implode o Path.expand;  | 
| 
26980
 
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
 
wenzelm 
parents: 
26946 
diff
changeset
 | 
41  | 
|
| 40746 | 42  | 
fun shell_quote s =  | 
43  | 
if exists_string (fn c => c = "'") s then  | 
|
44  | 
    error ("Illegal single quote in path specification: " ^ quote s)
 | 
|
45  | 
else enclose "'" "'" s;  | 
|
46  | 
||
| 
26980
 
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
 
wenzelm 
parents: 
26946 
diff
changeset
 | 
47  | 
val shell_path = shell_quote o platform_path;  | 
| 6224 | 48  | 
|
49  | 
||
| 
26503
 
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
 
wenzelm 
parents: 
26220 
diff
changeset
 | 
50  | 
(* current working directory *)  | 
| 6224 | 51  | 
|
| 
23826
 
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
 
wenzelm 
parents: 
22145 
diff
changeset
 | 
52  | 
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
 | 
53  | 
val pwd = Path.explode o pwd;  | 
| 6224 | 54  | 
|
| 
42003
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
55  | 
|
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
56  | 
(* full_path *)  | 
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
57  | 
|
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
58  | 
fun full_path dir path =  | 
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
59  | 
let  | 
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
60  | 
val path' = Path.expand path;  | 
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
61  | 
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
 | 
62  | 
val path'' = Path.append dir path';  | 
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
63  | 
in  | 
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
64  | 
if Path.is_absolute path'' then path''  | 
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
65  | 
else Path.append (pwd ()) path''  | 
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
66  | 
end;  | 
| 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  | 
||
| 
26980
 
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
 
wenzelm 
parents: 
26946 
diff
changeset
 | 
75  | 
(* directory entries *)  | 
| 
 
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
 
wenzelm 
parents: 
26946 
diff
changeset
 | 
76  | 
|
| 
23861
 
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
 
wenzelm 
parents: 
23826 
diff
changeset
 | 
77  | 
val exists = can OS.FileSys.modTime o platform_path;  | 
| 16261 | 78  | 
|
79  | 
val rm = OS.FileSys.remove o platform_path;  | 
|
80  | 
||
| 40785 | 81  | 
fun is_dir path =  | 
82  | 
the_default false (try OS.FileSys.isDir (platform_path path));  | 
|
83  | 
||
| 
42003
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
84  | 
fun check_dir path =  | 
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
85  | 
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
 | 
86  | 
  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
 | 
87  | 
|
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
88  | 
fun check_file path =  | 
| 
 
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
 
wenzelm 
parents: 
41944 
diff
changeset
 | 
89  | 
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
 | 
90  | 
  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
 | 
91  | 
|
| 16261 | 92  | 
|
| 28028 | 93  | 
(* open files *)  | 
| 6224 | 94  | 
|
| 16261 | 95  | 
local  | 
96  | 
||
| 
26503
 
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
 
wenzelm 
parents: 
26220 
diff
changeset
 | 
97  | 
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
 | 
98  | 
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
 | 
99  | 
in Exn.release (Exn.capture f file before close_file file) end;  | 
| 6224 | 100  | 
|
| 16261 | 101  | 
in  | 
| 6218 | 102  | 
|
| 
26503
 
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
 
wenzelm 
parents: 
26220 
diff
changeset
 | 
103  | 
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
 | 
104  | 
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
 | 
105  | 
fun open_append f = with_file TextIO.openAppend TextIO.closeOut f o platform_path;  | 
| 6224 | 106  | 
|
| 28028 | 107  | 
end;  | 
108  | 
||
109  | 
||
110  | 
(* input *)  | 
|
111  | 
||
112  | 
(*scalable iterator -- avoid size limit of TextIO.inputAll, and overhead of many TextIO.inputLine*)  | 
|
113  | 
fun fold_lines f path a = open_input (fn file =>  | 
|
114  | 
let  | 
|
| 
28510
 
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
 
wenzelm 
parents: 
28500 
diff
changeset
 | 
115  | 
fun read buf x =  | 
| 
 
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
 
wenzelm 
parents: 
28500 
diff
changeset
 | 
116  | 
(case TextIO.input file of  | 
| 
 
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
 
wenzelm 
parents: 
28500 
diff
changeset
 | 
117  | 
"" => (case Buffer.content buf of "" => x | line => f line x)  | 
| 
 
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
 
wenzelm 
parents: 
28500 
diff
changeset
 | 
118  | 
| input =>  | 
| 
 
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
 
wenzelm 
parents: 
28500 
diff
changeset
 | 
119  | 
(case String.fields (fn c => c = #"\n") input of  | 
| 
 
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
 
wenzelm 
parents: 
28500 
diff
changeset
 | 
120  | 
[rest] => read (Buffer.add rest buf) x  | 
| 
 
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
 
wenzelm 
parents: 
28500 
diff
changeset
 | 
121  | 
| 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
 | 
122  | 
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
 | 
123  | 
| read_lines (line :: more) x = read_lines more (f line x);  | 
| 28028 | 124  | 
in read Buffer.empty a end) path;  | 
125  | 
||
| 
26503
 
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
 
wenzelm 
parents: 
26220 
diff
changeset
 | 
126  | 
val read = open_input TextIO.inputAll;  | 
| 5009 | 127  | 
|
| 28028 | 128  | 
|
129  | 
(* output *)  | 
|
130  | 
||
131  | 
fun output txts file = List.app (fn txt => TextIO.output (file, txt)) txts;  | 
|
132  | 
||
| 
26503
 
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
 
wenzelm 
parents: 
26220 
diff
changeset
 | 
133  | 
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
 | 
134  | 
fun append_list path txts = open_append (output txts) path;  | 
| 16713 | 135  | 
|
136  | 
fun write path txt = write_list path [txt];  | 
|
137  | 
fun append path txt = append_list path [txt];  | 
|
| 6182 | 138  | 
|
| 28028 | 139  | 
fun write_buffer path buf = open_output (Buffer.output buf) path;  | 
140  | 
||
141  | 
||
142  | 
(* copy *)  | 
|
| 5009 | 143  | 
|
| 16603 | 144  | 
fun eq paths =  | 
145  | 
(case try (pairself (OS.FileSys.fileId o platform_path)) paths of  | 
|
| 26656 | 146  | 
SOME ids => is_equal (OS.FileSys.compare ids)  | 
| 16603 | 147  | 
| NONE => false);  | 
148  | 
||
| 21962 | 149  | 
fun copy src dst =  | 
150  | 
if eq (src, dst) then ()  | 
|
151  | 
else  | 
|
152  | 
let val target = if is_dir dst then Path.append dst (Path.base src) else dst  | 
|
153  | 
in write target (read src) end;  | 
|
| 6318 | 154  | 
|
| 5009 | 155  | 
end;  |