src/Pure/General/file.scala
author wenzelm
Sun, 22 Jan 2017 13:11:38 +0100
changeset 64932 89c0896a19ad
parent 64775 dd3797f1e0d6
child 64934 795055a0be98
permissions -rw-r--r--
clarified operation: include dirs as well;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
64698
e022a69db531 tuned comments;
wenzelm
parents: 64668
diff changeset
     4
File-system operations.
48411
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
18095684c5a6 basic support for xz files;
wenzelm
parents: 51251
diff changeset
    11
  OutputStream, InputStream, FileInputStream, BufferedInputStream, BufferedReader,
18095684c5a6 basic support for xz files;
wenzelm
parents: 51251
diff changeset
    12
  InputStreamReader, File => JFile, IOException}
62703
01b71da798dd more operations;
wenzelm
parents: 62636
diff changeset
    13
import java.nio.file.{StandardOpenOption, StandardCopyOption, Path => JPath,
01b71da798dd more operations;
wenzelm
parents: 62636
diff changeset
    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
64775
dd3797f1e0d6 clarified file URIs;
wenzelm
parents: 64760
diff changeset
    16
import java.net.{URL, 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
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    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
08f89f0e8a62 clarified modules;
wenzelm
parents: 64000
diff changeset
    20
import org.tukaani.xz.{XZInputStream, XZOutputStream}
08f89f0e8a62 clarified modules;
wenzelm
parents: 64000
diff changeset
    21
48411
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
    22
import scala.collection.mutable
60992
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    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
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    28
  /* standard path (Cygwin or Posix) */
60988
1d7a7e33fd67 tuned signature, according to ML version;
wenzelm
parents: 58610
diff changeset
    29
1d7a7e33fd67 tuned signature, according to ML version;
wenzelm
parents: 58610
diff changeset
    30
  def standard_path(path: Path): String = path.expand.implode
1d7a7e33fd67 tuned signature, according to ML version;
wenzelm
parents: 58610
diff changeset
    31
60992
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    32
  def standard_path(platform_path: String): String =
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    33
    if (Platform.is_windows) {
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    34
      val Platform_Root = new Regex("(?i)" +
61291
e00e1bf23d03 uniform treatment of bootstrap directories;
wenzelm
parents: 60992
diff changeset
    35
        Pattern.quote(Isabelle_System.cygwin_root()) + """(?:\\+|\z)(.*)""")
64775
dd3797f1e0d6 clarified file URIs;
wenzelm
parents: 64760
diff changeset
    36
      val Drive = new Regex("""([a-zA-Z]):\\*(.*)""")
60992
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    37
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    38
      platform_path.replace('/', '\\') match {
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    39
        case Platform_Root(rest) => "/" + rest.replace('\\', '/')
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    40
        case Drive(letter, rest) =>
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    41
          "/cygdrive/" + Word.lowercase(letter) +
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    42
            (if (rest == "") "" else "/" + rest.replace('\\', '/'))
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    43
        case path => path.replace('\\', '/')
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    44
      }
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    45
    }
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    46
    else platform_path
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    47
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    48
  def standard_path(file: JFile): String = standard_path(file.getPath)
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    49
64220
e7cbf81ec4b7 prefer Isabelle standard Path;
wenzelm
parents: 64214
diff changeset
    50
  def path(file: JFile): Path = Path.explode(standard_path(file))
64759
100941134718 clarified master_dir: file-URL;
wenzelm
parents: 64730
diff changeset
    51
  def pwd(): Path = path(Path.current.file.toPath.toAbsolutePath.toFile)
64220
e7cbf81ec4b7 prefer Isabelle standard Path;
wenzelm
parents: 64214
diff changeset
    52
60992
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    53
  def standard_url(name: String): String =
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    54
    try {
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    55
      val url = new URL(name)
64775
dd3797f1e0d6 clarified file URIs;
wenzelm
parents: 64760
diff changeset
    56
      if (url.getProtocol == "file" && Url.is_wellformed_file(name))
dd3797f1e0d6 clarified file URIs;
wenzelm
parents: 64760
diff changeset
    57
        standard_path(Url.parse_file(name))
60992
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    58
      else name
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    59
    }
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    60
    catch { case _: MalformedURLException => standard_path(name) }
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    61
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    62
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    63
  /* platform path (Windows or Posix) */
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    64
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    65
  private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)")
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    66
  private val Named_Root = new Regex("//+([^/]*)(.*)")
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    67
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    68
  def platform_path(standard_path: String): String =
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    69
    if (Platform.is_windows) {
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    70
      val result_path = new StringBuilder
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    71
      val rest =
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    72
        standard_path match {
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    73
          case Cygdrive(drive, rest) =>
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    74
            result_path ++= (Word.uppercase(drive) + ":" + JFile.separator)
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    75
            rest
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    76
          case Named_Root(root, rest) =>
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    77
            result_path ++= JFile.separator
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    78
            result_path ++= JFile.separator
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    79
            result_path ++= root
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    80
            rest
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    81
          case path if path.startsWith("/") =>
61291
e00e1bf23d03 uniform treatment of bootstrap directories;
wenzelm
parents: 60992
diff changeset
    82
            result_path ++= Isabelle_System.cygwin_root()
60992
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    83
            path
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    84
          case path => path
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    85
        }
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    86
      for (p <- space_explode('/', rest) if p != "") {
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    87
        val len = result_path.length
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    88
        if (len > 0 && result_path(len - 1) != JFile.separatorChar)
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    89
          result_path += JFile.separatorChar
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    90
        result_path ++= p
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    91
      }
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    92
      result_path.toString
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    93
    }
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    94
    else standard_path
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    95
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    96
  def platform_path(path: Path): String = platform_path(standard_path(path))
60988
1d7a7e33fd67 tuned signature, according to ML version;
wenzelm
parents: 58610
diff changeset
    97
  def platform_file(path: Path): JFile = new JFile(platform_path(path))
1d7a7e33fd67 tuned signature, according to ML version;
wenzelm
parents: 58610
diff changeset
    98
60992
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
    99
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
   100
  /* bash path */
60992
89effcb342df clarified modules, like ML version;
wenzelm
parents: 60988
diff changeset
   101
64304
96bc94c87a81 clarified modules;
wenzelm
parents: 64220
diff changeset
   102
  def bash_path(path: Path): String = Bash.string(standard_path(path))
96bc94c87a81 clarified modules;
wenzelm
parents: 64220
diff changeset
   103
  def bash_path(file: JFile): String = Bash.string(standard_path(file))
60988
1d7a7e33fd67 tuned signature, according to ML version;
wenzelm
parents: 58610
diff changeset
   104
1d7a7e33fd67 tuned signature, according to ML version;
wenzelm
parents: 58610
diff changeset
   105
62544
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents: 62444
diff changeset
   106
  /* directory entries */
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents: 62444
diff changeset
   107
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents: 62444
diff changeset
   108
  def check_dir(path: Path): Path =
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents: 62444
diff changeset
   109
    if (path.is_dir) path else error("No such directory: " + path)
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents: 62444
diff changeset
   110
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents: 62444
diff changeset
   111
  def check_file(path: Path): Path =
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents: 62444
diff changeset
   112
    if (path.is_file) path else error("No such file: " + path)
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents: 62444
diff changeset
   113
efa178abe023 manage the underlying ML process in Scala;
wenzelm
parents: 62444
diff changeset
   114
62829
4141c2a8458b clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents: 62704
diff changeset
   115
  /* directory content */
4141c2a8458b clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents: 62704
diff changeset
   116
4141c2a8458b clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents: 62704
diff changeset
   117
  def read_dir(dir: Path): List[String] =
4141c2a8458b clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents: 62704
diff changeset
   118
  {
4141c2a8458b clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents: 62704
diff changeset
   119
    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
   120
    val files = dir.file.listFiles
4141c2a8458b clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents: 62704
diff changeset
   121
    if (files == null) Nil
4141c2a8458b clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents: 62704
diff changeset
   122
    else files.toList.map(_.getName)
4141c2a8458b clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents: 62704
diff changeset
   123
  }
48613
232652ac346e clarified directory content operations (similar to ML version);
wenzelm
parents: 48550
diff changeset
   124
64932
89c0896a19ad clarified operation: include dirs as well;
wenzelm
parents: 64775
diff changeset
   125
  def find_files(
89c0896a19ad clarified operation: include dirs as well;
wenzelm
parents: 64775
diff changeset
   126
    start: JFile,
89c0896a19ad clarified operation: include dirs as well;
wenzelm
parents: 64775
diff changeset
   127
    pred: JFile => Boolean = _ => true,
89c0896a19ad clarified operation: include dirs as well;
wenzelm
parents: 64775
diff changeset
   128
    include_dirs: Boolean = false): List[JFile] =
48613
232652ac346e clarified directory content operations (similar to ML version);
wenzelm
parents: 48550
diff changeset
   129
  {
62443
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   130
    val result = new mutable.ListBuffer[JFile]
64932
89c0896a19ad clarified operation: include dirs as well;
wenzelm
parents: 64775
diff changeset
   131
    def check(file: JFile) { if (pred(file)) result += file }
62443
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   132
64932
89c0896a19ad clarified operation: include dirs as well;
wenzelm
parents: 64775
diff changeset
   133
    if (start.isFile) check(start)
62443
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   134
    else if (start.isDirectory) {
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   135
      Files.walkFileTree(start.toPath,
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   136
        new SimpleFileVisitor[JPath] {
64932
89c0896a19ad clarified operation: include dirs as well;
wenzelm
parents: 64775
diff changeset
   137
          override def preVisitDirectory(path: JPath, attrs: BasicFileAttributes): FileVisitResult =
89c0896a19ad clarified operation: include dirs as well;
wenzelm
parents: 64775
diff changeset
   138
          {
89c0896a19ad clarified operation: include dirs as well;
wenzelm
parents: 64775
diff changeset
   139
            if (include_dirs) check(path.toFile)
89c0896a19ad clarified operation: include dirs as well;
wenzelm
parents: 64775
diff changeset
   140
            FileVisitResult.CONTINUE
89c0896a19ad clarified operation: include dirs as well;
wenzelm
parents: 64775
diff changeset
   141
          }
62443
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   142
          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
   143
          {
64932
89c0896a19ad clarified operation: include dirs as well;
wenzelm
parents: 64775
diff changeset
   144
            check(path.toFile)
62443
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   145
            FileVisitResult.CONTINUE
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   146
          }
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   147
        }
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   148
      )
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   149
    }
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   150
133f65ac17e5 just one File.find_files, based on Java 7 Files operations;
wenzelm
parents: 62294
diff changeset
   151
    result.toList
48613
232652ac346e clarified directory content operations (similar to ML version);
wenzelm
parents: 48550
diff changeset
   152
  }
232652ac346e clarified directory content operations (similar to ML version);
wenzelm
parents: 48550
diff changeset
   153
232652ac346e clarified directory content operations (similar to ML version);
wenzelm
parents: 48550
diff changeset
   154
48411
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   155
  /* read */
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   156
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 53336
diff changeset
   157
  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
   158
  def read(path: Path): String = read(path.file)
f686cb016c0c more direct File.read_bytes -- avoid cumulative copying of StringBuilder;
wenzelm
parents: 48613
diff changeset
   159
50684
12b7e0b4a66e support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents: 50203
diff changeset
   160
12b7e0b4a66e support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents: 50203
diff changeset
   161
  def read_stream(reader: BufferedReader): String =
48411
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   162
  {
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   163
    val output = new StringBuilder(100)
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   164
    var c = -1
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   165
    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
   166
    reader.close
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   167
    output.toString
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   168
  }
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   169
50684
12b7e0b4a66e support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents: 50203
diff changeset
   170
  def read_stream(stream: InputStream): String =
64000
445b3deced8f clarified modules;
wenzelm
parents: 62854
diff changeset
   171
    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
   172
12b7e0b4a66e support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents: 50203
diff changeset
   173
  def read_gzip(file: JFile): String =
12b7e0b4a66e support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents: 50203
diff changeset
   174
    read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))))
64000
445b3deced8f clarified modules;
wenzelm
parents: 62854
diff changeset
   175
  def read_gzip(path: Path): String = read_gzip(path.file)
51504
18095684c5a6 basic support for xz files;
wenzelm
parents: 51251
diff changeset
   176
64002
08f89f0e8a62 clarified modules;
wenzelm
parents: 64000
diff changeset
   177
  def read_xz(file: JFile): String =
08f89f0e8a62 clarified modules;
wenzelm
parents: 64000
diff changeset
   178
    read_stream(new XZInputStream(new BufferedInputStream(new FileInputStream(file))))
64000
445b3deced8f clarified modules;
wenzelm
parents: 62854
diff changeset
   179
  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
   180
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   181
50845
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   182
  /* read lines */
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   183
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   184
  def read_lines(reader: BufferedReader, progress: String => Unit): List[String] =
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   185
  {
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   186
    val result = new mutable.ListBuffer[String]
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   187
    var line: String = null
51251
d55cce4d72dd more permissive File.read_lines, which is relevant for Managed_Process join/kill;
wenzelm
parents: 50845
diff changeset
   188
    while ({ line = try { reader.readLine} catch { case _: IOException => null }; line != null }) {
50845
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   189
      progress(line)
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   190
      result += line
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   191
    }
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   192
    reader.close
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   193
    result.toList
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   194
  }
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   195
477ca927676f immediate theory progress for build_dialog;
wenzelm
parents: 50684
diff changeset
   196
48411
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   197
  /* write */
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   198
64002
08f89f0e8a62 clarified modules;
wenzelm
parents: 64000
diff changeset
   199
  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
   200
  {
51504
18095684c5a6 basic support for xz files;
wenzelm
parents: 51251
diff changeset
   201
    val stream = make_stream(new FileOutputStream(file))
18095684c5a6 basic support for xz files;
wenzelm
parents: 51251
diff changeset
   202
    val writer = new BufferedWriter(new OutputStreamWriter(stream, UTF8.charset))
64002
08f89f0e8a62 clarified modules;
wenzelm
parents: 64000
diff changeset
   203
    try { writer.append(text) } finally { writer.close }
48411
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   204
  }
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   205
64003
73af1d36aeff tuned signature;
wenzelm
parents: 64002
diff changeset
   206
  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
   207
  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
   208
64002
08f89f0e8a62 clarified modules;
wenzelm
parents: 64000
diff changeset
   209
  def write_gzip(file: JFile, text: CharSequence): Unit =
51504
18095684c5a6 basic support for xz files;
wenzelm
parents: 51251
diff changeset
   210
    write_file(file, text, (s: OutputStream) => new GZIPOutputStream(new BufferedOutputStream(s)))
48494
00eb5be9e76b read/write dependency information;
wenzelm
parents: 48418
diff changeset
   211
  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
   212
64002
08f89f0e8a62 clarified modules;
wenzelm
parents: 64000
diff changeset
   213
  def write_xz(file: JFile, text: CharSequence, options: XZ.Options): Unit =
64003
73af1d36aeff tuned signature;
wenzelm
parents: 64002
diff changeset
   214
    File.write_file(file, text, s => new XZOutputStream(new BufferedOutputStream(s), options))
73af1d36aeff tuned signature;
wenzelm
parents: 64002
diff changeset
   215
  def write_xz(file: JFile, text: CharSequence): Unit = write_xz(file, text, XZ.options())
64002
08f89f0e8a62 clarified modules;
wenzelm
parents: 64000
diff changeset
   216
  def write_xz(path: Path, text: CharSequence, options: XZ.Options): Unit =
08f89f0e8a62 clarified modules;
wenzelm
parents: 64000
diff changeset
   217
    write_xz(path.file, text, options)
64003
73af1d36aeff tuned signature;
wenzelm
parents: 64002
diff changeset
   218
  def write_xz(path: Path, text: CharSequence): Unit = write_xz(path, text, XZ.options())
64000
445b3deced8f clarified modules;
wenzelm
parents: 62854
diff changeset
   219
53336
b3bf6d72fea5 more general backup files;
wenzelm
parents: 52671
diff changeset
   220
  def write_backup(path: Path, text: CharSequence)
b3bf6d72fea5 more general backup files;
wenzelm
parents: 52671
diff changeset
   221
  {
64482
43f6c28ff496 clarified File.move: target directory like File.copy;
wenzelm
parents: 64345
diff changeset
   222
    if (path.is_file) move(path, path.backup)
62444
wenzelm
parents: 62443
diff changeset
   223
    write(path, text)
53336
b3bf6d72fea5 more general backup files;
wenzelm
parents: 52671
diff changeset
   224
  }
b3bf6d72fea5 more general backup files;
wenzelm
parents: 52671
diff changeset
   225
58610
fffdbce036db added update_cartouches tool;
wenzelm
parents: 56783
diff changeset
   226
  def write_backup2(path: Path, text: CharSequence)
fffdbce036db added update_cartouches tool;
wenzelm
parents: 56783
diff changeset
   227
  {
64482
43f6c28ff496 clarified File.move: target directory like File.copy;
wenzelm
parents: 64345
diff changeset
   228
    if (path.is_file) move(path, path.backup2)
62444
wenzelm
parents: 62443
diff changeset
   229
    write(path, text)
58610
fffdbce036db added update_cartouches tool;
wenzelm
parents: 56783
diff changeset
   230
  }
fffdbce036db added update_cartouches tool;
wenzelm
parents: 56783
diff changeset
   231
48411
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   232
62703
01b71da798dd more operations;
wenzelm
parents: 62636
diff changeset
   233
  /* append */
01b71da798dd more operations;
wenzelm
parents: 62636
diff changeset
   234
01b71da798dd more operations;
wenzelm
parents: 62636
diff changeset
   235
  def append(file: JFile, text: CharSequence): Unit =
01b71da798dd more operations;
wenzelm
parents: 62636
diff changeset
   236
    Files.write(file.toPath, UTF8.bytes(text.toString),
01b71da798dd more operations;
wenzelm
parents: 62636
diff changeset
   237
      StandardOpenOption.APPEND, StandardOpenOption.CREATE)
01b71da798dd more operations;
wenzelm
parents: 62636
diff changeset
   238
01b71da798dd more operations;
wenzelm
parents: 62636
diff changeset
   239
  def append(path: Path, text: CharSequence): Unit = append(path.file, text)
01b71da798dd more operations;
wenzelm
parents: 62636
diff changeset
   240
01b71da798dd more operations;
wenzelm
parents: 62636
diff changeset
   241
64213
b265dd04d57d clarified file operations;
wenzelm
parents: 64003
diff changeset
   242
  /* eq */
48411
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   243
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   244
  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
   245
    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
   246
    catch { case ERROR(_) => false }
48411
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   247
64213
b265dd04d57d clarified file operations;
wenzelm
parents: 64003
diff changeset
   248
  def eq(path1: Path, path2: Path): Boolean = eq(path1.file, path2.file)
b265dd04d57d clarified file operations;
wenzelm
parents: 64003
diff changeset
   249
b265dd04d57d clarified file operations;
wenzelm
parents: 64003
diff changeset
   250
b265dd04d57d clarified file operations;
wenzelm
parents: 64003
diff changeset
   251
  /* copy */
b265dd04d57d clarified file operations;
wenzelm
parents: 64003
diff changeset
   252
48411
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   253
  def copy(src: JFile, dst: JFile)
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   254
  {
61371
17944b500166 clarified, according to Isabelle_System.copy_file in ML;
wenzelm
parents: 61291
diff changeset
   255
    val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst
61373
16ed9b97c72d more accurate imitation of "cp -p -f";
wenzelm
parents: 61371
diff changeset
   256
    if (!eq(src, target))
16ed9b97c72d more accurate imitation of "cp -p -f";
wenzelm
parents: 61371
diff changeset
   257
      Files.copy(src.toPath, target.toPath,
16ed9b97c72d more accurate imitation of "cp -p -f";
wenzelm
parents: 61371
diff changeset
   258
        StandardCopyOption.COPY_ATTRIBUTES,
16ed9b97c72d more accurate imitation of "cp -p -f";
wenzelm
parents: 61371
diff changeset
   259
        StandardCopyOption.REPLACE_EXISTING)
48411
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   260
  }
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   261
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   262
  def copy(path1: Path, path2: Path): Unit = copy(path1.file, path2.file)
64213
b265dd04d57d clarified file operations;
wenzelm
parents: 64003
diff changeset
   263
b265dd04d57d clarified file operations;
wenzelm
parents: 64003
diff changeset
   264
b265dd04d57d clarified file operations;
wenzelm
parents: 64003
diff changeset
   265
  /* move */
b265dd04d57d clarified file operations;
wenzelm
parents: 64003
diff changeset
   266
64482
43f6c28ff496 clarified File.move: target directory like File.copy;
wenzelm
parents: 64345
diff changeset
   267
  def move(src: JFile, dst: JFile)
43f6c28ff496 clarified File.move: target directory like File.copy;
wenzelm
parents: 64345
diff changeset
   268
  {
43f6c28ff496 clarified File.move: target directory like File.copy;
wenzelm
parents: 64345
diff changeset
   269
    val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst
43f6c28ff496 clarified File.move: target directory like File.copy;
wenzelm
parents: 64345
diff changeset
   270
    if (!eq(src, target))
43f6c28ff496 clarified File.move: target directory like File.copy;
wenzelm
parents: 64345
diff changeset
   271
      Files.move(src.toPath, target.toPath, StandardCopyOption.REPLACE_EXISTING)
43f6c28ff496 clarified File.move: target directory like File.copy;
wenzelm
parents: 64345
diff changeset
   272
  }
64213
b265dd04d57d clarified file operations;
wenzelm
parents: 64003
diff changeset
   273
64482
43f6c28ff496 clarified File.move: target directory like File.copy;
wenzelm
parents: 64345
diff changeset
   274
  def move(path1: Path, path2: Path): Unit = move(path1.file, path2.file)
48411
5b3440850d36 more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff changeset
   275
}