Wt examples: treeview-dragdrop/CsvUtil.C File Reference

Wt examples 4.10.4

Loading...
Searching...
No Matches
Classes | Functions
CsvUtil.C File Reference
`#include <fstream>`
`#include <boost/tokenizer.hpp>`
`#include <Wt/WAbstractItemModel.h>`
`#include <Wt/WStandardItemModel.h>`
`#include <Wt/WStandardItem.h>`
`#include <Wt/WString.h>`
`#include "CsvUtil.h"`

Go to the source code of this file.

## Classes
class | NumericItem

## Functions
std::shared_ptr< WStandardItemModel > | csvToModel (const std::string &csvFile, bool firstLineIsHeaders)
void | readFromCsv (std::istream &f, std::shared_ptr< WAbstractItemModel > model, int numRows, bool firstLineIsHeaders)

## Function Documentation

## ◆ csvToModel()

std::shared_ptr< WStandardItemModel > csvToModel | ( | const std::string & | csvFile,
|  | bool | firstLineIsHeaders )

Definition at line 40 of file CsvUtil.C.

42{
43 std::ifstream f(csvFile.c_str());
44
45 if (f) {
46 std::shared_ptr<WStandardItemModel> result = std::make_shared<WStandardItemModel>(0, 0);
47 result->setItemPrototype(std::make_unique<NumericItem>());
48 readFromCsv(f, result, -1, firstLineIsHeaders);
49 return result;
50 } else
51 return nullptr;
52}
readFromCsv
void readFromCsv(std::istream &f, std::shared_ptr< WAbstractItemModel > model, int numRows, bool firstLineIsHeaders)
Definition CsvUtil.C:54

## ◆ readFromCsv()

void readFromCsv | ( | std::istream & | f,
|  | std::shared_ptr< WAbstractItemModel > | model,
|  | int | numRows,
|  | bool | firstLineIsHeaders )

Definition at line 54 of file CsvUtil.C.

56{
57 int csvRow = 0;
58
59 while (f) {
60 std::string line;
61 getline(f, line);
62
63 if (f) {
64 typedef boost::tokenizer<boost::escaped_list_separator<char> >
65 CsvTokenizer;
66 CsvTokenizer tok(line);
67
68 int col = 0;
69 for (CsvTokenizer::iterator i = tok.begin();
70 i != tok.end(); ++i, ++col) {
71
72 if (col >= model->columnCount())
73 model->insertColumns(model->columnCount(),
74 col + 1 - model->columnCount());
75
76 if (firstLineIsHeaders && csvRow == 0)
77 model->setHeaderData(col, cpp17::any{WString{*i}});
78 else {
79 int dataRow = firstLineIsHeaders ? csvRow - 1 : csvRow;
80
81 if (numRows != -1 && dataRow >= numRows)
82 return;
83
84 if (dataRow >= model->rowCount())
85 model->insertRows(model->rowCount(),
86 dataRow + 1 - model->rowCount());
87
88 cpp17::any data{WString{*i}};
89 model->setData(dataRow, col, data);
90 }
91 }
92 }
93
94 ++csvRow;
95 }
96}