Zipios++
dircoll.cpp
Go to the documentation of this file.
1
2#include "zipios++/zipios-config.h"
3
4#include "zipios++/meta-iostreams.h"
5#include <vector>
6#include <sys/stat.h>
7#include <stddef.h>
8
9#include "zipios++/dircoll.h"
10
11#include "directory.h"
12
13
14namespace zipios {
15
16using std::cerr ;
17using std::endl ;
18using std::vector ;
19using std::ifstream ;
20
21DirectoryCollection::DirectoryCollection( const string &path, bool recursive,
22 bool load_now )
23 : _entries_loaded( false ),
24 _recursive ( recursive ),
25 _filepath ( path )
26{
27 _filename = _filepath ;
28 _valid = _filepath.isDirectory() ;
29
30 if( _valid && load_now )
31 loadEntries() ;
32}
33
35 _valid = false ;
36}
37
38
40 if ( ! _valid )
41 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
42
43 loadEntries() ;
44
46}
47
48
50DirectoryCollection::getEntry( const string &name,
51 MatchPath matchpath ) const {
52 if ( ! _valid )
53 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
54
55 if ( matchpath != MATCH || _entries_loaded ) {
56 loadEntries() ;
57 return FileCollection::getEntry( name, matchpath ) ;
58 } else {
59 // avoid loading entries if possible.
60 ConstEntryPointer ent ( new DirEntry( name, "", _filepath ) ) ;
61 if ( ent->isValid() )
62 return ent ;
63 else
64 return 0 ;
65 }
66}
67
68
70 if ( ! _valid )
71 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
72
73 return getInputStream( entry->getName() ) ;
74}
75
76
77istream *DirectoryCollection::getInputStream( const string &entry_name,
78 MatchPath matchpath ) {
79 if ( ! _valid )
80 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
81
82 if ( matchpath != MATCH || _entries_loaded ) {
83 loadEntries() ;
84
85 ConstEntryPointer ent = getEntry( entry_name, matchpath ) ;
86
87 if ( ent == 0 )
88 return 0 ;
89 else {
90 string real_path( _filepath + entry_name ) ;
91 return new ifstream( real_path.c_str(), ios::in | ios::binary ) ;
92 }
93
94 } else {
95 // avoid loading entries if possible.
96 string real_path( _filepath + entry_name ) ;
97 ifstream *ifs = new ifstream( real_path.c_str(), ios::in | ios::binary ) ;
98 if( ! *ifs ) {
99 delete ifs ;
100 return 0 ;
101 } else
102 return ifs ;
103 }
104}
105
106
108 if ( ! _valid )
109 throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
110 loadEntries() ;
111
112 return _entries.size() ;
113}
114
116 return new DirectoryCollection( *this ) ;
117}
118
120
121
122void DirectoryCollection::loadEntries() const {
123 if( _entries_loaded )
124 return ;
125
126 const_cast< DirectoryCollection * >( this )->load( _recursive ) ;
127
128 _entries_loaded = true ;
129}
130
131
132void DirectoryCollection::load( bool recursive, const FilePath &subdir ) {
133 using namespace boost::filesystem ;
134 BasicEntry *ent ;
135 for ( dir_it it( _filepath + subdir ) ; it != dir_it() ; ++it ) {
136
137 if ( *it == "." || *it == ".." || *it == "..." )
138 continue ;
139
140 if ( get< is_directory >( it ) && recursive ) {
141 load( recursive, subdir + *it ) ;
142 } else {
143 _entries.push_back( ent = new BasicEntry( subdir + *it, "", _filepath ) ) ;
144 ent->setSize( get< boost::filesystem::size >( it ) ) ;
145 }
146
147 }
148}
149
150} // namespace
151
156/*
157 Zipios++ - a small C++ library that provides easy access to .zip files.
158 Copyright (C) 2000 Thomas Søndergaard
159
160 This library is free software; you can redistribute it and/or
161 modify it under the terms of the GNU Lesser General Public
162 License as published by the Free Software Foundation; either
163 version 2 of the License, or (at your option) any later version.
164
165 This library is distributed in the hope that it will be useful,
166 but WITHOUT ANY WARRANTY; without even the implied warranty of
167 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
168 Lesser General Public License for more details.
169
170 You should have received a copy of the GNU Lesser General Public
171 License along with this library; if not, write to the Free Software
172 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
173*/
BasicEntry is a FileEntry that is suitable as a base class for basic entries, that e....
Definition basicentry.h:18
DirectoryCollection is a FileCollection that obtains its entries from a directory.
Definition dircoll.h:19
virtual ConstEntryPointer getEntry(const string &name, MatchPath matchpath=MATCH) const
Returns a ConstEntryPointer to a FileEntry object for the entry with the specified name.
Definition dircoll.cpp:50
virtual istream * getInputStream(const ConstEntryPointer &entry)
Returns a pointer to an opened istream for the specified FileEntry.
Definition dircoll.cpp:69
virtual FileCollection * clone() const
Create a heap allocated clone of the object this method is called for.
Definition dircoll.cpp:115
virtual void close()
Closes the FileCollection.
Definition dircoll.cpp:34
DirectoryCollection()
Default Constructor.
Definition dircoll.h:23
virtual ~DirectoryCollection()
Destructor.
Definition dircoll.cpp:119
virtual int size() const
Returns the number of entries in the FileCollection.
Definition dircoll.cpp:107
virtual ConstEntries entries() const
Returns a vector of const pointers to the entries in the FileCollection.
Definition dircoll.cpp:39
FileCollection is an abstract baseclass that represents a collection of files.
Definition fcoll.h:21
virtual ConstEntryPointer getEntry(const string &name, MatchPath matchpath=MATCH) const
Returns a ConstEntryPointer to a FileEntry object for the entry with the specified name.
Definition fcoll.cpp:34
virtual ConstEntries entries() const
Returns a vector of const pointers to the entries in the FileCollection.
Definition fcoll.cpp:17
bool isDirectory() const
Definition filepath.h:143
An object member function may throw this exception, if the operation it normally performs is inapprop...
SimpleSmartPointer is a simple reference counting smart pointer template.
Header file that defines DirectoryCollection.
vector< EntryPointer > ConstEntries
ConstEntries is a vector of ConstEntryPointer's.
Definition fileentry.h:43