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