-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIOUtil.scala
81 lines (66 loc) · 1.99 KB
/
IOUtil.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
trait Logger {
// throw on exception on error
var STRICT = true
// print an important message
def message(s: => String) = {
print(Console.RED)
println(s)
print(Console.RESET)
}
// print an error
def error(s: => String) = {
val elts = Thread.currentThread().getStackTrace()
var caller = elts(3)
message("Error occurred in " + caller.getMethodName + " at " + caller.getLineNumber)
println(s)
message("End of error message")
if (STRICT)
throw new RuntimeException("strict mode")
null
}
def print(s: => Any) = Console.out.print(s.toString)
def println(s: => Any) = Console.out.println(s.toString)
}
object GraphViz {
type Graph[V, L] = List[(V, L, V)]
import java.io.{PrintStream, File}
def clean(s: Any) =
s.toString.replace("\n", "\\l").replace("\"", "'") + "\\l"
def write[V, L](g: Graph[V, L], out: PrintStream) {
out println "digraph tmp {";
out println " node [shape=box] "
for ((from, l, to) <-g)
out.println("\"" + clean(from) + "\"->\"" + clean(to) + "\" [label=\"" + l +"\"]");
out println "}";
}
private[this] def makeTempDot = {
// write to a file
val f = File.createTempFile("graph", ".dot");
f.deleteOnExit;
println("created dot file " + f.getAbsolutePath)
f;
}
private[this] def executeDot(in: File) = {
// write to a png file
val out = File.createTempFile("graph", ".svg");
out.deleteOnExit;
val dot = Runtime.getRuntime.exec("dot -Tsvg -o " + out.getAbsolutePath + " " + in.getAbsolutePath);
if (dot.waitFor != 0)
println("dot failed to produce: " + out.getAbsolutePath);
out
}
private[this] def showDot(out: File) {
Runtime.getRuntime.exec("eog " + out.getAbsolutePath);
}
def createDot[V, L](g: Graph[V, L]) = {
val f = makeTempDot;
val out = new PrintStream(f);
write(g, out);
out.flush;
executeDot(f);
}
def display[V, L](g: Graph[V, L]) {
val f = createDot(g);
showDot(f);
}
}