2013-10-17 21:59:35 +02:00
|
|
|
/*
|
2022-07-14 17:58:12 +02:00
|
|
|
* Copyright 2003-2022 The Music Player Daemon Project
|
2013-10-17 21:59:35 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MPD_FS_ALLOCATED_PATH_HXX
|
|
|
|
#define MPD_FS_ALLOCATED_PATH_HXX
|
|
|
|
|
|
|
|
#include "Traits.hxx"
|
|
|
|
#include "Path.hxx"
|
|
|
|
|
2015-02-25 16:13:14 +01:00
|
|
|
#include <cstddef>
|
2013-10-17 21:59:35 +02:00
|
|
|
#include <utility>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A path name in the native file system character set.
|
|
|
|
*
|
|
|
|
* This class manages the memory chunk where this path string is
|
|
|
|
* stored.
|
|
|
|
*/
|
|
|
|
class AllocatedPath {
|
2018-07-18 13:05:23 +02:00
|
|
|
using Traits = PathTraitsFS;
|
2020-03-13 19:32:51 +01:00
|
|
|
using string = Traits::string;
|
2020-03-13 19:43:39 +01:00
|
|
|
using string_view = Traits::string_view;
|
2020-03-13 19:32:51 +01:00
|
|
|
using value_type = Traits::value_type;
|
|
|
|
using pointer = Traits::pointer;
|
|
|
|
using const_pointer = Traits::const_pointer;
|
2013-10-17 21:59:35 +02:00
|
|
|
|
|
|
|
string value;
|
|
|
|
|
2020-01-03 15:49:29 +01:00
|
|
|
explicit AllocatedPath(const_pointer _value) noexcept
|
2019-04-24 14:18:24 +02:00
|
|
|
:value(_value) {}
|
2013-10-17 21:59:35 +02:00
|
|
|
|
2020-03-13 19:46:12 +01:00
|
|
|
explicit AllocatedPath(string_view _value) noexcept
|
|
|
|
:value(_value) {}
|
|
|
|
|
2020-01-03 15:49:29 +01:00
|
|
|
AllocatedPath(const_pointer _begin, const_pointer _end) noexcept
|
2016-04-21 14:20:41 +02:00
|
|
|
:value(_begin, _end) {}
|
|
|
|
|
2019-04-24 14:18:24 +02:00
|
|
|
AllocatedPath(string &&_value) noexcept
|
|
|
|
:value(std::move(_value)) {}
|
2013-12-03 07:16:53 +01:00
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
public:
|
2018-01-17 12:17:41 +01:00
|
|
|
/**
|
|
|
|
* Construct a "nulled" instance. Its IsNull() method will
|
|
|
|
* return true. Such an object must not be used.
|
|
|
|
*
|
|
|
|
* @see IsNull()
|
|
|
|
*/
|
2019-04-24 14:18:24 +02:00
|
|
|
AllocatedPath(std::nullptr_t) noexcept:value() {}
|
2018-01-17 12:17:41 +01:00
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
/**
|
2014-09-28 18:06:14 +02:00
|
|
|
* Copy an #AllocatedPath object.
|
2013-10-17 21:59:35 +02:00
|
|
|
*/
|
|
|
|
AllocatedPath(const AllocatedPath &) = default;
|
|
|
|
|
|
|
|
/**
|
2014-09-28 18:06:14 +02:00
|
|
|
* Move an #AllocatedPath object.
|
2013-10-17 21:59:35 +02:00
|
|
|
*/
|
2019-04-24 14:18:24 +02:00
|
|
|
AllocatedPath(AllocatedPath &&other) noexcept
|
|
|
|
:value(std::move(other.value)) {}
|
2013-10-17 21:59:35 +02:00
|
|
|
|
2019-04-24 14:18:24 +02:00
|
|
|
explicit AllocatedPath(Path other) noexcept
|
|
|
|
:value(other.c_str()) {}
|
2014-01-30 23:36:53 +01:00
|
|
|
|
2019-04-24 14:18:24 +02:00
|
|
|
~AllocatedPath() noexcept;
|
2013-10-17 21:59:35 +02:00
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2017-06-03 21:33:44 +02:00
|
|
|
operator Path() const noexcept {
|
2013-10-17 21:59:35 +02:00
|
|
|
return Path::FromFS(c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Join two path components with the path separator.
|
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2020-03-13 19:49:46 +01:00
|
|
|
static AllocatedPath Build(string_view a, string_view b) noexcept {
|
|
|
|
return AllocatedPath(Traits::Build(a, b));
|
2013-12-03 08:50:50 +01:00
|
|
|
}
|
2013-10-17 21:59:35 +02:00
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2020-03-13 19:49:46 +01:00
|
|
|
static AllocatedPath Build(Path a, string_view b) noexcept {
|
2014-01-30 22:15:13 +01:00
|
|
|
return Build(a.c_str(), b);
|
|
|
|
}
|
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2017-06-03 21:33:44 +02:00
|
|
|
static AllocatedPath Build(Path a, Path b) noexcept {
|
2014-01-30 22:15:13 +01:00
|
|
|
return Build(a, b.c_str());
|
|
|
|
}
|
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2020-03-13 19:49:46 +01:00
|
|
|
static AllocatedPath Build(string_view a,
|
2017-06-03 21:33:44 +02:00
|
|
|
const AllocatedPath &b) noexcept {
|
2020-03-13 19:49:46 +01:00
|
|
|
return Build(a, b.value);
|
2013-10-17 21:59:35 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2017-06-03 21:33:44 +02:00
|
|
|
static AllocatedPath Build(const AllocatedPath &a,
|
2020-03-13 19:49:46 +01:00
|
|
|
string_view b) noexcept {
|
|
|
|
return Build(a.value, b);
|
2013-10-17 21:59:35 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2013-10-17 21:59:35 +02:00
|
|
|
static AllocatedPath Build(const AllocatedPath &a,
|
2017-06-03 21:33:44 +02:00
|
|
|
const AllocatedPath &b) noexcept {
|
2020-03-13 19:49:46 +01:00
|
|
|
return Build(a.value, b.value);
|
2013-10-17 21:59:35 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2018-07-18 12:54:44 +02:00
|
|
|
static AllocatedPath Apply(Path base, Path path) noexcept {
|
|
|
|
return Traits::Apply(base.c_str(), path.c_str());
|
|
|
|
}
|
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
/**
|
|
|
|
* Convert a C string that is already in the filesystem
|
|
|
|
* character set to a #Path instance.
|
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2020-01-03 15:49:29 +01:00
|
|
|
static AllocatedPath FromFS(const_pointer fs) noexcept {
|
2013-10-17 21:59:35 +02:00
|
|
|
return AllocatedPath(fs);
|
|
|
|
}
|
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2020-03-13 19:46:12 +01:00
|
|
|
static AllocatedPath FromFS(string_view fs) noexcept {
|
|
|
|
return AllocatedPath(fs);
|
|
|
|
}
|
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2020-01-03 15:49:29 +01:00
|
|
|
static AllocatedPath FromFS(const_pointer _begin,
|
|
|
|
const_pointer _end) noexcept {
|
2016-04-21 14:20:41 +02:00
|
|
|
return AllocatedPath(_begin, _end);
|
|
|
|
}
|
|
|
|
|
2013-12-03 07:16:53 +01:00
|
|
|
/**
|
|
|
|
* Convert a C++ string that is already in the filesystem
|
|
|
|
* character set to a #Path instance.
|
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2017-06-03 21:33:44 +02:00
|
|
|
static AllocatedPath FromFS(string &&fs) noexcept {
|
2013-12-03 07:16:53 +01:00
|
|
|
return AllocatedPath(std::move(fs));
|
|
|
|
}
|
|
|
|
|
2019-04-24 14:39:47 +02:00
|
|
|
#ifdef ANDROID
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2019-04-24 14:39:47 +02:00
|
|
|
static AllocatedPath FromUTF8(std::string &&utf8) noexcept {
|
|
|
|
/* on Android, the filesystem charset is hard-coded to
|
|
|
|
UTF-8 */
|
|
|
|
/* note: we should be using FS_CHARSET_ALWAYS_UTF8
|
|
|
|
here, but that would require adding a dependency on
|
|
|
|
header Features.hxx which I'd like to avoid for
|
|
|
|
now */
|
|
|
|
return FromFS(std::move(utf8));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
/**
|
2014-09-28 18:06:14 +02:00
|
|
|
* Convert a UTF-8 C string to an #AllocatedPath instance.
|
2013-10-17 21:59:35 +02:00
|
|
|
* Returns return a "nulled" instance on error.
|
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2020-04-03 15:55:19 +02:00
|
|
|
static AllocatedPath FromUTF8(std::string_view path_utf8) noexcept;
|
|
|
|
|
|
|
|
static AllocatedPath FromUTF8(const char *path_utf8) noexcept {
|
|
|
|
return FromUTF8(std::string_view(path_utf8));
|
|
|
|
}
|
2013-10-17 21:59:35 +02:00
|
|
|
|
2016-04-12 21:20:32 +02:00
|
|
|
/**
|
|
|
|
* Convert a UTF-8 C string to an #AllocatedPath instance.
|
|
|
|
* Throws a std::runtime_error on error.
|
|
|
|
*/
|
2020-04-03 15:55:19 +02:00
|
|
|
static AllocatedPath FromUTF8Throw(std::string_view path_utf8);
|
2016-04-12 21:20:32 +02:00
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
/**
|
2014-09-28 18:06:14 +02:00
|
|
|
* Copy an #AllocatedPath object.
|
2013-10-17 21:59:35 +02:00
|
|
|
*/
|
|
|
|
AllocatedPath &operator=(const AllocatedPath &) = default;
|
|
|
|
|
|
|
|
/**
|
2014-09-28 18:06:14 +02:00
|
|
|
* Move an #AllocatedPath object.
|
2013-10-17 21:59:35 +02:00
|
|
|
*/
|
2019-04-24 14:18:24 +02:00
|
|
|
AllocatedPath &operator=(AllocatedPath &&other) noexcept {
|
2013-10-17 21:59:35 +02:00
|
|
|
value = std::move(other.value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2017-06-03 21:33:44 +02:00
|
|
|
bool operator==(const AllocatedPath &other) const noexcept {
|
2015-01-13 10:41:54 +01:00
|
|
|
return value == other.value;
|
|
|
|
}
|
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2017-06-03 21:33:44 +02:00
|
|
|
bool operator!=(const AllocatedPath &other) const noexcept {
|
2015-01-13 10:41:54 +01:00
|
|
|
return value != other.value;
|
|
|
|
}
|
|
|
|
|
2014-09-28 18:12:17 +02:00
|
|
|
/**
|
|
|
|
* Allows the caller to "steal" the internal value by
|
|
|
|
* providing a rvalue reference to the std::string attribute.
|
|
|
|
*/
|
2019-04-24 14:18:24 +02:00
|
|
|
string &&Steal() noexcept {
|
2014-09-28 18:12:17 +02:00
|
|
|
return std::move(value);
|
|
|
|
}
|
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
/**
|
|
|
|
* Check if this is a "nulled" instance. A "nulled" instance
|
|
|
|
* must not be used.
|
|
|
|
*/
|
2017-06-03 21:33:44 +02:00
|
|
|
bool IsNull() const noexcept {
|
2013-10-17 21:59:35 +02:00
|
|
|
return value.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear this object's value, make it "nulled".
|
|
|
|
*
|
|
|
|
* @see IsNull()
|
|
|
|
*/
|
2017-06-03 21:33:44 +02:00
|
|
|
void SetNull() noexcept {
|
2013-10-17 21:59:35 +02:00
|
|
|
value.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the length of this string in number of "value_type"
|
|
|
|
* elements (which may not be the number of characters).
|
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2017-06-03 21:33:44 +02:00
|
|
|
size_t length() const noexcept {
|
2013-10-17 21:59:35 +02:00
|
|
|
return value.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value as a const C string. The returned
|
|
|
|
* pointer is invalidated whenever the value of life of this
|
|
|
|
* instance ends.
|
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2020-01-03 15:49:29 +01:00
|
|
|
const_pointer c_str() const noexcept {
|
2013-10-17 21:59:35 +02:00
|
|
|
return value.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a pointer to the raw value, not necessarily
|
|
|
|
* null-terminated.
|
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2020-01-03 15:49:29 +01:00
|
|
|
const_pointer data() const noexcept {
|
2013-10-17 21:59:35 +02:00
|
|
|
return value.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the path to UTF-8.
|
|
|
|
* Returns empty string on error or if this instance is "nulled"
|
|
|
|
* (#IsNull returns true).
|
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2018-07-18 16:38:57 +02:00
|
|
|
std::string ToUTF8() const noexcept {
|
2022-07-14 17:37:20 +02:00
|
|
|
return Path{*this}.ToUTF8();
|
2018-07-18 16:38:57 +02:00
|
|
|
}
|
2013-10-17 21:59:35 +02:00
|
|
|
|
2018-07-18 16:30:46 +02:00
|
|
|
std::string ToUTF8Throw() const {
|
2022-07-14 17:37:20 +02:00
|
|
|
return Path{*this}.ToUTF8Throw();
|
2018-07-18 16:30:46 +02:00
|
|
|
}
|
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
/**
|
|
|
|
* Gets directory name of this path.
|
|
|
|
* Returns a "nulled" instance on error.
|
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2018-07-18 16:38:57 +02:00
|
|
|
AllocatedPath GetDirectoryName() const noexcept {
|
2022-07-14 17:37:20 +02:00
|
|
|
return Path{*this}.GetDirectoryName();
|
2018-07-18 16:38:57 +02:00
|
|
|
}
|
2013-10-17 21:59:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the relative part of the given path to this
|
|
|
|
* object, not including the directory separator. Returns an
|
|
|
|
* empty string if the given path equals this object or
|
|
|
|
* nullptr on mismatch.
|
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2020-01-03 15:49:29 +01:00
|
|
|
const_pointer Relative(Path other_fs) const noexcept {
|
2018-07-18 13:05:23 +02:00
|
|
|
return Traits::Relative(c_str(), other_fs.c_str());
|
2015-02-28 23:30:53 +01:00
|
|
|
}
|
2013-10-17 21:59:35 +02:00
|
|
|
|
2022-07-14 17:36:21 +02:00
|
|
|
/**
|
|
|
|
* Returns the filename suffix (including the dot) or nullptr
|
|
|
|
* if the path does not have one.
|
|
|
|
*/
|
|
|
|
[[gnu::pure]]
|
|
|
|
const_pointer GetSuffix() const noexcept {
|
|
|
|
return Path{*this}.GetSuffix();
|
|
|
|
}
|
|
|
|
|
2022-07-14 15:59:55 +02:00
|
|
|
/**
|
|
|
|
* Replace the suffix of this path (or append the suffix if
|
|
|
|
* there is none currently).
|
|
|
|
*
|
|
|
|
* @param new_suffix the new filename suffix (must start with
|
|
|
|
* a dot)
|
|
|
|
*/
|
|
|
|
void SetSuffix(const_pointer new_suffix) noexcept;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a copy of this path but with the given suffix
|
|
|
|
* (replacing the existing suffix if there is one).
|
|
|
|
*
|
|
|
|
* @param new_suffix the new filename suffix (must start with
|
|
|
|
* a dot)
|
|
|
|
*/
|
|
|
|
[[gnu::pure]]
|
|
|
|
AllocatedPath WithSuffix(const_pointer new_suffix) const noexcept {
|
|
|
|
return Path{*this}.WithSuffix(new_suffix);
|
|
|
|
}
|
|
|
|
|
2022-07-14 17:35:56 +02:00
|
|
|
/**
|
|
|
|
* Returns the filename extension (excluding the dot) or
|
|
|
|
* nullptr if the path does not have one.
|
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2022-07-14 15:56:18 +02:00
|
|
|
const_pointer GetExtension() const noexcept {
|
|
|
|
return Path{*this}.GetExtension();
|
2019-06-15 13:57:16 +02:00
|
|
|
}
|
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
/**
|
|
|
|
* Chop trailing directory separators.
|
|
|
|
*/
|
2017-05-08 14:44:49 +02:00
|
|
|
void ChopSeparators() noexcept;
|
2013-10-17 21:59:35 +02:00
|
|
|
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2017-05-08 14:44:49 +02:00
|
|
|
bool IsAbsolute() const noexcept {
|
2018-07-18 13:05:23 +02:00
|
|
|
return Traits::IsAbsolute(c_str());
|
2013-10-17 21:59:35 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|