]>
git.saurik.com Git - wxWidgets.git/blob - src/common/stream.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxStream base classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "stream.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #include <wx/stream.h>
20 #include <wx/datstrm.h>
21 #include <wx/objstrm.h>
27 #define BUF_TEMP_SIZE 10000
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 #define CHECK_ERROR(err) \
34 if (m_stream->m_lasterror == wxStream_NOERROR) \
35 m_stream->m_lasterror = err
37 wxStreamBuffer::wxStreamBuffer(wxStreamBase
& stream
, BufMode mode
)
38 : m_buffer_start(NULL
), m_buffer_end(NULL
), m_buffer_pos(NULL
),
39 m_buffer_size(0), m_wback(NULL
), m_wbacksize(0), m_wbackcur(0),
40 m_fixed(TRUE
), m_flushable(TRUE
), m_stream(&stream
),
41 m_mode(mode
), m_destroybuf(FALSE
), m_destroystream(FALSE
)
45 wxStreamBuffer::wxStreamBuffer(BufMode mode
)
46 : m_buffer_start(NULL
), m_buffer_end(NULL
), m_buffer_pos(NULL
),
47 m_buffer_size(0), m_wback(NULL
), m_wbacksize(0), m_wbackcur(0),
48 m_fixed(TRUE
), m_flushable(FALSE
), m_stream(NULL
),
49 m_mode(mode
), m_destroybuf(FALSE
), m_destroystream(TRUE
)
51 m_stream
= new wxStreamBase();
54 wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer
& buffer
)
56 m_buffer_start
= buffer
.m_buffer_start
;
57 m_buffer_end
= buffer
.m_buffer_end
;
58 m_buffer_pos
= buffer
.m_buffer_pos
;
59 m_buffer_size
= buffer
.m_buffer_size
;
60 m_fixed
= buffer
.m_fixed
;
61 m_flushable
= buffer
.m_flushable
;
62 m_stream
= buffer
.m_stream
;
63 m_mode
= buffer
.m_mode
;
65 m_destroystream
= FALSE
;
71 wxStreamBuffer::~wxStreamBuffer()
76 wxDELETEA(m_buffer_start
);
81 size_t wxStreamBuffer::WriteBack(const char *buf
, size_t bufsize
)
88 ptrback
= AllocSpaceWBack(bufsize
);
92 memcpy(ptrback
, buf
, bufsize
);
96 bool wxStreamBuffer::WriteBack(char c
)
100 ptrback
= AllocSpaceWBack(1);
108 void wxStreamBuffer::SetBufferIO(char *buffer_start
, char *buffer_end
)
111 wxDELETEA(m_buffer_start
);
112 m_buffer_start
= buffer_start
;
113 m_buffer_end
= buffer_end
;
115 m_buffer_size
= m_buffer_end
-m_buffer_start
;
116 m_destroybuf
= FALSE
;
120 void wxStreamBuffer::SetBufferIO(size_t bufsize
)
125 wxDELETEA(m_buffer_start
);
128 m_buffer_start
= NULL
;
135 b_start
= new char[bufsize
];
136 SetBufferIO(b_start
, b_start
+ bufsize
);
140 void wxStreamBuffer::ResetBuffer()
142 m_stream
->m_lasterror
= wxStream_NOERROR
;
143 m_stream
->m_lastcount
= 0;
145 m_buffer_pos
= m_buffer_end
;
147 m_buffer_pos
= m_buffer_start
;
150 char *wxStreamBuffer::AllocSpaceWBack(size_t needed_size
)
154 m_wbacksize
+= needed_size
;
157 temp_b
= (char *)malloc(m_wbacksize
);
159 temp_b
= (char *)realloc(m_wback
, m_wbacksize
);
165 return (char *)(m_wback
+(m_wbacksize
-needed_size
));
168 size_t wxStreamBuffer::GetWBack(char *buf
, size_t bsize
)
170 size_t s_toget
= m_wbacksize
-m_wbackcur
;
175 memcpy(buf
, (m_wback
+m_wbackcur
), s_toget
);
177 m_wbackcur
+= s_toget
;
178 if (m_wbackcur
== m_wbacksize
) {
180 m_wback
= (char *)NULL
;
188 bool wxStreamBuffer::FillBuffer()
192 count
= m_stream
->OnSysRead(m_buffer_start
, m_buffer_size
);
193 m_buffer_end
= m_buffer_start
+count
;
194 m_buffer_pos
= m_buffer_start
;
201 bool wxStreamBuffer::FlushBuffer()
203 size_t count
, current
;
205 if (m_buffer_pos
== m_buffer_start
|| !m_flushable
)
208 current
= m_buffer_pos
-m_buffer_start
;
209 count
= m_stream
->OnSysWrite(m_buffer_start
, current
);
210 if (count
!= current
)
212 m_buffer_pos
= m_buffer_start
;
217 void wxStreamBuffer::GetFromBuffer(void *buffer
, size_t size
)
219 size_t s_toget
= m_buffer_end
-m_buffer_pos
;
224 memcpy(buffer
, m_buffer_pos
, s_toget
);
225 m_buffer_pos
+= s_toget
;
228 void wxStreamBuffer::PutToBuffer(const void *buffer
, size_t size
)
230 size_t s_toput
= m_buffer_end
-m_buffer_pos
;
232 if (s_toput
< size
&& !m_fixed
) {
233 m_buffer_start
= (char *)realloc(m_buffer_start
, m_buffer_size
+size
);
235 m_buffer_size
+= size
;
236 m_buffer_end
= m_buffer_start
+m_buffer_size
;
241 memcpy(m_buffer_pos
, buffer
, s_toput
);
242 m_buffer_pos
+= s_toput
;
245 void wxStreamBuffer::PutChar(char c
)
247 wxASSERT(m_stream
!= NULL
);
249 if (!m_buffer_size
) {
250 m_stream
->OnSysWrite(&c
, 1);
254 if (!GetDataLeft() && !FlushBuffer()) {
255 CHECK_ERROR(wxStream_READ_ERR
);
260 m_stream
->m_lastcount
= 1;
263 char wxStreamBuffer::GetChar()
267 wxASSERT(m_stream
!= NULL
);
269 if (!m_buffer_size
) {
270 m_stream
->OnSysRead(&c
, 1);
274 if (!GetDataLeft()) {
275 CHECK_ERROR(wxStream_READ_ERR
);
279 GetFromBuffer(&c
, 1);
280 m_stream
->m_lastcount
= 1;
284 size_t wxStreamBuffer::Read(void *buffer
, size_t size
)
286 wxASSERT(m_stream
!= NULL
);
288 // ------------------
289 // Buffering disabled
290 // ------------------
292 m_stream
->m_lasterror
= wxStream_NOERROR
;
293 m_stream
->m_lastcount
= GetWBack((char *)buffer
, size
);
294 size
-= m_stream
->m_lastcount
;
296 return m_stream
->m_lastcount
;
298 buffer
= (void *)((char *)buffer
+m_stream
->m_lastcount
);
301 return (m_stream
->m_lastcount
+= m_stream
->OnSysRead(buffer
, size
));
306 size_t buf_left
, orig_size
= size
;
309 buf_left
= GetDataLeft();
311 // First case: the requested buffer is larger than the stream buffer,
313 if (size
> buf_left
) {
314 GetFromBuffer(buffer
, buf_left
);
316 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
319 CHECK_ERROR(wxStream_READ_ERR
);
320 return (m_stream
->m_lastcount
= orig_size
-size
);
324 // Second case: we just copy from the stream buffer.
325 GetFromBuffer(buffer
, size
);
329 return (m_stream
->m_lastcount
+= orig_size
);
332 size_t wxStreamBuffer::Read(wxStreamBuffer
*s_buf
)
334 char buf
[BUF_TEMP_SIZE
];
335 size_t s
= 0, bytes_read
= BUF_TEMP_SIZE
;
337 while (bytes_read
== BUF_TEMP_SIZE
) {
338 bytes_read
= Read(buf
, bytes_read
);
339 bytes_read
= s_buf
->Write(buf
, bytes_read
);
345 size_t wxStreamBuffer::Write(const void *buffer
, size_t size
)
347 wxASSERT(m_stream
!= NULL
);
349 // ------------------
350 // Buffering disabled
351 // ------------------
353 m_stream
->m_lasterror
= wxStream_NOERROR
;
355 return (m_stream
->m_lastcount
= m_stream
->OnSysWrite(buffer
, size
));
357 // ------------------
359 // ------------------
361 size_t buf_left
, orig_size
= size
;
364 buf_left
= m_buffer_end
- m_buffer_pos
;
366 // First case: the buffer to write is larger than the stream buffer,
368 if (size
> buf_left
) {
369 PutToBuffer(buffer
, buf_left
);
371 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
373 if (!FlushBuffer()) {
374 CHECK_ERROR(wxStream_WRITE_ERR
);
375 return (m_stream
->m_lastcount
= orig_size
-size
);
378 m_buffer_pos
= m_buffer_start
;
382 // Second case: just copy it in the stream buffer.
383 PutToBuffer(buffer
, size
);
387 return (m_stream
->m_lastcount
= orig_size
);
390 size_t wxStreamBuffer::Write(wxStreamBuffer
*sbuf
)
392 char buf
[BUF_TEMP_SIZE
];
393 size_t s
= 0, bytes_count
= BUF_TEMP_SIZE
, b_count2
;
395 while (bytes_count
== BUF_TEMP_SIZE
) {
396 b_count2
= sbuf
->Read(buf
, bytes_count
);
397 bytes_count
= Write(buf
, b_count2
);
398 if (b_count2
> bytes_count
)
399 sbuf
->WriteBack(buf
+bytes_count
, b_count2
-bytes_count
);
405 off_t
wxStreamBuffer::Seek(off_t pos
, wxSeekMode mode
)
407 off_t ret_off
, diff
, last_access
;
409 last_access
= GetLastAccess();
412 diff
= pos
+ GetIntPosition();
413 if (diff
< 0 || diff
> last_access
)
414 return wxInvalidOffset
;
415 SetIntPosition(diff
);
421 // We'll try to compute an internal position later ...
422 ret_off
= m_stream
->OnSysSeek(pos
, wxFromStart
);
426 case wxFromCurrent
: {
427 diff
= pos
+ GetIntPosition();
429 if ( (diff
> last_access
) || (diff
< 0) ) {
430 ret_off
= m_stream
->OnSysSeek(pos
, wxFromCurrent
);
434 SetIntPosition(diff
);
439 // Hard to compute: always seek to the requested position.
440 ret_off
= m_stream
->OnSysSeek(pos
, wxFromEnd
);
444 return wxInvalidOffset
;
447 off_t
wxStreamBuffer::Tell() const
452 pos
= m_stream
->OnSysTell();
453 if (pos
== wxInvalidOffset
)
454 return wxInvalidOffset
;
455 return pos
- GetLastAccess() + GetIntPosition();
457 return GetIntPosition();
460 size_t wxStreamBuffer::GetDataLeft()
462 if (m_buffer_end
== m_buffer_pos
&& m_flushable
)
464 return m_buffer_end
-m_buffer_pos
;
467 // ----------------------------------------------------------------------------
469 // ----------------------------------------------------------------------------
471 wxStreamBase::wxStreamBase()
473 m_lasterror
= wxStream_NOERROR
;
477 wxStreamBase::~wxStreamBase()
481 size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer
), size_t WXUNUSED(size
))
486 size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer
), size_t WXUNUSED(bufsize
))
491 off_t
wxStreamBase::OnSysSeek(off_t
WXUNUSED(seek
), wxSeekMode
WXUNUSED(mode
))
493 return wxInvalidOffset
;
496 off_t
wxStreamBase::OnSysTell() const
498 return wxInvalidOffset
;
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
505 wxInputStream::wxInputStream()
508 m_i_destroybuf
= TRUE
;
509 m_i_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::read
);
512 wxInputStream::wxInputStream(wxStreamBuffer
*buffer
)
515 m_i_destroybuf
= FALSE
;
516 m_i_streambuf
= buffer
;
519 wxInputStream::~wxInputStream()
522 delete m_i_streambuf
;
525 char wxInputStream::GetC()
528 m_i_streambuf
->Read(&c
, 1);
532 wxInputStream
& wxInputStream::Read(void *buffer
, size_t size
)
534 m_i_streambuf
->Read(buffer
, size
);
535 // wxStreamBuffer sets all variables for us
539 char wxInputStream::Peek()
541 m_i_streambuf
->GetDataLeft();
543 return *(m_i_streambuf
->GetBufferPos());
547 wxInputStream
& wxInputStream::Read(wxOutputStream
& stream_out
)
549 char buf
[BUF_TEMP_SIZE
];
550 size_t bytes_read
= BUF_TEMP_SIZE
;
552 while (bytes_read
== BUF_TEMP_SIZE
) {
553 bytes_read
= Read(buf
, bytes_read
).LastRead();
554 bytes_read
= stream_out
.Write(buf
, bytes_read
).LastWrite();
559 off_t
wxInputStream::SeekI(off_t pos
, wxSeekMode mode
)
561 return m_i_streambuf
->Seek(pos
, mode
);
564 off_t
wxInputStream::TellI() const
566 return m_i_streambuf
->Tell();
569 // --------------------
570 // Overloaded operators
571 // --------------------
573 wxInputStream
& wxInputStream::operator>>(wxString
& line
)
575 wxDataInputStream
s(*this);
581 wxInputStream
& wxInputStream::operator>>(char& c
)
587 wxInputStream
& wxInputStream::operator>>(short& i
)
596 wxInputStream
& wxInputStream::operator>>(int& i
)
605 wxInputStream
& wxInputStream::operator>>(long& i
)
607 /* I only implemented a simple integer parser */
610 while (isspace( c
= GetC() ) )
614 if (! (c
== '-' || isdigit(c
)) ) {
615 InputStreamBuffer()->WriteBack(c
);
635 wxInputStream
& wxInputStream::operator>>(double& f
)
637 /* I only implemented a simple float parser */
640 while (isspace( c
= GetC() ) )
644 if (! (c
== '-' || isdigit(c
)) ) {
645 InputStreamBuffer()->WriteBack(c
);
656 f
= f
*10 + (c
- '0');
661 double f_multiplicator
= (double) 0.1;
665 f
+= (c
-'0')*f_multiplicator
;
666 f_multiplicator
/= 10;
677 wxInputStream
& wxInputStream::operator>>(wxObject
*& obj
)
679 wxObjectInputStream
obj_s(*this);
680 obj
= obj_s
.LoadObject();
686 // ----------------------------------------------------------------------------
688 // ----------------------------------------------------------------------------
689 wxOutputStream::wxOutputStream()
692 m_o_destroybuf
= TRUE
;
693 m_o_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::write
);
696 wxOutputStream::wxOutputStream(wxStreamBuffer
*buffer
)
699 m_o_destroybuf
= FALSE
;
700 m_o_streambuf
= buffer
;
703 wxOutputStream::~wxOutputStream()
706 delete m_o_streambuf
;
709 wxOutputStream
& wxOutputStream::Write(const void *buffer
, size_t size
)
711 m_o_streambuf
->Write(buffer
, size
);
715 wxOutputStream
& wxOutputStream::Write(wxInputStream
& stream_in
)
717 stream_in
.Read(*this);
721 off_t
wxOutputStream::TellO() const
723 return m_o_streambuf
->Tell();
726 off_t
wxOutputStream::SeekO(off_t pos
, wxSeekMode mode
)
728 return m_o_streambuf
->Seek(pos
, mode
);
731 void wxOutputStream::Sync()
733 m_o_streambuf
->FlushBuffer();
736 wxOutputStream
& wxOutputStream::operator<<(const char *string
)
738 return Write(string
, strlen(string
));
741 wxOutputStream
& wxOutputStream::operator<<(wxString
& string
)
743 return Write(string
, string
.Len());
746 wxOutputStream
& wxOutputStream::operator<<(char c
)
751 wxOutputStream
& wxOutputStream::operator<<(short i
)
755 strint
.Printf("%i", i
);
756 return Write(strint
, strint
.Len());
759 wxOutputStream
& wxOutputStream::operator<<(int i
)
763 strint
.Printf("%i", i
);
764 return Write(strint
, strint
.Len());
767 wxOutputStream
& wxOutputStream::operator<<(long i
)
771 strlong
.Printf("%i", i
);
772 return Write((const char *)strlong
, strlong
.Len());
775 wxOutputStream
& wxOutputStream::operator<<(double f
)
779 strfloat
.Printf("%f", f
);
780 return Write(strfloat
, strfloat
.Len());
784 wxOutputStream
& wxOutputStream::operator<<(wxObject
& obj
)
786 wxObjectOutputStream
obj_s(*this);
787 obj_s
.SaveObject(obj
);
792 // ----------------------------------------------------------------------------
793 // wxFilterInputStream
794 // ----------------------------------------------------------------------------
795 wxFilterInputStream::wxFilterInputStream()
796 : wxInputStream(NULL
)
798 // WARNING streambuf set to NULL !
801 wxFilterInputStream::wxFilterInputStream(wxInputStream
& stream
)
802 : wxInputStream(stream
.InputStreamBuffer())
804 m_parent_i_stream
= &stream
;
807 wxFilterInputStream::~wxFilterInputStream()
811 // ----------------------------------------------------------------------------
812 // wxFilterOutputStream
813 // ----------------------------------------------------------------------------
814 wxFilterOutputStream::wxFilterOutputStream()
815 : wxOutputStream(NULL
)
819 wxFilterOutputStream::wxFilterOutputStream(wxOutputStream
& stream
)
820 : wxOutputStream(stream
.OutputStreamBuffer())
822 m_parent_o_stream
= &stream
;
825 wxFilterOutputStream::~wxFilterOutputStream()
829 // ----------------------------------------------------------------------------
830 // Some IOManip function
831 // ----------------------------------------------------------------------------
833 wxOutputStream
& wxEndL(wxOutputStream
& stream
)
836 return stream
.Write("\r\n", 2);
838 return stream
.Write("\n", 1);