diff --git a/include/SFML/System/FileInputStream.hpp b/include/SFML/System/FileInputStream.hpp index 806665ab..fb3faf74 100644 --- a/include/SFML/System/FileInputStream.hpp +++ b/include/SFML/System/FileInputStream.hpp @@ -137,7 +137,16 @@ private: #ifdef SFML_SYSTEM_ANDROID std::unique_ptr m_file; #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 m_file; //!< stdio file stream #endif }; diff --git a/src/SFML/System/FileInputStream.cpp b/src/SFML/System/FileInputStream.cpp index 73316f35..46c1a6c4 100644 --- a/src/SFML/System/FileInputStream.cpp +++ b/src/SFML/System/FileInputStream.cpp @@ -35,21 +35,19 @@ namespace sf { //////////////////////////////////////////////////////////// -FileInputStream::FileInputStream() -: m_file(nullptr) +#ifndef SFML_SYSTEM_ANDROID +void FileInputStream::FileCloser::operator()(std::FILE* file) { - + std::fclose(file); } +#endif + +//////////////////////////////////////////////////////////// +FileInputStream::FileInputStream() = default; //////////////////////////////////////////////////////////// -FileInputStream::~FileInputStream() -{ -#ifndef SFML_SYSTEM_ANDROID - if (m_file) - std::fclose(m_file); -#endif -} +FileInputStream::~FileInputStream() = default; //////////////////////////////////////////////////////////// @@ -59,11 +57,7 @@ bool FileInputStream::open(const std::string& filename) m_file = std::make_unique(filename); return m_file->tell() != -1; #else - if (m_file) - std::fclose(m_file); - - m_file = std::fopen(filename.c_str(), "rb"); - + m_file.reset(std::fopen(filename.c_str(), "rb")); return m_file != nullptr; #endif } @@ -76,7 +70,7 @@ Int64 FileInputStream::read(void* data, Int64 size) return m_file->read(data, size); #else if (m_file) - return static_cast(std::fread(data, 1, static_cast(size), m_file)); + return static_cast(std::fread(data, 1, static_cast(size), m_file.get())); else return -1; #endif @@ -91,7 +85,7 @@ Int64 FileInputStream::seek(Int64 position) #else if (m_file) { - if (std::fseek(m_file, static_cast(position), SEEK_SET)) + if (std::fseek(m_file.get(), static_cast(position), SEEK_SET)) return -1; return tell(); @@ -111,7 +105,7 @@ Int64 FileInputStream::tell() return m_file->tell(); #else if (m_file) - return std::ftell(m_file); + return std::ftell(m_file.get()); else return -1; #endif @@ -127,7 +121,7 @@ Int64 FileInputStream::getSize() if (m_file) { Int64 position = tell(); - std::fseek(m_file, 0, SEEK_END); + std::fseek(m_file.get(), 0, SEEK_END); Int64 size = tell(); if (seek(position) == -1)