-
Notifications
You must be signed in to change notification settings - Fork 18
/
InputFile.scala
50 lines (43 loc) · 1.73 KB
/
InputFile.scala
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
43
44
45
46
47
48
49
50
package gapt.formats
import java.io.InputStream
import ammonite.ops._
import scala.io.Codec
import scala.sys.process
trait InputFile {
def read: String
def fileName: String
}
object InputFile {
implicit def fromPath( file: FilePath ): OnDiskInputFile = OnDiskInputFile( Path( file, pwd ) )
def fromString( content: String ) = StringInputFile( content )
implicit def fromJavaFile( file: java.io.File ): OnDiskInputFile = OnDiskInputFile( Path( file ) )
implicit def fromInputStream( stream: java.io.InputStream ): InputFile =
StringInputFile( readStream( stream ) )
implicit def fromFileName( fileName: String ): InputFile = fromPath( FilePath( fileName ) )
def readStream( stream: InputStream ): String =
scala.io.Source.fromInputStream( stream )( Codec.UTF8 ).mkString
}
case class StringInputFile( content: String ) extends InputFile {
def fileName = "<string>"
def read = content
}
case class OnDiskInputFile( file: Path ) extends InputFile {
def fileName = file.toString
def read = ammonite.ops.read ! file
}
case class StdinInputFile( content: String ) extends InputFile {
def fileName = "<stdin>"
def read = content
}
object StdinInputFile {
def apply(): StdinInputFile = StdinInputFile( InputFile.readStream( process.stdin ) )
}
case class ClasspathInputFile( fileName: String, classLoader: ClassLoader ) extends InputFile {
def read = InputFile.readStream( classLoader.getResourceAsStream( fileName ) )
}
object ClasspathInputFile {
def apply( fileName: String ): ClasspathInputFile =
ClasspathInputFile( fileName, Thread.currentThread.getContextClassLoader )
def apply( fileName: String, relativeToClass: Class[_] ): ClasspathInputFile =
ClasspathInputFile( fileName, relativeToClass.getClassLoader )
}