Zipios++
|
00001 00002 #include "zipios++/zipios-config.h" 00003 00004 #include "zipios++/meta-iostreams.h" 00005 00006 #include "zipios++/collcoll.h" 00007 #include "zipios_common.h" 00008 00009 namespace zipios { 00010 00011 using std::ifstream ; 00012 00013 CollectionCollection *CollectionCollection::_inst = 0 ; 00014 00015 00016 CollectionCollection::CollectionCollection() { 00017 _valid = true ; // we're valid even though we are empty! 00018 } 00019 00020 00021 bool CollectionCollection::addCollection( const FileCollection &collection ) { 00022 if ( ! _valid ) 00023 throw InvalidStateException( "Attempt to add a FileCollection to an invalid CollectionCollection" ) ; 00024 if ( this == &collection || ! collection.isValid() ) 00025 return false ; 00026 _collections.push_back( collection.clone() ) ; 00027 return true ; 00028 00029 } 00030 00031 bool CollectionCollection::addCollection( FileCollection *collection ) { 00032 if ( ! _valid ) 00033 throw InvalidStateException( "Attempt to add a FileCollection to an invalid CollectionCollection" ) ; 00034 if ( collection == 0 || this == collection || ! collection->isValid() ) 00035 return false ; 00036 _collections.push_back( collection ) ; 00037 return true ; 00038 } 00039 00040 00041 void CollectionCollection::close() { 00042 _valid = false ; 00043 } 00044 00045 00046 ConstEntries CollectionCollection::entries() const { 00047 if ( ! _valid ) 00048 throw InvalidStateException( "Attempt to get entries from an invalid CollectionCollection" ) ; 00049 00050 ConstEntries all_entries ; 00051 std::vector< FileCollection * >::const_iterator it ; 00052 for ( it = _collections.begin() ; it != _collections.end() ; it++ ) 00053 all_entries += (*it)->entries() ; 00054 return all_entries ; 00055 } 00056 00057 00058 ConstEntryPointer CollectionCollection::getEntry( const string &name, 00059 MatchPath matchpath ) const { 00060 if ( ! _valid ) 00061 throw InvalidStateException( "Attempt to get an entry from an invalid CollectionCollection" ) ; 00062 // Returns the first matching entry. 00063 std::vector< FileCollection * >::const_iterator it ; 00064 ConstEntryPointer cep ; 00065 00066 getEntry( name, cep, it, matchpath ) ; 00067 00068 return cep ; 00069 } 00070 00071 00072 istream *CollectionCollection::getInputStream( const ConstEntryPointer &entry ) { 00073 if ( ! _valid ) 00074 throw InvalidStateException( "Attempt to get an input stream from an invalid CollectionCollection" ) ; 00075 00076 return getInputStream( entry->getName() ) ; 00077 } 00078 00079 00080 istream *CollectionCollection::getInputStream( const string &entry_name, 00081 MatchPath matchpath ) { 00082 if ( ! _valid ) 00083 throw InvalidStateException( "Attempt to get an input stream from an invalid CollectionCollection" ) ; 00084 00085 std::vector< FileCollection * >::const_iterator it ; 00086 ConstEntryPointer cep ; 00087 00088 getEntry( entry_name, cep, it, matchpath ) ; 00089 00090 if ( cep == 0 ) 00091 return 0 ; 00092 else 00093 return (*it)->getInputStream( entry_name ) ; 00094 00095 } 00096 00097 00098 int CollectionCollection::size() const { 00099 if ( ! _valid ) 00100 throw InvalidStateException( "Attempt to get the size of an invalid CollectionCollection" ) ; 00101 int sz = 0 ; 00102 std::vector< FileCollection * >::const_iterator it ; 00103 for ( it = _collections.begin() ; it != _collections.end() ; it++ ) 00104 sz += (*it)->size() ; 00105 return sz ; 00106 } 00107 00108 FileCollection *CollectionCollection::clone() const { 00109 return new CollectionCollection( *this ) ; 00110 } 00111 00112 CollectionCollection::~CollectionCollection() { 00113 std::vector< FileCollection * >::iterator it ; 00114 for ( it = _collections.begin() ; it != _collections.end() ; ++it ) 00115 delete *it ; 00116 } 00117 00118 00119 // 00120 // Protected member functions 00121 // 00122 00123 void CollectionCollection::getEntry( const string &name, 00124 ConstEntryPointer &cep, 00125 std::vector< FileCollection * >::const_iterator &it, 00126 MatchPath matchpath ) const { 00127 00128 // Returns the first matching entry. 00129 cep = 0 ; 00130 for ( it = _collections.begin() ; it != _collections.end() ; it++ ) { 00131 cep = (*it)->getEntry( name, matchpath ) ; 00132 if ( cep ) 00133 break ; 00134 } 00135 } 00136 00137 00138 00139 } // namespace 00140 00145 /* 00146 Zipios++ - a small C++ library that provides easy access to .zip files. 00147 Copyright (C) 2000 Thomas Søndergaard 00148 00149 This library is free software; you can redistribute it and/or 00150 modify it under the terms of the GNU Lesser General Public 00151 License as published by the Free Software Foundation; either 00152 version 2 of the License, or (at your option) any later version. 00153 00154 This library is distributed in the hope that it will be useful, 00155 but WITHOUT ANY WARRANTY; without even the implied warranty of 00156 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00157 Lesser General Public License for more details. 00158 00159 You should have received a copy of the GNU Lesser General Public 00160 License along with this library; if not, write to the Free Software 00161 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00162 */