~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Base/zipios/dircoll.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam C. Powell, IV
  • Date: 2010-01-11 08:48:33 UTC
  • mfrom: (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100111084833-4g9vgdqbkw8u34zb
Tags: 0.9.2646.5-1
* New upstream version (closes: #561696).
* Added swig to Build-Depends (closes: #563523, #563772).
* Removed python-opencv from Build-Depends and Recommends (closes: #560768).

Show diffs side-by-side

added added

removed removed

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