Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 108 lines (90 sloc) 2.025 kB
9223f85 @thomasmoelhave Add compile line support, support splitting with year constraints
authored
1 #include "csv.h"
2 #include <boost/foreach.hpp>
3 #include <boost/tokenizer.hpp>
4 #include <boost/algorithm/string.hpp>
5 #include <limits>
6 #include <fstream>
7 #include <sstream>
8 #include <iostream>
9
10 void write_csv(const std::string& filename, std::vector<std::pair<size_t,point> >& v) {
11 std::ofstream f;
12 f.open(filename.c_str());
13
14 if (!f) {
15 throw std::runtime_error("Could not open file.");
16 }
17
18 for (int i = 0; i < v.size(); ++i) {
19 size_t id = v[i].first;
20 const point& p = v[i].second;
21 f << id << "," << p << "\n";
22 }
23 }
24
25 void read_csv(const std::string& filename, std::vector<point>& v, int& min_year, int& max_year) {
26 std::ifstream f;
27 f.open(filename.c_str());
28
29 if (!f) {
30 throw std::runtime_error("Could not open file.");
31 }
32
33 bool compute_max = false;
34 if (max_year==std::numeric_limits<int>::min()) {
35 compute_max=true;
36 }
37 bool compute_min = false;
38 if (min_year==std::numeric_limits<int>::max()) {
39 compute_min=true;
40 }
41 std::string line;
42
43 //skip header line;
44 std::getline(f,line);
45
46
47 while (std::getline(f,line)) {
48
49 boost::char_separator<char> sep(", \r");
50 boost::tokenizer<boost::char_separator<char> > tokens(line, sep);
51
52 int idx=0;
53 point p;
54
55 BOOST_FOREACH(std::string t, tokens)
56 {
57 for (size_t i = 0; i < t.length(); ++i) {
58 if (t[i]=='"') t[i]=' ';
59 }
60 boost::trim(t);
61
62 switch(idx) {
63 case 0: //id
64 case 1: //plt_cn
65 {
66 p.attribute(idx)=t;
67 } break;
68 case 5: //year
69 {
70 std::stringstream s(t);
71 int f;
72 s >> f;
73 assert(s);
74 p.year()=f;
75 } break;
76 case 2:
77 case 3:
78 case 4:
79 {
80 std::stringstream s(t);
81 float f;
82 s >> f;
83 assert(s);
84 p(idx-2)=f;
85 } break;
86 default:
87 assert(false);
88 };
89 ++idx;
90 }
91 bool ignore=false;
92 if (compute_min)
93 min_year=std::min(min_year,p.year());
94 else
95 ignore = ignore || p.year() < min_year;
96
97 if (compute_max)
98 max_year=std::max(max_year,p.year());
99 else
100 ignore = ignore || p.year() > max_year;
101
102 if (!ignore)
103 v.push_back(p);
104 }
105 }
106
107
Something went wrong with that request. Please try again.