ladybird/AK/FileSystemPath.cpp

49 lines
1.1 KiB
C++
Raw Normal View History

#include "FileSystemPath.h"
#include "Vector.h"
#include "kstdio.h"
#include "StringBuilder.h"
namespace AK {
FileSystemPath::FileSystemPath(const String& s)
: m_string(s)
{
2018-12-03 00:38:22 +00:00
m_is_valid = canonicalize();
}
2018-12-03 00:38:22 +00:00
bool FileSystemPath::canonicalize(bool resolve_symbolic_links)
{
// FIXME: Implement "resolve_symbolic_links"
2018-12-03 00:38:22 +00:00
(void) resolve_symbolic_links;
auto parts = m_string.split('/');
2018-12-03 00:38:22 +00:00
Vector<String> canonical_parts;
for (auto& part : parts) {
if (part == ".")
continue;
if (part == "..") {
2018-12-21 01:10:45 +00:00
if (!canonical_parts.is_empty())
2018-12-03 00:38:22 +00:00
canonical_parts.takeLast();
continue;
}
2018-12-21 01:10:45 +00:00
if (!part.is_empty())
2018-12-03 00:38:22 +00:00
canonical_parts.append(part);
}
2018-12-21 01:10:45 +00:00
if (canonical_parts.is_empty()) {
2018-11-18 22:28:43 +00:00
m_string = m_basename = "/";
return true;
}
2018-12-03 00:38:22 +00:00
m_basename = canonical_parts.last();
2018-11-18 22:28:43 +00:00
StringBuilder builder;
2018-12-03 00:38:22 +00:00
for (auto& cpart : canonical_parts) {
2018-11-18 22:28:43 +00:00
builder.append('/');
builder.append(move(cpart));
}
2018-11-18 22:28:43 +00:00
m_string = builder.build();
return true;
}
}