-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmycat.cpp
More file actions
103 lines (85 loc) · 3.35 KB
/
mycat.cpp
File metadata and controls
103 lines (85 loc) · 3.35 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
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
int main(int argc, char *argv[]) {
// Check for at least one file argument
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " [options] <filename...>" << std::endl;
return 1;
}
// Options map for future enhancements
std::unordered_map<std::string, bool> options{
{"--help", false}, // Display help information
{"-n", false}, // Number all output lines
{"-b", false}, // Number non-blank output lines
{"-s", false} // Squeeze multiple adjacent blank lines
};
size_t i = 1;
for (i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg[0] == '-') { // options
if (arg == "--help") {
std::cout << "Usage: " << argv[0] << " [options] <filename...>\n"
<< "Options:\n"
<< " --help Display this help information\n"
<< " -n Number all output lines\n"
<< " -b Number non-blank output lines\n"
<< " -s Squeeze multiple adjacent blank lines\n";
return 0;
} else {
for (size_t j = 1; j < arg.size(); ++j) {
std::string opt("-" + std::string(1, arg[j]));
if (options.find(opt) != options.end()) {
options[opt] = true; // Enable the option
} else {
std::cerr << "Warning: Unknown option " << opt << std::endl;
return 1;
}
}
}
} else { // files
break; // Stop processing options when a file is encountered
}
}
for ( ; i < argc ; ++i) { // Process each file from the remaining arguments
size_t line_number = 1; // counter for line number
std::ifstream infile(argv[i]);
if (!infile) {
std::cerr << "Error: Could not open file " << argv[i] << std::endl;
continue;
}
// sign for squeezing blank lines
bool squeeze_blank_lines = false;
std::string line;
while (std::getline(infile, line)) {
// Handle options here in the future
if (options["-s"] == true) {
if (line.empty()) {
if (squeeze_blank_lines == true) {
continue; // Skip this line
} else {
squeeze_blank_lines = true; // Mark that we've seen a blank line
}
} else {
squeeze_blank_lines = false; // Reset on non-blank line
}
}
if (options["-n"] == true || options["-b"] == true) {
if (options["-b"] == true && line.empty()) {
std::cout << "\t"; // No line number for blank lines
} else {
std::cout << line_number++ << "\t";
}
}
// ending options handling
std::cout << line << std::endl;
}
infile.close();
// Print a newline between files if there are multiple files
if (argc > 2 && i < argc - 1) {
std::cout << std::endl;
}
}
return 0;
}