ladybird/AK/FileSystemPath.cpp

56 lines
1.4 KiB
C++
Raw Normal View History

#include "FileSystemPath.h"
#include "StringBuilder.h"
#include "Vector.h"
#include "kstdio.h"
namespace AK {
FileSystemPath::FileSystemPath(const StringView& 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 "resolve_symbolic_links"
(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-21 04:10:45 +03:00
if (!canonical_parts.is_empty())
2019-01-20 00:53:05 +03:00
canonical_parts.take_last();
continue;
}
2018-12-21 04:10:45 +03:00
if (!part.is_empty())
2018-12-03 03:38:22 +03:00
canonical_parts.append(part);
}
2018-12-21 04:10:45 +03:00
if (canonical_parts.is_empty()) {
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(cpart);
}
m_parts = move(canonical_parts);
m_string = builder.to_string();
return true;
}
bool FileSystemPath::has_extension(StringView extension) const
{
// FIXME: This is inefficient, expand StringView with enough functionality that we don't need to copy strings here.
String extension_string = extension;
return m_string.to_lowercase().ends_with(extension_string.to_lowercase());
}
}