Wt examples  4.10.4
Namespaces | Functions
CsvUtil.h File Reference
#include <iostream>

Go to the source code of this file.

Namespaces

 Wt
 

Functions

void readFromCsv (std::istream &f, Wt::WAbstractItemModel *model, int numRows=-1, bool firstLineIsHeaders=true)
 Utility function that reads a model from a CSV file. More...
 

Function Documentation

◆ readFromCsv()

void readFromCsv ( std::istream &  f,
Wt::WAbstractItemModel model,
int  numRows = -1,
bool  firstLineIsHeaders = true 
)

Utility function that reads a model from a CSV file.

Definition at line 15 of file CsvUtil.C.

17 {
18  int csvRow = 0;
19 
20  while (f) {
21  std::string line;
22  getline(f, line);
23 
24  if (f) {
25  typedef boost::tokenizer<boost::escaped_list_separator<char> >
26  CsvTokenizer;
27  CsvTokenizer tok(line);
28 
29  int col = 0;
30  for (CsvTokenizer::iterator i = tok.begin();
31  i != tok.end(); ++i, ++col) {
32 
33  if (col >= model->columnCount())
34  model->insertColumns(model->columnCount(),
35  col + 1 - model->columnCount());
36 
37  if (firstLineIsHeaders && csvRow == 0)
38  model->setHeaderData(col, Wt::cpp17::any(Wt::WString(*i)));
39  else {
40  int dataRow = firstLineIsHeaders ? csvRow - 1 : csvRow;
41 
42  if (numRows != -1 && dataRow >= numRows)
43  return;
44 
45  if (dataRow >= model->rowCount())
46  model->insertRows(model->rowCount(),
47  dataRow + 1 - model->rowCount());
48 
49  Wt::cpp17::any data;
50  std::string s = *i;
51 
52  char *endptr;
53 
54  if (s.empty())
55  data = Wt::cpp17::any();
56  else {
57  double d = strtod(s.c_str(), &endptr);
58  if (*endptr == 0)
59  data = Wt::cpp17::any(d);
60  else
61  data = Wt::cpp17::any(Wt::WString(s));
62  }
63 
64  model->setData(dataRow, col, data);
65  }
66  }
67  }
68 
69  ++csvRow;
70  }
71 }
virtual bool insertColumns(int column, int count, const WModelIndex &parent=WModelIndex())
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
virtual bool setHeaderData(int section, Orientation orientation, const cpp17::any &value, ItemDataRole role=ItemDataRole::Edit)
virtual bool setData(const WModelIndex &index, const cpp17::any &value, ItemDataRole role=ItemDataRole::Edit)