diff --git a/Bin/LibD.pro b/Bin/LibD.pro index f77e42f..c69147f 100644 --- a/Bin/LibD.pro +++ b/Bin/LibD.pro @@ -39,6 +39,7 @@ HEADERS += ../src/Actor/Player.h \ ../src/System/ResourceManager.h \ ../src/System/ConvertType.h \ ../src/System/Filesystem/InputStream.h \ + ../src/System/Filesystem/OutputStream.h \ ../src/System/Filesystem/EmptyBuffer.h \ ../src/System/Filesystem/IFileList.h \ ../src/System/Filesystem/IFilePackage.h \ @@ -46,6 +47,8 @@ HEADERS += ../src/Actor/Player.h \ ../src/System/Filesystem/InputFileStream.h \ ../src/System/Filesystem/FilePackageManager.h \ ../src/System/Filesystem/InputStreamWrapper.h \ + ../src/System/Filesystem/MemoryStream.h \ + ../src/System/SCCopyFile.cpp \ ../src/System/Checksum.h \ ../src/Texture/Texture.h \ ../src/Sound/Music.h \ @@ -98,10 +101,13 @@ SOURCES += ../src/Actor/Player.cpp \ ../src/Sprite/Sprite.cpp \ ../src/System/Debug.cpp \ ../src/System/Filesystem/InputStream.cpp \ + ../src/System/Filesystem/OutputStream.cpp \ ../src/System/Filesystem/FileList.cpp \ ../src/System/Filesystem/InputFileStream.cpp \ ../src/System/Filesystem/FilePackageManager.cpp \ ../src/System/Filesystem/InputStreamWrapper.cpp \ + ../src/System/Filesystem/MemoryStream.cpp \ + ../src/System/SCCopyFile.cpp \ ../src/System/Checksum.cpp \ ../src/Texture/Texture.cpp \ ../src/Sound/Music.cpp \ diff --git a/src/System/Filesystem/MemoryStream.cpp b/src/System/Filesystem/MemoryStream.cpp new file mode 100644 index 0000000..08862ec --- /dev/null +++ b/src/System/Filesystem/MemoryStream.cpp @@ -0,0 +1,48 @@ +#include +#include "MemoryStream.h" + +namespace saracraft { +namespace filesystem { + +struct MemoryStreamBufferData { + std::queue buffer; +}; + +MemoryStreamBuffer::MemoryStreamBuffer(void) { + boost::scoped_ptr tempData(new MemoryStreamBufferData()); + _data.swap(tempData); +} + +MemoryStreamBuffer::~MemoryStreamBuffer(void) { + +} + +unsigned char MemoryStreamBuffer::PopByte(void) { + if(!_data->buffer.empty()) { + unsigned char value = _data->buffer.front(); + _data->buffer.pop(); + return value; + } + return 0; +} + +bool MemoryStreamBuffer::IsEof(void) const { + return _data->buffer.empty(); +} + +int MemoryStreamBuffer::GetSize(void) const { + return _data->buffer.size(); +} + +void MemoryStreamBuffer::PutByte(unsigned char byte) { + _data->buffer.push(byte); +} + +void MemoryStreamBuffer::PopBytes(char* buffer, int bytes) { + for(int i = 0; i < bytes; ++i) + buffer[i] = PopByte(); +} + +} // Namespace filesystem. +} // Namespace saracraft. + diff --git a/src/System/Filesystem/MemoryStream.h b/src/System/Filesystem/MemoryStream.h new file mode 100644 index 0000000..989bef2 --- /dev/null +++ b/src/System/Filesystem/MemoryStream.h @@ -0,0 +1,26 @@ +#include +#include "InputStream.h" +#include "OutputStream.h" + +namespace saracraft { +namespace filesystem { + +struct MemoryStreamBufferData; + +class MemoryStreamBuffer : public IInputStreamBuffer, public IOutputStreamBuffer { + boost::scoped_ptr _data; +public: + MemoryStreamBuffer(void); + ~MemoryStreamBuffer(void); + + unsigned char PopByte(void); + bool IsEof(void) const; + int GetSize(void) const; + + void PutByte(unsigned char byte); + void PopBytes(char* buffer, int bytes); +}; + +} // Namespace filesystem. +} // Namespace saracraft. + diff --git a/src/System/Filesystem/OutputStream.cpp b/src/System/Filesystem/OutputStream.cpp new file mode 100644 index 0000000..dbc4d5e --- /dev/null +++ b/src/System/Filesystem/OutputStream.cpp @@ -0,0 +1,130 @@ +#include + +#include "../ConvertType.h" +#include +#include "OutputStream.h" + +#ifdef __INTEL_COMPILER +#pragma warning(disable: 444) // I think intel will complain at the base class not being virtual. +#endif + +BOOST_STATIC_ASSERT(sizeof(saracraft::filesystem::uint16_t) * CHAR_BIT == 16); +BOOST_STATIC_ASSERT(CHAR_BIT == 8); + +namespace saracraft { +namespace filesystem { +namespace { + + template + void SendToStream(IOutputStreamBuffer& buffer, T& value) { + ConvertFrom converter(value); + for(int i = 0; i < converter.GetSize(); ++i) + buffer.PutByte(converter.GetByte(i)); + } + +} // End of unamed namespace. + +OutputStream::OutputStream(void) : _textStrings(false){ + +} + +OutputStream::~OutputStream(void) { + +} + +void OutputStream::SetBuffer(boost::shared_ptr streamBuffer_) { + assert(streamBuffer_); + _streamBuffer = streamBuffer_; +} + +void OutputStream::UseTextStrings(void) { + _textStrings = true; +} + + +OutputStream& OutputStream::Write(std::string& value) { + assert(_streamBuffer); + + uint16_t stringSize = 0; + + if(!_textStrings) + Write(stringSize); + + for(int i = 0; i < stringSize; ++i) + _streamBuffer->PutByte(value[i]); + + return *this; +} + +OutputStream& OutputStream::Write(bool& value) { + assert(_streamBuffer); + unsigned char b = (value) ? 1:0; + _streamBuffer->PutByte(b); + return *this; +} + +OutputStream& OutputStream::Write(unsigned char& value) { + assert(_streamBuffer); + + _streamBuffer->PutByte(value); + return *this; +} + +OutputStream& OutputStream::Write(char& value) { + assert(_streamBuffer); + + _streamBuffer->PutByte(value); + return *this; +} + +OutputStream& OutputStream::Write(signed char& value) { + assert(_streamBuffer); + + _streamBuffer->PutByte(value); + return *this; +} + +OutputStream& OutputStream::Write(unsigned short& value) { + assert(_streamBuffer); + + SendToStream(*_streamBuffer, value); + return *this; +} + +OutputStream& OutputStream::Write(signed short& value) { + assert(_streamBuffer); + + SendToStream(*_streamBuffer, value); + return *this; +} + +OutputStream& OutputStream::Write(unsigned int& value) { + assert(_streamBuffer); + + SendToStream(*_streamBuffer, value); + return * this; +} + +OutputStream& OutputStream::Write(signed int& value) { + assert(_streamBuffer); + + SendToStream(*_streamBuffer, value); + return *this; +} + +OutputStream& OutputStream::Write(float& value) { + assert(_streamBuffer); + + SendToStream(*_streamBuffer, value); + return *this; +} + +OutputStream& OutputStream::Write(double& value) { + assert(_streamBuffer); + + SendToStream(*_streamBuffer, value); + return *this; +} + +} // End of namespace filesystem. +} // End of namespace saracraft. diff --git a/src/System/Filesystem/OutputStream.h b/src/System/Filesystem/OutputStream.h new file mode 100644 index 0000000..3883921 --- /dev/null +++ b/src/System/Filesystem/OutputStream.h @@ -0,0 +1,80 @@ +#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 IOutputStreamBuffer { +public: + virtual ~IOutputStreamBuffer(void) { } + + virtual void PutByte(unsigned char byte) = 0; +}; + +class OutputStream { + boost::shared_ptr _streamBuffer; + bool _textStrings; +public: + OutputStream(void); + ~OutputStream(void); + + void SetBuffer(boost::shared_ptr streamBuffer); + void UseTextStrings(void); // Insert pure strings. Put std::endl manually!!! + +private: + OutputStream& Write(std::string& value); + OutputStream& Write(bool& value); + + OutputStream& Write(unsigned char& value); + OutputStream& Write(char& value); + OutputStream& Write(signed char& value); + + OutputStream& Write(unsigned short& value); + OutputStream& Write(signed short& value); + + OutputStream& Write(unsigned int& value); + OutputStream& Write(signed int& value); + + OutputStream& Write(float& value); + OutputStream& Write(double& value); + + friend OutputStream& operator >> (OutputStream&, std::string&); + friend OutputStream& operator >> (OutputStream&, bool&); + friend OutputStream& operator >> (OutputStream&, unsigned char&); + friend OutputStream& operator >> (OutputStream&, char&); + friend OutputStream& operator >> (OutputStream&, signed char&); + friend OutputStream& operator >> (OutputStream&, unsigned short&); + friend OutputStream& operator >> (OutputStream&, signed short&); + friend OutputStream& operator >> (OutputStream&, unsigned int&); + friend OutputStream& operator >> (OutputStream&, signed int&); + friend OutputStream& operator >> (OutputStream&, float&); + friend OutputStream& operator >> (OutputStream&, double&); +}; + +inline OutputStream& operator >> (OutputStream& stream, std::string& value) { return stream.Write(value); } +inline OutputStream& operator >> (OutputStream& stream, bool& value) { return stream.Write(value); } + +inline OutputStream& operator >> (OutputStream& stream, unsigned char& value) { return stream.Write(value); } +inline OutputStream& operator >> (OutputStream& stream, char& value) { return stream.Write(value); } +inline OutputStream& operator >> (OutputStream& stream, signed char& value) { return stream.Write(value); } + +inline OutputStream& operator >> (OutputStream& stream, unsigned short& value) { return stream.Write(value); } +inline OutputStream& operator >> (OutputStream& stream, signed short& value) { return stream.Write(value); } + +inline OutputStream& operator >> (OutputStream& stream, unsigned int& value) { return stream.Write(value); } +inline OutputStream& operator >> (OutputStream& stream, signed int& value) { return stream.Write(value); } + +inline OutputStream& operator >> (OutputStream& stream, float& value) { return stream.Write(value); } +inline OutputStream& operator >> (OutputStream& stream, double& value) { return stream.Write(value); } + +} // Namespace filesystem. +} // Namespace saracraft. + diff --git a/src/System/SCCopyFile.cpp b/src/System/SCCopyFile.cpp new file mode 100644 index 0000000..b190fc7 --- /dev/null +++ b/src/System/SCCopyFile.cpp @@ -0,0 +1,23 @@ +#include +#include +#include "Filesystem/InputStream.h" +#include "Filesystem/FilePackageManager.h" + +#include "SCCopyFile.h" + +namespace util { + +void SCCopyFile::CopyFile(const std::string& from, const std::string& to) { + std::fstream out(to.c_str(), std::ios::out); + saracraft::filesystem::InputStream in = saracraft::filesystem::FilePackageManager::GetInstance().GetFile(from); + + std::string temp; + temp.resize(in.GetSize()); + in.Read((unsigned char*)&temp[0], in.GetSize()); + out << temp << std::endl; + + out.close(); +} + +} // Namespace util. + diff --git a/src/System/SCCopyFile.h b/src/System/SCCopyFile.h new file mode 100644 index 0000000..4c404a1 --- /dev/null +++ b/src/System/SCCopyFile.h @@ -0,0 +1,16 @@ +#pragma once + +namespace util { + +class SCCopyFile { +public: + + // TODO: This will currently only handle smallish text files + // propperly. + + /*** DON'T USE FOR BINARY FILES!!!! ***/ + static void CopyFile(const std::string& from, const std::string& to); +}; + +} // Namespace util. +