Use a smart pointer to manage std::FILE pointer in FileInputStream

This commit is contained in:
kimci86 2021-12-21 21:50:16 +01:00 committed by Vittorio Romeo
parent cd1cc62f6d
commit d0c63f46fc
2 changed files with 23 additions and 20 deletions

View File

@ -137,7 +137,16 @@ private:
#ifdef SFML_SYSTEM_ANDROID #ifdef SFML_SYSTEM_ANDROID
std::unique_ptr<priv::ResourceStream> m_file; std::unique_ptr<priv::ResourceStream> m_file;
#else #else
std::FILE* m_file; //!< stdio file stream ////////////////////////////////////////////////////////////
/// \brief Deleter for stdio file stream that closes the file stream
///
////////////////////////////////////////////////////////////
struct FileCloser
{
void operator()(std::FILE* file);
};
std::unique_ptr<std::FILE, FileCloser> m_file; //!< stdio file stream
#endif #endif
}; };

View File

@ -35,21 +35,19 @@
namespace sf namespace sf
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
FileInputStream::FileInputStream() #ifndef SFML_SYSTEM_ANDROID
: m_file(nullptr) void FileInputStream::FileCloser::operator()(std::FILE* file)
{ {
std::fclose(file);
} }
#endif
////////////////////////////////////////////////////////////
FileInputStream::FileInputStream() = default;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
FileInputStream::~FileInputStream() FileInputStream::~FileInputStream() = default;
{
#ifndef SFML_SYSTEM_ANDROID
if (m_file)
std::fclose(m_file);
#endif
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -59,11 +57,7 @@ bool FileInputStream::open(const std::string& filename)
m_file = std::make_unique<priv::ResourceStream>(filename); m_file = std::make_unique<priv::ResourceStream>(filename);
return m_file->tell() != -1; return m_file->tell() != -1;
#else #else
if (m_file) m_file.reset(std::fopen(filename.c_str(), "rb"));
std::fclose(m_file);
m_file = std::fopen(filename.c_str(), "rb");
return m_file != nullptr; return m_file != nullptr;
#endif #endif
} }
@ -76,7 +70,7 @@ Int64 FileInputStream::read(void* data, Int64 size)
return m_file->read(data, size); return m_file->read(data, size);
#else #else
if (m_file) if (m_file)
return static_cast<Int64>(std::fread(data, 1, static_cast<std::size_t>(size), m_file)); return static_cast<Int64>(std::fread(data, 1, static_cast<std::size_t>(size), m_file.get()));
else else
return -1; return -1;
#endif #endif
@ -91,7 +85,7 @@ Int64 FileInputStream::seek(Int64 position)
#else #else
if (m_file) if (m_file)
{ {
if (std::fseek(m_file, static_cast<long>(position), SEEK_SET)) if (std::fseek(m_file.get(), static_cast<long>(position), SEEK_SET))
return -1; return -1;
return tell(); return tell();
@ -111,7 +105,7 @@ Int64 FileInputStream::tell()
return m_file->tell(); return m_file->tell();
#else #else
if (m_file) if (m_file)
return std::ftell(m_file); return std::ftell(m_file.get());
else else
return -1; return -1;
#endif #endif
@ -127,7 +121,7 @@ Int64 FileInputStream::getSize()
if (m_file) if (m_file)
{ {
Int64 position = tell(); Int64 position = tell();
std::fseek(m_file, 0, SEEK_END); std::fseek(m_file.get(), 0, SEEK_END);
Int64 size = tell(); Int64 size = tell();
if (seek(position) == -1) if (seek(position) == -1)