2016-05-04 19:24:41 +02:00
|
|
|
/*****************************************************************************
|
2024-12-31 20:30:04 +01:00
|
|
|
* Copyright (c) 2014-2025 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
|
|
|
|
2022-02-18 21:57:00 +01:00
|
|
|
#include "../platform/Platform.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
|
|
|
|
|
|
|
|
#include <iterator>
|
2016-01-12 19:08:41 +00:00
|
|
|
|
2024-07-26 09:59:58 +02:00
|
|
|
namespace OpenRCT2::Path
|
2016-01-12 19:08:41 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
2023-05-09 21:08:46 +02:00
|
|
|
bool CreateDirectory(u8string_view path)
|
2017-11-28 20:57:34 +00:00
|
|
|
{
|
2023-05-09 21:08:46 +02:00
|
|
|
std::error_code ec;
|
|
|
|
|
fs::create_directories(fs::u8path(path), ec);
|
|
|
|
|
// create_directories returns false if the directory already exists, but the error code is zero.
|
|
|
|
|
return ec.value() == 0;
|
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)
|
|
|
|
|
{
|
2023-10-11 22:45:43 +02:00
|
|
|
auto p = fs::u8path(path);
|
|
|
|
|
|
|
|
|
|
fs::path extensionWithDot;
|
|
|
|
|
if (!newExtension.empty() && newExtension.front() != '.')
|
|
|
|
|
{
|
|
|
|
|
extensionWithDot += ".";
|
|
|
|
|
}
|
|
|
|
|
extensionWithDot += fs::u8path(newExtension);
|
|
|
|
|
|
|
|
|
|
if (p.extension() != extensionWithDot)
|
|
|
|
|
{
|
|
|
|
|
p += extensionWithDot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return p.u8string();
|
2022-02-26 17:33:12 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-09 18:34:38 +01:00
|
|
|
bool IsAbsolute(u8string_view path)
|
|
|
|
|
{
|
|
|
|
|
auto p = fs::u8path(path);
|
|
|
|
|
return p.is_absolute();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-05-09 21:08:46 +02:00
|
|
|
u8string GetRelative(u8string_view path, u8string_view base)
|
|
|
|
|
{
|
|
|
|
|
std::error_code ec;
|
|
|
|
|
return fs::relative(fs::u8path(path), fs::u8path(base), ec).u8string();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 13:10:59 +01:00
|
|
|
bool Equals(u8string_view a, u8string_view b)
|
2016-01-29 19:39:31 +00:00
|
|
|
{
|
2024-12-11 22:28:31 +01:00
|
|
|
return Platform::ShouldIgnoreCase() ? String::iequals(a, b) : String::equals(a, b);
|
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
|
|
|
}
|
2024-07-26 09:59:58 +02:00
|
|
|
} // namespace OpenRCT2::Path
|