-
Notifications
You must be signed in to change notification settings - Fork 0
/
gunzip.cc
39 lines (33 loc) · 984 Bytes
/
gunzip.cc
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
#include <cstring>
#include <iostream>
#include "decompressor.h"
#include "io.h"
int usage(std::string const& program) {
std::cerr << "usage: " << program << " [-t]\n";
std::cerr
<< "\tDecompresses .gz file read from stdin and outputs to stdout\n";
std::cerr << "\t-t: employ two threads\n";
std::cerr << "\tExample: " << program << " < input.gz > output\n";
return -1;
}
constexpr std::streamsize BUFFER_SIZE = 64 << 10;
int main(int argc, const char** argv) {
std::ios_base::sync_with_stdio(false);
bool multithread = false;
if (argc == 2 && std::strcmp("-t", argv[1]) == 0) {
multithread = true;
} else if (argc != 1) {
return usage(argv[0]);
}
Stdin in;
Stdout out;
Decompressor decompressor{in, multithread};
std::vector<uint8_t> buffer(BUFFER_SIZE, 0);
Slice buf{buffer};
while (true) {
auto n = decompressor.read(buf);
out.write(Slice{buf.begin(), buf.begin() + n});
if (n < BUFFER_SIZE) break;
}
return 0;
}