-
Notifications
You must be signed in to change notification settings - Fork 4
/
hgrep.cc
67 lines (59 loc) · 1.88 KB
/
hgrep.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
#include <string>
#include <fstream>
#include <limits>
#include <algorithm>
#include <boost/regex.hpp>
#include "common.hpp"
#include <hgrep_cmdline.hpp>
int hgrep_main(int argc, char *argv[]) {
hgrep_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;
}
boost::regex regexp;
try {
regexp = args.regexp_arg;
} catch(boost::bad_expression& error) {
std::cerr << "Invalid regular expression: " << error.what() << "\n"
<< " " << args.regexp_arg << "\n"
<< std::string(1 + error.position(), ' ') << "^" << std::endl;
return EXIT_FAILURE;
}
size_t max_count = args.max_count_given ? args.max_count_arg : std::numeric_limits<size_t>::max();
std::string line;
for(auto file : args.file_arg) {
try {
std::ifstream is;
is.exceptions(std::ios::failbit|std::ios::badbit);
is.open(file);
bool skip = true;
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';
}
while(c != EOF) {
std::getline(is, line);
skip = !regex_search(++line.cbegin(), line.cend(), regexp) ^ args.invert_match_flag;
if(skip) {
for(c = is.peek(); c != '>' && c != EOF; c = is.peek())
skip_line(is);
} else {
if(max_count-- == 0) break;
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;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}