Wt examples  3.3.4
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  */
17 public:
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  }
35 
37  }
38 };
39 
40 Wt::WStandardItemModel *csvToModel(const std::string& csvFile,
41  Wt::WObject *parent,
42  bool firstLineIsHeaders)
43 {
44  std::ifstream f(csvFile.c_str());
45 
46  if (f) {
47  Wt::WStandardItemModel *result = new Wt::WStandardItemModel(0, 0, parent);
48  result->setItemPrototype(new NumericItem());
49  readFromCsv(f, result, -1, firstLineIsHeaders);
50  return result;
51  } else
52  return 0;
53 }
54 
55 void readFromCsv(std::istream& f, Wt::WAbstractItemModel *model,
56  int numRows, bool firstLineIsHeaders)
57 {
58  int csvRow = 0;
59 
60  while (f) {
61  std::string line;
62  getline(f, line);
63 
64  if (f) {
65  typedef boost::tokenizer<boost::escaped_list_separator<char> >
66  CsvTokenizer;
67  CsvTokenizer tok(line);
68 
69  int col = 0;
70  for (CsvTokenizer::iterator i = tok.begin();
71  i != tok.end(); ++i, ++col) {
72 
73  if (col >= model->columnCount())
74  model->insertColumns(model->columnCount(),
75  col + 1 - model->columnCount());
76 
77  if (firstLineIsHeaders && csvRow == 0)
78  model->setHeaderData(col, boost::any(Wt::WString::fromUTF8(*i)));
79  else {
80  int dataRow = firstLineIsHeaders ? csvRow - 1 : csvRow;
81 
82  if (numRows != -1 && dataRow >= numRows)
83  return;
84 
85  if (dataRow >= model->rowCount())
86  model->insertRows(model->rowCount(),
87  dataRow + 1 - model->rowCount());
88 
89  boost::any data(Wt::WString::fromUTF8(*i));
90  model->setData(dataRow, col, data);
91  }
92  }
93  }
94 
95  ++csvRow;
96  }
97 }
virtual bool insertColumns(int column, int count, const WModelIndex &parent=WModelIndex())
WString asString(const boost::any &v, const WString &formatString=WString())
virtual NumericItem * clone() const
Definition: CsvUtil.C:18
static WString fromUTF8(const std::string &value, bool checkValid=false)
virtual bool insertRows(int row, int count, const WModelIndex &parent=WModelIndex())
virtual void setData(const boost::any &data, int role=Wt::UserRole)
Definition: CsvUtil.C:22
virtual int rowCount(const WModelIndex &parent=WModelIndex()) const =0
void setItemPrototype(WStandardItem *item)
std::string toUTF8() const
virtual boost::any data(int role=UserRole) const
virtual int columnCount(const WModelIndex &parent=WModelIndex()) const =0
Wt::WStandardItemModel * csvToModel(const std::string &csvFile, Wt::WObject *parent, bool firstLineIsHeaders)
Definition: CsvUtil.C:40
virtual void setData(const boost::any &data, int role=UserRole)
virtual bool setHeaderData(int section, Orientation orientation, const boost::any &value, int role=EditRole)
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:55
virtual bool setData(const WModelIndex &index, const boost::any &value, int role=EditRole)

Generated on Fri Nov 13 2015 for the C++ Web Toolkit (Wt) by doxygen 1.8.9.1