Created
May 12, 2015 04:18
-
-
Save DevWurm/12ef930997255a717b1f to your computer and use it in GitHub Desktop.
CSV parsing function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> | |
void csv_parser<T>::set_line(string input) { | |
line.clear(); | |
T buffer; | |
stringstream converter; | |
while (input.size() > 0) { //get field from input and delete this segment until | |
//input is empty | |
if (input.find_first_of(",") != -1) { //(not the last segment [one ',' left]) | |
converter << input.substr(0, input.find_first_of(",")); //add segment to converter | |
converter >> buffer; //convert segment to T type | |
converter.clear(); //clear flags of converter (normally EOF flag is set | |
//after converting), so writing in converter is enabled again | |
line.push_back(buffer); //write segment into data | |
input.erase(0, input.find_first_of(",")+1); //delete segment from input | |
} | |
else { //(last segment in line) | |
converter << input.substr(0, input.length()); //get rest of the input | |
converter >> buffer; //convert segment to T type | |
converter.clear(); //clear flags of converter (normally EOF flag is set | |
//after converting), so writing in converter is enabled again | |
line.push_back(buffer);//write segment into data | |
input.erase(0, input.length()); //delete rest of input | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment