-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfold.cpp
More file actions
85 lines (77 loc) · 2.76 KB
/
fold.cpp
File metadata and controls
85 lines (77 loc) · 2.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
81
82
83
84
85
#include <cstdio>
#include <string>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/stream.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "fold",
.version = CFBOX_VERSION_STRING,
.one_line = "wrap each input line to fit in specified width",
.usage = "fold [-w WIDTH] [-s] [FILE]...",
.options = " -w WIDTH use WIDTH columns instead of 80\n"
" -s break at spaces",
.extra = "",
};
} // namespace
auto fold_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'w', true, "width"},
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; }
int width = 80;
if (auto w = parsed.get_any('w', "width")) {
width = std::stoi(std::string{*w});
if (width <= 0) {
CFBOX_ERR("fold", "invalid width: %d", width);
return 1;
}
}
bool break_spaces = parsed.has('s');
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) {
std::size_t col = 0;
std::size_t last_space = 0;
std::string segment;
for (std::size_t i = 0; i < line.size(); ++i) {
if (col >= static_cast<std::size_t>(width)) {
if (break_spaces && last_space > 0) {
// Break at last space
auto break_pos = last_space;
std::string before = line.substr(i - col, break_pos - (i - col));
std::puts(before.c_str());
col = i - break_pos;
last_space = 0;
} else {
std::string before = line.substr(i - col, col);
std::puts(before.c_str());
col = 0;
}
}
if (line[i] == ' ' || line[i] == '\t') {
last_space = col + 1;
}
++col;
}
// Print remaining
if (col > 0) {
auto start = line.size() - col;
std::puts(line.substr(start).c_str());
} else {
std::putchar('\n');
}
return true;
});
if (!result) {
CFBOX_ERR("fold", "%s", result.error().msg.c_str());
rc = 1;
}
}
return rc;
}