OGS
|
Interface for reading CSV file formats.
Definition at line 40 of file CsvInterface.h.
#include <CsvInterface.h>
Public Member Functions | |
CsvInterface () | |
Constructor (only needed for writing files) | |
std::size_t | getNArrays () const |
Returns the number of vectors currently staged for writing. | |
void | addIndexVectorForWriting (std::size_t s) |
Adds an index vector of size s to the CSV file. | |
void | setCsvHeader (bool write_header) |
Stores if the CSV file to be written should include a header or not. | |
template<typename T > | |
bool | addVectorForWriting (std::string const &vec_name, std::vector< T > const &vec) |
bool | write () override |
Writes the CSV file. | |
Public Member Functions inherited from BaseLib::IO::Writer | |
Writer () | |
virtual | ~Writer ()=default |
std::string | writeToString () |
Writes the object to a string. | |
Static Public Member Functions | |
static std::vector< std::string > | getColumnNames (std::string const &fname, char delim) |
static int | readPoints (std::string const &fname, char delim, std::vector< GeoLib::Point * > &points) |
static int | readPoints (std::string const &fname, char delim, std::vector< GeoLib::Point * > &points, std::string const &x_column_name, std::string const &y_column_name, std::string const &z_column_name="") |
static int | readPoints (std::string const &fname, char delim, std::vector< GeoLib::Point * > &points, std::size_t x_column_idx, std::size_t y_column_idx, std::size_t z_column_idx=std::numeric_limits< std::size_t >::max()) |
template<typename T > | |
static std::pair< int, std::vector< T > > | readColumn (std::string const &fname, char delim, std::string const &column_name) |
template<typename T > | |
static std::pair< int, std::vector< T > > | readColumn (std::string const &fname, char delim, std::size_t column_idx) |
Private Member Functions | |
std::size_t | getVectorSize (std::size_t idx) const |
Returns the size of the vector with the given index. | |
void | writeValue (std::size_t vec_idx, std::size_t in_vec_idx) |
Static Private Member Functions | |
static int | readPoints (std::ifstream &in, char delim, std::vector< GeoLib::Point * > &points, std::array< std::size_t, 3 > const &column_idx) |
Actual point reader for public methods. | |
template<typename T > | |
static std::pair< int, std::vector< T > > | readColumn (std::ifstream &in, char delim, std::size_t column_idx) |
Actual column reader for public methods. | |
static std::size_t | findColumn (std::string const &line, char delim, std::string const &column_name) |
Private Attributes | |
bool | _writeCsvHeader {true} |
std::vector< std::string > | _vec_names |
std::vector< std::any > | _data |
Additional Inherited Members | |
Protected Member Functions inherited from BaseLib::IO::Writer | |
Protected Attributes inherited from BaseLib::IO::Writer | |
std::ostringstream | out |
The stream to write to. | |
|
default |
Constructor (only needed for writing files)
void FileIO::CsvInterface::addIndexVectorForWriting | ( | std::size_t | s | ) |
Adds an index vector of size s to the CSV file.
Definition at line 254 of file CsvInterface.cpp.
References addVectorForWriting().
Referenced by FileIO::SwmmInterface::writeCsvForObject(), and FileIO::SwmmInterface::writeCsvForTimestep().
|
inline |
Adds a data vector to the CSV file. All data vectors have to have the same size. Vectors will be written in the same sequence they have been added to the interface.
Definition at line 64 of file CsvInterface.h.
References _data, _vec_names, ERR(), and getVectorSize().
Referenced by addIndexVectorForWriting(), FileIO::SwmmInterface::writeCsvForObject(), and FileIO::SwmmInterface::writeCsvForTimestep().
|
staticprivate |
Returns the number of the column with column_name (or std::numeric_limits::max() if no such column has been found).
Definition at line 226 of file CsvInterface.cpp.
References BaseLib::splitString().
Referenced by readColumn(), and readPoints().
|
static |
Returns a vector containing the names of columns in the file (assuming the file has a header)
Definition at line 25 of file CsvInterface.cpp.
References ERR(), and BaseLib::splitString().
|
inline |
Returns the number of vectors currently staged for writing.
Definition at line 47 of file CsvInterface.h.
References _vec_names.
Referenced by FileIO::SwmmInterface::writeCsvForObject(), and FileIO::SwmmInterface::writeCsvForTimestep().
|
private |
Returns the size of the vector with the given index.
Definition at line 295 of file CsvInterface.cpp.
References _data.
Referenced by addVectorForWriting(), and write().
|
inlinestaticprivate |
Actual column reader for public methods.
Definition at line 199 of file CsvInterface.h.
References ERR(), and BaseLib::splitString().
|
inlinestatic |
Definition at line 178 of file CsvInterface.h.
References ERR(), and readColumn().
|
inlinestatic |
Reads a column of the given name from a CSV file.
fname | Name of the file to be read |
delim | Deliminator, default is ',' |
column_name | The column's name to read |
Definition at line 155 of file CsvInterface.h.
References ERR(), findColumn(), and readColumn().
Referenced by readColumn(), and readColumn().
|
staticprivate |
Actual point reader for public methods.
Definition at line 172 of file CsvInterface.cpp.
References ERR(), and BaseLib::splitString().
|
static |
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 interpreted as x-, y- and z-coordinate, respectively.
fname | Name of the file to be read |
delim | Deliminator, default is ',' |
points | A vector containing the 3D points read from the file |
Definition at line 64 of file CsvInterface.cpp.
References ERR(), and BaseLib::splitString().
Referenced by readPoints(), and readPoints().
|
static |
Reads 3D points from a headerless CSV file, so columns for x-, y- and z-coordinates have to be specified using indices (starting with 0). If z_column_idx is not given (or set to numeric_limits::max()), all z-coordinates will be set to zero.
fname | Name of the file to be read |
delim | Deliminator, default is ',' |
points | A vector containing the 3D points read from the file |
x_column_idx | Index of the column to be interpreted as x-coordinate |
y_column_idx | Index of the column to be interpreted as y-coordinate |
z_column_idx | Index of the column to be interpreted as z-coordinate |
Definition at line 149 of file CsvInterface.cpp.
References ERR(), and readPoints().
|
static |
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 columns specified in the function call will be used for reading x-, y- and z-coordinates, respectively If z_column_name is an empty string or not given at all, all z-coordinates will be set to zero.
fname | Name of the file to be read |
delim | Deliminator, default is ',' |
points | A vector containing the 3D points read from the file |
x_column_name | Name of the column to be interpreted as x-coordinate |
y_column_name | Name of the column to be interpreted as y-coordinate |
z_column_name | Name of the column to be interpreted as z-coordinate |
Definition at line 112 of file CsvInterface.cpp.
References ERR(), findColumn(), and readPoints().
|
inline |
Stores if the CSV file to be written should include a header or not.
Definition at line 58 of file CsvInterface.h.
References _writeCsvHeader.
|
overridevirtual |
Writes the CSV file.
Implements BaseLib::IO::Writer.
Definition at line 261 of file CsvInterface.cpp.
References _data, _vec_names, _writeCsvHeader, ERR(), getVectorSize(), BaseLib::IO::Writer::out, and writeValue().
|
private |
Writes a value from a vector to the file.
vec_idx | Index of the vector |
in_vec_idx | Entry in the selected vector |
Definition at line 312 of file CsvInterface.cpp.
References _data, and BaseLib::IO::Writer::out.
Referenced by write().
|
private |
Definition at line 255 of file CsvInterface.h.
Referenced by addVectorForWriting(), getVectorSize(), write(), and writeValue().
|
private |
Definition at line 254 of file CsvInterface.h.
Referenced by addVectorForWriting(), getNArrays(), and write().
|
private |
Definition at line 253 of file CsvInterface.h.
Referenced by setCsvHeader(), and write().