From 096909ff909f7d85f304d4cd1f88f9b188ae69a2 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Sat, 30 Jun 2018 13:26:42 +0200 Subject: [PATCH 1/3] added optional delimiter choice --- core/src/processing/core/PApplet.java | 31 +++++++++++++++++++++++++++ core/src/processing/data/Table.java | 24 +++++++++++++++------ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 5d099759c9..712db92f99 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -6134,6 +6134,9 @@ public Table loadTable(String filename) { * @param options may contain "header", "tsv", "csv", or "bin" separated by commas */ public Table loadTable(String filename, String options) { + + // System.out.printf("DEBUG: class: PApplet loadTable(String, String) filename=%s, options=%s\n",filename, options); //DEBUG + try { String optionStr = Table.extensionOptions(true, filename, options); String[] optionList = trim(split(optionStr, ',')); @@ -6158,6 +6161,34 @@ public Table loadTable(String filename, String options) { } } + public Table loadTable(String filename, String options, char delimiter) { + + //System.out.printf("DEBUG: class: PApplet loadTable(String, String) filename=%s, options=%s\n",filename, options); //DEBUG + + try { + String optionStr = Table.extensionOptions(true, filename, options); + String[] optionList = trim(split(optionStr, ',')); + + Table dictionary = null; + for (String opt : optionList) { + if (opt.startsWith("dictionary=")) { + dictionary = loadTable(opt.substring(opt.indexOf('=') + 1), "tsv"); + return dictionary.typedParse(createInput(filename), optionStr); + } + } + InputStream input = createInput(filename); + if (input == null) { + System.err.println(filename + " does not exist or could not be read"); + return null; + } + return new Table(input, optionStr, delimiter); + + } catch (IOException e) { + printStackTrace(e); + return null; + } + } + /** * @webref output:files diff --git a/core/src/processing/data/Table.java b/core/src/processing/data/Table.java index 4d18651cc7..a1ba04b693 100644 --- a/core/src/processing/data/Table.java +++ b/core/src/processing/data/Table.java @@ -98,6 +98,7 @@ public class Table { // each expansion. protected int expandIncrement; + protected char delimiter = ','; /** * Creates a new, empty table. Use addRow() to add additional rows. @@ -155,6 +156,13 @@ public Table(InputStream input, String options) throws IOException { } + public Table(InputStream input, String options, char delimiter) throws IOException { + this.delimiter = delimiter; + init(); + parse(input, options); + } + + public Table(Iterable rows) { init(); @@ -565,17 +573,21 @@ static class CommaSeparatedLine { // int offset; int start; //, stop; - String[] handle(String line, BufferedReader reader) throws IOException { + char delimiter = ','; + + String[] handle(String line, BufferedReader reader, char delimiter) throws IOException { // PApplet.println("handle() called for: " + line); start = 0; pieceCount = 0; c = line.toCharArray(); + this.delimiter = delimiter; + // get tally of number of columns and allocate the array int cols = 1; // the first comma indicates the second column boolean quote = false; for (int i = 0; i < c.length; i++) { - if (!quote && (c[i] == ',')) { + if (!quote && (c[i] == delimiter)) { cols++; } else if (c[i] == '\"') { // double double quotes (escaped quotes like "") will simply toggle @@ -606,7 +618,7 @@ String[] handle(String line, BufferedReader reader) throws IOException { temp[c.length] = '\n'; nextLine.getChars(0, nextLine.length(), temp, c.length + 1); // c = temp; - return handle(new String(temp), reader); + return handle(new String(temp), reader, delimiter); //System.out.println(" full line is now " + new String(c)); //stop = nextComma(c, offset); //System.out.println("stop is now " + stop); @@ -675,7 +687,7 @@ protected boolean ingest() { hasEscapedQuotes = true; i += 2; - } else if (c[i+1] == ',') { + } else if (c[i+1] == delimiter) { // that was our closing quote, get outta here addPiece(start, i, hasEscapedQuotes); start = i+2; @@ -702,7 +714,7 @@ protected boolean ingest() { throw new RuntimeException("Unterminated quoted field mid-line"); } } - } else if (!quoted && c[i] == ',') { + } else if (!quoted && c[i] == delimiter) { addPiece(start, i, hasEscapedQuotes); start = i+1; return true; @@ -752,7 +764,7 @@ protected String[] splitLineCSV(String line, BufferedReader reader) throws IOExc if (csl == null) { csl = new CommaSeparatedLine(); } - return csl.handle(line, reader); + return csl.handle(line, reader, delimiter); } From e97107df6561793ff38fb807ce24b91024c230dd Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Sat, 30 Jun 2018 13:30:51 +0200 Subject: [PATCH 2/3] added some comments --- core/src/processing/core/PApplet.java | 6 ++++++ core/src/processing/data/Table.java | 2 ++ 2 files changed, 8 insertions(+) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 712db92f99..726872809b 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -6161,6 +6161,10 @@ public Table loadTable(String filename, String options) { } } + + /** + * Same method as above inclusive a optiona delimiter. + */ public Table loadTable(String filename, String options, char delimiter) { //System.out.printf("DEBUG: class: PApplet loadTable(String, String) filename=%s, options=%s\n",filename, options); //DEBUG @@ -6181,6 +6185,8 @@ public Table loadTable(String filename, String options, char delimiter) { System.err.println(filename + " does not exist or could not be read"); return null; } + + // call a other constructor of Table return new Table(input, optionStr, delimiter); } catch (IOException e) { diff --git a/core/src/processing/data/Table.java b/core/src/processing/data/Table.java index a1ba04b693..95a58ed049 100644 --- a/core/src/processing/data/Table.java +++ b/core/src/processing/data/Table.java @@ -98,6 +98,7 @@ public class Table { // each expansion. protected int expandIncrement; + // free delimiter - standard is the comma-seperator protected char delimiter = ','; /** @@ -573,6 +574,7 @@ static class CommaSeparatedLine { // int offset; int start; //, stop; + // free delimiter. standard is comma-seperator char delimiter = ','; String[] handle(String line, BufferedReader reader, char delimiter) throws IOException { From 1782d3ac48eb990d553604e300cab5d7b7a0bab7 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Sat, 30 Jun 2018 13:45:09 +0200 Subject: [PATCH 3/3] small changes --- core/src/processing/core/PApplet.java | 10 +++++++++- core/src/processing/data/Table.java | 18 ++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 726872809b..2c4151b408 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -6121,6 +6121,14 @@ public Table loadTable(String filename) { } + /** + * Other version with a delimiter + */ + public Table loadTable(String filename, char delimiter) { + return loadTable(filename, null, delimiter); + } + + /** * Options may contain "header", "tsv", "csv", or "bin" separated by commas. * @@ -6163,7 +6171,7 @@ public Table loadTable(String filename, String options) { /** - * Same method as above inclusive a optiona delimiter. + * Other version with a delimiter */ public Table loadTable(String filename, String options, char delimiter) { diff --git a/core/src/processing/data/Table.java b/core/src/processing/data/Table.java index 95a58ed049..08a970f96a 100644 --- a/core/src/processing/data/Table.java +++ b/core/src/processing/data/Table.java @@ -129,6 +129,14 @@ public Table(File file, String options) throws IOException { extensionOptions(true, file.getName(), options)); } + public Table(File file, String options, char delimiter) throws IOException { + // uses createInput() to handle .gz (and eventually .bz2) files + this.delimiter = delimiter; + init(); + parse(PApplet.createInput(file), + extensionOptions(true, file.getName(), options)); + } + /** * @nowebref */ @@ -137,6 +145,12 @@ public Table(InputStream input) throws IOException { } + public Table(InputStream input, char delimiter) throws IOException { + this(input, null); + this.delimiter = delimiter; + } + + /** * Read the table from a stream. Possible options include: *
    @@ -1274,7 +1288,7 @@ protected void writeCSV(PrintWriter writer) { if (columnTitles != null) { for (int col = 0; col < getColumnCount(); col++) { if (col != 0) { - writer.print(','); + writer.print(delimiter); } try { if (columnTitles[col] != null) { // col < columnTitles.length && @@ -1291,7 +1305,7 @@ protected void writeCSV(PrintWriter writer) { for (int row = 0; row < rowCount; row++) { for (int col = 0; col < getColumnCount(); col++) { if (col != 0) { - writer.print(','); + writer.print(delimiter); } String entry = getString(row, col); // just write null entries as blanks, rather than spewing 'null'