Path.hxx 3.63 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "check.h"
24
#include "Compiler.h"
25
#include "Traits.hxx"
Warren Dukes's avatar
Warren Dukes committed
26

27
#include <string>
28 29

#include <assert.h>
30
#include <string.h>
Warren Dukes's avatar
Warren Dukes committed
31

32 33
class AllocatedPath;

34 35
/**
 * A path name in the native file system character set.
36 37 38
 *
 * This class manages a pointer to an existing path string.  While an
 * instance lives, the string must not be invalidated.
39 40
 */
class Path {
41 42 43
	typedef PathTraitsFS::value_type value_type;
	typedef PathTraitsFS::pointer pointer;
	typedef PathTraitsFS::const_pointer const_pointer;
44

45
	const char *value;
46

47
	constexpr Path(const_pointer _value):value(_value) {}
48 49 50 51 52

public:
	/**
	 * Copy a #Path object.
	 */
53
	constexpr Path(const Path &) = default;
54 55 56 57 58 59 60

	/**
	 * Return a "nulled" instance.  Its IsNull() method will
	 * return true.  Such an object must not be used.
	 *
	 * @see IsNull()
	 */
61 62
	static constexpr Path Null() {
		return Path(nullptr);
63 64 65
	}

	/**
66 67
	 * Create a new instance pointing to the specified path
	 * string.
68
	 */
69
	static constexpr Path FromFS(const_pointer fs) {
70
		return Path(fs);
71 72 73 74 75
	}

	/**
	 * Copy a #Path object.
	 */
76
	Path &operator=(const Path &) = default;
77 78 79 80 81 82

	/**
	 * Check if this is a "nulled" instance.  A "nulled" instance
	 * must not be used.
	 */
	bool IsNull() const {
83
		return value == nullptr;
84 85 86 87 88 89 90 91
	}

	/**
	 * Clear this object's value, make it "nulled".
	 *
	 * @see IsNull()
	 */
	void SetNull() {
92
		value = nullptr;
93 94 95 96 97 98 99 100
	}

	/**
	 * @return the length of this string in number of "value_type"
	 * elements (which may not be the number of characters).
	 */
	gcc_pure
	size_t length() const {
101 102 103
		assert(value != nullptr);

		return strlen(value);
104 105 106 107 108 109 110 111 112
	}

	/**
	 * Returns the value as a const C string.  The returned
	 * pointer is invalidated whenever the value of life of this
	 * instance ends.
	 */
	gcc_pure
	const_pointer c_str() const {
113
		return value;
114 115
	}

116 117 118 119 120 121
	/**
	 * Returns a pointer to the raw value, not necessarily
	 * null-terminated.
	 */
	gcc_pure
	const_pointer data() const {
122
		return value;
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 131
	gcc_pure
	std::string ToUTF8() const;
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	/**
	 * Determine the "base" file name.
	 * The return value points inside this object.
	 */
	gcc_pure
	Path GetBase() const {
		return FromFS(PathTraitsFS::GetBase(value));
	}

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

149 150 151 152 153 154 155
	/**
	 * 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
156 157 158
	const char *RelativeFS(const char *other_fs) const {
		return PathTraitsFS::Relative(value, other_fs);
	}
159

160 161
	gcc_pure
	bool IsAbsolute() {
162
		return PathTraitsFS::IsAbsolute(c_str());
163
	}
164 165
};

Warren Dukes's avatar
Warren Dukes committed
166
#endif