(String line, BufferedReader reader)
| 566 | int start; //, stop; |
| 567 | |
| 568 | String[] handle(String line, BufferedReader reader) throws IOException { |
| 569 | // PApplet.println("handle() called for: " + line); |
| 570 | start = 0; |
| 571 | pieceCount = 0; |
| 572 | c = line.toCharArray(); |
| 573 | |
| 574 | // get tally of number of columns and allocate the array |
| 575 | int cols = 1; // the first comma indicates the second column |
| 576 | boolean quote = false; |
| 577 | for (int i = 0; i < c.length; i++) { |
| 578 | if (!quote && (c[i] == ',')) { |
| 579 | cols++; |
| 580 | } else if (c[i] == '\"') { |
| 581 | // double double quotes (escaped quotes like "") will simply toggle |
| 582 | // this back and forth, so it should remain accurate |
| 583 | quote = !quote; |
| 584 | } |
| 585 | } |
| 586 | pieces = new String[cols]; |
| 587 | |
| 588 | // while (offset < c.length) { |
| 589 | // start = offset; |
| 590 | while (start < c.length) { |
| 591 | boolean enough = ingest(); |
| 592 | while (!enough) { |
| 593 | // found a newline inside the quote, grab another line |
| 594 | String nextLine = reader.readLine(); |
| 595 | // System.out.println("extending to " + nextLine); |
| 596 | if (nextLine == null) { |
| 597 | // System.err.println(line); |
| 598 | throw new IOException("Found a quoted line that wasn't terminated properly."); |
| 599 | } |
| 600 | // for simplicity, not bothering to skip what's already been read |
| 601 | // from c (and reset the offset to 0), opting to make a bigger array |
| 602 | // with both lines. |
| 603 | char[] temp = new char[c.length + 1 + nextLine.length()]; |
| 604 | PApplet.arrayCopy(c, temp, c.length); |
| 605 | // NOTE: we're converting to \n here, which isn't perfect |
| 606 | temp[c.length] = '\n'; |
| 607 | nextLine.getChars(0, nextLine.length(), temp, c.length + 1); |
| 608 | // c = temp; |
| 609 | return handle(new String(temp), reader); |
| 610 | //System.out.println(" full line is now " + new String(c)); |
| 611 | //stop = nextComma(c, offset); |
| 612 | //System.out.println("stop is now " + stop); |
| 613 | //enough = ingest(); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | // Make any remaining entries blanks instead of nulls. Empty columns from |
| 618 | // CSV are always "" not null, so this handles successive commas in a line |
| 619 | for (int i = pieceCount; i < pieces.length; i++) { |
| 620 | pieces[i] = ""; |
| 621 | } |
| 622 | // PApplet.printArray(pieces); |
| 623 | return pieces; |
| 624 | } |
| 625 |
no test coverage detected