added ZIP classes by M.J.Wetherell (patch 1030239)
[wxWidgets.git] / include / wx / archive.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: archive.h
3 // Purpose: Streams for archive formats
4 // Author: Mike Wetherell
5 // RCS-ID: $Id$
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_ARCHIVE_H__
11 #define _WX_ARCHIVE_H__
12
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma interface "archive.h"
15 #endif
16
17 #include "wx/defs.h"
18
19 #if wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM
20
21 #include "wx/stream.h"
22 #include "wx/filename.h"
23
24
25 /////////////////////////////////////////////////////////////////////////////
26 // wxArchiveNotifier
27
28 class WXDLLIMPEXP_BASE wxArchiveNotifier
29 {
30 public:
31 virtual ~wxArchiveNotifier() { }
32
33 virtual void OnEntryUpdated(class wxArchiveEntry& entry) = 0;
34 };
35
36
37 /////////////////////////////////////////////////////////////////////////////
38 // wxArchiveEntry
39 //
40 // Holds an entry's meta data, such as filename and timestamp.
41
42 class WXDLLIMPEXP_BASE wxArchiveEntry : public wxObject
43 {
44 public:
45 virtual ~wxArchiveEntry() { }
46
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;
55
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;
62
63 wxArchiveEntry *Clone() const { return DoClone(); }
64
65 void SetNotifier(wxArchiveNotifier& notifier);
66 virtual void UnsetNotifier() { m_notifier = NULL; }
67
68 protected:
69 wxArchiveEntry() : m_notifier(NULL) { }
70
71 virtual void SetOffset(wxFileOffset offset) = 0;
72 virtual wxArchiveEntry* DoClone() const = 0;
73
74 wxArchiveNotifier *GetNotifier() const { return m_notifier; }
75 wxArchiveEntry& operator=(const wxArchiveEntry& entry);
76
77 private:
78 wxArchiveNotifier *m_notifier;
79
80 DECLARE_ABSTRACT_CLASS(wxArchiveEntry)
81 };
82
83
84 /////////////////////////////////////////////////////////////////////////////
85 // wxArchiveInputStream
86 //
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.
91 //
92 // When there are no more entries, GetNextEntry() returns NULL and sets Eof().
93
94 class WXDLLIMPEXP_BASE wxArchiveInputStream : public wxFilterInputStream
95 {
96 public:
97 typedef wxArchiveEntry entry_type;
98
99 virtual ~wxArchiveInputStream() { }
100
101 virtual bool OpenEntry(wxArchiveEntry& entry) = 0;
102 virtual bool CloseEntry() = 0;
103
104 wxArchiveEntry *GetNextEntry() { return DoGetNextEntry(); }
105
106 virtual char Peek() { return wxInputStream::Peek(); }
107
108 protected:
109 wxArchiveInputStream(wxInputStream& stream, wxMBConv& conv);
110
111 virtual wxArchiveEntry *DoGetNextEntry() = 0;
112
113 wxMBConv& GetConv() const { return m_conv; }
114
115 private:
116 wxMBConv& m_conv;
117 };
118
119
120 /////////////////////////////////////////////////////////////////////////////
121 // wxArchiveOutputStream
122 //
123 // PutNextEntry is used to create a new entry in the output archive, then
124 // the entry's data is written to the wxArchiveOutputStream.
125 //
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.
128 //
129 // The overload 'bool PutNextEntry(wxArchiveEntry *entry)' takes ownership
130 // of the entry object.
131
132 class WXDLLIMPEXP_BASE wxArchiveOutputStream : public wxFilterOutputStream
133 {
134 public:
135 virtual ~wxArchiveOutputStream() { }
136
137 virtual bool PutNextEntry(wxArchiveEntry *entry) = 0;
138
139 virtual bool PutNextEntry(const wxString& name,
140 const wxDateTime& dt = wxDateTime::Now(),
141 wxFileOffset size = wxInvalidOffset) = 0;
142
143 virtual bool PutNextDirEntry(const wxString& name,
144 const wxDateTime& dt = wxDateTime::Now()) = 0;
145
146 virtual bool CopyEntry(wxArchiveEntry *entry,
147 wxArchiveInputStream& stream) = 0;
148
149 virtual bool CopyArchiveMetaData(wxArchiveInputStream& stream) = 0;
150
151 virtual bool CloseEntry() = 0;
152 virtual bool Close() = 0;
153
154 protected:
155 wxArchiveOutputStream(wxOutputStream& stream, wxMBConv& conv);
156
157 wxMBConv& GetConv() const { return m_conv; }
158
159 private:
160 wxMBConv& m_conv;
161 };
162
163
164 /////////////////////////////////////////////////////////////////////////////
165 // wxArchiveClassFactory
166 //
167 // A wxArchiveClassFactory instance for a particular archive type allows
168 // the creation of the other classes that may be needed.
169
170 class WXDLLIMPEXP_BASE wxArchiveClassFactory : public wxObject
171 {
172 public:
173 virtual ~wxArchiveClassFactory() { }
174
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); }
181
182 virtual wxString GetInternalName(
183 const wxString& name,
184 wxPathFormat format = wxPATH_NATIVE) const = 0;
185
186 void SetConv(wxMBConv& conv) { m_pConv = &conv; }
187 wxMBConv& GetConv() const { return *m_pConv; }
188
189 protected:
190 virtual wxArchiveEntry *DoNewEntry() const = 0;
191 virtual wxArchiveInputStream *DoNewStream(wxInputStream& stream) const = 0;
192 virtual wxArchiveOutputStream *DoNewStream(wxOutputStream& stream) const = 0;
193
194 wxArchiveClassFactory() : m_pConv(&wxConvLocal) { }
195 wxArchiveClassFactory& operator=(const wxArchiveClassFactory& WXUNUSED(f))
196 { return *this; }
197
198 private:
199 wxMBConv *m_pConv;
200
201 DECLARE_ABSTRACT_CLASS(wxArchiveClassFactory)
202 };
203
204
205 /////////////////////////////////////////////////////////////////////////////
206 // wxArchiveIterator
207 //
208 // An input iterator that can be used to transfer an archive's catalog to
209 // a container.
210
211 #if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
212 #include <iterator>
213 #include <utility>
214
215 template <class X, class Y>
216 void WXDLLIMPEXP_BASE _wxSetArchiveIteratorValue(
217 X& val, Y entry, void *WXUNUSED(d))
218 {
219 val = X(entry);
220 }
221 template <class X, class Y, class Z>
222 void WXDLLIMPEXP_BASE _wxSetArchiveIteratorValue(
223 std::pair<X, Y>& val, Z entry, Z WXUNUSED(d))
224 {
225 val = std::make_pair(X(entry->GetInternalName()), Y(entry));
226 }
227
228 #if defined _MSC_VER && _MSC_VER < 1300
229 template <class Arc, class T = Arc::entry_type*>
230 #else
231 template <class Arc, class T = typename Arc::entry_type*>
232 #endif
233 class WXDLLIMPEXP_BASE wxArchiveIterator
234 {
235 public:
236 typedef std::input_iterator_tag iterator_category;
237 typedef T value_type;
238 typedef ptrdiff_t difference_type;
239 typedef T* pointer;
240 typedef T& reference;
241
242 wxArchiveIterator() : m_rep(NULL) { }
243
244 wxArchiveIterator(Arc& arc) {
245 typename Arc::entry_type* entry = arc.GetNextEntry();
246 m_rep = entry ? new Rep(arc, entry) : NULL;
247 }
248
249 wxArchiveIterator(const wxArchiveIterator& it) : m_rep(it.m_rep) {
250 if (m_rep)
251 m_rep->AddRef();
252 }
253
254 ~wxArchiveIterator() {
255 if (m_rep)
256 m_rep->UnRef();
257 }
258
259 const T& operator *() const {
260 return m_rep->GetValue();
261 }
262
263 const T* operator ->() const {
264 return &**this;
265 }
266
267 wxArchiveIterator& operator =(const wxArchiveIterator& it) {
268 if (it.m_rep)
269 it.m_rep.AddRef();
270 if (m_rep)
271 m_rep.UnRef();
272 m_rep = it.m_rep;
273 return *this;
274 }
275
276 wxArchiveIterator& operator ++() {
277 m_rep = m_rep->Next();
278 return *this;
279 }
280
281 wxArchiveIterator operator ++(int) {
282 wxArchiveIterator it(*this);
283 ++(*this);
284 return it;
285 }
286
287 friend bool operator ==(const wxArchiveIterator& i,
288 const wxArchiveIterator& j) {
289 return i.m_rep == j.m_rep;
290 }
291
292 friend bool operator !=(const wxArchiveIterator& i,
293 const wxArchiveIterator& j) {
294 return !(i == j);
295 }
296
297 private:
298 class Rep {
299 Arc& m_arc;
300 typename Arc::entry_type* m_entry;
301 T m_value;
302 int m_ref;
303
304 public:
305 Rep(Arc& arc, typename Arc::entry_type* entry)
306 : m_arc(arc), m_entry(entry), m_value(), m_ref(1) { }
307 ~Rep()
308 { delete m_entry; }
309
310 void AddRef() {
311 m_ref++;
312 }
313
314 void UnRef() {
315 if (--m_ref == 0)
316 delete this;
317 }
318
319 Rep *Next() {
320 typename Arc::entry_type* entry = m_arc.GetNextEntry();
321 if (!entry) {
322 UnRef();
323 return NULL;
324 }
325 if (m_ref > 1) {
326 m_ref--;
327 return new Rep(m_arc, entry);
328 }
329 delete m_entry;
330 m_entry = entry;
331 m_value = T();
332 return this;
333 }
334
335 const T& GetValue() {
336 if (m_entry) {
337 _wxSetArchiveIteratorValue(m_value, m_entry, m_entry);
338 m_entry = NULL;
339 }
340 return m_value;
341 }
342 } *m_rep;
343 };
344
345 typedef wxArchiveIterator<wxArchiveInputStream> wxArchiveIter;
346 typedef wxArchiveIterator<wxArchiveInputStream,
347 std::pair<wxString, wxArchiveEntry*> > wxArchivePairIter;
348
349 #endif // wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
350
351 #endif // wxUSE_STREAMS
352
353 #endif // _WX_ARCHIVE_H__