iLand
csvfile.h
Go to the documentation of this file.
1/********************************************************************************************
2** iLand - an individual based forest landscape and disturbance model
3** http://iland-model.org
4** Copyright (C) 2009- Werner Rammer
5**
6** This program is free software: you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation, either version 3 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program. If not, see <http://www.gnu.org/licenses/>.
18********************************************************************************************/
19
20#ifndef CSVFILE_H
21#define CSVFILE_H
22
23#include <QObject>
24#include <QStringList>
25#include <QJSEngine>
26class CSVFile : public QObject
27{
28 Q_OBJECT
29 Q_PROPERTY(bool captions WRITE setHasCaptions READ hasCaptions)
30 Q_PROPERTY(bool flat WRITE setFlat READ flat)
31 Q_PROPERTY(int colCount READ colCount)
32 Q_PROPERTY(int rowCount READ rowCount)
33public:
34 CSVFile(QObject *parent=0);
35 CSVFile(const QString &fileName) { mHasCaptions = true; mFlat = false; mFixedWidth=false; loadFile(fileName);}
36 // actions
37 bool openFile(const QString &fileName);
38 QVariant colValue(const int col);
39 bool next();
40 // properties
41 bool streamingMode() const { return mStreamingMode; }
42 bool hasCaptions() const { return mHasCaptions; }
43 bool flat() const { return mFlat; }
44 int rowCount() const { return mRowCount; }
45 int colCount() const { return mColCount; }
46 bool isEmpty() const { return mIsEmpty; }
47 QStringList captions() const { return mCaptions; }
48 QStringList column(const int col) const;
49 QVariantList values(const int row) const;
50 // setters
51 void setHasCaptions(const bool hasCaps) { mHasCaptions = hasCaps; }
52 void setFixedWidth(const bool hasFixedWidth) { mFixedWidth = hasFixedWidth; }
53 void setFlat(const bool isflat) { mFlat = isflat; }
54 static void addToScriptEngine(QJSEngine &engine); // called during setup of ScriptEngine
55public slots:
56 bool loadFile(const QString &fileName);
57 bool loadFromString(const QString &content);
58 QString columnName(const int col) { if (col<mColCount) return mCaptions[col]; return QString(); }
59 int columnIndex(const QString &columnName) const { return mCaptions.indexOf(columnName); }
60 // value function with a column name
61 QVariant value(const int row, const QString column_name) const { return value(row, columnIndex(column_name)); }
62
64 QVariant value(const int row, const int col) const;
65 QVariant row(const int row);
66 QJSValue jsValue(const int row, const int col) const;
67 QJSValue jsValue(const int row, const QString column_name) const { return jsValue(row, columnIndex(column_name)); }
68
69 void setValue(const int row, const int col, QVariant value);
70 void saveFile(const QString &fileName);
71
72private:
73 void clear();
74 bool mIsEmpty;
75 bool mHasCaptions;
76 bool mFixedWidth;
77 bool mFlat;
78 bool mStreamingMode;
79 QStringList mCaptions;
80 QStringList mRows;
81 QString mSeparator;
82 int mRowCount;
83 int mColCount;
84};
85
86#endif // CSVFILE_H
Provides access to table data stored in text files (CSV style).
Definition: csvfile.h:27
void setFixedWidth(const bool hasFixedWidth)
Definition: csvfile.h:52
void saveFile(const QString &fileName)
save the current content to a file
Definition: csvfile.cpp:336
CSVFile(QObject *parent=0)
Definition: csvfile.cpp:56
QStringList captions() const
returns true when no valid file has been loaded (returns false when a file with 0 rows is loaded)
Definition: csvfile.h:47
int colCount
Definition: csvfile.h:31
int columnIndex(const QString &columnName) const
index of column or -1 if not available
Definition: csvfile.h:59
bool next()
advance to next record (i.e. line). return false if end of file is reached.
QStringList column(const int col) const
retrieve a string list of a given row
Definition: csvfile.cpp:285
bool captions
if true, the first line are considered to be headers
Definition: csvfile.h:29
QJSValue jsValue(const int row, const QString column_name) const
Definition: csvfile.h:67
bool flat
if true, there is only one column (a flat file)
Definition: csvfile.h:30
bool streamingMode() const
return true, if in "streaming mode" (for large files)
Definition: csvfile.h:41
bool hasCaptions() const
true, if first line contains headers
Definition: csvfile.h:42
bool isEmpty() const
Definition: csvfile.h:46
QString columnName(const int col)
get caption of ith column.
Definition: csvfile.h:58
QVariant row(const int row)
retrieve content of the full row row as a QJSValue
Definition: csvfile.cpp:253
void setFlat(const bool isflat)
Definition: csvfile.h:53
bool flat() const
simple list, not multiple columns
Definition: csvfile.h:43
CSVFile(const QString &fileName)
ctor, load fileName.
Definition: csvfile.h:35
void setHasCaptions(const bool hasCaps)
Definition: csvfile.h:51
bool openFile(const QString &fileName)
open file in streaming mode.
Definition: csvfile.cpp:278
static void addToScriptEngine(QJSEngine &engine)
Definition: csvfile.cpp:44
int rowCount() const
number or rows (excl. captions), or -1.
Definition: csvfile.h:44
void setValue(const int row, const int col, QVariant value)
set the value of the column
Definition: csvfile.cpp:293
QVariant colValue(const int col)
get value of column with index col. Use in streaming mode.
QVariantList values(const int row) const
get a list of the values in row "row"
Definition: csvfile.cpp:149
bool loadFile(const QString &fileName)
load fileName. load the complete file at once.
Definition: csvfile.cpp:139
int colCount() const
number of columns, or -1
Definition: csvfile.h:45
QJSValue jsValue(const int row, const int col) const
Definition: csvfile.cpp:267
QVariant value(const int row, const QString column_name) const
Definition: csvfile.h:61
bool loadFromString(const QString &content)
load content from a given string.
Definition: csvfile.cpp:74
int rowCount
Definition: csvfile.h:32