diff --git a/include/Nazara/Core/File.hpp b/include/Nazara/Core/File.hpp index 8f8f6bc94..a01433be2 100644 --- a/include/Nazara/Core/File.hpp +++ b/include/Nazara/Core/File.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #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); diff --git a/include/Nazara/Core/InputStream.hpp b/include/Nazara/Core/InputStream.hpp index 1aea60cd8..17539aaa1 100644 --- a/include/Nazara/Core/InputStream.hpp +++ b/include/Nazara/Core/InputStream.hpp @@ -12,7 +12,7 @@ namespace Nz { - class NAZARA_CORE_API InputStream : public Stream + class NAZARA_CORE_API InputStream : public virtual Stream { public: virtual ~InputStream(); diff --git a/include/Nazara/Core/OutputStream.hpp b/include/Nazara/Core/OutputStream.hpp new file mode 100644 index 000000000..70d7ef40c --- /dev/null +++ b/include/Nazara/Core/OutputStream.hpp @@ -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 +#include + +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 diff --git a/src/Nazara/Core/File.cpp b/src/Nazara/Core/File.cpp index 9c83b708d..977b0353f 100644 --- a/src/Nazara/Core/File.cpp +++ b/src/Nazara/Core/File.cpp @@ -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) diff --git a/src/Nazara/Core/OutputStream.cpp b/src/Nazara/Core/OutputStream.cpp new file mode 100644 index 000000000..b9bc8a420 --- /dev/null +++ b/src/Nazara/Core/OutputStream.cpp @@ -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 +#include +#include +#include +#include +#include + +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; + } +} diff --git a/src/Nazara/Core/String.cpp b/src/Nazara/Core/String.cpp index cfddcf49d..d5230577a 100644 --- a/src/Nazara/Core/String.cpp +++ b/src/Nazara/Core/String.cpp @@ -4205,7 +4205,6 @@ namespace Nz const std::shared_ptr& String::GetEmptyString() { static auto emptyString = std::make_shared(); - return emptyString; }