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

@ -12,6 +12,7 @@
#include <Nazara/Core/Directory.hpp>
#include <Nazara/Core/Endianness.hpp>
#include <Nazara/Core/InputStream.hpp>
#include <Nazara/Core/OutputStream.hpp>
#include <Nazara/Core/String.hpp>
#if NAZARA_CORE_THREADSAFE && NAZARA_THREADSAFETY_FILE
@ -26,7 +27,7 @@ namespace Nz
{
class FileImpl;
class NAZARA_CORE_API File : public InputStream
class NAZARA_CORE_API File : public InputStream, public OutputStream
{
public:
File();
@ -42,38 +43,38 @@ namespace Nz
bool Delete();
bool EndOfFile() const;
bool EndOfStream() const;
bool EndOfStream() const override;
bool Exists() const;
void Flush();
void Flush() override;
time_t GetCreationTime() const;
UInt64 GetCursorPos() const;
String GetDirectory() const;
UInt64 GetCursorPos() const override;
String GetDirectory() const override;
String GetFileName() const;
time_t GetLastAccessTime() const;
time_t GetLastWriteTime() const;
String GetPath() const;
UInt64 GetSize() const;
String GetPath() const override;
UInt64 GetSize() const override;
bool IsOpen() const;
bool Open(unsigned int openMode = OpenMode_Current);
bool Open(const String& filePath, unsigned int openMode = OpenMode_Current);
std::size_t Read(void* buffer, std::size_t size);
std::size_t Read(void* buffer, std::size_t size) override;
std::size_t Read(void* buffer, std::size_t typeSize, unsigned int count);
bool Rename(const String& newFilePath);
bool SetCursorPos(CursorPosition pos, Int64 offset = 0);
bool SetCursorPos(UInt64 offset);
bool SetCursorPos(UInt64 offset) override;
void SetEndianness(Endianness endianness);
bool SetFile(const String& filePath);
bool SetOpenMode(unsigned int openMode);
bool Write(const Nz::ByteArray& byteArray);
bool Write(const String& string);
using OutputStream::Write;
std::size_t Write(const void* buffer, std::size_t size) override;
std::size_t Write(const void* buffer, std::size_t typeSize, unsigned int count);
File& operator=(const String& filePath);

View File

@ -12,7 +12,7 @@
namespace Nz
{
class NAZARA_CORE_API InputStream : public Stream
class NAZARA_CORE_API InputStream : public virtual Stream
{
public:
virtual ~InputStream();

View File

@ -0,0 +1,28 @@
// 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
#pragma once
#ifndef NAZARA_OUTPUTSTREAM_HPP
#define NAZARA_OUTPUTSTREAM_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Stream.hpp>
namespace Nz
{
class NAZARA_CORE_API OutputStream : public virtual Stream
{
public:
virtual ~OutputStream();
virtual void Flush() = 0;
bool Write(const ByteArray& byteArray);
bool Write(const String& string);
virtual std::size_t Write(const void* buffer, std::size_t size) = 0;
};
}
#endif // NAZARA_OUTPUTSTREAM_HPP

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;
}