2016-07-11 11:14:58 -04:00
|
|
|
#include "STDInclude.hpp"
|
|
|
|
|
|
|
|
namespace Utils
|
|
|
|
{
|
|
|
|
CSV::CSV(std::string file, bool isFile, bool allowComments)
|
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
this->parse(file, isFile, allowComments);
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CSV::~CSV()
|
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
for (auto row : this->dataMap)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
|
|
|
for (auto entry : row)
|
|
|
|
{
|
|
|
|
entry.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
row.clear();
|
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
this->dataMap.clear();
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
int CSV::getRows()
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
return this->dataMap.size();
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
int CSV::getColumns(size_t row)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
if (this->dataMap.size() > row)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
return this->dataMap[row].size();
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
int CSV::getColumns()
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
for (int i = 0; i < this->getRows(); ++i)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
count = std::max(this->getColumns(i), count);
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
std::string CSV::getElementAt(size_t row, size_t column)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
if (this->dataMap.size() > row)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
auto _row = this->dataMap[row];
|
2016-07-11 11:14:58 -04:00
|
|
|
|
|
|
|
if (_row.size() > column)
|
|
|
|
{
|
|
|
|
return _row[column];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
void CSV::parse(std::string file, bool isFile, bool allowComments)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
|
|
|
std::string buffer;
|
|
|
|
|
|
|
|
if (isFile)
|
|
|
|
{
|
|
|
|
if (!Utils::IO::FileExists(file)) return;
|
|
|
|
buffer = Utils::IO::ReadFile(file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer = file;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!buffer.empty())
|
|
|
|
{
|
|
|
|
auto rows = Utils::String::Explode(buffer, '\n');
|
|
|
|
|
|
|
|
for (auto row : rows)
|
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
this->parseRow(row, allowComments);
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
void CSV::parseRow(std::string row, bool allowComments)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
|
|
|
bool isString = false;
|
|
|
|
std::string element;
|
|
|
|
std::vector<std::string> _row;
|
|
|
|
char tempStr = 0;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < row.size(); ++i)
|
|
|
|
{
|
|
|
|
if (row[i] == ',' && !isString) // FLush entry
|
|
|
|
{
|
|
|
|
_row.push_back(element);
|
|
|
|
element.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (row[i] == '"') // Start/Terminate string
|
|
|
|
{
|
|
|
|
isString = !isString;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (i < (row.size() - 1) && row[i] == '\\' &&row[i + 1] == '"' && isString) // Handle quotes in strings as \"
|
|
|
|
{
|
|
|
|
tempStr = '"';
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
else if (!isString && (row[i] == '\n' || row[i] == '\x0D' || row[i] == '\x0A' || row[i] == '\t'))
|
|
|
|
{
|
|
|
|
//++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!isString && row[i] == '#' && allowComments) // Skip comments. I know CSVs usually don't have comments, but in this case it's useful
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tempStr = row[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
element.append(&tempStr, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push last element
|
|
|
|
_row.push_back(element);
|
|
|
|
|
|
|
|
if (_row.size() == 0 || (_row.size() == 1 && !_row[0].size())) // Skip empty rows
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
this->dataMap.push_back(_row);
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
}
|