]> git.saurik.com Git - wxWidgets.git/blob - include/wx/private/fileback.h
aaf5ab4afb8ebdfb3724b719f372da990381168a
[wxWidgets.git] / include / wx / private / fileback.h
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
9 #ifndef _WX_FILEBACK_H__
10 #define _WX_FILEBACK_H__
11
12 #include "wx/defs.h"
13
14 #if wxUSE_FILESYSTEM
15
16 #include "wx/stream.h"
17
18 // ----------------------------------------------------------------------------
19 // Backs an input stream with memory or a file to make it seekable.
20 //
21 // One or more wxBackedInputStreams can be used to read it's data. The data is
22 // reference counted, so stays alive until the last wxBackingFile or
23 // wxBackedInputStream using it is destroyed.
24 // ----------------------------------------------------------------------------
25
26 class WXDLLIMPEXP_BASE wxBackingFile
27 {
28 public:
29 enum { DefaultBufSize = 16384 };
30
31 // Takes ownership of stream. If the stream is smaller than bufsize, the
32 // backing file is never created and the backing is done with memory.
33 wxBackingFile(wxInputStream *stream,
34 size_t bufsize = DefaultBufSize,
35 const wxString& prefix = wxT("wxbf"));
36
37 wxBackingFile() : m_impl(NULL) { }
38 ~wxBackingFile();
39
40 wxBackingFile(const wxBackingFile& backer);
41 wxBackingFile& operator=(const wxBackingFile& backer);
42
43 operator bool() const { return m_impl != NULL; }
44
45 private:
46 class wxBackingFileImpl *m_impl;
47 friend class wxBackedInputStream;
48 };
49
50 // ----------------------------------------------------------------------------
51 // An input stream to read from a wxBackingFile.
52 // ----------------------------------------------------------------------------
53
54 class WXDLLIMPEXP_BASE wxBackedInputStream : public wxInputStream
55 {
56 public:
57 wxBackedInputStream(const wxBackingFile& backer);
58
59 // If the length of the backer's parent stream is unknown then GetLength()
60 // returns wxInvalidOffset until the parent has been read to the end.
61 wxFileOffset GetLength() const;
62
63 // Returns the length, reading the parent stream to the end if necessary.
64 wxFileOffset FindLength() const;
65
66 bool IsSeekable() const { return true; }
67
68 protected:
69 size_t OnSysRead(void *buffer, size_t size);
70 wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
71 wxFileOffset OnSysTell() const;
72
73 private:
74 wxBackingFile m_backer;
75 wxFileOffset m_pos;
76
77 wxDECLARE_NO_COPY_CLASS(wxBackedInputStream);
78 };
79
80 #endif // wxUSE_FILESYSTEM
81
82 #endif // _WX_FILEBACK_H__