iLand
Public Slots | Public Member Functions | Static Public Member Functions | Properties | List of all members
CSVFile Class Reference

Provides access to table data stored in text files (CSV style). More...

#include <csvfile.h>

Inheritance diagram for CSVFile:

Public Slots

bool loadFile (const QString &fileName)
 load fileName. load the complete file at once. More...
 
bool loadFromString (const QString &content)
 load content from a given string. More...
 
QString columnName (const int col)
 get caption of ith column. More...
 
int columnIndex (const QString &columnName) const
 index of column or -1 if not available More...
 
QVariant value (const int row, const QString column_name) const
 
QVariant value (const int row, const int col) const
 get value of cell denoted by row and cell. Not available in streaming mode. More...
 
QVariant row (const int row)
 retrieve content of the full row row as a QJSValue More...
 
QJSValue jsValue (const int row, const int col) const
 
QJSValue jsValue (const int row, const QString column_name) const
 
void setValue (const int row, const int col, QVariant value)
 set the value of the column More...
 
void saveFile (const QString &fileName)
 save the current content to a file More...
 

Public Member Functions

 CSVFile (QObject *parent=0)
 
 CSVFile (const QString &fileName)
 ctor, load fileName. More...
 
bool openFile (const QString &fileName)
 open file in streaming mode. More...
 
QVariant colValue (const int col)
 get value of column with index col. Use in streaming mode. More...
 
bool next ()
 advance to next record (i.e. line). return false if end of file is reached. More...
 
bool streamingMode () const
 return true, if in "streaming mode" (for large files) More...
 
bool hasCaptions () const
 true, if first line contains headers More...
 
bool flat () const
 simple list, not multiple columns More...
 
int rowCount () const
 number or rows (excl. captions), or -1. More...
 
int colCount () const
 number of columns, or -1 More...
 
bool isEmpty () const
 
QStringList captions () const
 returns true when no valid file has been loaded (returns false when a file with 0 rows is loaded) More...
 
QStringList column (const int col) const
 retrieve a string list of a given row More...
 
QVariantList values (const int row) const
 get a list of the values in row "row" More...
 
void setHasCaptions (const bool hasCaps)
 
void setFixedWidth (const bool hasFixedWidth)
 
void setFlat (const bool isflat)
 

Static Public Member Functions

static void addToScriptEngine (QJSEngine &engine)
 

Properties

bool captions
 if true, the first line are considered to be headers More...
 
bool flat
 if true, there is only one column (a flat file) More...
 
int colCount
 
int rowCount
 

Detailed Description

Provides access to table data stored in text files (CSV style).

Tables have optionally headers in first line (hasCaptions()) and can use various delimiters ("tab",";",","," "). If separated by spaces, consecuteive spaces are merged. Table dimensions can be accessed with colCount() and rowCount(), cell values as QVariant are retrieved by value(). full rows are retrieved using row(). Files are loaded by loadFile() or by passing a filename to the constructor:

CSVFile file(fileName);
for (int row=0; row<file.rowCount(); row++)
for (int col=0; col<file.colCount(); col++)
value = file.value(row, col);
Provides access to table data stored in text files (CSV style).
Definition: csvfile.h:27
QVariant row(const int row)
retrieve content of the full row row as a QJSValue
Definition: csvfile.cpp:253
QVariant value(const int row, const QString column_name) const
Definition: csvfile.h:61

Planned is also a "streaming" mode for large files (loadFile(), while(file.next()) file.value(x) ), but not finsihed yet.

Constructor & Destructor Documentation

◆ CSVFile() [1/2]

CSVFile::CSVFile ( QObject *  parent = 0)

◆ CSVFile() [2/2]

CSVFile::CSVFile ( const QString &  fileName)
inline

ctor, load fileName.

Member Function Documentation

◆ addToScriptEngine()

void CSVFile::addToScriptEngine ( QJSEngine &  engine)
static

◆ captions()

QStringList CSVFile::captions ( ) const
inline

returns true when no valid file has been loaded (returns false when a file with 0 rows is loaded)

retrieve (a copy) of column headers

◆ colCount()

int CSVFile::colCount ( ) const
inline

number of columns, or -1

◆ column()

QStringList CSVFile::column ( const int  col) const

retrieve a string list of a given row

◆ columnIndex

int CSVFile::columnIndex ( const QString &  columnName) const
inlineslot

index of column or -1 if not available

◆ columnName

QString CSVFile::columnName ( const int  col)
inlineslot

get caption of ith column.

◆ colValue()

QVariant CSVFile::colValue ( const int  col)

get value of column with index col. Use in streaming mode.

◆ flat()

bool CSVFile::flat ( ) const
inline

simple list, not multiple columns

◆ hasCaptions()

bool CSVFile::hasCaptions ( ) const
inline

true, if first line contains headers

◆ isEmpty()

bool CSVFile::isEmpty ( ) const
inline

◆ jsValue [1/2]

QJSValue CSVFile::jsValue ( const int  row,
const int  col 
) const
slot

◆ jsValue [2/2]

QJSValue CSVFile::jsValue ( const int  row,
const QString  column_name 
) const
inlineslot

◆ loadFile

bool CSVFile::loadFile ( const QString &  fileName)
slot

load fileName. load the complete file at once.

◆ loadFromString

bool CSVFile::loadFromString ( const QString &  content)
slot

load content from a given string.

◆ next()

bool CSVFile::next ( )

advance to next record (i.e. line). return false if end of file is reached.

◆ openFile()

bool CSVFile::openFile ( const QString &  fileName)

open file in streaming mode.

◆ row

QVariant CSVFile::row ( const int  row)
slot

retrieve content of the full row row as a QJSValue

◆ rowCount()

int CSVFile::rowCount ( ) const
inline

number or rows (excl. captions), or -1.

◆ saveFile

void CSVFile::saveFile ( const QString &  fileName)
slot

save the current content to a file

save the contents of the CSVFile back to a file.

this removes all comments and uses the system line-end

◆ setFixedWidth()

void CSVFile::setFixedWidth ( const bool  hasFixedWidth)
inline

◆ setFlat()

void CSVFile::setFlat ( const bool  isflat)
inline

◆ setHasCaptions()

void CSVFile::setHasCaptions ( const bool  hasCaps)
inline

◆ setValue

void CSVFile::setValue ( const int  row,
const int  col,
QVariant  value 
)
slot

set the value of the column

◆ streamingMode()

bool CSVFile::streamingMode ( ) const
inline

return true, if in "streaming mode" (for large files)

◆ value [1/2]

QVariant CSVFile::value ( const int  row,
const int  col 
) const
slot

get value of cell denoted by row and cell. Not available in streaming mode.

◆ value [2/2]

QVariant CSVFile::value ( const int  row,
const QString  column_name 
) const
inlineslot

◆ values()

QVariantList CSVFile::values ( const int  row) const

get a list of the values in row "row"

Property Documentation

◆ captions

bool CSVFile::captions
readwrite

if true, the first line are considered to be headers

◆ colCount

int CSVFile::colCount
read

◆ flat

bool CSVFile::flat
readwrite

if true, there is only one column (a flat file)

◆ rowCount

int CSVFile::rowCount
read

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