]> git.donarmstrong.com Git - flightcrew.git/blob - src/zipios/src/dircoll.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / zipios / src / dircoll.cpp
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 "../FlightCrew/Misc/BoostFilesystemUse.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 (int) _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 ( fs::directory_iterator it( (_filepath + subdir).asString() ) ; it != fs::directory_iterator() ; ++it ) {
135
136     if ( *it == "." || *it == ".." || *it == "..." )
137       continue ;
138
139     if ( fs::is_directory( it->path() ) && recursive ) {
140       load( recursive, subdir + it->path().filename().string() ) ;
141     } else {
142       _entries.push_back( ent = new BasicEntry( subdir + it->path().filename().string(), "", _filepath ) ) ;
143       ent->setSize( (zipios::uint32) fs::file_size( it->path() ) ) ;
144     }
145
146   }
147 }
148
149 } // namespace
150
151 /** \file
152     Implementation of DirectoryCollection.
153 */
154
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 */