dircoll.cpp
Go to the documentation of this file.00001
00002 #include "zipios-config.h"
00003
00004 #include "meta-iostreams.h"
00005 #include <vector>
00006 #include <sys/stat.h>
00007
00008 #include "dircoll.h"
00009
00010 #include "directory.h"
00011
00012
00013 using namespace zipios;
00014
00015 DirectoryCollection::DirectoryCollection( const string &path, bool recursive,
00016 bool load_now )
00017 : _entries_loaded( false ),
00018 _recursive ( recursive ),
00019 _filepath ( path )
00020 {
00021 _filename = _filepath ;
00022 _valid = _filepath.isDirectory() ;
00023
00024 if( _valid && load_now )
00025 loadEntries() ;
00026 }
00027
00028 void DirectoryCollection::close() {
00029 _valid = false ;
00030 }
00031
00032
00033 ConstEntries DirectoryCollection::entries() const {
00034 if ( ! _valid )
00035 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
00036
00037 loadEntries() ;
00038
00039 return FileCollection::entries() ;
00040 }
00041
00042
00043 ConstEntryPointer
00044 DirectoryCollection::getEntry( const string &name,
00045 MatchPath matchpath ) const {
00046 if ( ! _valid )
00047 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
00048
00049 if ( matchpath != MATCH || _entries_loaded ) {
00050 loadEntries() ;
00051 return FileCollection::getEntry( name, matchpath ) ;
00052 } else {
00053
00054 ConstEntryPointer ent ( new DirEntry( name, "", _filepath ) ) ;
00055 if ( ent->isValid() )
00056 return ent ;
00057 else
00058 return 0 ;
00059 }
00060 }
00061
00062
00063 istream *DirectoryCollection::getInputStream( const ConstEntryPointer &entry ) {
00064 if ( ! _valid )
00065 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
00066
00067 return getInputStream( entry->getName() ) ;
00068 }
00069
00070
00071 std::istream *DirectoryCollection::getInputStream( const string &entry_name,
00072 MatchPath matchpath )
00073 {
00074 using std::ifstream ;
00075
00076 if ( ! _valid )
00077 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
00078
00079 if ( matchpath != MATCH || _entries_loaded ) {
00080 loadEntries() ;
00081
00082 ConstEntryPointer ent = getEntry( entry_name, matchpath ) ;
00083
00084 if ( ent == 0 )
00085 return 0 ;
00086 else {
00087 string real_path( _filepath + entry_name ) ;
00088 return new ifstream( real_path.c_str(), ios::in | ios::binary ) ;
00089 }
00090
00091 } else {
00092
00093 string real_path( _filepath + entry_name ) ;
00094 ifstream *ifs = new ifstream( real_path.c_str(), ios::in | ios::binary ) ;
00095 if( ! *ifs ) {
00096 delete ifs ;
00097 return 0 ;
00098 } else
00099 return ifs ;
00100 }
00101 }
00102
00103
00104 int DirectoryCollection::size() const {
00105 if ( ! _valid )
00106 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
00107 loadEntries() ;
00108
00109 return _entries.size() ;
00110 }
00111
00112 FileCollection *DirectoryCollection::clone() const {
00113 return new DirectoryCollection( *this ) ;
00114 }
00115
00116 DirectoryCollection::~DirectoryCollection() {}
00117
00118
00119 void DirectoryCollection::loadEntries() const {
00120 if( _entries_loaded )
00121 return ;
00122
00123 const_cast< DirectoryCollection * >( this )->load( _recursive ) ;
00124
00125 _entries_loaded = true ;
00126 }
00127
00128
00129 void DirectoryCollection::load( bool recursive, const FilePath &subdir ) {
00130 using namespace boost::filesystem ;
00131 BasicEntry *ent ;
00132 for ( dir_it it( _filepath + subdir ) ; it != dir_it() ; ++it ) {
00133
00134 if ( *it == "." || *it == ".." || *it == "..." )
00135 continue ;
00136
00137 # if (defined(_MSC_VER) && (_MSC_VER >= 1800))
00138 if ( boost::filesystem::get< is_directory >( it ) && recursive ) {
00139 load( recursive, subdir + *it ) ;
00140 } else {
00141 _entries.push_back( ent = new BasicEntry( subdir + *it, "", _filepath ) ) ;
00142 ent->setSize( boost::filesystem::get< boost::filesystem::size >( it ) ) ;
00143 }
00144 #else
00145 if ( get< is_directory >( it ) && recursive ) {
00146 load( recursive, subdir + *it ) ;
00147 } else {
00148 _entries.push_back( ent = new BasicEntry( subdir + *it, "", _filepath ) ) ;
00149 ent->setSize( get< boost::filesystem::size >( it ) ) ;
00150 }
00151 #endif
00152 }
00153 }
00154
00155
00156
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178