[Add] Implementing a filesystem for LibD.
This commit is contained in:
parent
531e46357b
commit
b5210850b6
@ -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 \
|
||||
|
55
src/System/ConvertType.h
Normal file
55
src/System/ConvertType.h
Normal file
@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef INCLUDED_CASSERT
|
||||
#define INCLUSED_CASSERT
|
||||
#include <cassert>
|
||||
#endif
|
||||
|
||||
namespace saracraft {
|
||||
namespace filesystem {
|
||||
|
||||
template<class T>
|
||||
struct ConvertBase {
|
||||
enum { charCount = sizeof(T) };
|
||||
union Values {
|
||||
unsigned char c[charCount];
|
||||
T t;
|
||||
};
|
||||
|
||||
Values value;
|
||||
|
||||
int GetSize(void) const { return charCount; }
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct ConvertFrom: private ConvertBase<T> {
|
||||
explicit ConvertFrom(const T& t) {
|
||||
this->value.t = t;
|
||||
}
|
||||
using ConvertBase<T>::GetSize;
|
||||
|
||||
unsigned char GetByte(int index) const {
|
||||
assert((index >= 0) && (index < GetSize()));
|
||||
return this->value.c[index];
|
||||
}
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct ConvertTo: private ConvertBase<T> {
|
||||
using ConvertBase<T>::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....
|
||||
}
|
||||
|
150
src/System/Filesystem/InputStream.cpp
Normal file
150
src/System/Filesystem/InputStream.cpp
Normal file
@ -0,0 +1,150 @@
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
#include "../ConvertType.h"
|
||||
#include <limits.h>
|
||||
#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<class T>
|
||||
void ReadFromStream(IInputStreamBuffer& buffer, T& value) {
|
||||
ConvertTo<T> 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<IInputStreamBuffer> 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<char*>(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<char*>(buffer), elements * sizeof(short));
|
||||
}
|
||||
|
||||
} // End of namespace filesystem.
|
||||
} // End of namespace saracraft.
|
90
src/System/Filesystem/InputStream.h
Normal file
90
src/System/Filesystem/InputStream.h
Normal file
@ -0,0 +1,90 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef INCLUDED_BOOST_SHARED_PTR_HPP
|
||||
#define INCLUDED_BOOST_SHATED_PTR_HPP
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#endif
|
||||
#ifndef INCLUDED_STRING
|
||||
#define INCLUDED_STRING
|
||||
#include <string>
|
||||
#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<IInputStreamBuffer> _streamBuffer;
|
||||
public:
|
||||
InputStream(void);
|
||||
~InputStream(void);
|
||||
|
||||
void SetBuffer(boost::shared_ptr<IInputStreamBuffer> 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.
|
||||
|
Loading…
Reference in New Issue
Block a user