zipios++  2.0.2
Zipios++ – a small C++ library that provides easy access to .zip files.
collectioncollection.cpp
Go to the documentation of this file.
1 /*
2  Zipios++ - a small C++ library that provides easy access to .zip files.
3 
4  Copyright (C) 2000-2007 Thomas Sondergaard
5  Copyright (C) 2015 Made to Order Software Corporation
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
30 
32 
33 #include "zipios_common.hpp"
34 
35 
36 namespace zipios
37 {
38 
39 
40 namespace
41 {
42 
58 void matchEntry(CollectionCollection::vector_t collections, std::string const& name, FileEntry::pointer_t& cep, FileCollection::pointer_t& file_collection, CollectionCollection::MatchPath matchpath)
59 {
60  for(auto it = collections.begin(); it != collections.end(); ++it)
61  {
62  cep = (*it)->getEntry(name, matchpath);
63  if(cep)
64  {
65  file_collection = *it;
66  return;
67  }
68  }
69  cep.reset();
70  file_collection.reset();
71 }
72 
73 } // no name namespace
74 
75 
98 {
99  m_valid = true; // we are valid even though we are empty!
100 }
101 
102 
112  : FileCollection(src)
113 {
114  m_collections.reserve(src.m_collections.size());
115  for(auto it = src.m_collections.begin(); it != src.m_collections.end(); ++it)
116  {
117  m_collections.push_back((*it)->clone());
118  }
119 }
120 
121 
133 {
135 
136  if(this != &rhs)
137  {
138  m_collections.clear();
139  // A call to the CollectionCollection::size() function has side
140  // effects, try to avoid them at this time
141  //m_collections.reserve(rhs.m_collections.size());
142  for(auto it = rhs.m_collections.begin(); it != rhs.m_collections.end(); ++it)
143  {
144  m_collections.push_back((*it)->clone());
145  }
146  }
147 
148  return *this;
149 }
150 
151 
162 {
164 }
165 
166 
173 {
174  close();
175 }
176 
177 
194 {
195  mustBeValid();
196 
203  if(this == &collection || !collection.isValid())
204  {
205  return false;
206  }
207 
208  m_collections.push_back(collection.clone());
209 
210  return true;
211 }
212 
213 
237 {
238  if(!collection)
239  {
240  // TBD: should we return false instead?
241  throw InvalidException("CollectionCollection::addCollection(): called with a null collection pointer");
242  }
243 
244  return addCollection(*collection);
245 }
246 
247 
261 {
262  // make sure to close all the children first
263  // (although I would imagine that the m_collections.clear() should
264  // be enough, unless someone else has a refenrence to another one
265  // of the sub-collections--but I do not think one can get such as
266  // reference at this point, remember that the addCollection()
267  // creates a clone of the collection being added.)
268  for(auto it = m_collections.begin(); it != m_collections.end(); ++it)
269  {
270  // each collection in the collection must be valid since we
271  // may hit any one of them
272  (*it)->close();
273  }
274  m_collections.clear();
275 
277 }
278 
279 
294 {
295  mustBeValid();
296 
297  FileEntry::vector_t all_entries;
298  for(auto it = m_collections.begin(); it != m_collections.end(); ++it)
299  {
300  all_entries += (*it)->entries();
301  }
302 
303  return all_entries;
304 }
305 
306 
341 FileEntry::pointer_t CollectionCollection::getEntry(std::string const& name, MatchPath matchpath) const
342 {
343  mustBeValid();
344 
345  // Returns the first matching entry.
346  FileCollection::pointer_t file_colection;
348 
349  matchEntry(m_collections, name, cep, file_colection, matchpath);
350 
351  return cep;
352 }
353 
354 
385 {
386  mustBeValid();
387 
388  FileCollection::pointer_t file_collection;
390 
391  matchEntry(m_collections, entry_name, cep, file_collection, matchpath);
392 
393  return cep ? file_collection->getInputStream(entry_name) : nullptr;
394 }
395 
396 
409 {
410  mustBeValid();
411 
412  size_t sz(0);
413  for(auto it = m_collections.begin(); it != m_collections.end(); ++it)
414  {
415  sz += (*it)->size();
416  }
417 
418  return sz;
419 }
420 
421 
433 {
434  // self must be valid
436 
437  for(auto it = m_collections.begin(); it != m_collections.end(); ++it)
438  {
439  // each collection in the collection must be valid since we
440  // may hit any one of them
441  (*it)->mustBeValid();
442  }
443 }
444 
445 
446 } // zipios namespace
447 
448 // Local Variables:
449 // mode: cpp
450 // indent-tabs-mode: nil
451 // c-basic-offset: 4
452 // tab-width: 4
453 // End:
454 
455 // vim: ts=4 sw=4 et
A collection of collections.
bool addCollection(FileCollection const &collection)
Add a FileCollection to this CollectionCollection.
std::shared_ptr< FileCollection > pointer_t
Various exceptions used throughout the Zipios++ library, all based on zipios::Exception.
virtual size_t size() const override
Return the size of the of this collection.
virtual ~CollectionCollection() override
Clean up this CollectionCollection object.
virtual void close() override
Close the CollectionCollection object.
virtual stream_pointer_t getInputStream(std::string const &entry_name, MatchPath matchpath=MatchPath::MATCH) override
Retrieve pointer to an istream.
CollectionCollection & operator=(CollectionCollection const &src)
Copy assignment operator.
virtual pointer_t clone() const =0
Create a clone of this object.
std::shared_ptr< std::istream > stream_pointer_t
A shared pointer to an input stream.
std::vector< pointer_t > vector_t
void matchEntry(CollectionCollection::vector_t collections, std::string const &name, FileEntry::pointer_t &cep, FileCollection::pointer_t &file_collection, CollectionCollection::MatchPath matchpath)
Seach for an entry.
Define the zipios::CollectionCollection class.
virtual void close()
Close the current FileEntry of this FileCollection.
virtual void mustBeValid() const
Check whether the collection is valid.
FileCollection & operator=(FileCollection const &src)
Replace the content of a collection with a copy of another collection.
CollectionCollection()
Initialize a CollectionCollection object.
virtual void mustBeValid() const
Check whether the collection is valid.
virtual FileEntry::vector_t entries() const override
Retrieve a vector to all the collection entries.
Various functions used throughout the library.
An InvalidException is used when invalid data is provided.
Base class for various file collections.
virtual FileEntry::pointer_t getEntry(std::string const &name, MatchPath matchpath=MatchPath::MATCH) const override
Get an entry from the collection.
virtual pointer_t clone() const override
Create a clone of this object.
bool isValid() const
Check whether the current collection is valid.
std::shared_ptr< FileEntry > pointer_t
Definition: fileentry.hpp:77
std::vector< pointer_t > vector_t
Definition: fileentry.hpp:78