| author | wenzelm |
| Wed, 02 Sep 2015 22:14:44 +0200 | |
| changeset 61095 | 50e793295ce1 |
| parent 60992 | 89effcb342df |
| child 61291 | e00e1bf23d03 |
| permissions | -rw-r--r-- |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Pure/General/file.scala |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
3 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
4 |
File system operations. |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
6 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
8 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
9 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
10 |
import java.io.{BufferedWriter, OutputStreamWriter, FileOutputStream, BufferedOutputStream,
|
| 51504 | 11 |
OutputStream, InputStream, FileInputStream, BufferedInputStream, BufferedReader, |
12 |
InputStreamReader, File => JFile, IOException} |
|
| 60992 | 13 |
import java.net.{URL, URLDecoder, MalformedURLException}
|
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
14 |
import java.util.zip.{GZIPInputStream, GZIPOutputStream}
|
| 60992 | 15 |
import java.util.regex.Pattern |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
16 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
17 |
import scala.collection.mutable |
| 60992 | 18 |
import scala.util.matching.Regex |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
19 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
20 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
21 |
object File |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
22 |
{
|
| 60992 | 23 |
/* standard path (Cygwin or Posix) */ |
| 60988 | 24 |
|
25 |
def standard_path(path: Path): String = path.expand.implode |
|
26 |
||
| 60992 | 27 |
def standard_path(platform_path: String): String = |
28 |
if (Platform.is_windows) {
|
|
29 |
val Platform_Root = new Regex("(?i)" +
|
|
30 |
Pattern.quote(Isabelle_System.get_cygwin_root()) + """(?:\\+|\z)(.*)""") |
|
31 |
val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
|
|
32 |
||
33 |
platform_path.replace('/', '\\') match {
|
|
34 |
case Platform_Root(rest) => "/" + rest.replace('\\', '/')
|
|
35 |
case Drive(letter, rest) => |
|
36 |
"/cygdrive/" + Word.lowercase(letter) + |
|
37 |
(if (rest == "") "" else "/" + rest.replace('\\', '/'))
|
|
38 |
case path => path.replace('\\', '/')
|
|
39 |
} |
|
40 |
} |
|
41 |
else platform_path |
|
42 |
||
43 |
def standard_path(file: JFile): String = standard_path(file.getPath) |
|
44 |
||
45 |
def standard_url(name: String): String = |
|
46 |
try {
|
|
47 |
val url = new URL(name) |
|
48 |
if (url.getProtocol == "file") |
|
49 |
standard_path(URLDecoder.decode(url.getPath, UTF8.charset_name)) |
|
50 |
else name |
|
51 |
} |
|
52 |
catch { case _: MalformedURLException => standard_path(name) }
|
|
53 |
||
54 |
||
55 |
/* platform path (Windows or Posix) */ |
|
56 |
||
57 |
private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)")
|
|
58 |
private val Named_Root = new Regex("//+([^/]*)(.*)")
|
|
59 |
||
60 |
def platform_path(standard_path: String): String = |
|
61 |
if (Platform.is_windows) {
|
|
62 |
val result_path = new StringBuilder |
|
63 |
val rest = |
|
64 |
standard_path match {
|
|
65 |
case Cygdrive(drive, rest) => |
|
66 |
result_path ++= (Word.uppercase(drive) + ":" + JFile.separator) |
|
67 |
rest |
|
68 |
case Named_Root(root, rest) => |
|
69 |
result_path ++= JFile.separator |
|
70 |
result_path ++= JFile.separator |
|
71 |
result_path ++= root |
|
72 |
rest |
|
73 |
case path if path.startsWith("/") =>
|
|
74 |
result_path ++= Isabelle_System.get_cygwin_root() |
|
75 |
path |
|
76 |
case path => path |
|
77 |
} |
|
78 |
for (p <- space_explode('/', rest) if p != "") {
|
|
79 |
val len = result_path.length |
|
80 |
if (len > 0 && result_path(len - 1) != JFile.separatorChar) |
|
81 |
result_path += JFile.separatorChar |
|
82 |
result_path ++= p |
|
83 |
} |
|
84 |
result_path.toString |
|
85 |
} |
|
86 |
else standard_path |
|
87 |
||
88 |
def platform_path(path: Path): String = platform_path(standard_path(path)) |
|
| 60988 | 89 |
def platform_file(path: Path): JFile = new JFile(platform_path(path)) |
90 |
||
| 60992 | 91 |
def platform_url(raw_path: Path): String = |
| 60988 | 92 |
{
|
93 |
val path = raw_path.expand |
|
94 |
require(path.is_absolute) |
|
95 |
val s = platform_path(path).replaceAll(" ", "%20")
|
|
96 |
if (!Platform.is_windows) "file://" + s |
|
97 |
else if (s.startsWith("\\\\")) "file:" + s.replace('\\', '/')
|
|
98 |
else "file:///" + s.replace('\\', '/')
|
|
99 |
} |
|
100 |
||
| 60992 | 101 |
|
102 |
/* shell path (bash) */ |
|
103 |
||
| 60988 | 104 |
def shell_path(path: Path): String = "'" + standard_path(path) + "'" |
| 60992 | 105 |
def shell_path(file: JFile): String = "'" + standard_path(file) + "'" |
| 60988 | 106 |
|
107 |
||
|
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
108 |
/* directory content */ |
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
109 |
|
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
110 |
def read_dir(dir: Path): List[String] = |
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
111 |
{
|
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
112 |
if (!dir.is_dir) error("Bad directory: " + dir.toString)
|
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
113 |
val files = dir.file.listFiles |
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
114 |
if (files == null) Nil |
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
115 |
else files.toList.map(_.getName) |
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
116 |
} |
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
117 |
|
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
118 |
def find_files(dir: Path): Stream[Path] = |
|
56783
afaec818fcfd
ignore malformed file names outright, e.g. .class files with dollar;
wenzelm
parents:
56428
diff
changeset
|
119 |
read_dir(dir).toStream.map(name => |
|
afaec818fcfd
ignore malformed file names outright, e.g. .class files with dollar;
wenzelm
parents:
56428
diff
changeset
|
120 |
if (Path.is_wellformed(name)) {
|
|
afaec818fcfd
ignore malformed file names outright, e.g. .class files with dollar;
wenzelm
parents:
56428
diff
changeset
|
121 |
val path = dir + Path.basic(name) |
|
afaec818fcfd
ignore malformed file names outright, e.g. .class files with dollar;
wenzelm
parents:
56428
diff
changeset
|
122 |
path #:: (if (path.is_dir) find_files(path) else Stream.empty) |
|
afaec818fcfd
ignore malformed file names outright, e.g. .class files with dollar;
wenzelm
parents:
56428
diff
changeset
|
123 |
} |
|
afaec818fcfd
ignore malformed file names outright, e.g. .class files with dollar;
wenzelm
parents:
56428
diff
changeset
|
124 |
else Stream.empty).flatten |
|
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
125 |
|
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
126 |
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
127 |
/* read */ |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
128 |
|
| 54440 | 129 |
def read(file: JFile): String = Bytes.read(file).toString |
|
48913
f686cb016c0c
more direct File.read_bytes -- avoid cumulative copying of StringBuilder;
wenzelm
parents:
48613
diff
changeset
|
130 |
def read(path: Path): String = read(path.file) |
|
f686cb016c0c
more direct File.read_bytes -- avoid cumulative copying of StringBuilder;
wenzelm
parents:
48613
diff
changeset
|
131 |
|
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
132 |
|
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
133 |
def read_stream(reader: BufferedReader): String = |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
134 |
{
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
135 |
val output = new StringBuilder(100) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
136 |
var c = -1 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
137 |
while ({ c = reader.read; c != -1 }) output += c.toChar
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
138 |
reader.close |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
139 |
output.toString |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
140 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
141 |
|
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
142 |
def read_stream(stream: InputStream): String = |
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
143 |
read_stream(new BufferedReader(new InputStreamReader(stream, UTF8.charset))) |
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
144 |
|
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
145 |
def read_gzip(file: JFile): String = |
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
146 |
read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)))) |
| 51504 | 147 |
|
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
148 |
def read_gzip(path: Path): String = read_gzip(path.file) |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
149 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
150 |
|
| 50845 | 151 |
/* read lines */ |
152 |
||
153 |
def read_lines(reader: BufferedReader, progress: String => Unit): List[String] = |
|
154 |
{
|
|
155 |
val result = new mutable.ListBuffer[String] |
|
156 |
var line: String = null |
|
|
51251
d55cce4d72dd
more permissive File.read_lines, which is relevant for Managed_Process join/kill;
wenzelm
parents:
50845
diff
changeset
|
157 |
while ({ line = try { reader.readLine} catch { case _: IOException => null }; line != null }) {
|
| 50845 | 158 |
progress(line) |
159 |
result += line |
|
160 |
} |
|
161 |
reader.close |
|
162 |
result.toList |
|
163 |
} |
|
164 |
||
165 |
||
| 48550 | 166 |
/* try_read */ |
167 |
||
168 |
def try_read(paths: Seq[Path]): String = |
|
169 |
{
|
|
170 |
val buf = new StringBuilder |
|
171 |
for (path <- paths if path.is_file) {
|
|
172 |
buf.append(read(path)) |
|
173 |
buf.append('\n')
|
|
174 |
} |
|
175 |
buf.toString |
|
176 |
} |
|
177 |
||
178 |
||
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
179 |
/* write */ |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
180 |
|
|
52671
9a360530eac8
separate module XZ_File to avoid initial dependency on org.tukaani.xz;
wenzelm
parents:
51982
diff
changeset
|
181 |
def write_file(file: JFile, text: Iterable[CharSequence], |
| 51982 | 182 |
make_stream: OutputStream => OutputStream) |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
183 |
{
|
| 51504 | 184 |
val stream = make_stream(new FileOutputStream(file)) |
185 |
val writer = new BufferedWriter(new OutputStreamWriter(stream, UTF8.charset)) |
|
| 51982 | 186 |
try { text.iterator.foreach(writer.append(_)) }
|
187 |
finally { writer.close }
|
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
188 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
189 |
|
| 51982 | 190 |
def write(file: JFile, text: Iterable[CharSequence]): Unit = write_file(file, text, (s) => s) |
191 |
def write(file: JFile, text: CharSequence): Unit = write(file, List(text)) |
|
192 |
def write(path: Path, text: Iterable[CharSequence]): Unit = write(path.file, text) |
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
193 |
def write(path: Path, text: CharSequence): Unit = write(path.file, text) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
194 |
|
| 51982 | 195 |
def write_gzip(file: JFile, text: Iterable[CharSequence]): Unit = |
| 51504 | 196 |
write_file(file, text, (s: OutputStream) => new GZIPOutputStream(new BufferedOutputStream(s))) |
| 51982 | 197 |
def write_gzip(file: JFile, text: CharSequence): Unit = write_gzip(file, List(text)) |
198 |
def write_gzip(path: Path, text: Iterable[CharSequence]): Unit = write_gzip(path.file, text) |
|
| 48494 | 199 |
def write_gzip(path: Path, text: CharSequence): Unit = write_gzip(path.file, text) |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
200 |
|
| 53336 | 201 |
def write_backup(path: Path, text: CharSequence) |
202 |
{
|
|
203 |
path.file renameTo path.backup.file |
|
204 |
File.write(path, text) |
|
205 |
} |
|
206 |
||
| 58610 | 207 |
def write_backup2(path: Path, text: CharSequence) |
208 |
{
|
|
209 |
path.file renameTo path.backup2.file |
|
210 |
File.write(path, text) |
|
211 |
} |
|
212 |
||
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
213 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
214 |
/* copy */ |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
215 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
216 |
def eq(file1: JFile, file2: JFile): Boolean = |
|
49673
2a088cff1e7b
more robust File.eq, and thus File.copy of "~~/lib/logo/isabelle.gif";
wenzelm
parents:
49610
diff
changeset
|
217 |
try { java.nio.file.Files.isSameFile(file1.toPath, file2.toPath) }
|
|
2a088cff1e7b
more robust File.eq, and thus File.copy of "~~/lib/logo/isabelle.gif";
wenzelm
parents:
49610
diff
changeset
|
218 |
catch { case ERROR(_) => false }
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
219 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
220 |
def copy(src: JFile, dst: JFile) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
221 |
{
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
222 |
if (!eq(src, dst)) {
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
223 |
val in = new FileInputStream(src) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
224 |
try {
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
225 |
val out = new FileOutputStream(dst) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
226 |
try {
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
227 |
val buf = new Array[Byte](65536) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
228 |
var m = 0 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
229 |
do {
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
230 |
m = in.read(buf, 0, buf.length) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
231 |
if (m != -1) out.write(buf, 0, m) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
232 |
} while (m != -1) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
233 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
234 |
finally { out.close }
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
235 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
236 |
finally { in.close }
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
237 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
238 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
239 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
240 |
def copy(path1: Path, path2: Path): Unit = copy(path1.file, path2.file) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
241 |
} |