mirror of
https://github.com/SpartanJ/eepp.git
synced 2026-05-30 18:16:31 +03:00
Implemented IOStreamZip.
Added Pack::getFileStream. --HG-- branch : dev-2.1
This commit is contained in:
43
include/eepp/system/iostreamzip.hpp
Normal file
43
include/eepp/system/iostreamzip.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef EE_SYS_IOSTREAMZIP_HPP
|
||||
#define EE_SYS_IOSTREAMZIP_HPP
|
||||
|
||||
#include <eepp/system/iostream.hpp>
|
||||
|
||||
struct zip;
|
||||
struct zip_file;
|
||||
|
||||
namespace EE { namespace System {
|
||||
class Zip;
|
||||
|
||||
/** @brief An implementation for a zip file steam */
|
||||
class EE_API IOStreamZip: public IOStream {
|
||||
public:
|
||||
/** @brief Open a file from a zip file
|
||||
** @param pack Pack to open from path
|
||||
** @param path Path of the file in the pack file
|
||||
**/
|
||||
IOStreamZip( Zip * pack, const std::string& path );
|
||||
|
||||
virtual ~IOStreamZip();
|
||||
|
||||
ios_size read( char * data, ios_size size );
|
||||
|
||||
ios_size write( const char * data, ios_size size );
|
||||
|
||||
ios_size seek( ios_size position );
|
||||
|
||||
ios_size tell();
|
||||
|
||||
ios_size getSize();
|
||||
|
||||
bool isOpen();
|
||||
protected:
|
||||
std::string mPath;
|
||||
struct zip * mZip;
|
||||
struct zip_file * mFile;
|
||||
ios_size mPos;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <eepp/system/base.hpp>
|
||||
#include <eepp/system/mutex.hpp>
|
||||
#include <eepp/system/safedatapointer.hpp>
|
||||
#include <eepp/system/iostream.hpp>
|
||||
|
||||
namespace EE { namespace System {
|
||||
|
||||
@@ -68,6 +69,9 @@ class EE_API Pack : protected Mutex {
|
||||
|
||||
/** @return The file path of the opened package */
|
||||
virtual std::string getPackPath() = 0;
|
||||
|
||||
/** Open a file stream for reading */
|
||||
virtual IOStream * getFileStream( const std::string& path ) = 0;
|
||||
protected:
|
||||
bool mIsOpen;
|
||||
};
|
||||
|
||||
@@ -67,6 +67,8 @@ class EE_API Pak : public Pack {
|
||||
|
||||
/** @return The file path of the opened package */
|
||||
std::string getPackPath();
|
||||
|
||||
IOStream * getFileStream( const std::string& path );
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
||||
@@ -66,10 +66,16 @@ class EE_API Zip : public Pack {
|
||||
|
||||
/** @return The file path of the opened package */
|
||||
std::string getPackPath();
|
||||
|
||||
IOStream * getFileStream( const std::string& path );
|
||||
protected:
|
||||
friend class IOStreamZip;
|
||||
|
||||
struct zip * mZip;
|
||||
|
||||
std::string mZipPath;
|
||||
|
||||
struct zip * getZip();
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
../../include/eepp/math/interpolation1d.hpp
|
||||
../../include/eepp/math/interpolation2d.hpp
|
||||
../../include/eepp/system/color.hpp
|
||||
../../include/eepp/system/iostreamzip.hpp
|
||||
../../include/eepp/system/translator.hpp
|
||||
../../include/eepp/ui/uieventmouse.hpp
|
||||
../../include/eepp/ui/uigridlayout.hpp
|
||||
@@ -80,6 +81,7 @@
|
||||
../../src/eepp/math/interpolation1d.cpp
|
||||
../../src/eepp/math/interpolation2d.cpp
|
||||
../../src/eepp/system/color.cpp
|
||||
../../src/eepp/system/iostreamzip.cpp
|
||||
../../src/eepp/system/translator.cpp
|
||||
../../src/eepp/ui/uigridlayout.cpp
|
||||
../../src/eepp/ui/uiimage.cpp
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
../../src/thirdparty
|
||||
../../include/eepp/thirdparty
|
||||
../../src/eepp/system
|
||||
../../include/eepp/system
|
||||
|
||||
84
src/eepp/system/iostreamzip.cpp
Normal file
84
src/eepp/system/iostreamzip.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <eepp/system/iostreamzip.hpp>
|
||||
#include <eepp/system/zip.hpp>
|
||||
#include <libzip/zip.h>
|
||||
#include <libzip/zipint.h>
|
||||
|
||||
namespace EE { namespace System {
|
||||
|
||||
IOStreamZip::IOStreamZip( Zip * pack, const std::string& path ) :
|
||||
mPath( path ),
|
||||
mZip( pack->getZip() ),
|
||||
mFile( NULL ),
|
||||
mPos( 0 )
|
||||
{
|
||||
struct zip_stat zs;
|
||||
int err = zip_stat( mZip, path.c_str(), 0, &zs );
|
||||
|
||||
if ( !err ) {
|
||||
mFile = zip_fopen_index( mZip, zs.index, 0 );
|
||||
}
|
||||
}
|
||||
|
||||
IOStreamZip::~IOStreamZip() {
|
||||
if ( isOpen() ) {
|
||||
zip_fclose(mFile);
|
||||
}
|
||||
}
|
||||
|
||||
ios_size IOStreamZip::read( char * data, ios_size size ) {
|
||||
int res = -1;
|
||||
|
||||
if ( isOpen() ) {
|
||||
res = zip_fread( mFile, reinterpret_cast<void*> (&data[0]), size );
|
||||
|
||||
if ( -1 != res ) {
|
||||
mPos += size;
|
||||
}
|
||||
}
|
||||
|
||||
return -1 != res ? res : 0;
|
||||
}
|
||||
|
||||
ios_size IOStreamZip::write( const char * data, ios_size size ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ios_size IOStreamZip::seek( ios_size position ) {
|
||||
if ( isOpen() ) {
|
||||
zip_fclose( mFile );
|
||||
|
||||
struct zip_stat zs;
|
||||
int err = zip_stat( mZip, mPath.c_str(), 0, &zs );
|
||||
|
||||
if ( !err ) {
|
||||
mFile = zip_fopen_index( mZip, zs.index, 0 );
|
||||
|
||||
if ( 0 != position ) {
|
||||
SafeDataPointer ptr( eeNewArray( Uint8, position ), position );
|
||||
read( (char*)ptr.Data, position );
|
||||
}
|
||||
|
||||
mPos = position;
|
||||
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ios_size IOStreamZip::tell() {
|
||||
return mPos;
|
||||
}
|
||||
|
||||
ios_size IOStreamZip::getSize() {
|
||||
struct zip_stat zs;
|
||||
int err = zip_stat( mZip, mPath.c_str(), 0, &zs );
|
||||
return !err ? zs.size : 0;
|
||||
}
|
||||
|
||||
bool IOStreamZip::isOpen() {
|
||||
return NULL != mFile;
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -374,4 +374,8 @@ std::string Pak::getPackPath() {
|
||||
return mPak.pakPath;
|
||||
}
|
||||
|
||||
IOStream * Pak::getFileStream(const std::string & path) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <libzip/zip.h>
|
||||
#include <libzip/zipint.h>
|
||||
#include <eepp/system/filesystem.hpp>
|
||||
#include <eepp/system/iostreamzip.hpp>
|
||||
|
||||
namespace EE { namespace System {
|
||||
|
||||
@@ -250,4 +251,13 @@ std::string Zip::getPackPath() {
|
||||
return mZipPath;
|
||||
}
|
||||
|
||||
IOStream * Zip::getFileStream( const std::string & path ) {
|
||||
return eeNew( IOStreamZip, ( this, path ) );
|
||||
}
|
||||
|
||||
zip * Zip::getZip()
|
||||
{
|
||||
return mZip;
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user