Wt examples 3.3.12
Classes | Functions
CsvUtil.C File Reference
#include <fstream>
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>
#include <Wt/WAbstractItemModel>
#include <Wt/WStandardItemModel>
#include <Wt/WStandardItem>
#include <Wt/WString>
#include "CsvUtil.h"

Go to the source code of this file.

Classes

class  NumericItem
 

Functions

Wt::WStandardItemModelcsvToModel (const std::string &csvFile, Wt::WObject *parent, bool firstLineIsHeaders)
 
void readFromCsv (std::istream &f, Wt::WAbstractItemModel *model, int numRows, bool firstLineIsHeaders)
 Utility function that reads a model from a CSV file.
 

Function Documentation

◆ csvToModel()

Wt::WStandardItemModel * csvToModel ( const std::string &  csvFile,
Wt::WObject parent,
bool  firstLineIsHeaders 
)

Definition at line 41 of file CsvUtil.C.

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}
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
void setItemPrototype(WStandardItem *item)

◆ readFromCsv()

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

Utility function that reads a model from a CSV file.

Definition at line 56 of file CsvUtil.C.

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())
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
static WString fromUTF8(const std::string &value, bool checkValid=false)

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