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 
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 
34  _valid = false ;
35 }
36 
37 
39  if ( ! _valid )
40  throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
41 
42  loadEntries() ;
43 
44  return FileCollection::entries() ;
45 }
46 
47 
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 
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 
107  if ( ! _valid )
108  throw InvalidStateException( "Attempt to use an invalid DirectoryCollection" ) ;
109  loadEntries() ;
110 
111  return _entries.size() ;
112 }
113 
115  return new DirectoryCollection( *this ) ;
116 }
117 
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 */
virtual void setSize(uint32 size)
Sets the size field for the entry.
Definition: basicentry.cpp:118
virtual ConstEntries entries() const
Definition: dircoll.cpp:38
SimpleSmartPointer is a simple reference counting smart pointer template.
virtual ~DirectoryCollection()
Destructor.
Definition: dircoll.cpp:118
vector< EntryPointer > ConstEntries
ConstEntries is a vector of ConstEntryPointer&#39;s.
Definition: fileentry.h:43
bool isDirectory() const
Definition: filepath.h:143
Header file that defines DirectoryCollection.
BasicEntry is a FileEntry that is suitable as a base class for basic entries, that e...
Definition: basicentry.h:18
virtual ConstEntryPointer getEntry(const string &name, MatchPath matchpath=MATCH) const
Definition: fcoll.cpp:34
virtual istream * getInputStream(const ConstEntryPointer &entry)
Definition: dircoll.cpp:68
virtual void close()
Closes the FileCollection.
Definition: dircoll.cpp:33
DirectoryCollection()
Default Constructor.
Definition: dircoll.h:23
An object member function may throw this exception, if the operation it normally performs is inapprop...
virtual int size() const
Returns the number of entries in the FileCollection.
Definition: dircoll.cpp:106
virtual ConstEntryPointer getEntry(const string &name, MatchPath matchpath=MATCH) const
Definition: dircoll.cpp:49
virtual ConstEntries entries() const
Definition: fcoll.cpp:17
virtual FileCollection * clone() const
Create a heap allocated clone of the object this method is called for.
Definition: dircoll.cpp:114
FilePath represents a path to a file or directory name.
Definition: filepath.h:18