Improved File class source code

Made use of smart pointers to be exception-safe
Made use of StringStream (again)


Former-commit-id: 9b139468e5045abf7ee4832c40bdec25a2a9af50
This commit is contained in:
Lynix 2014-03-06 09:53:03 +01:00
parent 42c10268d2
commit 67951074b1
1 changed files with 19 additions and 27 deletions

View File

@ -6,9 +6,11 @@
#include <Nazara/Core/AbstractHash.hpp>
#include <Nazara/Core/Config.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Core/Hash.hpp>
#include <Nazara/Core/StringStream.hpp>
#include <cstring>
#include <memory>
#include <utility>
#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<NzFileImpl> 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<NzFileImpl> 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<NzFileImpl> 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<char[]> 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;