Files
OpenRCT2-Web/src/openrct2/core/Path.cpp
T

136 lines
3.6 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
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
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2021-03-26 00:07:53 -03:00
#include "Path.hpp"
2022-02-18 21:57:00 +01:00
#include "../platform/Platform.h"
#include "File.h"
#include "FileSystem.hpp"
#include "Memory.hpp"
#include "String.hpp"
2018-11-21 23:16:04 +01:00
#include <iterator>
namespace OpenRCT2::Path
{
u8string Combine(u8string_view a, u8string_view b)
2021-01-09 20:54:02 +00:00
{
if (a.empty())
return u8string(b);
2021-01-09 20:54:02 +00:00
if (b.empty())
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
{
return u8string(a) + u8string(b.substr(1));
2021-01-09 20:54:02 +00:00
}
2021-09-15 22:22:15 +02: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
{
return u8string(a) + u8string(b);
2021-01-09 20:54:02 +00:00
}
2021-09-15 22:22:15 +02:00
return u8string(a) + PATH_SEPARATOR + u8string(b);
2017-01-05 13:13:22 +00:00
}
u8string GetDirectory(u8string_view path)
{
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
}
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
}
u8string GetFileName(u8string_view path)
{
2022-03-18 23:21:23 +01:00
return fs::u8path(path).filename().u8string();
}
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
}
u8string GetExtension(u8string_view path)
{
2022-03-18 23:21:23 +01:00
return fs::u8path(path).extension().u8string();
}
u8string WithExtension(u8string_view path, u8string_view newExtension)
{
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-05-09 18:34:38 +01:00
bool IsAbsolute(u8string_view path)
{
auto p = fs::u8path(path);
return p.is_absolute();
}
u8string GetAbsolute(u8string_view relative)
2017-12-01 23:48:46 +00: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();
}
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
}
u8string ResolveCasing(u8string_view path)
{
2021-03-26 00:07:53 -03:00
return Platform::ResolveCasing(path, File::Exists(path));
}
2022-01-10 12:52:33 +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
}
} // namespace OpenRCT2::Path