From de72fac876e7f23cfbfea5bbb25fb0d3c288b8e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Fri, 5 Jan 2018 02:40:15 -0300 Subject: [PATCH] Implemented IOStreamZip. Added Pack::getFileStream. --HG-- branch : dev-2.1 --- include/eepp/system/iostreamzip.hpp | 43 +++++++++++++++ include/eepp/system/pack.hpp | 4 ++ include/eepp/system/pak.hpp | 2 + include/eepp/system/zip.hpp | 6 +++ projects/linux/ee.files | 2 + projects/linux/ee.includes | 1 + src/eepp/system/iostreamzip.cpp | 84 +++++++++++++++++++++++++++++ src/eepp/system/pak.cpp | 4 ++ src/eepp/system/zip.cpp | 10 ++++ 9 files changed, 156 insertions(+) create mode 100644 include/eepp/system/iostreamzip.hpp create mode 100644 src/eepp/system/iostreamzip.cpp diff --git a/include/eepp/system/iostreamzip.hpp b/include/eepp/system/iostreamzip.hpp new file mode 100644 index 000000000..3d0f49cce --- /dev/null +++ b/include/eepp/system/iostreamzip.hpp @@ -0,0 +1,43 @@ +#ifndef EE_SYS_IOSTREAMZIP_HPP +#define EE_SYS_IOSTREAMZIP_HPP + +#include + +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 diff --git a/include/eepp/system/pack.hpp b/include/eepp/system/pack.hpp index 3e57710b9..14b9a0968 100755 --- a/include/eepp/system/pack.hpp +++ b/include/eepp/system/pack.hpp @@ -4,6 +4,7 @@ #include #include #include +#include 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; }; diff --git a/include/eepp/system/pak.hpp b/include/eepp/system/pak.hpp index 2d0d633b2..afd5f19c0 100755 --- a/include/eepp/system/pak.hpp +++ b/include/eepp/system/pak.hpp @@ -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: diff --git a/include/eepp/system/zip.hpp b/include/eepp/system/zip.hpp index eba3f9c3f..f5ee4ca9f 100644 --- a/include/eepp/system/zip.hpp +++ b/include/eepp/system/zip.hpp @@ -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(); }; }} diff --git a/projects/linux/ee.files b/projects/linux/ee.files index 5a0403d16..04e08e226 100644 --- a/projects/linux/ee.files +++ b/projects/linux/ee.files @@ -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 diff --git a/projects/linux/ee.includes b/projects/linux/ee.includes index 1e8d06186..606425468 100644 --- a/projects/linux/ee.includes +++ b/projects/linux/ee.includes @@ -3,3 +3,4 @@ ../../src/thirdparty ../../include/eepp/thirdparty ../../src/eepp/system +../../include/eepp/system diff --git a/src/eepp/system/iostreamzip.cpp b/src/eepp/system/iostreamzip.cpp new file mode 100644 index 000000000..d159102e5 --- /dev/null +++ b/src/eepp/system/iostreamzip.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include + +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 (&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; +} + +}} diff --git a/src/eepp/system/pak.cpp b/src/eepp/system/pak.cpp index 91e9b4e64..7abac4e98 100755 --- a/src/eepp/system/pak.cpp +++ b/src/eepp/system/pak.cpp @@ -374,4 +374,8 @@ std::string Pak::getPackPath() { return mPak.pakPath; } +IOStream * Pak::getFileStream(const std::string & path) { + return NULL; +} + }} diff --git a/src/eepp/system/zip.cpp b/src/eepp/system/zip.cpp index 6bbb93b1c..442bd6160 100644 --- a/src/eepp/system/zip.cpp +++ b/src/eepp/system/zip.cpp @@ -2,6 +2,7 @@ #include #include #include +#include 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; +} + }}