All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Radiant::CSVDocument Class Reference

A simple class for loading CSV documents. More...

#include <Radiant/CSVDocument.hpp>

Public Types

typedef std::vector< QString > Row
 A single row of data.
 
typedef std::list< RowRows
 A list of rows.
 

Public Member Functions

RowappendRow ()
 Adds a new row to the end of the table. More...
 
Rows::iterator begin ()
 Returns an iterator to the first row in the document. More...
 
Rows::const_iterator begin () const
 Returns an iterator to the first row in the document. More...
 
void clear ()
 Clears (empties) the entire CSVDocument.
 
 CSVDocument ()
 Constructor.
 
Rows::iterator end ()
 Returns an iterator after the last row of the document. More...
 
Rows::const_iterator end () const
 Returns an iterator after the last row of the document. More...
 
int findColumnOnRow (const QString &key, unsigned rowIndex)
 Finds column on a given row. More...
 
RowfindRow (const QString &key, unsigned col)
 Finds a row in the document. More...
 
int load (const QString &filename, const char *delimiter=",", bool removeQuotations=true)
 Load a file, and return the number of lines read. More...
 
int loadFromString (const QString &csv, const char *delimiter=",", bool removeQuotations=true)
 
Rowrow (unsigned i)
 Returns a given row. More...
 
unsigned rowCount () const
 Returns the number of rows in the document. More...
 
bool save (const QString &filename, const char *delimiter=",", bool useQuotations=true)
 
 ~CSVDocument ()
 Destructor.
 

Detailed Description

A simple class for loading CSV documents.

CSV (Comma-Separeted Values) documents are basically spreadsheets, with information placed on rows, with an agreed separator between the cells. The Cells are expected to have quotation marks around the main content. The quotation marks are removed in the reading process, as are leading-, and trailing spaces.

Member Function Documentation

Row* Radiant::CSVDocument::appendRow ( )

Adds a new row to the end of the table.

Returns
The newly created row.
Rows::iterator Radiant::CSVDocument::begin ( )
inline

Returns an iterator to the first row in the document.

Returns
STL-like iterator to the beginning of the rows
Rows::const_iterator Radiant::CSVDocument::begin ( ) const
inline

Returns an iterator to the first row in the document.

Returns
STL-like iterator to the beginning of the rows
Rows::iterator Radiant::CSVDocument::end ( )
inline

Returns an iterator after the last row of the document.

Returns
STL-like iterator to the end of the rows
Rows::const_iterator Radiant::CSVDocument::end ( ) const
inline

Returns an iterator after the last row of the document.

Returns
STL-like iterator to the end of the rows
int Radiant::CSVDocument::findColumnOnRow ( const QString &  key,
unsigned  rowIndex 
)

Finds column on a given row.

Parameters
keyValy of ccell to be searched
rowIndexRow to search
Returns
Number of column where the item was, -1 if not found.
Row* Radiant::CSVDocument::findRow ( const QString &  key,
unsigned  col 
)

Finds a row in the document.

For each row in the document, this function checks if the text in the cell at that column matches the argument key.

Parameters
keyThe key to match
colThe column that is used for matching
Returns
If the key could not matched, return 0, otherwise returns a pointer to the row.
int Radiant::CSVDocument::load ( const QString &  filename,
const char *  delimiter = ",",
bool  removeQuotations = true 
)

Load a file, and return the number of lines read.

The file is assumed to be in the UTF-8 format.

Parameters
filenamefilename to read
delimitercolumn delimiter
removeQuotationsAre the quotations around the value removed
Returns
number of rows read
Row* Radiant::CSVDocument::row ( unsigned  i)

Returns a given row.

If the index is out of range, zero pointer is returned.

This method is quite slow, since it must iterate through each row to find the correct one. If performance is an issue you should use the iterator-functions instead.

Parameters
iindex of the row
Returns
the ith row
unsigned Radiant::CSVDocument::rowCount ( ) const
inline

Returns the number of rows in the document.

Returns
number of rows