stream.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/stream.h
  3. // Purpose: stream classes
  4. // Author: Guilhem Lavaux, Guillermo Rodriguez Garcia, Vadim Zeitlin
  5. // Modified by:
  6. // Created: 11/07/98
  7. // Copyright: (c) Guilhem Lavaux
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_WXSTREAM_H__
  11. #define _WX_WXSTREAM_H__
  12. #include "wx/defs.h"
  13. #if wxUSE_STREAMS
  14. #include <stdio.h>
  15. #include "wx/object.h"
  16. #include "wx/string.h"
  17. #include "wx/filefn.h" // for wxFileOffset, wxInvalidOffset and wxSeekMode
  18. class WXDLLIMPEXP_FWD_BASE wxStreamBase;
  19. class WXDLLIMPEXP_FWD_BASE wxInputStream;
  20. class WXDLLIMPEXP_FWD_BASE wxOutputStream;
  21. typedef wxInputStream& (*__wxInputManip)(wxInputStream&);
  22. typedef wxOutputStream& (*__wxOutputManip)(wxOutputStream&);
  23. WXDLLIMPEXP_BASE wxOutputStream& wxEndL(wxOutputStream& o_stream);
  24. // ----------------------------------------------------------------------------
  25. // constants
  26. // ----------------------------------------------------------------------------
  27. enum wxStreamError
  28. {
  29. wxSTREAM_NO_ERROR = 0, // stream is in good state
  30. wxSTREAM_EOF, // EOF reached in Read() or similar
  31. wxSTREAM_WRITE_ERROR, // generic write error
  32. wxSTREAM_READ_ERROR // generic read error
  33. };
  34. const int wxEOF = -1;
  35. // ============================================================================
  36. // base stream classes: wxInputStream and wxOutputStream
  37. // ============================================================================
  38. // ---------------------------------------------------------------------------
  39. // wxStreamBase: common (but non virtual!) base for all stream classes
  40. // ---------------------------------------------------------------------------
  41. class WXDLLIMPEXP_BASE wxStreamBase : public wxObject
  42. {
  43. public:
  44. wxStreamBase();
  45. virtual ~wxStreamBase();
  46. // error testing
  47. wxStreamError GetLastError() const { return m_lasterror; }
  48. virtual bool IsOk() const { return GetLastError() == wxSTREAM_NO_ERROR; }
  49. bool operator!() const { return !IsOk(); }
  50. // reset the stream state
  51. void Reset(wxStreamError error = wxSTREAM_NO_ERROR) { m_lasterror = error; }
  52. // this doesn't make sense for all streams, always test its return value
  53. virtual size_t GetSize() const;
  54. virtual wxFileOffset GetLength() const { return wxInvalidOffset; }
  55. // returns true if the streams supports seeking to arbitrary offsets
  56. virtual bool IsSeekable() const { return false; }
  57. protected:
  58. virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
  59. virtual wxFileOffset OnSysTell() const;
  60. size_t m_lastcount;
  61. wxStreamError m_lasterror;
  62. friend class wxStreamBuffer;
  63. DECLARE_ABSTRACT_CLASS(wxStreamBase)
  64. wxDECLARE_NO_COPY_CLASS(wxStreamBase);
  65. };
  66. // ----------------------------------------------------------------------------
  67. // wxInputStream: base class for the input streams
  68. // ----------------------------------------------------------------------------
  69. class WXDLLIMPEXP_BASE wxInputStream : public wxStreamBase
  70. {
  71. public:
  72. // ctor and dtor, nothing exciting
  73. wxInputStream();
  74. virtual ~wxInputStream();
  75. // IO functions
  76. // ------------
  77. // return a character from the stream without removing it, i.e. it will
  78. // still be returned by the next call to GetC()
  79. //
  80. // blocks until something appears in the stream if necessary, if nothing
  81. // ever does (i.e. EOF) LastRead() will return 0 (and the return value is
  82. // undefined), otherwise 1
  83. virtual char Peek();
  84. // return one byte from the stream, blocking until it appears if
  85. // necessary
  86. //
  87. // on success returns a value between 0 - 255, or wxEOF on EOF or error.
  88. int GetC();
  89. // read at most the given number of bytes from the stream
  90. //
  91. // there are 2 possible situations here: either there is nothing at all in
  92. // the stream right now in which case Read() blocks until something appears
  93. // (use CanRead() to avoid this) or there is already some data available in
  94. // the stream and then Read() doesn't block but returns just the data it
  95. // can read without waiting for more
  96. //
  97. // in any case, if there are not enough bytes in the stream right now,
  98. // LastRead() value will be less than size but greater than 0. If it is 0,
  99. // it means that EOF has been reached.
  100. virtual wxInputStream& Read(void *buffer, size_t size);
  101. // Read exactly the given number of bytes, unlike Read(), which may read
  102. // less than the requested amount of data without returning an error, this
  103. // method either reads all the data or returns false.
  104. bool ReadAll(void *buffer, size_t size);
  105. // copy the entire contents of this stream into streamOut, stopping only
  106. // when EOF is reached or an error occurs
  107. wxInputStream& Read(wxOutputStream& streamOut);
  108. // status functions
  109. // ----------------
  110. // returns the number of bytes read by the last call to Read(), GetC() or
  111. // Peek()
  112. //
  113. // this should be used to discover whether that call succeeded in reading
  114. // all the requested data or not
  115. virtual size_t LastRead() const { return wxStreamBase::m_lastcount; }
  116. // returns true if some data is available in the stream right now, so that
  117. // calling Read() wouldn't block
  118. virtual bool CanRead() const;
  119. // is the stream at EOF?
  120. //
  121. // note that this cannot be really implemented for all streams and
  122. // CanRead() is more reliable than Eof()
  123. virtual bool Eof() const;
  124. // write back buffer
  125. // -----------------
  126. // put back the specified number of bytes into the stream, they will be
  127. // fetched by the next call to the read functions
  128. //
  129. // returns the number of bytes really stuffed back
  130. size_t Ungetch(const void *buffer, size_t size);
  131. // put back the specified character in the stream
  132. //
  133. // returns true if ok, false on error
  134. bool Ungetch(char c);
  135. // position functions
  136. // ------------------
  137. // move the stream pointer to the given position (if the stream supports
  138. // it)
  139. //
  140. // returns wxInvalidOffset on error
  141. virtual wxFileOffset SeekI(wxFileOffset pos, wxSeekMode mode = wxFromStart);
  142. // return the current position of the stream pointer or wxInvalidOffset
  143. virtual wxFileOffset TellI() const;
  144. // stream-like operators
  145. // ---------------------
  146. wxInputStream& operator>>(wxOutputStream& out) { return Read(out); }
  147. wxInputStream& operator>>(__wxInputManip func) { return func(*this); }
  148. protected:
  149. // do read up to size bytes of data into the provided buffer
  150. //
  151. // this method should return 0 if EOF has been reached or an error occurred
  152. // (m_lasterror should be set accordingly as well) or the number of bytes
  153. // read
  154. virtual size_t OnSysRead(void *buffer, size_t size) = 0;
  155. // write-back buffer support
  156. // -------------------------
  157. // return the pointer to a buffer big enough to hold sizeNeeded bytes
  158. char *AllocSpaceWBack(size_t sizeNeeded);
  159. // read up to size data from the write back buffer, return the number of
  160. // bytes read
  161. size_t GetWBack(void *buf, size_t size);
  162. // write back buffer or NULL if none
  163. char *m_wback;
  164. // the size of the buffer
  165. size_t m_wbacksize;
  166. // the current position in the buffer
  167. size_t m_wbackcur;
  168. friend class wxStreamBuffer;
  169. DECLARE_ABSTRACT_CLASS(wxInputStream)
  170. wxDECLARE_NO_COPY_CLASS(wxInputStream);
  171. };
  172. // ----------------------------------------------------------------------------
  173. // wxOutputStream: base for the output streams
  174. // ----------------------------------------------------------------------------
  175. class WXDLLIMPEXP_BASE wxOutputStream : public wxStreamBase
  176. {
  177. public:
  178. wxOutputStream();
  179. virtual ~wxOutputStream();
  180. void PutC(char c);
  181. virtual wxOutputStream& Write(const void *buffer, size_t size);
  182. // This is ReadAll() equivalent for Write(): it either writes exactly the
  183. // given number of bytes or returns false, unlike Write() which can write
  184. // less data than requested but still return without error.
  185. bool WriteAll(const void *buffer, size_t size);
  186. wxOutputStream& Write(wxInputStream& stream_in);
  187. virtual wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart);
  188. virtual wxFileOffset TellO() const;
  189. virtual size_t LastWrite() const { return wxStreamBase::m_lastcount; }
  190. virtual void Sync();
  191. virtual bool Close() { return true; }
  192. wxOutputStream& operator<<(wxInputStream& out) { return Write(out); }
  193. wxOutputStream& operator<<( __wxOutputManip func) { return func(*this); }
  194. protected:
  195. // to be implemented in the derived classes (it should have been pure
  196. // virtual)
  197. virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
  198. friend class wxStreamBuffer;
  199. DECLARE_ABSTRACT_CLASS(wxOutputStream)
  200. wxDECLARE_NO_COPY_CLASS(wxOutputStream);
  201. };
  202. // ============================================================================
  203. // helper stream classes
  204. // ============================================================================
  205. // ---------------------------------------------------------------------------
  206. // A stream for measuring streamed output
  207. // ---------------------------------------------------------------------------
  208. class WXDLLIMPEXP_BASE wxCountingOutputStream : public wxOutputStream
  209. {
  210. public:
  211. wxCountingOutputStream();
  212. virtual wxFileOffset GetLength() const;
  213. bool Ok() const { return IsOk(); }
  214. virtual bool IsOk() const { return true; }
  215. protected:
  216. virtual size_t OnSysWrite(const void *buffer, size_t size);
  217. virtual wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
  218. virtual wxFileOffset OnSysTell() const;
  219. size_t m_currentPos,
  220. m_lastPos;
  221. DECLARE_DYNAMIC_CLASS(wxCountingOutputStream)
  222. wxDECLARE_NO_COPY_CLASS(wxCountingOutputStream);
  223. };
  224. // ---------------------------------------------------------------------------
  225. // "Filter" streams
  226. // ---------------------------------------------------------------------------
  227. class WXDLLIMPEXP_BASE wxFilterInputStream : public wxInputStream
  228. {
  229. public:
  230. wxFilterInputStream();
  231. wxFilterInputStream(wxInputStream& stream);
  232. wxFilterInputStream(wxInputStream *stream);
  233. virtual ~wxFilterInputStream();
  234. char Peek() { return m_parent_i_stream->Peek(); }
  235. wxFileOffset GetLength() const { return m_parent_i_stream->GetLength(); }
  236. wxInputStream *GetFilterInputStream() const { return m_parent_i_stream; }
  237. protected:
  238. wxInputStream *m_parent_i_stream;
  239. bool m_owns;
  240. DECLARE_ABSTRACT_CLASS(wxFilterInputStream)
  241. wxDECLARE_NO_COPY_CLASS(wxFilterInputStream);
  242. };
  243. class WXDLLIMPEXP_BASE wxFilterOutputStream : public wxOutputStream
  244. {
  245. public:
  246. wxFilterOutputStream();
  247. wxFilterOutputStream(wxOutputStream& stream);
  248. wxFilterOutputStream(wxOutputStream *stream);
  249. virtual ~wxFilterOutputStream();
  250. wxFileOffset GetLength() const { return m_parent_o_stream->GetLength(); }
  251. wxOutputStream *GetFilterOutputStream() const { return m_parent_o_stream; }
  252. bool Close();
  253. protected:
  254. wxOutputStream *m_parent_o_stream;
  255. bool m_owns;
  256. DECLARE_ABSTRACT_CLASS(wxFilterOutputStream)
  257. wxDECLARE_NO_COPY_CLASS(wxFilterOutputStream);
  258. };
  259. enum wxStreamProtocolType
  260. {
  261. wxSTREAM_PROTOCOL, // wxFileSystem protocol (should be only one)
  262. wxSTREAM_MIMETYPE, // MIME types the stream handles
  263. wxSTREAM_ENCODING, // The HTTP Content-Encodings the stream handles
  264. wxSTREAM_FILEEXT // File extensions the stream handles
  265. };
  266. void WXDLLIMPEXP_BASE wxUseFilterClasses();
  267. class WXDLLIMPEXP_BASE wxFilterClassFactoryBase : public wxObject
  268. {
  269. public:
  270. virtual ~wxFilterClassFactoryBase() { }
  271. wxString GetProtocol() const { return wxString(*GetProtocols()); }
  272. wxString PopExtension(const wxString& location) const;
  273. virtual const wxChar * const *GetProtocols(wxStreamProtocolType type
  274. = wxSTREAM_PROTOCOL) const = 0;
  275. bool CanHandle(const wxString& protocol,
  276. wxStreamProtocolType type
  277. = wxSTREAM_PROTOCOL) const;
  278. protected:
  279. wxString::size_type FindExtension(const wxString& location) const;
  280. DECLARE_ABSTRACT_CLASS(wxFilterClassFactoryBase)
  281. };
  282. class WXDLLIMPEXP_BASE wxFilterClassFactory : public wxFilterClassFactoryBase
  283. {
  284. public:
  285. virtual ~wxFilterClassFactory() { }
  286. virtual wxFilterInputStream *NewStream(wxInputStream& stream) const = 0;
  287. virtual wxFilterOutputStream *NewStream(wxOutputStream& stream) const = 0;
  288. virtual wxFilterInputStream *NewStream(wxInputStream *stream) const = 0;
  289. virtual wxFilterOutputStream *NewStream(wxOutputStream *stream) const = 0;
  290. static const wxFilterClassFactory *Find(const wxString& protocol,
  291. wxStreamProtocolType type
  292. = wxSTREAM_PROTOCOL);
  293. static const wxFilterClassFactory *GetFirst();
  294. const wxFilterClassFactory *GetNext() const { return m_next; }
  295. void PushFront() { Remove(); m_next = sm_first; sm_first = this; }
  296. void Remove();
  297. protected:
  298. wxFilterClassFactory() : m_next(this) { }
  299. wxFilterClassFactory& operator=(const wxFilterClassFactory&)
  300. { return *this; }
  301. private:
  302. static wxFilterClassFactory *sm_first;
  303. wxFilterClassFactory *m_next;
  304. DECLARE_ABSTRACT_CLASS(wxFilterClassFactory)
  305. };
  306. // ============================================================================
  307. // buffered streams
  308. // ============================================================================
  309. // ---------------------------------------------------------------------------
  310. // Stream buffer: this class can be derived from and passed to
  311. // wxBufferedStreams to implement custom buffering
  312. // ---------------------------------------------------------------------------
  313. class WXDLLIMPEXP_BASE wxStreamBuffer
  314. {
  315. public:
  316. enum BufMode
  317. {
  318. read,
  319. write,
  320. read_write
  321. };
  322. wxStreamBuffer(wxStreamBase& stream, BufMode mode)
  323. {
  324. InitWithStream(stream, mode);
  325. }
  326. wxStreamBuffer(size_t bufsize, wxInputStream& stream)
  327. {
  328. InitWithStream(stream, read);
  329. SetBufferIO(bufsize);
  330. }
  331. wxStreamBuffer(size_t bufsize, wxOutputStream& stream)
  332. {
  333. InitWithStream(stream, write);
  334. SetBufferIO(bufsize);
  335. }
  336. wxStreamBuffer(const wxStreamBuffer& buf);
  337. virtual ~wxStreamBuffer();
  338. // Filtered IO
  339. virtual size_t Read(void *buffer, size_t size);
  340. size_t Read(wxStreamBuffer *buf);
  341. virtual size_t Write(const void *buffer, size_t size);
  342. size_t Write(wxStreamBuffer *buf);
  343. virtual char Peek();
  344. virtual char GetChar();
  345. virtual void PutChar(char c);
  346. virtual wxFileOffset Tell() const;
  347. virtual wxFileOffset Seek(wxFileOffset pos, wxSeekMode mode);
  348. // Buffer control
  349. void ResetBuffer();
  350. void Truncate();
  351. // NB: the buffer must always be allocated with malloc() if takeOwn is
  352. // true as it will be deallocated by free()
  353. void SetBufferIO(void *start, void *end, bool takeOwnership = false);
  354. void SetBufferIO(void *start, size_t len, bool takeOwnership = false);
  355. void SetBufferIO(size_t bufsize);
  356. void *GetBufferStart() const { return m_buffer_start; }
  357. void *GetBufferEnd() const { return m_buffer_end; }
  358. void *GetBufferPos() const { return m_buffer_pos; }
  359. size_t GetBufferSize() const { return m_buffer_end - m_buffer_start; }
  360. size_t GetIntPosition() const { return m_buffer_pos - m_buffer_start; }
  361. void SetIntPosition(size_t pos) { m_buffer_pos = m_buffer_start + pos; }
  362. size_t GetLastAccess() const { return m_buffer_end - m_buffer_start; }
  363. size_t GetBytesLeft() const { return m_buffer_end - m_buffer_pos; }
  364. void Fixed(bool fixed) { m_fixed = fixed; }
  365. void Flushable(bool f) { m_flushable = f; }
  366. bool FlushBuffer();
  367. bool FillBuffer();
  368. size_t GetDataLeft();
  369. // misc accessors
  370. wxStreamBase *GetStream() const { return m_stream; }
  371. bool HasBuffer() const { return m_buffer_start != m_buffer_end; }
  372. bool IsFixed() const { return m_fixed; }
  373. bool IsFlushable() const { return m_flushable; }
  374. // only for input/output buffers respectively, returns NULL otherwise
  375. wxInputStream *GetInputStream() const;
  376. wxOutputStream *GetOutputStream() const;
  377. #if WXWIN_COMPATIBILITY_2_6
  378. // deprecated, for compatibility only
  379. wxDEPRECATED( wxStreamBase *Stream() );
  380. #endif // WXWIN_COMPATIBILITY_2_6
  381. // this constructs a dummy wxStreamBuffer, used by (and exists for)
  382. // wxMemoryStreams only, don't use!
  383. wxStreamBuffer(BufMode mode);
  384. protected:
  385. void GetFromBuffer(void *buffer, size_t size);
  386. void PutToBuffer(const void *buffer, size_t size);
  387. // set the last error to the specified value if we didn't have it before
  388. void SetError(wxStreamError err);
  389. // common part of several ctors
  390. void Init();
  391. // common part of ctors taking wxStreamBase parameter
  392. void InitWithStream(wxStreamBase& stream, BufMode mode);
  393. // init buffer variables to be empty
  394. void InitBuffer();
  395. // free the buffer (always safe to call)
  396. void FreeBuffer();
  397. // the buffer itself: the pointers to its start and end and the current
  398. // position in the buffer
  399. char *m_buffer_start,
  400. *m_buffer_end,
  401. *m_buffer_pos;
  402. // the stream we're associated with
  403. wxStreamBase *m_stream;
  404. // its mode
  405. BufMode m_mode;
  406. // flags
  407. bool m_destroybuf, // deallocate buffer?
  408. m_fixed,
  409. m_flushable;
  410. wxDECLARE_NO_ASSIGN_CLASS(wxStreamBuffer);
  411. };
  412. // ---------------------------------------------------------------------------
  413. // wxBufferedInputStream
  414. // ---------------------------------------------------------------------------
  415. class WXDLLIMPEXP_BASE wxBufferedInputStream : public wxFilterInputStream
  416. {
  417. public:
  418. // create a buffered stream on top of the specified low-level stream
  419. //
  420. // if a non NULL buffer is given to the stream, it will be deleted by it,
  421. // otherwise a default 1KB buffer will be used
  422. wxBufferedInputStream(wxInputStream& stream,
  423. wxStreamBuffer *buffer = NULL);
  424. // ctor allowing to specify the buffer size, it's just a more convenient
  425. // alternative to creating wxStreamBuffer, calling its SetBufferIO(bufsize)
  426. // and using the ctor above
  427. wxBufferedInputStream(wxInputStream& stream, size_t bufsize);
  428. virtual ~wxBufferedInputStream();
  429. char Peek();
  430. wxInputStream& Read(void *buffer, size_t size);
  431. // Position functions
  432. wxFileOffset SeekI(wxFileOffset pos, wxSeekMode mode = wxFromStart);
  433. wxFileOffset TellI() const;
  434. bool IsSeekable() const { return m_parent_i_stream->IsSeekable(); }
  435. // the buffer given to the stream will be deleted by it
  436. void SetInputStreamBuffer(wxStreamBuffer *buffer);
  437. wxStreamBuffer *GetInputStreamBuffer() const { return m_i_streambuf; }
  438. #if WXWIN_COMPATIBILITY_2_6
  439. // deprecated, for compatibility only
  440. wxDEPRECATED( wxStreamBuffer *InputStreamBuffer() const );
  441. #endif // WXWIN_COMPATIBILITY_2_6
  442. protected:
  443. virtual size_t OnSysRead(void *buffer, size_t bufsize);
  444. virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
  445. virtual wxFileOffset OnSysTell() const;
  446. wxStreamBuffer *m_i_streambuf;
  447. wxDECLARE_NO_COPY_CLASS(wxBufferedInputStream);
  448. };
  449. // ----------------------------------------------------------------------------
  450. // wxBufferedOutputStream
  451. // ----------------------------------------------------------------------------
  452. class WXDLLIMPEXP_BASE wxBufferedOutputStream : public wxFilterOutputStream
  453. {
  454. public:
  455. // create a buffered stream on top of the specified low-level stream
  456. //
  457. // if a non NULL buffer is given to the stream, it will be deleted by it,
  458. // otherwise a default 1KB buffer will be used
  459. wxBufferedOutputStream(wxOutputStream& stream,
  460. wxStreamBuffer *buffer = NULL);
  461. // ctor allowing to specify the buffer size, it's just a more convenient
  462. // alternative to creating wxStreamBuffer, calling its SetBufferIO(bufsize)
  463. // and using the ctor above
  464. wxBufferedOutputStream(wxOutputStream& stream, size_t bufsize);
  465. virtual ~wxBufferedOutputStream();
  466. wxOutputStream& Write(const void *buffer, size_t size);
  467. // Position functions
  468. wxFileOffset SeekO(wxFileOffset pos, wxSeekMode mode = wxFromStart);
  469. wxFileOffset TellO() const;
  470. bool IsSeekable() const { return m_parent_o_stream->IsSeekable(); }
  471. void Sync();
  472. bool Close();
  473. wxFileOffset GetLength() const;
  474. // the buffer given to the stream will be deleted by it
  475. void SetOutputStreamBuffer(wxStreamBuffer *buffer);
  476. wxStreamBuffer *GetOutputStreamBuffer() const { return m_o_streambuf; }
  477. #if WXWIN_COMPATIBILITY_2_6
  478. // deprecated, for compatibility only
  479. wxDEPRECATED( wxStreamBuffer *OutputStreamBuffer() const );
  480. #endif // WXWIN_COMPATIBILITY_2_6
  481. protected:
  482. virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
  483. virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
  484. virtual wxFileOffset OnSysTell() const;
  485. wxStreamBuffer *m_o_streambuf;
  486. wxDECLARE_NO_COPY_CLASS(wxBufferedOutputStream);
  487. };
  488. #if WXWIN_COMPATIBILITY_2_6
  489. inline wxStreamBase *wxStreamBuffer::Stream() { return m_stream; }
  490. inline wxStreamBuffer *wxBufferedInputStream::InputStreamBuffer() const { return m_i_streambuf; }
  491. inline wxStreamBuffer *wxBufferedOutputStream::OutputStreamBuffer() const { return m_o_streambuf; }
  492. #endif // WXWIN_COMPATIBILITY_2_6
  493. // ---------------------------------------------------------------------------
  494. // wxWrapperInputStream: forwards all IO to another stream.
  495. // ---------------------------------------------------------------------------
  496. class WXDLLIMPEXP_BASE wxWrapperInputStream : public wxFilterInputStream
  497. {
  498. public:
  499. // Constructor fully initializing the stream. The overload taking pointer
  500. // takes ownership of the parent stream, the one taking reference does not.
  501. //
  502. // Notice that this class also has a default ctor but it's protected as the
  503. // derived class is supposed to take care of calling InitParentStream() if
  504. // it's used.
  505. wxWrapperInputStream(wxInputStream& stream);
  506. wxWrapperInputStream(wxInputStream* stream);
  507. // Override the base class methods to forward to the wrapped stream.
  508. virtual wxFileOffset GetLength() const;
  509. virtual bool IsSeekable() const;
  510. protected:
  511. virtual size_t OnSysRead(void *buffer, size_t size);
  512. virtual wxFileOffset OnSysSeek(wxFileOffset pos, wxSeekMode mode);
  513. virtual wxFileOffset OnSysTell() const;
  514. // Ensure that our own last error is the same as that of the real stream.
  515. //
  516. // This method is const because the error must be updated even from const
  517. // methods (in other words, it really should have been mutable in the first
  518. // place).
  519. void SynchronizeLastError() const
  520. {
  521. const_cast<wxWrapperInputStream*>(this)->
  522. Reset(m_parent_i_stream->GetLastError());
  523. }
  524. // Default constructor, use InitParentStream() later.
  525. wxWrapperInputStream();
  526. // Set up the wrapped stream for an object initialized using the default
  527. // constructor. The ownership logic is the same as above.
  528. void InitParentStream(wxInputStream& stream);
  529. void InitParentStream(wxInputStream* stream);
  530. wxDECLARE_NO_COPY_CLASS(wxWrapperInputStream);
  531. };
  532. #endif // wxUSE_STREAMS
  533. #endif // _WX_WXSTREAM_H__