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"
|
2021-03-25 23:45:01 -03:00
|
|
|
#include "../platform/Platform2.h"
|
2017-09-18 17:05:28 +02: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
|
|
|
|
|
{
|
2018-06-22 22:58:39 +02:00
|
|
|
utf8* Append(utf8* buffer, size_t bufferSize, const utf8* src)
|
2017-01-05 17:51:31 +00:00
|
|
|
{
|
|
|
|
|
return safe_strcat_path(buffer, src, bufferSize);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-09 20:54:02 +00:00
|
|
|
std::string Combine(std::string_view a, std::string_view b)
|
|
|
|
|
{
|
|
|
|
|
if (a.empty())
|
|
|
|
|
return std::string(b);
|
|
|
|
|
if (b.empty())
|
|
|
|
|
return std::string(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 std::string(a) + std::string(b.substr(1));
|
|
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
|
|
|
|
return std::string(a) + std::string(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
|
|
|
{
|
2021-09-15 22:22:15 +02:00
|
|
|
return std::string(a) + std::string(b);
|
2021-01-09 20:54:02 +00:00
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
|
|
|
|
return std::string(a) + PATH_SEPARATOR + std::string(b);
|
2017-01-05 13:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-08 13:57:29 +01:00
|
|
|
std::string GetDirectory(std::string_view path)
|
2016-01-12 19:08:41 +00:00
|
|
|
{
|
2022-01-11 19:34:33 +00:00
|
|
|
return u8path(path).parent_path().u8string();
|
2016-01-29 23:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-08 13:57:29 +01:00
|
|
|
void CreateDirectory(std::string_view path)
|
2017-11-28 20:57:34 +00:00
|
|
|
{
|
2022-01-08 13:57:29 +01:00
|
|
|
platform_ensure_directory_exists(std::string(path).c_str());
|
2017-11-28 20:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-08 13:57:29 +01:00
|
|
|
bool DirectoryExists(std::string_view path)
|
2017-12-01 23:48:46 +00:00
|
|
|
{
|
2022-01-08 18:38:09 +01:00
|
|
|
return fs::is_directory(u8path(path));
|
2017-12-01 23:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-08 13:57:29 +01:00
|
|
|
std::string GetFileName(std::string_view path)
|
2017-01-05 17:51:31 +00:00
|
|
|
{
|
2022-01-11 19:34:33 +00:00
|
|
|
return u8path(path).filename().u8string();
|
2017-01-05 17:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-08 13:57:29 +01:00
|
|
|
std::string GetFileNameWithoutExtension(std::string_view path)
|
2016-04-17 15:21:35 +01:00
|
|
|
{
|
2022-01-11 19:34:33 +00:00
|
|
|
return u8path(path).stem().u8string();
|
2016-04-17 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-08 13:57:29 +01:00
|
|
|
std::string GetExtension(std::string_view path)
|
2017-01-05 17:51:31 +00:00
|
|
|
{
|
2022-01-11 19:34:33 +00:00
|
|
|
return u8path(path).extension().u8string();
|
2017-01-05 17:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-08 13:57:29 +01:00
|
|
|
std::string GetAbsolute(std::string_view relative)
|
2017-12-01 23:48:46 +00:00
|
|
|
{
|
2022-01-26 14:58:35 +01:00
|
|
|
std::error_code ec;
|
|
|
|
|
return fs::absolute(u8path(relative), ec).u8string();
|
2017-12-01 23:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-08 13:57:29 +01:00
|
|
|
bool Equals(std::string_view a, std::string_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-08 13:57:29 +01:00
|
|
|
std::string ResolveCasing(std::string_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
|
|
|
|
|
|
|
|
bool DeleteDirectory(std::string_view path)
|
|
|
|
|
{
|
|
|
|
|
return fs::remove_all(u8path(path)) > 0;
|
|
|
|
|
}
|
2018-05-04 22:40:09 +02:00
|
|
|
} // namespace Path
|