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