Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle windows CSV files on Linux #501

Merged
merged 2 commits into from
Mar 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions include/utilities/CSV_Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
*/
class CSVReader
{
std::string fileName;
std::string delimeter;
std::string fileName;
std::string delimeter;

public:
CSVReader(std::string filename, std::string delm = ",") :
fileName(filename), delimeter(delm)
{ }
CSVReader(std::string filename, std::string delm = ",") :
fileName(filename), delimeter(delm)
{ }

// Function to fetch data from a CSV File
std::vector<std::vector<std::string> > getData();
// Function to fetch data from a CSV File
std::vector<std::vector<std::string> > getData();
};

/*
Expand All @@ -31,7 +31,7 @@ class CSVReader
*/
inline std::vector<std::vector<std::string> > CSVReader::getData()
{
std::ifstream file(fileName);
std::ifstream file(fileName);

if(file.fail()){
/// \todo TODO: Return appropriate error
Expand All @@ -40,22 +40,27 @@ inline std::vector<std::vector<std::string> > CSVReader::getData()
/// \todo Potentially only output warning and fill array with sentinel values.
}

std::vector<std::vector<std::string> > dataList;
std::vector<std::vector<std::string> > dataList;

std::string line = "";
// Iterate through each line and split the content using delimeter
while (getline(file, line))
{
std::vector<std::string> vec;
std::string line = "";
// Iterate through each line and split the content using delimeter
while (getline(file, line))
{
// Consider more robust solution like https://stackoverflow.com/a/6089413/489116
if ( line.size() && line[line.size()-1] == '\r' ) {
line = line.substr( 0, line.size() - 1 );
}

std::vector<std::string> vec;

/// \todo Look into replacement from STD for split to reduce dependency on Boost
boost::algorithm::split(vec, line, boost::is_any_of(delimeter));
dataList.push_back(vec);
}
// Close the File
file.close();
boost::algorithm::split(vec, line, boost::is_any_of(delimeter));
dataList.push_back(vec);
}
// Close the File
file.close();

return dataList;
return dataList;
}

#endif //CSV_Reader_H