fileback.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/private/fileback.h
  3. // Purpose: Back an input stream with memory or a file
  4. // Author: Mike Wetherell
  5. // Copyright: (c) 2006 Mike Wetherell
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. #ifndef _WX_FILEBACK_H__
  9. #define _WX_FILEBACK_H__
  10. #include "wx/defs.h"
  11. #if wxUSE_FILESYSTEM
  12. #include "wx/stream.h"
  13. // ----------------------------------------------------------------------------
  14. // Backs an input stream with memory or a file to make it seekable.
  15. //
  16. // One or more wxBackedInputStreams can be used to read it's data. The data is
  17. // reference counted, so stays alive until the last wxBackingFile or
  18. // wxBackedInputStream using it is destroyed.
  19. // ----------------------------------------------------------------------------
  20. class WXDLLIMPEXP_BASE wxBackingFile
  21. {
  22. public:
  23. enum { DefaultBufSize = 16384 };
  24. // Takes ownership of stream. If the stream is smaller than bufsize, the
  25. // backing file is never created and the backing is done with memory.
  26. wxBackingFile(wxInputStream *stream,
  27. size_t bufsize = DefaultBufSize,
  28. const wxString& prefix = wxT("wxbf"));
  29. wxBackingFile() : m_impl(NULL) { }
  30. ~wxBackingFile();
  31. wxBackingFile(const wxBackingFile& backer);
  32. wxBackingFile& operator=(const wxBackingFile& backer);
  33. operator bool() const { return m_impl != NULL; }
  34. private:
  35. class wxBackingFileImpl *m_impl;
  36. friend class wxBackedInputStream;
  37. };
  38. // ----------------------------------------------------------------------------
  39. // An input stream to read from a wxBackingFile.
  40. // ----------------------------------------------------------------------------
  41. class WXDLLIMPEXP_BASE wxBackedInputStream : public wxInputStream
  42. {
  43. public:
  44. wxBackedInputStream(const wxBackingFile& backer);
  45. // If the length of the backer's parent stream is unknown then GetLength()
  46. // returns wxInvalidOffset until the parent has been read to the end.
  47. wxFileOffset GetLength() const;
  48. // Returns the length, reading the parent stream to the end if necessary.
  49. wxFileOffset FindLength() const;
  50. bool IsSeekable() const { return true; }
  51. protected:
  52. size_t OnSysRead(void *buffer, size_t size);
  53. wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
  54. wxFileOffset OnSysTell() const;
  55. private:
  56. wxBackingFile m_backer;
  57. wxFileOffset m_pos;
  58. wxDECLARE_NO_COPY_CLASS(wxBackedInputStream);
  59. };
  60. #endif // wxUSE_FILESYSTEM
  61. #endif // _WX_FILEBACK_H__