Path.hxx 4.2 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 "util/Compiler.h"
24
#include "Traits.hxx"
Warren Dukes's avatar
Warren Dukes committed
25

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

29 30
class AllocatedPath;

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

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

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

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

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

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

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

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

	/**
	 * @return the length of this string in number of "value_type"
	 * elements (which may not be the number of characters).
	 */
	gcc_pure
92
	size_t length() const noexcept {
93
		assert(!IsNull());
94

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

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

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

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

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

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

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

	/**
	 * Gets directory name of this path.
	 * Returns a "nulled" instance on error.
	 */
	gcc_pure
152
	AllocatedPath GetDirectoryName() const noexcept;
153

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

165
	gcc_pure
166
	bool IsAbsolute() const noexcept {
167
		return Traits::IsAbsolute(c_str());
168
	}
169 170

	gcc_pure
171
	const_pointer GetSuffix() const noexcept;
172 173
};

174 175 176 177 178 179 180 181
/**
 * 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
182
#endif