2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-06-29 11:11:03 +00:00
|
|
|
* Copyright (c) 2021, Max Wipfli <max.wipfli@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-28 07:54:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-09-06 13:34:26 +00:00
|
|
|
#include <AK/String.h>
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/Vector.h>
|
2018-10-28 07:54:20 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2020-05-26 11:52:44 +00:00
|
|
|
class LexicalPath {
|
2018-10-28 07:54:20 +00:00
|
|
|
public:
|
2021-01-10 23:29:28 +00:00
|
|
|
LexicalPath() = default;
|
2021-04-16 21:44:06 +00:00
|
|
|
explicit LexicalPath(String);
|
2018-10-28 07:54:20 +00:00
|
|
|
|
2020-04-27 12:09:08 +00:00
|
|
|
bool is_absolute() const { return m_is_absolute; }
|
2021-06-29 11:01:56 +00:00
|
|
|
String const& string() const { return m_string; }
|
2018-10-28 07:54:20 +00:00
|
|
|
|
2021-06-29 11:01:56 +00:00
|
|
|
String const& dirname() const { return m_dirname; }
|
|
|
|
String const& basename() const { return m_basename; }
|
|
|
|
String const& title() const { return m_title; }
|
|
|
|
String const& extension() const { return m_extension; }
|
2018-11-18 13:57:41 +00:00
|
|
|
|
2021-06-29 11:01:56 +00:00
|
|
|
Vector<String> const& parts() const { return m_parts; }
|
2019-03-30 02:27:25 +00:00
|
|
|
|
2021-06-29 11:01:56 +00:00
|
|
|
bool has_extension(StringView const&) const;
|
2019-05-26 20:33:30 +00:00
|
|
|
|
2021-05-12 19:17:39 +00:00
|
|
|
void append(String const& component);
|
|
|
|
|
2021-04-16 21:44:06 +00:00
|
|
|
static String canonicalized_path(String);
|
|
|
|
static String relative_path(String absolute_path, String const& prefix);
|
2020-05-26 11:52:44 +00:00
|
|
|
|
2021-05-12 19:17:39 +00:00
|
|
|
template<typename... S>
|
|
|
|
static LexicalPath join(String const& first, S&&... rest)
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(first);
|
|
|
|
((builder.append('/'), builder.append(forward<S>(rest))), ...);
|
|
|
|
|
|
|
|
return LexicalPath { builder.to_string() };
|
|
|
|
}
|
|
|
|
|
2021-06-29 14:46:16 +00:00
|
|
|
static String dirname(String path)
|
|
|
|
{
|
|
|
|
auto lexical_path = LexicalPath(move(path));
|
|
|
|
return lexical_path.dirname();
|
|
|
|
}
|
|
|
|
|
|
|
|
static String basename(String path)
|
|
|
|
{
|
|
|
|
auto lexical_path = LexicalPath(move(path));
|
|
|
|
return lexical_path.basename();
|
|
|
|
}
|
|
|
|
|
|
|
|
static String title(String path)
|
|
|
|
{
|
|
|
|
auto lexical_path = LexicalPath(move(path));
|
|
|
|
return lexical_path.title();
|
|
|
|
}
|
|
|
|
|
|
|
|
static String extension(String path)
|
|
|
|
{
|
|
|
|
auto lexical_path = LexicalPath(move(path));
|
|
|
|
return lexical_path.extension();
|
|
|
|
}
|
|
|
|
|
2018-10-28 07:54:20 +00:00
|
|
|
private:
|
2019-07-15 04:49:28 +00:00
|
|
|
void canonicalize();
|
2018-10-28 07:54:20 +00:00
|
|
|
|
2019-03-30 02:27:25 +00:00
|
|
|
Vector<String> m_parts;
|
2018-10-28 07:54:20 +00:00
|
|
|
String m_string;
|
2020-01-07 11:35:45 +00:00
|
|
|
String m_dirname;
|
2018-11-18 13:57:41 +00:00
|
|
|
String m_basename;
|
2019-07-29 04:45:50 +00:00
|
|
|
String m_title;
|
|
|
|
String m_extension;
|
2020-04-27 12:09:08 +00:00
|
|
|
bool m_is_absolute { false };
|
2018-10-28 07:54:20 +00:00
|
|
|
};
|
|
|
|
|
2020-10-08 12:09:38 +00:00
|
|
|
template<>
|
|
|
|
struct Formatter<LexicalPath> : Formatter<StringView> {
|
2021-06-29 11:01:56 +00:00
|
|
|
void format(FormatBuilder& builder, LexicalPath const& value)
|
2020-10-08 12:09:38 +00:00
|
|
|
{
|
2020-12-30 11:14:15 +00:00
|
|
|
Formatter<StringView>::format(builder, value.string());
|
2020-10-08 12:09:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-28 07:54:20 +00:00
|
|
|
};
|
|
|
|
|
2020-05-26 11:52:44 +00:00
|
|
|
using AK::LexicalPath;
|