archive.h 21 KB

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