support for CSV files;
authorwenzelm
Thu Mar 01 20:05:41 2018 +0100 (2018-03-01)
changeset 677378af6fcdc869d
parent 67736 65016740d3e0
child 67738 1bbe618c4b24
support for CSV files;
src/Pure/General/csv.scala
src/Pure/build-jars
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/General/csv.scala	Thu Mar 01 20:05:41 2018 +0100
     1.3 @@ -0,0 +1,33 @@
     1.4 +/*  Title:      Pure/General/csv.scala
     1.5 +    Author:     Makarius
     1.6 +
     1.7 +Support for CSV: RFC 4180.
     1.8 +*/
     1.9 +
    1.10 +package isabelle
    1.11 +
    1.12 +
    1.13 +object CSV
    1.14 +{
    1.15 +  def print_field(field: Any): String =
    1.16 +  {
    1.17 +    val str = field.toString
    1.18 +    if (str.exists("\",\r\n".contains(_))) {
    1.19 +      (for (c <- str) yield { if (c == '"') "\"\"" else c.toString }).mkString("\"", "", "\"")
    1.20 +    }
    1.21 +    else str
    1.22 +  }
    1.23 +
    1.24 +  sealed case class Record(fields: Any*)
    1.25 +  {
    1.26 +    override def toString: String = fields.iterator.map(print_field(_)).mkString(",")
    1.27 +  }
    1.28 +
    1.29 +  sealed case class File(name: String, header: List[String], records: List[Record])
    1.30 +  {
    1.31 +    override def toString: String = (Record(header:_*) :: records).mkString("\r\n")
    1.32 +
    1.33 +    def file_name: String = name + ".csv"
    1.34 +    def write(dir: Path) { isabelle.File.write(dir + Path.explode(file_name), toString) }
    1.35 +  }
    1.36 +}
     2.1 --- a/src/Pure/build-jars	Thu Mar 01 15:02:45 2018 +0100
     2.2 +++ b/src/Pure/build-jars	Thu Mar 01 20:05:41 2018 +0100
     2.3 @@ -46,6 +46,7 @@
     2.4    General/codepoint.scala
     2.5    General/comment.scala
     2.6    General/completion.scala
     2.7 +  General/csv.scala
     2.8    General/date.scala
     2.9    General/exn.scala
    2.10    General/file.scala