|
36 | 36 | #include <strings.h> |
37 | 37 | #endif |
38 | 38 |
|
| 39 | +#include <simplecpp.h> |
| 40 | + |
39 | 41 | /** Is the filesystem case insensitive? */ |
40 | 42 | static bool caseInsensitiveFilesystem() |
41 | 43 | { |
@@ -70,66 +72,7 @@ std::string Path::fromNativeSeparators(std::string path) |
70 | 72 |
|
71 | 73 | std::string Path::simplifyPath(std::string originalPath) |
72 | 74 | { |
73 | | - const bool isUnc = originalPath.compare(0,2,"//") == 0; |
74 | | - |
75 | | - // Remove ./, .//, ./// etc. at the beginning |
76 | | - while (originalPath.compare(0,2,"./") == 0) { // remove "./././" |
77 | | - const size_t toErase = originalPath.find_first_not_of('/', 2); |
78 | | - originalPath = originalPath.erase(0, toErase); |
79 | | - } |
80 | | - |
81 | | - std::string subPath; |
82 | | - std::vector<std::string> pathParts; |
83 | | - for (std::size_t i = 0; i < originalPath.size(); ++i) { |
84 | | - if (originalPath[i] == '/' || originalPath[i] == '\\') { |
85 | | - if (!subPath.empty()) { |
86 | | - pathParts.push_back(subPath); |
87 | | - subPath.clear(); |
88 | | - } |
89 | | - |
90 | | - pathParts.push_back(std::string(1 , originalPath[i])); |
91 | | - } else |
92 | | - subPath.append(1, originalPath[i]); |
93 | | - } |
94 | | - |
95 | | - if (!subPath.empty()) |
96 | | - pathParts.push_back(subPath); |
97 | | - |
98 | | - // First filter out all double slashes |
99 | | - for (unsigned int i = 1; i < pathParts.size(); ++i) { |
100 | | - if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") { |
101 | | - pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1); |
102 | | - --i; |
103 | | - } |
104 | | - } |
105 | | - |
106 | | - for (unsigned int i = 1; i < pathParts.size(); ++i) { |
107 | | - if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1) { |
108 | | - pathParts.erase(pathParts.begin() + static_cast<int>(i) + 1); |
109 | | - pathParts.erase(pathParts.begin() + static_cast<int>(i)); |
110 | | - pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1); |
111 | | - pathParts.erase(pathParts.begin() + static_cast<int>(i) - 2); |
112 | | - i = 0; |
113 | | - } else if (i > 0 && pathParts[i] == ".") { |
114 | | - pathParts.erase(pathParts.begin() + static_cast<int>(i)); |
115 | | - i = 0; |
116 | | - } else if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") { |
117 | | - pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1); |
118 | | - i = 0; |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - if (isUnc) { |
123 | | - // Restore the leading double slash |
124 | | - pathParts.insert(pathParts.begin(), "/"); |
125 | | - } |
126 | | - |
127 | | - std::ostringstream oss; |
128 | | - for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i) { |
129 | | - oss << pathParts[i]; |
130 | | - } |
131 | | - |
132 | | - return oss.str(); |
| 75 | + return simplecpp::simplifyPath(originalPath); |
133 | 76 | } |
134 | 77 |
|
135 | 78 | std::string Path::getPathFromFilename(const std::string &filename) |
|
0 commit comments