1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/stream.cpp
3 // Purpose: wxStream base classes
4 // Author: Guilhem Lavaux
5 // Modified by: VZ (23.11.00) to fix realloc()ing new[]ed memory,
9 // Copyright: (c) Guilhem Lavaux
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
30 #include "wx/stream.h"
37 #include "wx/datstrm.h"
38 #include "wx/textfile.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // the temporary buffer size used when copying from stream to stream
45 #define BUF_TEMP_SIZE 4096
47 // ============================================================================
49 // ============================================================================
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 void wxStreamBuffer::SetError(wxStreamError err
)
57 if ( m_stream
&& m_stream
->m_lasterror
== wxSTREAM_NO_ERROR
)
58 m_stream
->m_lasterror
= err
;
61 void wxStreamBuffer::InitBuffer()
68 // if we are going to allocate the buffer, we should free it later as well
72 void wxStreamBuffer::Init()
79 wxStreamBuffer::wxStreamBuffer(BufMode mode
)
89 wxStreamBuffer::wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
)
99 wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer
& buffer
)
101 // doing this has big chances to lead to a crash when the source buffer is
102 // destroyed (otherwise assume the caller knows what he does)
103 wxASSERT_MSG( !buffer
.m_destroybuf
,
104 _T("it's a bad idea to copy this buffer") );
106 m_buffer_start
= buffer
.m_buffer_start
;
107 m_buffer_end
= buffer
.m_buffer_end
;
108 m_buffer_pos
= buffer
.m_buffer_pos
;
109 m_buffer_size
= buffer
.m_buffer_size
;
110 m_fixed
= buffer
.m_fixed
;
111 m_flushable
= buffer
.m_flushable
;
112 m_stream
= buffer
.m_stream
;
113 m_mode
= buffer
.m_mode
;
114 m_destroybuf
= false;
117 void wxStreamBuffer::FreeBuffer()
121 free(m_buffer_start
);
122 m_buffer_start
= NULL
;
126 wxStreamBuffer::~wxStreamBuffer()
131 wxInputStream
*wxStreamBuffer::GetInputStream() const
133 return m_mode
== write
? NULL
: (wxInputStream
*)m_stream
;
136 wxOutputStream
*wxStreamBuffer::GetOutputStream() const
138 return m_mode
== read
? NULL
: (wxOutputStream
*)m_stream
;
141 void wxStreamBuffer::SetBufferIO(void *buffer_start
,
145 SetBufferIO(buffer_start
, (char *)buffer_end
- (char *)buffer_start
,
149 void wxStreamBuffer::SetBufferIO(void *start
,
153 // start by freeing the old buffer
156 m_buffer_start
= (char *)start
;
157 m_buffer_end
= m_buffer_start
+ len
;
161 // if we own it, we free it
162 m_destroybuf
= takeOwnership
;
167 void wxStreamBuffer::SetBufferIO(size_t bufsize
)
171 // this will free the old buffer and allocate the new one
172 SetBufferIO(malloc(bufsize
), bufsize
, true /* take ownership */);
174 else // no buffer size => no buffer
176 // still free the old one
182 void wxStreamBuffer::ResetBuffer()
187 m_stream
->m_lastcount
= 0;
190 m_buffer_pos
= m_mode
== read
&& m_flushable
195 // fill the buffer with as much data as possible (only for read buffers)
196 bool wxStreamBuffer::FillBuffer()
198 wxInputStream
*inStream
= GetInputStream();
200 // It's legal to have no stream, so we don't complain about it just return false
204 size_t count
= inStream
->OnSysRead(m_buffer_start
, m_buffer_size
);
208 m_buffer_end
= m_buffer_start
+ count
;
209 m_buffer_pos
= m_buffer_start
;
214 // write the buffer contents to the stream (only for write buffers)
215 bool wxStreamBuffer::FlushBuffer()
217 wxCHECK_MSG( m_flushable
, false, _T("can't flush this buffer") );
219 // FIXME: what is this check for? (VZ)
220 if ( m_buffer_pos
== m_buffer_start
)
223 wxOutputStream
*outStream
= GetOutputStream();
225 wxCHECK_MSG( outStream
, false, _T("should have a stream in wxStreamBuffer") );
227 size_t current
= m_buffer_pos
- m_buffer_start
;
228 size_t count
= outStream
->OnSysWrite(m_buffer_start
, current
);
229 if ( count
!= current
)
232 m_buffer_pos
= m_buffer_start
;
237 size_t wxStreamBuffer::GetDataLeft()
239 /* Why is this done? RR. */
240 if ( m_buffer_pos
== m_buffer_end
&& m_flushable
)
243 return GetBytesLeft();
246 // copy up to size bytes from our buffer into the provided one
247 void wxStreamBuffer::GetFromBuffer(void *buffer
, size_t size
)
249 // don't get more bytes than left in the buffer
250 size_t left
= GetBytesLeft();
255 memcpy(buffer
, m_buffer_pos
, size
);
256 m_buffer_pos
+= size
;
259 // copy the contents of the provided buffer into this one
260 void wxStreamBuffer::PutToBuffer(const void *buffer
, size_t size
)
262 size_t left
= GetBytesLeft();
268 // we can't realloc the buffer, so just copy what we can
273 // realloc the buffer to have enough space for the data
274 size_t delta
= m_buffer_pos
- m_buffer_start
;
276 char *startOld
= m_buffer_start
;
277 m_buffer_size
+= size
;
278 m_buffer_start
= (char *)realloc(m_buffer_start
, m_buffer_size
);
279 if ( !m_buffer_start
)
281 // don't leak memory if realloc() failed
282 m_buffer_start
= startOld
;
283 m_buffer_size
-= size
;
285 // what else can we do?
289 // adjust the pointers invalidated by realloc()
290 m_buffer_pos
= m_buffer_start
+ delta
;
291 m_buffer_end
= m_buffer_start
+ m_buffer_size
;
295 memcpy(m_buffer_pos
, buffer
, size
);
296 m_buffer_pos
+= size
;
299 void wxStreamBuffer::PutChar(char c
)
301 wxOutputStream
*outStream
= GetOutputStream();
303 wxCHECK_RET( outStream
, _T("should have a stream in wxStreamBuffer") );
305 // if we don't have buffer at all, just forward this call to the stream,
308 outStream
->OnSysWrite(&c
, sizeof(c
));
312 // otherwise check we have enough space left
313 if ( !GetDataLeft() && !FlushBuffer() )
316 SetError(wxSTREAM_WRITE_ERROR
);
320 PutToBuffer(&c
, sizeof(c
));
321 m_stream
->m_lastcount
= 1;
326 char wxStreamBuffer::Peek()
328 wxCHECK_MSG( m_stream
&& HasBuffer(), 0,
329 _T("should have the stream and the buffer in wxStreamBuffer") );
331 if ( !GetDataLeft() )
333 SetError(wxSTREAM_READ_ERROR
);
338 GetFromBuffer(&c
, sizeof(c
));
344 char wxStreamBuffer::GetChar()
346 wxInputStream
*inStream
= GetInputStream();
348 wxCHECK_MSG( inStream
, 0, _T("should have a stream in wxStreamBuffer") );
353 inStream
->OnSysRead(&c
, sizeof(c
));
357 if ( !GetDataLeft() )
359 SetError(wxSTREAM_READ_ERROR
);
364 GetFromBuffer(&c
, sizeof(c
));
365 m_stream
->m_lastcount
= 1;
372 size_t wxStreamBuffer::Read(void *buffer
, size_t size
)
374 wxASSERT_MSG( buffer
, _T("Warning: Null pointer is about to be used") );
376 /* Clear buffer first */
377 memset(buffer
, 0x00, size
);
379 // lasterror is reset before all new IO calls
386 wxInputStream
*inStream
= GetInputStream();
388 wxCHECK_MSG( inStream
, 0, _T("should have a stream in wxStreamBuffer") );
390 readBytes
= inStream
->OnSysRead(buffer
, size
);
392 else // we have a buffer, use it
394 size_t orig_size
= size
;
398 size_t left
= GetDataLeft();
400 // if the requested number of bytes if greater than the buffer
401 // size, read data in chunks
404 GetFromBuffer(buffer
, left
);
406 buffer
= (char *)buffer
+ left
;
410 SetError(wxSTREAM_EOF
);
414 else // otherwise just do it in one gulp
416 GetFromBuffer(buffer
, size
);
421 readBytes
= orig_size
- size
;
425 m_stream
->m_lastcount
= readBytes
;
430 // this should really be called "Copy()"
431 size_t wxStreamBuffer::Read(wxStreamBuffer
*dbuf
)
433 wxCHECK_MSG( m_mode
!= write
, 0, _T("can't read from this buffer") );
435 char buf
[BUF_TEMP_SIZE
];
441 nRead
= Read(buf
, WXSIZEOF(buf
));
444 nRead
= dbuf
->Write(buf
, nRead
);
453 size_t wxStreamBuffer::Write(const void *buffer
, size_t size
)
455 wxASSERT_MSG( buffer
, _T("Warning: Null pointer is about to be send") );
459 // lasterror is reset before all new IO calls
465 if ( !HasBuffer() && m_fixed
)
467 wxOutputStream
*outStream
= GetOutputStream();
469 wxCHECK_MSG( outStream
, 0, _T("should have a stream in wxStreamBuffer") );
471 // no buffer, just forward the call to the stream
472 ret
= outStream
->OnSysWrite(buffer
, size
);
474 else // we [may] have a buffer, use it
476 size_t orig_size
= size
;
480 size_t left
= GetBytesLeft();
482 // if the buffer is too large to fit in the stream buffer, split
483 // it in smaller parts
485 // NB: If stream buffer isn't fixed (as for wxMemoryOutputStream),
486 // we always go to the second case.
488 // FIXME: fine, but if it fails we should (re)try writing it by
489 // chunks as this will (hopefully) always work (VZ)
491 if ( size
> left
&& m_fixed
)
493 PutToBuffer(buffer
, left
);
495 buffer
= (char *)buffer
+ left
;
497 if ( !FlushBuffer() )
499 SetError(wxSTREAM_WRITE_ERROR
);
504 m_buffer_pos
= m_buffer_start
;
506 else // we can do it in one gulp
508 PutToBuffer(buffer
, size
);
513 ret
= orig_size
- size
;
518 // i am not entirely sure what we do this for
519 m_stream
->m_lastcount
= ret
;
525 size_t wxStreamBuffer::Write(wxStreamBuffer
*sbuf
)
527 wxCHECK_MSG( m_mode
!= read
, 0, _T("can't write to this buffer") );
528 wxCHECK_MSG( sbuf
->m_mode
!= write
, 0, _T("can't read from that buffer") );
530 char buf
[BUF_TEMP_SIZE
];
536 size_t nRead
= sbuf
->Read(buf
, WXSIZEOF(buf
));
539 nWrite
= Write(buf
, nRead
);
540 if ( nWrite
< nRead
)
542 // put back data we couldn't copy
543 wxInputStream
*in_stream
= (wxInputStream
*)sbuf
->GetStream();
545 in_stream
->Ungetch(buf
+ nWrite
, nRead
- nWrite
);
555 while ( nWrite
== WXSIZEOF(buf
) );
560 wxFileOffset
wxStreamBuffer::Seek(wxFileOffset pos
, wxSeekMode mode
)
562 wxFileOffset ret_off
, diff
;
564 wxFileOffset last_access
= GetLastAccess();
575 diff
= pos
+ GetIntPosition();
579 diff
= pos
+ last_access
;
583 wxFAIL_MSG( _T("invalid seek mode") );
585 return wxInvalidOffset
;
587 if (diff
< 0 || diff
> last_access
)
588 return wxInvalidOffset
;
589 size_t int_diff
= wx_truncate_cast(size_t, diff
);
590 wxCHECK_MSG( (wxFileOffset
)int_diff
== diff
, wxInvalidOffset
, wxT("huge file not supported") );
591 SetIntPosition(int_diff
);
598 // We'll try to compute an internal position later ...
599 ret_off
= m_stream
->OnSysSeek(pos
, wxFromStart
);
604 diff
= pos
+ GetIntPosition();
606 if ( (diff
> last_access
) || (diff
< 0) )
608 // We must take into account the fact that we have read
609 // something previously.
610 ret_off
= m_stream
->OnSysSeek(diff
-last_access
, wxFromCurrent
);
616 size_t int_diff
= wx_truncate_cast(size_t, diff
);
617 wxCHECK_MSG( (wxFileOffset
)int_diff
== diff
, wxInvalidOffset
, wxT("huge file not supported") );
618 SetIntPosition(int_diff
);
623 // Hard to compute: always seek to the requested position.
624 ret_off
= m_stream
->OnSysSeek(pos
, wxFromEnd
);
629 return wxInvalidOffset
;
632 wxFileOffset
wxStreamBuffer::Tell() const
636 // ask the stream for position if we have a real one
639 pos
= m_stream
->OnSysTell();
640 if ( pos
== wxInvalidOffset
)
641 return wxInvalidOffset
;
643 else // no associated stream
648 pos
+= GetIntPosition();
650 if ( m_mode
== read
&& m_flushable
)
651 pos
-= GetLastAccess();
656 // ----------------------------------------------------------------------------
658 // ----------------------------------------------------------------------------
660 wxStreamBase::wxStreamBase()
662 m_lasterror
= wxSTREAM_NO_ERROR
;
666 wxStreamBase::~wxStreamBase()
670 size_t wxStreamBase::GetSize() const
672 wxFileOffset length
= GetLength();
673 if ( length
== (wxFileOffset
)wxInvalidOffset
)
676 const size_t len
= wx_truncate_cast(size_t, length
);
677 wxASSERT_MSG( len
== length
+ size_t(0), _T("large files not supported") );
682 wxFileOffset
wxStreamBase::OnSysSeek(wxFileOffset
WXUNUSED(seek
), wxSeekMode
WXUNUSED(mode
))
684 return wxInvalidOffset
;
687 wxFileOffset
wxStreamBase::OnSysTell() const
689 return wxInvalidOffset
;
692 // ----------------------------------------------------------------------------
694 // ----------------------------------------------------------------------------
696 wxInputStream::wxInputStream()
703 wxInputStream::~wxInputStream()
708 bool wxInputStream::CanRead() const
710 // we don't know if there is anything to read or not and by default we
711 // prefer to be optimistic and try to read data unless we know for sure
712 // there is no more of it
713 return m_lasterror
!= wxSTREAM_EOF
;
716 bool wxInputStream::Eof() const
718 // the only way the base class can know we're at EOF is when we'd already
719 // tried to read beyond it in which case last error is set accordingly
720 return GetLastError() == wxSTREAM_EOF
;
723 char *wxInputStream::AllocSpaceWBack(size_t needed_size
)
725 // get number of bytes left from previous wback buffer
726 size_t toget
= m_wbacksize
- m_wbackcur
;
728 // allocate a buffer large enough to hold prev + new data
729 char *temp_b
= (char *)malloc(needed_size
+ toget
);
734 // copy previous data (and free old buffer) if needed
737 memmove(temp_b
+ needed_size
, m_wback
+ m_wbackcur
, toget
);
744 m_wbacksize
= needed_size
+ toget
;
749 size_t wxInputStream::GetWBack(void *buf
, size_t size
)
751 wxASSERT_MSG( buf
, _T("Warning: Null pointer is about to be used") );
753 /* Clear buffer first */
754 memset(buf
, 0x00, size
);
759 // how many bytes do we have in the buffer?
760 size_t toget
= m_wbacksize
- m_wbackcur
;
764 // we won't read everything
768 // copy the data from the cache
769 memcpy(buf
, m_wback
+ m_wbackcur
, toget
);
772 if ( m_wbackcur
== m_wbacksize
)
774 // TODO: should we really free it here all the time? maybe keep it?
781 // return the number of bytes copied
785 size_t wxInputStream::Ungetch(const void *buf
, size_t bufsize
)
787 wxASSERT_MSG( buf
, _T("Warning: Null pointer is about to be used in Ungetch()") );
789 if ( m_lasterror
!= wxSTREAM_NO_ERROR
&& m_lasterror
!= wxSTREAM_EOF
)
791 // can't operate on this stream until the error is cleared
795 char *ptrback
= AllocSpaceWBack(bufsize
);
799 // Eof() shouldn't return true any longer
800 if ( m_lasterror
== wxSTREAM_EOF
)
801 m_lasterror
= wxSTREAM_NO_ERROR
;
803 memcpy(ptrback
, buf
, bufsize
);
807 bool wxInputStream::Ungetch(char c
)
809 return Ungetch(&c
, sizeof(c
)) != 0;
812 int wxInputStream::GetC()
816 return LastRead() ? c
: wxEOF
;
819 wxInputStream
& wxInputStream::Read(void *buf
, size_t size
)
821 wxASSERT_MSG( buf
, _T("Warning: Null pointer is about to be read") );
823 char *p
= (char *)buf
;
826 size_t read
= GetWBack(buf
, size
);
835 // we read the requested amount of data
839 if ( p
!= buf
&& !CanRead() )
841 // we have already read something and we would block in OnSysRead()
842 // now: don't do it but return immediately
846 read
= OnSysRead(p
, size
);
849 // no more data available
857 char wxInputStream::Peek()
861 if (m_lasterror
== wxSTREAM_NO_ERROR
)
870 wxInputStream
& wxInputStream::Read(wxOutputStream
& stream_out
)
872 size_t lastcount
= 0;
873 char buf
[BUF_TEMP_SIZE
];
877 size_t bytes_read
= Read(buf
, WXSIZEOF(buf
)).LastRead();
881 if ( stream_out
.Write(buf
, bytes_read
).LastWrite() != bytes_read
)
884 lastcount
+= bytes_read
;
887 m_lastcount
= lastcount
;
892 wxFileOffset
wxInputStream::SeekI(wxFileOffset pos
, wxSeekMode mode
)
894 // RR: This code is duplicated in wxBufferedInputStream. This is
895 // not really a good design, but buffered stream are different
896 // from all other in that they handle two stream-related objects,
897 // the stream buffer and parent stream.
899 // I don't know whether it should be put as well in wxFileInputStream::OnSysSeek
900 if (m_lasterror
==wxSTREAM_EOF
)
901 m_lasterror
=wxSTREAM_NO_ERROR
;
903 /* RR: A call to SeekI() will automatically invalidate any previous
904 call to Ungetch(), otherwise it would be possible to SeekI() to
905 one position, unread some bytes there, SeekI() to another position
906 and the data would be corrupted.
908 GRG: Could add code here to try to navigate within the wback
909 buffer if possible, but is it really needed? It would only work
910 when seeking in wxFromCurrent mode, else it would invalidate
915 wxLogDebug( wxT("Seeking in stream which has data written back to it.") );
923 return OnSysSeek(pos
, mode
);
926 wxFileOffset
wxInputStream::TellI() const
928 wxFileOffset pos
= OnSysTell();
930 if (pos
!= wxInvalidOffset
)
931 pos
-= (m_wbacksize
- m_wbackcur
);
937 // ----------------------------------------------------------------------------
939 // ----------------------------------------------------------------------------
941 wxOutputStream::wxOutputStream()
945 wxOutputStream::~wxOutputStream()
949 size_t wxOutputStream::OnSysWrite(const void * WXUNUSED(buffer
),
950 size_t WXUNUSED(bufsize
))
955 void wxOutputStream::PutC(char c
)
957 Write(&c
, sizeof(c
));
960 wxOutputStream
& wxOutputStream::Write(const void *buffer
, size_t size
)
962 m_lastcount
= OnSysWrite(buffer
, size
);
966 wxOutputStream
& wxOutputStream::Write(wxInputStream
& stream_in
)
968 stream_in
.Read(*this);
972 wxFileOffset
wxOutputStream::TellO() const
977 wxFileOffset
wxOutputStream::SeekO(wxFileOffset pos
, wxSeekMode mode
)
979 return OnSysSeek(pos
, mode
);
982 void wxOutputStream::Sync()
987 // ----------------------------------------------------------------------------
988 // wxCountingOutputStream
989 // ----------------------------------------------------------------------------
991 wxCountingOutputStream::wxCountingOutputStream ()
996 wxFileOffset
wxCountingOutputStream::GetLength() const
1001 size_t wxCountingOutputStream::OnSysWrite(const void *WXUNUSED(buffer
),
1004 m_currentPos
+= size
;
1005 if (m_currentPos
> m_lastcount
)
1006 m_lastcount
= m_currentPos
;
1008 return m_currentPos
;
1011 wxFileOffset
wxCountingOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
1013 ssize_t new_pos
= wx_truncate_cast(ssize_t
, pos
);
1018 wxCHECK_MSG( (wxFileOffset
)new_pos
== pos
, wxInvalidOffset
, wxT("huge position not supported") );
1022 new_pos
= m_lastcount
+ new_pos
;
1023 wxCHECK_MSG( (wxFileOffset
)new_pos
== (wxFileOffset
)(m_lastcount
+ pos
), wxInvalidOffset
, wxT("huge position not supported") );
1027 new_pos
= m_currentPos
+ new_pos
;
1028 wxCHECK_MSG( (wxFileOffset
)new_pos
== (wxFileOffset
)(m_currentPos
+ pos
), wxInvalidOffset
, wxT("huge position not supported") );
1032 wxFAIL_MSG( _T("invalid seek mode") );
1033 return wxInvalidOffset
;
1036 m_currentPos
= new_pos
;
1038 if (m_currentPos
> m_lastcount
)
1039 m_lastcount
= m_currentPos
;
1041 return m_currentPos
;
1044 wxFileOffset
wxCountingOutputStream::OnSysTell() const
1046 return m_currentPos
;
1049 // ----------------------------------------------------------------------------
1050 // wxFilterInputStream
1051 // ----------------------------------------------------------------------------
1053 wxFilterInputStream::wxFilterInputStream()
1054 : m_parent_i_stream(NULL
),
1059 wxFilterInputStream::wxFilterInputStream(wxInputStream
& stream
)
1060 : m_parent_i_stream(&stream
),
1065 wxFilterInputStream::wxFilterInputStream(wxInputStream
*stream
)
1066 : m_parent_i_stream(stream
),
1071 wxFilterInputStream::~wxFilterInputStream()
1074 delete m_parent_i_stream
;
1077 // ----------------------------------------------------------------------------
1078 // wxFilterOutputStream
1079 // ----------------------------------------------------------------------------
1081 wxFilterOutputStream::wxFilterOutputStream()
1082 : m_parent_o_stream(NULL
),
1087 wxFilterOutputStream::wxFilterOutputStream(wxOutputStream
& stream
)
1088 : m_parent_o_stream(&stream
),
1093 wxFilterOutputStream::wxFilterOutputStream(wxOutputStream
*stream
)
1094 : m_parent_o_stream(stream
),
1099 bool wxFilterOutputStream::Close()
1101 if (m_parent_o_stream
&& m_owns
)
1102 return m_parent_o_stream
->Close();
1107 wxFilterOutputStream::~wxFilterOutputStream()
1110 delete m_parent_o_stream
;
1113 // ----------------------------------------------------------------------------
1114 // wxFilterClassFactoryBase
1115 // ----------------------------------------------------------------------------
1117 IMPLEMENT_ABSTRACT_CLASS(wxFilterClassFactoryBase
, wxObject
)
1119 wxString
wxFilterClassFactoryBase::PopExtension(const wxString
& location
) const
1121 return location
.substr(0, FindExtension(location
));
1124 wxString::size_type
wxFilterClassFactoryBase::FindExtension(
1125 const wxChar
*location
) const
1127 size_t len
= wxStrlen(location
);
1129 for (const wxChar
*const *p
= GetProtocols(wxSTREAM_FILEEXT
); *p
; p
++)
1131 size_t l
= wxStrlen(*p
);
1133 if (l
<= len
&& wxStrcmp(*p
, location
+ len
- l
) == 0)
1137 return wxString::npos
;
1140 bool wxFilterClassFactoryBase::CanHandle(const wxChar
*protocol
,
1141 wxStreamProtocolType type
) const
1143 if (type
== wxSTREAM_FILEEXT
)
1144 return FindExtension(protocol
) != wxString::npos
;
1146 for (const wxChar
*const *p
= GetProtocols(type
); *p
; p
++)
1147 if (wxStrcmp(*p
, protocol
) == 0)
1153 // ----------------------------------------------------------------------------
1154 // wxFilterClassFactory
1155 // ----------------------------------------------------------------------------
1157 IMPLEMENT_ABSTRACT_CLASS(wxFilterClassFactory
, wxFilterClassFactoryBase
)
1159 wxFilterClassFactory
*wxFilterClassFactory::sm_first
= NULL
;
1161 void wxFilterClassFactory::Remove()
1165 wxFilterClassFactory
**pp
= &sm_first
;
1168 pp
= &(*pp
)->m_next
;
1176 // ----------------------------------------------------------------------------
1177 // wxBufferedInputStream
1178 // ----------------------------------------------------------------------------
1180 wxBufferedInputStream::wxBufferedInputStream(wxInputStream
& s
,
1181 wxStreamBuffer
*buffer
)
1182 : wxFilterInputStream(s
)
1186 // use the buffer provided by the user
1187 m_i_streambuf
= buffer
;
1189 else // create a default buffer
1191 m_i_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::read
);
1193 m_i_streambuf
->SetBufferIO(1024);
1197 wxBufferedInputStream::~wxBufferedInputStream()
1199 m_parent_i_stream
->SeekI(-(wxFileOffset
)m_i_streambuf
->GetBytesLeft(),
1202 delete m_i_streambuf
;
1205 char wxBufferedInputStream::Peek()
1207 return m_i_streambuf
->Peek();
1210 wxInputStream
& wxBufferedInputStream::Read(void *buf
, size_t size
)
1212 // reset the error flag
1215 // first read from the already cached data
1216 m_lastcount
= GetWBack(buf
, size
);
1218 // do we have to read anything more?
1219 if ( m_lastcount
< size
)
1221 size
-= m_lastcount
;
1222 buf
= (char *)buf
+ m_lastcount
;
1224 // the call to wxStreamBuffer::Read() below will reset our m_lastcount,
1226 size_t countOld
= m_lastcount
;
1228 m_i_streambuf
->Read(buf
, size
);
1230 m_lastcount
+= countOld
;
1236 wxFileOffset
wxBufferedInputStream::SeekI(wxFileOffset pos
, wxSeekMode mode
)
1238 // RR: Look at wxInputStream for comments.
1240 if (m_lasterror
==wxSTREAM_EOF
)
1245 wxLogDebug( wxT("Seeking in stream which has data written back to it.") );
1253 return m_i_streambuf
->Seek(pos
, mode
);
1256 wxFileOffset
wxBufferedInputStream::TellI() const
1258 wxFileOffset pos
= m_i_streambuf
->Tell();
1260 if (pos
!= wxInvalidOffset
)
1261 pos
-= (m_wbacksize
- m_wbackcur
);
1266 size_t wxBufferedInputStream::OnSysRead(void *buffer
, size_t bufsize
)
1268 return m_parent_i_stream
->Read(buffer
, bufsize
).LastRead();
1271 wxFileOffset
wxBufferedInputStream::OnSysSeek(wxFileOffset seek
, wxSeekMode mode
)
1273 return m_parent_i_stream
->SeekI(seek
, mode
);
1276 wxFileOffset
wxBufferedInputStream::OnSysTell() const
1278 return m_parent_i_stream
->TellI();
1281 void wxBufferedInputStream::SetInputStreamBuffer(wxStreamBuffer
*buffer
)
1283 wxCHECK_RET( buffer
, _T("wxBufferedInputStream needs buffer") );
1285 delete m_i_streambuf
;
1286 m_i_streambuf
= buffer
;
1289 // ----------------------------------------------------------------------------
1290 // wxBufferedOutputStream
1291 // ----------------------------------------------------------------------------
1293 wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream
& s
,
1294 wxStreamBuffer
*buffer
)
1295 : wxFilterOutputStream(s
)
1299 m_o_streambuf
= buffer
;
1301 else // create a default one
1303 m_o_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::write
);
1305 m_o_streambuf
->SetBufferIO(1024);
1309 wxBufferedOutputStream::~wxBufferedOutputStream()
1312 delete m_o_streambuf
;
1315 bool wxBufferedOutputStream::Close()
1322 wxOutputStream
& wxBufferedOutputStream::Write(const void *buffer
, size_t size
)
1325 m_o_streambuf
->Write(buffer
, size
);
1329 wxFileOffset
wxBufferedOutputStream::SeekO(wxFileOffset pos
, wxSeekMode mode
)
1332 return m_o_streambuf
->Seek(pos
, mode
);
1335 wxFileOffset
wxBufferedOutputStream::TellO() const
1337 return m_o_streambuf
->Tell();
1340 void wxBufferedOutputStream::Sync()
1342 m_o_streambuf
->FlushBuffer();
1343 m_parent_o_stream
->Sync();
1346 size_t wxBufferedOutputStream::OnSysWrite(const void *buffer
, size_t bufsize
)
1348 return m_parent_o_stream
->Write(buffer
, bufsize
).LastWrite();
1351 wxFileOffset
wxBufferedOutputStream::OnSysSeek(wxFileOffset seek
, wxSeekMode mode
)
1353 return m_parent_o_stream
->SeekO(seek
, mode
);
1356 wxFileOffset
wxBufferedOutputStream::OnSysTell() const
1358 return m_parent_o_stream
->TellO();
1361 wxFileOffset
wxBufferedOutputStream::GetLength() const
1363 return m_parent_o_stream
->GetLength() + m_o_streambuf
->GetIntPosition();
1366 void wxBufferedOutputStream::SetOutputStreamBuffer(wxStreamBuffer
*buffer
)
1368 wxCHECK_RET( buffer
, _T("wxBufferedOutputStream needs buffer") );
1370 delete m_o_streambuf
;
1371 m_o_streambuf
= buffer
;
1374 // ----------------------------------------------------------------------------
1375 // Some IOManip function
1376 // ----------------------------------------------------------------------------
1378 wxOutputStream
& wxEndL(wxOutputStream
& stream
)
1380 static const wxChar
*eol
= wxTextFile::GetEOL();
1382 return stream
.Write(eol
, wxStrlen(eol
));
1385 #endif // wxUSE_STREAMS