]>
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
)
85 ptrback
= AllocSpaceWBack(bufsize
);
89 memcpy(ptrback
, buf
, bufsize
);
93 bool wxStreamBuffer::WriteBack(char c
)
97 ptrback
= AllocSpaceWBack(1);
105 void wxStreamBuffer::SetBufferIO(char *buffer_start
, char *buffer_end
)
108 wxDELETEA(m_buffer_start
);
109 m_buffer_start
= buffer_start
;
110 m_buffer_end
= buffer_end
;
112 m_buffer_size
= m_buffer_end
-m_buffer_start
;
113 m_destroybuf
= FALSE
;
117 void wxStreamBuffer::SetBufferIO(size_t bufsize
)
122 wxDELETEA(m_buffer_start
);
125 m_buffer_start
= NULL
;
132 b_start
= new char[bufsize
];
133 SetBufferIO(b_start
, b_start
+ bufsize
);
137 void wxStreamBuffer::ResetBuffer()
140 m_buffer_pos
= m_buffer_end
;
142 m_buffer_pos
= m_buffer_start
;
145 char *wxStreamBuffer::AllocSpaceWBack(size_t needed_size
)
149 m_wbacksize
+= needed_size
;
152 temp_b
= (char *)malloc(m_wbacksize
);
154 temp_b
= (char *)realloc(m_wback
, m_wbacksize
);
159 printf("Buffer(0x%x)->Write: 0x%x, %d\n", this, m_wback
, m_wbacksize
);
160 return (char *)(m_wback
+(m_wbacksize
-needed_size
));
163 size_t wxStreamBuffer::GetWBack(char *buf
, size_t bsize
)
165 size_t s_toget
= m_wbacksize
-m_wbackcur
;
167 printf("Buffer(0x%x): 0x%x, %d\n", this, m_wback
, m_wbacksize
);
171 memcpy(buf
, (m_wback
+m_wbackcur
), s_toget
);
173 m_wbackcur
+= s_toget
;
174 if (m_wbackcur
== m_wbacksize
) {
176 m_wback
= (char *)NULL
;
184 bool wxStreamBuffer::FillBuffer()
188 count
= m_stream
->OnSysRead(m_buffer_start
, m_buffer_size
);
189 m_buffer_end
= m_buffer_start
+count
;
190 m_buffer_pos
= m_buffer_start
;
197 bool wxStreamBuffer::FlushBuffer()
199 size_t count
, current
;
201 if (m_buffer_pos
== m_buffer_start
|| !m_flushable
)
204 current
= m_buffer_pos
-m_buffer_start
;
205 count
= m_stream
->OnSysWrite(m_buffer_start
, current
);
206 if (count
!= current
)
208 m_buffer_pos
= m_buffer_start
;
213 void wxStreamBuffer::GetFromBuffer(void *buffer
, size_t size
)
215 size_t s_toget
= m_buffer_end
-m_buffer_pos
;
220 memcpy(buffer
, m_buffer_pos
, s_toget
);
221 m_buffer_pos
+= s_toget
;
224 void wxStreamBuffer::PutToBuffer(const void *buffer
, size_t size
)
226 size_t s_toput
= m_buffer_end
-m_buffer_pos
;
228 if (s_toput
< size
&& !m_fixed
) {
229 m_buffer_start
= (char *)realloc(m_buffer_start
, m_buffer_size
+size
);
231 m_buffer_size
+= size
;
232 m_buffer_end
= m_buffer_start
+m_buffer_size
;
237 memcpy(m_buffer_pos
, buffer
, s_toput
);
238 m_buffer_pos
+= s_toput
;
241 void wxStreamBuffer::PutChar(char c
)
243 wxASSERT(m_stream
!= NULL
);
245 if (!m_buffer_size
) {
246 m_stream
->OnSysWrite(&c
, 1);
250 if (!GetDataLeft() && !FlushBuffer()) {
251 CHECK_ERROR(wxStream_READ_ERR
);
256 m_stream
->m_lastcount
= 1;
259 char wxStreamBuffer::GetChar()
263 wxASSERT(m_stream
!= NULL
);
265 if (!m_buffer_size
) {
266 m_stream
->OnSysRead(&c
, 1);
270 if (!GetDataLeft()) {
271 CHECK_ERROR(wxStream_READ_ERR
);
275 GetFromBuffer(&c
, 1);
276 m_stream
->m_lastcount
= 1;
280 size_t wxStreamBuffer::Read(void *buffer
, size_t size
)
282 wxASSERT(m_stream
!= NULL
);
284 // ------------------
285 // Buffering disabled
286 // ------------------
288 m_stream
->m_lastcount
= GetWBack((char *)buffer
, size
);
289 size
-= m_stream
->m_lastcount
;
291 return m_stream
->m_lastcount
;
293 buffer
= (void *)((char *)buffer
+m_stream
->m_lastcount
);
296 return (m_stream
->m_lastcount
+= m_stream
->OnSysRead(buffer
, size
));
301 size_t buf_left
, orig_size
= size
;
304 buf_left
= GetDataLeft();
306 // First case: the requested buffer is larger than the stream buffer,
308 if (size
> buf_left
) {
309 GetFromBuffer(buffer
, buf_left
);
311 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
314 CHECK_ERROR(wxStream_READ_ERR
);
315 return (m_stream
->m_lastcount
= orig_size
-size
);
319 // Second case: we just copy from the stream buffer.
320 GetFromBuffer(buffer
, size
);
324 return (m_stream
->m_lastcount
+= orig_size
);
327 size_t wxStreamBuffer::Read(wxStreamBuffer
*s_buf
)
329 char buf
[BUF_TEMP_SIZE
];
330 size_t s
= 0, bytes_read
= BUF_TEMP_SIZE
;
332 while (bytes_read
== BUF_TEMP_SIZE
) {
333 bytes_read
= Read(buf
, bytes_read
);
334 bytes_read
= s_buf
->Write(buf
, bytes_read
);
340 size_t wxStreamBuffer::Write(const void *buffer
, size_t size
)
342 wxASSERT(m_stream
!= NULL
);
344 // ------------------
345 // Buffering disabled
346 // ------------------
349 return (m_stream
->m_lastcount
= m_stream
->OnSysWrite(buffer
, size
));
351 // ------------------
353 // ------------------
355 size_t buf_left
, orig_size
= size
;
358 buf_left
= m_buffer_end
- m_buffer_pos
;
360 // First case: the buffer to write is larger than the stream buffer,
362 if (size
> buf_left
) {
363 PutToBuffer(buffer
, buf_left
);
365 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
367 if (!FlushBuffer()) {
368 CHECK_ERROR(wxStream_WRITE_ERR
);
369 return (m_stream
->m_lastcount
= orig_size
-size
);
372 m_buffer_pos
= m_buffer_start
;
376 // Second case: just copy it in the stream buffer.
377 PutToBuffer(buffer
, size
);
381 return (m_stream
->m_lastcount
= orig_size
);
384 size_t wxStreamBuffer::Write(wxStreamBuffer
*sbuf
)
386 char buf
[BUF_TEMP_SIZE
];
387 size_t s
= 0, bytes_count
= BUF_TEMP_SIZE
, b_count2
;
389 while (bytes_count
== BUF_TEMP_SIZE
) {
390 b_count2
= sbuf
->Read(buf
, bytes_count
);
391 bytes_count
= Write(buf
, b_count2
);
392 if (b_count2
> bytes_count
)
393 sbuf
->WriteBack(buf
+bytes_count
, b_count2
-bytes_count
);
399 off_t
wxStreamBuffer::Seek(off_t pos
, wxSeekMode mode
)
401 off_t ret_off
, diff
, last_access
;
403 last_access
= GetLastAccess();
406 diff
= pos
+ GetIntPosition();
407 if (diff
< 0 || diff
> last_access
)
408 return wxInvalidOffset
;
409 SetIntPosition(diff
);
415 // We'll try to compute an internal position later ...
416 ret_off
= m_stream
->OnSysSeek(pos
, wxFromStart
);
420 case wxFromCurrent
: {
421 diff
= pos
+ GetIntPosition();
423 if ( (diff
> last_access
) || (diff
< 0) ) {
424 ret_off
= m_stream
->OnSysSeek(pos
, wxFromCurrent
);
428 SetIntPosition(diff
);
433 // Hard to compute: always seek to the requested position.
434 ret_off
= m_stream
->OnSysSeek(pos
, wxFromEnd
);
438 return wxInvalidOffset
;
441 off_t
wxStreamBuffer::Tell() const
446 pos
= m_stream
->OnSysTell();
447 if (pos
== wxInvalidOffset
)
448 return wxInvalidOffset
;
449 return pos
- GetLastAccess() + GetIntPosition();
451 return GetIntPosition();
454 size_t wxStreamBuffer::GetDataLeft()
456 if (m_buffer_end
== m_buffer_pos
&& m_flushable
)
458 return m_buffer_end
-m_buffer_pos
;
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
465 wxStreamBase::wxStreamBase()
467 m_lasterror
= wxStream_NOERROR
;
471 wxStreamBase::~wxStreamBase()
475 size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer
), size_t WXUNUSED(size
))
480 size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer
), size_t WXUNUSED(bufsize
))
485 off_t
wxStreamBase::OnSysSeek(off_t
WXUNUSED(seek
), wxSeekMode
WXUNUSED(mode
))
487 return wxInvalidOffset
;
490 off_t
wxStreamBase::OnSysTell() const
492 return wxInvalidOffset
;
495 // ----------------------------------------------------------------------------
497 // ----------------------------------------------------------------------------
499 wxInputStream::wxInputStream()
502 m_i_destroybuf
= TRUE
;
503 m_i_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::read
);
506 wxInputStream::wxInputStream(wxStreamBuffer
*buffer
)
509 m_i_destroybuf
= FALSE
;
510 m_i_streambuf
= buffer
;
513 wxInputStream::~wxInputStream()
516 delete m_i_streambuf
;
519 char wxInputStream::GetC()
522 m_i_streambuf
->Read(&c
, 1);
526 wxInputStream
& wxInputStream::Read(void *buffer
, size_t size
)
528 m_i_streambuf
->Read(buffer
, size
);
529 // wxStreamBuffer sets all variables for us
533 char wxInputStream::Peek()
535 m_i_streambuf
->GetDataLeft();
537 return *(m_i_streambuf
->GetBufferPos());
541 wxInputStream
& wxInputStream::Read(wxOutputStream
& stream_out
)
543 char buf
[BUF_TEMP_SIZE
];
544 size_t bytes_read
= BUF_TEMP_SIZE
;
546 while (bytes_read
== BUF_TEMP_SIZE
) {
547 bytes_read
= Read(buf
, bytes_read
).LastRead();
548 bytes_read
= stream_out
.Write(buf
, bytes_read
).LastWrite();
553 off_t
wxInputStream::SeekI(off_t pos
, wxSeekMode mode
)
555 return m_i_streambuf
->Seek(pos
, mode
);
558 off_t
wxInputStream::TellI() const
560 return m_i_streambuf
->Tell();
563 // --------------------
564 // Overloaded operators
565 // --------------------
567 wxInputStream
& wxInputStream::operator>>(wxString
& line
)
569 wxDataInputStream
s(*this);
575 wxInputStream
& wxInputStream::operator>>(char& c
)
581 wxInputStream
& wxInputStream::operator>>(short& i
)
590 wxInputStream
& wxInputStream::operator>>(int& i
)
599 wxInputStream
& wxInputStream::operator>>(long& i
)
601 /* I only implemented a simple integer parser */
604 while (isspace( c
= GetC() ) )
608 if (! (c
== '-' || isdigit(c
)) ) {
609 InputStreamBuffer()->WriteBack(c
);
629 wxInputStream
& wxInputStream::operator>>(double& f
)
631 /* I only implemented a simple float parser */
634 while (isspace( c
= GetC() ) )
638 if (! (c
== '-' || isdigit(c
)) ) {
639 InputStreamBuffer()->WriteBack(c
);
650 f
= f
*10 + (c
- '0');
655 double f_multiplicator
= (double) 0.1;
659 f
+= (c
-'0')*f_multiplicator
;
660 f_multiplicator
/= 10;
671 wxInputStream
& wxInputStream::operator>>(wxObject
*& obj
)
673 wxObjectInputStream
obj_s(*this);
674 obj
= obj_s
.LoadObject();
680 // ----------------------------------------------------------------------------
682 // ----------------------------------------------------------------------------
683 wxOutputStream::wxOutputStream()
686 m_o_destroybuf
= TRUE
;
687 m_o_streambuf
= new wxStreamBuffer(*this, wxStreamBuffer::write
);
690 wxOutputStream::wxOutputStream(wxStreamBuffer
*buffer
)
693 m_o_destroybuf
= FALSE
;
694 m_o_streambuf
= buffer
;
697 wxOutputStream::~wxOutputStream()
700 delete m_o_streambuf
;
703 wxOutputStream
& wxOutputStream::Write(const void *buffer
, size_t size
)
705 m_o_streambuf
->Write(buffer
, size
);
709 wxOutputStream
& wxOutputStream::Write(wxInputStream
& stream_in
)
711 stream_in
.Read(*this);
715 off_t
wxOutputStream::TellO() const
717 return m_o_streambuf
->Tell();
720 off_t
wxOutputStream::SeekO(off_t pos
, wxSeekMode mode
)
722 return m_o_streambuf
->Seek(pos
, mode
);
725 void wxOutputStream::Sync()
727 m_o_streambuf
->FlushBuffer();
730 wxOutputStream
& wxOutputStream::operator<<(const char *string
)
732 return Write(string
, strlen(string
));
735 wxOutputStream
& wxOutputStream::operator<<(wxString
& string
)
737 return Write(string
, string
.Len());
740 wxOutputStream
& wxOutputStream::operator<<(char c
)
745 wxOutputStream
& wxOutputStream::operator<<(short i
)
749 strint
.Printf("%i", i
);
750 return Write(strint
, strint
.Len());
753 wxOutputStream
& wxOutputStream::operator<<(int i
)
757 strint
.Printf("%i", i
);
758 return Write(strint
, strint
.Len());
761 wxOutputStream
& wxOutputStream::operator<<(long i
)
765 strlong
.Printf("%i", i
);
766 return Write((const char *)strlong
, strlong
.Len());
769 wxOutputStream
& wxOutputStream::operator<<(double f
)
773 strfloat
.Printf("%f", f
);
774 return Write(strfloat
, strfloat
.Len());
778 wxOutputStream
& wxOutputStream::operator<<(wxObject
& obj
)
780 wxObjectOutputStream
obj_s(*this);
781 obj_s
.SaveObject(obj
);
786 // ----------------------------------------------------------------------------
787 // wxFilterInputStream
788 // ----------------------------------------------------------------------------
789 wxFilterInputStream::wxFilterInputStream()
790 : wxInputStream(NULL
)
792 // WARNING streambuf set to NULL !
795 wxFilterInputStream::wxFilterInputStream(wxInputStream
& stream
)
796 : wxInputStream(stream
.InputStreamBuffer())
798 m_parent_i_stream
= &stream
;
801 wxFilterInputStream::~wxFilterInputStream()
805 // ----------------------------------------------------------------------------
806 // wxFilterOutputStream
807 // ----------------------------------------------------------------------------
808 wxFilterOutputStream::wxFilterOutputStream()
809 : wxOutputStream(NULL
)
813 wxFilterOutputStream::wxFilterOutputStream(wxOutputStream
& stream
)
814 : wxOutputStream(stream
.OutputStreamBuffer())
816 m_parent_o_stream
= &stream
;
819 wxFilterOutputStream::~wxFilterOutputStream()
823 // ----------------------------------------------------------------------------
824 // Some IOManip function
825 // ----------------------------------------------------------------------------
827 wxOutputStream
& wxEndL(wxOutputStream
& stream
)
830 return stream
.Write("\r\n", 2);
832 return stream
.Write("\n", 1);