-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaste.cpp
More file actions
92 lines (82 loc) · 2.88 KB
/
paste.cpp
File metadata and controls
92 lines (82 loc) · 2.88 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
#include <cstdio>
#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 = "paste",
.version = CFBOX_VERSION_STRING,
.one_line = "merge lines of files",
.usage = "paste [-d DELIMS] [-s] [FILE]...",
.options = " -d DELIMS use DELIMS instead of TABs\n"
" -s paste one file at a time instead of in parallel",
.extra = "",
};
} // namespace
auto paste_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'d', true, "delimiters"},
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; }
std::string delims = "\t";
if (auto d = parsed.get_any('d', "delimiters")) {
delims = std::string{*d};
}
bool serial = parsed.has('s');
const auto& pos = parsed.positional();
auto paths = pos.empty() ? std::vector<std::string_view>{"-"} : pos;
auto get_delim = [&](std::size_t i) -> char {
if (delims.empty()) return '\t';
return delims[i % delims.size()];
};
if (serial) {
for (auto p : paths) {
bool first = true;
std::size_t dindex = 0;
auto result = cfbox::stream::for_each_line(p, [&](const std::string& line, std::size_t) {
if (!first) std::putchar(get_delim(dindex++));
std::fputs(line.c_str(), stdout);
first = false;
return true;
});
if (!result) {
CFBOX_ERR("paste", "%s", result.error().msg.c_str());
return 1;
}
std::putchar('\n');
}
return 0;
}
// Parallel mode: read all lines from all files
std::vector<std::vector<std::string>> all_lines;
std::size_t max_lines = 0;
for (auto p : paths) {
std::vector<std::string> lines;
auto result = cfbox::stream::for_each_line(p, [&](const std::string& line, std::size_t) {
lines.push_back(line);
return true;
});
if (!result) {
CFBOX_ERR("paste", "%s", result.error().msg.c_str());
return 1;
}
if (lines.size() > max_lines) max_lines = lines.size();
all_lines.push_back(std::move(lines));
}
for (std::size_t row = 0; row < max_lines; ++row) {
for (std::size_t col = 0; col < all_lines.size(); ++col) {
if (col > 0) std::putchar(get_delim(col - 1));
if (row < all_lines[col].size()) {
std::fputs(all_lines[col][row].c_str(), stdout);
}
}
std::putchar('\n');
}
return 0;
}