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
37namespace zipios
38{
39
40
41
42namespace
43{
44
50char const *g_default_filename = "-";
51
52
61{
62public:
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
95private:
96 std::string const m_name;
97};
98
99
112{
113public:
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
148private:
149 std::string const m_name;
150};
151
152
153} // no name namespace
154
155
156
269FileCollection::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 {
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();
381 m_filename = g_default_filename;
382 m_valid = false;
383}
384
385
397{
398 mustBeValid();
399
400 return m_entries;
401}
402
403
424FileEntry::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
456std::string FileCollection::getName() const
457{
458 mustBeValid();
459 return m_filename;
460}
461
462
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
532void 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
567void 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
598std::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
Base class for various file collections.
virtual FileEntry::pointer_t getEntry(std::string const &name, MatchPath matchpath=MatchPath::MATCH) const
Get an entry from this collection.
virtual void addEntry(FileEntry const &entry)
Add an entry to this collection.
FileCollection(std::string const &filename="")
Initializes a FileCollection object.
bool isValid() const
Check whether the current collection is valid.
virtual void mustBeValid() const
Check whether the collection is valid.
virtual size_t size() const
Returns the number of entries in the FileCollection.
virtual ~FileCollection()
Make sure the resources are released.
void setMethod(size_t limit, StorageMethod small_storage_method, StorageMethod large_storage_method)
Change the storage method to the specified value.
virtual std::string getName() const
Returns the name of the FileCollection.
virtual void close()
Close the current FileEntry of this FileCollection.
FileEntry::vector_t m_entries
FileCollection & operator=(FileCollection const &src)
Replace the content of a collection with a copy of another collection.
virtual FileEntry::vector_t entries() const
Retrieve the array of entries.
void setLevel(size_t limit, FileEntry::CompressionLevel small_compression_level, FileEntry::CompressionLevel large_compression_level)
Change the compression level to the specified value.
A FileEntry represents an entry in a FileCollection.
Definition fileentry.hpp:76
std::shared_ptr< FileEntry > pointer_t
Definition fileentry.hpp:78
int CompressionLevel
The compression level to be used to save an entry.
Definition fileentry.hpp:85
virtual pointer_t clone() const =0
Create a clone of a file entry.
std::vector< pointer_t > vector_t
Definition fileentry.hpp:79
Exception used when it is not possible to move forward.
Class object used with the std::find_if() function.
MatchFileName(std::string const &name)
Initialize a MatchFileName object.
Class object used with the std::find_if() function.
MatchName(std::string const &name)
Initialize a MatchName object.
Define the zipios::FileCollection class.
char const * g_default_filename
A default filename for unnamed collections.
The zipios namespace includes the Zipios library definitions.
std::ostream & operator<<(std::ostream &os, FileCollection const &collection)
Write a FileCollection to the output stream.
StorageMethod
The types used with FileEntry::setMethod and FileEntry::getMethod.
Definition fileentry.hpp:49
Various exceptions used throughout the Zipios library, all based on zipios::Exception.