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