| author | wenzelm |
| Sat, 15 Oct 2016 15:14:46 +0200 | |
| changeset 64223 | 9d5b9f41df77 |
| parent 64220 | e7cbf81ec4b7 |
| child 64304 | 96bc94c87a81 |
| 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} |
|
| 62703 | 13 |
import java.nio.file.{StandardOpenOption, StandardCopyOption, Path => JPath,
|
14 |
Files, SimpleFileVisitor, FileVisitResult} |
|
|
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
15 |
import java.nio.file.attribute.BasicFileAttributes |
| 60992 | 16 |
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
|
17 |
import java.util.zip.{GZIPInputStream, GZIPOutputStream}
|
| 60992 | 18 |
import java.util.regex.Pattern |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
19 |
|
| 64002 | 20 |
import org.tukaani.xz.{XZInputStream, XZOutputStream}
|
21 |
||
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
22 |
import scala.collection.mutable |
| 60992 | 23 |
import scala.util.matching.Regex |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
24 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
25 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
26 |
object File |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
27 |
{
|
| 60992 | 28 |
/* standard path (Cygwin or Posix) */ |
| 60988 | 29 |
|
30 |
def standard_path(path: Path): String = path.expand.implode |
|
31 |
||
| 60992 | 32 |
def standard_path(platform_path: String): String = |
33 |
if (Platform.is_windows) {
|
|
34 |
val Platform_Root = new Regex("(?i)" +
|
|
| 61291 | 35 |
Pattern.quote(Isabelle_System.cygwin_root()) + """(?:\\+|\z)(.*)""") |
| 60992 | 36 |
val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
|
37 |
||
38 |
platform_path.replace('/', '\\') match {
|
|
39 |
case Platform_Root(rest) => "/" + rest.replace('\\', '/')
|
|
40 |
case Drive(letter, rest) => |
|
41 |
"/cygdrive/" + Word.lowercase(letter) + |
|
42 |
(if (rest == "") "" else "/" + rest.replace('\\', '/'))
|
|
43 |
case path => path.replace('\\', '/')
|
|
44 |
} |
|
45 |
} |
|
46 |
else platform_path |
|
47 |
||
48 |
def standard_path(file: JFile): String = standard_path(file.getPath) |
|
49 |
||
| 64220 | 50 |
def path(file: JFile): Path = Path.explode(standard_path(file)) |
51 |
||
| 60992 | 52 |
def standard_url(name: String): String = |
53 |
try {
|
|
54 |
val url = new URL(name) |
|
55 |
if (url.getProtocol == "file") |
|
56 |
standard_path(URLDecoder.decode(url.getPath, UTF8.charset_name)) |
|
57 |
else name |
|
58 |
} |
|
59 |
catch { case _: MalformedURLException => standard_path(name) }
|
|
60 |
||
61 |
||
62 |
/* platform path (Windows or Posix) */ |
|
63 |
||
64 |
private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)")
|
|
65 |
private val Named_Root = new Regex("//+([^/]*)(.*)")
|
|
66 |
||
67 |
def platform_path(standard_path: String): String = |
|
68 |
if (Platform.is_windows) {
|
|
69 |
val result_path = new StringBuilder |
|
70 |
val rest = |
|
71 |
standard_path match {
|
|
72 |
case Cygdrive(drive, rest) => |
|
73 |
result_path ++= (Word.uppercase(drive) + ":" + JFile.separator) |
|
74 |
rest |
|
75 |
case Named_Root(root, rest) => |
|
76 |
result_path ++= JFile.separator |
|
77 |
result_path ++= JFile.separator |
|
78 |
result_path ++= root |
|
79 |
rest |
|
80 |
case path if path.startsWith("/") =>
|
|
| 61291 | 81 |
result_path ++= Isabelle_System.cygwin_root() |
| 60992 | 82 |
path |
83 |
case path => path |
|
84 |
} |
|
85 |
for (p <- space_explode('/', rest) if p != "") {
|
|
86 |
val len = result_path.length |
|
87 |
if (len > 0 && result_path(len - 1) != JFile.separatorChar) |
|
88 |
result_path += JFile.separatorChar |
|
89 |
result_path ++= p |
|
90 |
} |
|
91 |
result_path.toString |
|
92 |
} |
|
93 |
else standard_path |
|
94 |
||
95 |
def platform_path(path: Path): String = platform_path(standard_path(path)) |
|
| 60988 | 96 |
def platform_file(path: Path): JFile = new JFile(platform_path(path)) |
97 |
||
| 60992 | 98 |
def platform_url(raw_path: Path): String = |
| 60988 | 99 |
{
|
100 |
val path = raw_path.expand |
|
101 |
require(path.is_absolute) |
|
102 |
val s = platform_path(path).replaceAll(" ", "%20")
|
|
103 |
if (!Platform.is_windows) "file://" + s |
|
104 |
else if (s.startsWith("\\\\")) "file:" + s.replace('\\', '/')
|
|
105 |
else "file:///" + s.replace('\\', '/')
|
|
106 |
} |
|
107 |
||
| 60992 | 108 |
|
|
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
109 |
/* bash path */ |
| 60992 | 110 |
|
| 62548 | 111 |
private def bash_chr(c: Byte): String = |
|
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
112 |
{
|
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
113 |
val ch = c.toChar |
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
114 |
ch match {
|
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
115 |
case '\t' => "$'\\t'" |
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
116 |
case '\n' => "$'\\n'" |
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
117 |
case '\f' => "$'\\f'" |
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
118 |
case '\r' => "$'\\r'" |
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
119 |
case _ => |
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
120 |
if (Symbol.is_ascii_letter(ch) || Symbol.is_ascii_digit(ch) || "-./:_".contains(ch)) |
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
121 |
Symbol.ascii(ch) |
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
122 |
else if (c < 0) "$'\\x" + Integer.toHexString(256 + c) + "'" |
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
123 |
else if (c < 16) "$'\\x0" + Integer.toHexString(c) + "'" |
| 62548 | 124 |
else if (c < 32 || c >= 127) "$'\\x" + Integer.toHexString(c) + "'" |
|
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
125 |
else "\\" + ch |
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
126 |
} |
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
127 |
} |
|
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
128 |
|
| 62548 | 129 |
def bash_string(s: String): String = |
| 62854 | 130 |
if (s == "") "\"\"" |
131 |
else UTF8.bytes(s).iterator.map(bash_chr(_)).mkString |
|
|
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
132 |
|
| 62548 | 133 |
def bash_args(args: List[String]): String = |
134 |
args.iterator.map(bash_string(_)).mkString(" ")
|
|
|
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
135 |
|
| 62548 | 136 |
def bash_path(path: Path): String = bash_string(standard_path(path)) |
137 |
def bash_path(file: JFile): String = bash_string(standard_path(file)) |
|
| 60988 | 138 |
|
139 |
||
| 62544 | 140 |
/* directory entries */ |
141 |
||
142 |
def check_dir(path: Path): Path = |
|
143 |
if (path.is_dir) path else error("No such directory: " + path)
|
|
144 |
||
145 |
def check_file(path: Path): Path = |
|
146 |
if (path.is_file) path else error("No such file: " + path)
|
|
147 |
||
148 |
||
|
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
149 |
/* directory content */ |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
150 |
|
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
151 |
def read_dir(dir: Path): List[String] = |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
152 |
{
|
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
153 |
if (!dir.is_dir) error("Bad directory: " + dir.toString)
|
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
154 |
val files = dir.file.listFiles |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
155 |
if (files == null) Nil |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
156 |
else files.toList.map(_.getName) |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
157 |
} |
|
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
158 |
|
|
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
159 |
def find_files(start: JFile, pred: JFile => Boolean = _ => true): List[JFile] = |
|
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
160 |
{
|
|
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
161 |
val result = new mutable.ListBuffer[JFile] |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
162 |
|
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
163 |
if (start.isFile && pred(start)) result += start |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
164 |
else if (start.isDirectory) {
|
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
165 |
Files.walkFileTree(start.toPath, |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
166 |
new SimpleFileVisitor[JPath] {
|
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
167 |
override def visitFile(path: JPath, attrs: BasicFileAttributes): FileVisitResult = |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
168 |
{
|
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
169 |
val file = path.toFile |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
170 |
if (pred(file)) result += file |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
171 |
FileVisitResult.CONTINUE |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
172 |
} |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
173 |
} |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
174 |
) |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
175 |
} |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
176 |
|
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
177 |
result.toList |
|
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
178 |
} |
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
179 |
|
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
180 |
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
181 |
/* read */ |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
182 |
|
| 54440 | 183 |
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
|
184 |
def read(path: Path): String = read(path.file) |
|
f686cb016c0c
more direct File.read_bytes -- avoid cumulative copying of StringBuilder;
wenzelm
parents:
48613
diff
changeset
|
185 |
|
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
186 |
|
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
187 |
def read_stream(reader: BufferedReader): String = |
|
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 |
val output = new StringBuilder(100) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
190 |
var c = -1 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
191 |
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
|
192 |
reader.close |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
193 |
output.toString |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
194 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
195 |
|
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
196 |
def read_stream(stream: InputStream): String = |
| 64000 | 197 |
read_stream(new BufferedReader(new InputStreamReader(stream, UTF8.charset))) |
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
198 |
|
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
199 |
def read_gzip(file: JFile): String = |
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
200 |
read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)))) |
| 64000 | 201 |
def read_gzip(path: Path): String = read_gzip(path.file) |
| 51504 | 202 |
|
| 64002 | 203 |
def read_xz(file: JFile): String = |
204 |
read_stream(new XZInputStream(new BufferedInputStream(new FileInputStream(file)))) |
|
| 64000 | 205 |
def read_xz(path: Path): String = read_xz(path.file) |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
206 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
207 |
|
| 50845 | 208 |
/* read lines */ |
209 |
||
210 |
def read_lines(reader: BufferedReader, progress: String => Unit): List[String] = |
|
211 |
{
|
|
212 |
val result = new mutable.ListBuffer[String] |
|
213 |
var line: String = null |
|
|
51251
d55cce4d72dd
more permissive File.read_lines, which is relevant for Managed_Process join/kill;
wenzelm
parents:
50845
diff
changeset
|
214 |
while ({ line = try { reader.readLine} catch { case _: IOException => null }; line != null }) {
|
| 50845 | 215 |
progress(line) |
216 |
result += line |
|
217 |
} |
|
218 |
reader.close |
|
219 |
result.toList |
|
220 |
} |
|
221 |
||
222 |
||
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
223 |
/* write */ |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
224 |
|
| 64002 | 225 |
def write_file(file: JFile, text: CharSequence, make_stream: OutputStream => OutputStream) |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
226 |
{
|
| 51504 | 227 |
val stream = make_stream(new FileOutputStream(file)) |
228 |
val writer = new BufferedWriter(new OutputStreamWriter(stream, UTF8.charset)) |
|
| 64002 | 229 |
try { writer.append(text) } finally { writer.close }
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
230 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
231 |
|
| 64003 | 232 |
def write(file: JFile, text: CharSequence): Unit = write_file(file, text, s => s) |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
233 |
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
|
234 |
|
| 64002 | 235 |
def write_gzip(file: JFile, text: CharSequence): Unit = |
| 51504 | 236 |
write_file(file, text, (s: OutputStream) => new GZIPOutputStream(new BufferedOutputStream(s))) |
| 48494 | 237 |
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
|
238 |
|
| 64002 | 239 |
def write_xz(file: JFile, text: CharSequence, options: XZ.Options): Unit = |
| 64003 | 240 |
File.write_file(file, text, s => new XZOutputStream(new BufferedOutputStream(s), options)) |
241 |
def write_xz(file: JFile, text: CharSequence): Unit = write_xz(file, text, XZ.options()) |
|
| 64002 | 242 |
def write_xz(path: Path, text: CharSequence, options: XZ.Options): Unit = |
243 |
write_xz(path.file, text, options) |
|
| 64003 | 244 |
def write_xz(path: Path, text: CharSequence): Unit = write_xz(path, text, XZ.options()) |
| 64000 | 245 |
|
| 53336 | 246 |
def write_backup(path: Path, text: CharSequence) |
247 |
{
|
|
| 64213 | 248 |
mv(path, path.backup) |
| 62444 | 249 |
write(path, text) |
| 53336 | 250 |
} |
251 |
||
| 58610 | 252 |
def write_backup2(path: Path, text: CharSequence) |
253 |
{
|
|
| 64213 | 254 |
mv(path, path.backup2) |
| 62444 | 255 |
write(path, text) |
| 58610 | 256 |
} |
257 |
||
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
258 |
|
| 62703 | 259 |
/* append */ |
260 |
||
261 |
def append(file: JFile, text: CharSequence): Unit = |
|
262 |
Files.write(file.toPath, UTF8.bytes(text.toString), |
|
263 |
StandardOpenOption.APPEND, StandardOpenOption.CREATE) |
|
264 |
||
265 |
def append(path: Path, text: CharSequence): Unit = append(path.file, text) |
|
266 |
||
267 |
||
| 64213 | 268 |
/* eq */ |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
269 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
270 |
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
|
271 |
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
|
272 |
catch { case ERROR(_) => false }
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
273 |
|
| 64213 | 274 |
def eq(path1: Path, path2: Path): Boolean = eq(path1.file, path2.file) |
275 |
||
276 |
||
277 |
/* copy */ |
|
278 |
||
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
279 |
def copy(src: JFile, dst: JFile) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
280 |
{
|
|
61371
17944b500166
clarified, according to Isabelle_System.copy_file in ML;
wenzelm
parents:
61291
diff
changeset
|
281 |
val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst |
| 61373 | 282 |
if (!eq(src, target)) |
283 |
Files.copy(src.toPath, target.toPath, |
|
284 |
StandardCopyOption.COPY_ATTRIBUTES, |
|
285 |
StandardCopyOption.REPLACE_EXISTING) |
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
286 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
287 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
288 |
def copy(path1: Path, path2: Path): Unit = copy(path1.file, path2.file) |
| 64213 | 289 |
|
290 |
||
291 |
/* move */ |
|
292 |
||
293 |
def mv(file1: JFile, file2: JFile): Unit = |
|
294 |
Files.move(file1.toPath, file2.toPath, StandardCopyOption.REPLACE_EXISTING) |
|
295 |
||
296 |
def mv(path1: Path, path2: Path): Unit = mv(path1.file, path2.file) |
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
297 |
} |