From c7fd39f3b1029877279cfe8a59a4e2df01488eaf Mon Sep 17 00:00:00 2001 From: Conrad Pankoff Date: Tue, 7 Jan 2020 22:35:45 +1100 Subject: [PATCH] AK: Add dirname() to FileSystemPath --- AK/FileSystemPath.cpp | 11 ++++++++++- AK/FileSystemPath.h | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/AK/FileSystemPath.cpp b/AK/FileSystemPath.cpp index 8c95da826d..753c8d6603 100644 --- a/AK/FileSystemPath.cpp +++ b/AK/FileSystemPath.cpp @@ -45,10 +45,19 @@ void FileSystemPath::canonicalize() } } if (canonical_parts.is_empty()) { - m_string = m_basename = "/"; + m_string = m_basename = m_dirname = "/"; return; } + StringBuilder dirname_builder(approximate_canonical_length); + for (int i = 0; i < canonical_parts.size() - 1; ++i) { + auto& canonical_part = canonical_parts[i]; + if (is_absolute_path || i != 0) + dirname_builder.append('/'); + dirname_builder.append(canonical_part); + } + m_dirname = dirname_builder.to_string(); + m_basename = canonical_parts.last(); auto name_parts = m_basename.split('.'); m_title = name_parts[0]; diff --git a/AK/FileSystemPath.h b/AK/FileSystemPath.h index 3b03b64eaf..0b63572cef 100644 --- a/AK/FileSystemPath.h +++ b/AK/FileSystemPath.h @@ -12,6 +12,7 @@ public: bool is_valid() const { return m_is_valid; } const String& string() const { return m_string; } + const String& dirname() const { return m_dirname; } const String& basename() const { return m_basename; } const String& title() const { return m_title; } const String& extension() const { return m_extension; } @@ -25,6 +26,7 @@ private: Vector m_parts; String m_string; + String m_dirname; String m_basename; String m_title; String m_extension;