archive.h 11 KB

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