zipios  2.3.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-2022 Made to Order Software Corp. All Rights Reserved
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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
30 
32 
33 #include "zipios_common.hpp"
34 
35 
36 namespace zipios
37 {
38 
39 
40 namespace
41 {
42 
60  , std::string const & name
61  , FileEntry::pointer_t & cep
62  , FileCollection::pointer_t & file_collection
64 {
65  for(auto it = collections.begin(); it != collections.end(); ++it)
66  {
67  cep = (*it)->getEntry(name, matchpath);
68  if(cep)
69  {
70  file_collection = *it;
71  return;
72  }
73  }
74  cep.reset();
75  file_collection.reset();
76 }
77 
78 } // no name namespace
79 
80 
103 {
104  m_valid = true; // we are valid even though we are empty!
105 }
106 
107 
117  : FileCollection(rhs)
118 {
119  m_collections.reserve(rhs.m_collections.size());
120  for(auto it = rhs.m_collections.begin(); it != rhs.m_collections.end(); ++it)
121  {
122  m_collections.push_back((*it)->clone());
123  }
124 }
125 
126 
138 {
140 
141  if(this != &rhs)
142  {
143  m_collections.clear();
144  // A call to the CollectionCollection::size() function has side
145  // effects, try to avoid them at this time
146  //m_collections.reserve(rhs.m_collections.size());
147  for(auto it = rhs.m_collections.begin(); it != rhs.m_collections.end(); ++it)
148  {
149  m_collections.push_back((*it)->clone());
150  }
151  }
152 
153  return *this;
154 }
155 
156 
167 {
168  return std::make_shared<CollectionCollection>(*this);
169 }
170 
171 
178 {
179  close();
180 }
181 
182 
199 {
200  mustBeValid();
201 
208  if(this == &collection || !collection.isValid())
209  {
210  return false;
211  }
212 
213  m_collections.push_back(collection.clone());
214 
215  return true;
216 }
217 
218 
242 {
243  if(collection == nullptr)
244  {
245  // TBD: should we return false instead?
246  throw InvalidException("CollectionCollection::addCollection(): called with a null collection pointer");
247  }
248 
249  return addCollection(*collection);
250 }
251 
252 
266 {
267  // make sure to close all the children first
268  // (although I would imagine that the m_collections.clear() should
269  // be enough, unless someone else has a reference to another one
270  // of the sub-collections--but I do not think one can get such as
271  // reference at this point, remember that the addCollection()
272  // creates a clone of the collection being added.)
273  for(auto it = m_collections.begin(); it != m_collections.end(); ++it)
274  {
275  // each collection in the collection must be valid since we
276  // may hit any one of them
277  (*it)->close();
278  }
279  m_collections.clear();
280 
282 }
283 
284 
299 {
300  mustBeValid();
301 
302  FileEntry::vector_t all_entries;
303  for(auto it = m_collections.begin(); it != m_collections.end(); ++it)
304  {
305  all_entries += (*it)->entries();
306  }
307 
308  return all_entries;
309 }
310 
311 
346 FileEntry::pointer_t CollectionCollection::getEntry(std::string const & name, MatchPath matchpath) const
347 {
348  mustBeValid();
349 
350  // Returns the first matching entry.
351  FileCollection::pointer_t file_collection;
353 
354  matchEntry(m_collections, name, cep, file_collection, matchpath);
355 
356  return cep;
357 }
358 
359 
390 {
391  mustBeValid();
392 
393  FileCollection::pointer_t file_collection;
395 
396  matchEntry(m_collections, entry_name, cep, file_collection, matchpath);
397 
398  return cep ? file_collection->getInputStream(entry_name) : nullptr;
399 }
400 
401 
414 {
415  mustBeValid();
416 
417  size_t sz(0);
418  for(auto it = m_collections.begin(); it != m_collections.end(); ++it)
419  {
420  sz += (*it)->size();
421  }
422 
423  return sz;
424 }
425 
426 
438 {
439  // self must be valid
441 
442  for(auto it = m_collections.begin(); it != m_collections.end(); ++it)
443  {
444  // each collection in the collection must be valid since we
445  // may hit any one of them
446  (*it)->mustBeValid();
447  }
448 }
449 
450 
451 } // zipios namespace
452 
453 // Local Variables:
454 // mode: cpp
455 // indent-tabs-mode: nil
456 // c-basic-offset: 4
457 // tab-width: 4
458 // End:
459 
460 // vim: ts=4 sw=4 et
A collection of collections.
bool addCollection(FileCollection const &collection)
Add a FileCollection to this CollectionCollection.
The zipios namespace includes the Zipios library definitions.
Definition: backbuffer.cpp:35
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.
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
bool isValid() const
Check whether the current collection is valid.
void matchEntry(CollectionCollection::vector_t collections, std::string const &name, FileEntry::pointer_t &cep, FileCollection::pointer_t &file_collection, CollectionCollection::MatchPath matchpath)
Search for an entry.
Define the zipios::CollectionCollection class.
virtual void close()
Close the current FileEntry of this FileCollection.
CollectionCollection()
Initialize a CollectionCollection object.
FileCollection & operator=(FileCollection const &rhs)
Replace the content of a collection with a copy of another collection.
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.
std::shared_ptr< FileEntry > pointer_t
Definition: fileentry.hpp:78
CollectionCollection & operator=(CollectionCollection const &rhs)
Copy assignment operator.
virtual void mustBeValid() const
Check whether the collection is valid.
std::vector< pointer_t > vector_t
Definition: fileentry.hpp:79