LibrePCB Developers Documentation
CsvFile Class Referencefinal

The CsvFile class represents a comma-separated values (CSV) file. More...

#include <csvfile.h>

+ Collaboration diagram for CsvFile:

Public Member Functions

 CsvFile () noexcept
 
 CsvFile (const CsvFile &other)=delete
 
 ~CsvFile () noexcept
 
const QString & getComment () const noexcept
 Get the comment of the file. More...
 
const QStringList & getHeader () const noexcept
 Get the CSV header items. More...
 
const QList< QStringList > & getValues () const noexcept
 Get the CSV values. More...
 
void setComment (const QString &comment) noexcept
 Set file comment. More...
 
void setHeader (const QStringList &header) noexcept
 Set the header items. More...
 
void addValue (const QStringList &value)
 Add a row of values. More...
 
QString toString () const noexcept
 Build CSV file content and return it as a string. More...
 
void saveToFile (const FilePath &csvFp) const
 Write CSV file content to a file. More...
 
CsvFileoperator= (const CsvFile &rhs)=delete
 

Private Member Functions

QString getCommentLines () const noexcept
 
QString lineToString (const QStringList &line) const noexcept
 

Static Private Member Functions

static QString escapeValue (const QString &value) noexcept
 

Private Attributes

QString mComment
 
QStringList mHeader
 
QList< QStringList > mValues
 

Detailed Description

The CsvFile class represents a comma-separated values (CSV) file.

The class allows building CSV and write them to a file. It is guaranteed that the written files are valid:

  • Whenn adding a row with a wrong value count, addValue() throws an exception.
  • Linebreaks inside values are replaced by spaces.
  • If a value contains the separator character (e.g. the comma), the value gets quoted.
  • Quotes inside values are escaped.
Note
You have to call setHeader() before adding any values with addValue()! This is needed to make sure all value rows have the same value count as the header.
See also
https://en.wikipedia.org/wiki/Comma-separated_values

Constructor & Destructor Documentation

◆ CsvFile() [1/2]

CsvFile ( )
noexcept

◆ CsvFile() [2/2]

CsvFile ( const CsvFile other)
delete

◆ ~CsvFile()

~CsvFile ( )
noexcept

Member Function Documentation

◆ getComment()

const QString & getComment ( ) const
inlinenoexcept

Get the comment of the file.

Returns
File comment (raw comment without '#' at beginning of lines).

◆ getHeader()

const QStringList & getHeader ( ) const
inlinenoexcept

Get the CSV header items.

Returns
CSV header items (raw, i.e. without quotes and escaped characters).

◆ getValues()

const QList< QStringList > & getValues ( ) const
inlinenoexcept

Get the CSV values.

Returns
All value rows (raw, i.e. without quotes and escaped characters).

◆ setComment()

void setComment ( const QString &  comment)
noexcept

Set file comment.

Parameters
commentThe comment to set. May contain linebreaks.

◆ setHeader()

void setHeader ( const QStringList &  header)
noexcept

Set the header items.

Parameters
headerThe new header items.
Warning
This method clears all values!

◆ addValue()

void addValue ( const QStringList &  value)

Add a row of values.

Parameters
valueThe value row items.
Exceptions
librepcb::Exceptionif the value item count is different to the header item count.

◆ toString()

QString toString ( ) const
noexcept

Build CSV file content and return it as a string.

Returns
The string with the whole CSV file content.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ saveToFile()

void saveToFile ( const FilePath csvFp) const

Write CSV file content to a file.

Parameters
csvFpThe destination file path.
Exceptions
librepcb::Exceptionif the file could not be written.
+ Here is the call graph for this function:

◆ operator=()

CsvFile & operator= ( const CsvFile rhs)
delete

◆ getCommentLines()

QString getCommentLines ( ) const
privatenoexcept
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lineToString()

QString lineToString ( const QStringList &  line) const
privatenoexcept
+ Here is the caller graph for this function:

◆ escapeValue()

QString escapeValue ( const QString &  value)
staticprivatenoexcept

Member Data Documentation

◆ mComment

QString mComment
private

◆ mHeader

QStringList mHeader
private

◆ mValues

QList<QStringList> mValues
private

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