-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAbsPath.cpp
More file actions
136 lines (118 loc) · 3.62 KB
/
Copy pathAbsPath.cpp
File metadata and controls
136 lines (118 loc) · 3.62 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
#include "AbsPath.h"
#include <cstdio>
#include <cstdlib>
#include <climits>
#ifdef _WIN32
#include <direct.h>
#define getcwd _getcwd
#define PATH_MAX _MAX_PATH
#define IS_PATH_SEPARATOR(c) ((c) == '/' || (c) == '\\')
static bool isAbsolutePath(const std::string &path) {
if (path.empty())
return false;
if (IS_PATH_SEPARATOR(path.front()))
return true;
for (char c : path) {
if (c == ':')
return true;
if (IS_PATH_SEPARATOR(c))
break;
}
return false;
}
#else
#include <unistd.h>
#define IS_PATH_SEPARATOR(c) ((c) == '/')
static bool isAbsolutePath(const std::string &path) {
if (path.empty())
return false;
if (IS_PATH_SEPARATOR(path.front()))
return true;
return false;
}
#endif
static std::string getWorkingDirectoryPath() {
std::string path;
char buffer[PATH_MAX+8];
if (char *cwd = getcwd(buffer, sizeof(buffer)-2))
path = std::string(cwd)+'/';
return path;
}
static std::string normalizePath(const std::string &path) {
std::string outPath;
char prevPrevPrevChar = '\0';
char prevPrevChar = '\0';
char prevChar = '\0';
for (char c : path) {
if (IS_PATH_SEPARATOR(c)) {
c = '/';
if (prevChar == '.' && prevPrevChar == '.' && prevPrevPrevChar == '/') {
int i = (int) outPath.size()-4;
if (i >= 0) {
while (i >= 0 && outPath[i] != '/')
--i;
outPath.resize(i+1);
}
} else if (prevChar == '.' && prevPrevChar == '/') {
outPath.pop_back();
} else if (prevChar != '/')
outPath.push_back(c);
} else
outPath.push_back(c);
prevPrevPrevChar = prevPrevChar;
prevPrevChar = prevChar;
prevChar = c;
}
return outPath;
}
static int commonPathPrefix(const std::string &a, const std::string &b) {
int commonLen = 0, i = 0;
for (const char *ac = a.c_str(), *bc = b.c_str(); *ac && (*ac == *bc || (IS_PATH_SEPARATOR(*ac) && IS_PATH_SEPARATOR(*bc))); ++ac, ++bc, ++i) {
if (IS_PATH_SEPARATOR(*ac))
commonLen = i+1;
}
return commonLen;
}
const AbsPath AbsPath::workingDirectory(getWorkingDirectoryPath());
AbsPath::AbsPath(const std::string &path) {
if (isAbsolutePath(path))
absPath = normalizePath(path);
else {
absPath = workingDirectory.absPath;
while (!absPath.empty() && !IS_PATH_SEPARATOR(absPath.back()))
absPath.pop_back();
absPath = normalizePath(absPath+path);
}
}
AbsPath AbsPath::operator+(const std::string &path) const {
AbsPath outPath;
if (isAbsolutePath(path))
outPath.absPath = normalizePath(path);
else {
outPath.absPath = absPath;
while (!outPath.absPath.empty() && !IS_PATH_SEPARATOR(outPath.absPath.back()))
outPath.absPath.pop_back();
outPath.absPath = normalizePath(outPath.absPath+path);
}
return outPath;
}
std::string AbsPath::operator-(const AbsPath &other) const {
int prefixLen = commonPathPrefix(absPath, other.absPath);
if (!prefixLen)
return absPath;
if (prefixLen == (int) other.absPath.size())
return absPath.substr(prefixLen);
std::string relPath;
for (const char *c = other.absPath.c_str()+prefixLen; *c; ++c) {
if (IS_PATH_SEPARATOR(*c))
relPath += "../";
}
relPath += absPath.substr(prefixLen);
return relPath;
}
const std::string &AbsPath::string() const {
return absPath;
}
const char *AbsPath::cStr() const {
return absPath.c_str();
}