-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcut.cpp
More file actions
144 lines (132 loc) · 4.83 KB
/
cut.cpp
File metadata and controls
144 lines (132 loc) · 4.83 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <set>
#include <string>
#include <vector>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/io.hpp>
#include <cfbox/stream.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "cut",
.version = CFBOX_VERSION_STRING,
.one_line = "remove sections from each line of files",
.usage = "cut -d DELIM -f LIST [FILE]...",
.options = " -d DELIM use DELIM instead of TAB for field delimiter\n"
" -f LIST select only these fields\n"
" -c LIST select only these characters\n"
" -s do not print lines without delimiter",
.extra = "LIST: comma-separated ranges (e.g., 1,3-5,7-)",
};
} // namespace
static auto str_to_int(const std::string& s) -> int {
return static_cast<int>(std::strtol(s.c_str(), nullptr, 10));
}
static auto parse_range_list(const std::string& list) -> std::set<int> {
std::set<int> fields;
std::string token;
for (std::size_t i = 0; i <= list.size(); ++i) {
if (i == list.size() || list[i] == ',') {
auto dash = token.find('-');
if (dash == std::string::npos) {
fields.insert(str_to_int(token));
} else if (dash == 0) {
int end = str_to_int(token.substr(1));
for (int j = 1; j <= end; ++j) fields.insert(j);
} else if (dash == token.size() - 1) {
int start = str_to_int(token.substr(0, dash));
for (int j = start; j <= 1024; ++j) fields.insert(j);
} else {
int start = str_to_int(token.substr(0, dash));
int end = str_to_int(token.substr(dash + 1));
for (int j = start; j <= end; ++j) fields.insert(j);
}
token.clear();
} else {
token += list[i];
}
}
return fields;
}
auto cut_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'d', true, "delimiter"},
cfbox::args::OptSpec{'f', true, "fields"},
cfbox::args::OptSpec{'c', true, "characters"},
cfbox::args::OptSpec{'s', false},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
char delim = '\t';
if (auto d = parsed.get_any('d', "delimiter")) {
if (d->size() != 1) {
CFBOX_ERR("cut", "delimiter must be a single character");
return 1;
}
delim = (*d)[0];
}
bool skip_no_delim = parsed.has('s');
bool field_mode = parsed.has_any('f', "fields");
bool char_mode = parsed.has_any('c', "characters");
if (!field_mode && !char_mode) {
CFBOX_ERR("cut", "you must specify a list of fields or characters");
return 1;
}
std::set<int> indices;
if (field_mode) {
auto list = parsed.get_any('f', "fields");
if (!list) {
CFBOX_ERR("cut", "missing list for -f");
return 1;
}
indices = parse_range_list(std::string{*list});
} else {
auto list = parsed.get_any('c', "characters");
if (!list) {
CFBOX_ERR("cut", "missing list for -c");
return 1;
}
indices = parse_range_list(std::string{*list});
}
const auto& pos = parsed.positional();
auto paths = pos.empty() ? std::vector<std::string_view>{"-"} : pos;
int rc = 0;
for (auto p : paths) {
auto result = cfbox::stream::for_each_line(p, [&](const std::string& line, std::size_t) {
if (char_mode) {
bool first = true;
for (int idx : indices) {
if (idx >= 1 && static_cast<std::size_t>(idx - 1) < line.size()) {
if (!first) std::putchar(delim);
std::putchar(line[idx - 1]);
first = false;
}
}
std::putchar('\n');
} else {
auto fields = cfbox::stream::split_fields(line, delim);
if (fields.size() <= 1 && skip_no_delim) {
return true;
}
bool first = true;
for (int idx : indices) {
if (idx >= 1 && static_cast<std::size_t>(idx - 1) < fields.size()) {
if (!first) std::putchar(delim);
std::fputs(fields[idx - 1].c_str(), stdout);
first = false;
}
}
std::putchar('\n');
}
return true;
});
if (!result) {
CFBOX_ERR("cut", "%s", result.error().msg.c_str());
rc = 1;
}
}
return rc;
}