-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtail.cpp
More file actions
128 lines (107 loc) · 3.73 KB
/
tail.cpp
File metadata and controls
128 lines (107 loc) · 3.73 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <cstdio>
#include <cstdlib>
#include <string_view>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/io.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "tail",
.version = CFBOX_VERSION_STRING,
.one_line = "output the last part of files",
.usage = "tail [OPTIONS] [FILE]...",
.options = " -n N output the last N lines (default 10)\n"
" -c N output the last N bytes",
.extra = "",
};
auto tail_lines(const std::vector<std::string>& lines, long n, bool from_start) -> void {
if (from_start) {
long start = n - 1;
if (start < 0) start = 0;
for (long i = start; i < static_cast<long>(lines.size()); ++i) {
std::printf("%s\n", lines[static_cast<std::size_t>(i)].c_str());
}
} else {
if (n <= 0) return;
long start = static_cast<long>(lines.size()) - n;
if (start < 0) start = 0;
for (long i = start; i < static_cast<long>(lines.size()); ++i) {
std::printf("%s\n", lines[static_cast<std::size_t>(i)].c_str());
}
}
}
auto tail_bytes(const std::string& content, long n) -> void {
if (n <= 0) return;
long start = static_cast<long>(content.size()) - n;
if (start < 0) start = 0;
std::fwrite(content.data() + start, 1, static_cast<std::size_t>(n), stdout);
}
auto tail_file(std::string_view path, long n, bool use_bytes,
bool from_start, bool show_header) -> int {
bool use_stdin = (path == "-");
auto result = use_stdin ? cfbox::io::read_all_stdin() : cfbox::io::read_all(path);
if (!result) {
CFBOX_ERR("tail", "%s", result.error().msg.c_str());
return 1;
}
const auto& content = result.value();
if (show_header) {
std::printf("==> %s <==\n", use_stdin ? "standard input" : std::string{path}.c_str());
}
if (use_bytes) {
tail_bytes(content, n);
} else {
auto lines = cfbox::io::split_lines(content);
tail_lines(lines, n, from_start);
}
return 0;
}
auto parse_count(std::string_view val, bool& from_start) -> long {
if (!val.empty() && val[0] == '+') {
from_start = true;
return std::strtol(std::string{val.substr(1)}.c_str(), nullptr, 10);
}
return std::strtol(std::string{val}.c_str(), nullptr, 10);
}
} // namespace
auto tail_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'n', true},
cfbox::args::OptSpec{'c', true},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
bool use_lines = true;
bool use_bytes = false;
bool from_start = false;
long n = 10;
if (parsed.has('n')) {
n = parse_count(parsed.get('n').value_or("10"), from_start);
}
if (parsed.has('c')) {
n = parse_count(parsed.get('c').value_or("0"), from_start);
use_bytes = true;
use_lines = false;
}
const auto& pos = parsed.positional();
// Check for +N positional arg
std::vector<std::string_view> files;
for (const auto& p : pos) {
if (!p.empty() && p[0] == '+' && use_lines && !parsed.has('n')) {
from_start = true;
n = std::strtol(std::string{p.substr(1)}.c_str(), nullptr, 10);
} else {
files.push_back(p);
}
}
bool multi = files.size() > 1;
if (files.empty()) {
return tail_file("-", n, use_bytes, from_start, false);
}
int rc = 0;
for (std::size_t i = 0; i < files.size(); ++i) {
if (tail_file(files[i], n, use_bytes, from_start, multi) != 0) rc = 1;
}
return rc;
}