Skip to content
Snippets Groups Projects
Commit 40a30e4b authored by Tom Fischer's avatar Tom Fischer
Browse files

[A/IO/CsvInterface] Clang format.

parent 31dfdb5f
No related branches found
No related tags found
No related merge requests found
...@@ -19,7 +19,8 @@ ...@@ -19,7 +19,8 @@
#include "GeoLib/Point.h" #include "GeoLib/Point.h"
namespace FileIO { namespace FileIO
{
CsvInterface::CsvInterface() = default; CsvInterface::CsvInterface() = default;
std::vector<std::string> CsvInterface::getColumnNames(std::string const& fname, std::vector<std::string> CsvInterface::getColumnNames(std::string const& fname,
...@@ -59,11 +60,12 @@ std::vector<std::string> CsvInterface::getColumnNames(std::string const& fname, ...@@ -59,11 +60,12 @@ std::vector<std::string> CsvInterface::getColumnNames(std::string const& fname,
} }
int CsvInterface::readPoints(std::string const& fname, char delim, int CsvInterface::readPoints(std::string const& fname, char delim,
std::vector<GeoLib::Point*> &points) std::vector<GeoLib::Point*>& points)
{ {
std::ifstream in(fname.c_str()); std::ifstream in(fname.c_str());
if (!in.is_open()) { if (!in.is_open())
{
ERR("CsvInterface::readPoints(): Could not open file {:s}.", fname); ERR("CsvInterface::readPoints(): Could not open file {:s}.", fname);
return -1; return -1;
} }
...@@ -74,7 +76,7 @@ int CsvInterface::readPoints(std::string const& fname, char delim, ...@@ -74,7 +76,7 @@ int CsvInterface::readPoints(std::string const& fname, char delim,
std::size_t line_count(0); std::size_t line_count(0);
std::size_t error_count(0); std::size_t error_count(0);
std::list<std::string>::const_iterator it; std::list<std::string>::const_iterator it;
while ( getline(in, line) ) while (getline(in, line))
{ {
line_count++; line_count++;
std::list<std::string> const fields = BaseLib::splitString(line, delim); std::list<std::string> const fields = BaseLib::splitString(line, delim);
...@@ -89,12 +91,15 @@ int CsvInterface::readPoints(std::string const& fname, char delim, ...@@ -89,12 +91,15 @@ int CsvInterface::readPoints(std::string const& fname, char delim,
} }
it = fields.begin(); it = fields.begin();
std::array<double, 3> point{}; std::array<double, 3> point{};
try { try
{
point[0] = std::stod(*it); point[0] = std::stod(*it);
point[1] = std::stod(*(++it)); point[1] = std::stod(*(++it));
point[2] = std::stod(*(++it)); point[2] = std::stod(*(++it));
points.push_back(new GeoLib::Point(point[0], point[1], point[2])); points.push_back(new GeoLib::Point(point[0], point[1], point[2]));
} catch (const std::invalid_argument&) { }
catch (const std::invalid_argument&)
{
ERR("Error converting data to coordinates in line {:d}.", ERR("Error converting data to coordinates in line {:d}.",
line_count); line_count);
} }
...@@ -103,26 +108,29 @@ int CsvInterface::readPoints(std::string const& fname, char delim, ...@@ -103,26 +108,29 @@ int CsvInterface::readPoints(std::string const& fname, char delim,
} }
int CsvInterface::readPoints(std::string const& fname, char delim, int CsvInterface::readPoints(std::string const& fname, char delim,
std::vector<GeoLib::Point*> &points, std::vector<GeoLib::Point*>& points,
std::string const& x_column_name, std::string const& x_column_name,
std::string const& y_column_name, std::string const& y_column_name,
std::string const& z_column_name) std::string const& z_column_name)
{ {
std::ifstream in(fname.c_str()); std::ifstream in(fname.c_str());
std::array<std::string, 3> const column_names = {{x_column_name, y_column_name, z_column_name}}; std::array<std::string, 3> const column_names = {
{x_column_name, y_column_name, z_column_name}};
if (!in.is_open()) { if (!in.is_open())
{
ERR("CsvInterface::readPoints(): Could not open file {:s}.", fname); ERR("CsvInterface::readPoints(): Could not open file {:s}.", fname);
return -1; return -1;
} }
std::string line; std::string line;
getline(in, line); getline(in, line);
std::array<std::size_t, 3> const column_idx = std::array<std::size_t, 3> const column_idx = {
{{ CsvInterface::findColumn(line, delim, x_column_name), {CsvInterface::findColumn(line, delim, x_column_name),
CsvInterface::findColumn(line, delim, y_column_name), CsvInterface::findColumn(line, delim, y_column_name),
(z_column_name.empty()) ? CsvInterface::findColumn(line, delim, y_column_name) : (z_column_name.empty())
CsvInterface::findColumn(line, delim, z_column_name) }}; ? CsvInterface::findColumn(line, delim, y_column_name)
: CsvInterface::findColumn(line, delim, z_column_name)}};
for (std::size_t i = 0; i < 3; ++i) for (std::size_t i = 0; i < 3; ++i)
{ {
...@@ -137,14 +145,14 @@ int CsvInterface::readPoints(std::string const& fname, char delim, ...@@ -137,14 +145,14 @@ int CsvInterface::readPoints(std::string const& fname, char delim,
} }
int CsvInterface::readPoints(std::string const& fname, char delim, int CsvInterface::readPoints(std::string const& fname, char delim,
std::vector<GeoLib::Point*> &points, std::vector<GeoLib::Point*>& points,
std::size_t x_column_idx, std::size_t x_column_idx, std::size_t y_column_idx,
std::size_t y_column_idx,
std::size_t z_column_idx) std::size_t z_column_idx)
{ {
std::ifstream in(fname.c_str()); std::ifstream in(fname.c_str());
if (!in.is_open()) { if (!in.is_open())
{
ERR("CsvInterface::readPoints(): Could not open file {:s}.", fname); ERR("CsvInterface::readPoints(): Could not open file {:s}.", fname);
return -1; return -1;
} }
...@@ -153,34 +161,36 @@ int CsvInterface::readPoints(std::string const& fname, char delim, ...@@ -153,34 +161,36 @@ int CsvInterface::readPoints(std::string const& fname, char delim,
{ {
z_column_idx = y_column_idx; z_column_idx = y_column_idx;
} }
std::array<std::size_t, 3> const column_idx = {{ x_column_idx, y_column_idx, z_column_idx }}; std::array<std::size_t, 3> const column_idx = {
{x_column_idx, y_column_idx, z_column_idx}};
return readPoints(in, delim, points, column_idx); return readPoints(in, delim, points, column_idx);
} }
int CsvInterface::readPoints(std::ifstream &in, char delim, int CsvInterface::readPoints(std::ifstream& in, char delim,
std::vector<GeoLib::Point*> &points, std::vector<GeoLib::Point*>& points,
std::array<std::size_t, 3> const& column_idx) std::array<std::size_t, 3> const& column_idx)
{ {
std::array<std::size_t, 3> order = {{ 0, 1, 2 }}; std::array<std::size_t, 3> order = {{0, 1, 2}};
std::sort(order.begin(), order.end(), std::sort(order.begin(), order.end(),
[&column_idx](std::size_t idx1, std::size_t idx2) {return column_idx[idx1] < column_idx[idx2];}); [&column_idx](std::size_t idx1, std::size_t idx2) {
std::array<std::size_t, 3> const column_advance = return column_idx[idx1] < column_idx[idx2];
{{ column_idx[order[0]], });
column_idx[order[1]] - column_idx[order[0]], std::array<std::size_t, 3> const column_advance = {
column_idx[order[2]] - column_idx[order[1]] }}; {column_idx[order[0]], column_idx[order[1]] - column_idx[order[0]],
column_idx[order[2]] - column_idx[order[1]]}};
std::string line; std::string line;
std::size_t line_count(0); std::size_t line_count(0);
std::size_t error_count(0); std::size_t error_count(0);
std::list<std::string>::const_iterator it; std::list<std::string>::const_iterator it;
while ( getline(in, line) ) while (getline(in, line))
{ {
line_count++; line_count++;
std::list<std::string> const fields = BaseLib::splitString(line, delim); std::list<std::string> const fields = BaseLib::splitString(line, delim);
if (fields.size() < column_idx[order[2]]+1) if (fields.size() < column_idx[order[2]] + 1)
{ {
ERR("Line {:d} contains not enough columns of data. Skipping " ERR("Line {:d} contains not enough columns of data. Skipping "
"line...", "line...",
...@@ -191,15 +201,19 @@ int CsvInterface::readPoints(std::ifstream &in, char delim, ...@@ -191,15 +201,19 @@ int CsvInterface::readPoints(std::ifstream &in, char delim,
std::array<double, 3> point{}; std::array<double, 3> point{};
it = fields.begin(); it = fields.begin();
try { try
{
std::advance(it, column_advance[0]); std::advance(it, column_advance[0]);
point[order[0]] = std::stod(*it); point[order[0]] = std::stod(*it);
std::advance(it, column_advance[1]); std::advance(it, column_advance[1]);
point[order[1]] = std::stod(*it); point[order[1]] = std::stod(*it);
std::advance(it, column_advance[2]); std::advance(it, column_advance[2]);
point[order[2]] = (column_idx[1] == column_idx[2]) ? 0 : std::stod(*it); point[order[2]] =
(column_idx[1] == column_idx[2]) ? 0 : std::stod(*it);
points.push_back(new GeoLib::Point(point[0], point[1], point[2])); points.push_back(new GeoLib::Point(point[0], point[1], point[2]));
} catch (const std::invalid_argument&) { }
catch (const std::invalid_argument&)
{
ERR("Error converting data to coordinates in line {:d}.", ERR("Error converting data to coordinates in line {:d}.",
line_count); line_count);
error_count++; error_count++;
...@@ -208,7 +222,8 @@ int CsvInterface::readPoints(std::ifstream &in, char delim, ...@@ -208,7 +222,8 @@ int CsvInterface::readPoints(std::ifstream &in, char delim,
return error_count; return error_count;
} }
std::size_t CsvInterface::findColumn(std::string const& line, char delim, std::string const& column_name) std::size_t CsvInterface::findColumn(std::string const& line, char delim,
std::string const& column_name)
{ {
std::list<std::string> const fields = BaseLib::splitString(line, delim); std::list<std::string> const fields = BaseLib::splitString(line, delim);
if (fields.empty()) if (fields.empty())
...@@ -246,12 +261,12 @@ bool CsvInterface::write() ...@@ -246,12 +261,12 @@ bool CsvInterface::write()
{ {
if (_data.empty()) if (_data.empty())
{ {
ERR ("CsvInterface::write() - No data to write."); ERR("CsvInterface::write() - No data to write.");
return false; return false;
} }
std::size_t const n_vecs (_data.size()); std::size_t const n_vecs(_data.size());
std::size_t const vec_size (getVectorSize(0)); std::size_t const vec_size(getVectorSize(0));
if (_writeCsvHeader) if (_writeCsvHeader)
{ {
...@@ -263,13 +278,13 @@ bool CsvInterface::write() ...@@ -263,13 +278,13 @@ bool CsvInterface::write()
_out << "\n"; _out << "\n";
} }
for (std::size_t j=0; j<vec_size; ++j) for (std::size_t j = 0; j < vec_size; ++j)
{ {
writeValue(0,j); writeValue(0, j);
for (std::size_t i=1; i<n_vecs; ++i) for (std::size_t i = 1; i < n_vecs; ++i)
{ {
_out << "\t"; _out << "\t";
writeValue(i,j); writeValue(i, j);
} }
_out << "\n"; _out << "\n";
} }
...@@ -297,11 +312,13 @@ void CsvInterface::writeValue(std::size_t vec_idx, std::size_t in_vec_idx) ...@@ -297,11 +312,13 @@ void CsvInterface::writeValue(std::size_t vec_idx, std::size_t in_vec_idx)
{ {
if (_data[vec_idx].type() == typeid(std::vector<std::string>)) if (_data[vec_idx].type() == typeid(std::vector<std::string>))
{ {
_out << boost::any_cast<std::vector<std::string>>(_data[vec_idx])[in_vec_idx]; _out << boost::any_cast<std::vector<std::string>>(
_data[vec_idx])[in_vec_idx];
} }
else if (_data[vec_idx].type() == typeid(std::vector<double>)) else if (_data[vec_idx].type() == typeid(std::vector<double>))
{ {
_out << boost::any_cast<std::vector<double>>(_data[vec_idx])[in_vec_idx]; _out << boost::any_cast<std::vector<double>>(
_data[vec_idx])[in_vec_idx];
} }
else if (_data[vec_idx].type() == typeid(std::vector<int>)) else if (_data[vec_idx].type() == typeid(std::vector<int>))
{ {
...@@ -309,4 +326,4 @@ void CsvInterface::writeValue(std::size_t vec_idx, std::size_t in_vec_idx) ...@@ -309,4 +326,4 @@ void CsvInterface::writeValue(std::size_t vec_idx, std::size_t in_vec_idx)
} }
} }
} // end namespace FileIO } // end namespace FileIO
...@@ -29,18 +29,18 @@ ...@@ -29,18 +29,18 @@
#include "BaseLib/StringTools.h" #include "BaseLib/StringTools.h"
#include "BaseLib/IO/Writer.h" #include "BaseLib/IO/Writer.h"
namespace GeoLib { namespace GeoLib
class Point; {
class Point;
} }
namespace FileIO { namespace FileIO
{
/** /**
* Interface for reading CSV file formats. * Interface for reading CSV file formats.
*/ */
class CsvInterface : public BaseLib::IO::Writer class CsvInterface : public BaseLib::IO::Writer
{ {
public: public:
/// Contructor (only needed for writing files) /// Contructor (only needed for writing files)
CsvInterface(); CsvInterface();
...@@ -59,19 +59,21 @@ public: ...@@ -59,19 +59,21 @@ public:
/// Stores if the CSV file to be written should include a header or not. /// Stores if the CSV file to be written should include a header or not.
void setCsvHeader(bool write_header) { _writeCsvHeader = write_header; } void setCsvHeader(bool write_header) { _writeCsvHeader = write_header; }
/// Adds a data vector to the CSV file. All data vectors have to have the same size. /// Adds a data vector to the CSV file. All data vectors have to have the
/// Vectors will be written in the same sequence they have been added to the interface. /// same size. Vectors will be written in the same sequence they have been
template<typename T> /// added to the interface.
bool addVectorForWriting(std::string const& vec_name, std::vector<T> const& vec) template <typename T>
bool addVectorForWriting(std::string const& vec_name,
std::vector<T> const& vec)
{ {
static_assert(std::is_same<T, std::string>::value static_assert(
|| std::is_same<T, double>::value std::is_same<T, std::string>::value ||
|| std::is_same<T, int>::value, std::is_same<T, double>::value || std::is_same<T, int>::value,
"CsvInterface can only write vectors of strings, doubles or ints."); "CsvInterface can only write vectors of strings, doubles or ints.");
if (!_data.empty()) if (!_data.empty())
{ {
std::size_t const vec_size (getVectorSize(0)); std::size_t const vec_size(getVectorSize(0));
if (vec_size != vec.size()) if (vec_size != vec.size())
{ {
ERR("Vector size does not match existing data (should be " ERR("Vector size does not match existing data (should be "
...@@ -91,15 +93,15 @@ public: ...@@ -91,15 +93,15 @@ public:
/** /**
* Reads 3D points from a CSV file. It is assumed that the file has a header * Reads 3D points from a CSV file. It is assumed that the file has a header
* specifying a name for each of the columns. The first three columns will be * specifying a name for each of the columns. The first three columns will
* interpreted as x-, y- and z-coordinate, respectively. * be interpreted as x-, y- and z-coordinate, respectively. \param fname
* \param fname Name of the file to be read * Name of the file to be read \param delim Deliminator, default is ','
* \param delim Deliminator, default is ','
* \param points A vector containing the 3D points read from the file * \param points A vector containing the 3D points read from the file
* \return An error code (0 = ok, 0<i<max = number of skipped lines, -1 error reading file) * \return An error code (0 = ok, 0<i<max = number of skipped lines, -1
* error reading file)
*/ */
static int readPoints(std::string const& fname, char delim, static int readPoints(std::string const& fname, char delim,
std::vector<GeoLib::Point*> &points); std::vector<GeoLib::Point*>& points);
/** /**
* Reads 3D points from a CSV file. It is assumed that the file has a header * Reads 3D points from a CSV file. It is assumed that the file has a header
...@@ -109,14 +111,15 @@ public: ...@@ -109,14 +111,15 @@ public:
* z-coordinates will be set to zero. * z-coordinates will be set to zero.
* \param fname Name of the file to be read * \param fname Name of the file to be read
* \param delim Deliminator, default is ',' * \param delim Deliminator, default is ','
* \param points A vector containing the 3D points read from the file * \param points A vector containing the 3D points read from the
* \param x_column_name Name of the column to be interpreted as x-coordinate * file \param x_column_name Name of the column to be interpreted as
* \param y_column_name Name of the column to be interpreted as y-coordinate * x-coordinate \param y_column_name Name of the column to be interpreted
* \param z_column_name Name of the column to be interpreted as z-coordinate * as y-coordinate \param z_column_name Name of the column to be
* \return An error code (0 = ok, 0<i<max = number of skipped lines, -1 error reading file) * interpreted as z-coordinate \return An error code (0 = ok, 0<i<max =
* number of skipped lines, -1 error reading file)
*/ */
static int readPoints(std::string const& fname, char delim, static int readPoints(std::string const& fname, char delim,
std::vector<GeoLib::Point*> &points, std::vector<GeoLib::Point*>& points,
std::string const& x_column_name, std::string const& x_column_name,
std::string const& y_column_name, std::string const& y_column_name,
std::string const& z_column_name = ""); std::string const& z_column_name = "");
...@@ -128,17 +131,18 @@ public: ...@@ -128,17 +131,18 @@ public:
* z-coordinates will be set to zero. * z-coordinates will be set to zero.
* \param fname Name of the file to be read * \param fname Name of the file to be read
* \param delim Deliminator, default is ',' * \param delim Deliminator, default is ','
* \param points A vector containing the 3D points read from the file * \param points A vector containing the 3D points read from the
* \param x_column_idx Index of the column to be interpreted as x-coordinate * file \param x_column_idx Index of the column to be interpreted as
* \param y_column_idx Index of the column to be interpreted as y-coordinate * x-coordinate \param y_column_idx Index of the column to be interpreted
* \param z_column_idx Index of the column to be interpreted as z-coordinate * as y-coordinate \param z_column_idx Index of the column to be
* \return An error code (0 = ok, 0<i<max = number of skipped lines, -1 error reading file) * interpreted as z-coordinate \return An error code (0 = ok, 0<i<max =
* number of skipped lines, -1 error reading file)
*/ */
static int readPoints(std::string const& fname, char delim, static int readPoints(
std::vector<GeoLib::Point*> &points, std::string const& fname, char delim,
std::size_t x_column_idx, std::vector<GeoLib::Point*>& points, std::size_t x_column_idx,
std::size_t y_column_idx, std::size_t y_column_idx,
std::size_t z_column_idx = std::numeric_limits<std::size_t>::max()); std::size_t z_column_idx = std::numeric_limits<std::size_t>::max());
/** /**
* Reads a column of the given name from a CSV file. * Reads a column of the given name from a CSV file.
...@@ -146,22 +150,25 @@ public: ...@@ -146,22 +150,25 @@ public:
* \param delim Deliminator, default is ',' * \param delim Deliminator, default is ','
* \param data_array A vector containing the data read from the file * \param data_array A vector containing the data read from the file
* \param column_name The column's name to read * \param column_name The column's name to read
* \return An error code (0 = ok, 0<i<max = number of skipped lines, -1 error reading file) * \return An error code (0 = ok, 0<i<max = number of skipped lines, -1
* error reading file)
*/ */
template <typename T> template <typename T>
static int readColumn(std::string const& fname, char delim, static int readColumn(std::string const& fname, char delim,
std::vector<T> &data_array, std::vector<T>& data_array,
std::string const& column_name) std::string const& column_name)
{ {
std::ifstream in(fname.c_str()); std::ifstream in(fname.c_str());
if (!in.is_open()) { if (!in.is_open())
{
ERR("CsvInterface::readColumn(): Could not open file {:s}.", fname); ERR("CsvInterface::readColumn(): Could not open file {:s}.", fname);
return -1; return -1;
} }
std::string line; std::string line;
getline(in, line); getline(in, line);
std::size_t const column_idx = CsvInterface::findColumn(line, delim, column_name); std::size_t const column_idx =
CsvInterface::findColumn(line, delim, column_name);
if (column_idx == std::numeric_limits<std::size_t>::max()) if (column_idx == std::numeric_limits<std::size_t>::max())
{ {
ERR("Column '{:s}' not found in file header.", column_name); ERR("Column '{:s}' not found in file header.", column_name);
...@@ -172,11 +179,11 @@ public: ...@@ -172,11 +179,11 @@ public:
template <typename T> template <typename T>
static int readColumn(std::string const& fname, char delim, static int readColumn(std::string const& fname, char delim,
std::vector<T> &data_array, std::vector<T>& data_array, std::size_t column_idx)
std::size_t column_idx)
{ {
std::ifstream in(fname.c_str()); std::ifstream in(fname.c_str());
if (!in.is_open()) { if (!in.is_open())
{
ERR("CsvInterface::readColumn(): Could not open file {:s}.", fname); ERR("CsvInterface::readColumn(): Could not open file {:s}.", fname);
return -1; return -1;
} }
...@@ -185,26 +192,26 @@ public: ...@@ -185,26 +192,26 @@ public:
private: private:
/// Actual point reader for public methods /// Actual point reader for public methods
static int readPoints(std::ifstream &in, char delim, static int readPoints(std::ifstream& in, char delim,
std::vector<GeoLib::Point*> &points, std::vector<GeoLib::Point*>& points,
std::array<std::size_t, 3> const& column_idx); std::array<std::size_t, 3> const& column_idx);
/// Actual column reader for public methods /// Actual column reader for public methods
template <typename T> template <typename T>
static int readColumn(std::ifstream &in, char delim, static int readColumn(std::ifstream& in, char delim,
std::vector<T> &data_array, std::vector<T>& data_array, std::size_t column_idx)
std::size_t column_idx)
{ {
std::string line; std::string line;
std::size_t line_count(0); std::size_t line_count(0);
std::size_t error_count(0); std::size_t error_count(0);
std::list<std::string>::const_iterator it; std::list<std::string>::const_iterator it;
while ( getline(in, line) ) while (std::getline(in, line))
{ {
line_count++; line_count++;
std::list<std::string> const fields = BaseLib::splitString(line, delim); std::list<std::string> const fields =
BaseLib::splitString(line, delim);
if (fields.size() < column_idx+1) if (fields.size() < column_idx + 1)
{ {
ERR("Line {:d} contains not enough columns of data. Skipping " ERR("Line {:d} contains not enough columns of data. Skipping "
"line...", "line...",
...@@ -229,8 +236,10 @@ private: ...@@ -229,8 +236,10 @@ private:
return error_count; return error_count;
} }
/// Returns the number of the column with column_name (or std::numeric_limits::max() if no such column has been found). /// Returns the number of the column with column_name (or
static std::size_t findColumn(std::string const& line, char delim, std::string const& column_name); /// std::numeric_limits::max() if no such column has been found).
static std::size_t findColumn(std::string const& line, char delim,
std::string const& column_name);
/// Returns the size of the vector with the given index /// Returns the size of the vector with the given index
std::size_t getVectorSize(std::size_t idx) const; std::size_t getVectorSize(std::size_t idx) const;
...@@ -244,7 +253,7 @@ private: ...@@ -244,7 +253,7 @@ private:
bool _writeCsvHeader{true}; bool _writeCsvHeader{true};
std::vector<std::string> _vec_names; std::vector<std::string> _vec_names;
std::vector< boost::any > _data; std::vector<boost::any> _data;
}; };
} // namespace FileIO } // namespace FileIO
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment