]>
git.saurik.com Git - wxWidgets.git/blob - src/common/stream.cpp
dda5f6446bee5ba0e264eb2fb20d85bd4655cdf6
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 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 wxStreamBuffer::wxStreamBuffer(wxInputStream
& i_stream
)
32 : m_buffer_start(NULL
), m_buffer_end(NULL
), m_buffer_pos(NULL
),
33 m_buffer_size(0), m_istream(&i_stream
), m_ostream(NULL
)
37 wxStreamBuffer::wxStreamBuffer(wxOutputStream
& o_stream
)
38 : m_buffer_start(NULL
), m_buffer_end(NULL
), m_buffer_pos(NULL
),
39 m_buffer_size(0), m_istream(NULL
), m_ostream(&o_stream
)
43 wxStreamBuffer::~wxStreamBuffer()
45 wxDELETEA(m_buffer_start
);
48 void wxStreamBuffer::WriteBack(char c
)
53 // Assume that if we write "back" we have read a few bytes: so we have some
60 void wxStreamBuffer::SetBufferIO(char *buffer_start
, char *buffer_end
)
64 m_buffer_start
= buffer_start
;
65 m_buffer_end
= buffer_end
;
67 m_buffer_size
= m_buffer_end
-m_buffer_start
;
70 ret
= m_istream
->DoRead(m_buffer_start
, m_buffer_size
);
71 m_buffer_end
= m_buffer_start
+ ret
;
73 m_buffer_pos
= m_buffer_start
;
76 void wxStreamBuffer::SetBufferIO(size_t bufsize
)
80 wxDELETE(m_buffer_start
);
83 m_buffer_start
= NULL
;
90 b_start
= new char[bufsize
];
92 SetBufferIO(b_start
, b_start
+ bufsize
);
95 void wxStreamBuffer::ResetBuffer()
98 m_buffer_pos
= m_buffer_end
;
100 m_buffer_pos
= m_buffer_start
;
103 void wxStreamBuffer::Read(void *buffer
, size_t size
)
105 wxASSERT(m_istream
!= NULL
);
107 // ------------------
108 // Buffering disabled
109 // ------------------
111 if (!m_buffer_size
) {
112 m_istream
->m_lastread
= m_istream
->DoRead(buffer
, size
);
119 size_t buf_left
, orig_size
= size
;
123 buf_left
= m_buffer_end
- m_buffer_pos
;
125 // First case: the requested buffer is larger than the stream buffer,
127 if (size
> buf_left
) {
128 memcpy(buffer
, m_buffer_pos
, buf_left
);
130 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
132 read_ret
= m_istream
->DoRead(m_buffer_start
, m_buffer_size
);
136 m_istream
->m_lastread
= orig_size
-size
;
137 m_istream
->m_eof
= TRUE
;
138 m_buffer_pos
= m_buffer_end
= m_buffer_start
;
141 m_buffer_end
= m_buffer_start
+read_ret
;
142 m_buffer_pos
= m_buffer_start
;
146 // Second case: we just copy from the stream buffer.
147 memcpy(buffer
, m_buffer_pos
, size
);
148 m_buffer_pos
+= size
;
152 m_istream
->m_lastread
= orig_size
;
155 void wxStreamBuffer::Write(const void *buffer
, size_t size
)
157 wxASSERT(m_ostream
!= NULL
);
159 // ------------------
160 // Buffering disabled
161 // ------------------
163 if (!m_buffer_size
) {
164 m_ostream
->m_lastwrite
= m_ostream
->DoWrite(buffer
, size
);
168 // ------------------
170 // ------------------
172 size_t buf_left
, orig_size
= size
;
176 buf_left
= m_buffer_end
- m_buffer_pos
;
178 // First case: the buffer to write is larger than the stream buffer,
180 if (size
> buf_left
) {
181 memcpy(m_buffer_pos
, buffer
, buf_left
);
183 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
185 write_ret
= m_ostream
->DoWrite(m_buffer_start
, m_buffer_size
);
186 if (write_ret
!= m_buffer_size
) {
187 m_ostream
->m_bad
= TRUE
;
188 m_ostream
->m_lastwrite
= orig_size
-size
;
189 m_buffer_pos
= m_buffer_end
= m_buffer_start
;
192 m_buffer_pos
= m_buffer_start
;
196 // Second case: just copy it in the stream buffer.
198 memcpy(m_buffer_pos
, buffer
, size
);
199 m_buffer_pos
+= size
;
203 m_ostream
->m_lastwrite
= orig_size
;
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 wxInputStream::wxInputStream()
212 m_i_destroybuf
= TRUE
;
213 m_i_streambuf
= new wxStreamBuffer(*this);
217 wxInputStream::wxInputStream(wxStreamBuffer
*buffer
)
219 m_i_destroybuf
= FALSE
;
220 m_i_streambuf
= buffer
;
224 wxInputStream::~wxInputStream()
227 delete m_i_streambuf
;
230 char wxInputStream::GetC()
233 m_i_streambuf
->Read(&c
, 1);
237 wxInputStream
& wxInputStream::Read(void *buffer
, size_t size
)
239 m_i_streambuf
->Read(buffer
, size
);
240 // wxStreamBuffer sets all variables for us
244 #define BUF_TEMP_SIZE 10000
246 wxInputStream
& wxInputStream::Read(wxOutputStream
& stream_out
)
248 char buf
[BUF_TEMP_SIZE
];
249 size_t bytes_read
= BUF_TEMP_SIZE
;
251 while (bytes_read
== BUF_TEMP_SIZE
&& !stream_out
.Bad()) {
252 bytes_read
= Read(buf
, bytes_read
).LastRead();
254 stream_out
.Write(buf
, bytes_read
);
259 wxInputStream
& wxInputStream::operator>>(wxString
& line
)
261 wxDataInputStream
s(*this);
267 wxInputStream
& wxInputStream::operator>>(char& c
)
273 wxInputStream
& wxInputStream::operator>>(short& i
)
282 wxInputStream
& wxInputStream::operator>>(int& i
)
291 wxInputStream
& wxInputStream::operator>>(long& i
)
293 /* I only implemented a simple integer parser */
296 while (isspace( c
= GetC() ) )
300 if (! (c
== '-' || isdigit(c
)) ) {
301 InputStreamBuffer()->WriteBack(c
);
321 wxInputStream
& wxInputStream::operator>>(float& f
)
323 /* I only implemented a simple float parser */
326 while (isspace( c
= GetC() ) )
330 if (! (c
== '-' || isdigit(c
)) ) {
331 InputStreamBuffer()->WriteBack(c
);
347 float f_multiplicator
= 0.1;
351 f
+= c
*f_multiplicator
;
352 f_multiplicator
/= 10;
362 wxInputStream
& wxInputStream::operator>>(wxObject
*& obj
)
364 wxObjectInputStream
obj_s(*this);
365 obj
= obj_s
.LoadObject();
369 off_t
wxInputStream::SeekI(off_t pos
, wxSeekMode mode
)
371 off_t ret_off
, diff
, last_access
;
373 last_access
= m_i_streambuf
->GetLastAccess();
377 diff
= DoTellInput() - pos
;
378 if ( diff
< 0 || diff
> last_access
) {
379 ret_off
= DoSeekInput(pos
, wxFromStart
);
380 m_i_streambuf
->ResetBuffer();
383 m_i_streambuf
->SetIntPosition(last_access
- diff
);
387 diff
= pos
+ m_i_streambuf
->GetIntPosition();
389 if ( (diff
> last_access
) || (diff
< 0) ) {
390 ret_off
= DoSeekInput(pos
, wxFromCurrent
);
391 m_i_streambuf
->ResetBuffer();
394 m_i_streambuf
->SetIntPosition(diff
);
398 // Hard to compute: always seek to the requested position.
399 ret_off
= DoSeekInput(pos
, wxFromEnd
);
400 m_i_streambuf
->ResetBuffer();
403 return wxInvalidOffset
;
406 off_t
wxInputStream::TellI() const
408 return DoTellInput() - m_i_streambuf
->GetLastAccess() +
409 m_i_streambuf
->GetIntPosition();
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
415 wxOutputStream::wxOutputStream()
417 m_o_destroybuf
= TRUE
;
418 m_o_streambuf
= new wxStreamBuffer(*this);
421 wxOutputStream::wxOutputStream(wxStreamBuffer
*buffer
)
423 m_o_destroybuf
= FALSE
;
424 m_o_streambuf
= buffer
;
427 wxOutputStream::~wxOutputStream()
430 delete m_o_streambuf
;
433 wxOutputStream
& wxOutputStream::Write(const void *buffer
, size_t size
)
435 m_o_streambuf
->Write(buffer
, size
);
439 wxOutputStream
& wxOutputStream::Write(wxInputStream
& stream_in
)
441 stream_in
.Read(*this);
445 off_t
wxOutputStream::SeekO(off_t pos
, wxSeekMode mode
)
451 if ( (unsigned)abs (DoTellOutput()-pos
) > m_o_streambuf
->GetLastAccess() ) {
452 ret_off
= DoSeekOutput(pos
, wxFromStart
);
453 m_o_streambuf
->ResetBuffer();
456 m_o_streambuf
->SetIntPosition( DoTellOutput() - pos
);
460 if ( ((unsigned)pos
> m_o_streambuf
->GetLastAccess()) || (pos
< 0) ) {
461 ret_off
= DoSeekOutput(pos
, wxFromCurrent
);
462 m_o_streambuf
->ResetBuffer();
465 m_o_streambuf
->SetIntPosition(pos
);
469 // Hard to compute: always seek to the requested position.
470 ret_off
= DoSeekOutput(pos
, wxFromEnd
);
471 m_o_streambuf
->ResetBuffer();
474 return wxInvalidOffset
;
477 off_t
wxOutputStream::TellO() const
479 return DoTellOutput() - m_o_streambuf
->GetLastAccess()
480 + m_o_streambuf
->GetIntPosition();
483 void wxOutputStream::Sync()
485 DoWrite(m_o_streambuf
->GetBufferStart(), m_o_streambuf
->GetIntPosition());
487 m_o_streambuf
->ResetBuffer();
490 wxOutputStream
& wxOutputStream::operator<<(const char *string
)
492 return Write(string
, strlen(string
));
495 wxOutputStream
& wxOutputStream::operator<<(wxString
& string
)
497 return Write(string
, string
.Len());
500 wxOutputStream
& wxOutputStream::operator<<(char c
)
505 wxOutputStream
& wxOutputStream::operator<<(short i
)
509 strint
.Printf("%i", i
);
510 return Write(strint
, strint
.Len());
513 wxOutputStream
& wxOutputStream::operator<<(int i
)
517 strint
.Printf("%i", i
);
518 return Write(strint
, strint
.Len());
521 wxOutputStream
& wxOutputStream::operator<<(long i
)
525 strlong
.Printf("%i", i
);
526 return Write((const char *)strlong
, strlong
.Len());
529 wxOutputStream
& wxOutputStream::operator<<(double f
)
533 strfloat
.Printf("%f", f
);
534 return Write(strfloat
, strfloat
.Len());
537 wxOutputStream
& wxOutputStream::operator<<(wxObject
& obj
)
539 wxObjectOutputStream
obj_s(*this);
540 obj_s
.SaveObject(obj
);
544 // ----------------------------------------------------------------------------
545 // wxFilterInputStream
546 // ----------------------------------------------------------------------------
547 wxFilterInputStream::wxFilterInputStream(wxInputStream
& stream
)
548 : wxInputStream(NULL
)
550 m_parent_i_stream
= &stream
;
551 m_i_streambuf
= stream
.InputStreamBuffer();
554 wxFilterInputStream::~wxFilterInputStream()
558 size_t wxFilterInputStream::DoRead(void *buffer
, size_t size
)
560 return m_parent_i_stream
->Read(buffer
, size
).LastRead();
563 off_t
wxFilterInputStream::DoSeekInput(off_t pos
, wxSeekMode mode
)
565 return m_parent_i_stream
->SeekI(pos
, mode
);
568 off_t
wxFilterInputStream::DoTellInput() const
570 return m_parent_i_stream
->TellI();
574 // ----------------------------------------------------------------------------
575 // wxFilterOutputStream
576 // ----------------------------------------------------------------------------
577 wxFilterOutputStream::wxFilterOutputStream(wxOutputStream
& stream
)
578 : wxOutputStream(NULL
)
580 m_parent_o_stream
= &stream
;
581 m_o_streambuf
= stream
.OutputStreamBuffer();
584 wxFilterOutputStream::~wxFilterOutputStream()
588 size_t wxFilterOutputStream::DoWrite(const void *buffer
, size_t size
)
590 return m_parent_o_stream
->Write(buffer
, size
).LastWrite();
593 off_t
wxFilterOutputStream::DoSeekOutput(off_t pos
, wxSeekMode mode
)
595 return m_parent_o_stream
->SeekO(pos
, mode
);
598 off_t
wxFilterOutputStream::DoTellOutput() const
600 return m_parent_o_stream
->TellO();
603 // ----------------------------------------------------------------------------
604 // Some IOManip function
605 // ----------------------------------------------------------------------------
607 wxOutputStream
& wxEndL(wxOutputStream
& stream
)
610 return stream
.Write("\r\n", 2);
612 return stream
.Write("\n", 1);