zipios  2.3.2
Zipios -- a small C++ library that provides easy access to .zip files.
filecollection.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 
31 
33 
34 #include <algorithm>
35 
36 
37 namespace zipios
38 {
39 
40 
41 
42 namespace
43 {
44 
50 char const * g_default_filename = "-";
51 
52 
60 class MatchName
61 {
62 public:
72  explicit MatchName(std::string const & name)
73  : m_name(name)
74  {
75  }
76 
90  bool operator() (FileEntry::pointer_t entry) const
91  {
92  return entry->getName() == m_name;
93  }
94 
95 private:
96  std::string const m_name;
97 };
98 
99 
112 {
113 public:
125  explicit MatchFileName(std::string const & name)
126  : m_name(name)
127  {
128  }
129 
143  bool operator() (FileEntry::pointer_t entry) const
144  {
145  return entry->getFileName() == m_name;
146  }
147 
148 private:
149  std::string const m_name;
150 };
151 
152 
153 } // no name namespace
154 
155 
156 
269 FileCollection::FileCollection(std::string const & filename)
270  : m_filename(filename.empty() ? g_default_filename : filename)
271 {
272 }
273 
274 
287  : m_filename(rhs.m_filename)
288  , m_valid(rhs.m_valid)
289 {
290  m_entries.reserve(rhs.m_entries.size());
291  for(auto it = rhs.m_entries.begin(); it != rhs.m_entries.end(); ++it)
292  {
293  m_entries.push_back((*it)->clone());
294  }
295 }
296 
297 
313 {
314  if(this != &rhs)
315  {
316  m_filename = rhs.m_filename;
317 
318  m_entries.clear();
319  m_entries.reserve(rhs.m_entries.size());
320  for(auto it(rhs.m_entries.begin()); it != rhs.m_entries.end(); ++it)
321  {
322  m_entries.push_back((*it)->clone());
323  }
324 
325  m_valid = rhs.m_valid;
326  }
327 
328  return *this;
329 }
330 
331 
347 {
348 }
349 
350 
366 {
367  m_entries.push_back(entry.clone());
368 }
369 
370 
376 {
377  m_entries.clear();
379  m_valid = false;
380 }
381 
382 
394 {
395  mustBeValid();
396 
397  return m_entries;
398 }
399 
400 
421 FileEntry::pointer_t FileCollection::getEntry(std::string const & name, MatchPath matchpath) const
422 {
423  // make sure the entries were loaded if necessary
424  entries();
425 
426  mustBeValid();
427 
428  FileEntry::vector_t::const_iterator iter;
429  if(matchpath == MatchPath::MATCH)
430  {
431  iter = std::find_if(m_entries.begin(), m_entries.end(), MatchName(name));
432  }
433  else
434  {
435  iter = std::find_if(m_entries.begin(), m_entries.end(), MatchFileName(name));
436  }
437 
438  return iter == m_entries.end() ? FileEntry::pointer_t() : *iter;
439 }
440 
441 
453 std::string FileCollection::getName() const
454 {
455  mustBeValid();
456  return m_filename;
457 }
458 
459 
471 size_t FileCollection::size() const
472 {
473  // make sure the entries were loaded if necessary
474  entries();
475 
476  mustBeValid();
477  return m_entries.size();
478 }
479 
480 
491 {
492  return m_valid;
493 }
494 
495 
507 {
508  if(!m_valid)
509  {
510  throw InvalidStateException("Attempted to access an invalid FileCollection");
511  }
512 }
513 
514 
532  std::size_t limit
533  , StorageMethod small_storage_method
534  , StorageMethod large_storage_method)
535 {
536  // make sure the entries were loaded if necessary
537  entries();
538 
539  mustBeValid();
540 
541  for(auto it(m_entries.begin()); it != m_entries.end(); ++it)
542  {
543  if((*it)->getSize() > limit)
544  {
545  (*it)->setMethod(large_storage_method);
546  }
547  else
548  {
549  (*it)->setMethod(small_storage_method);
550  }
551  }
552 }
553 
554 
572  std::size_t limit
573  , FileEntry::CompressionLevel small_compression_level
574  , FileEntry::CompressionLevel large_compression_level)
575 {
576  // make sure the entries were loaded if necessary
577  entries();
578 
579  mustBeValid();
580 
581  for(auto it(m_entries.begin()); it != m_entries.end(); ++it)
582  {
583  if((*it)->getSize() > limit)
584  {
585  (*it)->setLevel(large_compression_level);
586  }
587  else
588  {
589  (*it)->setLevel(small_compression_level);
590  }
591  }
592 }
593 
594 
605 std::ostream & operator << (std::ostream & os, FileCollection const & collection)
606 {
607  os << "collection '" << collection.getName() << "' {";
608  FileEntry::vector_t entries(collection.entries());
609  char const *sep("");
610  for(auto it = entries.begin(); it != entries.end(); ++it)
611  {
612  os << sep;
613  sep = ", ";
614  os << (*it)->getName();
615  }
616  os << "}";
617  return os;
618 }
619 
620 
621 } // zipios namespace
622 
623 // Local Variables:
624 // mode: cpp
625 // indent-tabs-mode: nil
626 // c-basic-offset: 4
627 // tab-width: 4
628 // End:
629 
630 // vim: ts=4 sw=4 et
std::ostream & operator<<(std::ostream &os, FileCollection const &collection)
Write a FileCollection to the output stream.
virtual FileEntry::pointer_t getEntry(std::string const &name, MatchPath matchpath=MatchPath::MATCH) const
Get an entry from this collection.
The zipios namespace includes the Zipios library definitions.
Definition: backbuffer.cpp:35
Various exceptions used throughout the Zipios library, all based on zipios::Exception.
virtual void addEntry(FileEntry const &entry)
Add an entry to this collection.
Class object used with the std::find_if() function.
MatchName(std::string const &name)
Initialize a MatchName object.
StorageMethod
The types used with FileEntry::setMethod and FileEntry::getMethod.
Definition: fileentry.hpp:48
virtual FileEntry::vector_t entries() const
Retrieve the array of entries.
Define the zipios::FileCollection class.
bool isValid() const
Check whether the current collection is valid.
int CompressionLevel
The compression level to be used to save an entry.
Definition: fileentry.hpp:85
char const * g_default_filename
A default filename for unnamed collections.
FileEntry::vector_t m_entries
FileCollection(std::string const &filename=std::string())
Initializes a FileCollection object.
virtual ~FileCollection()
Make sure the resources are released.
Class object used with the std::find_if() function.
virtual void close()
Close the current FileEntry of this FileCollection.
virtual pointer_t clone() const =0
Create a clone of a file entry.
MatchFileName(std::string const &name)
Initialize a MatchFileName object.
A FileEntry represents an entry in a FileCollection.
Definition: fileentry.hpp:75
Exception used when it is not possible to move forward.
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.
Base class for various file collections.
void setLevel(size_t limit, FileEntry::CompressionLevel small_compression_level, FileEntry::CompressionLevel large_compression_level)
Change the compression level to the specified value.
virtual size_t size() const
Returns the number of entries in the FileCollection.
virtual std::string getName() const
Returns the name of the FileCollection.
std::shared_ptr< FileEntry > pointer_t
Definition: fileentry.hpp:78
void setMethod(size_t limit, StorageMethod small_storage_method, StorageMethod large_storage_method)
Change the storage method to the specified value.
std::vector< pointer_t > vector_t
Definition: fileentry.hpp:79