-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (68 loc) · 1.76 KB
/
main.cpp
File metadata and controls
80 lines (68 loc) · 1.76 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
#include <filesystem>
#include <fstream>
#include <iostream>
#include <vector>
#include <span>
#include <cstddef>
#include <string>
using namespace std;
using namespace filesystem;
auto read_file(const path &_path, const int argc, span<char*> args) -> int {
bool count = false;
for (size_t i = 1; i < argc; i++) {
const auto option = string(args[i]);
if (option.empty() || option[0] != '-') {
continue;
}
if (option == "--count" || option == "-c") {
count = true;
continue;
}
cout << "Unsupported option: " << option << "\n";
}
ifstream user_file;
user_file.open(_path.string());
if (!user_file.is_open()) {
cout << "Unable to open file " << absolute(_path) << "\n";
return 1;
}
string line;
cout << "Contents of " << absolute(_path) << ":" << "\n";
if (count) {
int _line_num = 1;
while (getline(user_file, line)) {
cout << _line_num << " " << line << "\n";
++_line_num;
}
} else {
while (getline(user_file, line)) {
cout << line << "\n";
}
}
user_file.close();
return 0;
}
auto main(const int argc, char *argv[]) -> int {
// Check if any arguments are provided
if (argc < 2) {
cout << "Please provide a file path. Usage: kat <filename>" << "\n";
return 1;
}
span<char*> args(argv, static_cast<size_t>(argc));
// Find which argument isn't a flag and use it as the path
path file_path;
bool _path = false;
for (size_t i = 1; i < argc; i++) {
const auto option = string(args[i]);
if (!option.starts_with("-")) {
file_path = option;
_path = true;
}
}
if (!_path) {
cout << "Please provide a file path. Usage: kat <filename>" << "\n";
return 1;
}
// Read the file
return read_file(file_path, argc, args);
}