forked from jeremy-rifkin/cpptrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.hpp
More file actions
313 lines (278 loc) · 9.79 KB
/
utils.hpp
File metadata and controls
313 lines (278 loc) · 9.79 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
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
303
304
305
306
307
308
309
310
311
312
313
#ifndef UTILS_HPP
#define UTILS_HPP
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "utils/common.hpp"
#include "utils/error.hpp"
#include "utils/optional.hpp"
#include "utils/result.hpp"
namespace cpptrace {
namespace detail {
bool isatty(int fd);
int fileno(std::FILE* stream);
inline std::vector<std::string> split(const std::string& str, const std::string& delims) {
std::vector<std::string> vec;
std::size_t old_pos = 0;
std::size_t pos = 0;
while((pos = str.find_first_of(delims, old_pos)) != std::string::npos) {
vec.emplace_back(str.substr(old_pos, pos - old_pos));
old_pos = pos + 1;
}
vec.emplace_back(str.substr(old_pos));
return vec;
}
template<typename C>
std::string join(const C& container, const std::string& delim) {
auto iter = std::begin(container);
auto end = std::end(container);
std::string str;
if(std::distance(iter, end) > 0) {
str += *iter;
while(++iter != end) {
str += delim;
str += *iter;
}
}
return str;
}
// first value in a sorted range such that *it <= value
template<typename ForwardIt, typename T>
ForwardIt first_less_than_or_equal(ForwardIt begin, ForwardIt end, const T& value) {
auto it = std::upper_bound(begin, end, value);
// it is first > value, we want first <= value
if(it != begin) {
return --it;
}
return end;
}
// first value in a sorted range such that *it <= value
template<typename ForwardIt, typename T, typename Compare>
ForwardIt first_less_than_or_equal(ForwardIt begin, ForwardIt end, const T& value, Compare compare) {
auto it = std::upper_bound(begin, end, value, compare);
// it is first > value, we want first <= value
if(it != begin) {
return --it;
}
return end;
}
constexpr const char* const whitespace = " \t\n\r\f\v";
inline std::string trim(const std::string& str) {
if(str.empty()) {
return "";
}
const std::size_t left = str.find_first_not_of(whitespace);
const std::size_t right = str.find_last_not_of(whitespace) + 1;
return str.substr(left, right - left);
}
inline bool starts_with(const std::string& str, const std::string& prefix) {
return str.size() >= prefix.size() && str.compare(0, prefix.size(), prefix) == 0;
}
inline bool is_little_endian() {
std::uint16_t num = 0x1;
const auto* ptr = (std::uint8_t*)#
return ptr[0] == 1;
}
// Modified from
// https://stackoverflow.com/questions/105252/how-do-i-convert-between-big-endian-and-little-endian-values-in-c
template<typename T, std::size_t N>
struct byte_swapper;
template<typename T>
struct byte_swapper<T, 1> {
T operator()(T val) {
return val;
}
};
template<typename T>
struct byte_swapper<T, 2> {
T operator()(T val) {
return (((val >> 8) & 0xff) | ((val & 0xff) << 8));
}
};
template<typename T>
struct byte_swapper<T, 4> {
T operator()(T val) {
return (((val & 0xff000000) >> 24) |
((val & 0x00ff0000) >> 8) |
((val & 0x0000ff00) << 8) |
((val & 0x000000ff) << 24));
}
};
template<typename T>
struct byte_swapper<T, 8> {
T operator()(T val) {
return (((val & 0xff00000000000000ULL) >> 56) |
((val & 0x00ff000000000000ULL) >> 40) |
((val & 0x0000ff0000000000ULL) >> 24) |
((val & 0x000000ff00000000ULL) >> 8 ) |
((val & 0x00000000ff000000ULL) << 8 ) |
((val & 0x0000000000ff0000ULL) << 24) |
((val & 0x000000000000ff00ULL) << 40) |
((val & 0x00000000000000ffULL) << 56));
}
};
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
T byteswap(T value) {
return byte_swapper<T, sizeof(T)>{}(value);
}
void enable_virtual_terminal_processing_if_needed() noexcept;
constexpr unsigned n_digits(unsigned value) noexcept {
return value < 10 ? 1 : 1 + n_digits(value / 10);
}
// TODO: Re-evaluate use of off_t
template<typename T, typename std::enable_if<std::is_trivial<T>::value, int>::type = 0>
Result<T, internal_error> load_bytes(std::FILE* object_file, off_t offset) {
T object;
if(std::fseek(object_file, offset, SEEK_SET) != 0) {
return internal_error("fseek error");
}
if(std::fread(&object, sizeof(T), 1, object_file) != 1) {
return internal_error("fread error");
}
return object;
}
// shamelessly stolen from stackoverflow
bool directory_exists(const std::string& path);
inline std::string basename(const std::string& path, bool maybe_windows = false) {
// Assumes no trailing /'s
auto pos = path.find_last_of(maybe_windows ? "/\\" : "/");
if(pos == std::string::npos) {
return path;
} else {
return path.substr(pos + 1);
}
}
// A way to cast to unsigned long long without "warning: useless cast to type"
template<typename T>
unsigned long long to_ull(T t) {
return static_cast<unsigned long long>(t);
}
template<typename T>
frame_ptr to_frame_ptr(T t) {
return static_cast<frame_ptr>(t);
}
// A way to cast to U without "warning: useless cast to type"
template<typename U, typename V>
U to(V v) {
return static_cast<U>(v);
}
template<typename T, typename U = T>
T exchange(T& obj, U&& value) {
T old = std::move(obj);
obj = std::forward<U>(value);
return old;
}
struct monostate {};
// TODO: Rework some stuff here. Not sure deleters should be optional or moved.
// Also allow file_wrapper file = std::fopen(object_path.c_str(), "rb");
template<
typename T,
typename D,
// Note: Previously checked if D was invocable and returned void but this kept causing problems for MSVC
// == 19.38-specific msvc bug https://developercommunity.visualstudio.com/t/MSVC-1938331290-preview-fails-to-comp/10505565
// <= 19.23 msvc also appears to fail (but for a different reason https://godbolt.org/z/6Y5EvdWPK)
// <= 19.39 msvc also has trouble with it for different reasons https://godbolt.org/z/aPPPT7z3z
typename std::enable_if<
std::is_standard_layout<T>::value && std::is_trivial<T>::value,
int
>::type = 0,
typename std::enable_if<
std::is_nothrow_move_constructible<T>::value,
int
>::type = 0
>
class raii_wrapper {
T obj;
optional<D> deleter;
public:
raii_wrapper(T obj, D deleter) : obj(obj), deleter(deleter) {}
raii_wrapper(raii_wrapper&& other) noexcept : obj(std::move(other.obj)), deleter(std::move(other.deleter)) {
other.deleter = nullopt;
}
raii_wrapper(const raii_wrapper&) = delete;
raii_wrapper& operator=(raii_wrapper&&) = delete;
raii_wrapper& operator=(const raii_wrapper&) = delete;
~raii_wrapper() {
if(deleter.has_value()) {
deleter.unwrap()(obj);
}
}
operator T&() {
return obj;
}
operator const T&() const {
return obj;
}
T& get() {
return obj;
}
const T& get() const {
return obj;
}
};
template<typename T, typename D>
raii_wrapper<typename std::remove_reference<T>::type, D> raii_wrap(T obj, D deleter) {
return raii_wrapper<typename std::remove_reference<T>::type, D>(obj, deleter);
}
inline void file_deleter(std::FILE* ptr) {
if(ptr) {
fclose(ptr);
}
}
using file_wrapper = raii_wrapper<std::FILE*, void(*)(std::FILE*)>;
template<class T, class... Args>
auto make_unique(Args&&... args) -> typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<typename T>
class maybe_owned {
std::unique_ptr<T> owned;
T* ptr;
public:
maybe_owned(T* ptr) : ptr(ptr) {}
maybe_owned(std::unique_ptr<T>&& owned) : owned(std::move(owned)), ptr(this->owned.get()) {}
T* operator->() {
return ptr;
}
T& operator*() {
return *ptr;
}
};
template<typename F>
class scope_guard {
F f;
bool active;
public:
template<
typename G,
typename std::enable_if<!std::is_same<typename std::decay<G>::type, scope_guard<F>>::value, int>::type = 0
>
scope_guard(G&& f) : f(std::forward<F>(f)), active(true) {}
~scope_guard() {
if(active) {
f();
}
}
scope_guard(const scope_guard&) = delete;
scope_guard(scope_guard&& other) : f(std::move(other.f)), active(exchange(other.active, false)) {}
scope_guard& operator=(const scope_guard&) = delete;
scope_guard& operator=(scope_guard&& other) {
f = std::move(other.f);
active = exchange(other.active, false);
return *this;
}
};
template<typename F>
NODISCARD auto scope_exit(F&& f) -> scope_guard<F> {
return scope_guard<F>(std::forward<F>(f));
}
}
}
#endif