AllocatedPath.hxx 7.24 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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
 * 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

23
#include "util/Compiler.h"
24 25 26
#include "Traits.hxx"
#include "Path.hxx"

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

	string value;

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

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

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

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

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

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

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

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

82
	~AllocatedPath() noexcept;
83 84

	gcc_pure
85
	operator Path() const noexcept {
86 87 88 89 90 91
		return Path::FromFS(c_str());
	}

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

97
	gcc_pure gcc_nonnull_all
98
	static AllocatedPath Build(Path a, string_view b) noexcept {
99 100 101 102
		return Build(a.c_str(), b);
	}

	gcc_pure gcc_nonnull_all
103
	static AllocatedPath Build(Path a, Path b) noexcept {
104 105 106
		return Build(a, b.c_str());
	}

107
	gcc_pure gcc_nonnull_all
108
	static AllocatedPath Build(string_view a,
109
				   const AllocatedPath &b) noexcept {
110
		return Build(a, b.value);
111 112 113
	}

	gcc_pure gcc_nonnull_all
114
	static AllocatedPath Build(const AllocatedPath &a,
115 116
				   string_view b) noexcept {
		return Build(a.value, b);
117 118 119 120
	}

	gcc_pure
	static AllocatedPath Build(const AllocatedPath &a,
121
				   const AllocatedPath &b) noexcept {
122
		return Build(a.value, b.value);
123 124
	}

Max Kellermann's avatar
Max Kellermann committed
125 126 127 128 129
	gcc_pure
	static AllocatedPath Apply(Path base, Path path) noexcept {
		return Traits::Apply(base.c_str(), path.c_str());
	}

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

139 140 141 142 143
	gcc_pure
	static AllocatedPath FromFS(string_view fs) noexcept {
		return AllocatedPath(fs);
	}

144
	gcc_pure
145 146
	static AllocatedPath FromFS(const_pointer _begin,
				    const_pointer _end) noexcept {
147 148 149
		return AllocatedPath(_begin, _end);
	}

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

159 160 161 162 163 164 165 166 167 168 169 170 171
#ifdef ANDROID
	gcc_pure
	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

172
	/**
173
	 * Convert a UTF-8 C string to an #AllocatedPath instance.
174 175 176
	 * Returns return a "nulled" instance on error.
	 */
	gcc_pure gcc_nonnull_all
177
	static AllocatedPath FromUTF8(const char *path_utf8) noexcept;
178

179 180 181 182
	/**
	 * Convert a UTF-8 C string to an #AllocatedPath instance.
	 * Throws a std::runtime_error on error.
	 */
183
	gcc_nonnull_all
184 185
	static AllocatedPath FromUTF8Throw(const char *path_utf8);

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

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

199
	gcc_pure
200
	bool operator==(const AllocatedPath &other) const noexcept {
201 202 203 204
		return value == other.value;
	}

	gcc_pure
205
	bool operator!=(const AllocatedPath &other) const noexcept {
206 207 208
		return value != other.value;
	}

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

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

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

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

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

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

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

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

	/**
	 * 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.
	 */
	gcc_pure
292
	const_pointer Relative(Path other_fs) const noexcept {
293
		return Traits::Relative(c_str(), other_fs.c_str());
294
	}
295

296
	gcc_pure
297
	const_pointer GetSuffix() const noexcept {
298 299 300
		return ((Path)*this).GetSuffix();
	}

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

	gcc_pure
307
	bool IsAbsolute() const noexcept {
308
		return Traits::IsAbsolute(c_str());
309 310 311 312
	}
};

#endif