zipios  2.2.0
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-2019 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.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  //, m_entries() -- auto-init
272  //, m_valid(true) -- auto-init
273 {
274 }
275 
276 
289  : m_filename(src.m_filename)
290  //, m_entries() -- see below
291  , m_valid(src.m_valid)
292 {
293  m_entries.reserve(src.m_entries.size());
294  for(auto it = src.m_entries.begin(); it != src.m_entries.end(); ++it)
295  {
296  m_entries.push_back((*it)->clone());
297  }
298 }
299 
300 
316 {
317  if(this != &rhs)
318  {
319  m_filename = rhs.m_filename;
320 
321  m_entries.clear();
322  m_entries.reserve(rhs.m_entries.size());
323  for(auto it(rhs.m_entries.begin()); it != rhs.m_entries.end(); ++it)
324  {
325  m_entries.push_back((*it)->clone());
326  }
327 
328  m_valid = rhs.m_valid;
329  }
330 
331  return *this;
332 }
333 
334 
350 {
351 }
352 
353 
369 {
370  m_entries.push_back(entry.clone());
371 }
372 
373 
379 {
380  m_entries.clear();
382  m_valid = false;
383 }
384 
385 
397 {
398  mustBeValid();
399 
400  return m_entries;
401 }
402 
403 
424 FileEntry::pointer_t FileCollection::getEntry(std::string const& name, MatchPath matchpath) const
425 {
426  // make sure the entries were loaded if necessary
427  entries();
428 
429  mustBeValid();
430 
431  FileEntry::vector_t::const_iterator iter;
432  if(matchpath == MatchPath::MATCH)
433  {
434  iter = std::find_if(m_entries.begin(), m_entries.end(), MatchName(name));
435  }
436  else
437  {
438  iter = std::find_if(m_entries.begin(), m_entries.end(), MatchFileName(name));
439  }
440 
441  return iter == m_entries.end() ? FileEntry::pointer_t() : *iter;
442 }
443 
444 
456 std::string FileCollection::getName() const
457 {
458  mustBeValid();
459  return m_filename;
460 }
461 
462 
474 size_t FileCollection::size() const
475 {
476  // make sure the entries were loaded if necessary
477  entries();
478 
479  mustBeValid();
480  return m_entries.size();
481 }
482 
483 
494 {
495  return m_valid;
496 }
497 
498 
510 {
511  if(!m_valid)
512  {
513  throw InvalidStateException("Attempted to access an invalid FileCollection");
514  }
515 }
516 
517 
532 void FileCollection::setMethod(size_t limit, StorageMethod small_storage_method, StorageMethod large_storage_method)
533 {
534  // make sure the entries were loaded if necessary
535  entries();
536 
537  mustBeValid();
538 
539  for(auto it(m_entries.begin()); it != m_entries.end(); ++it)
540  {
541  if((*it)->getSize() > limit)
542  {
543  (*it)->setMethod(large_storage_method);
544  }
545  else
546  {
547  (*it)->setMethod(small_storage_method);
548  }
549  }
550 }
551 
552 
567 void FileCollection::setLevel(size_t limit, FileEntry::CompressionLevel small_compression_level, FileEntry::CompressionLevel large_compression_level)
568 {
569  // make sure the entries were loaded if necessary
570  entries();
571 
572  mustBeValid();
573 
574  for(auto it(m_entries.begin()); it != m_entries.end(); ++it)
575  {
576  if((*it)->getSize() > limit)
577  {
578  (*it)->setLevel(large_compression_level);
579  }
580  else
581  {
582  (*it)->setLevel(small_compression_level);
583  }
584  }
585 }
586 
587 
598 std::ostream& operator << (std::ostream& os, FileCollection const& collection)
599 {
600  os << "collection '" << collection.getName() << "' {";
601  FileEntry::vector_t entries(collection.entries());
602  char const *sep("");
603  for(auto it = entries.begin(); it != entries.end(); ++it)
604  {
605  os << sep;
606  sep = ", ";
607  os << (*it)->getName();
608  }
609  os << "}";
610  return os;
611 }
612 
613 
614 } // zipios namespace
615 
616 // Local Variables:
617 // mode: cpp
618 // indent-tabs-mode: nil
619 // c-basic-offset: 4
620 // tab-width: 4
621 // End:
622 
623 // vim: ts=4 sw=4 et
zipios::FileEntry::CompressionLevel
int CompressionLevel
The compression level to be used to save an entry.
Definition: fileentry.hpp:85
zipios::FileCollection::setLevel
void setLevel(size_t limit, FileEntry::CompressionLevel small_compression_level, FileEntry::CompressionLevel large_compression_level)
Change the compression level to the specified value.
Definition: filecollection.cpp:567
zipios::FileCollection::entries
virtual FileEntry::vector_t entries() const
Retrieve the array of entries.
Definition: filecollection.cpp:396
zipios::anonymous_namespace{filecollection.cpp}::MatchFileName::m_name
const std::string m_name
Definition: filecollection.cpp:149
zipios::FileCollection::getEntry
virtual FileEntry::pointer_t getEntry(std::string const &name, MatchPath matchpath=MatchPath::MATCH) const
Get an entry from this collection.
Definition: filecollection.cpp:424
zipios::FileCollection::m_valid
bool m_valid
Definition: filecollection.hpp:74
zipios::FileCollection::size
virtual size_t size() const
Returns the number of entries in the FileCollection.
Definition: filecollection.cpp:474
zipios::FileEntry
A FileEntry represents an entry in a FileCollection.
Definition: fileentry.hpp:75
zipios::FileCollection::FileCollection
FileCollection(std::string const &filename="")
Initializes a FileCollection object.
Definition: filecollection.cpp:269
zipios::FileCollection::mustBeValid
virtual void mustBeValid() const
Check whether the collection is valid.
Definition: filecollection.cpp:509
zipios::anonymous_namespace{filecollection.cpp}::g_default_filename
const char * g_default_filename
A default filename for unnamed collections.
Definition: filecollection.cpp:50
zipiosexceptions.hpp
Various exceptions used throughout the Zipios library, all based on zipios::Exception.
zipios::FileEntry::pointer_t
std::shared_ptr< FileEntry > pointer_t
Definition: fileentry.hpp:78
zipios::anonymous_namespace{filecollection.cpp}::MatchName::MatchName
MatchName(std::string const &name)
Initialize a MatchName object.
Definition: filecollection.cpp:72
zipios::FileCollection::operator=
FileCollection & operator=(FileCollection const &src)
Replace the content of a collection with a copy of another collection.
Definition: filecollection.cpp:315
zipios::InvalidStateException
Exception used when it is not possible to move forward.
Definition: zipiosexceptions.hpp:107
zipios::operator<<
std::ostream & operator<<(std::ostream &os, FileCollection const &collection)
Write a FileCollection to the output stream.
Definition: filecollection.cpp:598
zipios::anonymous_namespace{filecollection.cpp}::MatchFileName::MatchFileName
MatchFileName(std::string const &name)
Initialize a MatchFileName object.
Definition: filecollection.cpp:125
zipios::FileCollection
Base class for various file collections.
Definition: filecollection.hpp:40
zipios::FileCollection::MatchPath
MatchPath
Definition: filecollection.hpp:47
zipios::FileCollection::close
virtual void close()
Close the current FileEntry of this FileCollection.
Definition: filecollection.cpp:378
zipios::FileCollection::~FileCollection
virtual ~FileCollection()
Make sure the resources are released.
Definition: filecollection.cpp:349
zipios::StorageMethod
StorageMethod
The types used with FileEntry::setMethod and FileEntry::getMethod.
Definition: fileentry.hpp:48
zipios::FileCollection::MatchPath::MATCH
@ MATCH
filecollection.hpp
Define the zipios::FileCollection class.
zipios::anonymous_namespace{filecollection.cpp}::MatchFileName
Class object used with the std::find_if() function.
Definition: filecollection.cpp:111
zipios::anonymous_namespace{filecollection.cpp}::MatchName
Class object used with the std::find_if() function.
Definition: filecollection.cpp:60
zipios::FileEntry::clone
virtual pointer_t clone() const =0
Create a clone of a file entry.
zipios::FileCollection::setMethod
void setMethod(size_t limit, StorageMethod small_storage_method, StorageMethod large_storage_method)
Change the storage method to the specified value.
Definition: filecollection.cpp:532
zipios::FileCollection::m_entries
FileEntry::vector_t m_entries
Definition: filecollection.hpp:73
zipios::anonymous_namespace{filecollection.cpp}::MatchName::m_name
const std::string m_name
Definition: filecollection.cpp:96
zipios::FileCollection::getName
virtual std::string getName() const
Returns the name of the FileCollection.
Definition: filecollection.cpp:456
zipios::FileCollection::m_filename
std::string m_filename
Definition: filecollection.hpp:72
zipios::FileEntry::vector_t
std::vector< pointer_t > vector_t
Definition: fileentry.hpp:79
zipios
The zipios namespace includes the Zipios library definitions.
Definition: backbuffer.cpp:35
zipios::FileCollection::isValid
bool isValid() const
Check whether the current collection is valid.
Definition: filecollection.cpp:493
zipios::FileCollection::addEntry
virtual void addEntry(FileEntry const &entry)
Add an entry to this collection.
Definition: filecollection.cpp:368