]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/archive.h | |
3 | // Purpose: Streams for archive formats | |
4 | // Author: Mike Wetherell | |
5 | // Copyright: (c) 2004 Mike Wetherell | |
6 | // Licence: wxWindows licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | #ifndef _WX_ARCHIVE_H__ | |
10 | #define _WX_ARCHIVE_H__ | |
11 | ||
12 | #include "wx/defs.h" | |
13 | ||
14 | #if wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS | |
15 | ||
16 | #include "wx/stream.h" | |
17 | #include "wx/filename.h" | |
18 | ||
19 | ||
20 | ///////////////////////////////////////////////////////////////////////////// | |
21 | // wxArchiveNotifier | |
22 | ||
23 | class WXDLLIMPEXP_BASE wxArchiveNotifier | |
24 | { | |
25 | public: | |
26 | virtual ~wxArchiveNotifier() { } | |
27 | ||
28 | virtual void OnEntryUpdated(class wxArchiveEntry& entry) = 0; | |
29 | }; | |
30 | ||
31 | ||
32 | ///////////////////////////////////////////////////////////////////////////// | |
33 | // wxArchiveEntry | |
34 | // | |
35 | // Holds an entry's meta data, such as filename and timestamp. | |
36 | ||
37 | class WXDLLIMPEXP_BASE wxArchiveEntry : public wxObject | |
38 | { | |
39 | public: | |
40 | virtual ~wxArchiveEntry() { } | |
41 | ||
42 | virtual wxDateTime GetDateTime() const = 0; | |
43 | virtual wxFileOffset GetSize() const = 0; | |
44 | virtual wxFileOffset GetOffset() const = 0; | |
45 | virtual bool IsDir() const = 0; | |
46 | virtual bool IsReadOnly() const = 0; | |
47 | virtual wxString GetInternalName() const = 0; | |
48 | virtual wxPathFormat GetInternalFormat() const = 0; | |
49 | virtual wxString GetName(wxPathFormat format = wxPATH_NATIVE) const = 0; | |
50 | ||
51 | virtual void SetDateTime(const wxDateTime& dt) = 0; | |
52 | virtual void SetSize(wxFileOffset size) = 0; | |
53 | virtual void SetIsDir(bool isDir = true) = 0; | |
54 | virtual void SetIsReadOnly(bool isReadOnly = true) = 0; | |
55 | virtual void SetName(const wxString& name, | |
56 | wxPathFormat format = wxPATH_NATIVE) = 0; | |
57 | ||
58 | wxArchiveEntry *Clone() const { return DoClone(); } | |
59 | ||
60 | void SetNotifier(wxArchiveNotifier& notifier); | |
61 | virtual void UnsetNotifier() { m_notifier = NULL; } | |
62 | ||
63 | protected: | |
64 | wxArchiveEntry() : m_notifier(NULL) { } | |
65 | wxArchiveEntry(const wxArchiveEntry& e) : wxObject(e), m_notifier(NULL) { } | |
66 | ||
67 | virtual void SetOffset(wxFileOffset offset) = 0; | |
68 | virtual wxArchiveEntry* DoClone() const = 0; | |
69 | ||
70 | wxArchiveNotifier *GetNotifier() const { return m_notifier; } | |
71 | wxArchiveEntry& operator=(const wxArchiveEntry& entry); | |
72 | ||
73 | private: | |
74 | wxArchiveNotifier *m_notifier; | |
75 | ||
76 | DECLARE_ABSTRACT_CLASS(wxArchiveEntry) | |
77 | }; | |
78 | ||
79 | ||
80 | ///////////////////////////////////////////////////////////////////////////// | |
81 | // wxArchiveInputStream | |
82 | // | |
83 | // GetNextEntry() returns an wxArchiveEntry object containing the meta-data | |
84 | // for the next entry in the archive (and gives away ownership). Reading from | |
85 | // the wxArchiveInputStream then returns the entry's data. Eof() becomes true | |
86 | // after an attempt has been made to read past the end of the entry's data. | |
87 | // | |
88 | // When there are no more entries, GetNextEntry() returns NULL and sets Eof(). | |
89 | ||
90 | class WXDLLIMPEXP_BASE wxArchiveInputStream : public wxFilterInputStream | |
91 | { | |
92 | public: | |
93 | typedef wxArchiveEntry entry_type; | |
94 | ||
95 | virtual ~wxArchiveInputStream() { } | |
96 | ||
97 | virtual bool OpenEntry(wxArchiveEntry& entry) = 0; | |
98 | virtual bool CloseEntry() = 0; | |
99 | ||
100 | wxArchiveEntry *GetNextEntry() { return DoGetNextEntry(); } | |
101 | ||
102 | virtual char Peek() { return wxInputStream::Peek(); } | |
103 | ||
104 | protected: | |
105 | wxArchiveInputStream(wxInputStream& stream, wxMBConv& conv); | |
106 | wxArchiveInputStream(wxInputStream *stream, wxMBConv& conv); | |
107 | ||
108 | virtual wxArchiveEntry *DoGetNextEntry() = 0; | |
109 | ||
110 | wxMBConv& GetConv() const { return m_conv; } | |
111 | ||
112 | private: | |
113 | wxMBConv& m_conv; | |
114 | }; | |
115 | ||
116 | ||
117 | ///////////////////////////////////////////////////////////////////////////// | |
118 | // wxArchiveOutputStream | |
119 | // | |
120 | // PutNextEntry is used to create a new entry in the output archive, then | |
121 | // the entry's data is written to the wxArchiveOutputStream. | |
122 | // | |
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. | |
125 | // | |
126 | // The overload 'bool PutNextEntry(wxArchiveEntry *entry)' takes ownership | |
127 | // of the entry object. | |
128 | ||
129 | class WXDLLIMPEXP_BASE wxArchiveOutputStream : public wxFilterOutputStream | |
130 | { | |
131 | public: | |
132 | virtual ~wxArchiveOutputStream() { } | |
133 | ||
134 | virtual bool PutNextEntry(wxArchiveEntry *entry) = 0; | |
135 | ||
136 | virtual bool PutNextEntry(const wxString& name, | |
137 | const wxDateTime& dt = wxDateTime::Now(), | |
138 | wxFileOffset size = wxInvalidOffset) = 0; | |
139 | ||
140 | virtual bool PutNextDirEntry(const wxString& name, | |
141 | const wxDateTime& dt = wxDateTime::Now()) = 0; | |
142 | ||
143 | virtual bool CopyEntry(wxArchiveEntry *entry, | |
144 | wxArchiveInputStream& stream) = 0; | |
145 | ||
146 | virtual bool CopyArchiveMetaData(wxArchiveInputStream& stream) = 0; | |
147 | ||
148 | virtual bool CloseEntry() = 0; | |
149 | ||
150 | protected: | |
151 | wxArchiveOutputStream(wxOutputStream& stream, wxMBConv& conv); | |
152 | wxArchiveOutputStream(wxOutputStream *stream, wxMBConv& conv); | |
153 | ||
154 | wxMBConv& GetConv() const { return m_conv; } | |
155 | ||
156 | private: | |
157 | wxMBConv& m_conv; | |
158 | }; | |
159 | ||
160 | ||
161 | ///////////////////////////////////////////////////////////////////////////// | |
162 | // wxArchiveIterator | |
163 | // | |
164 | // An input iterator that can be used to transfer an archive's catalog to | |
165 | // a container. | |
166 | ||
167 | #if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR | |
168 | #include <iterator> | |
169 | #include <utility> | |
170 | ||
171 | template <class X, class Y> inline | |
172 | void _wxSetArchiveIteratorValue( | |
173 | X& val, Y entry, void *WXUNUSED(d)) | |
174 | { | |
175 | val = X(entry); | |
176 | } | |
177 | template <class X, class Y, class Z> inline | |
178 | void _wxSetArchiveIteratorValue( | |
179 | std::pair<X, Y>& val, Z entry, Z WXUNUSED(d)) | |
180 | { | |
181 | val = std::make_pair(X(entry->GetInternalName()), Y(entry)); | |
182 | } | |
183 | ||
184 | #if defined _MSC_VER && _MSC_VER < 1300 | |
185 | template <class Arc, class T = Arc::entry_type*> | |
186 | #else | |
187 | template <class Arc, class T = typename Arc::entry_type*> | |
188 | #endif | |
189 | class wxArchiveIterator | |
190 | { | |
191 | public: | |
192 | typedef std::input_iterator_tag iterator_category; | |
193 | typedef T value_type; | |
194 | typedef ptrdiff_t difference_type; | |
195 | typedef T* pointer; | |
196 | typedef T& reference; | |
197 | ||
198 | wxArchiveIterator() : m_rep(NULL) { } | |
199 | ||
200 | wxArchiveIterator(Arc& arc) { | |
201 | typename Arc::entry_type* entry = arc.GetNextEntry(); | |
202 | m_rep = entry ? new Rep(arc, entry) : NULL; | |
203 | } | |
204 | ||
205 | wxArchiveIterator(const wxArchiveIterator& it) : m_rep(it.m_rep) { | |
206 | if (m_rep) | |
207 | m_rep->AddRef(); | |
208 | } | |
209 | ||
210 | ~wxArchiveIterator() { | |
211 | if (m_rep) | |
212 | m_rep->UnRef(); | |
213 | } | |
214 | ||
215 | const T& operator *() const { | |
216 | return m_rep->GetValue(); | |
217 | } | |
218 | ||
219 | const T* operator ->() const { | |
220 | return &**this; | |
221 | } | |
222 | ||
223 | wxArchiveIterator& operator =(const wxArchiveIterator& it) { | |
224 | if (it.m_rep) | |
225 | it.m_rep.AddRef(); | |
226 | if (m_rep) | |
227 | this->m_rep.UnRef(); | |
228 | m_rep = it.m_rep; | |
229 | return *this; | |
230 | } | |
231 | ||
232 | wxArchiveIterator& operator ++() { | |
233 | m_rep = m_rep->Next(); | |
234 | return *this; | |
235 | } | |
236 | ||
237 | wxArchiveIterator operator ++(int) { | |
238 | wxArchiveIterator it(*this); | |
239 | ++(*this); | |
240 | return it; | |
241 | } | |
242 | ||
243 | bool operator ==(const wxArchiveIterator& j) const { | |
244 | return m_rep == j.m_rep; | |
245 | } | |
246 | ||
247 | bool operator !=(const wxArchiveIterator& j) const { | |
248 | return !(*this == j); | |
249 | } | |
250 | ||
251 | private: | |
252 | class Rep { | |
253 | Arc& m_arc; | |
254 | typename Arc::entry_type* m_entry; | |
255 | T m_value; | |
256 | int m_ref; | |
257 | ||
258 | public: | |
259 | Rep(Arc& arc, typename Arc::entry_type* entry) | |
260 | : m_arc(arc), m_entry(entry), m_value(), m_ref(1) { } | |
261 | ~Rep() | |
262 | { delete m_entry; } | |
263 | ||
264 | void AddRef() { | |
265 | m_ref++; | |
266 | } | |
267 | ||
268 | void UnRef() { | |
269 | if (--m_ref == 0) | |
270 | delete this; | |
271 | } | |
272 | ||
273 | Rep *Next() { | |
274 | typename Arc::entry_type* entry = m_arc.GetNextEntry(); | |
275 | if (!entry) { | |
276 | UnRef(); | |
277 | return NULL; | |
278 | } | |
279 | if (m_ref > 1) { | |
280 | m_ref--; | |
281 | return new Rep(m_arc, entry); | |
282 | } | |
283 | delete m_entry; | |
284 | m_entry = entry; | |
285 | m_value = T(); | |
286 | return this; | |
287 | } | |
288 | ||
289 | const T& GetValue() { | |
290 | if (m_entry) { | |
291 | _wxSetArchiveIteratorValue(m_value, m_entry, m_entry); | |
292 | m_entry = NULL; | |
293 | } | |
294 | return m_value; | |
295 | } | |
296 | } *m_rep; | |
297 | }; | |
298 | ||
299 | typedef wxArchiveIterator<wxArchiveInputStream> wxArchiveIter; | |
300 | typedef wxArchiveIterator<wxArchiveInputStream, | |
301 | std::pair<wxString, wxArchiveEntry*> > wxArchivePairIter; | |
302 | ||
303 | #endif // wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR | |
304 | ||
305 | ||
306 | ///////////////////////////////////////////////////////////////////////////// | |
307 | // wxArchiveClassFactory | |
308 | // | |
309 | // A wxArchiveClassFactory instance for a particular archive type allows | |
310 | // the creation of the other classes that may be needed. | |
311 | ||
312 | void WXDLLIMPEXP_BASE wxUseArchiveClasses(); | |
313 | ||
314 | class WXDLLIMPEXP_BASE wxArchiveClassFactory : public wxFilterClassFactoryBase | |
315 | { | |
316 | public: | |
317 | typedef wxArchiveEntry entry_type; | |
318 | typedef wxArchiveInputStream instream_type; | |
319 | typedef wxArchiveOutputStream outstream_type; | |
320 | typedef wxArchiveNotifier notifier_type; | |
321 | #if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR | |
322 | typedef wxArchiveIter iter_type; | |
323 | typedef wxArchivePairIter pairiter_type; | |
324 | #endif | |
325 | ||
326 | virtual ~wxArchiveClassFactory() { } | |
327 | ||
328 | wxArchiveEntry *NewEntry() const | |
329 | { return DoNewEntry(); } | |
330 | wxArchiveInputStream *NewStream(wxInputStream& stream) const | |
331 | { return DoNewStream(stream); } | |
332 | wxArchiveOutputStream *NewStream(wxOutputStream& stream) const | |
333 | { return DoNewStream(stream); } | |
334 | wxArchiveInputStream *NewStream(wxInputStream *stream) const | |
335 | { return DoNewStream(stream); } | |
336 | wxArchiveOutputStream *NewStream(wxOutputStream *stream) const | |
337 | { return DoNewStream(stream); } | |
338 | ||
339 | virtual wxString GetInternalName( | |
340 | const wxString& name, | |
341 | wxPathFormat format = wxPATH_NATIVE) const = 0; | |
342 | ||
343 | // FIXME-UTF8: remove these from this file, they are used for ANSI | |
344 | // build only | |
345 | void SetConv(wxMBConv& conv) { m_pConv = &conv; } | |
346 | wxMBConv& GetConv() const | |
347 | { if (m_pConv) return *m_pConv; else return wxConvLocal; } | |
348 | ||
349 | static const wxArchiveClassFactory *Find(const wxString& protocol, | |
350 | wxStreamProtocolType type | |
351 | = wxSTREAM_PROTOCOL); | |
352 | ||
353 | static const wxArchiveClassFactory *GetFirst(); | |
354 | const wxArchiveClassFactory *GetNext() const { return m_next; } | |
355 | ||
356 | void PushFront() { Remove(); m_next = sm_first; sm_first = this; } | |
357 | void Remove(); | |
358 | ||
359 | protected: | |
360 | // old compilers don't support covarient returns, so 'Do' methods are | |
361 | // used to simulate them | |
362 | virtual wxArchiveEntry *DoNewEntry() const = 0; | |
363 | virtual wxArchiveInputStream *DoNewStream(wxInputStream& stream) const = 0; | |
364 | virtual wxArchiveOutputStream *DoNewStream(wxOutputStream& stream) const = 0; | |
365 | virtual wxArchiveInputStream *DoNewStream(wxInputStream *stream) const = 0; | |
366 | virtual wxArchiveOutputStream *DoNewStream(wxOutputStream *stream) const = 0; | |
367 | ||
368 | wxArchiveClassFactory() : m_pConv(NULL), m_next(this) { } | |
369 | wxArchiveClassFactory& operator=(const wxArchiveClassFactory& WXUNUSED(f)) | |
370 | { return *this; } | |
371 | ||
372 | private: | |
373 | wxMBConv *m_pConv; | |
374 | static wxArchiveClassFactory *sm_first; | |
375 | wxArchiveClassFactory *m_next; | |
376 | ||
377 | DECLARE_ABSTRACT_CLASS(wxArchiveClassFactory) | |
378 | }; | |
379 | ||
380 | #endif // wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS | |
381 | ||
382 | #endif // _WX_ARCHIVE_H__ |