]> git.donarmstrong.com Git - flightcrew.git/blob - src/zipios/src/collcoll.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / zipios / src / collcoll.cpp
1
2 #include "zipios++/zipios-config.h"
3
4 #include "zipios++/meta-iostreams.h"
5
6 #include "zipios++/collcoll.h"
7 #include "zipios_common.h"
8
9 namespace zipios {
10
11 using std::ifstream ;
12
13 CollectionCollection *CollectionCollection::_inst = 0 ;
14
15
16 CollectionCollection::CollectionCollection() {
17   _valid = true ; // we're valid even though we are empty!
18 }
19
20
21 bool CollectionCollection::addCollection( const FileCollection &collection ) {
22   if ( ! _valid )
23     throw InvalidStateException( "Attempt to add a FileCollection to an invalid CollectionCollection" ) ;
24   if ( this == &collection || ! collection.isValid()  )
25     return false ;
26   _collections.push_back( collection.clone() ) ;
27   return true ;
28   
29 }
30
31 bool CollectionCollection::addCollection( FileCollection *collection ) {
32   if ( ! _valid )
33     throw InvalidStateException( "Attempt to add a FileCollection to an invalid CollectionCollection" ) ;
34   if ( collection == 0 || this == collection || ! collection->isValid() )
35     return false ;
36   _collections.push_back( collection ) ;
37   return true ;
38 }
39
40
41 void CollectionCollection::close() {
42   _valid = false ;
43 }
44
45
46 ConstEntries CollectionCollection::entries() const {
47   if ( ! _valid )
48     throw InvalidStateException( "Attempt to get entries from an invalid CollectionCollection" ) ;
49
50   ConstEntries all_entries ;
51   std::vector< FileCollection * >::const_iterator it ;
52   for ( it = _collections.begin() ; it != _collections.end() ; it++ )
53     all_entries += (*it)->entries() ;
54   return all_entries ;
55 }
56
57
58 ConstEntryPointer CollectionCollection::getEntry( const string &name, 
59                                                   MatchPath matchpath ) const {
60   if ( ! _valid )
61     throw InvalidStateException( "Attempt to get an entry from an invalid CollectionCollection" ) ;
62   // Returns the first matching entry.
63   std::vector< FileCollection * >::const_iterator it ;
64   ConstEntryPointer cep ;
65
66   getEntry( name, cep, it, matchpath ) ; 
67
68   return cep ;
69 }
70
71
72 istream *CollectionCollection::getInputStream( const ConstEntryPointer &entry ) {
73   if ( ! _valid )
74     throw InvalidStateException( "Attempt to get an input stream from an invalid CollectionCollection" ) ;
75
76   return getInputStream( entry->getName() ) ;
77 }
78
79
80 istream *CollectionCollection::getInputStream( const string &entry_name, 
81                                                MatchPath matchpath ) {
82   if ( ! _valid )
83     throw InvalidStateException( "Attempt to get an input stream from an invalid CollectionCollection" ) ;
84
85   std::vector< FileCollection * >::const_iterator it ;
86   ConstEntryPointer cep ;
87
88   getEntry( entry_name, cep, it, matchpath ) ; 
89   
90   if ( cep == 0 )
91     return 0 ;
92   else
93     return (*it)->getInputStream( entry_name ) ;
94   
95 }
96
97
98 int CollectionCollection::size() const {
99   if ( ! _valid )
100     throw InvalidStateException( "Attempt to get the size of an invalid CollectionCollection" ) ;
101   int sz = 0 ;
102   std::vector< FileCollection * >::const_iterator it ;
103   for ( it = _collections.begin() ; it != _collections.end() ; it++ )
104     sz += (*it)->size() ;
105   return sz ;
106 }
107
108 FileCollection *CollectionCollection::clone() const {
109   return new CollectionCollection( *this ) ;
110 }
111
112 CollectionCollection::~CollectionCollection() {
113   std::vector< FileCollection * >::iterator it ;
114   for ( it = _collections.begin() ; it != _collections.end() ; ++it )
115     delete *it ;
116 }
117
118
119 //
120 // Protected member functions
121 //
122
123 void CollectionCollection::getEntry( const string &name,
124                                      ConstEntryPointer &cep, 
125                                      std::vector< FileCollection * >::const_iterator &it, 
126                                      MatchPath matchpath ) const {
127   
128   // Returns the first matching entry.
129   cep = 0 ;
130   for ( it = _collections.begin() ; it != _collections.end() ; it++ ) {
131     cep = (*it)->getEntry( name, matchpath ) ;
132     if ( cep )
133       break ;
134   }
135 }
136
137
138
139 } // namespace
140
141 /** \file
142     Implementation of CollectionCollection.
143 */
144
145 /*
146   Zipios++ - a small C++ library that provides easy access to .zip files.
147   Copyright (C) 2000  Thomas Søndergaard
148   
149   This library is free software; you can redistribute it and/or
150   modify it under the terms of the GNU Lesser General Public
151   License as published by the Free Software Foundation; either
152   version 2 of the License, or (at your option) any later version.
153   
154   This library is distributed in the hope that it will be useful,
155   but WITHOUT ANY WARRANTY; without even the implied warranty of
156   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
157   Lesser General Public License for more details.
158   
159   You should have received a copy of the GNU Lesser General Public
160   License along with this library; if not, write to the Free Software
161   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
162 */