Add portable abstraction for opening files from Unicode filepaths

This commit is contained in:
Chris Thrasher 2025-02-07 08:48:45 -07:00
parent 0eb92fc27d
commit 41dc238981
3 changed files with 16 additions and 5 deletions

View File

@ -27,6 +27,7 @@
////////////////////////////////////////////////////////////
#include <SFML/System/Exception.hpp>
#include <SFML/System/FileInputStream.hpp>
#include <SFML/System/Utils.hpp>
#ifdef SFML_SYSTEM_ANDROID
#include <SFML/System/Android/Activity.hpp>
#include <SFML/System/Android/ResourceStream.hpp>
@ -78,11 +79,7 @@ bool FileInputStream::open(const std::filesystem::path& filename)
return m_androidFile->tell().has_value();
}
#endif
#ifdef SFML_SYSTEM_WINDOWS
m_file.reset(_wfopen(filename.c_str(), L"rb"));
#else
m_file.reset(std::fopen(filename.c_str(), "rb"));
#endif
m_file.reset(openFile(filename, "rb"));
return m_file != nullptr;
}

View File

@ -51,4 +51,14 @@ std::string formatDebugPathInfo(const std::filesystem::path& path)
return oss.str();
}
std::FILE* openFile(const std::filesystem::path& filename, std::string_view mode)
{
#ifdef SFML_SYSTEM_WINDOWS
const std::wstring wmode(mode.begin(), mode.end());
return _wfopen(filename.c_str(), wmode.data());
#else
return std::fopen(filename.c_str(), mode.data());
#endif
}
} // namespace sf

View File

@ -31,8 +31,10 @@
#include <filesystem>
#include <string>
#include <string_view>
#include <cstddef>
#include <cstdio>
namespace sf
@ -51,4 +53,6 @@ template <typename IntegerType, typename... Bytes>
std::size_t index = 0;
return ((integer |= static_cast<IntegerType>(static_cast<IntegerType>(byte) << 8 * index++)), ...);
}
[[nodiscard]] SFML_SYSTEM_API std::FILE* openFile(const std::filesystem::path& filename, std::string_view mode);
} // namespace sf