From b5210850b61ccbae92242ab0c70dbeb173f415e7 Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Sun, 2 Sep 2012 18:42:03 +0100 Subject: [PATCH] [Add] Implementing a filesystem for LibD. --- Bin/LibD.pro | 3 + src/System/ConvertType.h | 55 ++++++++++ src/System/Filesystem/InputStream.cpp | 150 ++++++++++++++++++++++++++ src/System/Filesystem/InputStream.h | 90 ++++++++++++++++ 4 files changed, 298 insertions(+) create mode 100644 src/System/ConvertType.h create mode 100644 src/System/Filesystem/InputStream.cpp create mode 100644 src/System/Filesystem/InputStream.h diff --git a/Bin/LibD.pro b/Bin/LibD.pro index 9291c3d..a354021 100644 --- a/Bin/LibD.pro +++ b/Bin/LibD.pro @@ -37,6 +37,8 @@ HEADERS += ../src/Actor/Player.h \ ../src/Sprite/Sprite.h \ ../src/System/Debug.h \ ../src/System/ResourceManager.h \ + ../src/System/ConvertType.h \ + ../src/System/Filesystem/InputStream.h \ ../src/Texture/Texture.h \ ../src/Sound/Music.h \ ../src/TMXParser/base64.h \ @@ -87,6 +89,7 @@ SOURCES += ../src/Actor/Player.cpp \ ../src/Math/FPS.cpp \ ../src/Sprite/Sprite.cpp \ ../src/System/Debug.cpp \ + ../src/System/Filesystem/InputStream.cpp \ ../src/Texture/Texture.cpp \ ../src/Sound/Music.cpp \ ../src/Actor/NPC.cpp \ diff --git a/src/System/ConvertType.h b/src/System/ConvertType.h new file mode 100644 index 0000000..ca93ad9 --- /dev/null +++ b/src/System/ConvertType.h @@ -0,0 +1,55 @@ +#pragma once + +#ifndef INCLUDED_CASSERT +#define INCLUSED_CASSERT +#include +#endif + +namespace saracraft { +namespace filesystem { + +template +struct ConvertBase { + enum { charCount = sizeof(T) }; + union Values { + unsigned char c[charCount]; + T t; + }; + + Values value; + + int GetSize(void) const { return charCount; } +}; + +template +struct ConvertFrom: private ConvertBase { + explicit ConvertFrom(const T& t) { + this->value.t = t; + } + using ConvertBase::GetSize; + + unsigned char GetByte(int index) const { + assert((index >= 0) && (index < GetSize())); + return this->value.c[index]; + } +}; + +template +struct ConvertTo: private ConvertBase { + using ConvertBase::GetSize; + + void SetByte(int index, unsigned char c) { + assert((index >= 0) && (index < GetSize())); + this->value.c[index] = c; + } + + const T& GetValue(void) const { + return this->value.t; + } +}; + +typedef unsigned short uint16_t; + +} // Namespaces.... +} + diff --git a/src/System/Filesystem/InputStream.cpp b/src/System/Filesystem/InputStream.cpp new file mode 100644 index 0000000..12b16fd --- /dev/null +++ b/src/System/Filesystem/InputStream.cpp @@ -0,0 +1,150 @@ +#include + +#include "../ConvertType.h" +#include +#include "InputStream.h" +BOOST_STATIC_ASSERT(sizeof(saracraft::filesystem::uint16_t) * CHAR_BIT == 16); +BOOST_STATIC_ASSERT(CHAR_BIT == 8); + +namespace saracraft { +namespace filesystem { +namespace { + + template + void ReadFromStream(IInputStreamBuffer& buffer, T& value) { + ConvertTo converter; + for(int i = 0; i < converter.GetSize(); ++i) + converter.SetByte(i, buffer.PopByte()); + + value = converter.GetValue(); + } + +} // End of unamed namespace. + +InputStream::InputStream(void) { + +} + +InputStream::~InputStream(void) { + +} + +void InputStream::SetBuffer(boost::shared_ptr streamBuffer_) { + assert(streamBuffer_); + _streamBuffer = streamBuffer_; +} + +bool InputStream::IsEof(void) const { + assert(_streamBuffer); + return _streamBuffer->IsEof(); +} + +int InputStream::GetSize(void) const { + assert(_streamBuffer); + return _streamBuffer->GetSize(); +} + +InputStream& InputStream::Read(std::string& value) { + assert(_streamBuffer); + + uint16_t stringSize = 0; + this->Read(stringSize); + + value.resize(stringSize); + for(int i = 0; i < stringSize; ++i) + value[i] = _streamBuffer->PopByte(); + + return *this; +} + +InputStream& InputStream::Read(bool& value) { + assert(_streamBuffer); + value = _streamBuffer->PopByte() != 0; + return *this; +} + +InputStream& InputStream::Read(unsigned char& value) { + assert(_streamBuffer); + + value = _streamBuffer->PopByte(); + return *this; +} + +InputStream& InputStream::Read(char& value) { + assert(_streamBuffer); + + value = _streamBuffer->PopByte(); + return *this; +} + +InputStream& InputStream::Read(signed char& value) { + assert(_streamBuffer); + + value = _streamBuffer->PopByte(); + return *this; +} + +InputStream& InputStream::Read(unsigned short& value) { + assert(_streamBuffer); + + ReadFromStream(*_streamBuffer, value); + return *this; +} + +InputStream& InputStream::Read(signed short& value) { + assert(_streamBuffer); + + ReadFromStream(*_streamBuffer, value); + return *this; +} + +InputStream& InputStream::Read(unsigned int& value) { + assert(_streamBuffer); + + ReadFromStream(*_streamBuffer, value); + return * this; +} + +InputStream& InputStream::Read(signed int& value) { + assert(_streamBuffer); + + ReadFromStream(*_streamBuffer, value); + return *this; +} + +InputStream& InputStream::Read(float& value) { + assert(_streamBuffer); + + ReadFromStream(*_streamBuffer, value); + return *this; +} + +InputStream& InputStream::Read(double& value) { + assert(_streamBuffer); + + ReadFromStream(*_streamBuffer, value); + return *this; +} + +InputStream& InputStream::Read(unsigned char* buffer, int elements) { + assert(_streamBuffer); + + _streamBuffer->PopBytes(reinterpret_cast(buffer), elements); + return *this; +} + +InputStream& InputStream::Read(char* buffer, int elements) { + assert(_streamBuffer); + _streamBuffer->PopBytes(buffer, elements); + + return *this; +} + +InputStream& InputStream::Read(unsigned short* buffer, int elements) { + assert(_streamBuffer); + + _streamBuffer->PopBytes(reinterpret_cast(buffer), elements * sizeof(short)); +} + +} // End of namespace filesystem. +} // End of namespace saracraft. diff --git a/src/System/Filesystem/InputStream.h b/src/System/Filesystem/InputStream.h new file mode 100644 index 0000000..2d544d0 --- /dev/null +++ b/src/System/Filesystem/InputStream.h @@ -0,0 +1,90 @@ +#pragma once + +#ifndef INCLUDED_BOOST_SHARED_PTR_HPP +#define INCLUDED_BOOST_SHATED_PTR_HPP +#include +#endif +#ifndef INCLUDED_STRING +#define INCLUDED_STRING +#include +#endif + +namespace saracraft { +namespace filesystem { + +class IInputStreamBuffer { +public: + virtual ~IInputStreamBuffer(void) { } + + virtual unsigned char PopByte(void) = 0; + virtual bool IsEof(void) const = 0; + virtual int GetSize(void) const = 0; + + virtual void PopBytes(char* buffer, int bytes) = 0; +}; + +class InputStream { + boost::shared_ptr _streamBuffer; +public: + InputStream(void); + ~InputStream(void); + + void SetBuffer(boost::shared_ptr streamBuffer); + bool IsEof(void) const; + int GetSize(void) const; + +private: + InputStream& Read(std::string& value); + InputStream& Read(bool& value); + + InputStream& Read(unsigned char& value); + InputStream& Read(char& value); + InputStream& Read(signed char& value); + + InputStream& Read(unsigned short& value); + InputStream& Read(signed short& value); + + InputStream& Read(unsigned int& value); + InputStream& Read(signed int& value); + + InputStream& Read(float& value); + InputStream& Read(double& value); + + friend InputStream& operator >> (InputStream&, std::string&); + friend InputStream& operator >> (InputStream&, bool&); + friend InputStream& operator >> (InputStream&, unsigned char&); + friend InputStream& operator >> (InputStream&, char&); + friend InputStream& operator >> (InputStream&, signed char&); + friend InputStream& operator >> (InputStream&, unsigned short&); + friend InputStream& operator >> (InputStream&, signed short&); + friend InputStream& operator >> (InputStream&, unsigned int&); + friend InputStream& operator >> (InputStream&, signed int&); + friend InputStream& operator >> (InputStream&, float&); + friend InputStream& operator >> (InputStream&, double&); + +public: + // Optimized readers. + InputStream& Read(unsigned char* buffer, int elements); + InputStream& Read(char* buffer, int elements); + InputStream& Read(unsigned short* buffer, int elements); +}; + +inline InputStream& operator >> (InputStream& stream, std::string& value) { return stream.Read(value); } +inline InputStream& operator >> (InputStream& stream, bool& value) { return stream.Read(value); } + +inline InputStream& operator >> (InputStream& stream, unsigned char& value) { return stream.Read(value); } +inline InputStream& operator >> (InputStream& stream, char& value) { return stream.Read(value); } +inline InputStream& operator >> (InputStream& stream, signed char& value) { return stream.Read(value); } + +inline InputStream& operator >> (InputStream& stream, unsigned short& value) { return stream.Read(value); } +inline InputStream& operator >> (InputStream& stream, signed short& value) { return stream.Read(value); } + +inline InputStream& operator >> (InputStream& stream, unsigned int& value) { return stream.Read(value); } +inline InputStream& operator >> (InputStream& stream, signed int& value) { return stream.Read(value); } + +inline InputStream& operator >> (InputStream& stream, float& value) { return stream.Read(value); } +inline InputStream& operator >> (InputStream& stream, double& value) { return stream.Read(value); } + +} // Namespace filesystem. +} // Namespace saracraft. +