-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
77 lines (58 loc) · 1.88 KB
/
main.cpp
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
#include <array>
#include <vector>
#include <map>
#include <regex>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <chrono>
#include <thread>
const std::string PROG_NAME = "cat";
std::tuple <std::map<char, int>, std::vector<std::string>> parse_opts(int argc, char *argv[]) {
std::regex parse_opts_regex("[a-z]");
std::map<char, int> opts{};
std::vector <std::string> filenames{};
for (auto i = 0; i < argc; i++) {
std::string arg(argv[i]);
if (arg[0] == '-') { // arg is opt
auto opts_begin = std::sregex_iterator(arg.begin(), arg.end(), parse_opts_regex);
auto opts_end = std::sregex_iterator();
for (std::sregex_iterator i = opts_begin; i != opts_end; i++) {
std::smatch match = *i;
char opt = match.str()[0];
opts[opt] += 1;
}
} else { // arg is filename
filenames.push_back(arg);
}
}
return std::make_pair(opts, filenames);
}
void cat(std::istream& is, const std::map<char, int>& opts) {
char buf[BUFSIZ];
for (;;) {
is.read(buf, BUFSIZ);
auto gcount = is.gcount();
if (gcount > 0) std::cout.write(buf, gcount);
if (is.eof()) break;
}
}
int main(int argc, char *argv[]) {
auto opts_n_files = parse_opts(argc, argv);
auto opts = std::get<0>(opts_n_files);
auto filenames = std::get<1>(opts_n_files);
auto filecount = filenames.size();
if (filecount > 1) {
for (int i = 1; i < filenames.size(); i++) {
std::ifstream fs(filenames[i], std::ios_base::in);
if (fs.is_open()) {
cat(fs, opts);
} else {
std::cerr << PROG_NAME << ": " << filenames[i] << ": " << std::strerror(errno) << std::endl;
}
}
} else {
cat(std::cin, opts);
}
return 0;
}