-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmywc.cpp
More file actions
129 lines (109 loc) · 3.68 KB
/
mywc.cpp
File metadata and controls
129 lines (109 loc) · 3.68 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
129
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <fstream>
#include <sstream>
using std::cin;
using std::cout;
using std::cerr;
using std::string;
using std::unordered_map;
using std::vector;
using std::ifstream;
using std::istringstream;
using std::ostream;
ostream &process(std::istream &in, unordered_map<string, bool> &options);
int main(int argc, char *argv[]) {
unordered_map<string, bool> options{
{"--help", false}, // Display help information
{"-l", false}, // Show line counts
{"-c", false}, // Show byte counts
{"-w", false}, // Show word counts
{"-L", false} // Show longest line length
};
// process command-line arguments
vector<string> files;
size_t i = 1;;
for (i = 1; i < argc; ++i) {
string arg = argv[i];
if (arg[0] == '-') { // options
if (arg == "--help") {
cout << "Usage: " << argv[0] << " [options] <filename...>\n"
<< "Options:\n"
<< " --help Display this help information\n"
<< " -l Show line counts\n"
<< " -c Show byte counts\n"
<< " -w Show word counts\n"
<< " -L Show longest line length\n";
return 0;
} else {
for (size_t j = 1; j < arg.size(); ++j) {
string opt("-" + string(1, arg[j]));
if (options.find(opt) != options.end()) {
options[opt] = true; // Enable the option
} else {
cerr << "Warning: Unknown option " << opt << std::endl;
return 1;
}
}
}
} else { // files
files.push_back(arg);
}
}
// process each file
if (files.empty()) { // no files specified, read from standard input, support pipe input
process(cin, options) << std::endl;
} else { // files specified
for (const auto &filename : files) {
ifstream infile(filename);
if (!infile) {
cerr << "Error: Could not open file " << filename << std::endl;
continue;
}
process(infile, options) << " " + filename << std::endl;
infile.close();
}
}
}
ostream &process(std::istream &in, unordered_map<string, bool> &options) {
size_t line_count = 0;
size_t word_count = 0;
size_t char_count = 0;
size_t max_line_length = 0;
string line;
while (std::getline(in, line)) {
++line_count;
char_count += line.size() + 1; // +1 for newline character
if (options["-L"] == true) {
if (line.size() > max_line_length) {
max_line_length = line.size();
}
}
istringstream iss(line);
string word;
while (iss >> word) {
++word_count;
}
}
if (options["-l"] == true) {
cout << "Lines: " << line_count << " ";
}
if (options["-w"] == true) {
cout << "Words: " << word_count << " ";
}
if (options["-c"] == true) {
cout << "Bytes: " << char_count << " ";
}
if (options["-L"] == true) {
cout << "Max Length: " << max_line_length << " ";
}
// default: show all counts if no specific option is given
if (options["-l"] == false && options["-w"] == false && options["-c"] == false && options["-L"] == false) {
cout << "Lines: " << line_count << " "
<< "Words: " << word_count << " "
<< "Bytes: " << char_count << " ";
}
return cout;
}