-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglob.cpp
More file actions
81 lines (60 loc) · 1.71 KB
/
Copy pathglob.cpp
File metadata and controls
81 lines (60 loc) · 1.71 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
// SPDX-FileCopyrightText: 2026 Paolo Anzani
// SPDX-License-Identifier: Apache-2.0
#include "glob.h"
#include <glob.h>
#include <expected>
#include <string>
namespace {
class GlobResult {
public:
GlobResult() = default;
GlobResult(const GlobResult&) = delete;
GlobResult& operator=(const GlobResult&) = delete;
~GlobResult() {
globfree(&value_);
}
glob_t* get() {
return &value_;
}
const glob_t& value() const {
return value_;
}
private:
glob_t value_{};
};
std::string globErrorMessage(const int error_code) {
switch (error_code) {
case GLOB_NOSPACE:
return "Not enough memory to expand pattern";
case GLOB_ABORTED:
return "Could not read a directory while expanding pattern";
case GLOB_NOMATCH:
return "No files matched pattern";
default:
return "Could not expand pattern";
}
}
std::string joinMatches(const glob_t& matches) {
std::string result;
for (std::size_t index = 0; index < matches.gl_pathc; ++index) {
if (!result.empty()) {
result += '\n';
}
result += matches.gl_pathv[index];
}
return result;
}
} // namespace
namespace microcodex {
std::expected<std::string, std::string> glob(const std::string& pattern) {
if (pattern.empty()) {
return std::unexpected("Pattern cannot be empty");
}
GlobResult matches;
const int error_code = ::glob(pattern.c_str(), 0, nullptr, matches.get());
if (error_code != 0) {
return std::unexpected(globErrorMessage(error_code));
}
return joinMatches(matches.value());
}
} // namespace microcodex