Wt examples 3.3.12
CsvUtil.C
Go to the documentation of this file.
1#include <fstream>
2
3#include <boost/tokenizer.hpp>
4#include <boost/lexical_cast.hpp>
5
6#include <Wt/WAbstractItemModel>
7#include <Wt/WStandardItemModel>
8#include <Wt/WStandardItem>
9#include <Wt/WString>
10
11#include "CsvUtil.h"
12
13/*
14 * A standard item which converts text edits to numbers
15 */
17public:
18 virtual NumericItem *clone() const {
19 return new NumericItem();
20 }
21
22 virtual void setData(const boost::any &data, int role = Wt::UserRole) {
23 boost::any dt;
24
25 if (role == Wt::EditRole) {
26 std::string s = Wt::asString(data).toUTF8();
27
28 char *end;
29 double d = std::strtod(s.c_str(), &end);
30 if (*end == 0)
31 dt = boost::any(d);
32 else
33 dt = data;
34 } else
35 dt = data;
36
38 }
39};
40
41Wt::WStandardItemModel *csvToModel(const std::string& csvFile,
42 Wt::WObject *parent,
43 bool firstLineIsHeaders)
44{
45 std::ifstream f(csvFile.c_str());
46
47 if (f) {
48 Wt::WStandardItemModel *result = new Wt::WStandardItemModel(0, 0, parent);
49 result->setItemPrototype(new NumericItem());
50 readFromCsv(f, result, -1, firstLineIsHeaders);
51 return result;
52 } else
53 return 0;
54}
55
56void readFromCsv(std::istream& f, Wt::WAbstractItemModel *model,
57 int numRows, bool firstLineIsHeaders)
58{
59 int csvRow = 0;
60
61 while (f) {
62 std::string line;
63 getline(f, line);
64
65 if (f) {
66 typedef boost::tokenizer<boost::escaped_list_separator<char> >
67 CsvTokenizer;
68 CsvTokenizer tok(line);
69
70 int col = 0;
71 for (CsvTokenizer::iterator i = tok.begin();
72 i != tok.end(); ++i, ++col) {
73
74 if (col >= model->columnCount())
75 model->insertColumns(model->columnCount(),
76 col + 1 - model->columnCount());
77
78 if (firstLineIsHeaders && csvRow == 0)
79 model->setHeaderData(col, boost::any(Wt::WString::fromUTF8(*i)));
80 else {
81 int dataRow = firstLineIsHeaders ? csvRow - 1 : csvRow;
82
83 if (numRows != -1 && dataRow >= numRows)
84 return;
85
86 if (dataRow >= model->rowCount())
87 model->insertRows(model->rowCount(),
88 dataRow + 1 - model->rowCount());
89
90 boost::any data(Wt::WString::fromUTF8(*i));
91 model->setData(dataRow, col, data);
92 }
93 }
94 }
95
96 ++csvRow;
97 }
98}
void readFromCsv(std::istream &f, Wt::WAbstractItemModel *model, int numRows, bool firstLineIsHeaders)
Utility function that reads a model from a CSV file.
Definition CsvUtil.C:15
virtual void setData(const boost::any &data, int role=Wt::UserRole)
Definition CsvUtil.C:22
virtual NumericItem * clone() const
Definition CsvUtil.C:18
virtual bool insertColumns(int column, int count, const WModelIndex &parent=WModelIndex())
virtual bool setHeaderData(int section, Orientation orientation, const boost::any &value, int role=EditRole)
virtual bool setData(const WModelIndex &index, const boost::any &value, int role=EditRole)
virtual int rowCount(const WModelIndex &parent=WModelIndex()) const=0
virtual bool insertRows(int row, int count, const WModelIndex &parent=WModelIndex())
virtual int columnCount(const WModelIndex &parent=WModelIndex()) const=0
void setItemPrototype(WStandardItem *item)
virtual boost::any data(int role=UserRole) const
virtual void setData(const boost::any &data, int role=UserRole)
static WString fromUTF8(const std::string &value, bool checkValid=false)
std::string toUTF8() const
WString asString(const boost::any &v, const WString &formatString=WString())
EditRole
UserRole
Wt::WStandardItemModel * csvToModel(const std::string &csvFile, Wt::WObject *parent, bool firstLineIsHeaders)
Definition CsvUtil.C:41

Generated on Fri May 17 2024 for the C++ Web Toolkit (Wt) by doxygen 1.9.8