AllocatedPath.hxx 7.36 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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"

26
#include <cstddef>
27 28 29 30 31 32 33 34 35 36
#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 {
37
	using Traits = PathTraitsFS;
38
	using string = Traits::string;
39
	using string_view = Traits::string_view;
40 41 42
	using value_type = Traits::value_type;
	using pointer = Traits::pointer;
	using const_pointer = Traits::const_pointer;
43 44 45

	string value;

46
	explicit AllocatedPath(const_pointer _value) noexcept
47
		:value(_value) {}
48

49 50 51
	explicit AllocatedPath(string_view _value) noexcept
		:value(_value) {}

52
	AllocatedPath(const_pointer _begin, const_pointer _end) noexcept
53 54
		:value(_begin, _end) {}

55 56
	AllocatedPath(string &&_value) noexcept
		:value(std::move(_value)) {}
57

58
public:
59 60 61 62 63 64
	/**
	 * Construct a "nulled" instance.  Its IsNull() method will
	 * return true.  Such an object must not be used.
	 *
	 * @see IsNull()
	 */
65
	AllocatedPath(std::nullptr_t) noexcept:value() {}
66

67
	/**
68
	 * Copy an #AllocatedPath object.
69 70 71 72
	 */
	AllocatedPath(const AllocatedPath &) = default;

	/**
73
	 * Move an #AllocatedPath object.
74
	 */
75 76
	AllocatedPath(AllocatedPath &&other) noexcept
		:value(std::move(other.value)) {}
77

78 79
	explicit AllocatedPath(Path other) noexcept
		:value(other.c_str()) {}
80

81
	~AllocatedPath() noexcept;
82

83
	[[gnu::pure]]
84
	operator Path() const noexcept {
85 86 87 88 89 90
		return Path::FromFS(c_str());
	}

	/**
	 * Join two path components with the path separator.
	 */
91
	[[gnu::pure]]
92 93
	static AllocatedPath Build(string_view a, string_view b) noexcept {
		return AllocatedPath(Traits::Build(a, b));
94
	}
95

96
	[[gnu::pure]]
97
	static AllocatedPath Build(Path a, string_view b) noexcept {
98 99 100
		return Build(a.c_str(), b);
	}

101
	[[gnu::pure]]
102
	static AllocatedPath Build(Path a, Path b) noexcept {
103 104 105
		return Build(a, b.c_str());
	}

106
	[[gnu::pure]]
107
	static AllocatedPath Build(string_view a,
108
				   const AllocatedPath &b) noexcept {
109
		return Build(a, b.value);
110 111
	}

112
	[[gnu::pure]]
113
	static AllocatedPath Build(const AllocatedPath &a,
114 115
				   string_view b) noexcept {
		return Build(a.value, b);
116 117
	}

118
	[[gnu::pure]]
119
	static AllocatedPath Build(const AllocatedPath &a,
120
				   const AllocatedPath &b) noexcept {
121
		return Build(a.value, b.value);
122 123
	}

124
	[[gnu::pure]]
125 126 127 128
	static AllocatedPath Apply(Path base, Path path) noexcept {
		return Traits::Apply(base.c_str(), path.c_str());
	}

129 130 131 132
	/**
	 * Convert a C string that is already in the filesystem
	 * character set to a #Path instance.
	 */
133
	[[gnu::pure]]
134
	static AllocatedPath FromFS(const_pointer fs) noexcept {
135 136 137
		return AllocatedPath(fs);
	}

138
	[[gnu::pure]]
139 140 141 142
	static AllocatedPath FromFS(string_view fs) noexcept {
		return AllocatedPath(fs);
	}

143
	[[gnu::pure]]
144 145
	static AllocatedPath FromFS(const_pointer _begin,
				    const_pointer _end) noexcept {
146 147 148
		return AllocatedPath(_begin, _end);
	}

149 150 151 152
	/**
	 * Convert a C++ string that is already in the filesystem
	 * character set to a #Path instance.
	 */
153
	[[gnu::pure]]
154
	static AllocatedPath FromFS(string &&fs) noexcept {
155 156 157
		return AllocatedPath(std::move(fs));
	}

158
#ifdef ANDROID
159
	[[gnu::pure]]
160 161 162 163 164 165 166 167 168 169 170
	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

171
	/**
172
	 * Convert a UTF-8 C string to an #AllocatedPath instance.
173 174
	 * Returns return a "nulled" instance on error.
	 */
175
	[[gnu::pure]]
176 177 178 179 180
	static AllocatedPath FromUTF8(std::string_view path_utf8) noexcept;

	static AllocatedPath FromUTF8(const char *path_utf8) noexcept {
		return FromUTF8(std::string_view(path_utf8));
	}
181

182 183 184 185
	/**
	 * Convert a UTF-8 C string to an #AllocatedPath instance.
	 * Throws a std::runtime_error on error.
	 */
186
	static AllocatedPath FromUTF8Throw(std::string_view path_utf8);
187

188
	/**
189
	 * Copy an #AllocatedPath object.
190 191 192 193
	 */
	AllocatedPath &operator=(const AllocatedPath &) = default;

	/**
194
	 * Move an #AllocatedPath object.
195
	 */
196
	AllocatedPath &operator=(AllocatedPath &&other) noexcept {
197 198 199 200
		value = std::move(other.value);
		return *this;
	}

201
	[[gnu::pure]]
202
	bool operator==(const AllocatedPath &other) const noexcept {
203 204 205
		return value == other.value;
	}

206
	[[gnu::pure]]
207
	bool operator!=(const AllocatedPath &other) const noexcept {
208 209 210
		return value != other.value;
	}

211 212 213 214
	/**
	 * Allows the caller to "steal" the internal value by
	 * providing a rvalue reference to the std::string attribute.
	 */
215
	string &&Steal() noexcept {
216 217 218
		return std::move(value);
	}

219 220 221 222
	/**
	 * Check if this is a "nulled" instance.  A "nulled" instance
	 * must not be used.
	 */
223
	bool IsNull() const noexcept {
224 225 226 227 228 229 230 231
		return value.empty();
	}

	/**
	 * Clear this object's value, make it "nulled".
	 *
	 * @see IsNull()
	 */
232
	void SetNull() noexcept {
233 234 235 236 237 238 239
		value.clear();
	}

	/**
	 * @return the length of this string in number of "value_type"
	 * elements (which may not be the number of characters).
	 */
240
	[[gnu::pure]]
241
	size_t length() const noexcept {
242 243 244 245 246 247 248 249
		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.
	 */
250
	[[gnu::pure]]
251
	const_pointer c_str() const noexcept {
252 253 254 255 256 257 258
		return value.c_str();
	}

	/**
	 * Returns a pointer to the raw value, not necessarily
	 * null-terminated.
	 */
259
	[[gnu::pure]]
260
	const_pointer data() const noexcept {
261 262 263 264 265 266 267 268
		return value.data();
	}

	/**
	 * Convert the path to UTF-8.
	 * Returns empty string on error or if this instance is "nulled"
	 * (#IsNull returns true).
	 */
269
	[[gnu::pure]]
270 271 272
	std::string ToUTF8() const noexcept {
		return ((Path)*this).ToUTF8();
	}
273

274 275 276 277
	std::string ToUTF8Throw() const {
		return ((Path)*this).ToUTF8Throw();
	}

278 279 280 281
	/**
	 * Gets directory name of this path.
	 * Returns a "nulled" instance on error.
	 */
282
	[[gnu::pure]]
283 284 285
	AllocatedPath GetDirectoryName() const noexcept {
		return ((Path)*this).GetDirectoryName();
	}
286 287 288 289 290 291 292

	/**
	 * 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.
	 */
293
	[[gnu::pure]]
294
	const_pointer Relative(Path other_fs) const noexcept {
295
		return Traits::Relative(c_str(), other_fs.c_str());
296
	}
297

298
	[[gnu::pure]]
299
	const_pointer GetSuffix() const noexcept {
300 301 302
		return ((Path)*this).GetSuffix();
	}

303 304 305
	/**
	 * Chop trailing directory separators.
	 */
306
	void ChopSeparators() noexcept;
307

308
	[[gnu::pure]]
309
	bool IsAbsolute() const noexcept {
310
		return Traits::IsAbsolute(c_str());
311 312 313 314
	}
};

#endif