PageRenderTime 178ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/common/SDK/wxWidgets/include/wx/archive.h

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