Core: Merge InputStream and OutputStream to Stream
Remove serialization support from Stream Former-commit-id: 7a761e4fcd07cab561f13e4709c4492ed18da88a
This commit is contained in:
@@ -50,8 +50,6 @@ namespace Nz
|
||||
|
||||
File::File(File&& file) noexcept :
|
||||
Stream(std::move(file)),
|
||||
InputStream(std::move(file)),
|
||||
OutputStream(std::move(file)),
|
||||
m_filePath(std::move(file.m_filePath)),
|
||||
m_impl(file.m_impl)
|
||||
{
|
||||
@@ -119,16 +117,6 @@ namespace Nz
|
||||
return Exists(m_filePath);
|
||||
}
|
||||
|
||||
void File::Flush()
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
NazaraAssert(IsOpen(), "File is not open");
|
||||
NazaraAssert(IsWritable(), "File not opened with write access");
|
||||
|
||||
m_impl->Flush();
|
||||
}
|
||||
|
||||
time_t File::GetCreationTime() const
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
@@ -194,29 +182,6 @@ namespace Nz
|
||||
return m_impl != nullptr;
|
||||
}
|
||||
|
||||
std::size_t File::Read(void* buffer, std::size_t size)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
NazaraAssert(IsOpen(), "File is not opened");
|
||||
NazaraAssert(IsReadable(), "File not opened with read access");
|
||||
|
||||
if (size == 0)
|
||||
return 0;
|
||||
|
||||
if (buffer)
|
||||
return m_impl->Read(buffer, size);
|
||||
else
|
||||
{
|
||||
// Si nous ne devons rien lire, nous avançons simplement
|
||||
UInt64 currentPos = m_impl->GetCursorPos();
|
||||
|
||||
m_impl->SetCursorPos(CursorPosition_AtCurrent, size);
|
||||
|
||||
return static_cast<std::size_t>(m_impl->GetCursorPos() - currentPos);
|
||||
}
|
||||
}
|
||||
|
||||
bool File::Rename(const String& newFilePath)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
@@ -321,21 +286,6 @@ namespace Nz
|
||||
return true;
|
||||
}
|
||||
|
||||
std::size_t File::Write(const void* buffer, std::size_t size)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
NazaraAssert(IsOpen(), "File is not opened");
|
||||
NazaraAssert(IsWritable(), "File not opened with write access");
|
||||
|
||||
if (size == 0)
|
||||
return 0;
|
||||
|
||||
NazaraAssert(buffer, "Invalid buffer");
|
||||
|
||||
return m_impl->Write(buffer, size);
|
||||
}
|
||||
|
||||
File& File::operator=(const String& filePath)
|
||||
{
|
||||
SetFile(filePath);
|
||||
@@ -549,6 +499,52 @@ namespace Nz
|
||||
return FileImpl::Rename(NormalizePath(sourcePath), NormalizePath(targetPath));
|
||||
}
|
||||
|
||||
void File::FlushStream()
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
NazaraAssert(IsOpen(), "File is not open");
|
||||
|
||||
m_impl->Flush();
|
||||
}
|
||||
|
||||
std::size_t File::ReadBlock(void* buffer, std::size_t size)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
NazaraAssert(IsOpen(), "File is not opened");
|
||||
|
||||
if (size == 0)
|
||||
return 0;
|
||||
|
||||
if (buffer)
|
||||
return m_impl->Read(buffer, size);
|
||||
else
|
||||
{
|
||||
// Si nous ne devons rien lire, nous avançons simplement
|
||||
UInt64 currentPos = m_impl->GetCursorPos();
|
||||
|
||||
m_impl->SetCursorPos(CursorPosition_AtCurrent, size);
|
||||
|
||||
return static_cast<std::size_t>(m_impl->GetCursorPos() - currentPos);
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t File::WriteBlock(const void* buffer, std::size_t size)
|
||||
{
|
||||
NazaraLock(m_mutex)
|
||||
|
||||
NazaraAssert(IsOpen(), "File is not opened");
|
||||
|
||||
if (size == 0)
|
||||
return 0;
|
||||
|
||||
NazaraAssert(buffer, "Invalid buffer");
|
||||
|
||||
return m_impl->Write(buffer, size);
|
||||
}
|
||||
|
||||
|
||||
NAZARA_CORE_API bool HashAppend(AbstractHash* hash, const File& originalFile)
|
||||
{
|
||||
File file(originalFile.GetPath());
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/InputStream.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <cstring>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
InputStream::~InputStream() = default;
|
||||
|
||||
String InputStream::ReadLine(unsigned int lineSize)
|
||||
{
|
||||
String line;
|
||||
if (lineSize == 0) // Taille maximale indéterminée
|
||||
{
|
||||
const unsigned int bufferSize = 64;
|
||||
|
||||
char buffer[bufferSize+1];
|
||||
buffer[bufferSize] = '\0';
|
||||
|
||||
unsigned int readSize;
|
||||
do
|
||||
{
|
||||
readSize = Read(buffer, bufferSize);
|
||||
|
||||
const char* ptr = std::strchr(buffer, '\n');
|
||||
if (ptr)
|
||||
{
|
||||
unsigned int pos = ptr-buffer;
|
||||
|
||||
if (m_streamOptions & StreamOption_Text && pos > 0 && buffer[pos-1] == '\r')
|
||||
line.Append(buffer, pos-1);
|
||||
else
|
||||
line.Append(buffer, pos);
|
||||
|
||||
if (!SetCursorPos(GetCursorPos() - readSize + pos + 1))
|
||||
NazaraWarning("Failed to reset cursos pos");
|
||||
|
||||
break;
|
||||
}
|
||||
else
|
||||
line.Append(buffer, readSize);
|
||||
}
|
||||
while (readSize == bufferSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
line.Set(lineSize, '\0');
|
||||
unsigned int readSize = Read(&line[0], lineSize);
|
||||
unsigned int pos = line.Find('\n');
|
||||
if (pos <= readSize) // Faux uniquement si le caractère n'est pas présent (npos étant le plus grand entier)
|
||||
{
|
||||
if (m_streamOptions & StreamOption_Text && pos > 0 && line[pos-1] == '\r')
|
||||
line.Resize(pos);
|
||||
else
|
||||
line.Resize(pos+1);
|
||||
|
||||
if (!SetCursorPos(GetCursorPos() - readSize + pos + 1))
|
||||
NazaraWarning("Failed to reset cursos pos");
|
||||
}
|
||||
else
|
||||
line.Resize(readSize);
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
}
|
||||
@@ -33,11 +33,6 @@ namespace Nz
|
||||
return m_pos >= m_buffer.size();
|
||||
}
|
||||
|
||||
void MemoryStream::Flush()
|
||||
{
|
||||
// Nothing to flush
|
||||
}
|
||||
|
||||
const ByteArray& MemoryStream::GetBuffer() const
|
||||
{
|
||||
return m_buffer;
|
||||
@@ -58,7 +53,19 @@ namespace Nz
|
||||
return m_buffer.size();
|
||||
}
|
||||
|
||||
std::size_t MemoryStream::Read(void* buffer, std::size_t size)
|
||||
bool MemoryStream::SetCursorPos(UInt64 offset)
|
||||
{
|
||||
m_pos = std::min<UInt64>(offset, m_buffer.size());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void MemoryStream::FlushStream()
|
||||
{
|
||||
// Nothing to flush
|
||||
}
|
||||
|
||||
std::size_t MemoryStream::ReadBlock(void* buffer, std::size_t size)
|
||||
{
|
||||
std::size_t readSize = std::min<std::size_t>(size, static_cast<std::size_t>(m_buffer.size() - m_pos));
|
||||
|
||||
@@ -69,14 +76,7 @@ namespace Nz
|
||||
return readSize;
|
||||
}
|
||||
|
||||
bool MemoryStream::SetCursorPos(UInt64 offset)
|
||||
{
|
||||
m_pos = std::min<UInt64>(offset, m_buffer.size());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::size_t MemoryStream::Write(const void* buffer, std::size_t size)
|
||||
std::size_t MemoryStream::WriteBlock(const void* buffer, std::size_t size)
|
||||
{
|
||||
std::size_t endPos = static_cast<std::size_t>(m_pos + size);
|
||||
if (endPos > m_buffer.size())
|
||||
|
||||
@@ -32,7 +32,19 @@ namespace Nz
|
||||
return m_size;
|
||||
}
|
||||
|
||||
std::size_t MemoryView::Read(void* buffer, std::size_t size)
|
||||
bool MemoryView::SetCursorPos(UInt64 offset)
|
||||
{
|
||||
m_pos = std::min(offset, m_size);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void MemoryView::FlushStream()
|
||||
{
|
||||
NazaraInternalError("FlushStream has been called on a MemoryView");
|
||||
}
|
||||
|
||||
std::size_t MemoryView::ReadBlock(void* buffer, std::size_t size)
|
||||
{
|
||||
std::size_t readSize = std::min<std::size_t>(size, static_cast<std::size_t>(m_size - m_pos));
|
||||
|
||||
@@ -43,10 +55,9 @@ namespace Nz
|
||||
return readSize;
|
||||
}
|
||||
|
||||
bool MemoryView::SetCursorPos(UInt64 offset)
|
||||
std::size_t MemoryView::WriteBlock(const void* buffer, std::size_t size)
|
||||
{
|
||||
m_pos = std::min(offset, m_size);
|
||||
|
||||
return true;
|
||||
NazaraInternalError("WriteBlock has been called on a MemoryView");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/OutputStream.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <cstring>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
OutputStream::~OutputStream() = default;
|
||||
|
||||
bool OutputStream::Write(const ByteArray& byteArray)
|
||||
{
|
||||
ByteArray::size_type size = byteArray.GetSize();
|
||||
return Write(byteArray.GetConstBuffer(), size) == size;
|
||||
}
|
||||
|
||||
bool OutputStream::Write(const String& string)
|
||||
{
|
||||
String temp(string);
|
||||
|
||||
if (m_streamOptions & StreamOption_Text)
|
||||
{
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
temp.Replace("\n", "\r\n");
|
||||
#elif defined(NAZARA_PLATFORM_LINUX)
|
||||
// Nothing to do
|
||||
#elif defined(NAZARA_PLATFORM_MACOS)
|
||||
temp.Replace('\n', '\r');
|
||||
#endif
|
||||
}
|
||||
|
||||
std::size_t size = temp.GetSize();
|
||||
return Write(temp.GetConstBuffer(), size) == size;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/Stream.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
@@ -19,4 +21,86 @@ namespace Nz
|
||||
{
|
||||
return String();
|
||||
}
|
||||
|
||||
String Stream::ReadLine(unsigned int lineSize)
|
||||
{
|
||||
String line;
|
||||
if (lineSize == 0) // Taille maximale indéterminée
|
||||
{
|
||||
const unsigned int bufferSize = 64;
|
||||
|
||||
char buffer[bufferSize + 1];
|
||||
buffer[bufferSize] = '\0';
|
||||
|
||||
unsigned int readSize;
|
||||
do
|
||||
{
|
||||
readSize = Read(buffer, bufferSize);
|
||||
|
||||
const char* ptr = std::strchr(buffer, '\n');
|
||||
if (ptr)
|
||||
{
|
||||
unsigned int pos = ptr - buffer;
|
||||
|
||||
if (m_streamOptions & StreamOption_Text && pos > 0 && buffer[pos - 1] == '\r')
|
||||
line.Append(buffer, pos - 1);
|
||||
else
|
||||
line.Append(buffer, pos);
|
||||
|
||||
if (!SetCursorPos(GetCursorPos() - readSize + pos + 1))
|
||||
NazaraWarning("Failed to reset cursos pos");
|
||||
|
||||
break;
|
||||
}
|
||||
else
|
||||
line.Append(buffer, readSize);
|
||||
}
|
||||
while (readSize == bufferSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
line.Set(lineSize, '\0');
|
||||
unsigned int readSize = Read(&line[0], lineSize);
|
||||
unsigned int pos = line.Find('\n');
|
||||
if (pos <= readSize) // Faux uniquement si le caractère n'est pas présent (npos étant le plus grand entier)
|
||||
{
|
||||
if (m_streamOptions & StreamOption_Text && pos > 0 && line[pos - 1] == '\r')
|
||||
line.Resize(pos);
|
||||
else
|
||||
line.Resize(pos + 1);
|
||||
|
||||
if (!SetCursorPos(GetCursorPos() - readSize + pos + 1))
|
||||
NazaraWarning("Failed to reset cursos pos");
|
||||
}
|
||||
else
|
||||
line.Resize(readSize);
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
bool Stream::Write(const ByteArray& byteArray)
|
||||
{
|
||||
ByteArray::size_type size = byteArray.GetSize();
|
||||
return Write(byteArray.GetConstBuffer(), size) == size;
|
||||
}
|
||||
|
||||
bool Stream::Write(const String& string)
|
||||
{
|
||||
String temp(string);
|
||||
|
||||
if (m_streamOptions & StreamOption_Text)
|
||||
{
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
temp.Replace("\n", "\r\n");
|
||||
#elif defined(NAZARA_PLATFORM_LINUX)
|
||||
// Nothing to do
|
||||
#elif defined(NAZARA_PLATFORM_MACOS)
|
||||
temp.Replace('\n', '\r');
|
||||
#endif
|
||||
}
|
||||
|
||||
std::size_t size = temp.GetSize();
|
||||
return Write(temp.GetConstBuffer(), size) == size;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user