Zipios++
|
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 // avoid loading entries if possible. 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 // avoid loading entries if possible. 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 } // namespace 00150 00155 /* 00156 Zipios++ - a small C++ library that provides easy access to .zip files. 00157 Copyright (C) 2000 Thomas Søndergaard 00158 00159 This library is free software; you can redistribute it and/or 00160 modify it under the terms of the GNU Lesser General Public 00161 License as published by the Free Software Foundation; either 00162 version 2 of the License, or (at your option) any later version. 00163 00164 This library is distributed in the hope that it will be useful, 00165 but WITHOUT ANY WARRANTY; without even the implied warranty of 00166 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00167 Lesser General Public License for more details. 00168 00169 You should have received a copy of the GNU Lesser General Public 00170 License along with this library; if not, write to the Free Software 00171 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00172 */