collcoll.cpp
Go to the documentation of this file.
#include "zipios++/zipios-config.h"
#include "zipios++/meta-iostreams.h"
#include "zipios++/collcoll.h"
#include "zipios_common.h"
namespace zipios {
using std::ifstream ;
CollectionCollection *CollectionCollection::_inst = 0 ;
CollectionCollection::CollectionCollection() {
_valid = true ; // we're valid even though we are empty!
}
bool CollectionCollection::addCollection( const FileCollection &collection ) {
if ( ! _valid )
throw InvalidStateException( "Attempt to add a FileCollection to an invalid CollectionCollection" ) ;
if ( this == &collection || ! collection.isValid() )
return false ;
_collections.push_back( collection.clone() ) ;
return true ;
}
bool CollectionCollection::addCollection( FileCollection *collection ) {
if ( ! _valid )
throw InvalidStateException( "Attempt to add a FileCollection to an invalid CollectionCollection" ) ;
if ( collection == 0 || this == collection || ! collection->isValid() )
return false ;
_collections.push_back( collection ) ;
return true ;
}
void CollectionCollection::close() {
_valid = false ;
}
ConstEntries CollectionCollection::entries() const {
if ( ! _valid )
throw InvalidStateException( "Attempt to get entries from an invalid CollectionCollection" ) ;
ConstEntries all_entries ;
std::vector< FileCollection * >::const_iterator it ;
for ( it = _collections.begin() ; it != _collections.end() ; it++ )
all_entries += (*it)->entries() ;
return all_entries ;
}
ConstEntryPointer CollectionCollection::getEntry( const string &name,
MatchPath matchpath ) const {
if ( ! _valid )
throw InvalidStateException( "Attempt to get an entry from an invalid CollectionCollection" ) ;
// Returns the first matching entry.
std::vector< FileCollection * >::const_iterator it ;
ConstEntryPointer cep ;
getEntry( name, cep, it, matchpath ) ;
return cep ;
}
istream *CollectionCollection::getInputStream( const ConstEntryPointer &entry ) {
if ( ! _valid )
throw InvalidStateException( "Attempt to get an input stream from an invalid CollectionCollection" ) ;
return getInputStream( entry->getName() ) ;
}
istream *CollectionCollection::getInputStream( const string &entry_name,
MatchPath matchpath ) {
if ( ! _valid )
throw InvalidStateException( "Attempt to get an input stream from an invalid CollectionCollection" ) ;
std::vector< FileCollection * >::const_iterator it ;
ConstEntryPointer cep ;
getEntry( entry_name, cep, it, matchpath ) ;
if ( cep == 0 )
return 0 ;
else
return (*it)->getInputStream( entry_name ) ;
}
int CollectionCollection::size() const {
if ( ! _valid )
throw InvalidStateException( "Attempt to get the size of an invalid CollectionCollection" ) ;
int sz = 0 ;
std::vector< FileCollection * >::const_iterator it ;
for ( it = _collections.begin() ; it != _collections.end() ; it++ )
sz += (*it)->size() ;
return sz ;
}
FileCollection *CollectionCollection::clone() const {
return new CollectionCollection( *this ) ;
}
CollectionCollection::~CollectionCollection() {
std::vector< FileCollection * >::iterator it ;
for ( it = _collections.begin() ; it != _collections.end() ; ++it )
delete *it ;
}
//
// Protected member functions
//
void CollectionCollection::getEntry( const string &name,
ConstEntryPointer &cep,
std::vector< FileCollection * >::const_iterator &it,
MatchPath matchpath ) const {
// Returns the first matching entry.
cep = 0 ;
for ( it = _collections.begin() ; it != _collections.end() ; it++ ) {
cep = (*it)->getEntry( name, matchpath ) ;
if ( cep )
break ;
}
}
} // namespace
/*
Zipios++ - a small C++ library that provides easy access to .zip files.
Copyright (C) 2000 Thomas Søndergaard
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 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 GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
zipios::CollectionCollection
CollectionCollection is a FileCollection that consists of an arbitrary number of FileCollections.
Definition collcoll.h:26
zipios::CollectionCollection::entries
virtual ConstEntries entries() const
Returns a vector of const pointers to the entries in the FileCollection.
Definition collcoll.cpp:46
zipios::CollectionCollection::getEntry
virtual ConstEntryPointer getEntry(const string &name, MatchPath matchpath=MATCH) const
Returns a ConstEntryPointer to a FileEntry object for the entry with the specified name.
Definition collcoll.cpp:58
zipios::CollectionCollection::addCollection
bool addCollection(const FileCollection &collection)
Adds a collection.
Definition collcoll.cpp:21
zipios::CollectionCollection::clone
virtual FileCollection * clone() const
Create a heap allocated clone of the object this method is called for.
Definition collcoll.cpp:108
zipios::CollectionCollection::getInputStream
virtual istream * getInputStream(const ConstEntryPointer &entry)
Returns a pointer to an opened istream for the specified FileEntry.
Definition collcoll.cpp:72
zipios::CollectionCollection::CollectionCollection
CollectionCollection()
Constructor.
Definition collcoll.cpp:16
zipios::CollectionCollection::size
virtual int size() const
Returns the number in entries in all collections kept by the CollectionCollection object.
Definition collcoll.cpp:98
zipios::CollectionCollection::close
virtual void close()
Closes the FileCollection.
Definition collcoll.cpp:41
zipios::FileCollection::FileCollection
FileCollection()
FileCollection constructor.
Definition fcoll.h:24
zipios::FileCollection::isValid
bool isValid() const
The member function returns true if the collection is valid.
Definition fcoll.h:93
zipios::FileCollection::clone
virtual FileCollection * clone() const =0
Create a heap allocated clone of the object this method is called for.
zipios::FileEntry::getName
virtual string getName() const =0
Returns the full filename of the entry, including a path if the entry is stored in a subfolder.
zipios::InvalidStateException
An object member function may throw this exception, if the operation it normally performs is inapprop...
Definition fcollexceptions.h:47
collcoll.h
Header file that defines CollectionCollection.
zipios::ConstEntryPointer
SimpleSmartPointer< const FileEntry > ConstEntryPointer
ConstEntryPointer is a SimpleSmartPointer for const FileEntry's.
Definition fileentry.h:37
zipios::ConstEntries
vector< EntryPointer > ConstEntries
ConstEntries is a vector of ConstEntryPointer's.
Definition fileentry.h:43
zipios_common.h
Header file containing miscellaneous small functions.