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
|
#include "util.ih"
string Util::realPath(string const &path)
{
String::Type type;
vector<string> component = String::split(&type, path, "/");
if (type != String::NORMAL)
throw Exception() << "Filename `" << path << "': not supported";
component.resize(
remove(component.begin(), component.end(), ".")
-
component.begin()
);
while (true)
{
size_t from = find(component.begin(), component.end(), "..")
- component.begin();
if (from == component.size()) // at the last entry: done
break;
size_t to = find_if(component.begin() + from + 1, component.end(),
[&](string const &element)
{
return element != "..";
}
) - component.begin();
size_t count = to - from; // # ".." entries
size_t firstErase = from >= count ? // first to erase
from - count
:
0;
component.erase(component.begin() + firstErase,
component.begin() + to);
}
string ret(1, '/');
for(auto &str: component)
ret += str + '/';
ret.pop_back();
return ret;
}
|