2016-05-04 19:24:41 +02:00
|
|
|
/*****************************************************************************
|
2020-07-21 15:04:34 +02:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2016-05-04 19:24:41 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2016-05-04 19:24:41 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-05-04 19:24:41 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2021-03-26 00:07:53 -03:00
|
|
|
#include "Path.hpp"
|
2017-06-30 20:38:51 +01:00
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
#include "../localisation/Language.h"
|
2022-02-18 21:57:00 +01:00
|
|
|
#include "../platform/Platform.h"
|
2017-12-13 13:02:24 +01:00
|
|
|
#include "../util/Util.h"
|
2017-06-30 20:38:51 +01:00
|
|
|
#include "File.h"
|
2019-11-19 15:40:15 +01:00
|
|
|
#include "FileSystem.hpp"
|
2016-01-12 19:08:41 +00:00
|
|
|
#include "Memory.hpp"
|
|
|
|
|
#include "String.hpp"
|
2018-11-21 23:16:04 +01:00
|
|
|
|
2021-03-26 00:07:53 -03:00
|
|
|
#include <algorithm>
|
2018-11-21 23:16:04 +01:00
|
|
|
#include <iterator>
|
2016-01-12 19:08:41 +00:00
|
|
|
|
|
|
|
|
namespace Path
|
|
|
|
|
{
|
2022-01-27 13:10:59 +01:00
|
|
|
u8string Combine(u8string_view a, u8string_view b)
|
2021-01-09 20:54:02 +00:00
|
|
|
{
|
|
|
|
|
if (a.empty())
|
2022-01-27 13:10:59 +01:00
|
|
|
return u8string(b);
|
2021-01-09 20:54:02 +00:00
|
|
|
if (b.empty())
|
2022-01-27 13:10:59 +01:00
|
|
|
return u8string(a);
|
2021-01-20 21:04:49 +00:00
|
|
|
auto aEnd = a.back();
|
|
|
|
|
auto bBegin = b.front();
|
2021-03-25 23:45:01 -03:00
|
|
|
if (Platform::IsPathSeparator(aEnd))
|
2021-01-09 20:54:02 +00:00
|
|
|
{
|
2021-03-25 23:45:01 -03:00
|
|
|
if (Platform::IsPathSeparator(bBegin))
|
2021-01-09 20:54:02 +00:00
|
|
|
{
|
2022-01-27 13:10:59 +01:00
|
|
|
return u8string(a) + u8string(b.substr(1));
|
2021-01-09 20:54:02 +00:00
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
return u8string(a) + u8string(b);
|
2021-01-09 20:54:02 +00:00
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
|
|
|
|
if (Platform::IsPathSeparator(bBegin))
|
2021-01-09 20:54:02 +00:00
|
|
|
{
|
2022-01-27 13:10:59 +01:00
|
|
|
return u8string(a) + u8string(b);
|
2021-01-09 20:54:02 +00:00
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
return u8string(a) + PATH_SEPARATOR + u8string(b);
|
2017-01-05 13:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
u8string GetDirectory(u8string_view path)
|
2016-01-12 19:08:41 +00:00
|
|
|
{
|
2022-03-18 23:21:23 +01:00
|
|
|
return fs::u8path(path).parent_path().u8string();
|
2016-01-29 23:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
void CreateDirectory(u8string_view path)
|
2017-11-28 20:57:34 +00:00
|
|
|
{
|
2022-02-18 21:57:00 +01:00
|
|
|
Platform::EnsureDirectoryExists(u8string(path).c_str());
|
2017-11-28 20:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
bool DirectoryExists(u8string_view path)
|
2017-12-01 23:48:46 +00:00
|
|
|
{
|
2022-01-27 00:33:17 +01:00
|
|
|
std::error_code ec;
|
2022-03-18 23:21:23 +01:00
|
|
|
const auto result = fs::is_directory(fs::u8path(path), ec);
|
2022-01-27 00:33:17 +01:00
|
|
|
return result && ec.value() == 0;
|
2017-12-01 23:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
u8string GetFileName(u8string_view path)
|
2017-01-05 17:51:31 +00:00
|
|
|
{
|
2022-03-18 23:21:23 +01:00
|
|
|
return fs::u8path(path).filename().u8string();
|
2017-01-05 17:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
u8string GetFileNameWithoutExtension(u8string_view path)
|
2016-04-17 15:21:35 +01:00
|
|
|
{
|
2022-03-18 23:21:23 +01:00
|
|
|
return fs::u8path(path).stem().u8string();
|
2016-04-17 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
u8string GetExtension(u8string_view path)
|
2017-01-05 17:51:31 +00:00
|
|
|
{
|
2022-03-18 23:21:23 +01:00
|
|
|
return fs::u8path(path).extension().u8string();
|
2017-01-05 17:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-26 17:33:12 +01:00
|
|
|
u8string WithExtension(u8string_view path, u8string_view newExtension)
|
|
|
|
|
{
|
2022-03-18 23:21:23 +01:00
|
|
|
return fs::u8path(path).replace_extension(fs::u8path(newExtension)).u8string();
|
2022-02-26 17:33:12 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
u8string GetAbsolute(u8string_view relative)
|
2017-12-01 23:48:46 +00:00
|
|
|
{
|
2022-01-26 14:58:35 +01:00
|
|
|
std::error_code ec;
|
2022-03-18 23:21:23 +01:00
|
|
|
return fs::absolute(fs::u8path(relative), ec).u8string();
|
2017-12-01 23:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
bool Equals(u8string_view a, u8string_view b)
|
2016-01-29 19:39:31 +00:00
|
|
|
{
|
2021-03-25 23:55:45 -03:00
|
|
|
return String::Equals(a, b, Platform::ShouldIgnoreCase());
|
2016-01-29 19:39:31 +00:00
|
|
|
}
|
2017-06-30 20:38:51 +01:00
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
u8string ResolveCasing(u8string_view path)
|
2017-06-30 20:38:51 +01:00
|
|
|
{
|
2021-03-26 00:07:53 -03:00
|
|
|
return Platform::ResolveCasing(path, File::Exists(path));
|
2017-06-30 20:38:51 +01:00
|
|
|
}
|
2022-01-10 12:52:33 +01:00
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
bool DeleteDirectory(u8string_view path)
|
2022-01-10 12:52:33 +01:00
|
|
|
{
|
2022-01-27 00:33:17 +01:00
|
|
|
std::error_code ec;
|
2022-03-18 23:21:23 +01:00
|
|
|
const auto result = fs::remove_all(fs::u8path(path), ec);
|
2022-01-27 00:33:17 +01:00
|
|
|
return (result > 0) && ec.value() == 0;
|
2022-01-10 12:52:33 +01:00
|
|
|
}
|
2018-05-04 22:40:09 +02:00
|
|
|
} // namespace Path
|