This repository was archived by the owner on Jan 14, 2018. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdbg_path.cpp
More file actions
111 lines (98 loc) · 2.37 KB
/
Copy pathdbg_path.cpp
File metadata and controls
111 lines (98 loc) · 2.37 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
#include "dbg_path.h"
#include <deque>
#include <algorithm>
namespace vscode
{
fs::path path_uncomplete(const fs::path& path, const fs::path& base, std::error_code& ec)
{
fs::path npath = path_normalize(path);
fs::path nbase = path_normalize(base);
if (npath == nbase)
return "./";
fs::path from_path, from_base, output;
fs::path::iterator path_it = npath.begin(), path_end = npath.end();
fs::path::iterator base_it = nbase.begin(), base_end = nbase.end();
if ((path_it == path_end) || (base_it == base_end))
{
ec = std::make_error_code(std::errc::not_a_directory);
return npath;
}
#ifdef WIN32
if (*path_it != *base_it)
return npath;
++path_it, ++base_it;
#endif
while (true) {
if ((path_it == path_end) || (base_it == base_end) || (*path_it != *base_it)) {
for (; base_it != base_end; ++base_it) {
if (*base_it == ".")
continue;
else if (*base_it == "/")
continue;
output /= "../";
}
fs::path::iterator path_it_start = path_it;
for (; path_it != path_end; ++path_it) {
if (path_it != path_it_start)
output /= "/";
if (*path_it == ".")
continue;
if (*path_it == "/")
continue;
output /= *path_it;
}
break;
}
from_path /= fs::path(*path_it);
from_base /= fs::path(*base_it);
++path_it, ++base_it;
}
return output;
}
fs::path path_normalize(const fs::path& p)
{
fs::path result = p.root_path();
std::deque<std::string> stack;
for (auto e : p.relative_path()) {
if (e == ".." && !stack.empty() && stack.back() != "..") {
stack.pop_back();
}
else if (e != ".") {
#if _MSC_VER >= 1900
stack.push_back(e.string());
#else
stack.push_back(e);
#endif
}
}
for (auto e : stack) {
result /= e;
}
std::string ret = result.string();
std::string tmp;
tmp.resize(ret.size());
std::transform(ret.begin(), ret.end(), tmp.begin(), tolower);
return tmp;
}
bool path_is_subpath(const fs::path& path, const fs::path& base)
{
fs::path npath = path_normalize(path);
fs::path::iterator path_it = npath.begin(), path_end = npath.end();
fs::path::iterator base_it = base.begin(), base_end = base.end();
for (;;)
{
if (base_it == base_end) {
return true;
}
if (path_it == path_end) {
return false;
}
if (*path_it != *base_it) {
return false;
}
++path_it;
++base_it;
}
return false;
}
}