-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.h
68 lines (57 loc) · 1.32 KB
/
common.h
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
#pragma once
#include <span>
#include <cstdint>
#include <vector>
#include <string>
#include <stdexcept>
struct File998
{
uint16_t width;
uint16_t height;
uint16_t bpp;
std::vector<uint8_t> data;
};
template <typename T>
struct checked_span : public std::span<T>
{
// using std::span<T>::span;
checked_span(std::span<T> const &span) : std::span<T>(span) {}
inline auto &at(size_t i)
{
if (i >= this->size())
throw std::out_of_range("index out of range");
return this->operator[](i);
}
inline auto const &at(size_t i) const
{
if (i >= this->size())
throw std::out_of_range("index out of range");
return this->operator[](i);
}
};
static inline std::string strip_ext(std::string const &input)
{
#ifdef _WIN32
constexpr auto dir_sep = '\\';
#else
constexpr auto dir_sep = '/';
#endif
auto sep_pos = input.find_last_of(dir_sep);
std::string path;
std::string filename;
if (sep_pos != std::string::npos)
{
path = input.substr(0, sep_pos + 1);
filename = input.substr(sep_pos + 1);
}
else
{
filename = input;
}
auto dot_pos = filename.find_last_of('.');
if (dot_pos != std::string::npos)
{
filename.resize(dot_pos);
}
return path + filename;
}