]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_arc.cpp
Fix assert during separator items creation introduced by r64226.
[wxWidgets.git] / src / common / fs_arc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: fs_arc.cpp
3 // Purpose: wxArchive file system
4 // Author: Vaclav Slavik, Mike Wetherell
5 // Copyright: (c) 1999 Vaclav Slavik, (c) 2006 Mike Wetherell
6 // CVS-ID: $Id$
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #include "wx/wxprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 #if wxUSE_FS_ARCHIVE
17
18 #include "wx/fs_arc.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/intl.h"
22 #include "wx/log.h"
23 #endif
24
25 #if WXWIN_COMPATIBILITY_2_6 && wxUSE_ZIPSTREAM
26 #include "wx/zipstrm.h"
27 #else
28 #include "wx/archive.h"
29 #endif
30
31 #include "wx/private/fileback.h"
32
33 //---------------------------------------------------------------------------
34 // wxArchiveFSCacheDataImpl
35 //
36 // Holds the catalog of an archive file, and if it is being read from a
37 // non-seekable stream, a copy of its backing file.
38 //
39 // This class is actually the reference counted implementation for the
40 // wxArchiveFSCacheData class below. It was done that way to allow sharing
41 // between instances of wxFileSystem, though that's a feature not used in this
42 // version.
43 //---------------------------------------------------------------------------
44
45 WX_DECLARE_STRING_HASH_MAP(wxArchiveEntry*, wxArchiveFSEntryHash);
46
47 struct wxArchiveFSEntry
48 {
49 wxArchiveEntry *entry;
50 wxArchiveFSEntry *next;
51 };
52
53 class wxArchiveFSCacheDataImpl
54 {
55 public:
56 wxArchiveFSCacheDataImpl(const wxArchiveClassFactory& factory,
57 const wxBackingFile& backer);
58 wxArchiveFSCacheDataImpl(const wxArchiveClassFactory& factory,
59 wxInputStream *stream);
60
61 ~wxArchiveFSCacheDataImpl();
62
63 void Release() { if (--m_refcount == 0) delete this; }
64 wxArchiveFSCacheDataImpl *AddRef() { m_refcount++; return this; }
65
66 wxArchiveEntry *Get(const wxString& name);
67 wxInputStream *NewStream() const;
68
69 wxArchiveFSEntry *GetNext(wxArchiveFSEntry *fse);
70
71 private:
72 wxArchiveFSEntry *AddToCache(wxArchiveEntry *entry);
73 void CloseStreams();
74
75 int m_refcount;
76
77 wxArchiveFSEntryHash m_hash;
78 wxArchiveFSEntry *m_begin;
79 wxArchiveFSEntry **m_endptr;
80
81 wxBackingFile m_backer;
82 wxInputStream *m_stream;
83 wxArchiveInputStream *m_archive;
84 };
85
86 wxArchiveFSCacheDataImpl::wxArchiveFSCacheDataImpl(
87 const wxArchiveClassFactory& factory,
88 const wxBackingFile& backer)
89 : m_refcount(1),
90 m_begin(NULL),
91 m_endptr(&m_begin),
92 m_backer(backer),
93 m_stream(new wxBackedInputStream(backer)),
94 m_archive(factory.NewStream(*m_stream))
95 {
96 }
97
98 wxArchiveFSCacheDataImpl::wxArchiveFSCacheDataImpl(
99 const wxArchiveClassFactory& factory,
100 wxInputStream *stream)
101 : m_refcount(1),
102 m_begin(NULL),
103 m_endptr(&m_begin),
104 m_stream(stream),
105 m_archive(factory.NewStream(*m_stream))
106 {
107 }
108
109 wxArchiveFSCacheDataImpl::~wxArchiveFSCacheDataImpl()
110 {
111 WX_CLEAR_HASH_MAP(wxArchiveFSEntryHash, m_hash);
112
113 wxArchiveFSEntry *entry = m_begin;
114
115 while (entry)
116 {
117 wxArchiveFSEntry *next = entry->next;
118 delete entry;
119 entry = next;
120 }
121
122 CloseStreams();
123 }
124
125 wxArchiveFSEntry *wxArchiveFSCacheDataImpl::AddToCache(wxArchiveEntry *entry)
126 {
127 m_hash[entry->GetName(wxPATH_UNIX)] = entry;
128 wxArchiveFSEntry *fse = new wxArchiveFSEntry;
129 *m_endptr = fse;
130 (*m_endptr)->entry = entry;
131 (*m_endptr)->next = NULL;
132 m_endptr = &(*m_endptr)->next;
133 return fse;
134 }
135
136 void wxArchiveFSCacheDataImpl::CloseStreams()
137 {
138 delete m_archive;
139 m_archive = NULL;
140 delete m_stream;
141 m_stream = NULL;
142 }
143
144 wxArchiveEntry *wxArchiveFSCacheDataImpl::Get(const wxString& name)
145 {
146 wxArchiveFSEntryHash::iterator it = m_hash.find(name);
147
148 if (it != m_hash.end())
149 return it->second;
150
151 if (!m_archive)
152 return NULL;
153
154 wxArchiveEntry *entry;
155
156 while ((entry = m_archive->GetNextEntry()) != NULL)
157 {
158 AddToCache(entry);
159
160 if (entry->GetName(wxPATH_UNIX) == name)
161 return entry;
162 }
163
164 CloseStreams();
165
166 return NULL;
167 }
168
169 wxInputStream* wxArchiveFSCacheDataImpl::NewStream() const
170 {
171 if (m_backer)
172 return new wxBackedInputStream(m_backer);
173 else
174 return NULL;
175 }
176
177 wxArchiveFSEntry *wxArchiveFSCacheDataImpl::GetNext(wxArchiveFSEntry *fse)
178 {
179 wxArchiveFSEntry *next = fse ? fse->next : m_begin;
180
181 if (!next && m_archive)
182 {
183 wxArchiveEntry *entry = m_archive->GetNextEntry();
184
185 if (entry)
186 next = AddToCache(entry);
187 else
188 CloseStreams();
189 }
190
191 return next;
192 }
193
194 //---------------------------------------------------------------------------
195 // wxArchiveFSCacheData
196 //
197 // This is the inteface for wxArchiveFSCacheDataImpl above. Holds the catalog
198 // of an archive file, and if it is being read from a non-seekable stream, a
199 // copy of its backing file.
200 //---------------------------------------------------------------------------
201
202 class wxArchiveFSCacheData
203 {
204 public:
205 wxArchiveFSCacheData() : m_impl(NULL) { }
206 wxArchiveFSCacheData(const wxArchiveClassFactory& factory,
207 const wxBackingFile& backer);
208 wxArchiveFSCacheData(const wxArchiveClassFactory& factory,
209 wxInputStream *stream);
210
211 wxArchiveFSCacheData(const wxArchiveFSCacheData& data);
212 wxArchiveFSCacheData& operator=(const wxArchiveFSCacheData& data);
213
214 ~wxArchiveFSCacheData() { if (m_impl) m_impl->Release(); }
215
216 wxArchiveEntry *Get(const wxString& name) { return m_impl->Get(name); }
217 wxInputStream *NewStream() const { return m_impl->NewStream(); }
218 wxArchiveFSEntry *GetNext(wxArchiveFSEntry *fse)
219 { return m_impl->GetNext(fse); }
220
221 private:
222 wxArchiveFSCacheDataImpl *m_impl;
223 };
224
225 wxArchiveFSCacheData::wxArchiveFSCacheData(
226 const wxArchiveClassFactory& factory,
227 const wxBackingFile& backer)
228 : m_impl(new wxArchiveFSCacheDataImpl(factory, backer))
229 {
230 }
231
232 wxArchiveFSCacheData::wxArchiveFSCacheData(
233 const wxArchiveClassFactory& factory,
234 wxInputStream *stream)
235 : m_impl(new wxArchiveFSCacheDataImpl(factory, stream))
236 {
237 }
238
239 wxArchiveFSCacheData::wxArchiveFSCacheData(const wxArchiveFSCacheData& data)
240 : m_impl(data.m_impl ? data.m_impl->AddRef() : NULL)
241 {
242 }
243
244 wxArchiveFSCacheData& wxArchiveFSCacheData::operator=(
245 const wxArchiveFSCacheData& data)
246 {
247 if (data.m_impl != m_impl)
248 {
249 if (m_impl)
250 m_impl->Release();
251
252 m_impl = data.m_impl;
253
254 if (m_impl)
255 m_impl->AddRef();
256 }
257
258 return *this;
259 }
260
261 //---------------------------------------------------------------------------
262 // wxArchiveFSCache
263 //
264 // wxArchiveFSCacheData caches a single archive, and this class holds a
265 // collection of them to cache all the archives accessed by this instance
266 // of wxFileSystem.
267 //---------------------------------------------------------------------------
268
269 WX_DECLARE_STRING_HASH_MAP(wxArchiveFSCacheData, wxArchiveFSCacheDataHash);
270
271 class wxArchiveFSCache
272 {
273 public:
274 wxArchiveFSCache() { }
275 ~wxArchiveFSCache() { }
276
277 wxArchiveFSCacheData* Add(const wxString& name,
278 const wxArchiveClassFactory& factory,
279 wxInputStream *stream);
280
281 wxArchiveFSCacheData *Get(const wxString& name);
282
283 private:
284 wxArchiveFSCacheDataHash m_hash;
285 };
286
287 wxArchiveFSCacheData* wxArchiveFSCache::Add(
288 const wxString& name,
289 const wxArchiveClassFactory& factory,
290 wxInputStream *stream)
291 {
292 wxArchiveFSCacheData& data = m_hash[name];
293
294 if (stream->IsSeekable())
295 data = wxArchiveFSCacheData(factory, stream);
296 else
297 data = wxArchiveFSCacheData(factory, wxBackingFile(stream));
298
299 return &data;
300 }
301
302 wxArchiveFSCacheData *wxArchiveFSCache::Get(const wxString& name)
303 {
304 wxArchiveFSCacheDataHash::iterator it;
305
306 if ((it = m_hash.find(name)) != m_hash.end())
307 return &it->second;
308
309 return NULL;
310 }
311
312 //----------------------------------------------------------------------------
313 // wxArchiveFSHandler
314 //----------------------------------------------------------------------------
315
316 IMPLEMENT_DYNAMIC_CLASS(wxArchiveFSHandler, wxFileSystemHandler)
317
318 wxArchiveFSHandler::wxArchiveFSHandler()
319 : wxFileSystemHandler()
320 {
321 m_Archive = NULL;
322 m_FindEntry = NULL;
323 m_ZipFile = m_Pattern = m_BaseDir = wxEmptyString;
324 m_AllowDirs = m_AllowFiles = true;
325 m_DirsFound = NULL;
326 m_cache = NULL;
327 }
328
329 wxArchiveFSHandler::~wxArchiveFSHandler()
330 {
331 Cleanup();
332 delete m_cache;
333 }
334
335 void wxArchiveFSHandler::Cleanup()
336 {
337 wxDELETE(m_DirsFound);
338 }
339
340 bool wxArchiveFSHandler::CanOpen(const wxString& location)
341 {
342 wxString p = GetProtocol(location);
343 return wxArchiveClassFactory::Find(p) != NULL;
344 }
345
346 wxFSFile* wxArchiveFSHandler::OpenFile(
347 wxFileSystem& WXUNUSED(fs),
348 const wxString& location)
349 {
350 wxString right = GetRightLocation(location);
351 wxString left = GetLeftLocation(location);
352 wxString protocol = GetProtocol(location);
353 wxString key = left + wxT("#") + protocol + wxT(":");
354
355 if (right.Contains(wxT("./")))
356 {
357 if (right.GetChar(0) != wxT('/')) right = wxT('/') + right;
358 wxFileName rightPart(right, wxPATH_UNIX);
359 rightPart.Normalize(wxPATH_NORM_DOTS, wxT("/"), wxPATH_UNIX);
360 right = rightPart.GetFullPath(wxPATH_UNIX);
361 }
362
363 if (right.GetChar(0) == wxT('/')) right = right.Mid(1);
364
365 if (!m_cache)
366 m_cache = new wxArchiveFSCache;
367
368 const wxArchiveClassFactory *factory;
369 factory = wxArchiveClassFactory::Find(protocol);
370 if (!factory)
371 return NULL;
372
373 wxArchiveFSCacheData *cached = m_cache->Get(key);
374 if (!cached)
375 {
376 wxFSFile *leftFile = m_fs.OpenFile(left);
377 if (!leftFile)
378 return NULL;
379 cached = m_cache->Add(key, *factory, leftFile->DetachStream());
380 delete leftFile;
381 }
382
383 wxArchiveEntry *entry = cached->Get(right);
384 if (!entry)
385 return NULL;
386
387 wxInputStream *leftStream = cached->NewStream();
388 if (!leftStream)
389 {
390 wxFSFile *leftFile = m_fs.OpenFile(left);
391 if (!leftFile)
392 return NULL;
393 leftStream = leftFile->DetachStream();
394 delete leftFile;
395 }
396
397 wxArchiveInputStream *s = factory->NewStream(leftStream);
398 if ( !s )
399 return NULL;
400
401 s->OpenEntry(*entry);
402
403 if (!s->IsOk())
404 {
405 delete s;
406 return NULL;
407 }
408
409 #if WXWIN_COMPATIBILITY_2_6 && wxUSE_ZIPSTREAM
410 if (factory->IsKindOf(CLASSINFO(wxZipClassFactory)))
411 ((wxZipInputStream*)s)->m_allowSeeking = true;
412 #endif // WXWIN_COMPATIBILITY_2_6
413
414 return new wxFSFile(s,
415 key + right,
416 wxEmptyString,
417 GetAnchor(location)
418 #if wxUSE_DATETIME
419 , entry->GetDateTime()
420 #endif // wxUSE_DATETIME
421 );
422 }
423
424 wxString wxArchiveFSHandler::FindFirst(const wxString& spec, int flags)
425 {
426 wxString right = GetRightLocation(spec);
427 wxString left = GetLeftLocation(spec);
428 wxString protocol = GetProtocol(spec);
429 wxString key = left + wxT("#") + protocol + wxT(":");
430
431 if (!right.empty() && right.Last() == wxT('/')) right.RemoveLast();
432
433 if (!m_cache)
434 m_cache = new wxArchiveFSCache;
435
436 const wxArchiveClassFactory *factory;
437 factory = wxArchiveClassFactory::Find(protocol);
438 if (!factory)
439 return wxEmptyString;
440
441 m_Archive = m_cache->Get(key);
442 if (!m_Archive)
443 {
444 wxFSFile *leftFile = m_fs.OpenFile(left);
445 if (!leftFile)
446 return wxEmptyString;
447 m_Archive = m_cache->Add(key, *factory, leftFile->DetachStream());
448 delete leftFile;
449 }
450
451 m_FindEntry = NULL;
452
453 switch (flags)
454 {
455 case wxFILE:
456 m_AllowDirs = false, m_AllowFiles = true; break;
457 case wxDIR:
458 m_AllowDirs = true, m_AllowFiles = false; break;
459 default:
460 m_AllowDirs = m_AllowFiles = true; break;
461 }
462
463 m_ZipFile = key;
464
465 m_Pattern = right.AfterLast(wxT('/'));
466 m_BaseDir = right.BeforeLast(wxT('/'));
467 if (m_BaseDir.StartsWith(wxT("/")))
468 m_BaseDir = m_BaseDir.Mid(1);
469
470 if (m_Archive)
471 {
472 if (m_AllowDirs)
473 {
474 delete m_DirsFound;
475 m_DirsFound = new wxArchiveFilenameHashMap();
476 if (right.empty()) // allow "/" to match the archive root
477 return spec;
478 }
479 return DoFind();
480 }
481 return wxEmptyString;
482 }
483
484 wxString wxArchiveFSHandler::FindNext()
485 {
486 if (!m_Archive) return wxEmptyString;
487 return DoFind();
488 }
489
490 wxString wxArchiveFSHandler::DoFind()
491 {
492 wxString namestr, dir, filename;
493 wxString match = wxEmptyString;
494
495 while (match == wxEmptyString)
496 {
497 m_FindEntry = m_Archive->GetNext(m_FindEntry);
498
499 if (!m_FindEntry)
500 {
501 m_Archive = NULL;
502 m_FindEntry = NULL;
503 break;
504 }
505 namestr = m_FindEntry->entry->GetName(wxPATH_UNIX);
506
507 if (m_AllowDirs)
508 {
509 dir = namestr.BeforeLast(wxT('/'));
510 while (!dir.empty())
511 {
512 if( m_DirsFound->find(dir) == m_DirsFound->end() )
513 {
514 (*m_DirsFound)[dir] = 1;
515 filename = dir.AfterLast(wxT('/'));
516 dir = dir.BeforeLast(wxT('/'));
517 if (!filename.empty() && m_BaseDir == dir &&
518 wxMatchWild(m_Pattern, filename, false))
519 match = m_ZipFile + dir + wxT("/") + filename;
520 }
521 else
522 break; // already tranversed
523 }
524 }
525
526 filename = namestr.AfterLast(wxT('/'));
527 dir = namestr.BeforeLast(wxT('/'));
528 if (m_AllowFiles && !filename.empty() && m_BaseDir == dir &&
529 wxMatchWild(m_Pattern, filename, false))
530 match = m_ZipFile + namestr;
531 }
532
533 return match;
534 }
535
536 #endif // wxUSE_FS_ARCHIVE