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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/*
* Copyright (c) 2019-2020, Marios Prokopakis <mariosprokopakis@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/QuickSort.h>
#include <AK/StdLibExtras.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Index {
enum class Type {
SingleIndex,
SliceIndex,
RangedIndex
};
ssize_t m_from { -1 };
ssize_t m_to { -1 };
Type m_type { Type::SingleIndex };
bool intersects(const Index& other)
{
if (m_type != Type::RangedIndex)
return m_from == other.m_from;
return !(other.m_from > m_to || other.m_to < m_from);
}
};
static void print_usage_and_exit(int ret)
{
warnln("Usage: cut -b list [File]");
exit(ret);
}
static void add_if_not_exists(Vector<Index>& indices, Index data)
{
bool append_to_vector = true;
for (auto& index : indices) {
if (index.intersects(data)) {
if (index.m_type == Index::Type::RangedIndex) {
index.m_from = min(index.m_from, data.m_from);
index.m_to = max(index.m_to, data.m_to);
}
append_to_vector = false;
}
}
if (append_to_vector) {
indices.append(data);
}
}
static void expand_list(Vector<String>& tokens, Vector<Index>& indices)
{
for (auto& token : tokens) {
if (token.length() == 0) {
warnln("cut: byte/character positions are numbered from 1");
print_usage_and_exit(1);
}
if (token == "-") {
warnln("cut: invalid range with no endpoint: {}", token);
print_usage_and_exit(1);
}
if (token[0] == '-') {
auto index = token.substring(1, token.length() - 1).to_int();
if (!index.has_value()) {
warnln("cut: invalid byte/character position '{}'", token);
print_usage_and_exit(1);
}
if (index.value() == 0) {
warnln("cut: byte/character positions are numbered from 1");
print_usage_and_exit(1);
}
Index tmp = { 1, index.value(), Index::Type::RangedIndex };
add_if_not_exists(indices, tmp);
} else if (token[token.length() - 1] == '-') {
auto index = token.substring(0, token.length() - 1).to_int();
if (!index.has_value()) {
warnln("cut: invalid byte/character position '{}'", token);
print_usage_and_exit(1);
}
if (index.value() == 0) {
warnln("cut: byte/character positions are numbered from 1");
print_usage_and_exit(1);
}
Index tmp = { index.value(), -1, Index::Type::SliceIndex };
add_if_not_exists(indices, tmp);
} else {
auto range = token.split('-');
if (range.size() == 2) {
auto index1 = range[0].to_int();
if (!index1.has_value()) {
warnln("cut: invalid byte/character position '{}'", range[0]);
print_usage_and_exit(1);
}
auto index2 = range[1].to_int();
if (!index2.has_value()) {
warnln("cut: invalid byte/character position '{}'", range[1]);
print_usage_and_exit(1);
}
if (index1.value() > index2.value()) {
warnln("cut: invalid decreasing range");
print_usage_and_exit(1);
} else if (index1.value() == 0 || index2.value() == 0) {
warnln("cut: byte/character positions are numbered from 1");
print_usage_and_exit(1);
}
Index tmp = { index1.value(), index2.value(), Index::Type::RangedIndex };
add_if_not_exists(indices, tmp);
} else if (range.size() == 1) {
auto index = range[0].to_int();
if (!index.has_value()) {
warnln("cut: invalid byte/character position '{}'", range[0]);
print_usage_and_exit(1);
}
if (index.value() == 0) {
warnln("cut: byte/character positions are numbered from 1");
print_usage_and_exit(1);
}
Index tmp = { index.value(), index.value(), Index::Type::SingleIndex };
add_if_not_exists(indices, tmp);
} else {
warnln("cut: invalid byte or character range");
print_usage_and_exit(1);
}
}
}
}
static void cut_file(const String& file, const Vector<Index>& byte_vector)
{
FILE* fp = stdin;
if (!file.is_null()) {
fp = fopen(file.characters(), "r");
if (!fp) {
warnln("cut: Could not open file '{}'", file);
return;
}
}
char* line = nullptr;
ssize_t line_length = 0;
size_t line_capacity = 0;
while ((line_length = getline(&line, &line_capacity, fp)) != -1) {
line[line_length - 1] = '\0';
line_length--;
for (auto& i : byte_vector) {
if (i.m_type == Index::Type::SliceIndex && i.m_from < line_length)
out("{}", line + i.m_from - 1);
else if (i.m_type == Index::Type::SingleIndex && i.m_from <= line_length)
out("{:c}", line[i.m_from - 1]);
else if (i.m_type == Index::Type::RangedIndex && i.m_from <= line_length) {
auto to = i.m_to > line_length ? line_length : i.m_to;
auto sub_string = String(line).substring(i.m_from - 1, to - i.m_from + 1);
out("{}", sub_string);
} else
break;
}
outln();
}
if (line)
free(line);
if (!file.is_null())
fclose(fp);
}
int main(int argc, char** argv)
{
String byte_list = "";
Vector<String> tokens;
Vector<String> files;
if (argc == 1) {
print_usage_and_exit(1);
}
for (int i = 1; i < argc;) {
if (!strcmp(argv[i], "-b")) {
/* The next argument should be a list of bytes. */
byte_list = (i + 1 < argc) ? argv[i + 1] : "";
if (byte_list == "") {
print_usage_and_exit(1);
}
tokens = byte_list.split(',');
i += 2;
} else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
print_usage_and_exit(1);
} else if (argv[i][0] != '-') {
files.append(argv[i++]);
} else {
warnln("cut: invalid argument {}", argv[i]);
print_usage_and_exit(1);
}
}
if (byte_list == "")
print_usage_and_exit(1);
Vector<Index> byte_vector;
expand_list(tokens, byte_vector);
quick_sort(byte_vector, [](auto& a, auto& b) { return a.m_from < b.m_from; });
if (files.is_empty())
files.append(String());
/* Process each file */
for (auto& file : files)
cut_file(file, byte_vector);
return 0;
}
|