]> git.saurik.com Git - wxWidgets.git/blob - interface/archive.h
18a6754df7c77e5a0cd61b3aea82b31bb2977131
[wxWidgets.git] / interface / archive.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: archive.h
3 // Purpose: documentation for wxArchiveInputStream class
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 @class wxArchiveInputStream
11 @wxheader{archive.h}
12
13 An abstract base class which serves as a common interface to
14 archive input streams such as wxZipInputStream.
15
16 wxArchiveInputStream::GetNextEntry returns an
17 wxArchiveEntry object containing the meta-data
18 for the next entry in the archive (and gives away ownership). Reading from
19 the wxArchiveInputStream then returns the entry's data. Eof() becomes @true
20 after an attempt has been made to read past the end of the entry's data.
21 When there are no more entries, GetNextEntry() returns @NULL and sets Eof().
22
23 @library{wxbase}
24 @category{FIXME}
25
26 @seealso
27 @ref overview_wxarc "Archive formats such as zip", wxArchiveEntry,
28 wxArchiveOutputStream
29 */
30 class wxArchiveInputStream : public wxFilterInputStream
31 {
32 public:
33 /**
34 Closes the current entry. On a non-seekable stream reads to the end of
35 the current entry first.
36 */
37 bool CloseEntry();
38
39 /**
40 Closes the current entry if one is open, then reads the meta-data for
41 the next entry and returns it in a wxArchiveEntry
42 object, giving away ownership. Reading this wxArchiveInputStream then
43 returns the entry's data.
44 */
45 wxArchiveEntry* GetNextEntry();
46
47 /**
48 Closes the current entry if one is open, then opens the entry specified
49 by the wxArchiveEntry object.
50
51 @e entry must be from the same archive file that this
52 wxArchiveInputStream is reading, and it must be reading it from a
53 seekable stream.
54 */
55 bool OpenEntry(wxArchiveEntry& entry);
56 };
57
58
59 /**
60 @class wxArchiveOutputStream
61 @wxheader{archive.h}
62
63 An abstract base class which serves as a common interface to
64 archive output streams such as wxZipOutputStream.
65
66 wxArchiveOutputStream::PutNextEntry is used
67 to create a new entry in the output archive, then the entry's data is
68 written to the wxArchiveOutputStream. Another call to PutNextEntry()
69 closes the current entry and begins the next.
70
71 @library{wxbase}
72 @category{FIXME}
73
74 @seealso
75 @ref overview_wxarc "Archive formats such as zip", wxArchiveEntry,
76 wxArchiveInputStream
77 */
78 class wxArchiveOutputStream : public wxFilterOutputStream
79 {
80 public:
81 /**
82 Calls Close() if it has not already
83 been called.
84 */
85 ~wxArchiveOutputStream();
86
87 /**
88 Closes the archive, returning @true if it was successfully written.
89 Called by the destructor if not called explicitly.
90 */
91 bool Close();
92
93 /**
94 Close the current entry. It is called implicitly whenever another new
95 entry is created with CopyEntry()
96 or PutNextEntry(), or
97 when the archive is closed.
98 */
99 bool CloseEntry();
100
101 /**
102 Some archive formats have additional meta-data that applies to the archive
103 as a whole. For example in the case of zip there is a comment, which
104 is stored at the end of the zip file. CopyArchiveMetaData() can be used
105 to transfer such information when writing a modified copy of an archive.
106
107 Since the position of the meta-data can vary between the various archive
108 formats, it is best to call CopyArchiveMetaData() before transferring
109 the entries. The wxArchiveOutputStream
110 will then hold on to the meta-data and write it at the correct point in
111 the output file.
112
113 When the input archive is being read from a non-seekable stream, the
114 meta-data may not be available when CopyArchiveMetaData() is called,
115 in which case the two streams set up a link and transfer the data
116 when it becomes available.
117 */
118 bool CopyArchiveMetaData(wxArchiveInputStream& stream);
119
120 /**
121 Takes ownership of @e entry and uses it to create a new entry in the
122 archive. @e entry is then opened in the input stream @e stream
123 and its contents copied to this stream.
124
125 For archive types which compress entry data, CopyEntry() is likely to be
126 much more efficient than transferring the data using Read() and Write()
127 since it will copy them without decompressing and recompressing them.
128
129 @e entry must be from the same archive file that @e stream is
130 accessing. For non-seekable streams, @e entry must also be the last
131 thing read from @e stream.
132 */
133 bool CopyEntry(wxArchiveEntry* entry,
134 wxArchiveInputStream& stream);
135
136 /**
137 )
138
139 Create a new directory entry
140 (see wxArchiveEntry::IsDir)
141 with the given name and timestamp.
142
143 PutNextEntry() can
144 also be used to create directory entries, by supplying a name with
145 a trailing path separator.
146 */
147 bool PutNextDirEntry(const wxString& name);
148
149 //@{
150 /**
151 , @b off_t@e size = wxInvalidOffset)
152
153 Create a new entry with the given name, timestamp and size. The entry's
154 data can then be written by writing to this wxArchiveOutputStream.
155 */
156 bool PutNextEntry(wxArchiveEntry* entry);
157 bool PutNextEntry(const wxString& name);
158 //@}
159 };
160
161
162 /**
163 @class wxArchiveEntry
164 @wxheader{archive.h}
165
166 An abstract base class which serves as a common interface to
167 archive entry classes such as wxZipEntry.
168 These hold the meta-data (filename, timestamp, etc.), for entries
169 in archive files such as zips and tars.
170
171 @library{wxbase}
172 @category{FIXME}
173
174 @seealso
175 @ref overview_wxarc "Archive formats such as zip", @ref overview_wxarcgeneric
176 "Generic archive programming", wxArchiveInputStream, wxArchiveOutputStream, wxArchiveNotifier
177 */
178 class wxArchiveEntry : public wxObject
179 {
180 public:
181 /**
182 Returns a copy of this entry object.
183 */
184 wxArchiveEntry* Clone();
185
186 //@{
187 /**
188 The entry's timestamp.
189 */
190 wxDateTime GetDateTime();
191 void SetDateTime(const wxDateTime& dt);
192 //@}
193
194 //@{
195 /**
196 The entry's name, by default in the native format. The name can include
197 directory components, i.e. it can be a full path.
198
199 If this is a directory entry, (i.e. if IsDir()
200 is @true) then GetName() returns the name with a trailing path separator.
201
202 Similarly, setting a name with a trailing path separator sets IsDir().
203 */
204 wxString GetName(wxPathFormat format = wxPATH_NATIVE);
205 void SetName(const wxString& name,
206 wxPathFormat format = wxPATH_NATIVE);
207 //@}
208
209 //@{
210 /**
211 The size of the entry's data in bytes.
212 */
213 off_t GetSize();
214 void SetSize(off_t size);
215 //@}
216
217 /**
218 Returns the path format used internally within the archive to store
219 filenames.
220 */
221 wxPathFormat GetInternalFormat();
222
223 /**
224 Returns the entry's filename in the internal format used within the
225 archive. The name can include directory components, i.e. it can be a
226 full path.
227
228 The names of directory entries are returned without any trailing path
229 separator. This gives a canonical name that can be used in comparisons.
230
231 @sa @ref overview_wxarcbyname "Looking up an archive entry by name"
232 */
233 wxString GetInternalName();
234
235 /**
236 Returns a numeric value unique to the entry within the archive.
237 */
238 off_t GetOffset();
239
240 //@{
241 /**
242 True if this is a directory entry.
243
244 Directory entries are entries with no data, which are used to store
245 the meta-data of directories. They also make it possible for completely
246 empty directories to be stored.
247
248 The names of entries within an archive can be complete paths, and
249 unarchivers typically create whatever directories are necessary as they
250 restore files, even if the archive contains no explicit directory entries.
251 */
252 bool IsDir();
253 void SetIsDir(bool isDir = @true);
254 //@}
255
256 //@{
257 /**
258 True if the entry is a read-only file.
259 */
260 bool IsReadOnly();
261 void SetIsReadOnly(bool isReadOnly = @true);
262 //@}
263
264 //@{
265 /**
266 Sets the notifier for this entry.
267 Whenever the wxArchiveInputStream updates
268 this entry, it will then invoke the associated
269 notifier's wxArchiveNotifier::OnEntryUpdated
270 method.
271
272 Setting a notifier is not usually necessary. It is used to handle
273 certain cases when modifying an archive in a pipeline (i.e. between
274 non-seekable streams).
275 */
276 void SetNotifier(wxArchiveNotifier& notifier);
277 void UnsetNotifier();
278 //@}
279 };
280
281
282 /**
283 @class wxArchiveClassFactory
284 @wxheader{archive.h}
285
286 Allows the creation of streams to handle archive formats such
287 as zip and tar.
288
289 For example, given a filename you can search for a factory that will
290 handle it and create a stream to read it:
291
292 @code
293 factory = wxArchiveClassFactory::Find(filename, wxSTREAM_FILEEXT);
294 if (factory)
295 stream = factory-NewStream(new wxFFileInputStream(filename));
296 @endcode
297
298 wxArchiveClassFactory::Find can also search
299 for a factory by MIME type or wxFileSystem protocol.
300 The available factories can be enumerated
301 using @ref wxArchiveClassFactory::getfirst "GetFirst() and GetNext".
302
303 @library{wxbase}
304 @category{FIXME}
305
306 @seealso
307 @ref overview_wxarc "Archive formats such as zip", @ref overview_wxarcgeneric
308 "Generic archive programming", wxArchiveEntry, wxArchiveInputStream, wxArchiveOutputStream, wxFilterClassFactory
309 */
310 class wxArchiveClassFactory : public wxObject
311 {
312 public:
313 /**
314 Returns @true if this factory can handle the given protocol, MIME type
315 or file extension.
316
317 When using wxSTREAM_FILEEXT for the second parameter, the first parameter
318 can be a complete filename rather than just an extension.
319 */
320 bool CanHandle(const wxChar* protocol,
321 wxStreamProtocolType type = wxSTREAM_PROTOCOL);
322
323 /**
324 A static member that finds a factory that can handle a given protocol, MIME
325 type or file extension. Returns a pointer to the class factory if found, or
326 @NULL otherwise. It does not give away ownership of the factory.
327
328 When using wxSTREAM_FILEEXT for the second parameter, the first parameter
329 can be a complete filename rather than just an extension.
330 */
331 static const wxArchiveClassFactory* Find(const wxChar* protocol,
332 wxStreamProtocolType type = wxSTREAM_PROTOCOL);
333
334 //@{
335 /**
336 The wxMBConv object that the created streams
337 will use when translating meta-data. The initial default, set by the
338 constructor, is wxConvLocal.
339 */
340 wxMBConv GetConv();
341 void SetConv(wxMBConv& conv);
342 //@}
343
344 //@{
345 /**
346 GetFirst and GetNext can be used to enumerate the available factories.
347
348 For example, to list them:
349 GetFirst()/GetNext() return a pointer to a factory or @NULL if no more
350 are available. They do not give away ownership of the factory.
351 */
352 static const wxArchiveClassFactory* GetFirst();
353 const wxArchiveClassFactory* GetNext();
354 //@}
355
356 /**
357 Calls the static GetInternalName() function for the archive entry type,
358 for example
359 wxZipEntry::GetInternalName.
360 */
361 wxString GetInternalName(const wxString& name,
362 wxPathFormat format = wxPATH_NATIVE);
363
364 /**
365 Returns the wxFileSystem protocol supported by this factory. Equivalent
366 to wxString(*GetProtcols()).
367 */
368 wxString GetProtocol();
369
370 /**
371 Returns the protocols, MIME types or file extensions supported by this
372 factory, as an array of null terminated strings. It does not give away
373 ownership of the array or strings.
374
375 For example, to list the file extensions a factory supports:
376 */
377 const wxChar * const* GetProtocols(wxStreamProtocolType type = wxSTREAM_PROTOCOL);
378
379 /**
380 Create a new wxArchiveEntry object of the
381 appropriate type.
382 */
383 wxArchiveEntry* NewEntry();
384
385 //@{
386 /**
387 Create a new input or output stream to read or write an archive.
388
389 If the parent stream is passed as a pointer then the new archive stream
390 takes ownership of it. If it is passed by reference then it does not.
391 */
392 wxArchiveInputStream* NewStream(wxInputStream& stream);
393 wxArchiveOutputStream* NewStream(wxOutputStream& stream);
394 wxArchiveInputStream* NewStream(wxInputStream* stream);
395 wxArchiveOutputStream* NewStream(wxOutputStream* stream);
396 //@}
397
398 /**
399 Adds this class factory to the list returned
400 by @ref getfirst() GetFirst()/GetNext.
401
402 It is not necessary to do this to use the archive streams. It is usually
403 used when implementing streams, typically the implementation will
404 add a static instance of its factory class.
405
406 It can also be used to change the order of a factory already in the list,
407 bringing it to the front. This isn't a thread safe operation
408 so can't be done when other threads are running that will be using the list.
409
410 The list does not take ownership of the factory.
411 */
412 void PushFront();
413
414 /**
415 Removes this class factory from the list returned
416 by @ref getfirst() GetFirst()/GetNext.
417
418 Removing from the list isn't a thread safe operation
419 so can't be done when other threads are running that will be using the list.
420
421 The list does not own the factories, so removing a factory does not delete it.
422 */
423 void Remove();
424 };
425
426
427 /**
428 @class wxArchiveNotifier
429 @wxheader{archive.h}
430
431 If you need to know when a
432 wxArchiveInputStream updates a
433 wxArchiveEntry object, you can create
434 a notifier by deriving from this abstract base class, overriding
435 wxArchiveNotifier::OnEntryUpdated. An instance
436 of your notifier class can then be assigned to the wxArchiveEntry object
437 using wxArchiveEntry::SetNotifier.
438 Your OnEntryUpdated() method will then be invoked whenever the input
439 stream updates the entry.
440
441 Setting a notifier is not usually necessary. It is used to handle
442 certain cases when modifying an archive in a pipeline (i.e. between
443 non-seekable streams).
444 See @ref overview_wxarcnoseek "Archives on non-seekable streams".
445
446 @library{wxbase}
447 @category{FIXME}
448
449 @seealso
450 @ref overview_wxarcnoseek "Archives on non-seekable streams", wxArchiveEntry,
451 wxArchiveInputStream, wxArchiveOutputStream
452 */
453 class wxArchiveNotifier
454 {
455 public:
456 /**
457 This method must be overridden in your derived class.
458 */
459 void OnEntryUpdated(class wxArchiveEntry& entry);
460 };
461
462
463 /**
464 @class wxArchiveIterator
465 @wxheader{archive.h}
466
467 An input iterator template class that can be used to transfer an archive's
468 catalogue to a container. It is only available if wxUSE_STL is set to 1
469 in setup.h, and the uses for it outlined below require a compiler which
470 supports member templates.
471
472 @code
473 template class Arc, class T = typename Arc::entry_type*
474 class wxArchiveIterator
475 {
476 // this constructor creates an 'end of sequence' object
477 wxArchiveIterator();
478
479 // template parameter 'Arc' should be the type of an archive input stream
480 wxArchiveIterator(Arc& arc) {
481
482 /* ... */
483 };
484 @endcode
485
486 The first template parameter should be the type of archive input stream
487 (e.g. wxArchiveInputStream) and the
488 second can either be a pointer to an entry
489 (e.g. wxArchiveEntry*), or a string/pointer pair
490 (e.g. std::pairwxString, wxArchiveEntry*).
491
492 The @c wx/archive.h header defines the following typedefs:
493
494 @code
495 typedef wxArchiveIteratorwxArchiveInputStream wxArchiveIter;
496
497 typedef wxArchiveIteratorwxArchiveInputStream,
498 std::pairwxString, wxArchiveEntry* wxArchivePairIter;
499 @endcode
500
501 The header for any implementation of this interface should define similar
502 typedefs for its types, for example in @c wx/zipstrm.h there is:
503
504 @code
505 typedef wxArchiveIteratorwxZipInputStream wxZipIter;
506
507 typedef wxArchiveIteratorwxZipInputStream,
508 std::pairwxString, wxZipEntry* wxZipPairIter;
509 @endcode
510
511 Transferring the catalogue of an archive @e arc to a vector @e cat,
512 can then be done something like this:
513
514 @code
515 std::vectorwxArchiveEntry* cat((wxArchiveIter)arc, wxArchiveIter());
516 @endcode
517
518 When the iterator is dereferenced, it gives away ownership of an entry
519 object. So in the above example, when you have finished with @e cat
520 you must delete the pointers it contains.
521
522 If you have smart pointers with normal copy semantics (i.e. not auto_ptr
523 or wxScopedPtr), then you can create an iterator
524 which uses them instead. For example, with a smart pointer class for
525 zip entries @e ZipEntryPtr:
526
527 @code
528 typedef std::vectorZipEntryPtr ZipCatalog;
529 typedef wxArchiveIteratorwxZipInputStream, ZipEntryPtr ZipIter;
530 ZipCatalog cat((ZipIter)zip, ZipIter());
531 @endcode
532
533 Iterators that return std::pair objects can be used to
534 populate a std::multimap, to allow entries to be looked
535 up by name. The string is initialised using the wxArchiveEntry object's
536 wxArchiveEntry::GetInternalName function.
537
538 @code
539 typedef std::multimapwxString, wxZipEntry* ZipCatalog;
540 ZipCatalog cat((wxZipPairIter)zip, wxZipPairIter());
541 @endcode
542
543
544 Note that this iterator also gives away ownership of an entry
545 object each time it is dereferenced. So in the above example, when
546 you have finished with @e cat you must delete the pointers it contains.
547
548 Or if you have them, a pair containing a smart pointer can be used
549 (again @e ZipEntryPtr), no worries about ownership:
550
551 @code
552 typedef std::multimapwxString, ZipEntryPtr ZipCatalog;
553 typedef wxArchiveIteratorwxZipInputStream,
554 std::pairwxString, ZipEntryPtr ZipPairIter;
555 ZipCatalog cat((ZipPairIter)zip, ZipPairIter());
556 @endcode
557
558 @library{wxbase}
559 @category{FIXME}
560
561 @seealso
562 wxArchiveEntry, wxArchiveInputStream, wxArchiveOutputStream
563 */
564 class wxArchiveIterator
565 {
566 public:
567 //@{
568 /**
569 Construct iterator that returns all the entries in the archive input
570 stream @e arc.
571 */
572 wxArchiveIterator();
573 wxArchiveIterator(Arc& arc);
574 //@}
575
576 /**
577 Returns an entry object from the archive input stream, giving away
578 ownership.
579 */
580 const T operator*();
581
582 //@{
583 /**
584 Position the input iterator at the next entry in the archive input stream.
585 */
586 wxArchiveIterator operator++();
587 wxArchiveIterator operator++(int );
588 //@}
589 };