Ensure consistent behavior between Android and other OSes

Android never checked if m_file was null and thus would invoke UB
upon calling certain functions whereas all other OSes would return
a defined value.
This commit is contained in:
Chris Thrasher 2023-11-27 13:21:52 -07:00
parent 9b751899eb
commit fe0785769e

View File

@ -80,13 +80,12 @@ bool FileInputStream::open(const std::filesystem::path& filename)
////////////////////////////////////////////////////////////
std::int64_t FileInputStream::read(void* data, std::int64_t size)
{
if (!m_file)
return -1;
#ifdef SFML_SYSTEM_ANDROID
return m_file->read(data, size);
#else
if (m_file)
return static_cast<std::int64_t>(std::fread(data, 1, static_cast<std::size_t>(size), m_file.get()));
else
return -1;
return static_cast<std::int64_t>(std::fread(data, 1, static_cast<std::size_t>(size), m_file.get()));
#endif
}
@ -94,20 +93,15 @@ std::int64_t FileInputStream::read(void* data, std::int64_t size)
////////////////////////////////////////////////////////////
std::int64_t FileInputStream::seek(std::int64_t position)
{
if (!m_file)
return -1;
#ifdef SFML_SYSTEM_ANDROID
return m_file->seek(position);
#else
if (m_file)
{
if (std::fseek(m_file.get(), static_cast<long>(position), SEEK_SET))
return -1;
return tell();
}
else
{
if (std::fseek(m_file.get(), static_cast<long>(position), SEEK_SET))
return -1;
}
return tell();
#endif
}
@ -115,13 +109,12 @@ std::int64_t FileInputStream::seek(std::int64_t position)
////////////////////////////////////////////////////////////
std::int64_t FileInputStream::tell()
{
if (!m_file)
return -1;
#ifdef SFML_SYSTEM_ANDROID
return m_file->tell();
#else
if (m_file)
return std::ftell(m_file.get());
else
return -1;
return std::ftell(m_file.get());
#endif
}
@ -129,24 +122,19 @@ std::int64_t FileInputStream::tell()
////////////////////////////////////////////////////////////
std::int64_t FileInputStream::getSize()
{
if (!m_file)
return -1;
#ifdef SFML_SYSTEM_ANDROID
return m_file->getSize();
#else
if (m_file)
{
const std::int64_t position = tell();
std::fseek(m_file.get(), 0, SEEK_END);
const std::int64_t size = tell();
const std::int64_t position = tell();
std::fseek(m_file.get(), 0, SEEK_END);
const std::int64_t size = tell();
if (seek(position) == -1)
return -1;
return size;
}
else
{
if (seek(position) == -1)
return -1;
}
return size;
#endif
}