Core/File: Add SetSize method (currently missing Posix implementation!)

Former-commit-id: c9ffb545485d90940d620e94fd25a7256d62ab58
This commit is contained in:
Lynix
2016-04-29 14:20:22 +02:00
parent e5a7bd52dd
commit 019c1f4a36
4 changed files with 47 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Win32/FileImpl.hpp>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Win32/Time.hpp>
#include <memory>
@@ -158,6 +159,31 @@ namespace Nz
return SetFilePointerEx(m_handle, distance, nullptr, moveMethod) != 0;
}
bool FileImpl::SetSize(UInt64 size)
{
UInt64 cursorPos = GetCursorPos();
CallOnExit resetCursor([this, cursorPos] ()
{
if (!SetCursorPos(CursorPosition_AtBegin, cursorPos))
NazaraWarning("Failed to reset cursor position to previous position: " + Error::GetLastSystemError());
});
if (!SetCursorPos(CursorPosition_AtBegin, size))
{
NazaraError("Failed to set file size: failed to move cursor position: " + Error::GetLastSystemError());
return false;
}
if (!SetEndOfFile(m_handle))
{
NazaraError("Failed to set file size: " + Error::GetLastSystemError());
return false;
}
return true;
}
std::size_t FileImpl::Write(const void* buffer, std::size_t size)
{
DWORD written = 0;

View File

@@ -32,6 +32,7 @@ namespace Nz
bool Open(const String& filePath, UInt32 mode);
std::size_t Read(void* buffer, std::size_t size);
bool SetCursorPos(CursorPosition pos, Int64 offset);
bool SetSize(UInt64 size);
std::size_t Write(const void* buffer, std::size_t size);
FileImpl& operator=(const FileImpl&) = delete;