2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-17 00:43:27 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2013-01-21 19:14:37 +01:00
|
|
|
#ifndef MPD_FS_PATH_HXX
|
|
|
|
#define MPD_FS_PATH_HXX
|
2013-01-17 00:43:27 +01:00
|
|
|
|
|
|
|
#include "check.h"
|
2013-01-17 00:56:57 +01:00
|
|
|
#include "gcc.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-01-17 00:56:57 +01:00
|
|
|
#include <algorithm>
|
2013-01-23 19:48:14 +01:00
|
|
|
#include <string>
|
2013-01-17 00:56:57 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <limits.h>
|
2007-12-28 03:56:25 +01:00
|
|
|
|
|
|
|
#if !defined(MPD_PATH_MAX)
|
2013-01-23 19:53:53 +01:00
|
|
|
# if defined(WIN32)
|
|
|
|
# define MPD_PATH_MAX 260
|
|
|
|
# elif defined(MAXPATHLEN)
|
2007-12-28 03:56:25 +01:00
|
|
|
# define MPD_PATH_MAX MAXPATHLEN
|
|
|
|
# elif defined(PATH_MAX)
|
|
|
|
# define MPD_PATH_MAX PATH_MAX
|
|
|
|
# else
|
|
|
|
# define MPD_PATH_MAX 256
|
|
|
|
# endif
|
|
|
|
#endif
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-09-12 10:02:11 +02:00
|
|
|
class Error;
|
|
|
|
|
2013-09-12 10:03:37 +02:00
|
|
|
extern const class Domain path_domain;
|
|
|
|
|
2013-01-17 00:56:57 +01:00
|
|
|
/**
|
|
|
|
* A path name in the native file system character set.
|
|
|
|
*/
|
|
|
|
class Path {
|
2013-10-01 18:35:37 +02:00
|
|
|
typedef std::string string;
|
|
|
|
|
2013-01-17 00:56:57 +01:00
|
|
|
public:
|
2013-10-01 18:35:37 +02:00
|
|
|
typedef string::value_type value_type;
|
|
|
|
typedef string::pointer pointer;
|
|
|
|
typedef string::const_pointer const_pointer;
|
2013-01-17 00:56:57 +01:00
|
|
|
|
|
|
|
private:
|
2013-10-01 18:35:37 +02:00
|
|
|
string value;
|
2013-01-17 00:56:57 +01:00
|
|
|
|
|
|
|
struct Donate {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Donate the allocated pointer to a new #Path object.
|
|
|
|
*/
|
2013-10-01 18:35:37 +02:00
|
|
|
Path(Donate, pointer _value);
|
2013-01-17 00:56:57 +01:00
|
|
|
|
2013-10-01 18:35:37 +02:00
|
|
|
Path(const_pointer _value):value(_value) {}
|
2013-01-17 00:56:57 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Copy a #Path object.
|
|
|
|
*/
|
2013-10-01 18:35:37 +02:00
|
|
|
Path(const Path &) = default;
|
2013-01-17 00:56:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Move a #Path object.
|
|
|
|
*/
|
2013-10-01 18:35:37 +02:00
|
|
|
Path(Path &&other):value(std::move(other.value)) {}
|
2013-01-17 00:56:57 +01:00
|
|
|
|
2013-10-01 18:35:37 +02:00
|
|
|
~Path();
|
2013-01-17 00:56:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a "nulled" instance. Its IsNull() method will
|
|
|
|
* return true. Such an object must not be used.
|
|
|
|
*
|
|
|
|
* @see IsNull()
|
|
|
|
*/
|
|
|
|
gcc_const
|
|
|
|
static Path Null() {
|
2013-10-01 18:35:37 +02:00
|
|
|
return Path("");
|
2013-01-17 00:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Join two path components with the path separator.
|
|
|
|
*/
|
|
|
|
gcc_pure gcc_nonnull_all
|
2013-10-01 18:35:37 +02:00
|
|
|
static Path Build(const_pointer a, const_pointer b);
|
2013-01-17 00:56:57 +01:00
|
|
|
|
|
|
|
gcc_pure gcc_nonnull_all
|
|
|
|
static Path Build(const_pointer a, const Path &b) {
|
|
|
|
return Build(a, b.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure gcc_nonnull_all
|
|
|
|
static Path Build(const Path &a, const_pointer b) {
|
|
|
|
return Build(a.c_str(), b);
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static Path Build(const Path &a, const Path &b) {
|
|
|
|
return Build(a.c_str(), b.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a C string that is already in the filesystem
|
|
|
|
* character set to a #Path instance.
|
|
|
|
*/
|
|
|
|
gcc_pure
|
|
|
|
static Path FromFS(const_pointer fs) {
|
2013-10-01 18:35:37 +02:00
|
|
|
return Path(fs);
|
2013-01-17 00:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a UTF-8 C string to a #Path instance.
|
2013-01-27 08:26:17 +01:00
|
|
|
* Returns return a "nulled" instance on error.
|
2013-01-17 00:56:57 +01:00
|
|
|
*/
|
|
|
|
gcc_pure
|
2013-01-23 21:35:35 +01:00
|
|
|
static Path FromUTF8(const char *path_utf8);
|
2013-01-17 00:56:57 +01:00
|
|
|
|
2013-09-12 10:02:11 +02:00
|
|
|
gcc_pure
|
|
|
|
static Path FromUTF8(const char *path_utf8, Error &error);
|
|
|
|
|
2013-01-23 21:26:38 +01:00
|
|
|
/**
|
|
|
|
* Convert the path to UTF-8.
|
|
|
|
* Returns empty string on error or if #path_fs is null pointer.
|
|
|
|
*/
|
|
|
|
gcc_pure
|
|
|
|
static std::string ToUTF8(const_pointer path_fs);
|
|
|
|
|
2013-01-27 19:07:31 +01:00
|
|
|
/**
|
|
|
|
* Performs global one-time initialization of this class.
|
|
|
|
*/
|
|
|
|
static void GlobalInit();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets file system character set name.
|
|
|
|
*/
|
|
|
|
static const std::string &GetFSCharset();
|
|
|
|
|
2013-01-17 00:56:57 +01:00
|
|
|
/**
|
|
|
|
* Copy a #Path object.
|
|
|
|
*/
|
2013-10-01 18:35:37 +02:00
|
|
|
Path &operator=(const Path &) = default;
|
2013-01-17 00:56:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Move a #Path object.
|
|
|
|
*/
|
|
|
|
Path &operator=(Path &&other) {
|
2013-10-01 18:35:37 +02:00
|
|
|
value = std::move(other.value);
|
2013-01-17 00:56:57 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this is a "nulled" instance. A "nulled" instance
|
|
|
|
* must not be used.
|
|
|
|
*/
|
|
|
|
bool IsNull() const {
|
2013-10-01 18:35:37 +02:00
|
|
|
return value.empty();
|
2013-01-17 00:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear this object's value, make it "nulled".
|
|
|
|
*
|
|
|
|
* @see IsNull()
|
|
|
|
*/
|
|
|
|
void SetNull() {
|
2013-10-01 18:35:37 +02:00
|
|
|
value.clear();
|
2013-01-17 00:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 {
|
2013-10-01 18:35:37 +02:00
|
|
|
return value.length();
|
2013-01-17 00:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2013-10-01 18:35:37 +02:00
|
|
|
return value.c_str();
|
2013-01-17 00:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-23 19:48:14 +01:00
|
|
|
* Convert the path to UTF-8.
|
|
|
|
* Returns empty string on error or if this instance is "nulled"
|
|
|
|
* (#IsNull returns true).
|
2013-01-17 00:56:57 +01:00
|
|
|
*/
|
2013-01-23 21:26:38 +01:00
|
|
|
std::string ToUTF8() const {
|
2013-10-01 18:35:37 +02:00
|
|
|
return ToUTF8(value.c_str());
|
2013-01-23 21:26:38 +01:00
|
|
|
}
|
2013-02-02 14:28:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets directory name of this path.
|
|
|
|
* Returns a "nulled" instance on error.
|
|
|
|
*/
|
2013-10-01 18:35:37 +02:00
|
|
|
gcc_pure
|
|
|
|
Path GetDirectoryName() const;
|
2013-01-17 00:56:57 +01:00
|
|
|
};
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|