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