fix table loading quirk with extensions

This commit is contained in:
benfry
2012-12-11 22:53:11 +00:00
parent 7855bc65de
commit 2a07a1f79b
3 changed files with 43 additions and 74 deletions
+14 -20
View File
@@ -5727,6 +5727,7 @@ public class PApplet extends Applet
}
//////////////////////////////////////////////////////////////
// DATA I/O
@@ -5750,19 +5751,7 @@ public class PApplet extends Applet
}
// 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);
return new Table();
}
@@ -5781,8 +5770,14 @@ public class PApplet extends Applet
public Table loadTable(String filename, String options) {
try {
// return new Table(this, filename, options);
String ext = checkExtension(filename);
if (ext != null) {
if (ext.equals("csv") || ext.equals("tsv")) {
options = ext + ",";
}
}
return new Table(createInput(filename), options);
} catch (IOException e) {
e.printStackTrace();
return null;
@@ -5804,13 +5799,12 @@ public class PApplet extends Applet
}
// static public Table loadTable(File file) {
// return new Table(this, file);
// }
static public String[] fixOptions(String options) {
return options == null ? new String[0] : options.split("\\s*,\\s*");
protected String checkExtension(String filename) {
int index = filename.lastIndexOf('.');
if (index == -1) {
return null;
}
return filename.substring(index + 1).toLowerCase();
}
+28 -54
View File
@@ -88,14 +88,12 @@ public class Table {
protected Object[] columns; // [column]
// typed data
static final int STRING = 0;
static final int INT = 1;
static final int LONG = 2;
static final int FLOAT = 3;
static final int DOUBLE = 4;
static final int CATEGORICAL = 5;
// static final int TIME = 5;
int[] columnTypes;
protected RowIterator rowIterator;
@@ -105,26 +103,10 @@ public class Table {
* Creates a new, empty table. Use addRow() to add additional rows.
*/
public Table() {
// init(null);
init();
}
// public Table(PApplet sketch) {
// init(sketch);
// }
// public Table(File file) {
// this(PApplet.createReader(file));
// }
// public Table(PApplet parent, String filename) throws IOException {
// this(parent, filename, null);
// }
public Table(File file) throws IOException {
this(file, null);
}
@@ -155,7 +137,6 @@ public class Table {
public Table(ResultSet rs) {
// init(null);
init();
try {
ResultSetMetaData rsmd = rs.getMetaData();
@@ -251,29 +232,25 @@ public class Table {
protected void parse(InputStream input, String options) throws IOException {
init();
String[] opts = PApplet.fixOptions(options);
// if (options != null) {
// opts = options.split("\\s*,\\s*");
//// PApplet.println("options:");
//// PApplet.println(opts);
boolean awfulCSV = false;
boolean header = false;
String extension = null;
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 if (opt.equals("header")) {
header = true;
} else {
throw new IllegalArgumentException("'" + opt + "' is not a valid option for loading a Table");
if (options != null) {
String[] opts = PApplet.splitTokens(options, " ,");
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 if (opt.equals("header")) {
header = true;
} else {
throw new IllegalArgumentException("'" + opt + "' is not a valid option for loading a Table");
}
}
}
// }
BufferedReader reader = PApplet.createReader(input);
if (awfulCSV) {
@@ -410,11 +387,6 @@ public class Table {
}
// protected String[] splitLine(String line) {
// return commaSeparatedValues ? splitLineCSV(line) : PApplet.split(line, '\t');
// }
/**
* Parse a line of text as comma-separated values, returning each value as
* one entry in an array of String objects. Remove quotes from entries that
@@ -650,18 +622,20 @@ public class Table {
public void save(OutputStream output, String options) {
PrintWriter writer = PApplet.createWriter(output);
String[] opts = PApplet.fixOptions(options);
for (String opt : opts) {
if (opt.equals("csv")) {
writeCSV(writer);
} else if (opt.equals("tsv")) {
writeTSV(writer);
} else if (opt.equals("html")) {
writeHTML(writer);
} else {
throw new IllegalArgumentException("'" + opt + "' not understood. " +
"Only csv, tsv, and html are " +
"accepted as save parameters");
if (options != null) {
String[] opts = PApplet.splitTokens(options, ", ");
for (String opt : opts) {
if (opt.equals("csv")) {
writeCSV(writer);
} else if (opt.equals("tsv")) {
writeTSV(writer);
} else if (opt.equals("html")) {
writeHTML(writer);
} else {
throw new IllegalArgumentException("'" + opt + "' not understood. " +
"Only csv, tsv, and html are " +
"accepted as save parameters");
}
}
}
writer.close();
+1
View File
@@ -8,6 +8,7 @@ X http://code.google.com/p/processing/issues/detail?id=987
o hint(OPENGL_ERRORS) should be the opposite to enable the reporting, no?
o hint(ENABLE_OPENGL_ERRORS) should be the hint.. disabled by default
X nah, just leave these turned on since (potentially) important
X fix table loading quirk with extensions
cleaning/earlier
C textureWrap() CLAMP and REPEAT now added