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 03:38:22 +03:00
m_is_valid = canonicalize();
}
2018-12-03 03:38:22 +03:00
bool FileSystemPath::canonicalize(bool resolve_symbolic_links)
{
// FIXME: Implement "resolveSymbolicLinks"
2018-12-03 03:38:22 +03:00
(void) resolve_symbolic_links;
auto parts = m_string.split('/');
2018-12-03 03:38:22 +03:00
Vector<String> canonical_parts;
for (auto& part : parts) {
if (part == ".")
continue;
if (part == "..") {
2018-12-03 03:38:22 +03:00
if (!canonical_parts.isEmpty())
canonical_parts.takeLast();
continue;
}
if (!part.isEmpty())
2018-12-03 03:38:22 +03:00
canonical_parts.append(part);
}
2018-12-03 03:38:22 +03:00
if (canonical_parts.isEmpty()) {
2018-11-19 01:28:43 +03:00
m_string = m_basename = "/";
return true;
}
2018-12-03 03:38:22 +03:00
m_basename = canonical_parts.last();
2018-11-19 01:28:43 +03:00
StringBuilder builder;
2018-12-03 03:38:22 +03:00
for (auto& cpart : canonical_parts) {
2018-11-19 01:28:43 +03:00
builder.append('/');
builder.append(move(cpart));
}
2018-11-19 01:28:43 +03:00
m_string = builder.build();
return true;
}
}