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