[Add] Copy File method.

[Add] Output streams.
[Add] Memory streams.
This commit is contained in:
Rtch90 2012-09-03 23:20:24 +01:00
parent bfde44079c
commit a329000a5b
7 changed files with 329 additions and 0 deletions

View File

@ -39,6 +39,7 @@ HEADERS += ../src/Actor/Player.h \
../src/System/ResourceManager.h \ ../src/System/ResourceManager.h \
../src/System/ConvertType.h \ ../src/System/ConvertType.h \
../src/System/Filesystem/InputStream.h \ ../src/System/Filesystem/InputStream.h \
../src/System/Filesystem/OutputStream.h \
../src/System/Filesystem/EmptyBuffer.h \ ../src/System/Filesystem/EmptyBuffer.h \
../src/System/Filesystem/IFileList.h \ ../src/System/Filesystem/IFileList.h \
../src/System/Filesystem/IFilePackage.h \ ../src/System/Filesystem/IFilePackage.h \
@ -46,6 +47,8 @@ HEADERS += ../src/Actor/Player.h \
../src/System/Filesystem/InputFileStream.h \ ../src/System/Filesystem/InputFileStream.h \
../src/System/Filesystem/FilePackageManager.h \ ../src/System/Filesystem/FilePackageManager.h \
../src/System/Filesystem/InputStreamWrapper.h \ ../src/System/Filesystem/InputStreamWrapper.h \
../src/System/Filesystem/MemoryStream.h \
../src/System/SCCopyFile.cpp \
../src/System/Checksum.h \ ../src/System/Checksum.h \
../src/Texture/Texture.h \ ../src/Texture/Texture.h \
../src/Sound/Music.h \ ../src/Sound/Music.h \
@ -98,10 +101,13 @@ SOURCES += ../src/Actor/Player.cpp \
../src/Sprite/Sprite.cpp \ ../src/Sprite/Sprite.cpp \
../src/System/Debug.cpp \ ../src/System/Debug.cpp \
../src/System/Filesystem/InputStream.cpp \ ../src/System/Filesystem/InputStream.cpp \
../src/System/Filesystem/OutputStream.cpp \
../src/System/Filesystem/FileList.cpp \ ../src/System/Filesystem/FileList.cpp \
../src/System/Filesystem/InputFileStream.cpp \ ../src/System/Filesystem/InputFileStream.cpp \
../src/System/Filesystem/FilePackageManager.cpp \ ../src/System/Filesystem/FilePackageManager.cpp \
../src/System/Filesystem/InputStreamWrapper.cpp \ ../src/System/Filesystem/InputStreamWrapper.cpp \
../src/System/Filesystem/MemoryStream.cpp \
../src/System/SCCopyFile.cpp \
../src/System/Checksum.cpp \ ../src/System/Checksum.cpp \
../src/Texture/Texture.cpp \ ../src/Texture/Texture.cpp \
../src/Sound/Music.cpp \ ../src/Sound/Music.cpp \

View File

@ -0,0 +1,48 @@
#include <queue>
#include "MemoryStream.h"
namespace saracraft {
namespace filesystem {
struct MemoryStreamBufferData {
std::queue<unsigned char> buffer;
};
MemoryStreamBuffer::MemoryStreamBuffer(void) {
boost::scoped_ptr<MemoryStreamBufferData> 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.

View File

@ -0,0 +1,26 @@
#include <boost/scoped_ptr.hpp>
#include "InputStream.h"
#include "OutputStream.h"
namespace saracraft {
namespace filesystem {
struct MemoryStreamBufferData;
class MemoryStreamBuffer : public IInputStreamBuffer, public IOutputStreamBuffer {
boost::scoped_ptr<MemoryStreamBufferData> _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.

View File

@ -0,0 +1,130 @@
#include <boost/static_assert.hpp>
#include "../ConvertType.h"
#include <limits.h>
#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<class T>
void SendToStream(IOutputStreamBuffer& buffer, T& value) {
ConvertFrom<T> 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<IOutputStreamBuffer> 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.

View File

@ -0,0 +1,80 @@
#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 IOutputStreamBuffer {
public:
virtual ~IOutputStreamBuffer(void) { }
virtual void PutByte(unsigned char byte) = 0;
};
class OutputStream {
boost::shared_ptr<IOutputStreamBuffer> _streamBuffer;
bool _textStrings;
public:
OutputStream(void);
~OutputStream(void);
void SetBuffer(boost::shared_ptr<IOutputStreamBuffer> 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.

23
src/System/SCCopyFile.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <fstream>
#include <string>
#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.

16
src/System/SCCopyFile.h Normal file
View File

@ -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.