/home/koen/project/wt/cvs/wt/examples/treeview-dragdrop/CsvUtil.h File Reference

#include <iostream>

Go to the source code of this file.

Namespaces

namespace  Wt

Functions

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


Function Documentation

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

Definition at line 9 of file CsvUtil.C.

00011 {
00012   int csvRow = 0;
00013 
00014   while (f) {
00015     std::string line;
00016     getline(f, line);
00017 
00018     if (f) {
00019       typedef boost::tokenizer<boost::escaped_list_separator<char> >
00020         CsvTokenizer;
00021       CsvTokenizer tok(line);
00022 
00023       int col = 0;
00024       for (CsvTokenizer::iterator i = tok.begin();
00025            i != tok.end(); ++i, ++col) {
00026 
00027         if (col >= model->columnCount())
00028           model->insertColumns(model->columnCount(),
00029                                col + 1 - model->columnCount());
00030 
00031         if (firstLineIsHeaders && csvRow == 0)
00032           model->setHeaderData(col, boost::any(Wt::WString::fromUTF8(*i)));
00033         else {
00034           int dataRow = firstLineIsHeaders ? csvRow - 1 : csvRow;
00035 
00036           if (numRows != -1 && dataRow >= numRows)
00037             return;
00038 
00039           if (dataRow >= model->rowCount())
00040             model->insertRows(model->rowCount(),
00041                               dataRow + 1 - model->rowCount());
00042 
00043           boost::any data;
00044           std::string s = *i;
00045 
00046           try {
00047             int i = boost::lexical_cast<int>(s);
00048             data = boost::any(i);
00049           } catch (boost::bad_lexical_cast&) {
00050             try {
00051               double d = boost::lexical_cast<double>(s);
00052               data = boost::any(d);
00053             } catch (boost::bad_lexical_cast&) {
00054               data = boost::any(Wt::WString::fromUTF8(s));
00055             }
00056           }
00057 
00058           model->setData(dataRow, col, data);
00059         }
00060       }
00061     }
00062 
00063     ++csvRow;
00064   }
00065 }


Generated on Mon Jan 26 14:14:20 2009 for Wt by doxygen 1.5.6