Extract ResolveCasing to Platform

This commit is contained in:
Tulio Leao 2021-03-26 00:07:53 -03:00
parent c7ae064a4c
commit 62fc4c8034
4 changed files with 56 additions and 42 deletions

View File

@ -7,10 +7,7 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include <algorithm>
#ifndef _WIN32
# include <dirent.h>
#endif
#include "Path.hpp"
#include "../localisation/Language.h"
#include "../platform/Platform2.h"
@ -19,9 +16,9 @@
#include "File.h"
#include "FileSystem.hpp"
#include "Memory.hpp"
#include "Path.hpp"
#include "String.hpp"
#include <algorithm>
#include <iterator>
namespace Path
@ -217,42 +214,6 @@ namespace Path
std::string ResolveCasing(const std::string& path)
{
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);
struct dirent** files;
auto count = scandir(directory.c_str(), &files, nullptr, alphasort);
if (count != -1)
{
// Find a file which matches by name (case insensitive)
for (int32_t i = 0; i < count; i++)
{
if (String::Equals(files[i]->d_name, fileName.c_str(), true))
{
result = Path::Combine(directory, std::string(files[i]->d_name));
break;
}
}
// Free memory
for (int32_t i = 0; i < count; i++)
{
free(files[i]);
}
free(files);
}
}
#endif
return result;
return Platform::ResolveCasing(path, File::Exists(path));
}
} // namespace Path

View File

@ -10,6 +10,7 @@
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__)
# include "../core/Memory.hpp"
# include "../core/Path.hpp"
# include "../core/String.hpp"
# include "Platform2.h"
# include "platform.h"
@ -18,6 +19,7 @@
# include <cstdlib>
# include <cstring>
# include <ctime>
# include <dirent.h>
# include <pwd.h>
# include <sys/stat.h>
@ -199,6 +201,45 @@ namespace Platform
return buffer;
}
}
std::string ResolveCasing(const std::string& path, bool fileExists)
{
std::string result;
if (fileExists)
{
// 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;
}
else
{
std::string fileName = Path::GetFileName(path);
std::string directory = Path::GetDirectory(path);
struct dirent** files;
auto count = scandir(directory.c_str(), &files, nullptr, alphasort);
if (count != -1)
{
// Find a file which matches by name (case insensitive)
for (int32_t i = 0; i < count; i++)
{
if (String::Equals(files[i]->d_name, fileName.c_str(), true))
{
result = Path::Combine(directory, std::string(files[i]->d_name));
break;
}
}
// Free memory
for (int32_t i = 0; i < count; i++)
{
free(files[i]);
}
free(files);
}
}
return result;
}
} // namespace Platform
#endif

View File

@ -582,6 +582,17 @@ namespace Platform
}
}
std::string ResolveCasing(const std::string& path, bool fileExists)
{
std::string result;
if (fileExists)
{
// 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;
}
return result;
}
} // namespace Platform
#endif

View File

@ -39,6 +39,7 @@ namespace Platform
bool IsPathSeparator(char c);
utf8* GetAbsolutePath(utf8* buffer, size_t bufferSize, const utf8* relativePath);
uint64_t GetLastModified(const std::string& path);
std::string ResolveCasing(const std::string& path, bool fileExists);
rct2_time GetTimeLocal();
rct2_date GetDateLocal();
bool FindApp(const std::string& app, std::string* output);