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

107 lines
2.8 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 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"
2018-06-22 22:58:39 +02:00
#include "../localisation/Language.h"
2021-03-25 23:45:01 -03:00
#include "../platform/Platform2.h"
#include "../platform/platform.h"
2017-12-13 13:02:24 +01:00
#include "../util/Util.h"
#include "File.h"
#include "FileSystem.hpp"
#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>
namespace Path
{
2018-06-22 22:58:39 +02:00
utf8* Append(utf8* buffer, size_t bufferSize, const utf8* src)
{
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)
{
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
{
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)
{
return u8path(path).filename().u8string();
}
2022-01-08 13:57:29 +01:00
std::string GetFileNameWithoutExtension(std::string_view path)
2016-04-17 15:21:35 +01: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)
{
return u8path(path).extension().u8string();
}
2022-01-08 13:57:29 +01:00
std::string GetAbsolute(std::string_view relative)
2017-12-01 23:48:46 +00: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
}
2022-01-08 13:57:29 +01:00
std::string ResolveCasing(std::string_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(std::string_view path)
{
return fs::remove_all(u8path(path)) > 0;
}
2018-05-04 22:40:09 +02:00
} // namespace Path