diff options
author | Paul Phillips <paulp@improving.org> | 2009-06-24 20:54:18 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-06-24 20:54:18 +0000 |
commit | 24781591253de7a9f54a3fc7bcf6ae4b3e969098 (patch) | |
tree | f9b63c0fd117c2da79c378cbb8eda2869274d326 /src | |
parent | f537546d8b32d06e9d9a921a47bb75f1b1f7ecfa (diff) | |
download | scala-24781591253de7a9f54a3fc7bcf6ae4b3e969098.tar.gz scala-24781591253de7a9f54a3fc7bcf6ae4b3e969098.tar.bz2 scala-24781591253de7a9f54a3fc7bcf6ae4b3e969098.zip |
Skeletal implementation of scala.io.File.
Separated default source code encoding (the one specified by -encoding)
from default system encoding, because otherwise you can't compile scala
with the defaults on OSX.
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/Global.scala | 2 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/Settings.scala | 2 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/doc/DocUtil.scala | 3 | ||||
-rw-r--r-- | src/dotnet-library/scala/io/File.scala | 1 | ||||
-rw-r--r-- | src/library/scala/collection/Iterable.scala | 2 | ||||
-rw-r--r-- | src/library/scala/io/BufferedSource.scala | 4 | ||||
-rw-r--r-- | src/library/scala/io/Codec.scala | 13 | ||||
-rw-r--r-- | src/library/scala/io/File.scala | 76 | ||||
-rw-r--r-- | src/library/scala/io/Source.scala | 11 | ||||
-rw-r--r-- | src/library/scala/util/Properties.scala | 16 | ||||
-rw-r--r-- | src/partest/scala/tools/partest/utils/Properties.scala | 3 |
11 files changed, 113 insertions, 20 deletions
diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala index 76507271c0..2f28468f3c 100644 --- a/src/compiler/scala/tools/nsc/Global.scala +++ b/src/compiler/scala/tools/nsc/Global.scala @@ -191,7 +191,7 @@ class Global(var settings: Settings, var reporter: Reporter) extends SymbolTable private val reader: SourceReader = { def stdCharset: Charset = { - settings.encoding.value = Properties.encodingString // A mandatory charset + settings.encoding.value = Properties.sourceEncoding // A mandatory charset Charset.forName(settings.encoding.value) } val charset = diff --git a/src/compiler/scala/tools/nsc/Settings.scala b/src/compiler/scala/tools/nsc/Settings.scala index 20f16c785e..f0515c27d3 100644 --- a/src/compiler/scala/tools/nsc/Settings.scala +++ b/src/compiler/scala/tools/nsc/Settings.scala @@ -707,7 +707,7 @@ trait ScalacSettings { val outdir = OutputSetting (outputDirs, ".") val dependenciesFile = StringSetting ("-dependencyfile", "file", "Specify the file in which dependencies are tracked", ".scala_dependencies") val deprecation = BooleanSetting ("-deprecation", "Output source locations where deprecated APIs are used") - val encoding = StringSetting ("-encoding", "encoding", "Specify character encoding used by source files", Properties.encodingString) + val encoding = StringSetting ("-encoding", "encoding", "Specify character encoding used by source files", Properties.sourceEncoding) val explaintypes = BooleanSetting ("-explaintypes", "Explain type errors in more detail") val extdirs = StringSetting ("-extdirs", "dirs", "Override location of installed extensions", extdirsDefault) val debuginfo = DebugSetting ("-g", "Specify level of generated debugging info", List("none", "source", "line", "vars", "notailcalls"), "vars", "vars") diff --git a/src/compiler/scala/tools/nsc/doc/DocUtil.scala b/src/compiler/scala/tools/nsc/doc/DocUtil.scala index ad2c4ca521..7c976472d3 100644 --- a/src/compiler/scala/tools/nsc/doc/DocUtil.scala +++ b/src/compiler/scala/tools/nsc/doc/DocUtil.scala @@ -56,7 +56,8 @@ object DocUtil { (<a href={href}>{t0}</a>); } - val encoding = Properties.encodingString + // can't use platform default here or the generated XML may end up all MacRoman + val encoding = Properties.sourceEncoding val generator = System.getProperty("doc.generator", "scaladoc (" + Properties.versionString + ")") val header = (<meta http-equiv="content-type" content={"text/html; charset=" + encoding}/> diff --git a/src/dotnet-library/scala/io/File.scala b/src/dotnet-library/scala/io/File.scala new file mode 100644 index 0000000000..e57b4fb979 --- /dev/null +++ b/src/dotnet-library/scala/io/File.scala @@ -0,0 +1 @@ +/* File.scala does not exist for the dotnet target */
\ No newline at end of file diff --git a/src/library/scala/collection/Iterable.scala b/src/library/scala/collection/Iterable.scala index 826248bf2a..9975e45bec 100644 --- a/src/library/scala/collection/Iterable.scala +++ b/src/library/scala/collection/Iterable.scala @@ -25,7 +25,7 @@ import generic._ * all elements. Subclasses of `Iterable` should re-implement `foreach` with * something more efficient, if possible. * - * This trait adds methods `elements`, `zip`, `zipAll`, `zipWithIndex`, `sameElements`, + * This trait adds methods `iterator`, `zip`, `zipAll`, `zipWithIndex`, `sameElements`, * `takeRight`, `dropRight` to the methods inherited from trait `Traversable`. * * @author Martin Odersky diff --git a/src/library/scala/io/BufferedSource.scala b/src/library/scala/io/BufferedSource.scala index a1dbe1306f..d84138e5fe 100644 --- a/src/library/scala/io/BufferedSource.scala +++ b/src/library/scala/io/BufferedSource.scala @@ -22,9 +22,9 @@ object BufferedSource * using encoding in implicit parameter <code>codec</code>. * * @param inputStream the input stream from which to read - * @param bufferSize buffer size (defaults to BufferedSource.DefaultBufSize) + * @param bufferSize buffer size (defaults to Source.DefaultBufSize) * @param reset a () => Source which resets the stream (defaults to Source.NoReset) - * @param codec (implicit) a scala.io.Codec specifying behavior (defaults to BufferedSource.defaultCodec) + * @param codec (implicit) a scala.io.Codec specifying behavior (defaults to Codec.default) * @return the buffered source */ def fromInputStream( diff --git a/src/library/scala/io/Codec.scala b/src/library/scala/io/Codec.scala index a5e8deada9..b2d8eeaf3b 100644 --- a/src/library/scala/io/Codec.scala +++ b/src/library/scala/io/Codec.scala @@ -12,6 +12,17 @@ package scala.io import java.nio.charset.{ Charset, CharsetDecoder, CodingErrorAction } +// Some notes about encodings for use in refining this implementation. +// +// Emails: encoding recorded in header, e.g. Content-Type: charset= "iso-8859-1" +// HTML: optional content-type meta tag. +// <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> +// XML: optional encoding parameter. +// <?xml version="1.0" encoding="ISO8859-1" ?> +// +// MacRoman vs. UTF-8: see http://jira.codehaus.org/browse/JRUBY-3576 +// -Dfile.encoding: see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4375816 + /** A class for character encoding/decoding preferences. * */ @@ -19,7 +30,7 @@ class Codec(charSet: Charset) { def name = charSet.name def decoder = charSet.newDecoder() - // by default we replace bad chars with the decoder's replacement value (e.g. "?") + // by default we ignore bad characters. // this behavior can be altered by overriding these two methods def malformedAction(): CodingErrorAction = CodingErrorAction.IGNORE def receivedMalformedInput(e: Exception): Char = decoder.replacement()(0) diff --git a/src/library/scala/io/File.scala b/src/library/scala/io/File.scala new file mode 100644 index 0000000000..593bbdc6d6 --- /dev/null +++ b/src/library/scala/io/File.scala @@ -0,0 +1,76 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + +package scala.io + +import java.io.{ FileInputStream, FileOutputStream, File => JFile } + +object File +{ + final val extensionRegex = """^.*\.([^.]+)$""".r + implicit def fileWrapper(file: JFile): File = new File(file) + + def apply(fileName: String) = new File(new JFile(fileName)) + def apply(file: JFile) = new File(file) +} +import File._ + +/** An abstraction for files. For now it is little more than a wrapper + * around java.io.File. + * + * @author Paul Phillips + * @since 2.8 + */ +class File(val file: JFile) extends collection.Iterable[File] +{ + /** If file is a directory, an iterator over its contents. + * If not, an empty iterator. + */ + def iterator: Iterator[File] = + if (file.isDirectory) file.listFiles.iterator map (x => new File(x)) + else Iterator.empty + + /** Deletes the file or directory recursively. Returns false if it failed. + * Use with caution! + */ + def deleteRecursively(): Boolean = deleteRecursively(file) + private def deleteRecursively(f: JFile): Boolean = { + if (f.isDirectory) file.listFiles match { + case null => + case xs => xs foreach deleteRecursively + } + f.delete() + } + + /** Obtains an InputStream. */ + def inputStream = new FileInputStream(file) + + /** Obtains a OutputStream. */ + def outputStream(append: Boolean = false) = new FileOutputStream(file, append) + + /** Attempts to return the file extension. */ + def extension = file.getName match { + case extensionRegex(x) => Some(x) + case _ => None + } + + /** Creates a Source from this file. */ + def toSource = Source.fromFile(file)() + + /** Creates a new File with the specified path appended. */ + def /(child: String) = new File(new JFile(file, child)) + + override def toString() = file.toString + override def equals(other: Any) = other match { + case x: File => this.file == x.file + case _ => false + } + override def hashCode = file.hashCode +} diff --git a/src/library/scala/io/Source.scala b/src/library/scala/io/Source.scala index bd0de4a584..25d9ef644b 100644 --- a/src/library/scala/io/Source.scala +++ b/src/library/scala/io/Source.scala @@ -11,8 +11,7 @@ package scala.io - -import java.io.{ File, FileInputStream, InputStream, PrintStream } +import java.io.{ FileInputStream, InputStream, PrintStream, File => JFile } import java.net.{ URI, URL } /** This object provides convenience methods to create an iterable @@ -64,17 +63,17 @@ object Source { /** creates Source from file with given name, setting * its description to filename. */ - def fromFilename(name: String)(implicit codec: Codec = Codec.default): Source = fromFile(new File(name)) + def fromFilename(name: String)(implicit codec: Codec = Codec.default): Source = fromFile(new JFile(name)) /** creates <code>Source</code> from file with given file: URI */ - def fromURI(uri: URI)(implicit codec: Codec = Codec.default): Source = fromFile(new File(uri)) + def fromURI(uri: URI)(implicit codec: Codec = Codec.default): Source = fromFile(new JFile(uri)) /** Creates Source from <code>file</code>, using given character encoding, * setting its description to filename. Input is buffered in a buffer of * size <code>bufferSize</code>. */ - def fromFile(file: File, bufferSize: Int = DefaultBufSize)(implicit codec: Codec = Codec.default): Source = { + def fromFile(file: JFile, bufferSize: Int = DefaultBufSize)(implicit codec: Codec = Codec.default): Source = { val inputStream = new FileInputStream(file) setFileDescriptor(file, BufferedSource.fromInputStream(inputStream, bufferSize, () => fromFile(file, bufferSize)(codec))) @@ -85,7 +84,7 @@ object Source { * @param s the source whose property we set * @return s */ - private def setFileDescriptor(file: File, source: Source): Source = { + private def setFileDescriptor(file: JFile, source: Source): Source = { source.descr = "file:" + file.getAbsolutePath source } diff --git a/src/library/scala/util/Properties.scala b/src/library/scala/util/Properties.scala index 8f45c2b388..fb4ecbbd90 100644 --- a/src/library/scala/util/Properties.scala +++ b/src/library/scala/util/Properties.scala @@ -47,15 +47,22 @@ private[scala] trait PropertiesTrait def prop(name: String): String = props.getProperty(name, "") def prop(name: String, default: String): String = props.getProperty(name, default) - // XXX file.encoding should not be here, as it causes system setting to - // be ignored. See https://lampsvn.epfl.ch/trac/scala/ticket/1581 - /** The version number of the jar this was loaded from plus "version " prefix, * or "version (unknown)" if it cannot be determined. */ val versionString = "version " + prop("version.number", "(unknown)") val copyrightString = prop("copyright.string", "(c) 2002-2009 LAMP/EPFL") - val encodingString = prop("file.encoding", "UTF8") + + /** This is the encoding to use reading in source files, overridden with -encoding + * Note that it uses "prop" i.e. looks in the scala jar, not the system properties. + */ + val sourceEncoding = prop("file.encoding", "UTF8") + + /** This is the default text encoding, overridden (unreliably) with + * JAVA_OPTS="-Dfile.encoding=Foo" + */ + val encodingString = sysprop("file.encoding", "UTF8") + val isWin = sysprop("os.name") startsWith "Windows" val isMac = sysprop("java.vendor") startsWith "Apple" val javaClassPath = sysprop("java.class.path") @@ -64,6 +71,7 @@ private[scala] trait PropertiesTrait val javaVmVersion = sysprop("java.vm.version") val javaVmInfo = sysprop("java.vm.info") val javaVersion = sysprop("java.version") + val tmpDir = sysprop("java.io.tmpdir") val scalaHome = sysprop("scala.home", null) // XXX places do null checks... // provide a main method so version info can be obtained by running this diff --git a/src/partest/scala/tools/partest/utils/Properties.scala b/src/partest/scala/tools/partest/utils/Properties.scala index 1ab1d47020..d5fc60db65 100644 --- a/src/partest/scala/tools/partest/utils/Properties.scala +++ b/src/partest/scala/tools/partest/utils/Properties.scala @@ -14,7 +14,4 @@ package scala.tools.partest.utils object Properties extends scala.util.PropertiesTrait { protected def propCategory = "partest" protected def pickJarBasedOn = classOf[Application] - - // XXX unlikely it's intentional that only partest uses ISO-8859-1 - override val encodingString = prop("file.encoding", "ISO-8859-1") }
\ No newline at end of file |