Skip to content

Commit

Permalink
Working version. Writer in scala.
Browse files Browse the repository at this point in the history
  • Loading branch information
rinmalavi committed Feb 15, 2012
1 parent 631755b commit 1870205
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 39 deletions.
12 changes: 6 additions & 6 deletions csvs/test1.csv

Large diffs are not rendered by default.

46 changes: 41 additions & 5 deletions doit-csv/src/main/scala/hr/element/doit/csv/CSVWriter.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hr.element.doit.csv
import java.io.Writer
import scala.annotation.tailrec

class CSVWriter(config: CSVFactory, w: Writer) {

Expand All @@ -14,14 +15,49 @@ class CSVWriter(config: CSVFactory, w: Writer) {
w.write(config.newLine)
}

// def parse2(l: String){
// val quoteNum =
// while()
// }
def parse2(l: String) ={
var quoteCount = 0
var valuePivot = config.quotes.length()
@tailrec
def count(lastPivot: Int){
val valuePivot = l.indexOf(config.quotes,
lastPivot + config.quotes.length());
if(valuePivot == -1) {}
else{
quoteCount += 1
count(valuePivot)
}
}
count(-config.quotes.length)
if( Seq(config.newLine,
config.delimiter)
.exists(l.contains)||(quoteCount != 0)){
val quoteLen = config.quotes.length
val newValueLength = l.length +(quoteCount + 2) * quoteLen
val newValue = new StringBuilder()
newValue append config.quotes
def buildStringFrom(i: Int) {
val oldValHead = if (i < 0) 0 else i
val oldValTail = l.indexOf(config.quotes,
i + config.quotes.length)
if (oldValTail == -1){
newValue append l.drop(oldValHead)
} else {
newValue append l.take(oldValTail).drop(oldValHead)
newValue append config.quotes
buildStringFrom(oldValTail)
}
}
buildStringFrom(-config.quotes.length)
newValue append config.quotes
newValue result
} else l
}



def parse(l: String) = {
if( List(config.quotes,
if( Seq(config.quotes,
config.newLine,
config.delimiter)
.exists(l.contains)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private String getStringToWrite(final String value) {
int quoteCount = 0; {
final boolean hasNewLine = -1 != value.indexOf(config.newLine);
final boolean hasDelimiter = -1 != value.indexOf(config.delimiter);
int valuePivot = -1;
int valuePivot = -config.quotes.length();
while(true){
valuePivot = value.indexOf(config.quotes, valuePivot + config.quotes.length());
if(valuePivot == -1) break ;
Expand Down Expand Up @@ -61,7 +61,12 @@ private String getStringToWrite(final String value) {
oldValueTailPivot + quoteLength);

if (oldValueTailPivot == -1) {
System.arraycopy(oldValue, oldValueHeadPivot, newValue, newValuePivot, oldValue.length - oldValueHeadPivot);
System.arraycopy(
oldValue,
oldValueHeadPivot,
newValue,
newValuePivot,
oldValue.length - oldValueHeadPivot);
break;
}

Expand Down
8 changes: 1 addition & 7 deletions doit-csv/src/main/scala/hr/element/doit/csv/LineReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,7 @@ class LineReader(config: CSVFactory, reader: Reader) extends Traversable[String]
}
}

// def checkvalid(mode: SmrModet, smr: Smr){
// (mode, smr) match {
// case (StartMode, x) => startMode(x)
// case (VerboseMode, x) => verboseMode(x)
// case (QuotedMode,x) => quotedMode(x)
// }
// }

val words: List[String] = {

val res = new ArrayBuffer[String]
Expand Down
11 changes: 1 addition & 10 deletions doit-csv/src/main/scala/hr/element/doit/csv/SlidingMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class CyclicCharacterMatcher(
clone()

var writePoint = 0
def consume(read: Char, mode: (Smr => ModeCase)) = { // should return error if buffer is not empty after special cases
def consume(read: Char, mode: (Smr => ModeCase)) = {
val modedMatchers = rH.filter(x => mode(x.matchMsg) != Ignore)
val bufflength = modedMatchers.maxBy(_.length).length
last(writePoint) = read
Expand Down Expand Up @@ -147,15 +147,6 @@ class CyclicCharacterMatcher(
last.drop(head) ++ last.take(tail)
else
last.drop(head).take(tail - head)
// val tail = (writePoint + last.length -1 ) % last.length
// val head = (tail - buffTake + last.length ) % last.length
// buffTake = 0
// writePoint = 0
//
// if ( head > tail )
// last.drop(head).take(tail)
// else
// last.drop(head + 1 ).take(tail - head)
}
}

Expand Down
23 changes: 14 additions & 9 deletions doit-csv/src/test/scala/hr/element/doit/csv/Test1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,21 @@ class ExampleSuite extends GivenWhenThen


for (i <- startCase to endCase) {
val delimiter = (i * 127).toString()
val newLine = (i * 25).toString()
val quotes = (i * 3).toString()
val t= new Random( i - 1)
// val delimiter = t.nextString(i / 50)
// val newLine = t.nextString(i / 60)
// val quotes = t.nextString(i / 70)
val delimiter = ( 50).toString
val newLine = ( 60).toString
val quotes = ( 170).toString
//info("deli: " + delimiter + ", nL" + newLine + ", q " + quotes)
if (delimiter.contains(quotes) ||
delimiter.contains(newLine) ||
quotes.contains(newLine) ||
quotes.contains(delimiter) ||
newLine.contains(quotes) ||
newLine.contains(delimiter)) {}
newLine.contains(delimiter)) {
}
else {
val factory = CSVFactory.factory().
setDelimiter(delimiter).
Expand Down Expand Up @@ -87,8 +92,8 @@ class ExampleSuite extends GivenWhenThen
val reader = factory.getReader(new FileInputStream(f))
while (reader.hasNext()) {
val lr = reader.next()
/*lr.foreach(
x => {
lr.foreach{
x =>
val str = new StringBuilder(
r.nextString(r.nextInt().abs % strSize + 4))
val l = str.length
Expand All @@ -112,11 +117,11 @@ class ExampleSuite extends GivenWhenThen
println(",|"+str.result+"|"+str.length())
}
}*/
}
})*/
}
info(" time: " + (System.currentTimeMillis() - time))
}}

info(" time: " + (System.currentTimeMillis() - time))
}
}
}
Expand Down

0 comments on commit 1870205

Please sign in to comment.