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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
#include <iostream>
#include <vector>
#include <string>
#include <string_view>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <tuple>
#include <memory>
#include <cstdlib>
#include <cstring>
#include <cerrno>
static void usage(const char *argv0) {
std::cerr <<
"Usage: " << argv0 << " [options] <row> <value>\n"
" " << argv0 << " [options] <row> <column> <value>\n"
" " << argv0 << " [options] <table> <row> <column> <value>\n"
"Splits lines on a separator character (-s) and creates tables from the result.\n"
"Each positional argument is a field index specification (after splitting) that\n"
"determines which parts of the input lines get used for what purpose.\n"
"A field index specification is like that of the '-f' flag to cut(1). If multiple\n"
"fields are specified for a particular purpose, they are joined using the\n"
"separator character and henceforth treated as a single string. If a table cell\n"
"has multiple values in the input, the values are appended using the separator\n"
"character.\n"
"Row, column and table labels are printed in order of occurrence in the input.\n"
"UTF-8 is assumed for string-length calculation.\n"
"\n"
"Options:\n"
" -h Show help.\n"
" -s DELIM The character that separates fields. Default space (' ').\n";
}
struct Range {
int from, to;
};
struct Fieldspec {
std::vector<Range> ranges;
bool empty() const { return ranges.empty(); }
};
static std::pair<int, const char*> parse_int(const char *str) {
errno = 0;
const char *endp = NULL;
long val = strtol(str, (char**)&endp, 10);
if (str[0] == '\0' || errno != 0 || (long)(int)val != val) {
std::cerr << "Invalid number: " << str << std::endl;
exit(1);
}
return std::make_pair(val, endp);
}
static Fieldspec parse_fieldspec(const char *str) {
Fieldspec spec;
while (true) {
int num;
const char *endp;
std::tie(num, endp) = parse_int(str);
if (*endp == '-') {
int num2;
std::tie(num2, endp) = parse_int(endp + 1);
spec.ranges.push_back(Range{num - 1, num2 - 1});
} else {
spec.ranges.push_back(Range{num - 1, num - 1});
}
if (*endp == '\0') break;
if (*endp == ',') str = endp + 1;
else {
std::cerr << "Invalid character in field spec: '" << *endp << "'" << std::endl;
exit(1);
}
}
return spec;
}
static void collect(std::string &dest, const Fieldspec &spec, const std::vector<std::string> &parts, char sepchar) {
dest.clear();
for (const Range &range : spec.ranges) {
for (int i = range.from; i <= std::min<int>(range.to, parts.size() - 1); i++) {
if (i > range.from) dest += sepchar;
dest += parts[i];
}
}
};
struct Spaces {
const int n;
Spaces(int n) : n{n} {}
};
static std::ostream& operator<<(std::ostream &os, Spaces spaces) {
for (int i = 0; i < spaces.n; i++) os << ' ';
return os;
}
static int swidth(const std::string &s) {
int len = 0;
for (char c : s) len += (c & 0xc0) != 0x80;
return len;
}
enum class Dir { left, center, right };
template <typename T, Dir dir>
struct AlignBase {
T s;
const int outw, sw;
AlignBase(T s, int outw, int sw) : s{s}, outw{outw}, sw{sw} {}
};
using Left = AlignBase<const std::string&, Dir::left>;
using Center = AlignBase<const std::string&, Dir::center>;
using Right = AlignBase<const std::string&, Dir::right>;
template <typename T>
std::ostream& operator<<(std::ostream &os, AlignBase<T, Dir::left> al) {
return os << al.s << Spaces(al.outw - al.sw);
}
template <typename T>
std::ostream& operator<<(std::ostream &os, AlignBase<T, Dir::center> al) {
const int n = al.outw - al.sw;
return os << Spaces((n + 1) / 2) << al.s << Spaces(n / 2);
}
template <typename T>
std::ostream& operator<<(std::ostream &os, AlignBase<T, Dir::right> al) {
return os << Spaces(al.outw - al.sw) << al.s;
}
int main(int argc, char **argv) {
char sepchar = ' ';
std::vector<Fieldspec> fieldspecs;
fieldspecs.reserve(4);
for (int i = 1; i < argc; i++) {
if (argv[i][0] != '-') {
fieldspecs.push_back(parse_fieldspec(argv[i]));
} else if (strcmp(argv[i], "--help") == 0) {
usage(argv[0]);
return 0;
} else {
for (int j = 1; argv[i][j]; j++) {
switch (argv[i][j]) {
case 'h': usage(argv[0]); return 0;
case 's':
if (argv[i][j+1] != '\0') {
sepchar = argv[i][j+1];
j++;
} else if (i + 1 < argc && argv[i+1][1] == '\0') {
sepchar = argv[i+1][1];
} else {
std::cerr << "Argument to '-s' missing or multiple bytes" << std::endl;
return 1;
}
break;
default:
std::cerr << "Invalid option '-" << argv[i][j] << "'" << std::endl;
return 1;
}
}
}
}
#define TAB 0
#define ROW 1
#define COL 2
#define VAL 3
Fieldspec specs[4];
switch (fieldspecs.size()) {
case 2:
specs[ROW] = std::move(fieldspecs[0]);
specs[VAL] = std::move(fieldspecs[1]);
break;
case 3:
specs[ROW] = std::move(fieldspecs[0]);
specs[COL] = std::move(fieldspecs[1]);
specs[VAL] = std::move(fieldspecs[2]);
break;
case 4:
specs[TAB] = std::move(fieldspecs[0]);
specs[ROW] = std::move(fieldspecs[1]);
specs[COL] = std::move(fieldspecs[2]);
specs[VAL] = std::move(fieldspecs[3]);
break;
default:
std::cerr << "Unexpected number of field specs; expected 2, 3 or 4" << std::endl;
return 1;
}
// Need a box around the std::string to make sure their buffer stays stable
// even if it does small-string optimisation
std::vector<std::unique_ptr<std::string>> labels[3];
std::map<std::tuple<int, int, int>, std::string> values; // key: indices in tab, row, col
{
// string_views refer to the strings in labels
std::unordered_map<std::string_view, int> labels_idx[3]; // only tab, row, col
std::string line;
std::vector<std::string> parts;
std::string texts[4];
while (std::getline(std::cin, line)) {
parts.clear();
size_t cursor = 0;
while (cursor < line.size()) {
size_t idx = line.find(sepchar, cursor);
if (idx == std::string::npos) idx = line.size();
parts.emplace_back(line, cursor, idx - cursor);
cursor = idx + 1;
}
if (parts.size() == 0) continue; // empty line, no fields
for (int i = 0; i < 4; i++) collect(texts[i], specs[i], parts, sepchar);
// check that all parts we need are indeed there
bool present = true;
for (int i = 0; i < 4; i++) {
if (!specs[i].empty() && texts[i].empty()) {
present = false;
break;
}
}
if (!present) continue;
// add to the label lists and collect indices
int idxs[3];
for (int i = 0; i < 3; i++) {
auto it = labels_idx[i].find(texts[i]);
if (it == labels_idx[i].end()) {
idxs[i] = labels[i].size();
labels[i].push_back(std::make_unique<std::string>(std::move(texts[i])));
texts[i].clear();
labels_idx[i].emplace(*labels[i].back(), idxs[i]);
} else {
idxs[i] = it->second;
}
}
// store the value at the appropriate index triplet
values.emplace(std::make_tuple(idxs[TAB], idxs[ROW], idxs[COL]), std::move(texts[VAL]));
}
}
int leftwid = 0; // does not include table name
for (const std::unique_ptr<std::string> &rowname : labels[ROW])
leftwid = std::max<int>(leftwid, swidth(*rowname));
std::vector<int> collabwid(labels[COL].size());
for (int coli = 0; coli < (int)labels[COL].size(); coli++)
collabwid[coli] = swidth(*labels[COL][coli]);
std::vector<int> rowlabwid(labels[ROW].size());
for (int rowi = 0; rowi < (int)labels[ROW].size(); rowi++)
rowlabwid[rowi] = swidth(*labels[ROW][rowi]);
for (int tabi = 0; tabi < (int)labels[TAB].size(); tabi++) {
if (tabi > 0) std::cout << '\n';
const std::string &tabname = *labels[TAB][tabi];
const int thisleftwid = std::max<int>(leftwid, swidth(tabname));
std::vector<int> colvalwid(labels[COL].size());
for (int coli = 0; coli < (int)labels[COL].size(); coli++) {
for (int rowi = 0; rowi < (int)labels[ROW].size(); rowi++) {
auto it = values.find(std::make_tuple(tabi, rowi, coli));
if (it != values.end())
colvalwid[coli] = std::max<int>(colvalwid[coli], swidth(it->second));
}
}
std::vector<int> colwid(labels[COL].size());
for (int coli = 0; coli < (int)labels[COL].size(); coli++)
colwid[coli] = std::max(collabwid[coli], colvalwid[coli]);
if (!specs[COL].empty()) {
std::cout << Left(tabname, thisleftwid, swidth(tabname));
for (int coli = 0; coli < (int)labels[COL].size(); coli++) {
std::cout << ' ' << Left(*labels[COL][coli], colwid[coli], collabwid[coli]);
}
std::cout << '\n';
}
for (int rowi = 0; rowi < (int)labels[ROW].size(); rowi++) {
std::cout << Left(*labels[ROW][rowi], thisleftwid, rowlabwid[rowi]);
for (int coli = 0; coli < (int)labels[COL].size(); coli++) {
auto it = values.find(std::make_tuple(tabi, rowi, coli));
if (it != values.end()) {
std::cout << ' ' << AlignBase<Right, Dir::center>(Right(it->second, colvalwid[coli], swidth(it->second)), colwid[coli], colvalwid[coli]);
} else {
std::cout << Spaces(colwid[coli] + 1);
}
}
std::cout << '\n';
}
}
std::cout << std::flush;
}
|