diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 402dc3735..ddfb2e022 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -5558,13 +5558,19 @@ public class PApplet extends Applet } - static public XML loadXML(File file) { - try { - return new XML(file); - } catch (Exception e) { - e.printStackTrace(); - return null; - } + // do we want this? for advanced users won't they just use 'new XML'? +// static public XML loadXML(File file) { +// try { +// return new XML(file); +// } catch (Exception e) { +// e.printStackTrace(); +// return null; +// } +// } + + + public Table createTable() { + return new Table(this); } @@ -5576,14 +5582,26 @@ public class PApplet extends Applet * @see PApplet#loadXML(String) */ public Table loadTable(String filename) { - return new Table(this, filename); + return loadTable(filename, null); } - static public Table loadTable(File file) { - return new Table(file); + public Table loadTable(String filename, String options) { + try { + return new Table(this, filename, options); + } catch (IOException e) { + e.printStackTrace(); + return null; + } } + +// static public Table loadTable(File file) { +// return new Table(this, file); +// } + + + ////////////////////////////////////////////////////////////// // FONT I/O @@ -10419,7 +10437,7 @@ public class PApplet extends Applet * Description to come... * * ( end auto-generated from textureWrap.xml ) - * + * * @webref image:textures * @param wrap Either CLAMP (default) or REPEAT */ @@ -10588,7 +10606,7 @@ public class PApplet extends Applet * This is a new reference entry for Processing 2.0. It will be updated shortly. * * ( end auto-generated ) - * + * * @webref Rendering * @param mode the blending mode to use */ @@ -10632,7 +10650,7 @@ public class PApplet extends Applet * This is a new reference entry for Processing 2.0. It will be updated shortly. * * ( end auto-generated ) - * + * * @webref rendering:shaders * @param fragFilename name of fragment shader file */ @@ -10655,7 +10673,7 @@ public class PApplet extends Applet * This is a new reference entry for Processing 2.0. It will be updated shortly. * * ( end auto-generated ) - * + * * @webref rendering:shaders * @param shader name of shader file */ @@ -10680,7 +10698,7 @@ public class PApplet extends Applet * This is a new reference entry for Processing 2.0. It will be updated shortly. * * ( end auto-generated ) - * + * * @webref rendering:shaders */ public void resetShader() { @@ -11561,9 +11579,9 @@ public class PApplet extends Applet /** - * + * * @param level either 2, 4, or 8 - */ + */ public void smooth(int level) { if (recorder != null) recorder.smooth(level); g.smooth(level); diff --git a/core/src/processing/data/Table.java b/core/src/processing/data/Table.java index c8dc79d2c..b921b9b5f 100644 --- a/core/src/processing/data/Table.java +++ b/core/src/processing/data/Table.java @@ -71,7 +71,8 @@ public class Table { // protected boolean skipEmptyRows = true; // protected boolean skipCommentLines = true; - protected boolean commaSeparatedValues = false; + protected String extension = null; +// protected boolean commaSeparatedValues = false; protected boolean awfulCSV = false; protected String missingString = null; @@ -119,20 +120,22 @@ public class Table { * Creates a new, empty table. Use addRow() to add additional rows. */ public Table() { - columns = new Object[0]; - columnTypes = new int[0]; - columnCategories = new HashMapBlows[0]; + init(null); } - public Table(PApplet parent) { - this(); - this.sketch = parent; + public Table(PApplet sketch) { + init(sketch); } - public Table(File file) { - this(PApplet.createReader(file)); +// public Table(File file) { +// this(PApplet.createReader(file)); +// } + + + public Table(PApplet parent, String filename) throws IOException { + this(parent, filename, null); } @@ -140,37 +143,86 @@ public class Table { * Can handle TSV or CSV files. * @param parent * @param filename + * @throws IOException */ - public Table(PApplet parent, String filename) { - this.sketch = parent; - read(parent.createReader(filename)); + public Table(PApplet parent, String filename, String options) throws IOException { + init(parent); + //String[] opts = PApplet.split(options, ','); + //PApplet.trim(opts); + + // try to determine file type from extension + String extension = null; + int dotIndex = filename.lastIndexOf('.'); + if (dotIndex != -1) { + extension = filename.substring(dotIndex + 1).toLowerCase(); + if (!extension.equals("csv") && + !extension.equals("tsv")) { + // ignore extension + extension = null; + } + } + + String[] opts = null; + if (options != null) { + opts = options.split("\\s*,\\s*"); +// PApplet.println("options:"); +// PApplet.println(opts); + + for (String opt : opts) { + if (opt.equals("tsv")) { + extension = "tsv"; + } else if (opt.equals("csv")) { + extension = "csv"; + } else if (opt.equals("newlines")) { + awfulCSV = true; + } else { + System.err.println(opt + " is not a valid option for loading a Table"); + } + } + } + + BufferedReader reader = parent.createReader(filename); + if (awfulCSV) { + parseAwfulCSV(reader); + } else if ("tsv".equals(extension)) { + parseBasic(reader, true); + } else if ("csv".equals(extension)) { + parseBasic(reader, false); + } + //read(); } - public Table(BufferedReader reader) { - read(reader); - } +// public Table(BufferedReader reader) { +// read(reader); +// } - protected void read(BufferedReader reader) { + protected void init(PApplet sketch) { + this.sketch = sketch; columns = new Object[0]; columnTypes = new int[0]; columnCategories = new HashMapBlows[0]; - try { - boolean csv = peekCSV(reader); - if (csv) { - parseCSV(reader); - } else { - parseTSV(reader); - } - } catch (IOException e) { - e.printStackTrace(); - } } +// protected void read(BufferedReader reader) { +// init(); +// try { +// boolean csv = peekCSV(reader); +// if (csv) { +// parseCSV(reader); +// } else { +// parseTSV(reader); +// } +// } catch (IOException e) { +// e.printStackTrace(); +// } +// } + + public Table(ResultSet rs) { - this(); + init(null); try { ResultSetMetaData rsmd = rs.getMetaData(); @@ -231,6 +283,7 @@ public class Table { * Guess whether this file is tab separated or comma separated by checking * whether there are more tabs or commas in the first 100 characters. */ + /* protected boolean peekCSV(BufferedReader reader) throws IOException { char[] buffer = new char[100]; int remaining = buffer.length; @@ -260,6 +313,7 @@ public class Table { parseTSV(reader); } } + */ public void parseTSV(BufferedReader reader) throws IOException { @@ -419,7 +473,7 @@ public class Table { } - public void parseAwfulCSV(BufferedReader reader) throws IOException { + protected void parseAwfulCSV(BufferedReader reader) throws IOException { char[] c = new char[100]; int count = 0; boolean insideQuote = false; @@ -497,9 +551,9 @@ public class Table { } - protected String[] splitLine(String line) { - return commaSeparatedValues ? splitLineCSV(line) : PApplet.split(line, '\t'); - } +// protected String[] splitLine(String line) { +// return commaSeparatedValues ? splitLineCSV(line) : PApplet.split(line, '\t'); +// } /**