add io::path.normalized()

This commit is contained in:
MihailRis
2025-02-04 14:58:41 +03:00
parent 1ec8f89599
commit 59f46ad530
5 changed files with 39 additions and 35 deletions
+28 -1
View File
@@ -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;
}