Skip to content

Commit 9b8357b

Browse files
author
Toni Cebrián
committed
Extended the example to accept Bziped files
1 parent 2b2ac03 commit 9b8357b

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ find_package(Boost COMPONENTS iostreams REQUIRED)
1111

1212
add_executable(csvItExample src/main.cpp)
1313

14+
target_link_libraries(csvItExample ${Boost_IOSTREAMS_LIBRARIES})
15+
1416
# Testing
1517
find_package(CppUnit REQUIRED)
1618
add_executable(UnitTester test/main.cpp

src/main.cpp

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
#include <csv_iterator.hpp>
2+
#include <iostream>
23
#include <fstream>
34
#include <boost/tuple/tuple.hpp>
45

6+
// Compressed streams
7+
#include <boost/iostreams/filtering_stream.hpp>
8+
#include <boost/iostreams/filter/bzip2.hpp>
9+
510
int main(int argc, const char *argv[])
611
{
712
// Example of csv_iterator usage
8-
if(argc != 2){
9-
std::cerr << "Usage ./csvItExample <file.csv>" << std::endl;
10-
return 1;
11-
}
12-
13-
std::ifstream in(argv[1]);
1413
using namespace boost::tuples;
1514
typedef tuple<int,std::string,double> record;
16-
csv::iterator<record> it(in);
1715

18-
double acc = 0.0;
16+
std::ifstream in;
17+
boost::iostreams::filtering_istream bzs;
18+
csv::iterator<record> it;
19+
20+
if (argc == 2){
21+
in.open(argv[1],std::ifstream::in);
22+
it=csv::iterator<record>(in);
23+
} else if (argc == 3) {
24+
in.open(argv[2], std::ios_base::in | std::ios_base::binary);
25+
bzs.push(boost::iostreams::bzip2_decompressor());
26+
bzs.push(in);
27+
it=csv::iterator<record>(bzs);
28+
}
29+
else{
30+
std::cerr << "Usage ./csvItExample [-BZIP] <file.csv>" << std::endl;
31+
return 1;
32+
}
33+
34+
unsigned long long acc = 0;
1935
std::for_each(it,csv::iterator<record>(),
20-
[&acc](const record& rec){
21-
acc += rec.get<0>();
22-
});
36+
[&acc](const record& rec){
37+
acc += rec.get<0>();
38+
});
2339

2440
std::cout << acc << std::endl;
25-
41+
2642
return 0;
2743
}

0 commit comments

Comments
 (0)