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
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2018-02-01 12:52:39 +00:00
|
|
|
#include <algorithm>
|
2017-06-30 20:38:51 +01:00
|
|
|
#ifndef _WIN32
|
2018-07-21 16:17:06 +02:00
|
|
|
# include <dirent.h>
|
2017-06-30 20:38:51 +01:00
|
|
|
#endif
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
#include "../localisation/Language.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 "Path.hpp"
|
|
|
|
|
#include "String.hpp"
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
std::string Combine(const std::string& a, const std::string& b)
|
2017-01-05 13:13:22 +00:00
|
|
|
{
|
|
|
|
|
utf8 buffer[MAX_PATH];
|
|
|
|
|
String::Set(buffer, sizeof(buffer), a.c_str());
|
|
|
|
|
Path::Append(buffer, sizeof(buffer), b.c_str());
|
|
|
|
|
return std::string(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
std::string GetDirectory(const std::string& path)
|
2016-01-12 19:08:41 +00:00
|
|
|
{
|
2017-05-26 10:04:13 +02:00
|
|
|
const utf8* directory = GetDirectory(path.c_str());
|
|
|
|
|
std::string result(directory);
|
|
|
|
|
Memory::Free(directory);
|
|
|
|
|
return result;
|
2016-01-12 19:08:41 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
utf8* GetDirectory(const utf8* path)
|
2016-11-13 13:15:01 +00:00
|
|
|
{
|
|
|
|
|
size_t maxSize = String::SizeOf(path) + 1;
|
2018-06-22 22:58:39 +02:00
|
|
|
utf8* result = Memory::Allocate<utf8>(maxSize);
|
2016-11-13 13:15:01 +00:00
|
|
|
GetDirectory(result, maxSize, path);
|
|
|
|
|
size_t reducedSize = String::SizeOf(path) + 1;
|
|
|
|
|
result = Memory::Reallocate(result, reducedSize);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
utf8* GetDirectory(utf8* buffer, size_t bufferSize, const utf8* path)
|
2016-01-29 23:18:28 +00:00
|
|
|
{
|
2018-06-22 22:58:39 +02:00
|
|
|
auto lastPathSepIndex = std::max(String::LastIndexOf(path, *PATH_SEPARATOR), String::LastIndexOf(path, '/'));
|
2017-10-08 13:28:38 -06:00
|
|
|
if (lastPathSepIndex < 0)
|
2016-01-29 23:18:28 +00:00
|
|
|
{
|
|
|
|
|
return String::Set(buffer, bufferSize, String::Empty);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:11:35 +02:00
|
|
|
size_t copyLength = std::min(lastPathSepIndex, static_cast<ptrdiff_t>(bufferSize - 1));
|
2018-02-01 12:52:39 +00:00
|
|
|
std::copy_n(path, copyLength, buffer);
|
2016-01-29 23:18:28 +00:00
|
|
|
buffer[copyLength] = '\0';
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
void CreateDirectory(const std::string& path)
|
2017-11-28 20:57:34 +00:00
|
|
|
{
|
|
|
|
|
platform_ensure_directory_exists(path.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
bool DirectoryExists(const std::string& path)
|
2017-12-01 23:48:46 +00:00
|
|
|
{
|
|
|
|
|
return platform_directory_exists(path.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
std::string GetFileName(const std::string& path)
|
2017-01-05 17:51:31 +00:00
|
|
|
{
|
|
|
|
|
return GetFileName(path.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
const utf8* GetFileName(const utf8* path)
|
2016-04-17 15:21:35 +01:00
|
|
|
{
|
2018-06-22 22:58:39 +02:00
|
|
|
const utf8* lastPathSeparator = nullptr;
|
|
|
|
|
for (const utf8* ch = path; *ch != '\0'; ch++)
|
2016-04-17 15:21:35 +01:00
|
|
|
{
|
2017-10-08 13:28:38 -06:00
|
|
|
if (*ch == *PATH_SEPARATOR || *ch == '/')
|
2016-04-17 15:21:35 +01:00
|
|
|
{
|
2017-11-13 16:25:31 +01:00
|
|
|
lastPathSeparator = ch;
|
2016-04-17 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
return lastPathSeparator == nullptr ? path : lastPathSeparator + 1;
|
2016-04-17 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
std::string GetFileNameWithoutExtension(const std::string& path)
|
2017-01-05 13:13:22 +00:00
|
|
|
{
|
2018-06-22 22:58:39 +02:00
|
|
|
utf8* cstr = GetFileNameWithoutExtension(path.c_str());
|
2017-01-05 13:13:22 +00:00
|
|
|
std::string result = String::ToStd(cstr);
|
|
|
|
|
Memory::Free(cstr);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
utf8* GetFileNameWithoutExtension(const utf8* path)
|
2016-11-12 15:52:03 +00:00
|
|
|
{
|
|
|
|
|
size_t maxSize = String::SizeOf(path) + 1;
|
2018-06-22 22:58:39 +02:00
|
|
|
utf8* result = Memory::Allocate<utf8>(maxSize);
|
2016-11-12 15:52:03 +00:00
|
|
|
GetFileNameWithoutExtension(result, maxSize, path);
|
|
|
|
|
size_t reducedSize = String::SizeOf(path) + 1;
|
|
|
|
|
result = Memory::Reallocate(result, reducedSize);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
utf8* GetFileNameWithoutExtension(utf8* buffer, size_t bufferSize, const utf8* path)
|
2016-01-29 00:06:02 +00:00
|
|
|
{
|
2016-07-01 21:01:06 +01:00
|
|
|
path = GetFileName(path);
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
const utf8* lastDot = nullptr;
|
|
|
|
|
const utf8* ch = path;
|
2016-01-29 23:18:28 +00:00
|
|
|
for (; *ch != '\0'; ch++)
|
2016-01-29 00:06:02 +00:00
|
|
|
{
|
|
|
|
|
if (*ch == '.')
|
|
|
|
|
{
|
|
|
|
|
lastDot = ch;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lastDot == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return String::Set(buffer, bufferSize, path);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 17:11:35 +02:00
|
|
|
size_t truncatedLength = std::min<size_t>(bufferSize - 1, lastDot - path);
|
2018-02-01 12:52:39 +00:00
|
|
|
std::copy_n(path, truncatedLength, buffer);
|
2016-01-29 00:06:02 +00:00
|
|
|
buffer[truncatedLength] = '\0';
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
const std::string GetExtension(const std::string& path)
|
2017-01-05 17:51:31 +00:00
|
|
|
{
|
2020-01-09 20:47:54 +00:00
|
|
|
return fs::u8path(path).extension().string();
|
2017-01-05 17:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
const utf8* GetExtension(const utf8* path)
|
2016-04-19 22:09:42 +01:00
|
|
|
{
|
2018-06-22 22:58:39 +02:00
|
|
|
const utf8* lastDot = nullptr;
|
|
|
|
|
const utf8* ch = GetFileName(path);
|
2016-04-19 22:09:42 +01:00
|
|
|
for (; *ch != '\0'; ch++)
|
|
|
|
|
{
|
|
|
|
|
if (*ch == '.')
|
|
|
|
|
{
|
|
|
|
|
lastDot = ch;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lastDot == nullptr)
|
|
|
|
|
{
|
|
|
|
|
// Return the null terminator, i.e. a blank extension
|
|
|
|
|
return ch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return the extension including the dot
|
|
|
|
|
return lastDot;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
utf8* GetAbsolute(utf8* buffer, size_t bufferSize, const utf8* relativePath)
|
2016-01-12 19:08:41 +00:00
|
|
|
{
|
2017-06-12 18:00:15 +01:00
|
|
|
#ifdef _WIN32
|
2019-07-22 23:11:12 +01:00
|
|
|
auto relativePathW = String::ToWideChar(relativePath);
|
2018-06-22 22:58:39 +02:00
|
|
|
wchar_t absolutePathW[MAX_PATH];
|
2020-07-14 12:48:46 -03:00
|
|
|
DWORD length = GetFullPathNameW(
|
|
|
|
|
relativePathW.c_str(), static_cast<DWORD>(std::size(absolutePathW)), absolutePathW, nullptr);
|
2016-01-12 19:08:41 +00:00
|
|
|
if (length == 0)
|
|
|
|
|
{
|
|
|
|
|
return String::Set(buffer, bufferSize, relativePath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-07-22 22:13:08 +01:00
|
|
|
auto absolutePath = String::ToUtf8(absolutePathW);
|
|
|
|
|
String::Set(buffer, bufferSize, absolutePath.c_str());
|
2016-01-12 19:08:41 +00:00
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
#else
|
2018-06-22 22:58:39 +02:00
|
|
|
utf8* absolutePath = realpath(relativePath, nullptr);
|
2016-01-12 19:08:41 +00:00
|
|
|
if (absolutePath == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return String::Set(buffer, bufferSize, relativePath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
String::Set(buffer, bufferSize, absolutePath);
|
|
|
|
|
Memory::Free(absolutePath);
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2016-01-29 19:39:31 +00:00
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
std::string GetAbsolute(const std::string& relative)
|
2017-12-01 23:48:46 +00:00
|
|
|
{
|
|
|
|
|
utf8 absolute[MAX_PATH];
|
|
|
|
|
return GetAbsolute(absolute, sizeof(absolute), relative.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
bool Equals(const std::string& a, const std::string& b)
|
2017-01-05 17:51:31 +00:00
|
|
|
{
|
|
|
|
|
return String::Equals(a.c_str(), b.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
bool Equals(const utf8* a, const utf8* b)
|
2016-01-29 19:39:31 +00:00
|
|
|
{
|
|
|
|
|
bool ignoreCase = false;
|
2017-06-12 18:00:15 +01:00
|
|
|
#ifdef _WIN32
|
2016-01-29 19:39:31 +00:00
|
|
|
ignoreCase = true;
|
|
|
|
|
#endif
|
|
|
|
|
return String::Equals(a, b, ignoreCase);
|
|
|
|
|
}
|
2017-06-30 20:38:51 +01:00
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
std::string ResolveCasing(const std::string& path)
|
2017-06-30 20:38:51 +01:00
|
|
|
{
|
|
|
|
|
std::string result;
|
|
|
|
|
if (File::Exists(path))
|
|
|
|
|
{
|
|
|
|
|
// Windows is case insensitive so it will exist and that is all that matters
|
|
|
|
|
// for now. We can properly resolve the casing if we ever need to.
|
|
|
|
|
result = path;
|
|
|
|
|
}
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::string fileName = Path::GetFileName(path);
|
|
|
|
|
std::string directory = Path::GetDirectory(path);
|
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
struct dirent** files;
|
2017-06-30 20:38:51 +01:00
|
|
|
auto count = scandir(directory.c_str(), &files, nullptr, alphasort);
|
|
|
|
|
if (count != -1)
|
|
|
|
|
{
|
|
|
|
|
// Find a file which matches by name (case insensitive)
|
2018-06-20 17:28:51 +02:00
|
|
|
for (int32_t i = 0; i < count; i++)
|
2017-06-30 20:38:51 +01:00
|
|
|
{
|
|
|
|
|
if (String::Equals(files[i]->d_name, fileName.c_str(), true))
|
|
|
|
|
{
|
|
|
|
|
result = Path::Combine(directory, std::string(files[i]->d_name));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Free memory
|
2018-06-20 17:28:51 +02:00
|
|
|
for (int32_t i = 0; i < count; i++)
|
2017-06-30 20:38:51 +01:00
|
|
|
{
|
|
|
|
|
free(files[i]);
|
|
|
|
|
}
|
|
|
|
|
free(files);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2018-05-04 22:40:09 +02:00
|
|
|
} // namespace Path
|