-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgunzip.cpp
More file actions
56 lines (49 loc) · 1.67 KB
/
gunzip.cpp
File metadata and controls
56 lines (49 loc) · 1.67 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
#include <cstdio>
#include <string>
#include <cfbox/args.hpp>
#include <cfbox/compress.hpp>
#include <cfbox/help.hpp>
#include <cfbox/io.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "gunzip",
.version = CFBOX_VERSION_STRING,
.one_line = "decompress files",
.usage = "gunzip [FILE]...",
.options = "",
.extra = "",
};
} // namespace
auto gunzip_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
const auto& pos = parsed.positional();
if (pos.empty()) {
auto input = cfbox::io::read_all_stdin();
if (!input) { CFBOX_ERR("gunzip", "read error"); return 1; };
auto result = cfbox::compress::gzip_decompress(*input);
std::fwrite(result.data(), 1, result.size(), stdout);
return 0;
}
int rc = 0;
for (auto p : pos) {
std::string path{p};
auto input = cfbox::io::read_all(path);
if (!input) {
CFBOX_ERR("gunzip", "%s", input.error().msg.c_str());
rc = 1;
continue;
}
auto result = cfbox::compress::gzip_decompress(*input);
std::string outpath = (path.size() > 3 && path.substr(path.size() - 3) == ".gz")
? path.substr(0, path.size() - 3) : path + ".out";
auto wresult = cfbox::io::write_all(outpath, result);
if (!wresult) {
CFBOX_ERR("gunzip", "%s", wresult.error().msg.c_str());
rc = 1;
}
}
return rc;
}