2018-10-28 04:54:20 -03:00
|
|
|
#include "FileSystemPath.h"
|
2019-05-28 06:53:16 -03:00
|
|
|
#include "StringBuilder.h"
|
2018-10-28 04:54:20 -03:00
|
|
|
#include "Vector.h"
|
|
|
|
|
#include "kstdio.h"
|
|
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
2019-06-02 07:26:28 -03:00
|
|
|
FileSystemPath::FileSystemPath(const StringView& s)
|
2018-10-28 04:54:20 -03:00
|
|
|
: m_string(s)
|
|
|
|
|
{
|
2019-07-15 01:49:28 -03:00
|
|
|
canonicalize();
|
|
|
|
|
m_is_valid = true;
|
2018-10-28 04:54:20 -03:00
|
|
|
}
|
|
|
|
|
|
2019-07-15 01:49:28 -03:00
|
|
|
void FileSystemPath::canonicalize()
|
2018-10-28 04:54:20 -03:00
|
|
|
{
|
2019-07-15 01:49:28 -03:00
|
|
|
auto parts = m_string.split_view('/');
|
|
|
|
|
int approximate_canonical_length = 0;
|
2018-12-02 22:38:22 -02:00
|
|
|
Vector<String> canonical_parts;
|
2018-10-28 04:54:20 -03:00
|
|
|
|
|
|
|
|
for (auto& part : parts) {
|
|
|
|
|
if (part == ".")
|
|
|
|
|
continue;
|
|
|
|
|
if (part == "..") {
|
2018-12-20 23:10:45 -02:00
|
|
|
if (!canonical_parts.is_empty())
|
2019-01-19 19:53:05 -02:00
|
|
|
canonical_parts.take_last();
|
2018-10-28 04:54:20 -03:00
|
|
|
continue;
|
|
|
|
|
}
|
2019-07-15 01:49:28 -03:00
|
|
|
if (!part.is_empty()) {
|
|
|
|
|
approximate_canonical_length += part.length() + 1;
|
2018-12-02 22:38:22 -02:00
|
|
|
canonical_parts.append(part);
|
2019-07-15 01:49:28 -03:00
|
|
|
}
|
2018-10-28 04:54:20 -03:00
|
|
|
}
|
2018-12-20 23:10:45 -02:00
|
|
|
if (canonical_parts.is_empty()) {
|
2018-11-18 20:28:43 -02:00
|
|
|
m_string = m_basename = "/";
|
2019-07-15 01:49:28 -03:00
|
|
|
return;
|
2018-10-28 04:54:20 -03:00
|
|
|
}
|
2018-11-18 11:57:41 -02:00
|
|
|
|
2018-12-02 22:38:22 -02:00
|
|
|
m_basename = canonical_parts.last();
|
2019-07-29 01:45:50 -03:00
|
|
|
auto name_parts = m_basename.split('.');
|
|
|
|
|
m_title = name_parts[0];
|
|
|
|
|
if (name_parts.size() > 1)
|
|
|
|
|
m_extension = name_parts[1];
|
|
|
|
|
|
2019-07-15 01:49:28 -03:00
|
|
|
StringBuilder builder(approximate_canonical_length);
|
2018-12-02 22:38:22 -02:00
|
|
|
for (auto& cpart : canonical_parts) {
|
2018-11-18 20:28:43 -02:00
|
|
|
builder.append('/');
|
2019-01-18 00:27:51 -02:00
|
|
|
builder.append(cpart);
|
2018-10-28 04:54:20 -03:00
|
|
|
}
|
2019-03-29 23:27:25 -03:00
|
|
|
m_parts = move(canonical_parts);
|
2019-01-31 14:31:23 -02:00
|
|
|
m_string = builder.to_string();
|
2018-10-28 04:54:20 -03:00
|
|
|
}
|
|
|
|
|
|
2019-05-26 17: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 04:54:20 -03:00
|
|
|
}
|
|
|
|
|
|
2019-07-15 01:49:28 -03:00
|
|
|
String canonicalized_path(const StringView& path)
|
|
|
|
|
{
|
|
|
|
|
return FileSystemPath(path).string();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-26 17:33:30 -03:00
|
|
|
}
|