6118
|
1 |
(* Title: Pure/General/file.ML
|
5009
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
File system operations.
|
6182
|
6 |
|
|
7 |
TODO:
|
|
8 |
- close in case of error (!?);
|
5009
|
9 |
*)
|
|
10 |
|
|
11 |
signature FILE =
|
|
12 |
sig
|
6182
|
13 |
val sys_path_fn: (Path.T -> string) ref
|
|
14 |
val tmp_path: Path.T -> Path.T
|
|
15 |
eqtype info
|
|
16 |
val info: Path.T -> info option
|
6218
|
17 |
val join_info: info -> info -> info
|
6182
|
18 |
val exists: Path.T -> bool
|
|
19 |
val read: Path.T -> string
|
|
20 |
val write: Path.T -> string -> unit
|
|
21 |
val append: Path.T -> string -> unit
|
|
22 |
val copy: Path.T -> Path.T -> unit
|
|
23 |
val use: Path.T -> unit
|
|
24 |
val cd: Path.T -> unit
|
|
25 |
val rm: Path.T -> unit
|
5009
|
26 |
end;
|
|
27 |
|
|
28 |
structure File: FILE =
|
|
29 |
struct
|
|
30 |
|
|
31 |
|
6182
|
32 |
(* sys_path (default for Unix) *)
|
|
33 |
|
|
34 |
val sys_path_fn = ref Path.pack;
|
|
35 |
fun sysify path = ! sys_path_fn (Path.expand path);
|
5009
|
36 |
|
6182
|
37 |
|
|
38 |
(* tmp_path *)
|
|
39 |
|
|
40 |
fun tmp_path path =
|
|
41 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
|
5009
|
42 |
|
|
43 |
|
6182
|
44 |
(* info / exists *)
|
|
45 |
|
6218
|
46 |
datatype info = Info of string list;
|
|
47 |
|
|
48 |
fun join_info (Info xs) (Info ys) = Info (xs @ ys);
|
5009
|
49 |
|
6182
|
50 |
fun info path =
|
|
51 |
let val name = sysify path in
|
|
52 |
(case file_info name of
|
|
53 |
"" => None
|
6218
|
54 |
| s => Some (Info [name ^ ": " ^ s])) (* FIXME include full path (!?) *)
|
6182
|
55 |
end;
|
|
56 |
|
|
57 |
val exists = is_some o info;
|
5009
|
58 |
|
|
59 |
|
|
60 |
(* read / write files *)
|
|
61 |
|
6182
|
62 |
fun read path =
|
5009
|
63 |
let
|
6182
|
64 |
val instream = TextIO.openIn (sysify path);
|
5009
|
65 |
val intext = TextIO.inputAll instream;
|
|
66 |
in
|
|
67 |
TextIO.closeIn instream;
|
|
68 |
intext
|
|
69 |
end;
|
|
70 |
|
6182
|
71 |
fun write path txt =
|
|
72 |
let val outstream = TextIO.openOut (sysify path) in
|
|
73 |
TextIO.output (outstream, txt);
|
|
74 |
TextIO.closeOut outstream
|
|
75 |
end;
|
|
76 |
|
|
77 |
fun append path txt =
|
|
78 |
let val outstream = TextIO.openAppend (sysify path) in
|
5009
|
79 |
TextIO.output (outstream, txt);
|
|
80 |
TextIO.closeOut outstream
|
|
81 |
end;
|
|
82 |
|
6182
|
83 |
fun copy inpath outpath = write outpath (read inpath);
|
|
84 |
|
5009
|
85 |
|
6182
|
86 |
(* misc operations *)
|
|
87 |
|
|
88 |
val use = use o sysify;
|
|
89 |
val cd = Library.cd o sysify;
|
|
90 |
val rm = OS.FileSys.remove o sysify;
|
5009
|
91 |
|
|
92 |
|
|
93 |
end;
|