T
tom t/LA
Here is a function to convert a CSV file to a Javascript array. Uses
idealized file reading functions based on the std C library, since
there is no Javascript standard. Not fully tested.
function csvToArray (f /* file handle */)
{
// convert csv file to Javascript 2d array (array of arrays)
// written in Javascript but file lib functions are idealized
var array2d = new Array(0);
var i = 0;
var maxlen = 0;
while (! feof (f)) { // return True if at EOF
array2d = csvRowToArray (f);
i++;
}
return array2d;
}
function csvRowToArray (f /* file handle */)
{
// convert one line in CSV format to array of strings
// return array
var array = new Array(0);
array [0] = "";
for (state = "cellData", si=0, ai=0; state != "end"; si++) {
// get one char
var c = getc(f);
// Assume EOL is just a character
// at end of file EOF is returned
LogMessage ("state "+state+", read char "+c+".");
switch (state) {
case "cellData":
if (c == '"') {
// **should also clear cell because chars before
// quote should be ignored.
state = "quotedCellData";
}
else if (c == ",") {
state = "cellBdy";
// ** for strict compliance, trim spaces from
// begin and end of string here
}
else if (c == EOL) { // end of line
state = "end";
}
else {
// regular char --add to cell data
array[ai] = array[ai] + c;
// stay in same state
}
break;
case "cellBdy":
// cell boundary- start new cell
ai++;
array [ai] = "";
if (c == '"') {
state = "quotedCellData";
}
else if (c == ",") {
// stay in same state
}
else if (c == EOL) { // end of line
state = "end";
}
else {
// regular char --
array[ai] = array[ai] + c;
state = "cellData";
}
break;
case "quotedCellData" :
if (c == '"') {
state = "quoteInQuote";
}else {
// normal char, add to cell
// Note EOLs are considered normal here
array[ai] = array[ai] + c;
// stay in same state
}
break;
case "quoteInQuote":
if (c == '"') {
// "" in quote string- add a " to string
array[ai] = array[ai] + '"';
state = "quotedCellData";
}else if (c == ",") {
state = "cellBdy";
}else if (c == EOL) { // end line;
state = "end";
}else {
// skip junk between end of quote and , or EOL
}
break;
case "end":
// never get here
break;
default:
LogError ("unknown state in switch: "+state);
} // end switch
LogMessage ("array ["+ai+"]="+array[ai]);
} // end while
// in case some elements are unassigned
for (var i = 0; i < array.length; i++) {
if (array == null) {
array == "";
}
LogMessage ("array ="+array);
}
return array;
}
idealized file reading functions based on the std C library, since
there is no Javascript standard. Not fully tested.
function csvToArray (f /* file handle */)
{
// convert csv file to Javascript 2d array (array of arrays)
// written in Javascript but file lib functions are idealized
var array2d = new Array(0);
var i = 0;
var maxlen = 0;
while (! feof (f)) { // return True if at EOF
array2d = csvRowToArray (f);
i++;
}
return array2d;
}
function csvRowToArray (f /* file handle */)
{
// convert one line in CSV format to array of strings
// return array
var array = new Array(0);
array [0] = "";
for (state = "cellData", si=0, ai=0; state != "end"; si++) {
// get one char
var c = getc(f);
// Assume EOL is just a character
// at end of file EOF is returned
LogMessage ("state "+state+", read char "+c+".");
switch (state) {
case "cellData":
if (c == '"') {
// **should also clear cell because chars before
// quote should be ignored.
state = "quotedCellData";
}
else if (c == ",") {
state = "cellBdy";
// ** for strict compliance, trim spaces from
// begin and end of string here
}
else if (c == EOL) { // end of line
state = "end";
}
else {
// regular char --add to cell data
array[ai] = array[ai] + c;
// stay in same state
}
break;
case "cellBdy":
// cell boundary- start new cell
ai++;
array [ai] = "";
if (c == '"') {
state = "quotedCellData";
}
else if (c == ",") {
// stay in same state
}
else if (c == EOL) { // end of line
state = "end";
}
else {
// regular char --
array[ai] = array[ai] + c;
state = "cellData";
}
break;
case "quotedCellData" :
if (c == '"') {
state = "quoteInQuote";
}else {
// normal char, add to cell
// Note EOLs are considered normal here
array[ai] = array[ai] + c;
// stay in same state
}
break;
case "quoteInQuote":
if (c == '"') {
// "" in quote string- add a " to string
array[ai] = array[ai] + '"';
state = "quotedCellData";
}else if (c == ",") {
state = "cellBdy";
}else if (c == EOL) { // end line;
state = "end";
}else {
// skip junk between end of quote and , or EOL
}
break;
case "end":
// never get here
break;
default:
LogError ("unknown state in switch: "+state);
} // end switch
LogMessage ("array ["+ai+"]="+array[ai]);
} // end while
// in case some elements are unassigned
for (var i = 0; i < array.length; i++) {
if (array == null) {
array == "";
}
LogMessage ("array ="+array);
}
return array;
}