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__
15 #if wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS
17 #include "wx/stream.h"
18 #include "wx/filename.h"
21 /////////////////////////////////////////////////////////////////////////////
24 class WXDLLIMPEXP_BASE wxArchiveNotifier
27 virtual ~wxArchiveNotifier() { }
29 virtual void OnEntryUpdated(class wxArchiveEntry
& entry
) = 0;
33 /////////////////////////////////////////////////////////////////////////////
36 // Holds an entry's meta data, such as filename and timestamp.
38 class WXDLLIMPEXP_BASE wxArchiveEntry
: public wxObject
41 virtual ~wxArchiveEntry() { }
43 virtual wxDateTime
GetDateTime() const = 0;
44 virtual wxFileOffset
GetSize() const = 0;
45 virtual wxFileOffset
GetOffset() const = 0;
46 virtual bool IsDir() const = 0;
47 virtual bool IsReadOnly() const = 0;
48 virtual wxString
GetInternalName() const = 0;
49 virtual wxPathFormat
GetInternalFormat() const = 0;
50 virtual wxString
GetName(wxPathFormat format
= wxPATH_NATIVE
) const = 0;
52 virtual void SetDateTime(const wxDateTime
& dt
) = 0;
53 virtual void SetSize(wxFileOffset size
) = 0;
54 virtual void SetIsDir(bool isDir
= true) = 0;
55 virtual void SetIsReadOnly(bool isReadOnly
= true) = 0;
56 virtual void SetName(const wxString
& name
,
57 wxPathFormat format
= wxPATH_NATIVE
) = 0;
59 wxArchiveEntry
*Clone() const { return DoClone(); }
61 void SetNotifier(wxArchiveNotifier
& notifier
);
62 virtual void UnsetNotifier() { m_notifier
= NULL
; }
65 wxArchiveEntry() : m_notifier(NULL
) { }
66 wxArchiveEntry(const wxArchiveEntry
& e
) : wxObject(e
), m_notifier(NULL
) { }
68 virtual void SetOffset(wxFileOffset offset
) = 0;
69 virtual wxArchiveEntry
* DoClone() const = 0;
71 wxArchiveNotifier
*GetNotifier() const { return m_notifier
; }
72 wxArchiveEntry
& operator=(const wxArchiveEntry
& entry
);
75 wxArchiveNotifier
*m_notifier
;
77 DECLARE_ABSTRACT_CLASS(wxArchiveEntry
)
81 /////////////////////////////////////////////////////////////////////////////
82 // wxArchiveInputStream
84 // GetNextEntry() returns an wxArchiveEntry object containing the meta-data
85 // for the next entry in the archive (and gives away ownership). Reading from
86 // the wxArchiveInputStream then returns the entry's data. Eof() becomes true
87 // after an attempt has been made to read past the end of the entry's data.
89 // When there are no more entries, GetNextEntry() returns NULL and sets Eof().
91 class WXDLLIMPEXP_BASE wxArchiveInputStream
: public wxFilterInputStream
94 typedef wxArchiveEntry entry_type
;
96 virtual ~wxArchiveInputStream() { }
98 virtual bool OpenEntry(wxArchiveEntry
& entry
) = 0;
99 virtual bool CloseEntry() = 0;
101 wxArchiveEntry
*GetNextEntry() { return DoGetNextEntry(); }
103 virtual char Peek() { return wxInputStream::Peek(); }
106 wxArchiveInputStream(wxInputStream
& stream
, wxMBConv
& conv
);
108 virtual wxArchiveEntry
*DoGetNextEntry() = 0;
110 wxMBConv
& GetConv() const { return m_conv
; }
117 /////////////////////////////////////////////////////////////////////////////
118 // wxArchiveOutputStream
120 // PutNextEntry is used to create a new entry in the output archive, then
121 // the entry's data is written to the wxArchiveOutputStream.
123 // Only one entry can be open for output at a time; another call to
124 // PutNextEntry closes the current entry and begins the next.
126 // The overload 'bool PutNextEntry(wxArchiveEntry *entry)' takes ownership
127 // of the entry object.
129 class WXDLLIMPEXP_BASE wxArchiveOutputStream
: public wxFilterOutputStream
132 virtual ~wxArchiveOutputStream() { }
134 virtual bool PutNextEntry(wxArchiveEntry
*entry
) = 0;
136 virtual bool PutNextEntry(const wxString
& name
,
137 const wxDateTime
& dt
= wxDateTime::Now(),
138 wxFileOffset size
= wxInvalidOffset
) = 0;
140 virtual bool PutNextDirEntry(const wxString
& name
,
141 const wxDateTime
& dt
= wxDateTime::Now()) = 0;
143 virtual bool CopyEntry(wxArchiveEntry
*entry
,
144 wxArchiveInputStream
& stream
) = 0;
146 virtual bool CopyArchiveMetaData(wxArchiveInputStream
& stream
) = 0;
148 virtual bool CloseEntry() = 0;
151 wxArchiveOutputStream(wxOutputStream
& stream
, wxMBConv
& conv
);
153 wxMBConv
& GetConv() const { return m_conv
; }
160 /////////////////////////////////////////////////////////////////////////////
163 // An input iterator that can be used to transfer an archive's catalog to
166 #if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
170 template <class X
, class Y
> inline
171 void _wxSetArchiveIteratorValue(
172 X
& val
, Y entry
, void *WXUNUSED(d
))
176 template <class X
, class Y
, class Z
> inline
177 void _wxSetArchiveIteratorValue(
178 std::pair
<X
, Y
>& val
, Z entry
, Z
WXUNUSED(d
))
180 val
= std::make_pair(X(entry
->GetInternalName()), Y(entry
));
183 #if defined _MSC_VER && _MSC_VER < 1300
184 template <class Arc
, class T
= Arc::entry_type
*>
186 template <class Arc
, class T
= typename
Arc::entry_type
*>
188 class wxArchiveIterator
191 typedef std::input_iterator_tag iterator_category
;
192 typedef T value_type
;
193 typedef ptrdiff_t difference_type
;
195 typedef T
& reference
;
197 wxArchiveIterator() : m_rep(NULL
) { }
199 wxArchiveIterator(Arc
& arc
) {
200 typename
Arc::entry_type
* entry
= arc
.GetNextEntry();
201 m_rep
= entry
? new Rep(arc
, entry
) : NULL
;
204 wxArchiveIterator(const wxArchiveIterator
& it
) : m_rep(it
.m_rep
) {
209 ~wxArchiveIterator() {
214 const T
& operator *() const {
215 return m_rep
->GetValue();
218 const T
* operator ->() const {
222 wxArchiveIterator
& operator =(const wxArchiveIterator
& it
) {
231 wxArchiveIterator
& operator ++() {
232 m_rep
= m_rep
->Next();
236 wxArchiveIterator
operator ++(int) {
237 wxArchiveIterator
it(*this);
242 bool operator ==(const wxArchiveIterator
& j
) const {
243 return m_rep
== j
.m_rep
;
246 bool operator !=(const wxArchiveIterator
& j
) const {
247 return !(*this == j
);
253 typename
Arc::entry_type
* m_entry
;
258 Rep(Arc
& arc
, typename
Arc::entry_type
* entry
)
259 : m_arc(arc
), m_entry(entry
), m_value(), m_ref(1) { }
273 typename
Arc::entry_type
* entry
= m_arc
.GetNextEntry();
280 return new Rep(m_arc
, entry
);
288 const T
& GetValue() {
290 _wxSetArchiveIteratorValue(m_value
, m_entry
, m_entry
);
298 typedef wxArchiveIterator
<wxArchiveInputStream
> wxArchiveIter
;
299 typedef wxArchiveIterator
<wxArchiveInputStream
,
300 std::pair
<wxString
, wxArchiveEntry
*> > wxArchivePairIter
;
302 #endif // wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
305 /////////////////////////////////////////////////////////////////////////////
306 // wxArchiveClassFactory
308 // A wxArchiveClassFactory instance for a particular archive type allows
309 // the creation of the other classes that may be needed.
311 class WXDLLIMPEXP_BASE wxArchiveClassFactory
: public wxObject
314 typedef wxArchiveEntry entry_type
;
315 typedef wxArchiveInputStream instream_type
;
316 typedef wxArchiveOutputStream outstream_type
;
317 typedef wxArchiveNotifier notifier_type
;
318 #if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
319 typedef wxArchiveIter iter_type
;
320 typedef wxArchivePairIter pairiter_type
;
323 virtual ~wxArchiveClassFactory() { }
325 wxArchiveEntry
*NewEntry() const
326 { return DoNewEntry(); }
327 wxArchiveInputStream
*NewStream(wxInputStream
& stream
) const
328 { return DoNewStream(stream
); }
329 wxArchiveOutputStream
*NewStream(wxOutputStream
& stream
) const
330 { return DoNewStream(stream
); }
332 virtual wxString
GetInternalName(
333 const wxString
& name
,
334 wxPathFormat format
= wxPATH_NATIVE
) const = 0;
336 void SetConv(wxMBConv
& conv
) { m_pConv
= &conv
; }
337 wxMBConv
& GetConv() const { return *m_pConv
; }
340 // old compilers don't support covarient returns, so 'Do' methods are
341 // used to simulate them
342 virtual wxArchiveEntry
*DoNewEntry() const = 0;
343 virtual wxArchiveInputStream
*DoNewStream(wxInputStream
& stream
) const = 0;
344 virtual wxArchiveOutputStream
*DoNewStream(wxOutputStream
& stream
) const = 0;
346 wxArchiveClassFactory() : m_pConv(&wxConvLocal
) { }
347 wxArchiveClassFactory
& operator=(const wxArchiveClassFactory
& WXUNUSED(f
))
353 DECLARE_ABSTRACT_CLASS(wxArchiveClassFactory
)
356 #endif // wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS
358 #endif // _WX_ARCHIVE_H__