AuxPort 0.2
Modules for Audio Software Development
Loading...
Searching...
No Matches
AuxPort::CSV Class Reference

This class provides functionality to read and write CSV Files. More...

#include <AuxFile.h>

Inheritance diagram for AuxPort::CSV:
AuxPort::TextFile AuxPort::TextFormat AuxPort::File AuxPort::ILog AuxPort::ILog

Public Member Functions

 CSV (const CSV &csv)=default
 
bool open (const std::string &fileName, const Mode &mode=Mode::Read, bool containsHeader=false, const char &delimiter=',', bool log=false)
 Opens a CSV File.
 
bool close (bool log=false)
 Closes the stream to currently opened CSV File.
 
void read (std::vector< std::vector< std::string > > &data)
 Reads data from a CSV file and stores it in the 2-d Vector.
 
void read (std::vector< std::vector< std::string > > &data, std::vector< std::string > &headers)
 Reads data from a CSV file.
 
bool getDataRow (std::vector< std::string > &dataRow, size_t rowNum)
 Reads a specific data row from the file.
 
void setHeader (const std::vector< std::string > &header)
 Sets the header row to be written to the CSV File.
 
void addDataRow (const std::vector< std::string > &dataRow)
 Adds a data row to be written to the CSV File.
 
void write ()
 Writes a CSV file using information set through addDataRow(), setHeader() functions.
 
void write (const std::vector< std::vector< std::string > > &data, const std::vector< std::string > &header=std::vector< std::string >())
 Writes a CSV file with the given data and header rows.
 
void Log () override
 Logs the file info.
 
- Public Member Functions inherited from AuxPort::TextFormat
 TextFormat (const TextFormat &textFormat)=default
 
void Log () override
 Implement this method in the derived class to specify logging behaviour.
 
std::string & getFormatName ()
 Return the format name.
 
std::string & getUniqueIdentifier ()
 Return the unique identifier for the text format.
 
std::string & getExtensionName ()
 Returns the extension name.
 
void setExtensionName (const std::string &extensionName)
 Sets the extension name.
 
void setUniqueIdentifier (const std::string &uniqueIdentifier)
 Sets the unique identifier for the text format.
 
void setFormatName (const std::string &formatName)
 Sets the format name.
 

Additional Inherited Members

- Protected Types inherited from AuxPort::File
enum  Mode { Idle = 0 , Read = 1 , Write = 2 }
 Easy way to specify the Read/Write behavior of the File Stream. More...
 
- Protected Member Functions inherited from AuxPort::TextFile
 TextFile ()=default
 Default Constructor, Initializes the TextFile API.
 
 ~TextFile ()=default
 Default Destructor.
 
std::string & readFileAsString ()
 Reads the entire text file and saves it in a String.
 
void writeLineToFile (const std::string &data, bool log=false)
 Writes a line to the Text File.
 
bool readLineFromFile (std::string &line, bool log=false)
 Reads a line to the Text File.
 
bool open (const std::string &fileName, const Mode &mode=Mode::Read, bool log=false) override
 Opens the given file as a text file.
 
bool close (bool log=false) override
 Closes current text file stream.
 
- Protected Member Functions inherited from AuxPort::File
 File (const File &file)=default
 
std::string getCurrentDirectory ()
 Returns the current directory, the program is running in.
 
std::vector< std::string > & getListOfFiles (const std::string &extension="")
 Returns list of files present in the current directory.
 
void Log () override
 Override this function to specify logging behavior.
 
- Protected Member Functions inherited from AuxPort::ILog
void setColour (const ColourType &colourType)
 This function allows you to set text colour for the text to be printed onto the console.
 
- Protected Member Functions inherited from AuxPort::TextFormat
 TextFormat (const TextFormat &textFormat)=default
 
void Log () override
 Implement this method in the derived class to specify logging behaviour.
 
std::string & getFormatName ()
 Return the format name.
 
std::string & getUniqueIdentifier ()
 Return the unique identifier for the text format.
 
std::string & getExtensionName ()
 Returns the extension name.
 
void setExtensionName (const std::string &extensionName)
 Sets the extension name.
 
void setUniqueIdentifier (const std::string &uniqueIdentifier)
 Sets the unique identifier for the text format.
 
void setFormatName (const std::string &formatName)
 Sets the format name.
 
- Protected Attributes inherited from AuxPort::File
std::unique_ptr< std::ofstream > fileWriter
 
std::unique_ptr< std::ifstream > fileReader
 
std::string fileName
 
std::string fileExtension
 
std::string errorMessage
 
Mode mode
 
- Protected Attributes inherited from AuxPort::TextFormat
std::string formatName
 
std::string uniqueIdentifier
 
std::string extensionName
 

Detailed Description

This class provides functionality to read and write CSV Files.

Definition at line 362 of file AuxFile.h.

Constructor & Destructor Documentation

◆ CSV()

AuxPort::CSV::CSV ( )

Definition at line 278 of file AuxFile.cpp.

Member Function Documentation

◆ addDataRow()

void AuxPort::CSV::addDataRow ( const std::vector< std::string > & dataRow)

Adds a data row to be written to the CSV File.

Note
This function does not write the data row to CSV File. Added data rows are written to file when write() is called.

Definition at line 361 of file AuxFile.cpp.

◆ close()

bool AuxPort::CSV::close ( bool log = false)
virtual

Closes the stream to currently opened CSV File.

Implements AuxPort::File.

Definition at line 292 of file AuxFile.cpp.

◆ getDataRow()

bool AuxPort::CSV::getDataRow ( std::vector< std::string > & dataRow,
size_t rowNum )

Reads a specific data row from the file.

Returns
true if the read was sucessful. Otherwise, it returns false in case the given rowNum is greater than number of data rows in the file.
Note
If the containsHeader Flag has been set to false, all rows in the file will be considered as data rows and there will be no header rows. Otherwise, the first row will be considered a header row and all successive rows will be considered data rows.

Definition at line 322 of file AuxFile.cpp.

◆ Log()

void AuxPort::CSV::Log ( )
overridevirtual

Logs the file info.

Reimplemented from AuxPort::ILog.

Definition at line 373 of file AuxFile.cpp.

◆ open()

bool AuxPort::CSV::open ( const std::string & fileName,
const Mode & mode = Mode::Read,
bool containsHeader = false,
const char & delimiter = ',',
bool log = false )

Opens a CSV File.

Definition at line 285 of file AuxFile.cpp.

◆ read() [1/2]

void AuxPort::CSV::read ( std::vector< std::vector< std::string > > & data)

Reads data from a CSV file and stores it in the 2-d Vector.

Note
If the containsHeader Flag has been set to false, all rows in the file will be considered as data rows. Otherwise, the first row will not be considered a data row and will not be populated in the given vector.

Definition at line 297 of file AuxFile.cpp.

◆ read() [2/2]

void AuxPort::CSV::read ( std::vector< std::vector< std::string > > & data,
std::vector< std::string > & headers )

Reads data from a CSV file.

Note
If the containsHeader Flag has been set to false, all rows in the file will be considered as data rows and there will be no header rows. Otherwise, the first row will be considered a header row and all successive rows will be considered data rows.

Definition at line 304 of file AuxFile.cpp.

◆ setHeader()

void AuxPort::CSV::setHeader ( const std::vector< std::string > & header)

Sets the header row to be written to the CSV File.

Note
This function does not write the headers to CSV File, instead, it sets the headers which will be written to file later using write().

Definition at line 356 of file AuxFile.cpp.

◆ write() [1/2]

void AuxPort::CSV::write ( )

Writes a CSV file using information set through addDataRow(), setHeader() functions.

Note
File must be opened in Write mode.
If containsHeader flag is set to false, then headers will not be written (even if they have been set using setHeaders()).

Definition at line 341 of file AuxFile.cpp.

◆ write() [2/2]

void AuxPort::CSV::write ( const std::vector< std::vector< std::string > > & data,
const std::vector< std::string > & header = std::vector<std::string>() )

Writes a CSV file with the given data and header rows.

Note
File must be opened in Write mode.
If containsHeader flag is set to false, then headers will not be written (even if they have been set using setHeaders()).

Definition at line 366 of file AuxFile.cpp.


The documentation for this class was generated from the following files: