Skip to content

Instantly share code, notes, and snippets.

@DevWurm
Created May 12, 2015 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DevWurm/12ef930997255a717b1f to your computer and use it in GitHub Desktop.
Save DevWurm/12ef930997255a717b1f to your computer and use it in GitHub Desktop.
CSV parsing function
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