#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2013 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License // If the library is compiled as a DLL, this ensures symbols are exported #define NUCLEX_STORAGE_SOURCE 1 #include "ZippedContainer.h" #include "ZippedFile.h" #include "Nuclex/Storage/FileSystem/ContainerFileCodec.h" #include "../../Helpers/StringHelper.h" #include #include namespace Nuclex { namespace Storage { namespace FileSystem { namespace Zip { // ------------------------------------------------------------------------------------------- // ZippedContainer::ZippedContainer( const std::shared_ptr &codecs, const std::string &nativePath, const std::string &name ) : Container(codecs), nativePath(nativePath), name(name) {} // ------------------------------------------------------------------------------------------- // ZippedContainer::~ZippedContainer() {} // ------------------------------------------------------------------------------------------- // std::vector ZippedContainer::GetContainers( const std::string &wildcard /* = std::wstring() */ ) const { std::vector containers; if(wildcard.empty()) { containers.reserve(this->containers.size()); ZippedContainerMap::const_iterator iterator = this->containers.begin(); for(; iterator != this->containers.end(); ++iterator) { containers.push_back(iterator->second); } } else { ZippedContainerMap::const_iterator iterator = this->containers.begin(); for(; iterator != this->containers.end(); ++iterator) { if(Helpers::StringHelper::MatchesWildcard(iterator->first, wildcard)) { containers.push_back(iterator->second); } } } return containers; } // ------------------------------------------------------------------------------------------- // std::vector ZippedContainer::GetContainers( const std::string &wildcard /* = std::wstring() */ ) { std::vector containers; if(wildcard.empty()) { containers.reserve(this->containers.size()); ZippedContainerMap::const_iterator iterator = this->containers.begin(); for(; iterator != this->containers.end(); ++iterator) { containers.push_back(iterator->second); } } else { ZippedContainerMap::const_iterator iterator = this->containers.begin(); for(; iterator != this->containers.end(); ++iterator) { if(Helpers::StringHelper::MatchesWildcard(iterator->first, wildcard)) { containers.push_back(iterator->second); } } } return containers; } // ------------------------------------------------------------------------------------------- // ZippedContainer::ConstContainerPointer ZippedContainer::GetContainer( const std::string &name ) const { ZippedContainerMap::const_iterator iterator = this->containers.find(name); if(iterator == this->containers.end()) { throw std::runtime_error("No such container"); } return iterator->second; } // ------------------------------------------------------------------------------------------- // ZippedContainer::ContainerPointer ZippedContainer::GetContainer( const std::string &name ) { ZippedContainerMap::const_iterator iterator = this->containers.find(name); if(iterator == this->containers.end()) { throw std::runtime_error("No such container"); } return iterator->second; } // ------------------------------------------------------------------------------------------- // bool ZippedContainer::HasContainer(const std::string &name) const { ZippedContainerMap::const_iterator iterator = this->containers.find(name); if(iterator == this->containers.end()) { ZippedFileMap::const_iterator iterator = this->files.find(name); if(iterator == this->files.end()) { return false; } else { return this->Codecs->CanOpenAsContainer(iterator->second); } } else { return true; } } // ------------------------------------------------------------------------------------------- // ZippedContainer::ContainerPointer ZippedContainer::CreateContainer(const std::string &) { throw std::runtime_error("Zip archives cannot be modified"); } // ------------------------------------------------------------------------------------------- // bool ZippedContainer::DeleteContainer(const std::string &) { throw std::runtime_error("Zip archives cannot be modified"); } // ------------------------------------------------------------------------------------------- // std::vector ZippedContainer::GetFiles( const std::string &wildcard /* = std::string() */ ) const { std::vector files; if(wildcard.empty()) { files.reserve(this->files.size()); ZippedFileMap::const_iterator iterator = this->files.begin(); for(; iterator != this->files.end(); ++iterator) { files.push_back(iterator->second); } } else { ZippedFileMap::const_iterator iterator = this->files.begin(); for(; iterator != this->files.end(); ++iterator) { if(Helpers::StringHelper::MatchesWildcard(iterator->first, wildcard)) { files.push_back(iterator->second); } } } return files; } // ------------------------------------------------------------------------------------------- // std::vector ZippedContainer::GetFiles( const std::string &wildcard /* = std::string() */ ) { std::vector files; if(wildcard.empty()) { files.reserve(this->files.size()); ZippedFileMap::const_iterator iterator = this->files.begin(); for(; iterator != this->files.end(); ++iterator) { files.push_back(iterator->second); } } else { ZippedFileMap::const_iterator iterator = this->files.begin(); for(; iterator != this->files.end(); ++iterator) { if(Helpers::StringHelper::MatchesWildcard(iterator->first, wildcard)) { files.push_back(iterator->second); } } } return files; } // ------------------------------------------------------------------------------------------- // ZippedContainer::ConstFilePointer ZippedContainer::GetFile( const std::string &name ) const { ZippedFileMap::const_iterator iterator = this->files.find(name); if(iterator == this->files.end()) { throw std::runtime_error("No such file"); } return iterator->second; } // ------------------------------------------------------------------------------------------- // ZippedContainer::FilePointer ZippedContainer::GetFile(const std::string &name) { ZippedFileMap::const_iterator iterator = this->files.find(name); if(iterator == this->files.end()) { throw std::runtime_error("No such file"); } return iterator->second; } // ------------------------------------------------------------------------------------------- // bool ZippedContainer::HasFile(const std::string &name) const { ZippedFileMap::const_iterator iterator = this->files.find(name); return (iterator != this->files.end()); } // ------------------------------------------------------------------------------------------- // ZippedContainer::FilePointer ZippedContainer::CreateFile(const std::string &) { throw std::runtime_error("Zip archives cannot be modified"); } // ------------------------------------------------------------------------------------------- // bool ZippedContainer::DeleteFile(const std::string &) { throw std::runtime_error("Zip archives cannot be modified"); } // ------------------------------------------------------------------------------------------- // void ZippedContainer::AddContainer(const std::shared_ptr &container) { this->containers.insert(ZippedContainerMap::value_type(container->GetName(), container)); } // ------------------------------------------------------------------------------------------- // void ZippedContainer::AddFile(const std::shared_ptr &file) { this->files.insert(ZippedFileMap::value_type(file->GetName(), file)); } // ------------------------------------------------------------------------------------------- // }}}} // namespace Nuclex::Storage::FileSystem::Zip