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