summaryrefslogtreecommitdiff
path: root/src/compiler
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-09-01 12:52:59 +0000
committerPaul Phillips <paulp@improving.org>2009-09-01 12:52:59 +0000
commit6da528df440dd72742f9e0b6773a2b79dc07d5b2 (patch)
tree14ab3efa3e6d61a48006eabe57b786a688cc23dc /src/compiler
parent0600724c0ad913680c1d4ed8182d5c0d3bf8de97 (diff)
downloadscala-6da528df440dd72742f9e0b6773a2b79dc07d5b2.tar.gz
scala-6da528df440dd72742f9e0b6773a2b79dc07d5b2.tar.bz2
scala-6da528df440dd72742f9e0b6773a2b79dc07d5b2.zip
More cleanup in nsc.io, and a toByteArray() met...
More cleanup in nsc.io, and a toByteArray() method for File.
Diffstat (limited to 'src/compiler')
-rw-r--r--src/compiler/scala/tools/nsc/io/AbstractFile.scala67
-rw-r--r--src/compiler/scala/tools/nsc/io/VirtualDirectory.scala36
-rw-r--r--src/compiler/scala/tools/nsc/io/VirtualFile.scala11
3 files changed, 35 insertions, 79 deletions
diff --git a/src/compiler/scala/tools/nsc/io/AbstractFile.scala b/src/compiler/scala/tools/nsc/io/AbstractFile.scala
index 2e26c07d88..d54e982753 100644
--- a/src/compiler/scala/tools/nsc/io/AbstractFile.scala
+++ b/src/compiler/scala/tools/nsc/io/AbstractFile.scala
@@ -8,9 +8,10 @@
package scala.tools.nsc
package io
-import java.io.{File, FileOutputStream, IOException, InputStream, OutputStream}
+import java.io.{ File => JFile, FileOutputStream, IOException, InputStream, OutputStream }
import java.net.URL
-import scala.io.{ File => SFile }
+import scala.io.{ Path, Directory, File }
+import PartialFunction._
import scala.collection.mutable.ArrayBuffer
@@ -20,19 +21,21 @@ import scala.collection.mutable.ArrayBuffer
*/
object AbstractFile
{
+ def isJarOrZip(f: Path) = cond(f.extension) { case Some("zip" | "jar") => true }
+
/** Returns "getFile(new File(path))". */
- def getFile(path: String): AbstractFile = getFile(new File(path))
+ def getFile(path: String): AbstractFile = getFile(Path(path))
+ def getFile(path: Path): AbstractFile = getFile(path.toFile)
/**
* If the specified File exists and is a regular file, returns an
* abstract regular file backed by it. Otherwise, returns <code>null</code>.
*/
def getFile(file: File): AbstractFile =
- if (file.isFile() && file.exists()) new PlainFile(file) else null
-
+ if (file.isFile) new PlainFile(file) else null
/** Returns "getDirectory(new File(path))". */
- def getDirectory(path: String): AbstractFile = getDirectory(new File(path))
+ def getDirectory(path: Path): AbstractFile = getDirectory(path.toFile)
/**
* If the specified File exists and is either a directory or a
@@ -42,15 +45,10 @@ object AbstractFile
* @param file ...
* @return ...
*/
- def getDirectory(file: File): AbstractFile = {
- if (file.isDirectory() && file.exists()) return new PlainFile(file)
- if (file.isFile() && file.exists()) {
- val path = file.getPath()
- if (path.endsWith(".jar") || path.endsWith(".zip"))
- return ZipArchive fromFile SFile(file)
- }
- null
- }
+ def getDirectory(file: File): AbstractFile =
+ if (file.isDirectory) new PlainFile(file)
+ else if (file.isFile && isJarOrZip(file)) ZipArchive fromFile file
+ else null
/**
* If the specified URL exists and is a readable zip or jar archive,
@@ -61,15 +59,7 @@ object AbstractFile
* @return ...
*/
def getURL(url: URL): AbstractFile =
- if (url ne null) {
- val path = url.getPath()
- if (path.endsWith(".jar") || path.endsWith(".zip"))
- ZipArchive.fromURL(url)
- else
- null
- }
- else
- null
+ Some(url) filterMap { case url: URL if isJarOrZip(url.getPath) => ZipArchive fromURL url } orNull
}
/**
@@ -111,11 +101,12 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
def container : AbstractFile
/** Returns the underlying File if any and null otherwise. */
- def file: File
+ def file: JFile
+ def sfile = File(file) // XXX
/** Does this abstract file denote an existing file? */
def exists: Boolean =
- if (file ne null) file.exists()
+ if (file ne null) file.exists
else true
/** Create a file on disk, if one does not exist already. */
@@ -208,11 +199,11 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
private def lookup(getFile: (AbstractFile, String, Boolean) => AbstractFile,
path0: String,
directory: Boolean): AbstractFile = {
- val separator = File.separatorChar
+ val separator = JFile.separatorChar
// trim trailing '/'s
- val path = if (path0.charAt(path0.length - 1) == separator) path0.substring(0, path0.length - 1) else path0
+ val path: String = if (path0.last == separator) path0 dropRight 1 else path0
val length = path.length()
- assert(0 < length && path.lastIndexOf(separator) < length - 1, path)
+ assert(length > 0 && !(path.last == separator), path)
var file = this
var start = 0
while (true) {
@@ -232,14 +223,7 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
*/
def fileNamed(name: String): AbstractFile = {
assert(isDirectory)
- val existing = lookupName(name, false)
- if (existing == null) {
- val newFile = new File(file, name)
- newFile.createNewFile()
- new PlainFile(newFile)
- } else {
- existing
- }
+ Option(lookupName(name, false)) getOrElse new PlainFile((sfile / name).ensureFile())
}
/**
@@ -248,14 +232,7 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
*/
def subdirectoryNamed(name: String): AbstractFile = {
assert (isDirectory)
- val existing = lookupName(name, true)
- if (existing == null) {
- val dir = new File(file, name)
- dir.mkdir()
- new PlainFile(dir)
- } else {
- existing
- }
+ Option(lookupName(name, true)) getOrElse new PlainFile((sfile / name).ensureDirectory())
}
/** Returns the path of this abstract file. */
diff --git a/src/compiler/scala/tools/nsc/io/VirtualDirectory.scala b/src/compiler/scala/tools/nsc/io/VirtualDirectory.scala
index 16381bbe6e..6b5af87d7c 100644
--- a/src/compiler/scala/tools/nsc/io/VirtualDirectory.scala
+++ b/src/compiler/scala/tools/nsc/io/VirtualDirectory.scala
@@ -4,7 +4,7 @@
// $Id$
package scala.tools.nsc
package io
-import scala.collection.{mutable=>mut}
+import scala.collection.mutable
/**
* An in-memory directory.
@@ -24,9 +24,7 @@ extends AbstractFile {
def container = maybeContainer.get
def isDirectory = true
var lastModified: Long = System.currentTimeMillis
- private def updateLastModified {
- lastModified = System.currentTimeMillis
- }
+
override def file = null
override def input = error("directories cannot be read")
override def output = error("directories cannot be written")
@@ -47,44 +45,28 @@ extends AbstractFile {
def lookupNameUnchecked(name: String, directory: Boolean): AbstractFile =
throw new UnsupportedOperationException()
- private val files = mut.Map.empty[String, AbstractFile]
+ private val files = mutable.Map.empty[String, AbstractFile]
// the toList is so that the directory may continue to be
// modified while its elements are iterated
def iterator = files.valuesIterator.toList.iterator
- override def lookupName(name: String, directory: Boolean): AbstractFile = {
- files.get(name) match {
- case None => null
- case Some(file) =>
- if (file.isDirectory == directory)
- file
- else
- null
- }
- }
+ override def lookupName(name: String, directory: Boolean): AbstractFile =
+ files get name filter (_.isDirectory == directory) orNull
- override def fileNamed(name: String): AbstractFile = {
- val existing = lookupName(name, false)
- if (existing == null) {
+ override def fileNamed(name: String): AbstractFile =
+ Option(lookupName(name, false)) getOrElse {
val newFile = new VirtualFile(name, path+'/'+name)
files(name) = newFile
newFile
- } else {
- existing
}
- }
- override def subdirectoryNamed(name: String): AbstractFile = {
- val existing = lookupName(name, true)
- if (existing == null) {
+ override def subdirectoryNamed(name: String): AbstractFile =
+ Option(lookupName(name, true)) getOrElse {
val dir = new VirtualDirectory(name, Some(this))
files(name) = dir
dir
- } else {
- existing
}
- }
def clear() {
files.clear();
diff --git a/src/compiler/scala/tools/nsc/io/VirtualFile.scala b/src/compiler/scala/tools/nsc/io/VirtualFile.scala
index 3881cdce1b..1c8f132d36 100644
--- a/src/compiler/scala/tools/nsc/io/VirtualFile.scala
+++ b/src/compiler/scala/tools/nsc/io/VirtualFile.scala
@@ -9,14 +9,15 @@ package scala.tools.nsc
package io
import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, InputStream, OutputStream, File }
+import PartialFunction._
/** This class implements an in-memory file.
*
* @author Philippe Altherr
* @version 1.0, 23/03/2004
*/
-class VirtualFile(val name: String, _path: String) extends AbstractFile {
-
+class VirtualFile(val name: String, _path: String) extends AbstractFile
+{
assert((name ne null) && (path ne null), name + " - " + path)
//########################################################################
@@ -32,11 +33,7 @@ class VirtualFile(val name: String, _path: String) extends AbstractFile {
def this(name: String) = this(name, name)
override def hashCode = name.hashCode
- override def equals(that : Any) = that match {
- case that : VirtualFile => name == that.name
- case _ => false
- }
-
+ override def equals(that: Any) = cond(that) { case x: VirtualFile => x.name == name }
//########################################################################
// Private data