-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhead.cc
104 lines (92 loc) · 3.25 KB
/
head.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
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
#include <cstdlib>
#include <string>
#include <fstream>
#include <common.hpp>
#include <head_cmdline.hpp>
// With a negative value, display all but the last N entries of a
// file. Store the entries in a circular buffer of size N. Any
// overflow get printed out.
static int head_negative(const head_cmdline& args) {
int res = EXIT_SUCCESS;
const int entries = std::abs(args.entries_arg);
std::vector<entry> cache(entries);
for(const auto& file : args.file_arg) {
try {
std::ifstream is;
is.exceptions(std::ios::failbit|std::ios::badbit);
is.open(file);
if((args.file_arg.size() > 1 && !args.quiet_flag) || args.verbose_flag)
std::cout << "==> " << file << " <==\n";
int c;
// Display unchanged up to first header
std::string line;
for(c = is.peek(); c != '>' && c != EOF; c = is.peek()) {
std::getline(is, line);
std::cout << line << '\n';
}
int cz = 0; // cache size
for(int i = 0; c != EOF; cz = std::min(cz + 1, entries), i = (i + 1) % entries) {
entry& ce = cache[i];
if(cz == entries) { // Print with a delay (overflow)
for(int j = 0; j < ce.size; ++j)
std::cout << ce.lines[j] << '\n';
}
ce.size = 0;
ce.add_line(is); // Replace entry
for(c = is.peek(); c != '>' && c != EOF; c = is.peek())
ce.add_line(is);
}
} catch(std::ios::failure&) {
std::cerr << "Error with file '" << file << '\'' << std::endl;
res = EXIT_FAILURE;
}
}
return res;
}
// With a positive value, display the first N entries
static int head_positive(const head_cmdline& args) {
int res = EXIT_SUCCESS;
const auto line_limit = args.bytes_given ? std::numeric_limits<std::streamoff>::max() : (std::streamoff)args.entries_arg;
const auto byte_limit = args.bytes_given ? (std::streamoff)args.bytes_arg : std::numeric_limits<std::streamoff>::max();
std::string line;
for(const auto& file : args.file_arg) {
try {
std::ifstream is;
is.exceptions(std::ios::failbit|std::ios::badbit);
is.open(file);
if((args.file_arg.size() > 1 && !args.quiet_flag) || args.verbose_flag)
std::cout << "==> " << file << " <==\n";
int c;
// Display unchanged up to first header
for(c = is.peek(); c != '>' && c != EOF; c = is.peek()) {
std::getline(is, line);
std::cout << line << '\n';
}
for(std::streamoff i = 0; c != EOF && i < line_limit && is.tellg() < byte_limit; ++i) {
std::getline(is, line);
std::cout << line << '\n';
for(c = is.peek(); c != '>' && c != EOF; c = is.peek()) {
std::getline(is, line);
std::cout << line << '\n';
}
}
} catch(std::ios::failure&) {
std::cerr << "Error with file '" << file << '\'' << std::endl;
res = EXIT_FAILURE;
}
}
return res;
}
int head_main(int argc, char *argv[]) {
head_cmdline args(argc, argv);
if(args.file_arg.empty()) {
args.file_arg.push_back("/dev/stdin");
if(isatty(0))
std::cerr << "Warning: reading from terminal" << std::endl;
}
if(args.entries_arg == 0) return EXIT_SUCCESS;
if(args.entries_arg > 0)
return head_positive(args);
else
return head_negative(args);
}