/dev/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/dev/trunk

« back to all changes in this revision

Viewing changes to apps/csv2root/csv.cpp

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <iostream>
 
2
#include <vector>
 
3
#include <string>
 
4
#include <stdexcept>
 
5
 
 
6
#include <boost/algorithm/string/classification.hpp>
 
7
#include <boost/algorithm/string/split.hpp>
 
8
#include <boost/algorithm/string/trim.hpp>
 
9
#include <boost/lambda/lambda.hpp>
 
10
#include <boost/lambda/bind.hpp>
 
11
#include <boost/lexical_cast.hpp>
 
12
 
 
13
#include <boost/date_time/time_formatting_streams.hpp>
 
14
#include <boost/date_time/local_time/local_time.hpp>
 
15
#include <boost/date_time/local_time/local_time_io.hpp>
 
16
#include <boost/date_time/posix_time/posix_time.hpp>
 
17
#include <boost/date_time/microsec_time_clock.hpp>
 
18
#include <boost/date_time/c_local_time_adjustor.hpp>
 
19
#include <boost/date_time/local_time_adjustor.hpp>
 
20
 
 
21
#include "csv.hpp"
 
22
 
 
23
using namespace ds;
 
24
using namespace std;
 
25
 
 
26
using namespace boost;
 
27
using namespace boost::lambda;
 
28
using namespace boost::date_time;
 
29
using namespace boost::local_time;
 
30
using namespace boost::posix_time;
 
31
 
 
32
typedef boost::split_iterator<string::iterator> string_split_iterator;
 
33
 
 
34
 
 
35
template <class EC>
 
36
void CSVCustom<EC>::set_date_format(string &format) {
 
37
    date_format = format;    
 
38
}
 
39
 
 
40
 
 
41
template <class EC>
 
42
void CSVCustom<EC>::get_headers(std::vector<string> &headers) {
 
43
    csv_string s;
 
44
    
 
45
    if (std::getline(*CSVCustom<EC>::csv_stream, s, '\n')) {
 
46
        s.split(headers, ',');
 
47
    } else {
 
48
        throw invalid_argument("No data is supplied");
 
49
    }
 
50
}
 
51
 
 
52
template <class EC>
 
53
void CSVCustom<EC>::parse_time(boost::posix_time::ptime &parsed_time, std::string &string_time) {
 
54
    boost::date_time::time_input_facet<ptime,char> *csv_facet = new boost::date_time::time_input_facet<ptime,char>(date_format);
 
55
    std::locale csv_time_locale(std::locale::classic(), csv_facet);
 
56
 
 
57
    stringstream ss(string_time);
 
58
    ss.exceptions(std::ios_base::failbit);
 
59
    ss.imbue(csv_time_locale);
 
60
    ss >> parsed_time;
 
61
}
 
62
 
 
63
 
 
64
template <class EC>
 
65
bool CSVCustom<EC>::get_row(boost::posix_time::ptime &header, vector<EC> &data) {
 
66
    csv_string s;
 
67
 
 
68
    data.clear();
 
69
    
 
70
    while (std::getline(*CSVCustom<EC>::csv_stream, s, '\n')) {
 
71
        ds::split_iterator i = s.create_split_iterator(',');
 
72
        if (i == ds::split_iterator()) continue;
 
73
        
 
74
        csv_string s1 = *i;
 
75
        parse_time(header, s1);
 
76
 
 
77
        for (++i; i!=ds::split_iterator(); ++i) {
 
78
            data.push_back(lexical_cast<EC>(*i));
 
79
        }
 
80
        return true;
 
81
    }
 
82
    return false;
 
83
}
 
84
 
 
85
template <class EC>
 
86
bool CSVCustom<EC>::get_row(boost::posix_time::ptime &header, std::size_t n_columns, EC *column) {
 
87
    csv_string s;
 
88
 
 
89
    while (std::getline(*CSVCustom<EC>::csv_stream, s, '\n')) {
 
90
        ds::split_iterator i = s.create_split_iterator(',');
 
91
        if (i == ds::split_iterator()) continue;
 
92
        
 
93
        csv_string s1 = *i;
 
94
        parse_time(header, s1);
 
95
 
 
96
        size_t counter = 0;
 
97
        for (++i; i != ds::split_iterator(); ++i, ++counter) {
 
98
            if (counter == n_columns) break;
 
99
            column[counter] = lexical_cast<EC>(*i);
 
100
        }
 
101
        
 
102
        for (;counter < n_columns; ++counter) {
 
103
            column[counter] = lexical_cast<EC>(0);
 
104
        }
 
105
        
 
106
        return true;
 
107
    }
 
108
    return false;
 
109
}
 
110
 
 
111
 
 
112
template <class EC>
 
113
bool CSVCustom<EC>::get_row(time_t &header, vector<EC> &data) {
 
114
    boost::posix_time::ptime cpp_time;
 
115
    if (get_row(cpp_time, data)) {
 
116
//      header = mktime(&to_tm(tm));
 
117
        boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
 
118
        header = (cpp_time - epoch).total_seconds();
 
119
        return true;
 
120
    }
 
121
    return false;    
 
122
}
 
123
 
 
124
template <class TElement>
 
125
bool CSVCustom<TElement>::get_row(time_t &header, std::size_t n_columns, TElement *column) {
 
126
    boost::posix_time::ptime cpp_time;
 
127
    if (get_row(cpp_time, n_columns, column)) {
 
128
        boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
 
129
        header = (cpp_time - epoch).total_seconds();
 
130
        return true;
 
131
    }
 
132
    return false;    
 
133
}
 
134
 
 
135
 
 
136
template <class EC>
 
137
bool CSVCustom<EC>::skip_rows(boost::posix_time::ptime to) {
 
138
    return false;
 
139
}
 
140
 
 
141
 
 
142
template class CSVCustom<>;