Wt examples  3.3.5
/usr/src/RPM/BUILD/wt-3.3.5-rc2/examples/treeview-dragdrop/CsvUtil.C
Go to the documentation of this file.
00001 #include <fstream>
00002 
00003 #include <boost/tokenizer.hpp>
00004 #include <boost/lexical_cast.hpp>
00005 
00006 #include <Wt/WAbstractItemModel>
00007 #include <Wt/WStandardItemModel>
00008 #include <Wt/WStandardItem>
00009 #include <Wt/WString>
00010 
00011 #include "CsvUtil.h"
00012 
00013 /*
00014  * A standard item which converts text edits to numbers
00015  */
00016 class NumericItem : public Wt::WStandardItem {
00017 public:
00018   virtual NumericItem *clone() const {
00019     return new NumericItem();
00020   }
00021 
00022   virtual void setData(const boost::any &data, int role = Wt::UserRole) {
00023     boost::any dt;
00024 
00025     if (role == Wt::EditRole) {
00026       std::string s = Wt::asString(data).toUTF8();
00027 
00028       char *end;
00029       double d = std::strtod(s.c_str(), &end);
00030       if (*end == 0)
00031         dt = boost::any(d);
00032       else
00033         dt = data;
00034     } else
00035       dt = data;
00036 
00037     Wt::WStandardItem::setData(dt, role);
00038   }
00039 };
00040 
00041 Wt::WStandardItemModel *csvToModel(const std::string& csvFile,
00042                                    Wt::WObject *parent,
00043                                    bool firstLineIsHeaders)
00044 {
00045   std::ifstream f(csvFile.c_str());
00046 
00047   if (f) {
00048     Wt::WStandardItemModel *result = new Wt::WStandardItemModel(0, 0, parent);
00049     result->setItemPrototype(new NumericItem());
00050     readFromCsv(f, result, -1, firstLineIsHeaders);
00051     return result;
00052   } else
00053     return 0;
00054 }
00055 
00056 void readFromCsv(std::istream& f, Wt::WAbstractItemModel *model,
00057                  int numRows, bool firstLineIsHeaders)
00058 {
00059   int csvRow = 0;
00060 
00061   while (f) {
00062     std::string line;
00063     getline(f, line);
00064 
00065     if (f) {
00066       typedef boost::tokenizer<boost::escaped_list_separator<char> >
00067         CsvTokenizer;
00068       CsvTokenizer tok(line);
00069 
00070       int col = 0;
00071       for (CsvTokenizer::iterator i = tok.begin();
00072            i != tok.end(); ++i, ++col) {
00073 
00074         if (col >= model->columnCount())
00075           model->insertColumns(model->columnCount(),
00076                                col + 1 - model->columnCount());
00077 
00078         if (firstLineIsHeaders && csvRow == 0)
00079           model->setHeaderData(col, boost::any(Wt::WString::fromUTF8(*i)));
00080         else {
00081           int dataRow = firstLineIsHeaders ? csvRow - 1 : csvRow;
00082 
00083           if (numRows != -1 && dataRow >= numRows)
00084             return;
00085 
00086           if (dataRow >= model->rowCount())
00087             model->insertRows(model->rowCount(),
00088                               dataRow + 1 - model->rowCount());
00089 
00090           boost::any data(Wt::WString::fromUTF8(*i));
00091           model->setData(dataRow, col, data);
00092         }
00093       }
00094     }
00095 
00096     ++csvRow;
00097   }
00098 }

Generated on Tue Mar 22 2016 for the C++ Web Toolkit (Wt) by doxygen 1.7.6.1