Path.hxx 3.24 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
/**
 * A path name in the native file system character set.
34 35 36
 *
 * This class manages a pointer to an existing path string.  While an
 * instance lives, the string must not be invalidated.
37 38
 */
class Path {
39 40 41
	typedef PathTraitsFS::value_type value_type;
	typedef PathTraitsFS::pointer pointer;
	typedef PathTraitsFS::const_pointer const_pointer;
42

43
	const char *value;
44

45
	constexpr Path(const_pointer _value):value(_value) {}
46 47 48 49 50

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

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

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

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

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

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

	/**
	 * @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 {
99 100 101
		assert(value != nullptr);

		return strlen(value);
102 103 104 105 106 107 108 109 110
	}

	/**
	 * 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 {
111
		return value;
112 113
	}

114 115 116 117 118 119
	/**
	 * Returns a pointer to the raw value, not necessarily
	 * null-terminated.
	 */
	gcc_pure
	const_pointer data() const {
120
		return value;
121 122
	}

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

131 132 133 134 135 136 137 138
	/**
	 * 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
	const char *RelativeFS(const char *other_fs) const;
139

140 141
	gcc_pure
	bool IsAbsolute() {
142
		return PathTraitsFS::IsAbsolute(c_str());
143
	}
144 145
};

Warren Dukes's avatar
Warren Dukes committed
146
#endif