diff --git a/src/Nazara/Core/File.cpp b/src/Nazara/Core/File.cpp index 980e69377..db25a64bc 100644 --- a/src/Nazara/Core/File.cpp +++ b/src/Nazara/Core/File.cpp @@ -6,9 +6,11 @@ #include #include #include +#include #include #include #include +#include #include #if defined(NAZARA_PLATFORM_WINDOWS) @@ -295,15 +297,16 @@ bool NzFile::Open(unsigned long openMode) if (m_openMode == 0) return false; - m_impl = new NzFileImpl(this); - if (!m_impl->Open(m_filePath, m_openMode)) + std::unique_ptr impl(new NzFileImpl(this)); + if (!impl->Open(m_filePath, m_openMode)) { - delete m_impl; - m_impl = nullptr; - + NzErrorFlags flags(nzErrorFlag_Silent); // Silencieux par défaut + NazaraError("Failed to open \"" + m_filePath + "\": " + NzError::GetLastSystemError()); return false; } + m_impl = impl.release(); + if (m_openMode & Text) m_streamOptions |= nzStreamOption_Text; @@ -356,17 +359,17 @@ bool NzFile::SetFile(const NzString& filePath) if (filePath.IsEmpty()) return false; - NzFileImpl* impl = new NzFileImpl(this); + std::unique_ptr impl(new NzFileImpl(this)); if (!impl->Open(filePath, m_openMode)) { - delete impl; + NazaraError("Failed to open new file; " + NzError::GetLastSystemError()); return false; } m_impl->Close(); delete m_impl; - m_impl = impl; + m_impl = impl.release(); } m_filePath = AbsolutePath(filePath); @@ -382,18 +385,17 @@ bool NzFile::SetOpenMode(unsigned int openMode) if (IsOpen()) { - NzFileImpl* impl = new NzFileImpl(this); + std::unique_ptr impl(new NzFileImpl(this)); if (!impl->Open(m_filePath, openMode)) { - delete impl; - + NazaraError("Failed to open file with new mode: " + NzError::GetLastSystemError()); return false; } m_impl->Close(); delete m_impl; - m_impl = impl; + m_impl = impl.release(); if (m_openMode & Text) m_streamOptions |= nzStreamOption_Text; @@ -457,15 +459,13 @@ std::size_t NzFile::Write(const void* buffer, std::size_t typeSize, unsigned int std::size_t bytesWritten; if (m_endianness != nzEndianness_Unknown && m_endianness != NzGetPlatformEndianness() && typeSize != 1) { - char* buf = new char[count*typeSize]; - std::memcpy(buf, buffer, count*typeSize); + std::unique_ptr buf(new char[count*typeSize]); + std::memcpy(buf.get(), buffer, count*typeSize); for (unsigned int i = 0; i < count; ++i) NzByteSwap(&buf[i*typeSize], typeSize); - bytesWritten = m_impl->Write(buf, count*typeSize); - - delete[] buf; + bytesWritten = m_impl->Write(buf.get(), count*typeSize); } else bytesWritten = m_impl->Write(buffer, count*typeSize); @@ -560,21 +560,13 @@ NzString NzFile::AbsolutePath(const NzString& filePath) pathLen += sep.size()-1; - ///FIXME: Le destructeur de NzStringStream provoque un bug lors de la libération de son vector - - //NzStringStream stream(base); - NzString stream; - stream.Reserve(pathLen); - stream.Append(base); + NzStringStream stream(base); for (unsigned int i = 0; i < sep.size(); ++i) { - stream.Append(sep[i]); if (i != sep.size()-1) - stream.Append(NAZARA_DIRECTORY_SEPARATOR); - /*if (i != sep.size()-1) stream << sep[i] << NAZARA_DIRECTORY_SEPARATOR; else - stream << sep[i];*/ + stream << sep[i]; } return stream;