-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
43 lines (39 loc) · 1.63 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "utils.h"
#include "constant.h"
#include <boost/algorithm/string.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <log4cxx/logger.h>
static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("arcs.Utils"));
namespace Utils {
std::istream* ifstream(const std::string& filename) {
boost::iostreams::filtering_istream* stream = new boost::iostreams::filtering_istream();
try {
if (boost::algorithm::ends_with(filename, GZIP_EXT)) {
stream->push(boost::iostreams::gzip_decompressor());
} else if (boost::algorithm::ends_with(filename, BZIP_EXT)) {
stream->push(boost::iostreams::bzip2_decompressor());
}
stream->push(boost::iostreams::file_descriptor_source(filename));
} catch (...) {
SAFE_DELETE(stream);
}
return stream;
}
std::ostream* ofstream(const std::string& filename) {
boost::iostreams::filtering_ostream* stream = new boost::iostreams::filtering_ostream();
try {
if (boost::algorithm::ends_with(filename, GZIP_EXT)) {
stream->push(boost::iostreams::gzip_compressor());
} else if (boost::algorithm::ends_with(filename, BZIP_EXT)) {
stream->push(boost::iostreams::bzip2_compressor());
}
stream->push(boost::iostreams::file_descriptor_sink(filename));
} catch (...) {
SAFE_DELETE(stream);
}
return stream;
}
};