Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include <stdlib.h>
00008 #include <boost/tokenizer.hpp>
00009
00010 #include <Wt/WAbstractItemModel>
00011 #include <Wt/WString>
00012
00013 #include "CsvUtil.h"
00014
00015 void readFromCsv(std::istream& f, Wt::WAbstractItemModel *model,
00016 int numRows, bool firstLineIsHeaders)
00017 {
00018 int csvRow = 0;
00019
00020 while (f) {
00021 std::string line;
00022 getline(f, line);
00023
00024 if (f) {
00025 typedef boost::tokenizer<boost::escaped_list_separator<char> >
00026 CsvTokenizer;
00027 CsvTokenizer tok(line);
00028
00029 int col = 0;
00030 for (CsvTokenizer::iterator i = tok.begin();
00031 i != tok.end(); ++i, ++col) {
00032
00033 if (col >= model->columnCount())
00034 model->insertColumns(model->columnCount(),
00035 col + 1 - model->columnCount());
00036
00037 if (firstLineIsHeaders && csvRow == 0)
00038 model->setHeaderData(col, boost::any(Wt::WString::fromUTF8(*i)));
00039 else {
00040 int dataRow = firstLineIsHeaders ? csvRow - 1 : csvRow;
00041
00042 if (numRows != -1 && dataRow >= numRows)
00043 return;
00044
00045 if (dataRow >= model->rowCount())
00046 model->insertRows(model->rowCount(),
00047 dataRow + 1 - model->rowCount());
00048
00049 boost::any data;
00050 std::string s = *i;
00051
00052 char *endptr;
00053
00054
00055
00056
00057
00058
00059 double d = strtod(s.c_str(), &endptr);
00060 if (*endptr == 0)
00061 data = boost::any(d);
00062 else
00063 data = boost::any(Wt::WString::fromUTF8(s));
00064
00065
00066 model->setData(dataRow, col, data);
00067 }
00068 }
00069 }
00070
00071 ++csvRow;
00072 }
00073 }