Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Applied CSVFormat to CSVWriter too.

  • Loading branch information...
commit 15e1543d77ab72105cd5eade1c41a347f0123c52 1 parent 23a0a38
@tototoshi authored
View
5 src/main/scala/com/github/tototoshi/csv/CSVFormat.scala
@@ -20,9 +20,9 @@ trait CSVFormat {
val separator: Char
- val quote: Char
+ val quoteChar: Char
- val escape: Char
+ val escapeChar: Char
val numberOfLinesToSkip: Int
@@ -30,4 +30,5 @@ trait CSVFormat {
val ignoreLeadingWhiteSpace: Boolean
+ val lineEnd: String
}
View
6 src/main/scala/com/github/tototoshi/csv/CSVReader.scala
@@ -24,7 +24,7 @@ import au.com.bytecode.opencsv
class CSVReader protected (private val underlying: JCSVReader) {
- @deprecated("0.8.0")
+ @deprecated("No longer supported", "0.8.0")
def apply[A](f: Iterator[Seq[String]] => A): A = {
try {
f(this.iterator)
@@ -83,13 +83,13 @@ object CSVReader {
val DEFAULT_ENCODING = "UTF-8"
@deprecated("Use #open instead", "0.5.0")
- def apply(file: File, encoding: String = "UTF-8"): CSVReader = open(file, encoding)
+ def apply(file: File, encoding: String = "UTF-8"): CSVReader = open(file, encoding)(defaultCSVFormat)
@deprecated("Use #open instead", "0.5.0")
def apply(reader: Reader): CSVReader = open(reader)(defaultCSVFormat)
def open(reader: Reader)(implicit format: CSVFormat): CSVReader =
- new CSVReader(new JCSVReader(reader, format.separator, format.quote, format.numberOfLinesToSkip))
+ new CSVReader(new JCSVReader(reader, format.separator, format.quoteChar, format.numberOfLinesToSkip))
def open(file: File)(implicit format: CSVFormat): CSVReader = {
open(file, this.DEFAULT_ENCODING)(format)
View
28 src/main/scala/com/github/tototoshi/csv/CSVWriter.scala
@@ -24,6 +24,7 @@ class CSVWriter protected (writer: Writer) {
private val underlying: JCSVWriter = new JCSVWriter(writer)
+ @deprecated("No longer supported","0.8.0")
def apply[A](f: CSVWriter => A): A = {
try {
f(this)
@@ -54,30 +55,33 @@ class CSVWriter protected (writer: Writer) {
object CSVWriter {
@deprecated("Use #open instead", "0.5.0")
- def apply(file: File, encoding: String = "UTF-8"): CSVWriter = open(file, false, encoding)
+ def apply(file: File, encoding: String = "UTF-8")(implicit format: CSVFormat): CSVWriter =
+ open(file, false, encoding)(defaultCSVFormat)
@deprecated("Use #open instead", "0.5.0")
- def apply(writer: Writer): CSVWriter = open(writer)
+ def apply(writer: Writer): CSVWriter = open(writer)(defaultCSVFormat)
- def open(writer: Writer): CSVWriter = new CSVWriter(writer)
+ def open(writer: Writer)(implicit format: CSVFormat): CSVWriter =
+ new CSVWriter(writer)
- def open(file: File): CSVWriter = open(file, false, "UTF-8")
+ def open(file: File)(implicit format: CSVFormat): CSVWriter = open(file, false, "UTF-8")(format)
- def open(file: File, encoding: String): CSVWriter = open(file, false, encoding)
+ def open(file: File, encoding: String)(implicit format: CSVFormat): CSVWriter = open(file, false, encoding)(format)
- def open(file: File, append: Boolean): CSVWriter = open(file, append, "UTF-8")
+ def open(file: File, append: Boolean)(implicit format: CSVFormat): CSVWriter = open(file, append, "UTF-8")(format)
- def open(file: File, append: Boolean, encoding: String): CSVWriter = {
+ def open(file: File, append: Boolean, encoding: String)(implicit format: CSVFormat): CSVWriter = {
val fos = new FileOutputStream(file, append)
val writer = new OutputStreamWriter(fos, encoding)
- open(writer)
+ open(writer)(format)
}
- def open(file: String): CSVWriter = open(file, false, "UTF-8")
+ def open(file: String)(implicit format: CSVFormat): CSVWriter = open(file, false, "UTF-8")(format)
- def open(file: String, encoding: String): CSVWriter = open(file, false, encoding)
+ def open(file: String, encoding: String)(implicit format: CSVFormat): CSVWriter = open(file, false, encoding)(format)
- def open(file: String, append: Boolean): CSVWriter = open(file, append, "UTF-8")
+ def open(file: String, append: Boolean)(implicit format: CSVFormat): CSVWriter = open(file, append, "UTF-8")(format)
- def open(file: String, append: Boolean, encoding: String): CSVWriter = open(new File(file), append, encoding)
+ def open(file: String, append: Boolean, encoding: String)(implicit format: CSVFormat): CSVWriter =
+ open(new File(file), append, encoding)(format)
}
View
8 src/main/scala/com/github/tototoshi/csv/DefaultCSVFormat.scala
@@ -15,15 +15,15 @@
*/
package com.github.tototoshi.csv
-import au.com.bytecode.opencsv.{ CSVParser, CSVReader => JCSVReader }
+import au.com.bytecode.opencsv.{ CSVParser, CSVReader => JCSVReader, CSVWriter => JCSVWriter }
trait DefaultCSVFormat extends CSVFormat {
val separator: Char = CSVParser.DEFAULT_SEPARATOR
- val quote: Char = CSVParser.DEFAULT_QUOTE_CHARACTER
+ val quoteChar: Char = CSVParser.DEFAULT_QUOTE_CHARACTER
- val escape: Char = CSVParser.DEFAULT_ESCAPE_CHARACTER
+ val escapeChar: Char = CSVParser.DEFAULT_ESCAPE_CHARACTER
val numberOfLinesToSkip: Int = JCSVReader.DEFAULT_SKIP_LINES
@@ -31,4 +31,6 @@ trait DefaultCSVFormat extends CSVFormat {
val ignoreLeadingWhiteSpace: Boolean = CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE
+ val lineEnd: String = JCSVWriter.DEFAULT_LINE_END
+
}
View
4 src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala
@@ -63,7 +63,7 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
it("should be consutrcted with separators and quotes") {
implicit object format extends DefaultCSVFormat {
override val separator: Char = '#'
- override val quote: Char = '$'
+ override val quoteChar: Char = '$'
}
using (CSVReader.open("src/test/resources/hash-separated-dollar-quote.csv")) { reader => {
@@ -76,7 +76,7 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
it("should be constructed with separators, quotes, and line skipping") {
implicit object format extends DefaultCSVFormat {
override val separator: Char = '#'
- override val quote: Char = '$'
+ override val quoteChar: Char = '$'
override val numberOfLinesToSkip: Int = 3
}
using (CSVReader.open("src/test/resources/beginning-junk-hash-separated-dollar-quote.csv")) { reader => {
View
7 src/test/scala/com/github/tototoshi/csv/CSVWriterSpec.scala
@@ -22,13 +22,6 @@ class CSVWriterSpec extends FunSpec with ShouldMatchers with BeforeAndAfter with
describe("CSVWriter") {
describe ("#apply") {
- it ("should provide loan pattern") {
- CSVWriter.open("test.csv") { writer =>
- writer.writeRow(List(1, 2, 3))
- writer.writeRow(List(4, 5, 6))
- }
- readFileAsString("test.csv") should be ("\"1\",\"2\",\"3\"\n\"4\",\"5\",\"6\"\n")
- }
it ("should close csv writer") {
val writer = CSVWriter.open("test.csv")
writer(w => ())
Please sign in to comment.
Something went wrong with that request. Please try again.