This repository was archived by the owner on Sep 7, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlibpath.hpp
More file actions
182 lines (139 loc) · 4.17 KB
/
Copy pathlibpath.hpp
File metadata and controls
182 lines (139 loc) · 4.17 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
#pragma once
#include <string>
#include <vector>
#include <cctype>
#include "libstrmanip.hpp"
class Path {
vector_string pathObjects;
std::string source;
std::string vol;
std::string sysPathType;
bool absPath;
bool dirPath;
void clearPath() {
replaceAll(source, "\\", "/");
replaceAll(source, "\r", "");
replaceAll(source, "\n", "");
replaceAll(source, "\t", "");
replaceAll(source, "?", "");
replaceAll(source, ":", "");
replaceAll(source, "*", "");
replaceAll(source, "\"", "");
replaceAll(source, "<", "");
replaceAll(source, ">", "");
replaceAll(source, "|", "");
}
void splitPath() {
pathObjects = split(source, "/");
}
void recognizePath() {
if (source[0] == '/' || (isalpha(source[0]) && source[1] == ':')) {
absPath = true;
if (source[0] == '/') {
sysPathType = "unix";
vol = "/";
}
else if (isalpha(source[0]) && source[1] == ':') {
sysPathType = "win";
vol = source[0];
source.erase(0, 1);
}
}
else {
absPath = false;
sysPathType = "relative";
}
dirPath = source.back() == '/' || source.back() == '\\';
}
void clear() {
pathObjects.clear();
vol.clear();
sysPathType.clear();
absPath = false;
}
public:
Path() {}
Path(std::string path) {
fromString(path);
}
Path(const char* path) {
fromString(path);
}
Path(vector_string pathObjs, std::string type, std::string __vol, bool __abs) {
pathObjects = pathObjs;
sysPathType = type;
vol = __vol;
absPath = __abs;
}
Path& operator=(const std::string path) {
fromString(path);
return *this;
}
Path& operator=(const char* path) {
fromString(path);
return *this;
}
bool empty() {
return !pathObjects.size();
}
std::string type() {
return sysPathType;
}
std::string volume() {
return vol;
}
void simplify() {
for (int i = 0; i < pathObjects.size(); i++) {
if (pathObjects[i] == "" || pathObjects[i] == ".") pathObjects.erase(pathObjects.begin() + i--);
else if (pathObjects[i] == "..") {
pathObjects.erase(pathObjects.begin() + i--);
if (i >= 0) pathObjects.erase(pathObjects.begin() + i--);
}
}
}
vector_string pathParentDirs() {
vector_string tmp = pathObjects;
tmp.pop_back();
return tmp;
}
std::string filename() {
return (pathObjects.size()) ? pathObjects.back() : "";
}
std::string toString() {
if (!pathObjects.size()) return "";
std::string ret;
if (absPath) {
if (sysPathType == "unix") ret = vol;
else if (sysPathType == "win") ret = toUpper(vol) + ":\\";
}
for (std::string& i : pathObjects) if (i.size()) ret += i + ((sysPathType == "win") ? "\\" : "/");
if (ret != "/" && ret != toUpper(vol) + ":\\") {
ret.pop_back();
if (dirPath) ret += (sysPathType == "win") ? "\\" : "/";
}
return ret;
}
void fromString(std::string strpath) {
clear();
if (!strpath.size()) return;
source = strpath;
recognizePath();
clearPath();
splitPath();
}
};
Path operator+(Path path1, Path path2) {
if (path2.empty()) return path1;
std::string p1 = path1.toString();
std::string p2 = path2.toString();
if (path2.type() == "win") p2 = p2.substr(3);
else if (path2.type() == "unix") p2 = p2.substr(1);
return Path(p1 + "/" + p2);
}
Path operator/(Path path1, Path path2) {
return path1 + path2;
}
std::ostream& operator<<(std::ostream& ostr, Path path) {
ostr << path.toString();
return ostr;
}