Skip to content

Instantly share code, notes, and snippets.

@bakercp
Last active December 15, 2015 03:09
Show Gist options
  • Save bakercp/5192707 to your computer and use it in GitHub Desktop.
Save bakercp/5192707 to your computer and use it in GitHub Desktop.
CSV Parsing in openFrameworks
ofHttpResponse response = ofLoadURL("http://lvaqi.org/data/data.txt");
if(response.status == 200 /* successfully http response */) {
ofBuffer buffer = response.data;
string lastLine = ""; // empty for the moment
// we are going to cycle through until we come to the last line.
// since we stop when the variable is !buffer.isLastLine(), the last line
// will fill the lastLine variable.
do {
lastLine = buffer.getNextLine();
} while(!buffer.isLastLine());
ofLogNotice() << "The last line is: " << lastLine;
if(lastLine.empty()) {
ofLogError() << "There was no data in the last line ... probably should quit."
}
vector<string> tokens = ofSplitString(lastLine,",",true);
if(tokens.size() >= 5) {
// this parces the last line into separate arrays
int _temp = ofToInt(tokens[1]);
int _hum = ofToInt(tokens[2]);
int _o3 = ofToInt(tokens[3]);
int _co = ofToInt(tokens[4]);
//maps arrays into new variables
float temp = ofMap(_temp, 0, 100, 0, ofGetWidth()/2);
float hum = ofMap(_hum, 0, 50, 0, ofGetWidth()/2);
float o3 = ofMap(_o3, 0, 1200, 0, ofGetWidth()/2);
float co = ofMap(_co, 0, 1200, 0, ofGetWidth()/2);
ofLogNotice() << "Temp = " << temp;
ofLogNotice() << "Hum = " << hum;
ofLogNotice() << "O3 = " << o3;
ofLogNotice() << "CO = " << co;
} else {
ofLogError() << "There were only " << tokens.size() << " tokens parse, and we need at least 5.";
}
} else {
ofLogError() << "Unsuccessful data load: " << response.status << " : " << response.error;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment