1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Streams for archive formats
4 // Author: Mike Wetherell
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_ARCHIVE_H__
11 #define _WX_ARCHIVE_H__
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma interface "archive.h"
19 #if wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM
21 #include "wx/stream.h"
22 #include "wx/filename.h"
25 /////////////////////////////////////////////////////////////////////////////
28 class WXDLLIMPEXP_BASE wxArchiveNotifier
31 virtual ~wxArchiveNotifier() { }
33 virtual void OnEntryUpdated(class wxArchiveEntry
& entry
) = 0;
37 /////////////////////////////////////////////////////////////////////////////
40 // Holds an entry's meta data, such as filename and timestamp.
42 class WXDLLIMPEXP_BASE wxArchiveEntry
: public wxObject
45 virtual ~wxArchiveEntry() { }
47 virtual wxDateTime
GetDateTime() const = 0;
48 virtual wxFileOffset
GetSize() const = 0;
49 virtual wxFileOffset
GetOffset() const = 0;
50 virtual bool IsDir() const = 0;
51 virtual bool IsReadOnly() const = 0;
52 virtual wxString
GetInternalName() const = 0;
53 virtual wxPathFormat
GetInternalFormat() const = 0;
54 virtual wxString
GetName(wxPathFormat format
= wxPATH_NATIVE
) const = 0;
56 virtual void SetDateTime(const wxDateTime
& dt
) = 0;
57 virtual void SetSize(wxFileOffset size
) = 0;
58 virtual void SetIsDir(bool isDir
= true) = 0;
59 virtual void SetIsReadOnly(bool isReadOnly
= true) = 0;
60 virtual void SetName(const wxString
& name
,
61 wxPathFormat format
= wxPATH_NATIVE
) = 0;
63 wxArchiveEntry
*Clone() const { return DoClone(); }
65 void SetNotifier(wxArchiveNotifier
& notifier
);
66 virtual void UnsetNotifier() { m_notifier
= NULL
; }
69 wxArchiveEntry() : m_notifier(NULL
) { }
70 wxArchiveEntry(const wxArchiveEntry
& e
) : wxObject(e
), m_notifier(NULL
) { }
72 virtual void SetOffset(wxFileOffset offset
) = 0;
73 virtual wxArchiveEntry
* DoClone() const = 0;
75 wxArchiveNotifier
*GetNotifier() const { return m_notifier
; }
76 wxArchiveEntry
& operator=(const wxArchiveEntry
& entry
);
79 wxArchiveNotifier
*m_notifier
;
81 DECLARE_ABSTRACT_CLASS(wxArchiveEntry
)
85 /////////////////////////////////////////////////////////////////////////////
86 // wxArchiveInputStream
88 // GetNextEntry() returns an wxArchiveEntry object containing the meta-data
89 // for the next entry in the archive (and gives away ownership). Reading from
90 // the wxArchiveInputStream then returns the entry's data. Eof() becomes true
91 // after an attempt has been made to read past the end of the entry's data.
93 // When there are no more entries, GetNextEntry() returns NULL and sets Eof().
95 class WXDLLIMPEXP_BASE wxArchiveInputStream
: public wxFilterInputStream
98 typedef wxArchiveEntry entry_type
;
100 virtual ~wxArchiveInputStream() { }
102 virtual bool OpenEntry(wxArchiveEntry
& entry
) = 0;
103 virtual bool CloseEntry() = 0;
105 wxArchiveEntry
*GetNextEntry() { return DoGetNextEntry(); }
107 virtual char Peek() { return wxInputStream::Peek(); }
110 wxArchiveInputStream(wxInputStream
& stream
, wxMBConv
& conv
);
112 virtual wxArchiveEntry
*DoGetNextEntry() = 0;
114 wxMBConv
& GetConv() const { return m_conv
; }
121 /////////////////////////////////////////////////////////////////////////////
122 // wxArchiveOutputStream
124 // PutNextEntry is used to create a new entry in the output archive, then
125 // the entry's data is written to the wxArchiveOutputStream.
127 // Only one entry can be open for output at a time; another call to
128 // PutNextEntry closes the current entry and begins the next.
130 // The overload 'bool PutNextEntry(wxArchiveEntry *entry)' takes ownership
131 // of the entry object.
133 class WXDLLIMPEXP_BASE wxArchiveOutputStream
: public wxFilterOutputStream
136 virtual ~wxArchiveOutputStream() { }
138 virtual bool PutNextEntry(wxArchiveEntry
*entry
) = 0;
140 virtual bool PutNextEntry(const wxString
& name
,
141 const wxDateTime
& dt
= wxDateTime::Now(),
142 wxFileOffset size
= wxInvalidOffset
) = 0;
144 virtual bool PutNextDirEntry(const wxString
& name
,
145 const wxDateTime
& dt
= wxDateTime::Now()) = 0;
147 virtual bool CopyEntry(wxArchiveEntry
*entry
,
148 wxArchiveInputStream
& stream
) = 0;
150 virtual bool CopyArchiveMetaData(wxArchiveInputStream
& stream
) = 0;
152 virtual bool CloseEntry() = 0;
155 wxArchiveOutputStream(wxOutputStream
& stream
, wxMBConv
& conv
);
157 wxMBConv
& GetConv() const { return m_conv
; }
164 /////////////////////////////////////////////////////////////////////////////
165 // wxArchiveClassFactory
167 // A wxArchiveClassFactory instance for a particular archive type allows
168 // the creation of the other classes that may be needed.
170 class WXDLLIMPEXP_BASE wxArchiveClassFactory
: public wxObject
173 virtual ~wxArchiveClassFactory() { }
175 wxArchiveEntry
*NewEntry() const
176 { return DoNewEntry(); }
177 wxArchiveInputStream
*NewStream(wxInputStream
& stream
) const
178 { return DoNewStream(stream
); }
179 wxArchiveOutputStream
*NewStream(wxOutputStream
& stream
) const
180 { return DoNewStream(stream
); }
182 virtual wxString
GetInternalName(
183 const wxString
& name
,
184 wxPathFormat format
= wxPATH_NATIVE
) const = 0;
186 void SetConv(wxMBConv
& conv
) { m_pConv
= &conv
; }
187 wxMBConv
& GetConv() const { return *m_pConv
; }
190 virtual wxArchiveEntry
*DoNewEntry() const = 0;
191 virtual wxArchiveInputStream
*DoNewStream(wxInputStream
& stream
) const = 0;
192 virtual wxArchiveOutputStream
*DoNewStream(wxOutputStream
& stream
) const = 0;
194 wxArchiveClassFactory() : m_pConv(&wxConvLocal
) { }
195 wxArchiveClassFactory
& operator=(const wxArchiveClassFactory
& WXUNUSED(f
))
201 DECLARE_ABSTRACT_CLASS(wxArchiveClassFactory
)
205 /////////////////////////////////////////////////////////////////////////////
208 // An input iterator that can be used to transfer an archive's catalog to
211 #if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
215 template <class X
, class Y
> inline
216 void _wxSetArchiveIteratorValue(
217 X
& val
, Y entry
, void *WXUNUSED(d
))
221 template <class X
, class Y
, class Z
> inline
222 void _wxSetArchiveIteratorValue(
223 std::pair
<X
, Y
>& val
, Z entry
, Z
WXUNUSED(d
))
225 val
= std::make_pair(X(entry
->GetInternalName()), Y(entry
));
228 #if defined _MSC_VER && _MSC_VER < 1300
229 template <class Arc
, class T
= Arc::entry_type
*>
231 template <class Arc
, class T
= typename
Arc::entry_type
*>
233 class wxArchiveIterator
236 typedef std::input_iterator_tag iterator_category
;
237 typedef T value_type
;
238 typedef ptrdiff_t difference_type
;
240 typedef T
& reference
;
242 wxArchiveIterator() : m_rep(NULL
) { }
244 wxArchiveIterator(Arc
& arc
) {
245 typename
Arc::entry_type
* entry
= arc
.GetNextEntry();
246 m_rep
= entry
? new Rep(arc
, entry
) : NULL
;
249 wxArchiveIterator(const wxArchiveIterator
& it
) : m_rep(it
.m_rep
) {
254 ~wxArchiveIterator() {
259 const T
& operator *() const {
260 return m_rep
->GetValue();
263 const T
* operator ->() const {
267 wxArchiveIterator
& operator =(const wxArchiveIterator
& it
) {
276 wxArchiveIterator
& operator ++() {
277 m_rep
= m_rep
->Next();
281 wxArchiveIterator
operator ++(int) {
282 wxArchiveIterator
it(*this);
287 bool operator ==(const wxArchiveIterator
& j
) const {
288 return m_rep
== j
.m_rep
;
291 bool operator !=(const wxArchiveIterator
& j
) const {
292 return !(*this == j
);
298 typename
Arc::entry_type
* m_entry
;
303 Rep(Arc
& arc
, typename
Arc::entry_type
* entry
)
304 : m_arc(arc
), m_entry(entry
), m_value(), m_ref(1) { }
318 typename
Arc::entry_type
* entry
= m_arc
.GetNextEntry();
325 return new Rep(m_arc
, entry
);
333 const T
& GetValue() {
335 _wxSetArchiveIteratorValue(m_value
, m_entry
, m_entry
);
343 typedef wxArchiveIterator
<wxArchiveInputStream
> wxArchiveIter
;
344 typedef wxArchiveIterator
<wxArchiveInputStream
,
345 std::pair
<wxString
, wxArchiveEntry
*> > wxArchivePairIter
;
347 #endif // wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
349 #endif // wxUSE_STREAMS
351 #endif // _WX_ARCHIVE_H__