-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilepath.hpp
69 lines (64 loc) · 1.78 KB
/
filepath.hpp
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
#pragma once
#include <string>
class FilePath {
protected:
std::string _full;
#ifdef _WIN32
static const char s_separator = '\\';
#else
static const char s_separator = '/';
#endif
public:
FilePath(const char* src) : _full(src) {}
FilePath(std::string src) : _full(src) {}
FilePath(const FilePath& src) : _full(src._full) {}
FilePath& operator+=(const FilePath& part) {
if (part._full.size() == 0) {
return *this;
}
if (_full.size() && _full.back() != s_separator && part._full[0] != s_separator) {
_full += s_separator;
}
_full += part._full;
return *this;
}
FilePath operator+(const FilePath& right) {
FilePath ret(*this);
ret += right;
return ret;
}
FilePath& operator+=(const char* part) {
std::string right(part);
return this->operator+=(right);
}
operator std::string() { return _full; }
std::string dir() {
std::string part = _full;
if (part.size() && part.back() == s_separator) {
part = _full.substr(0, part.size() - 1);
}
size_t i = part.rfind(s_separator);
if (i == part.npos) {
return part;
}
return part.substr(0,i);
}
std::string base_name() {
std::string part = _full;
if (part.size() && part.back() == s_separator) {
part = _full.substr(0, part.size() - 1);
}
size_t i = part.rfind(s_separator);
if (i == part.npos) {
return part;
}
return part.substr(i+1);
}
std::string extension_name() {
size_t i = _full.rfind('.');
if (i == _full.npos || i >= _full.size()) {
return "";
}
return _full.substr(i + 1);
}
};