2018-10-28 10:54:20 +03:00
|
|
|
#include "FileSystemPath.h"
|
2019-05-28 12:53:16 +03:00
|
|
|
#include "StringBuilder.h"
|
2018-10-28 10:54:20 +03:00
|
|
|
#include "Vector.h"
|
|
|
|
#include "kstdio.h"
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-06-02 13:26:28 +03:00
|
|
|
FileSystemPath::FileSystemPath(const StringView& s)
|
2018-10-28 10:54:20 +03:00
|
|
|
: m_string(s)
|
|
|
|
{
|
2018-12-03 03:38:22 +03:00
|
|
|
m_is_valid = canonicalize();
|
2018-10-28 10:54:20 +03:00
|
|
|
}
|
|
|
|
|
2018-12-03 03:38:22 +03:00
|
|
|
bool FileSystemPath::canonicalize(bool resolve_symbolic_links)
|
2018-10-28 10:54:20 +03:00
|
|
|
{
|
2018-12-21 19:45:42 +03:00
|
|
|
// FIXME: Implement "resolve_symbolic_links"
|
2019-05-28 12:53:16 +03:00
|
|
|
(void)resolve_symbolic_links;
|
2018-10-28 10:54:20 +03:00
|
|
|
auto parts = m_string.split('/');
|
2018-12-03 03:38:22 +03:00
|
|
|
Vector<String> canonical_parts;
|
2018-10-28 10:54:20 +03:00
|
|
|
|
|
|
|
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();
|
2018-10-28 10:54:20 +03:00
|
|
|
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-10-28 10:54:20 +03:00
|
|
|
}
|
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 = "/";
|
2018-10-28 10:54:20 +03:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-18 16:57:41 +03:00
|
|
|
|
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('/');
|
2019-01-18 05:27:51 +03:00
|
|
|
builder.append(cpart);
|
2018-10-28 10:54:20 +03:00
|
|
|
}
|
2019-03-30 05:27:25 +03:00
|
|
|
m_parts = move(canonical_parts);
|
2019-01-31 19:31:23 +03:00
|
|
|
m_string = builder.to_string();
|
2018-10-28 10:54:20 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-26 23:33:30 +03:00
|
|
|
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());
|
2018-10-28 10:54:20 +03:00
|
|
|
}
|
|
|
|
|
2019-05-26 23:33:30 +03:00
|
|
|
}
|