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
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-10-17 22:00:01 +02:00
|
|
|
#include "Path.hxx"
|
2013-10-17 22:20:53 +02:00
|
|
|
#include "Domain.hxx"
|
2013-10-17 22:13:54 +02:00
|
|
|
#include "Charset.hxx"
|
2013-09-12 10:02:11 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-10-15 09:21:13 +02:00
|
|
|
#include "Compiler.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-15 19:36:30 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2009-02-20 12:31:00 +01:00
|
|
|
#include <assert.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-10-01 18:35:37 +02:00
|
|
|
inline Path::Path(Donate, pointer _value)
|
|
|
|
:value(_value) {
|
|
|
|
g_free(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no inlining, please */
|
|
|
|
Path::~Path() {}
|
|
|
|
|
|
|
|
Path
|
|
|
|
Path::Build(const_pointer a, const_pointer b)
|
|
|
|
{
|
|
|
|
return Path(Donate(), g_build_filename(a, b, nullptr));
|
|
|
|
}
|
|
|
|
|
2013-01-23 21:35:35 +01:00
|
|
|
Path Path::FromUTF8(const char *path_utf8)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-10-17 22:13:54 +02:00
|
|
|
return Path(Donate(), ::PathFromUTF8(path_utf8));
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2013-09-12 10:02:11 +02:00
|
|
|
Path
|
|
|
|
Path::FromUTF8(const char *path_utf8, Error &error)
|
|
|
|
{
|
|
|
|
Path path = FromUTF8(path_utf8);
|
|
|
|
if (path.IsNull())
|
|
|
|
error.Format(path_domain,
|
|
|
|
"Failed to convert to file system charset: %s",
|
|
|
|
path_utf8);
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2013-10-01 18:35:37 +02:00
|
|
|
Path
|
|
|
|
Path::GetDirectoryName() const
|
|
|
|
{
|
|
|
|
return Path(Donate(), g_path_get_dirname(value.c_str()));
|
|
|
|
}
|
|
|
|
|
2013-10-17 22:13:54 +02:00
|
|
|
std::string
|
|
|
|
Path::ToUTF8() const
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-10-17 22:13:54 +02:00
|
|
|
return ::PathToUTF8(value.c_str());
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2013-10-14 22:09:02 +02:00
|
|
|
|
|
|
|
const char *
|
|
|
|
Path::RelativeFS(const char *other_fs) const
|
|
|
|
{
|
|
|
|
const size_t l = length();
|
|
|
|
if (memcmp(data(), other_fs, l) != 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
other_fs += l;
|
|
|
|
if (*other_fs != 0) {
|
2013-10-17 23:23:25 +02:00
|
|
|
if (!PathTraits::IsSeparatorFS(*other_fs))
|
2013-10-14 22:09:02 +02:00
|
|
|
/* mismatch */
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
/* skip remaining path separators */
|
|
|
|
do {
|
|
|
|
++other_fs;
|
2013-10-17 23:23:25 +02:00
|
|
|
} while (PathTraits::IsSeparatorFS(*other_fs));
|
2013-10-14 22:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return other_fs;
|
|
|
|
}
|
2013-10-14 22:26:23 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
Path::ChopSeparators()
|
|
|
|
{
|
|
|
|
size_t l = length();
|
|
|
|
const char *p = data();
|
|
|
|
|
2013-10-17 23:23:25 +02:00
|
|
|
while (l >= 2 && PathTraits::IsSeparatorFS(p[l - 1])) {
|
2013-10-14 22:26:23 +02:00
|
|
|
--l;
|
|
|
|
|
|
|
|
#if GCC_CHECK_VERSION(4,7) && !defined(__clang__)
|
|
|
|
value.pop_back();
|
|
|
|
#else
|
|
|
|
value.erase(value.end() - 1, value.end());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|