Path.hxx 4.21 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20 21
#ifndef MPD_FS_PATH_HXX
#define MPD_FS_PATH_HXX
Max Kellermann's avatar
Max Kellermann committed
22

23
#include "Traits.hxx"
Warren Dukes's avatar
Warren Dukes committed
24

25
#include <cassert>
26
#include <string>
27

28 29
class AllocatedPath;

30 31
/**
 * A path name in the native file system character set.
32 33 34
 *
 * This class manages a pointer to an existing path string.  While an
 * instance lives, the string must not be invalidated.
35
 */
36
class Path : public PathTraitsFS::Pointer {
37
	using Traits = PathTraitsFS;
38
	using Base = Traits::Pointer;
39

40
	explicit constexpr Path(const_pointer _value) noexcept:Base(_value) {}
41 42 43

public:
	/**
44
	 * Construct a "nulled" instance.  Its IsNull() method will
45 46 47 48
	 * return true.  Such an object must not be used.
	 *
	 * @see IsNull()
	 */
49
	constexpr Path(std::nullptr_t) noexcept:Base(nullptr) {}
50 51 52 53 54

	/**
	 * Copy a #Path object.
	 */
	constexpr Path(const Path &) = default;
55 56

	/**
57 58
	 * Create a new instance pointing to the specified path
	 * string.
59
	 */
60
	static constexpr Path FromFS(const_pointer fs) noexcept {
61
		return Path(fs);
62 63 64 65 66
	}

	/**
	 * Copy a #Path object.
	 */
67
	Path &operator=(const Path &) = default;
68 69 70 71 72

	/**
	 * Check if this is a "nulled" instance.  A "nulled" instance
	 * must not be used.
	 */
73
	constexpr bool IsNull() const noexcept {
74
		return Base::IsNull();
75 76 77 78 79 80 81
	}

	/**
	 * Clear this object's value, make it "nulled".
	 *
	 * @see IsNull()
	 */
82
	void SetNull() noexcept {
83
		*this = nullptr;
84 85 86 87 88 89
	}

	/**
	 * @return the length of this string in number of "value_type"
	 * elements (which may not be the number of characters).
	 */
90
	[[gnu::pure]]
91
	size_t length() const noexcept {
92
		assert(!IsNull());
93

94
		return Traits::GetLength(c_str());
95 96 97 98 99 100 101
	}

	/**
	 * Returns the value as a const C string.  The returned
	 * pointer is invalidated whenever the value of life of this
	 * instance ends.
	 */
102
	constexpr const_pointer c_str() const noexcept {
103
		return Base::c_str();
104 105
	}

106 107 108 109
	/**
	 * Returns a pointer to the raw value, not necessarily
	 * null-terminated.
	 */
110
	constexpr const_pointer data() const noexcept {
111
		return c_str();
112 113
	}

114 115 116 117 118
	/**
	 * Does the path contain a newline character?  (Which is
	 * usually rejected by MPD because its protocol cannot
	 * transfer newline characters).
	 */
119
	[[gnu::pure]]
120
	bool HasNewline() const noexcept {
121
		return Traits::Find(c_str(), '\n') != nullptr;
122 123
	}

124
	/**
125 126 127
	 * Convert the path to UTF-8.
	 * Returns empty string on error or if this instance is "nulled"
	 * (#IsNull returns true).
128
	 */
129
	[[gnu::pure]]
130
	std::string ToUTF8() const noexcept;
131

132 133 134 135 136
	/**
	 * Like ToUTF8(), but throws on error.
	 */
	std::string ToUTF8Throw() const;

137 138 139 140
	/**
	 * Determine the "base" file name.
	 * The return value points inside this object.
	 */
141
	[[gnu::pure]]
142
	Path GetBase() const noexcept {
143
		return FromFS(Traits::GetBase(c_str()));
144 145 146 147 148 149
	}

	/**
	 * Gets directory name of this path.
	 * Returns a "nulled" instance on error.
	 */
150
	[[gnu::pure]]
151
	AllocatedPath GetDirectoryName() const noexcept;
152

153 154 155 156 157 158
	/**
	 * 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.
	 */
159
	[[gnu::pure]]
160
	const_pointer Relative(Path other_fs) const noexcept {
161
		return Traits::Relative(c_str(), other_fs.c_str());
162
	}
163

164
	[[gnu::pure]]
165
	bool IsAbsolute() const noexcept {
166
		return Traits::IsAbsolute(c_str());
167
	}
168

169
	[[gnu::pure]]
170
	const_pointer GetSuffix() const noexcept;
171 172
};

173 174 175 176 177 178 179 180
/**
 * Concatenate two path components using the directory separator.
 *
 * Wrapper for AllocatedPath::Build().
 */
AllocatedPath
operator/(Path a, Path b) noexcept;

Warren Dukes's avatar
Warren Dukes committed
181
#endif