-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasename.cpp
More file actions
71 lines (59 loc) · 1.89 KB
/
basename.cpp
File metadata and controls
71 lines (59 loc) · 1.89 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
#include <cstdio>
#include <string>
#include <string_view>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "basename",
.version = CFBOX_VERSION_STRING,
.one_line = "strip directory and suffix from file names",
.usage = "basename PATH [SUFFIX]",
.options = "",
.extra = "",
};
auto do_basename(std::string_view path, std::string_view suffix) -> std::string {
// Remove trailing slashes (but keep root "/")
while (path.size() > 1 && path.back() == '/') {
path.remove_suffix(1);
}
std::string result;
if (path.empty()) {
result = ".";
} else if (path == "/") {
result = "/";
} else {
// Find last component after last '/'
auto pos = path.rfind('/');
if (pos == std::string_view::npos) {
result = std::string{path};
} else {
result = std::string{path.substr(pos + 1)};
}
}
// Strip suffix if given and result ends with it (but not if suffix == result)
if (!suffix.empty() && result.size() > suffix.size()) {
if (result.compare(result.size() - suffix.size(), suffix.size(), suffix) == 0) {
result.erase(result.size() - suffix.size());
}
}
return result;
}
} // namespace
auto basename_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
const auto& pos = parsed.positional();
if (pos.empty()) {
CFBOX_ERR("basename", "missing operand");
return 1;
}
std::string_view suffix;
if (pos.size() >= 2) {
suffix = pos[1];
}
std::puts(do_basename(pos[0], suffix).c_str());
return 0;
}