Core: Add OutputStream class

Former-commit-id: a6b8f642e263833c75a338cc23266c94edbced6f
This commit is contained in:
Lynix
2015-11-17 12:47:13 +01:00
parent 9f7468d940
commit fb920f0016
6 changed files with 88 additions and 37 deletions

View File

@@ -416,33 +416,16 @@ namespace Nz
return true;
}
bool File::Write(const ByteArray& byteArray)
std::size_t File::Write(const void* buffer, std::size_t size)
{
ByteArray::size_type size = byteArray.GetSize();
return Write(byteArray.GetConstBuffer(), 1, size) == size;
}
NazaraAssert(IsOpen(), "File is not open");
NazaraAssert(m_openMode & OpenMode_ReadWrite || m_openMode & OpenMode_WriteOnly, "File not opened with write access");
NazaraLock(m_mutex)
bool File::Write(const String& string)
{
String temp(string);
if (!buffer || size == 0)
return 0;
if (m_streamOptions & StreamOption_Text)
{
#if defined(NAZARA_PLATFORM_WINDOWS)
temp.Replace("\n", "\r\n");
#elif defined(NAZARA_PLATFORM_LINUX)
// Rien à faire
#elif defined(NAZARA_PLATFORM_MACOS)
temp.Replace('\n', '\r');
#else
#error OS not handled
#endif
}
unsigned int size = temp.GetSize();
std::size_t bytesWritten = Write(temp.GetBuffer(), sizeof(char), size);
return bytesWritten == size*sizeof(char);
return m_impl->Write(buffer, size);
}
std::size_t File::Write(const void* buffer, std::size_t typeSize, unsigned int count)

View File

@@ -0,0 +1,40 @@
// 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;
}
}

View File

@@ -4205,7 +4205,6 @@ namespace Nz
const std::shared_ptr<String::SharedString>& String::GetEmptyString()
{
static auto emptyString = std::make_shared<SharedString>();
return emptyString;
}