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
) { }
71 virtual void SetOffset(wxFileOffset offset
) = 0;
72 virtual wxArchiveEntry
* DoClone() const = 0;
74 wxArchiveNotifier
*GetNotifier() const { return m_notifier
; }
75 wxArchiveEntry
& operator=(const wxArchiveEntry
& entry
);
78 wxArchiveNotifier
*m_notifier
;
80 DECLARE_ABSTRACT_CLASS(wxArchiveEntry
)
84 /////////////////////////////////////////////////////////////////////////////
85 // wxArchiveInputStream
87 // GetNextEntry() returns an wxArchiveEntry object containing the meta-data
88 // for the next entry in the archive (and gives away ownership). Reading from
89 // the wxArchiveInputStream then returns the entry's data. Eof() becomes true
90 // after an attempt has been made to read past the end of the entry's data.
92 // When there are no more entries, GetNextEntry() returns NULL and sets Eof().
94 class WXDLLIMPEXP_BASE wxArchiveInputStream
: public wxFilterInputStream
97 typedef wxArchiveEntry entry_type
;
99 virtual ~wxArchiveInputStream() { }
101 virtual bool OpenEntry(wxArchiveEntry
& entry
) = 0;
102 virtual bool CloseEntry() = 0;
104 wxArchiveEntry
*GetNextEntry() { return DoGetNextEntry(); }
106 virtual char Peek() { return wxInputStream::Peek(); }
109 wxArchiveInputStream(wxInputStream
& stream
, wxMBConv
& conv
);
111 virtual wxArchiveEntry
*DoGetNextEntry() = 0;
113 wxMBConv
& GetConv() const { return m_conv
; }
120 /////////////////////////////////////////////////////////////////////////////
121 // wxArchiveOutputStream
123 // PutNextEntry is used to create a new entry in the output archive, then
124 // the entry's data is written to the wxArchiveOutputStream.
126 // Only one entry can be open for output at a time; another call to
127 // PutNextEntry closes the current entry and begins the next.
129 // The overload 'bool PutNextEntry(wxArchiveEntry *entry)' takes ownership
130 // of the entry object.
132 class WXDLLIMPEXP_BASE wxArchiveOutputStream
: public wxFilterOutputStream
135 virtual ~wxArchiveOutputStream() { }
137 virtual bool PutNextEntry(wxArchiveEntry
*entry
) = 0;
139 virtual bool PutNextEntry(const wxString
& name
,
140 const wxDateTime
& dt
= wxDateTime::Now(),
141 wxFileOffset size
= wxInvalidOffset
) = 0;
143 virtual bool PutNextDirEntry(const wxString
& name
,
144 const wxDateTime
& dt
= wxDateTime::Now()) = 0;
146 virtual bool CopyEntry(wxArchiveEntry
*entry
,
147 wxArchiveInputStream
& stream
) = 0;
149 virtual bool CopyArchiveMetaData(wxArchiveInputStream
& stream
) = 0;
151 virtual bool CloseEntry() = 0;
154 wxArchiveOutputStream(wxOutputStream
& stream
, wxMBConv
& conv
);
156 wxMBConv
& GetConv() const { return m_conv
; }
163 /////////////////////////////////////////////////////////////////////////////
164 // wxArchiveClassFactory
166 // A wxArchiveClassFactory instance for a particular archive type allows
167 // the creation of the other classes that may be needed.
169 class WXDLLIMPEXP_BASE wxArchiveClassFactory
: public wxObject
172 virtual ~wxArchiveClassFactory() { }
174 wxArchiveEntry
*NewEntry() const
175 { return DoNewEntry(); }
176 wxArchiveInputStream
*NewStream(wxInputStream
& stream
) const
177 { return DoNewStream(stream
); }
178 wxArchiveOutputStream
*NewStream(wxOutputStream
& stream
) const
179 { return DoNewStream(stream
); }
181 virtual wxString
GetInternalName(
182 const wxString
& name
,
183 wxPathFormat format
= wxPATH_NATIVE
) const = 0;
185 void SetConv(wxMBConv
& conv
) { m_pConv
= &conv
; }
186 wxMBConv
& GetConv() const { return *m_pConv
; }
189 virtual wxArchiveEntry
*DoNewEntry() const = 0;
190 virtual wxArchiveInputStream
*DoNewStream(wxInputStream
& stream
) const = 0;
191 virtual wxArchiveOutputStream
*DoNewStream(wxOutputStream
& stream
) const = 0;
193 wxArchiveClassFactory() : m_pConv(&wxConvLocal
) { }
194 wxArchiveClassFactory
& operator=(const wxArchiveClassFactory
& WXUNUSED(f
))
200 DECLARE_ABSTRACT_CLASS(wxArchiveClassFactory
)
204 /////////////////////////////////////////////////////////////////////////////
207 // An input iterator that can be used to transfer an archive's catalog to
210 #if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
214 template <class X
, class Y
>
215 void WXDLLIMPEXP_BASE
_wxSetArchiveIteratorValue(
216 X
& val
, Y entry
, void *WXUNUSED(d
))
220 template <class X
, class Y
, class Z
>
221 void WXDLLIMPEXP_BASE
_wxSetArchiveIteratorValue(
222 std::pair
<X
, Y
>& val
, Z entry
, Z
WXUNUSED(d
))
224 val
= std::make_pair(X(entry
->GetInternalName()), Y(entry
));
227 #if defined _MSC_VER && _MSC_VER < 1300
228 template <class Arc
, class T
= Arc::entry_type
*>
230 template <class Arc
, class T
= typename
Arc::entry_type
*>
232 class WXDLLIMPEXP_BASE wxArchiveIterator
235 typedef std::input_iterator_tag iterator_category
;
236 typedef T value_type
;
237 typedef ptrdiff_t difference_type
;
239 typedef T
& reference
;
241 wxArchiveIterator() : m_rep(NULL
) { }
243 wxArchiveIterator(Arc
& arc
) {
244 typename
Arc::entry_type
* entry
= arc
.GetNextEntry();
245 m_rep
= entry
? new Rep(arc
, entry
) : NULL
;
248 wxArchiveIterator(const wxArchiveIterator
& it
) : m_rep(it
.m_rep
) {
253 ~wxArchiveIterator() {
258 const T
& operator *() const {
259 return m_rep
->GetValue();
262 const T
* operator ->() const {
266 wxArchiveIterator
& operator =(const wxArchiveIterator
& it
) {
275 wxArchiveIterator
& operator ++() {
276 m_rep
= m_rep
->Next();
280 wxArchiveIterator
operator ++(int) {
281 wxArchiveIterator
it(*this);
286 bool operator ==(const wxArchiveIterator
& j
) {
287 return (*this).m_rep
== j
.m_rep
;
290 bool operator !=(const wxArchiveIterator
& j
) {
291 return !(*this == j
);
297 typename
Arc::entry_type
* m_entry
;
302 Rep(Arc
& arc
, typename
Arc::entry_type
* entry
)
303 : m_arc(arc
), m_entry(entry
), m_value(), m_ref(1) { }
317 typename
Arc::entry_type
* entry
= m_arc
.GetNextEntry();
324 return new Rep(m_arc
, entry
);
332 const T
& GetValue() {
334 _wxSetArchiveIteratorValue(m_value
, m_entry
, m_entry
);
342 typedef wxArchiveIterator
<wxArchiveInputStream
> wxArchiveIter
;
343 typedef wxArchiveIterator
<wxArchiveInputStream
,
344 std::pair
<wxString
, wxArchiveEntry
*> > wxArchivePairIter
;
346 #endif // wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
348 #endif // wxUSE_STREAMS
350 #endif // _WX_ARCHIVE_H__