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