2 #include "zipios++/zipios-config.h"
4 #include "zipios++/meta-iostreams.h"
8 #include "zipios++/dircoll.h"
10 #include "../FlightCrew/Misc/BoostFilesystemUse.h"
20 DirectoryCollection::DirectoryCollection( const string &path, bool recursive,
22 : _entries_loaded( false ),
23 _recursive ( recursive ),
26 _filename = _filepath ;
27 _valid = _filepath.isDirectory() ;
29 if( _valid && load_now )
33 void DirectoryCollection::close() {
38 ConstEntries DirectoryCollection::entries() const {
40 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
44 return FileCollection::entries() ;
49 DirectoryCollection::getEntry( const string &name,
50 MatchPath matchpath ) const {
52 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
54 if ( matchpath != MATCH || _entries_loaded ) {
56 return FileCollection::getEntry( name, matchpath ) ;
58 // avoid loading entries if possible.
59 ConstEntryPointer ent ( new DirEntry( name, "", _filepath ) ) ;
68 istream *DirectoryCollection::getInputStream( const ConstEntryPointer &entry ) {
70 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
72 return getInputStream( entry->getName() ) ;
76 istream *DirectoryCollection::getInputStream( const string &entry_name,
77 MatchPath matchpath ) {
79 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
81 if ( matchpath != MATCH || _entries_loaded ) {
84 ConstEntryPointer ent = getEntry( entry_name, matchpath ) ;
89 string real_path( _filepath + entry_name ) ;
90 return new ifstream( real_path.c_str(), ios::in | ios::binary ) ;
94 // avoid loading entries if possible.
95 string real_path( _filepath + entry_name ) ;
96 ifstream *ifs = new ifstream( real_path.c_str(), ios::in | ios::binary ) ;
106 int DirectoryCollection::size() const {
108 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
111 return (int) _entries.size() ;
114 FileCollection *DirectoryCollection::clone() const {
115 return new DirectoryCollection( *this ) ;
118 DirectoryCollection::~DirectoryCollection() {}
121 void DirectoryCollection::loadEntries() const {
122 if( _entries_loaded )
125 const_cast< DirectoryCollection * >( this )->load( _recursive ) ;
127 _entries_loaded = true ;
131 void DirectoryCollection::load( bool recursive, const FilePath &subdir ) {
132 using namespace boost::filesystem ;
134 for ( fs::directory_iterator it( (_filepath + subdir).asString() ) ; it != fs::directory_iterator() ; ++it ) {
136 if ( *it == "." || *it == ".." || *it == "..." )
139 if ( fs::is_directory( it->path() ) && recursive ) {
140 load( recursive, subdir + it->path().filename().string() ) ;
142 _entries.push_back( ent = new BasicEntry( subdir + it->path().filename().string(), "", _filepath ) ) ;
143 ent->setSize( (zipios::uint32) fs::file_size( it->path() ) ) ;
152 Implementation of DirectoryCollection.
156 Zipios++ - a small C++ library that provides easy access to .zip files.
157 Copyright (C) 2000 Thomas Søndergaard
159 This library is free software; you can redistribute it and/or
160 modify it under the terms of the GNU Lesser General Public
161 License as published by the Free Software Foundation; either
162 version 2 of the License, or (at your option) any later version.
164 This library is distributed in the hope that it will be useful,
165 but WITHOUT ANY WARRANTY; without even the implied warranty of
166 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
167 Lesser General Public License for more details.
169 You should have received a copy of the GNU Lesser General Public
170 License along with this library; if not, write to the Free Software
171 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA