zipios++  2.0.2
Zipios++ – a small C++ library that provides easy access to .zip files.
Public Types | Public Member Functions | Protected Attributes | List of all members
zipios::CollectionCollection Class Reference

A collection of collections. More...

#include <collectioncollection.hpp>

Inheritance diagram for zipios::CollectionCollection:
Inheritance graph
[legend]
Collaboration diagram for zipios::CollectionCollection:
Collaboration graph
[legend]

Public Types

enum  MatchPath : uint32_t {
  MatchPath::IGNORE,
  MatchPath::MATCH
}
 
typedef std::shared_ptr
< FileCollection
pointer_t
 
typedef std::shared_ptr
< std::istream > 
stream_pointer_t
 A shared pointer to an input stream. More...
 
typedef std::vector< pointer_tvector_t
 

Public Member Functions

 CollectionCollection ()
 Initialize a CollectionCollection object. More...
 
 CollectionCollection (CollectionCollection const &src)
 Copy a CollectionCollection in another. More...
 
virtual ~CollectionCollection () override
 Clean up this CollectionCollection object. More...
 
bool addCollection (FileCollection const &collection)
 Add a FileCollection to this CollectionCollection. More...
 
bool addCollection (FileCollection::pointer_t collection)
 Add a collection to this CollectionCollection. More...
 
virtual void addEntry (FileEntry const &entry)
 Add an entry to this collection. More...
 
virtual pointer_t clone () const override
 Create a clone of this object. More...
 
virtual void close () override
 Close the CollectionCollection object. More...
 
virtual FileEntry::vector_t entries () const override
 Retrieve a vector to all the collection entries. More...
 
virtual FileEntry::pointer_t getEntry (std::string const &name, MatchPath matchpath=MatchPath::MATCH) const override
 Get an entry from the collection. More...
 
virtual stream_pointer_t getInputStream (std::string const &entry_name, MatchPath matchpath=MatchPath::MATCH) override
 Retrieve pointer to an istream. More...
 
virtual std::string getName () const
 Returns the name of the FileCollection. More...
 
bool isValid () const
 Check whether the current collection is valid. More...
 
virtual void mustBeValid () const
 Check whether the collection is valid. More...
 
CollectionCollectionoperator= (CollectionCollection const &src)
 Copy assignment operator. More...
 
void setLevel (size_t limit, FileEntry::CompressionLevel small_compression_level, FileEntry::CompressionLevel large_compression_level)
 Change the compression level to the specified value. More...
 
void setMethod (size_t limit, StorageMethod small_storage_method, StorageMethod large_storage_method)
 Change the storage method to the specified value. More...
 
virtual size_t size () const override
 Return the size of the of this collection. More...
 

Protected Attributes

vector_t m_collections
 
FileEntry::vector_t m_entries
 
std::string m_filename
 
bool m_valid = true
 

Detailed Description

CollectionCollection is a FileCollection that consists of an arbitrary number of FileCollection's. With a CollectionCollection the user can use multiple FileCollection objects transparently, making it easy for a program to keep some of its files in a zip archive and others stored in ordinary files. CollectionCollection can be used to create a simple virtual filesystem, where all collections are mounted on /. If more than one collection contain a file with the same path only the one in the first added collection is accessible.

Definition at line 40 of file collectioncollection.hpp.

Member Typedef Documentation

typedef std::shared_ptr<FileCollection> zipios::FileCollection::pointer_t
inherited

Definition at line 43 of file filecollection.hpp.

std::shared_ptr< std::istream > zipios::FileCollection::stream_pointer_t
inherited

This type of pointer is used whenever you retrieve an input stream from a file collection such as the ZipFile class. Having shared pointers ensures that the pointers can be shared between various functions and it gets deleted in the end.

Definition at line 45 of file filecollection.hpp.

typedef std::vector<pointer_t> zipios::FileCollection::vector_t
inherited

Definition at line 44 of file filecollection.hpp.

Member Enumeration Documentation

enum zipios::FileCollection::MatchPath : uint32_t
stronginherited
Enumerator
IGNORE 
MATCH 

Definition at line 47 of file filecollection.hpp.

Constructor & Destructor Documentation

zipios::CollectionCollection::CollectionCollection ( )
explicit

The constructor initializes the CollectionCollection as a valid collection.

Definition at line 97 of file collectioncollection.cpp.

References zipios::FileCollection::m_valid.

Referenced by clone().

zipios::CollectionCollection::CollectionCollection ( CollectionCollection const &  src)

This function copies a collection of collections in another. Note that all the children get cloned so the copy can be edited without modify the source and vice versa.

Parameters
[in]srcThe source to copy in the new CollectionCollection.

Definition at line 111 of file collectioncollection.cpp.

References m_collections.

zipios::CollectionCollection::~CollectionCollection ( )
overridevirtual

This function ensures that the CollectionCollection object is cleaned up before deallcoating the memory.

Definition at line 172 of file collectioncollection.cpp.

References close().

Member Function Documentation

bool zipios::CollectionCollection::addCollection ( FileCollection const &  collection)

This function adds a collection in this CollectionCollection. Since a CollectionCollection is itself a FileCollection, you may add a CollectionCollection to another CollectionCollection.

Note
The FileCollection to be added must be valid or it will be ignored.
Parameters
[in]collectionThe collection to add.
Returns
true if the collection was added succesfully.
See Also
addCollection(FileCollection::pointer_t collection);
Todo:
At this time the function verifies that you are not trying to add a CollectionCollection to itself. However, this test is currently really weak. We need to check whether any collection in the input collection represents this collection.

Definition at line 193 of file collectioncollection.cpp.

References zipios::FileCollection::clone(), zipios::FileCollection::isValid(), m_collections, and mustBeValid().

Referenced by addCollection().

bool zipios::CollectionCollection::addCollection ( FileCollection::pointer_t  collection)

This function adds the collection pointed to by collection to this CollectionCollection.

The CollectionCollection makes a clone of the specified collection to make sure management of the child collection works as expected.

If the collection does not get added, the function returns false. This happens when the collection parameter represents an invalid collection.

Exceptions
InvalidExceptionThe function raises InvalidException if the collection parameter is a null pointer.
Parameters
[in]collectionA pointer to the collection to add.
Returns
true if the collection was added succesfully.
See Also
addCollection(FileCollection const& collection);

Definition at line 236 of file collectioncollection.cpp.

References addCollection().

void zipios::FileCollection::addEntry ( FileEntry const &  entry)
virtualinherited

This function adds an entry to the file collection allowing you to create a FileCollection from the exact files you want to have in the collection instead of having to read an entire directory as the DirectoryCollection offers by default.

Warning
This function creates a clone of the entry to make sure that the caller's entry can be modified without affecting the FileCollection.
Parameters
[in]entryThe entry to add to the FileCollection.

Definition at line 368 of file filecollection.cpp.

References zipios::FileEntry::clone(), and zipios::FileCollection::m_entries.

FileCollection::pointer_t zipios::CollectionCollection::clone ( ) const
overridevirtual

This function creates a heap allocated clone of the CollectionCollection.

Note that all the collections that this CollectionCollection points to are all going to get cloned.

Returns
A shared pointer to a copy of this CollectionCollection.

Implements zipios::FileCollection.

Definition at line 161 of file collectioncollection.cpp.

References CollectionCollection().

void zipios::CollectionCollection::close ( )
overridevirtual

This function marks the collection as invalid in effect rendering the collection unusable. Note that all the collections that you previously added to this collection all get marked as invalid (i.e. their close() function gets called.) This has the nice side effect to release memory immediately.

Note
This is different from creating an empty CollectionCollection which is empty and valid.

Reimplemented from zipios::FileCollection.

Definition at line 260 of file collectioncollection.cpp.

References zipios::FileCollection::close(), and m_collections.

Referenced by ~CollectionCollection().

FileEntry::vector_t zipios::CollectionCollection::entries ( ) const
overridevirtual

This function gathers the entries of all the children collections and add them to a vector that it then returns.

The CollectionCollection itself has no entries.

It is possible to define a CollectionCollection as a child of another CollectionCollection. The process repeats infinitum as required.

Returns
A copy of all the entries found in the child Collections.

Reimplemented from zipios::FileCollection.

Definition at line 293 of file collectioncollection.cpp.

References m_collections, and mustBeValid().

FileEntry::pointer_t zipios::CollectionCollection::getEntry ( std::string const &  name,
MatchPath  matchpath = MatchPath::MATCH 
) const
overridevirtual

This function returns a shared pointer to a FileEntry object for the entry with the specified name. To ignore the path part of the filename while searching for a match, specify FileCollection::MatchPath::IGNORE as the second argument. (the default is FileCollection::MatchPath::MATCH.

Warning
In case of the CollectionCollection, the matching goes from child collection to child collection in the order they were added to the CollectionCollection. The first match is returned and at this point there is nothing linking a FileEntry to its collection so you will NOT be able to retrieve an istream to access that FileEntry data. To do that, you must directly call the getInputStream() function. We may fix that problem at a later time and offer the getInputStream directly on the FileEntry instead of the collection. This is problematic at this point since, as we can see in the zipfile.cpp, we need to have access to the m_zs offset.
Note
The collection must be valid or the function raises an exception.
Parameters
[in]nameA string containing the name of the entry to get.
[in]matchpathSpeficy MatchPath::MATCH, if the path should match as well, specify MatchPath::IGNORE, if the path should be ignored.
Returns
A shared pointer to the found entry. The returned pointer is null if no entry is found.
See Also
mustBeValid()

Reimplemented from zipios::FileCollection.

Definition at line 341 of file collectioncollection.cpp.

References m_collections, zipios::anonymous_namespace{collectioncollection.cpp}::matchEntry(), and mustBeValid().

CollectionCollection::stream_pointer_t zipios::CollectionCollection::getInputStream ( std::string const &  entry_name,
MatchPath  matchpath = MatchPath::MATCH 
)
overridevirtual

This function returns a shared pointer to an istream defined from the named entry, which is expected to be available in this collection.

The function returns a NULL pointer if there is no entry with the specified name in this CollectionCollection. Note that the name is searched in all the child collections of the CollectionCollection.

Note that the function returns a smart pointer to an istream. In general the CollectionCollection will not hold a copy of that pointer meaning that if you call getInputStream() multiple times with the same entry_name parameter, you get distinct istream instances each time.

By default the entry_name parameter is expected to match the full path and filename (MatchPath::MATCH). If you are looking for a file and want to ignore the directory name, set the matchpath parameter to MatchPath::IGNORE.

Parameters
[in]entry_nameThe name of the file to search in the collection.
[in]matchpathWhether the full path or just the filename is matched.
Returns
A shared pointer to an open istream for the specified entry.
See Also
FileCollection
DirectoryCollection
ZipFile

Implements zipios::FileCollection.

Definition at line 384 of file collectioncollection.cpp.

References m_collections, zipios::anonymous_namespace{collectioncollection.cpp}::matchEntry(), and mustBeValid().

std::string zipios::FileCollection::getName ( ) const
virtualinherited

This function returns the filename of the collection as a whole.

Note
The collection my be valid.
Returns
The name of the FileCollection.
See Also
mustBeValid()

Definition at line 456 of file filecollection.cpp.

References zipios::FileCollection::m_filename, and zipios::FileCollection::mustBeValid().

Referenced by zipios::operator<<().

bool zipios::FileCollection::isValid ( ) const
inherited

This function returns true if the collection is valid.

Note that by default (just after a new) a collection is not considered valid.

Returns
true if the collection is valid.

Definition at line 493 of file filecollection.cpp.

References zipios::FileCollection::m_valid.

Referenced by addCollection().

void zipios::CollectionCollection::mustBeValid ( ) const
virtual

This function verifies that the collection is valid. If not, an exception is raised. Many other functions from the various collection functions are calling this function before accessing data.

Exceptions
InvalidStateExceptionThis exception is raised if the m_valid field is currently false and thus most of the collection data is considered invalid.

Reimplemented from zipios::FileCollection.

Definition at line 432 of file collectioncollection.cpp.

References m_collections, and zipios::FileCollection::mustBeValid().

Referenced by addCollection(), entries(), getEntry(), getInputStream(), and size().

CollectionCollection & zipios::CollectionCollection::operator= ( CollectionCollection const &  rhs)

This assignment operator copies rhs to this collection replacing the file entries that exist in this collection.

Note that the source file entries are cloned in the destination so modifying this collection will not modify the source.

Parameters
[in]rhsThe source to copy in this collection.

Definition at line 132 of file collectioncollection.cpp.

References m_collections, and zipios::FileCollection::operator=().

void zipios::FileCollection::setLevel ( size_t  limit,
FileEntry::CompressionLevel  small_compression_level,
FileEntry::CompressionLevel  large_compression_level 
)
inherited

This function changes the compression level of all the entries in this collection to the specified value.

The size limit is used to know which compression level to use: small_compression_level for any file that has a size smaller or equal to the specified limit and large_compression_level for the others.

Parameters
[in]limitThe threshold to use to define the compression level.
[in]small_compression_levelThe compression level for smaller files.
[in]large_compression_levelThe compression level for larger files.

Definition at line 567 of file filecollection.cpp.

References zipios::FileCollection::entries(), zipios::FileCollection::m_entries, and zipios::FileCollection::mustBeValid().

void zipios::FileCollection::setMethod ( size_t  limit,
StorageMethod  small_storage_method,
StorageMethod  large_storage_method 
)
inherited

This function changes the storage method of all the entries in this collection to the specified value.

The size limit is used to know which storage method to use: small_storage_method for any file that has a size smaller or equal to the specified limit and large_storage_method for the others.

Parameters
[in]limitThe threshold to use to define the compression level.
[in]small_storage_methodThe storage method for smaller files.
[in]large_storage_methodThe storage method for larger files.

Definition at line 532 of file filecollection.cpp.

References zipios::FileCollection::entries(), zipios::FileCollection::m_entries, and zipios::FileCollection::mustBeValid().

size_t zipios::CollectionCollection::size ( ) const
overridevirtual

This function computes the total size of this collection which is to sum of the size of its child collections.

Warning
This function has the side effect of loading all the data from DirectoryCollection objects.
Returns
The total size of the collection.

Reimplemented from zipios::FileCollection.

Definition at line 408 of file collectioncollection.cpp.

References m_collections, and mustBeValid().

Member Data Documentation

vector_t zipios::CollectionCollection::m_collections
protected
FileEntry::vector_t zipios::FileCollection::m_entries
protectedinherited
std::string zipios::FileCollection::m_filename
protectedinherited
bool zipios::FileCollection::m_valid = true
protectedinherited

The documentation for this class was generated from the following files: