Wt examples: /home/koen/project/wt/public-git/wt/examples/charts/CsvUtil.C Source File

Wt examples 3.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

/home/koen/project/wt/public-git/wt/examples/charts/CsvUtil.C
Go to the documentation of this file.

    00001 /*
    00002  * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
    00003  *
    00004  * See the LICENSE file for terms of use.
    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           if (s.empty())
    00055             data = boost::any();
    00056           else {
    00057             double d = strtod(s.c_str(), &endptr);
    00058             if (*endptr == 0)
    00059               data = boost::any(d);
    00060             else
    00061               data = boost::any(Wt::WString::fromUTF8(s));
    00062           }
    00063
    00064           model->setData(dataRow, col, data);
    00065         }
    00066       }
    00067     }
    00068
    00069     ++csvRow;
    00070   }
    00071 }