This commit is contained in:
Tamir Atias 2012-09-08 12:38:57 +03:00
commit 099f1ebbcf
4 changed files with 152 additions and 0 deletions

View File

@ -38,8 +38,10 @@ HEADERS += ../src/Actor/Player.h \
../src/System/Debug.h \
../src/System/ResourceManager.h \
../src/System/Convert/str2int.h \
../src/System/Filesystem/IOapi.h \
../src/System/Filesystem/FileTimestampChecker.h \
../src/System/Convert/ConvertType.h \
../src/System/Filesystem/OutputCompressedFileStream.h \
../src/System/Filesystem/InputCompressedFileStream.h \
../src/System/Filesystem/InputStream.h \
../src/System/Filesystem/OutputStream.h \
@ -104,6 +106,7 @@ SOURCES += ../src/Actor/Player.cpp \
../src/System/Debug.cpp \
../src/System/Convert/str2int.cpp \
../src/System/Filesystem/FileTimestampChecker.cpp \
../src/System/Filesystem/OutputCompressedFileStream.cpp \
../src/System/Filesystem/InputCompressedFileStream.cpp \
../src/System/Filesystem/InputStream.cpp \
../src/System/Filesystem/OutputStream.cpp \

View File

@ -0,0 +1,60 @@
// This is an IO base function header for compress and uncompress.
// This is for files using zlib + zip or unzip.
#pragma once
#define ZLIB_FILEFUNC_SEEK_CUR (1)
#define ZLIB_FILEFUNC_SEEK_END (2)
#define ZLIB_FILEFUNC_SEEK_SET (3)
#define ZLIB_FILEFUNC_MODE_READ (1)
#define ZLIB_FILEFUNC_MODE_WRITE (2)
#define ZLIB_FILEFUNC_MODE_READWRITEFILTER (3)
#define ZLIB_FILEFUNC_MODE_EXISTING (4)
#define ZLIB_FILEFUNC_MODE_CREATE (8)
#ifndef ZCALLBACK
#if(defined(WIN32) || defined(WINDOWS) || defined(_WINDOWS)) && \
defined(CALLBACK) && defined(USEWINDOWS_CALLBACK)
#else
#define ZCALLBACK
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef voidpf(ZCALLBACK* open_file_func) OF((voidpf opaque, const char* filename, int mode));
typedef uLong (ZCALLBACK* read_file_func) OF((voidpf opaque, voidpf stream, void* buf, uLong size));
typedef uLong (ZCALLBACK* write_file_func) OF((voidpf opaque, voidpf stream, const void* buf, uLong sixe));
typedef long (ZCALLBACK* tell_file_func) OF((voidpf opaque, voidpf stream));
typedef long (ZCALLBACK* seek_file_func) OF((voidpf opaque, voidpf stream, uLong offset, int origin));
typedef int (ZCALLBACK* close_file_func) OF((voidpf opaque, voidpf stream));
typedef int (ZCALLBACK* testerror_file_func) OF((voidpf opaque, voidpf stream));
typedef struct zlib_filefunc_def_s { // WHY ?!?!?!!?!
open_file_func zopen_file;
read_file_func zread_file;
write_file_func zwrite_file;
tell_file_func ztell_file;
seek_file_func zseek_file;
close_file_func zclose_file;
testerror_file_func zerror_file;
voidpf opaque;
} zlib_filefunc_def;
void Fill_Fopen_FileFunc OF((zlib_filefunc_def* pzlib_filefunc_def));
#define ZREAD(filefunc,filestream,buf,size) ((*((filefunc).zread_file))((filefunc).opaque,filestream,buf,size))
#define ZWRITE(filefunc,filestream,buf,size) ((*((filefunc).zwrite_file))((filefunc).opaque,filestream,buf,size))
#define ZTELL(filefunc,filestream) ((*((filefunc).ztell_file))((filefunc).opaque,filestream))
#define ZSEEK(filefunc,filestream,pos,mode) ((*((filefunc).zseek_file))((filfunc).opaque,filestream,pos,mode))
#define ZCLOSE(filefunc,filestream) ((*((filefunc).zclose_file))((filefunc).opaque,filestream))
#define ZERROR(filefunc,filestream) ((*((filefunc).zerror_file))((filefunc).opaque,filestream))
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,66 @@
#include <vector>
#include <zlib.h>
#include <fstream>
#include "OutputCompressedFileStream.h"
namespace saracraft {
namespace filesystem {
struct OutputCompressedFileStreamBufferData {
std::ofstream stream;
std::vector<unsigned char> buffer;
OutputCompressedFileStreamBufferData(const std::string filename) :
stream(filename.c_str(), std::ios::binary) {
}
~OutputCompressedFileStreamBufferData(void) {
}
void Compress(void) {
int bytes = buffer.size();
stream.write((char*)& bytes, sizeof(int));
if(!bytes)
return;
std::vector<char> tempBuffer(bytes+12);
Bytef* source = &buffer[0];
uLong sourceLength = buffer.size();
Bytef* destination = (Bytef*)& tempBuffer[0];
uLong destinationLength = tempBuffer.size();
int code = ::compress(destination, &destinationLength, source, sourceLength);
if(code != Z_OK)
int a = 0;
stream.write(&tempBuffer[0], destinationLength);
}
};
OutputCompressedFileStreamBuffer::OutputCompressedFileStreamBuffer(const std::string& filename) {
boost::scoped_ptr<OutputCompressedFileStreamBufferData> tempData(new OutputCompressedFileStreamBufferData(filename));
_data.swap(tempData);
}
OutputCompressedFileStreamBuffer::~OutputCompressedFileStreamBuffer(void) {
_data->Compress();
}
void OutputCompressedFileStreamBuffer::PutByte(unsigned char c) {
_data->buffer.push_back(c);
//char c_ = c;
//_data->stream.Write(&c_, 1);
}
OutputStream CreateOutputCompressedFileStream(const std::string& filename) {
OutputStream outputStream;
boost::shared_ptr<OutputCompressedFileStreamBuffer> outputBuffer(new OutputCompressedFileStreamBuffer(filename));
outputStream.SetBuffer(outputBuffer);
return outputStream;
}
} // Namespace filesystem.
} // Namespace saracraft.

View File

@ -0,0 +1,23 @@
#pragma once
#include <boost/scoped_ptr.hpp>
#include "OutputStream.h"
namespace saracraft {
namespace filesystem {
struct OutputCompressedFileStreamBufferData;
class OutputCompressedFileStreamBuffer : public IOutputStreamBuffer {
boost::scoped_ptr<OutputCompressedFileStreamBufferData> _data;
public:
OutputCompressedFileStreamBuffer(const std::string& filename);
~OutputCompressedFileStreamBuffer(void);
void PutByte(unsigned char c);
};
OutputStream CreateOutputCompressedFileStream(const std::string& filename);
} // Namespace filesystem.
} // Namespace saracraft.