mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:28:12 +00:00
Add a simple FileSystemPath class that can canonicalize paths.
Also a simple StringBuilder to help him out.
This commit is contained in:
parent
43475f248b
commit
88ad59bfb1
6 changed files with 102 additions and 6 deletions
44
AK/FileSystemPath.cpp
Normal file
44
AK/FileSystemPath.cpp
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include "FileSystemPath.h"
|
||||
#include "Vector.h"
|
||||
#include "kstdio.h"
|
||||
#include "StringBuilder.h"
|
||||
|
||||
namespace AK {
|
||||
|
||||
FileSystemPath::FileSystemPath(const String& s)
|
||||
: m_string(s)
|
||||
{
|
||||
m_isValid = canonicalize();
|
||||
}
|
||||
|
||||
bool FileSystemPath::canonicalize(bool resolveSymbolicLinks)
|
||||
{
|
||||
// FIXME: Implement "resolveSymbolicLinks"
|
||||
auto parts = m_string.split('/');
|
||||
Vector<String> canonicalParts;
|
||||
|
||||
for (auto& part : parts) {
|
||||
if (part == ".")
|
||||
continue;
|
||||
if (part == "..") {
|
||||
if (!canonicalParts.isEmpty())
|
||||
canonicalParts.takeLast();
|
||||
continue;
|
||||
}
|
||||
canonicalParts.append(part);
|
||||
}
|
||||
if (canonicalParts.isEmpty()) {
|
||||
m_string = "/";
|
||||
return true;
|
||||
}
|
||||
StringBuilder builder;
|
||||
for (auto& cpart : canonicalParts) {
|
||||
builder.append('/');
|
||||
builder.append(move(cpart));
|
||||
}
|
||||
m_string = builder.build();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue