dircoll.cpp
Go to the documentation of this file.
#include "zipios++/zipios-config.h"
#include "zipios++/meta-iostreams.h"
#include <vector>
#include <sys/stat.h>
#include <stddef.h>
#include "zipios++/dircoll.h"
#include "directory.h"
namespace zipios {
using std::cerr ;
using std::endl ;
using std::vector ;
using std::ifstream ;
DirectoryCollection::DirectoryCollection( const string &path, bool recursive,
bool load_now )
: _entries_loaded( false ),
_recursive ( recursive ),
_filepath ( path )
{
_filename = _filepath ;
_valid = _filepath.isDirectory() ;
if( _valid && load_now )
loadEntries() ;
}
void DirectoryCollection::close() {
_valid = false ;
}
ConstEntries DirectoryCollection::entries() const {
if ( ! _valid )
throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
loadEntries() ;
return FileCollection::entries() ;
}
ConstEntryPointer
DirectoryCollection::getEntry( const string &name,
MatchPath matchpath ) const {
if ( ! _valid )
throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
if ( matchpath != MATCH || _entries_loaded ) {
loadEntries() ;
return FileCollection::getEntry( name, matchpath ) ;
} else {
// avoid loading entries if possible.
ConstEntryPointer ent ( new DirEntry( name, "", _filepath ) ) ;
if ( ent->isValid() )
return ent ;
else
return 0 ;
}
}
istream *DirectoryCollection::getInputStream( const ConstEntryPointer &entry ) {
if ( ! _valid )
throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
return getInputStream( entry->getName() ) ;
}
istream *DirectoryCollection::getInputStream( const string &entry_name,
MatchPath matchpath ) {
if ( ! _valid )
throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
if ( matchpath != MATCH || _entries_loaded ) {
loadEntries() ;
ConstEntryPointer ent = getEntry( entry_name, matchpath ) ;
if ( ent == 0 )
return 0 ;
else {
string real_path( _filepath + entry_name ) ;
return new ifstream( real_path.c_str(), ios::in | ios::binary ) ;
}
} else {
// avoid loading entries if possible.
string real_path( _filepath + entry_name ) ;
ifstream *ifs = new ifstream( real_path.c_str(), ios::in | ios::binary ) ;
if( ! *ifs ) {
delete ifs ;
return 0 ;
} else
return ifs ;
}
}
int DirectoryCollection::size() const {
if ( ! _valid )
throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
loadEntries() ;
return _entries.size() ;
}
FileCollection *DirectoryCollection::clone() const {
return new DirectoryCollection( *this ) ;
}
DirectoryCollection::~DirectoryCollection() {}
void DirectoryCollection::loadEntries() const {
if( _entries_loaded )
return ;
const_cast< DirectoryCollection * >( this )->load( _recursive ) ;
_entries_loaded = true ;
}
void DirectoryCollection::load( bool recursive, const FilePath &subdir ) {
using namespace boost::filesystem ;
BasicEntry *ent ;
for ( dir_it it( _filepath + subdir ) ; it != dir_it() ; ++it ) {
if ( *it == "." || *it == ".." || *it == "..." )
continue ;
if ( get< is_directory >( it ) && recursive ) {
load( recursive, subdir + *it ) ;
} else {
_entries.push_back( ent = new BasicEntry( subdir + *it, "", _filepath ) ) ;
ent->setSize( get< boost::filesystem::size >( it ) ) ;
}
}
}
} // 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::DirectoryCollection
DirectoryCollection is a FileCollection that obtains its entries from a directory.
Definition dircoll.h:19
zipios::DirectoryCollection::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 dircoll.cpp:50
zipios::DirectoryCollection::getInputStream
virtual istream * getInputStream(const ConstEntryPointer &entry)
Returns a pointer to an opened istream for the specified FileEntry.
Definition dircoll.cpp:69
zipios::DirectoryCollection::clone
virtual FileCollection * clone() const
Create a heap allocated clone of the object this method is called for.
Definition dircoll.cpp:115
zipios::DirectoryCollection::close
virtual void close()
Closes the FileCollection.
Definition dircoll.cpp:34
zipios::DirectoryCollection::DirectoryCollection
DirectoryCollection()
Default Constructor.
Definition dircoll.h:23
zipios::DirectoryCollection::~DirectoryCollection
virtual ~DirectoryCollection()
Destructor.
Definition dircoll.cpp:119
zipios::DirectoryCollection::size
virtual int size() const
Returns the number of entries in the FileCollection.
Definition dircoll.cpp:107
zipios::DirectoryCollection::entries
virtual ConstEntries entries() const
Returns a vector of const pointers to the entries in the FileCollection.
Definition dircoll.cpp:39
zipios::FileCollection::FileCollection
FileCollection()
FileCollection constructor.
Definition fcoll.h:24
zipios::FileCollection::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 fcoll.cpp:34
zipios::FileCollection::entries
virtual ConstEntries entries() const
Returns a vector of const pointers to the entries in the FileCollection.
Definition fcoll.cpp:17
zipios::FileEntry::isValid
virtual bool isValid() const =0
Any method or operator that initializes a FileEntry may set a flag, that specifies whether the read e...
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
dircoll.h
Header file that defines DirectoryCollection.
zipios::DirEntry
BasicEntry DirEntry
DirEntry is a BasicEntry.
Definition dircoll.h:14
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