1 /*
2 
3   Copyright (C) 2003 - 2013  Razvan Cojocaru <rzvncj@gmail.com>
4 
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18   MA 02110-1301, USA.
19 
20 */
21 
22 
23 #ifndef __CHMINPUTSTREAM_H_
24 #define __CHMINPUTSTREAM_H_
25 
26 #include <wx/stream.h>
27 #include <chmfile.h>
28 
29 
30 /*!
31   \class wxInputStream
32   \brief wxWidgets input stream class.
33 */
34 
35 
36 //! Input stream from a .chm archive.
37 class CHMInputStream : public wxInputStream
38 {
39 public:
40 	/*!
41 	  \brief Creates a stream.
42 	  \param archive The .chm file name on disk. It makes sense to
43 	  pass the empty string if you're sure file is a link to a
44 	  page, in the form of "/MS-ITS:archive.chm::/filename.html".
45 	  \param file The file requested from the archive.
46 	 */
47 	CHMInputStream(const wxString& archive, const wxString& file);
48 
49 	//! Returns the size of the file.
50 	virtual size_t GetSize() const;
51 
52 	//! True if EOF has been found.
53 	virtual bool Eof() const;
54 
55 	/*!
56 	  \brief Returns the static CHMFile pointer associated with
57 	  this stream. Archives are being cached until it is
58 	  explicitly requested to open a different one.
59 	  \return A valid pointer to a CHMFile object or NULL if no
60 	  .chm file has been opened yet.
61 	 */
62 	static CHMFile* GetCache();
63 	/*!
64 	  \brief Cleans up the cache. Has to be public and static
65 	  since the stream doesn't know how many other streams using
66 	  the same cache will be created after it. Somebody else has
67 	  to turn off the lights, and in this case it's CHMFSHandler.
68 	 */
69 	static void Cleanup();
70 
71 protected:
72 	/*!
73 	  \brief Attempts to read a chunk from the stream.
74 	  \param buffer The read data is being placed here.
75 	  \param bufsize Number of bytes requested.
76 	  \return Number of bytes actually read.
77 	*/
78 	virtual size_t OnSysRead(void *buffer, size_t bufsize);
79 
80 	/*!
81 	  \brief Seeks to the requested position in the file.
82 	  \param seek Where to seek.
83 	  \param mode Seek from the beginning, current position, etc.
84 	  \return Position in the file.
85 	 */
86 	virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
87 
88 	/*!
89 	  \brief Asks what is the current position in the file.
90 	  \return Current position.
91 	 */
OnSysTell()92 	virtual wxFileOffset OnSysTell() const { return _currPos; }
93 
94 private:
95 	//! Helper. Inits the cache.
96 	bool Init(const wxString& archive);
97 
98 
99 private:
100 	static CHMFile *_archiveCache;
101 	off_t _currPos;
102 	chmUnitInfo _ui;
103 	static wxString _path;
104 };
105 
106 #endif // __CHMINPUTSTREAM_H_
107 
108 
109 /*
110   Local Variables:
111   mode: c++
112   c-basic-offset: 8
113   tab-width: 8
114   c-indent-comments-syntactically-p: t
115   c-tab-always-indent: t
116   indent-tabs-mode: t
117   End:
118 */
119 
120 // vim:shiftwidth=8:autoindent:tabstop=8:noexpandtab:softtabstop=8
121