C++ CSV Parser
The function featured here is of course csvline_populate. It parses a line of data by a delimiter. If you pass in a comma as your delimiter it will parse out a Comma Separated Value (CSV) file. If you pass in a '\t' char it will parse out a tab delimited file (.txt or .tsv). CSV files often have commas in the actual data, but accounts for this by surrounding the data in quotes. This also means the quotes need to be parsed out, this function accounts for that as well.
It would make some sense to only pass in the line and delimiter to the function, and have the return type be the vector. However in terms of performance under heavy loads, this makes less sense. Passing in a predefined vector allows a function to populate it, copying bytes from your line as it goes. However, to not pass in a predefined vector, we'd have to declare it as a local variable to the function, which declares it on the stack. This means when the function completes, the variable will be deallocated, so when it returns the vector, the return keyword uses the copy constructor of the vector (which uses the copy constructor of each string object) to assign the return type to the variable in the caller function. To copy the return value of an object (depending on the size of your vector and the number of times you do it), can be an expensive operation.
The only other problem with this code here, is that the definition of a csv allows for the newline character '\n' to be part of a csv field if the field is surrounded by quotes. The csvline_populate function takes care of this properly, but the main function which calls csvline_populate doesn't handle it. The way the main function works is that the getline(in, line) function populates the next line of the file from the 'in' stream using '\n' as a delimiter. So, if there is a '\n' in the middle of the csv field, the getline(in,line) still treats it as an end of line character. Most CSV files do not have a \n in the middle of the field, so it is usually not worth worrying about.
If one were to fix this issue, it would be by making a fgetcsv type function which operates directly on the FILE* handle or the ifstream. The function would also have to detect EOF (end of file).
Runia on Jun 5th, 2011 11:33 am said:
Thank you for sharing the csv parser. It works perfectly. Maybe http://www.codeproject.com/KB/recipes/Tokenizer.aspx is more efficient and the ideas behind are nice but it is not as easily applied neither does it yield immediate results like your code. Thank you.