forked from heavywatal/cxxwtl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzfstream.hpp
More file actions
67 lines (59 loc) · 2.1 KB
/
zfstream.hpp
File metadata and controls
67 lines (59 loc) · 2.1 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once
#ifndef WTL_ZFSTREAM_HPP_
#define WTL_ZFSTREAM_HPP_
#include <fstream>
#include <string>
#include <vector>
#include <regex>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////
namespace wtl {
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////
namespace bios = boost::iostreams;
class ozfstream: public bios::filtering_ostream {
public:
ozfstream(const std::string& path, std::ios::openmode mode=std::ios::out):
bios::filtering_ostream(), path_(path) {
if (std::regex_search(path, std::regex("\\.gz$"))) {
push(bios::gzip_compressor());
}
push(bios::file_descriptor_sink(path, mode));
}
const std::string& path() const {return path_;}
private:
const std::string path_;
};
class izfstream: public bios::filtering_istream {
public:
izfstream(const std::string& path, std::ios::openmode mode=std::ios::in):
bios::filtering_istream(), path_(path) {
if (std::regex_search(path, std::regex("\\.gz$"))) {
push(bios::gzip_decompressor());
}
push(bios::file_descriptor_source(path, mode));
}
std::string readline(const char delimiter='\n') {
std::string buffer;
std::getline(*this, buffer, delimiter);
return buffer;
}
std::vector<std::string> readlines(const char delimiter='\n') {
std::vector<std::string> lines;
std::string buffer;
while (std::getline(*this, buffer, delimiter)) {
lines.push_back(buffer);
}
return lines;
}
std::string read(const char delimiter='\0') {return readline(delimiter);}
void close() {pop();}
const std::string& path() const {return path_;}
private:
const std::string path_;
};
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////
} // namespace wtl
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////
#endif // WTL_ZFSTREAM_HPP_