add io::path.normalized()
This commit is contained in:
+28
-1
@@ -1,6 +1,6 @@
|
||||
#include "path.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <stack>
|
||||
|
||||
using namespace io;
|
||||
|
||||
@@ -9,3 +9,30 @@ void path::checkValid() const {
|
||||
throw std::runtime_error("path entry point is not specified: " + str);
|
||||
}
|
||||
}
|
||||
|
||||
path path::normalized() const {
|
||||
io::path path = pathPart();
|
||||
|
||||
std::stack<std::string> parts;
|
||||
do {
|
||||
parts.push(path.name());
|
||||
path.str = path.parent().string();
|
||||
} while (!path.empty());
|
||||
|
||||
while (!parts.empty()) {
|
||||
const std::string part = parts.top();
|
||||
parts.pop();
|
||||
if (part == ".") {
|
||||
continue;
|
||||
}
|
||||
if (part == "..") {
|
||||
throw access_error("entry point reached");
|
||||
}
|
||||
|
||||
path = path / part;
|
||||
}
|
||||
if (path.colonPos != std::string::npos) {
|
||||
path = path.entryPoint() + ":" + path.string();
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user