]>
Commit | Line | Data |
---|---|---|
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_STREAMS && wxUSE_ARCHIVE_STREAMS | |
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 | wxArchiveEntry(const wxArchiveEntry& e) : wxObject(e), m_notifier(NULL) { } | |
71 | ||
72 | virtual void SetOffset(wxFileOffset offset) = 0; | |
73 | virtual wxArchiveEntry* DoClone() const = 0; | |
74 | ||
75 | wxArchiveNotifier *GetNotifier() const { return m_notifier; } | |
76 | wxArchiveEntry& operator=(const wxArchiveEntry& entry); | |
77 | ||
78 | private: | |
79 | wxArchiveNotifier *m_notifier; | |
80 | ||
81 | DECLARE_ABSTRACT_CLASS(wxArchiveEntry) | |
82 | }; | |
83 | ||
84 | ||
85 | ///////////////////////////////////////////////////////////////////////////// | |
86 | // wxArchiveInputStream | |
87 | // | |
88 | // GetNextEntry() returns an wxArchiveEntry object containing the meta-data | |
89 | // for the next entry in the archive (and gives away ownership). Reading from | |
90 | // the wxArchiveInputStream then returns the entry's data. Eof() becomes true | |
91 | // after an attempt has been made to read past the end of the entry's data. | |
92 | // | |
93 | // When there are no more entries, GetNextEntry() returns NULL and sets Eof(). | |
94 | ||
95 | class WXDLLIMPEXP_BASE wxArchiveInputStream : public wxFilterInputStream | |
96 | { | |
97 | public: | |
98 | typedef wxArchiveEntry entry_type; | |
99 | ||
100 | virtual ~wxArchiveInputStream() { } | |
101 | ||
102 | virtual bool OpenEntry(wxArchiveEntry& entry) = 0; | |
103 | virtual bool CloseEntry() = 0; | |
104 | ||
105 | wxArchiveEntry *GetNextEntry() { return DoGetNextEntry(); } | |
106 | ||
107 | virtual char Peek() { return wxInputStream::Peek(); } | |
108 | ||
109 | protected: | |
110 | wxArchiveInputStream(wxInputStream& stream, wxMBConv& conv); | |
111 | ||
112 | virtual wxArchiveEntry *DoGetNextEntry() = 0; | |
113 | ||
114 | wxMBConv& GetConv() const { return m_conv; } | |
115 | ||
116 | private: | |
117 | wxMBConv& m_conv; | |
118 | }; | |
119 | ||
120 | ||
121 | ///////////////////////////////////////////////////////////////////////////// | |
122 | // wxArchiveOutputStream | |
123 | // | |
124 | // PutNextEntry is used to create a new entry in the output archive, then | |
125 | // the entry's data is written to the wxArchiveOutputStream. | |
126 | // | |
127 | // Only one entry can be open for output at a time; another call to | |
128 | // PutNextEntry closes the current entry and begins the next. | |
129 | // | |
130 | // The overload 'bool PutNextEntry(wxArchiveEntry *entry)' takes ownership | |
131 | // of the entry object. | |
132 | ||
133 | class WXDLLIMPEXP_BASE wxArchiveOutputStream : public wxFilterOutputStream | |
134 | { | |
135 | public: | |
136 | virtual ~wxArchiveOutputStream() { } | |
137 | ||
138 | virtual bool PutNextEntry(wxArchiveEntry *entry) = 0; | |
139 | ||
140 | virtual bool PutNextEntry(const wxString& name, | |
141 | const wxDateTime& dt = wxDateTime::Now(), | |
142 | wxFileOffset size = wxInvalidOffset) = 0; | |
143 | ||
144 | virtual bool PutNextDirEntry(const wxString& name, | |
145 | const wxDateTime& dt = wxDateTime::Now()) = 0; | |
146 | ||
147 | virtual bool CopyEntry(wxArchiveEntry *entry, | |
148 | wxArchiveInputStream& stream) = 0; | |
149 | ||
150 | virtual bool CopyArchiveMetaData(wxArchiveInputStream& stream) = 0; | |
151 | ||
152 | virtual bool CloseEntry() = 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 | // wxArchiveIterator | |
166 | // | |
167 | // An input iterator that can be used to transfer an archive's catalog to | |
168 | // a container. | |
169 | ||
170 | #if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR | |
171 | #include <iterator> | |
172 | #include <utility> | |
173 | ||
174 | template <class X, class Y> inline | |
175 | void _wxSetArchiveIteratorValue( | |
176 | X& val, Y entry, void *WXUNUSED(d)) | |
177 | { | |
178 | val = X(entry); | |
179 | } | |
180 | template <class X, class Y, class Z> inline | |
181 | void _wxSetArchiveIteratorValue( | |
182 | std::pair<X, Y>& val, Z entry, Z WXUNUSED(d)) | |
183 | { | |
184 | val = std::make_pair(X(entry->GetInternalName()), Y(entry)); | |
185 | } | |
186 | ||
187 | #if defined _MSC_VER && _MSC_VER < 1300 | |
188 | template <class Arc, class T = Arc::entry_type*> | |
189 | #else | |
190 | template <class Arc, class T = typename Arc::entry_type*> | |
191 | #endif | |
192 | class wxArchiveIterator | |
193 | { | |
194 | public: | |
195 | typedef std::input_iterator_tag iterator_category; | |
196 | typedef T value_type; | |
197 | typedef ptrdiff_t difference_type; | |
198 | typedef T* pointer; | |
199 | typedef T& reference; | |
200 | ||
201 | wxArchiveIterator() : m_rep(NULL) { } | |
202 | ||
203 | wxArchiveIterator(Arc& arc) { | |
204 | typename Arc::entry_type* entry = arc.GetNextEntry(); | |
205 | m_rep = entry ? new Rep(arc, entry) : NULL; | |
206 | } | |
207 | ||
208 | wxArchiveIterator(const wxArchiveIterator& it) : m_rep(it.m_rep) { | |
209 | if (m_rep) | |
210 | m_rep->AddRef(); | |
211 | } | |
212 | ||
213 | ~wxArchiveIterator() { | |
214 | if (m_rep) | |
215 | m_rep->UnRef(); | |
216 | } | |
217 | ||
218 | const T& operator *() const { | |
219 | return m_rep->GetValue(); | |
220 | } | |
221 | ||
222 | const T* operator ->() const { | |
223 | return &**this; | |
224 | } | |
225 | ||
226 | wxArchiveIterator& operator =(const wxArchiveIterator& it) { | |
227 | if (it.m_rep) | |
228 | it.m_rep.AddRef(); | |
229 | if (m_rep) | |
230 | m_rep.UnRef(); | |
231 | m_rep = it.m_rep; | |
232 | return *this; | |
233 | } | |
234 | ||
235 | wxArchiveIterator& operator ++() { | |
236 | m_rep = m_rep->Next(); | |
237 | return *this; | |
238 | } | |
239 | ||
240 | wxArchiveIterator operator ++(int) { | |
241 | wxArchiveIterator it(*this); | |
242 | ++(*this); | |
243 | return it; | |
244 | } | |
245 | ||
246 | bool operator ==(const wxArchiveIterator& j) const { | |
247 | return m_rep == j.m_rep; | |
248 | } | |
249 | ||
250 | bool operator !=(const wxArchiveIterator& j) const { | |
251 | return !(*this == j); | |
252 | } | |
253 | ||
254 | private: | |
255 | class Rep { | |
256 | Arc& m_arc; | |
257 | typename Arc::entry_type* m_entry; | |
258 | T m_value; | |
259 | int m_ref; | |
260 | ||
261 | public: | |
262 | Rep(Arc& arc, typename Arc::entry_type* entry) | |
263 | : m_arc(arc), m_entry(entry), m_value(), m_ref(1) { } | |
264 | ~Rep() | |
265 | { delete m_entry; } | |
266 | ||
267 | void AddRef() { | |
268 | m_ref++; | |
269 | } | |
270 | ||
271 | void UnRef() { | |
272 | if (--m_ref == 0) | |
273 | delete this; | |
274 | } | |
275 | ||
276 | Rep *Next() { | |
277 | typename Arc::entry_type* entry = m_arc.GetNextEntry(); | |
278 | if (!entry) { | |
279 | UnRef(); | |
280 | return NULL; | |
281 | } | |
282 | if (m_ref > 1) { | |
283 | m_ref--; | |
284 | return new Rep(m_arc, entry); | |
285 | } | |
286 | delete m_entry; | |
287 | m_entry = entry; | |
288 | m_value = T(); | |
289 | return this; | |
290 | } | |
291 | ||
292 | const T& GetValue() { | |
293 | if (m_entry) { | |
294 | _wxSetArchiveIteratorValue(m_value, m_entry, m_entry); | |
295 | m_entry = NULL; | |
296 | } | |
297 | return m_value; | |
298 | } | |
299 | } *m_rep; | |
300 | }; | |
301 | ||
302 | typedef wxArchiveIterator<wxArchiveInputStream> wxArchiveIter; | |
303 | typedef wxArchiveIterator<wxArchiveInputStream, | |
304 | std::pair<wxString, wxArchiveEntry*> > wxArchivePairIter; | |
305 | ||
306 | #endif // wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR | |
307 | ||
308 | ||
309 | ///////////////////////////////////////////////////////////////////////////// | |
310 | // wxArchiveClassFactory | |
311 | // | |
312 | // A wxArchiveClassFactory instance for a particular archive type allows | |
313 | // the creation of the other classes that may be needed. | |
314 | ||
315 | class WXDLLIMPEXP_BASE wxArchiveClassFactory : public wxObject | |
316 | { | |
317 | public: | |
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; | |
325 | #endif | |
326 | ||
327 | virtual ~wxArchiveClassFactory() { } | |
328 | ||
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 | ||
336 | virtual wxString GetInternalName( | |
337 | const wxString& name, | |
338 | wxPathFormat format = wxPATH_NATIVE) const = 0; | |
339 | ||
340 | void SetConv(wxMBConv& conv) { m_pConv = &conv; } | |
341 | wxMBConv& GetConv() const { return *m_pConv; } | |
342 | ||
343 | protected: | |
344 | // old compilers don't support covarient returns, so 'Do' methods are | |
345 | // used to simulate them | |
346 | virtual wxArchiveEntry *DoNewEntry() const = 0; | |
347 | virtual wxArchiveInputStream *DoNewStream(wxInputStream& stream) const = 0; | |
348 | virtual wxArchiveOutputStream *DoNewStream(wxOutputStream& stream) const = 0; | |
349 | ||
350 | wxArchiveClassFactory() : m_pConv(&wxConvLocal) { } | |
351 | wxArchiveClassFactory& operator=(const wxArchiveClassFactory& WXUNUSED(f)) | |
352 | { return *this; } | |
353 | ||
354 | private: | |
355 | wxMBConv *m_pConv; | |
356 | ||
357 | DECLARE_ABSTRACT_CLASS(wxArchiveClassFactory) | |
358 | }; | |
359 | ||
360 | #endif // wxUSE_STREAMS && wxUSE_ARCHIVE_STREAMS | |
361 | ||
362 | #endif // _WX_ARCHIVE_H__ |