summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/util/ArgumentsExpander.scala
blob: 68aa3479d218e4ed74a5b28480849a631dd78f6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package scala.tools.nsc.util

import java.io.{FileReader, BufferedReader, StreamTokenizer, FileNotFoundException}
import scala.tools.nsc.io.AbstractFile
import scala.collection.mutable.ListBuffer

/**
 * Expands all arguments starting with @ to the contents of the
 * file named like each argument.
 */
object ArgumentsExpander {

  def expandArg(arg: String): List[String] =
    expandFromFile(arg.substring(1))

  /*
   * Extracts all the arguments in a specified file.
   * Throws FileNotFoundException if the file does not exist.
   */
  private def expandFromFile(fileName: String): List[String]  = {
    val f = AbstractFile.getFile(fileName)
    if (f eq null) throw new FileNotFoundException(
      "argument file "+ fileName +" could not be found")

    val in = new BufferedReader(new FileReader(f.file))

    val tokenizer = new StreamTokenizer( in )
    tokenizer.resetSyntax
    tokenizer.wordChars(' ', 255)
    tokenizer.whitespaceChars(0, ' ')
    tokenizer.commentChar('#')
    tokenizer.quoteChar('"')
    tokenizer.quoteChar('\'')

    val ts = new ListBuffer[String]
    while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
      ts += tokenizer.sval
    }
    in.close()
    ts.toList
  }
}