Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions core/src/processing/core/PApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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, ','));
Expand All @@ -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
Expand Down
44 changes: 36 additions & 8 deletions core/src/processing/data/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
*/
Expand All @@ -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:
* <ul>
Expand All @@ -155,6 +171,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<TableRow> rows) {
init();

Expand Down Expand Up @@ -565,17 +588,22 @@ static class CommaSeparatedLine {
// int offset;
int start; //, stop;

String[] handle(String line, BufferedReader reader) throws IOException {
// free delimiter. standard is comma-seperator
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
Expand Down Expand Up @@ -606,7 +634,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);
Expand Down Expand Up @@ -675,7 +703,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;
Expand All @@ -702,7 +730,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;
Expand Down Expand Up @@ -752,7 +780,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);
}


Expand Down Expand Up @@ -1260,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 &&
Expand All @@ -1277,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'
Expand Down