Wt examples  3.7.1
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  } else
35  dt = data;
36 
38  }
39 };
40 
41 Wt::WStandardItemModel *csvToModel(const std::string& csvFile,
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 
56 void 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 }
virtual bool insertColumns(int column, int count, const WModelIndex &parent=WModelIndex())
static WString fromUTF8(const std::string &value, bool checkValid=false)
virtual bool insertRows(int row, int count, const WModelIndex &parent=WModelIndex())
virtual cpp17::any data(ItemDataRole role=ItemDataRole::User) const
virtual NumericItem * clone() const
Definition: CsvUtil.C:18
virtual void setData(const boost::any &data, int role=Wt::UserRole)
Definition: CsvUtil.C:22
virtual bool setData(const WModelIndex &index, const cpp17::any &value, ItemDataRole role=ItemDataRole::Edit)
virtual int rowCount(const WModelIndex &parent=WModelIndex()) const=0
virtual void setData(const cpp17::any &data, ItemDataRole role=ItemDataRole::User)
std::string toUTF8() const
WString asString(const cpp17::any &v, const WString &formatString=WString())
void setItemPrototype(std::unique_ptr< WStandardItem > item)
virtual int columnCount(const WModelIndex &parent=WModelIndex()) const=0
Wt::WStandardItemModel * csvToModel(const std::string &csvFile, Wt::WObject *parent, bool firstLineIsHeaders)
Definition: CsvUtil.C:41
virtual bool setHeaderData(int section, Orientation orientation, const cpp17::any &value, ItemDataRole role=ItemDataRole::Edit)
WStandardItemModel * model() const
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:56
WStandardItem * parent() const

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13