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
);
107 wxArchiveInputStream(wxInputStream
*stream
, wxMBConv
& conv
);
109 virtual wxArchiveEntry
*DoGetNextEntry() = 0;
111 wxMBConv
& GetConv() const { return m_conv
; }
118 /////////////////////////////////////////////////////////////////////////////
119 // wxArchiveOutputStream
121 // PutNextEntry is used to create a new entry in the output archive, then
122 // the entry's data is written to the wxArchiveOutputStream.
124 // Only one entry can be open for output at a time; another call to
125 // PutNextEntry closes the current entry and begins the next.
127 // The overload 'bool PutNextEntry(wxArchiveEntry *entry)' takes ownership
128 // of the entry object.
130 class WXDLLIMPEXP_BASE wxArchiveOutputStream
: public wxFilterOutputStream
133 virtual ~wxArchiveOutputStream() { }
135 virtual bool PutNextEntry(wxArchiveEntry
*entry
) = 0;
137 virtual bool PutNextEntry(const wxString
& name
,
138 const wxDateTime
& dt
= wxDateTime::Now(),
139 wxFileOffset size
= wxInvalidOffset
) = 0;
141 virtual bool PutNextDirEntry(const wxString
& name
,
142 const wxDateTime
& dt
= wxDateTime::Now()) = 0;
144 virtual bool CopyEntry(wxArchiveEntry
*entry
,
145 wxArchiveInputStream
& stream
) = 0;
147 virtual bool CopyArchiveMetaData(wxArchiveInputStream
& stream
) = 0;
149 virtual bool CloseEntry() = 0;
152 wxArchiveOutputStream(wxOutputStream
& stream
, wxMBConv
& conv
);
153 wxArchiveOutputStream(wxOutputStream
*stream
, wxMBConv
& conv
);
155 wxMBConv
& GetConv() const { return m_conv
; }
162 /////////////////////////////////////////////////////////////////////////////
165 // An input iterator that can be used to transfer an archive's catalog to
168 #if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
172 template <class X
, class Y
> inline
173 void _wxSetArchiveIteratorValue(
174 X
& val
, Y entry
, void *WXUNUSED(d
))
178 template <class X
, class Y
, class Z
> inline
179 void _wxSetArchiveIteratorValue(
180 std::pair
<X
, Y
>& val
, Z entry
, Z
WXUNUSED(d
))
182 val
= std::make_pair(X(entry
->GetInternalName()), Y(entry
));
185 #if defined _MSC_VER && _MSC_VER < 1300
186 template <class Arc
, class T
= Arc::entry_type
*>
188 template <class Arc
, class T
= typename
Arc::entry_type
*>
190 class wxArchiveIterator
193 typedef std::input_iterator_tag iterator_category
;
194 typedef T value_type
;
195 typedef ptrdiff_t difference_type
;
197 typedef T
& reference
;
199 wxArchiveIterator() : m_rep(NULL
) { }
201 wxArchiveIterator(Arc
& arc
) {
202 typename
Arc::entry_type
* entry
= arc
.GetNextEntry();
203 m_rep
= entry
? new Rep(arc
, entry
) : NULL
;
206 wxArchiveIterator(const wxArchiveIterator
& it
) : m_rep(it
.m_rep
) {
211 ~wxArchiveIterator() {
216 const T
& operator *() const {
217 return m_rep
->GetValue();
220 const T
* operator ->() const {
224 wxArchiveIterator
& operator =(const wxArchiveIterator
& it
) {
233 wxArchiveIterator
& operator ++() {
234 m_rep
= m_rep
->Next();
238 wxArchiveIterator
operator ++(int) {
239 wxArchiveIterator
it(*this);
244 bool operator ==(const wxArchiveIterator
& j
) const {
245 return m_rep
== j
.m_rep
;
248 bool operator !=(const wxArchiveIterator
& j
) const {
249 return !(*this == j
);
255 typename
Arc::entry_type
* m_entry
;
260 Rep(Arc
& arc
, typename
Arc::entry_type
* entry
)
261 : m_arc(arc
), m_entry(entry
), m_value(), m_ref(1) { }
275 typename
Arc::entry_type
* entry
= m_arc
.GetNextEntry();
282 return new Rep(m_arc
, entry
);
290 const T
& GetValue() {
292 _wxSetArchiveIteratorValue(m_value
, m_entry
, m_entry
);
300 typedef wxArchiveIterator
<wxArchiveInputStream
> wxArchiveIter
;
301 typedef wxArchiveIterator
<wxArchiveInputStream
,
302 std::pair
<wxString
, wxArchiveEntry
*> > wxArchivePairIter
;
304 #endif // wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
307 /////////////////////////////////////////////////////////////////////////////
308 // wxArchiveClassFactory
310 // A wxArchiveClassFactory instance for a particular archive type allows
311 // the creation of the other classes that may be needed.
313 void WXDLLIMPEXP_BASE
wxUseArchiveClasses();
315 class WXDLLIMPEXP_BASE wxArchiveClassFactory
: public wxFilterClassFactoryBase
318 typedef wxArchiveEntry entry_type
;
319 typedef wxArchiveInputStream instream_type
;
320 typedef wxArchiveOutputStream outstream_type
;
321 typedef wxArchiveNotifier notifier_type
;
322 #if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
323 typedef wxArchiveIter iter_type
;
324 typedef wxArchivePairIter pairiter_type
;
327 virtual ~wxArchiveClassFactory() { }
329 wxArchiveEntry
*NewEntry() const
330 { return DoNewEntry(); }
331 wxArchiveInputStream
*NewStream(wxInputStream
& stream
) const
332 { return DoNewStream(stream
); }
333 wxArchiveOutputStream
*NewStream(wxOutputStream
& stream
) const
334 { return DoNewStream(stream
); }
335 wxArchiveInputStream
*NewStream(wxInputStream
*stream
) const
336 { return DoNewStream(stream
); }
337 wxArchiveOutputStream
*NewStream(wxOutputStream
*stream
) const
338 { return DoNewStream(stream
); }
340 virtual wxString
GetInternalName(
341 const wxString
& name
,
342 wxPathFormat format
= wxPATH_NATIVE
) const = 0;
344 // FIXME-UTF8: remove these from this file, they are used for ANSI
346 void SetConv(wxMBConv
& conv
) { m_pConv
= &conv
; }
347 wxMBConv
& GetConv() const
348 { if (m_pConv
) return *m_pConv
; else return wxConvLocal
; }
350 static const wxArchiveClassFactory
*Find(const wxString
& protocol
,
351 wxStreamProtocolType type
352 = wxSTREAM_PROTOCOL
);
354 static const wxArchiveClassFactory
*GetFirst();
355 const wxArchiveClassFactory
*GetNext() const { return m_next
; }
357 void PushFront() { Remove(); m_next
= sm_first
; sm_first
= this; }
361 // old compilers don't support covarient returns, so 'Do' methods are
362 // used to simulate them
363 virtual wxArchiveEntry
*DoNewEntry() const = 0;
364 virtual wxArchiveInputStream
*DoNewStream(wxInputStream
& stream
) const = 0;
365 virtual wxArchiveOutputStream
*DoNewStream(wxOutputStream
& stream
) const = 0;
366 virtual wxArchiveInputStream
*DoNewStream(wxInputStream
*stream
) const = 0;
367 virtual wxArchiveOutputStream
*DoNewStream(wxOutputStream
*stream
) const = 0;
369 wxArchiveClassFactory() : m_pConv(NULL
), m_next(this) { }
370 wxArchiveClassFactory
& operator=(const wxArchiveClassFactory
& WXUNUSED(f
))
375 static wxArchiveClassFactory
*sm_first
;
376 wxArchiveClassFactory
*m_next
;
378 DECLARE_ABSTRACT_CLASS(wxArchiveClassFactory
)
381 #endif // wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS
383 #endif // _WX_ARCHIVE_H__