diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 5d099759c9..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. * @@ -6134,6 +6142,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, ',')); @@ -6159,6 +6170,40 @@ public Table loadTable(String filename, String options) { } + /** + * Other version with a 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 + + 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; + } + + // call a other constructor of Table + return new Table(input, optionStr, delimiter); + + } catch (IOException e) { + printStackTrace(e); + return null; + } + } + + /** * @webref output:files * @param table the Table object to save to a file diff --git a/core/src/processing/data/Table.java b/core/src/processing/data/Table.java index 4d18651cc7..08a970f96a 100644 --- a/core/src/processing/data/Table.java +++ b/core/src/processing/data/Table.java @@ -98,6 +98,8 @@ public class Table { // each expansion. protected int expandIncrement; + // free delimiter - standard is the comma-seperator + protected char delimiter = ','; /** * Creates a new, empty table. Use addRow() to add additional rows. @@ -127,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 */ @@ -135,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: *