]>
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>
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 wxStreamBuffer::wxStreamBuffer(wxInputStream
& i_stream
)
31 : m_buffer_start(NULL
), m_buffer_end(NULL
), m_buffer_pos(NULL
),
32 m_buffer_size(0), m_istream(&i_stream
), m_ostream(NULL
)
36 wxStreamBuffer::wxStreamBuffer(wxOutputStream
& o_stream
)
37 : m_buffer_start(NULL
), m_buffer_end(NULL
), m_buffer_pos(NULL
),
38 m_buffer_size(0), m_istream(NULL
), m_ostream(&o_stream
)
42 wxStreamBuffer::~wxStreamBuffer()
46 void wxStreamBuffer::WriteBack(char c
)
51 // Assume that if we write "back" we have read a few bytes: so we have some
58 void wxStreamBuffer::SetBufferIO(char *buffer_start
, char *buffer_end
)
60 m_buffer_pos
= m_buffer_start
= buffer_start
;
61 m_buffer_end
= buffer_end
;
63 m_buffer_size
= m_buffer_end
-m_buffer_start
;
66 void wxStreamBuffer::SetBufferIO(size_t bufsize
)
69 delete[] m_buffer_start
;
72 m_buffer_start
= NULL
;
78 m_buffer_start
= new char[bufsize
];
79 m_buffer_end
= m_buffer_start
+ bufsize
;
81 m_buffer_pos
= m_buffer_end
;
83 m_buffer_pos
= m_buffer_start
;
84 m_buffer_size
= bufsize
;
87 void wxStreamBuffer::ResetBuffer()
90 m_buffer_pos
= m_buffer_end
;
92 m_buffer_pos
= m_buffer_start
;
95 void wxStreamBuffer::Read(void *buffer
, size_t size
)
97 wxASSERT(m_istream
!= NULL
);
100 // Buffering disabled
101 // ------------------
103 if (!m_buffer_size
) {
104 m_istream
->m_lastread
= m_istream
->DoRead(buffer
, size
);
111 size_t buf_left
, orig_size
= size
;
115 buf_left
= m_buffer_end
- m_buffer_pos
;
117 // First case: the requested buffer is larger than the stream buffer,
119 if (size
> buf_left
) {
120 memcpy(buffer
, m_buffer_pos
, buf_left
);
122 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
124 read_ret
= m_istream
->DoRead(m_buffer_start
, m_buffer_size
);
128 m_istream
->m_lastread
= orig_size
-size
;
129 m_buffer_pos
= m_buffer_end
= m_buffer_start
;
132 m_buffer_end
= m_buffer_start
+read_ret
;
133 m_buffer_pos
= m_buffer_start
;
137 // Second case: we just copy from the stream buffer.
138 memcpy(buffer
, m_buffer_pos
, size
);
139 m_buffer_pos
+= size
;
143 m_istream
->m_lastread
= orig_size
;
146 void wxStreamBuffer::Write(const void *buffer
, size_t size
)
148 wxASSERT(m_ostream
!= NULL
);
150 // ------------------
151 // Buffering disabled
152 // ------------------
154 if (!m_buffer_size
) {
155 m_ostream
->m_lastwrite
= m_ostream
->DoWrite(buffer
, size
);
159 // ------------------
161 // ------------------
163 size_t buf_left
, orig_size
= size
;
167 buf_left
= m_buffer_end
- m_buffer_pos
;
169 // First case: the buffer to write is larger than the stream buffer,
171 if (size
> buf_left
) {
172 memcpy(m_buffer_pos
, buffer
, buf_left
);
174 buffer
= (char *)buffer
+ buf_left
; // ANSI C++ violation.
176 write_ret
= m_ostream
->DoWrite(m_buffer_start
, m_buffer_size
);
177 if (write_ret
!= m_buffer_size
) {
178 m_ostream
->m_bad
= TRUE
;
179 m_ostream
->m_lastwrite
= orig_size
-size
;
180 m_buffer_pos
= m_buffer_end
= m_buffer_start
;
183 m_buffer_pos
= m_buffer_start
;
187 // Second case: just copy it in the stream buffer.
189 memcpy(m_buffer_pos
, buffer
, size
);
190 m_buffer_pos
+= size
;
194 m_ostream
->m_lastwrite
= orig_size
;
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 wxInputStream::wxInputStream()
203 m_i_destroybuf
= TRUE
;
204 m_i_streambuf
= new wxStreamBuffer(*this);
207 wxInputStream::wxInputStream(wxStreamBuffer
*buffer
)
209 m_i_destroybuf
= FALSE
;
210 m_i_streambuf
= buffer
;
213 wxInputStream::~wxInputStream()
216 delete m_i_streambuf
;
219 char wxInputStream::GetC()
222 m_i_streambuf
->Read(&c
, 1);
226 wxInputStream
& wxInputStream::Read(void *buffer
, size_t size
)
228 m_i_streambuf
->Read(buffer
, size
);
229 // wxStreamBuffer sets all variables for us
233 #define BUF_TEMP_SIZE 10000
235 wxInputStream
& wxInputStream::Read(wxOutputStream
& stream_out
)
237 char buf
[BUF_TEMP_SIZE
];
238 size_t bytes_read
= BUF_TEMP_SIZE
;
240 while (bytes_read
== BUF_TEMP_SIZE
&& !stream_out
.Bad()) {
241 bytes_read
= Read(buf
, bytes_read
).LastRead();
243 stream_out
.Write(buf
, bytes_read
);
248 wxInputStream
& wxInputStream::operator>>(wxString
& line
)
250 wxDataInputStream
s(*this);
256 wxInputStream
& wxInputStream::operator>>(char& c
)
262 wxInputStream
& wxInputStream::operator>>(short& i
)
271 wxInputStream
& wxInputStream::operator>>(long& i
)
273 /* I only implemented a simple integer parser */
276 while (isspace( c
= GetC() ) )
280 if (! (c
== '-' || isdigit(c
)) ) {
281 InputStreamBuffer()->WriteBack(c
);
301 wxInputStream
& wxInputStream::operator>>(float& f
)
303 /* I only implemented a simple float parser */
306 while (isspace( c
= GetC() ) )
310 if (! (c
== '-' || isdigit(c
)) ) {
311 InputStreamBuffer()->WriteBack(c
);
327 float f_multiplicator
= 0.1;
331 f
+= c
*f_multiplicator
;
332 f_multiplicator
/= 10;
342 off_t
wxInputStream::SeekI(off_t pos
, wxSeekMode mode
)
348 if ( (unsigned)abs (DoTellInput()-pos
) > m_i_streambuf
->GetLastAccess() ) {
349 ret_off
= DoSeekInput(pos
, wxFromStart
);
350 m_i_streambuf
->ResetBuffer();
353 m_i_streambuf
->SetIntPosition(DoTellInput() - pos
);
357 if ( ((unsigned)pos
> m_i_streambuf
->GetLastAccess()) || (pos
< 0) ) {
358 ret_off
= DoSeekInput(pos
, wxFromCurrent
);
359 m_i_streambuf
->ResetBuffer();
362 m_i_streambuf
->SetIntPosition(pos
);
366 // Hard to compute: always seek to the requested position.
367 ret_off
= DoSeekInput(pos
, wxFromEnd
);
368 m_i_streambuf
->ResetBuffer();
371 return wxInvalidOffset
;
374 off_t
wxInputStream::TellI() const
376 return DoTellInput() - m_i_streambuf
->GetLastAccess() +
377 m_i_streambuf
->GetIntPosition();
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
383 wxOutputStream::wxOutputStream()
385 m_o_destroybuf
= TRUE
;
386 m_o_streambuf
= new wxStreamBuffer(*this);
389 wxOutputStream::wxOutputStream(wxStreamBuffer
*buffer
)
391 m_o_destroybuf
= FALSE
;
392 m_o_streambuf
= buffer
;
395 wxOutputStream::~wxOutputStream()
398 delete m_o_streambuf
;
401 wxOutputStream
& wxOutputStream::Write(const void *buffer
, size_t size
)
403 m_o_streambuf
->Write(buffer
, size
);
407 wxOutputStream
& wxOutputStream::Write(wxInputStream
& stream_in
)
409 stream_in
.Read(*this);
413 off_t
wxOutputStream::SeekO(off_t pos
, wxSeekMode mode
)
419 if ( (unsigned)abs (DoTellOutput()-pos
) > m_o_streambuf
->GetLastAccess() ) {
420 ret_off
= DoSeekOutput(pos
, wxFromStart
);
421 m_o_streambuf
->ResetBuffer();
424 m_o_streambuf
->SetIntPosition( DoTellOutput() - pos
);
428 if ( ((unsigned)pos
> m_o_streambuf
->GetLastAccess()) || (pos
< 0) ) {
429 ret_off
= DoSeekOutput(pos
, wxFromCurrent
);
430 m_o_streambuf
->ResetBuffer();
433 m_o_streambuf
->SetIntPosition(pos
);
437 // Hard to compute: always seek to the requested position.
438 ret_off
= DoSeekOutput(pos
, wxFromEnd
);
439 m_o_streambuf
->ResetBuffer();
442 return wxInvalidOffset
;
445 off_t
wxOutputStream::TellO() const
447 return DoTellOutput() - m_o_streambuf
->GetLastAccess()
448 + m_o_streambuf
->GetIntPosition();
451 void wxOutputStream::Sync()
453 DoWrite(m_o_streambuf
->GetBufferStart(), m_o_streambuf
->GetIntPosition());
455 m_o_streambuf
->ResetBuffer();
458 wxOutputStream
& wxOutputStream::operator<<(const char *string
)
460 return Write(string
, strlen(string
));
463 wxOutputStream
& wxOutputStream::operator<<(wxString
& string
)
465 return Write(string
, string
.Len());
468 wxOutputStream
& wxOutputStream::operator<<(char c
)
473 wxOutputStream
& wxOutputStream::operator<<(short i
)
477 strint
.Printf("%i", i
);
478 return Write(strint
, strint
.Len());
481 wxOutputStream
& wxOutputStream::operator<<(int i
)
485 strint
.Printf("%i", i
);
486 return Write(strint
, strint
.Len());
489 wxOutputStream
& wxOutputStream::operator<<(long i
)
493 strlong
.Printf("%i", i
);
494 return Write((const char *)strlong
, strlong
.Len());
497 wxOutputStream
& wxOutputStream::operator<<(double f
)
501 strfloat
.Printf("%f", f
);
502 return Write(strfloat
, strfloat
.Len());
505 // ----------------------------------------------------------------------------
506 // wxFilterInputStream
507 // ----------------------------------------------------------------------------
508 wxFilterInputStream::wxFilterInputStream(wxInputStream
& stream
)
509 : wxInputStream(NULL
)
511 m_parent_i_stream
= &stream
;
512 m_i_streambuf
= stream
.InputStreamBuffer();
515 wxFilterInputStream::~wxFilterInputStream()
519 size_t wxFilterInputStream::DoRead(void *buffer
, size_t size
)
521 return m_parent_i_stream
->Read(buffer
, size
).LastRead();
524 off_t
wxFilterInputStream::DoSeekInput(off_t pos
, wxSeekMode mode
)
526 return m_parent_i_stream
->SeekI(pos
, mode
);
529 off_t
wxFilterInputStream::DoTellInput() const
531 return m_parent_i_stream
->TellI();
535 // ----------------------------------------------------------------------------
536 // wxFilterOutputStream
537 // ----------------------------------------------------------------------------
538 wxFilterOutputStream::wxFilterOutputStream(wxOutputStream
& stream
)
539 : wxOutputStream(NULL
)
541 m_parent_o_stream
= &stream
;
542 m_o_streambuf
= stream
.OutputStreamBuffer();
545 wxFilterOutputStream::~wxFilterOutputStream()
549 size_t wxFilterOutputStream::DoWrite(const void *buffer
, size_t size
)
551 return m_parent_o_stream
->Write(buffer
, size
).LastWrite();
554 off_t
wxFilterOutputStream::DoSeekOutput(off_t pos
, wxSeekMode mode
)
556 return m_parent_o_stream
->SeekO(pos
, mode
);
559 off_t
wxFilterOutputStream::DoTellOutput() const
561 return m_parent_o_stream
->TellO();
564 // ----------------------------------------------------------------------------
565 // Some IOManip function
566 // ----------------------------------------------------------------------------
568 wxOutputStream
& wxEndL(wxOutputStream
& stream
)
571 return stream
.Write("\r\n", 2);
573 return stream
.Write("\n", 1);