+ wxArchiveEntry *entry;
+ wxArchiveFSEntry *next;
+};
+
+class wxArchiveFSCacheDataImpl
+{
+public:
+ wxArchiveFSCacheDataImpl(const wxArchiveClassFactory& factory,
+ const wxBackingFile& backer);
+ wxArchiveFSCacheDataImpl(const wxArchiveClassFactory& factory,
+ wxInputStream *stream);
+
+ ~wxArchiveFSCacheDataImpl();
+
+ void Release() { if (--m_refcount == 0) delete this; }
+ wxArchiveFSCacheDataImpl *AddRef() { m_refcount++; return this; }
+
+ wxArchiveEntry *Get(const wxString& name);
+ wxInputStream *NewStream() const;
+
+ wxArchiveFSEntry *GetNext(wxArchiveFSEntry *fse);
+
+private:
+ wxArchiveFSEntry *AddToCache(wxArchiveEntry *entry);
+ void CloseStreams();
+
+ int m_refcount;
+
+ wxArchiveFSEntryHash m_hash;
+ wxArchiveFSEntry *m_begin;
+ wxArchiveFSEntry **m_endptr;
+
+ wxBackingFile m_backer;
+ wxInputStream *m_stream;
+ wxArchiveInputStream *m_archive;
+};
+
+wxArchiveFSCacheDataImpl::wxArchiveFSCacheDataImpl(
+ const wxArchiveClassFactory& factory,
+ const wxBackingFile& backer)
+ : m_refcount(1),
+ m_begin(NULL),
+ m_endptr(&m_begin),
+ m_backer(backer),
+ m_stream(new wxBackedInputStream(backer)),
+ m_archive(factory.NewStream(*m_stream))
+{
+}
+
+wxArchiveFSCacheDataImpl::wxArchiveFSCacheDataImpl(
+ const wxArchiveClassFactory& factory,
+ wxInputStream *stream)
+ : m_refcount(1),
+ m_begin(NULL),
+ m_endptr(&m_begin),
+ m_stream(stream),
+ m_archive(factory.NewStream(*m_stream))
+{
+}
+
+wxArchiveFSCacheDataImpl::~wxArchiveFSCacheDataImpl()
+{
+ WX_CLEAR_HASH_MAP(wxArchiveFSEntryHash, m_hash);
+
+ wxArchiveFSEntry *entry = m_begin;
+
+ while (entry)
+ {
+ wxArchiveFSEntry *next = entry->next;
+ delete entry;
+ entry = next;
+ }
+
+ CloseStreams();
+}
+
+wxArchiveFSEntry *wxArchiveFSCacheDataImpl::AddToCache(wxArchiveEntry *entry)
+{
+ m_hash[entry->GetName(wxPATH_UNIX)] = entry;
+ wxArchiveFSEntry *fse = new wxArchiveFSEntry;
+ *m_endptr = fse;
+ (*m_endptr)->entry = entry;
+ (*m_endptr)->next = NULL;
+ m_endptr = &(*m_endptr)->next;
+ return fse;
+}
+
+void wxArchiveFSCacheDataImpl::CloseStreams()
+{
+ delete m_archive;
+ m_archive = NULL;
+ delete m_stream;
+ m_stream = NULL;
+}
+
+wxArchiveEntry *wxArchiveFSCacheDataImpl::Get(const wxString& name)
+{
+ wxArchiveFSEntryHash::iterator it = m_hash.find(name);
+
+ if (it != m_hash.end())
+ return it->second;
+
+ if (!m_archive)
+ return NULL;
+
+ wxArchiveEntry *entry;
+
+ while ((entry = m_archive->GetNextEntry()) != NULL)
+ {
+ AddToCache(entry);
+
+ if (entry->GetName(wxPATH_UNIX) == name)
+ return entry;
+ }
+
+ CloseStreams();
+
+ return NULL;
+}
+
+wxInputStream* wxArchiveFSCacheDataImpl::NewStream() const
+{
+ if (m_backer)
+ return new wxBackedInputStream(m_backer);
+ else
+ return NULL;
+}
+
+wxArchiveFSEntry *wxArchiveFSCacheDataImpl::GetNext(wxArchiveFSEntry *fse)
+{
+ wxArchiveFSEntry *next = fse ? fse->next : m_begin;
+
+ if (!next && m_archive)
+ {
+ wxArchiveEntry *entry = m_archive->GetNextEntry();
+
+ if (entry)
+ next = AddToCache(entry);
+ else
+ CloseStreams();
+ }
+
+ return next;
+}
+
+//---------------------------------------------------------------------------
+// wxArchiveFSCacheData
+//
+// This is the inteface for wxArchiveFSCacheDataImpl above. Holds the catalog
+// of an archive file, and if it is being read from a non-seekable stream, a
+// copy of its backing file.
+//---------------------------------------------------------------------------
+
+class wxArchiveFSCacheData
+{
+public:
+ wxArchiveFSCacheData() : m_impl(NULL) { }
+ wxArchiveFSCacheData(const wxArchiveClassFactory& factory,
+ const wxBackingFile& backer);
+ wxArchiveFSCacheData(const wxArchiveClassFactory& factory,
+ wxInputStream *stream);
+
+ wxArchiveFSCacheData(const wxArchiveFSCacheData& data);
+ wxArchiveFSCacheData& operator=(const wxArchiveFSCacheData& data);
+
+ ~wxArchiveFSCacheData() { if (m_impl) m_impl->Release(); }