diff --git a/include/eepp/system.hpp b/include/eepp/system.hpp index 6a288324e..47d94a3db 100644 --- a/include/eepp/system.hpp +++ b/include/eepp/system.hpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include diff --git a/include/eepp/system/directorypack.hpp b/include/eepp/system/directorypack.hpp new file mode 100644 index 000000000..497a7024d --- /dev/null +++ b/include/eepp/system/directorypack.hpp @@ -0,0 +1,76 @@ +#ifndef EE_SYSTEM_DIRECTORYPACK_HPP +#define EE_SYSTEM_DIRECTORYPACK_HPP + +#include + +namespace EE { namespace System { + +/** @brief A local file system directory. */ +class EE_API DirectoryPack : public Pack { + public: + DirectoryPack(); + + ~DirectoryPack(); + + /** Creates a new pack file */ + bool create( const std::string& path ); + + /** Open a pack file */ + bool open( const std::string& path ); + + /** Close the pack file */ + bool close(); + + /** Add a file to the pack file + * @param path Path to the file in the disk + * @param inpack Path that will have the file inside the pak + * @return True if success + */ + bool addFile( const std::string& path, const std::string& inpack ); + + /** Add a new file from memory */ + bool addFile( const Uint8 * data, const Uint32& dataSize, const std::string& inpack ); + + /** Add a new file from memory */ + bool addFile( std::vector& data, const std::string& inpack ); + + /** Add a map of files to the pack file ( myMap[ myFilepath ] = myInPackFilepath ) */ + bool addFiles( std::map paths ); + + /** Erase a file from the pack file.*/ + bool eraseFile( const std::string& path ); + + /** Erase all passed files from the pack file. */ + bool eraseFiles( const std::vector& paths ); + + /** Extract a file from the pack file */ + bool extractFile( const std::string& path , const std::string& dest ); + + /** Extract a file to memory from the pack file */ + bool extractFileToMemory( const std::string& path, std::vector& data ); + + /** Extract a file to memory from the pakFile */ + bool extractFileToMemory( const std::string& path, SafeDataPointer& data ); + + /** Check if a file exists in the pack file and return the number of the file, otherwise return -1. */ + Int32 exists( const std::string& path ); + + /** Check the integrity of the pack file. \n If return 0 integrity OK. -1 wrong indentifier. -2 wrong header. */ + Int8 checkPack(); + + /** @return a vector with all the files inside the pack file */ + std::vector getFileList(); + + /** @return The file path of the opened package */ + std::string getPackPath(); + + IOStream * getFileStream( const std::string& path ); + protected: + std::string mPath; + + void getDirectoryFiles(std::vector & files, std::string directory); +}; + +}} +#endif + diff --git a/projects/linux/ee.files b/projects/linux/ee.files index dc787746f..eb9efbdb0 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/directorypack.hpp ../../include/eepp/system/iostreampak.hpp ../../include/eepp/system/iostreamzip.hpp ../../include/eepp/system/translator.hpp @@ -83,6 +84,7 @@ ../../src/eepp/math/interpolation1d.cpp ../../src/eepp/math/interpolation2d.cpp ../../src/eepp/system/color.cpp +../../src/eepp/system/directorypack.cpp ../../src/eepp/system/iostreampak.cpp ../../src/eepp/system/iostreamzip.cpp ../../src/eepp/system/translator.cpp diff --git a/src/eepp/system/directorypack.cpp b/src/eepp/system/directorypack.cpp new file mode 100644 index 000000000..e4f76f89b --- /dev/null +++ b/src/eepp/system/directorypack.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + +namespace EE { namespace System { + +DirectoryPack::DirectoryPack() {} + +DirectoryPack::~DirectoryPack() {} + +bool DirectoryPack::create( const std::string& path ) { + if ( !FileSystem::isDirectory( path ) ) { + FileSystem::makeDir( path ); + } + + return open( path ); +} + +bool DirectoryPack::open( const std::string& path ) { + if ( FileSystem::isDirectory( path ) ) { + mPath = path; + + #if EE_PLATFORM == EE_PLATFORM_WIN + if ( mPath.find_first_of( '\\' ) != std::string::npos ) { + String::replaceAll( mPath, "\\", "/" ); + } + #endif + + if ( mPath.size() && mPath[ path.size() - 1 ] != '/' ) + mPath += "/"; + + mIsOpen = true; + + onPackOpened(); + + return true; + } + + return false; +} + +bool DirectoryPack::close() { + mPath = ""; + onPackClosed(); + return true; +} + +bool DirectoryPack::addFile( const std::string& path, const std::string& inpack ) { + return FileSystem::fileCopy( path, mPath + inpack ); +} + +bool DirectoryPack::addFile( const Uint8 * data, const Uint32 & dataSize, const std::string& inpack ) { + return FileSystem::fileWrite( mPath + inpack, data, dataSize ); +} + +bool DirectoryPack::addFile( std::vector& data, const std::string& inpack ) { + return FileSystem::fileWrite( mPath + inpack, data ); +} + +bool DirectoryPack::addFiles( std::map paths ) { + for( std::map::iterator itr = paths.begin(); itr != paths.end(); itr++) + if ( !addFile( itr->first, itr->second ) ) + return false; + return true; +} + +bool DirectoryPack::eraseFile( const std::string& path ) { + return FileSystem::fileRemove( mPath + path ); +} + +bool DirectoryPack::eraseFiles( const std::vector& paths ) { + for ( auto it = paths.begin(); it != paths.end(); ++it ) { + if ( !eraseFile( mPath + (*it) ) ) + return false; + } + return true; +} + +bool DirectoryPack::extractFile( const std::string& path, const std::string& dest) { + return FileSystem::fileCopy( mPath + path, dest ); +} + +bool DirectoryPack::extractFileToMemory( const std::string& path, std::vector& data ) { + return FileSystem::fileGet( mPath + path, data ); +} + +bool DirectoryPack::extractFileToMemory( const std::string& path, SafeDataPointer& data ) { + return FileSystem::fileGet( mPath + path, data ); +} + +Int32 DirectoryPack::exists( const std::string& path ) { + return FileSystem::fileExists( mPath + path ) ? 0 : -1; +} + +Int8 DirectoryPack::checkPack() { + return mPath.empty() ? -1 : 1; +} + +void DirectoryPack::getDirectoryFiles( std::vector& files, std::string directory ) { + std::vector pathFiles = FileSystem::filesGetInPath( directory ); + std::string path( directory ); + + if ( String::startsWith( path, mPath ) && mPath.length() <= path.size() ) { + path = path.substr( mPath.length() ); + } + + for ( auto it = pathFiles.begin(); it != pathFiles.end(); ++it ) { + if ( FileSystem::isDirectory( directory + (*it) ) ) { + getDirectoryFiles( files, directory + (*it) + "/" ); + } else { + files.push_back( path + (*it) ); + } + } +} + +std::vector DirectoryPack::getFileList() { + std::vector files; + + if ( !mPath.empty() ) { + getDirectoryFiles( files, mPath ); + } + + return files; +} + +std::string DirectoryPack::getPackPath() { + return mPath; +} + +IOStream * DirectoryPack::getFileStream( const std::string & path ) { + return eeNew( IOStreamFile, ( path ) ); +} + +}} diff --git a/src/eepp/system/inifile.cpp b/src/eepp/system/inifile.cpp index f34845866..6778c56c5 100755 --- a/src/eepp/system/inifile.cpp +++ b/src/eepp/system/inifile.cpp @@ -45,7 +45,7 @@ IniFile::IniFile( Pack * Pack, std::string iniPackPath, const bool& shouldReadFi } bool IniFile::loadFromPack( Pack * Pack, std::string iniPackPath ) { - if ( NULL != Pack && Pack->isOpen() && Pack->exists( iniPackPath ) ) { + if ( NULL != Pack && Pack->isOpen() && -1 != Pack->exists( iniPackPath ) ) { SafeDataPointer PData; Pack->extractFileToMemory( iniPackPath, PData );