-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
CSV.scala
67 lines (52 loc) · 1.68 KB
/
CSV.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
// (C) Uri Wilensky. https://github.com/NetLogo/NetLogo
package org.nlogo.api
// This class handles output of CSV files. Input of CSV files is handled elsewhere, in
// ImportLexer. - ST 7/22/03, 2/13/08
import java.util.{ List => JList }
class CSV(dump: Any => String) {
def blank = ""
// this dumps a object in the exporting format:
// --if the object is a string, all double quotes are doubled and escape
// chars properly escaped.
// --all objects are returned as a string surrounded by double quotes.
def data(obj: Any) =
encode(dump(obj))
def number(d: Double) =
encode(dump(d: java.lang.Double))
def number(i: Int) =
encode(dump(i: java.lang.Integer))
def dataRow[T](objs: Array[T]) =
objs.map(data).mkString(",")
def header(s: String) =
encode(s)
def headerRow(strings: Array[String]) =
strings.map(encode).mkString(",")
def encode(s: String) = {
def escape(c: Char) =
if(c == '"') "\"\"" else c.toString
val ss = Option(s).getOrElse("null")
'"' + ss.flatMap(escape) + '"'
}
def stringToCSV(writer: java.io.PrintWriter, text: String) {
val CellWidth = 10000
val MaxColumns = 2
var i = 0
while(i < text.size) {
var line = new StringBuilder
var k = 0
while(k < MaxColumns && i < text.size) {
val end = (i + CellWidth) min text.size
line ++= text.substring(i, end)
i += CellWidth
k += 1
if (i < text.size && k < MaxColumns)
line += ','
}
writer.println(data(line.toString))
}
}
def variableNameRow(v: JList[String]): String = {
import collection.JavaConverters._
v.asScala.map(encode(_).toLowerCase).mkString(",")
}
}