]> git.saurik.com Git - wxWidgets.git/blame - src/common/stream.cpp
remove unnecessary include of wx/cairo.h
[wxWidgets.git] / src / common / stream.cpp
CommitLineData
3d4c6a21 1/////////////////////////////////////////////////////////////////////////////
49e399d8 2// Name: src/common/stream.cpp
3d4c6a21
GL
3// Purpose: wxStream base classes
4// Author: Guilhem Lavaux
49e399d8
VZ
5// Modified by: VZ (23.11.00) to fix realloc()ing new[]ed memory,
6// general code review
3d4c6a21
GL
7// Created: 11/07/98
8// RCS-ID: $Id$
9// Copyright: (c) Guilhem Lavaux
65571936 10// Licence: wxWindows licence
3d4c6a21
GL
11/////////////////////////////////////////////////////////////////////////////
12
49e399d8
VZ
13// ============================================================================
14// declarations
15// ============================================================================
16
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
47fc03d2 20
db138a4c
JS
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
ce4169a4
RR
23
24#ifdef __BORLANDC__
8898456d 25 #pragma hdrstop
ce4169a4
RR
26#endif
27
8898456d
WS
28#if wxUSE_STREAMS
29
530ecef0
WS
30#include "wx/stream.h"
31
ce4169a4 32#ifndef WX_PRECOMP
8898456d 33 #include "wx/log.h"
ce4169a4
RR
34#endif
35
6d44bf31 36#include <ctype.h>
3096bd2f 37#include "wx/datstrm.h"
733b8ed3 38#include "wx/textfile.h"
f5ef4d69 39#include "wx/scopeguard.h"
db138a4c 40
49e399d8
VZ
41// ----------------------------------------------------------------------------
42// constants
43// ----------------------------------------------------------------------------
44
45// the temporary buffer size used when copying from stream to stream
2b5f62a0 46#define BUF_TEMP_SIZE 4096
8ef6a930 47
49e399d8
VZ
48// ============================================================================
49// implementation
50// ============================================================================
51
6d44bf31
GL
52// ----------------------------------------------------------------------------
53// wxStreamBuffer
54// ----------------------------------------------------------------------------
55
49e399d8
VZ
56void wxStreamBuffer::SetError(wxStreamError err)
57{
2b5f62a0 58 if ( m_stream && m_stream->m_lasterror == wxSTREAM_NO_ERROR )
49e399d8
VZ
59 m_stream->m_lasterror = err;
60}
61
62void wxStreamBuffer::InitBuffer()
63{
64 m_buffer_start =
65 m_buffer_end =
66 m_buffer_pos = NULL;
49e399d8 67
d4b45f48 68 // if we are going to allocate the buffer, we should free it later as well
d775fa82 69 m_destroybuf = true;
49e399d8
VZ
70}
71
72void wxStreamBuffer::Init()
73{
74 InitBuffer();
75
d775fa82 76 m_fixed = true;
49e399d8 77}
926c550d 78
f42c1512 79void wxStreamBuffer::InitWithStream(wxStreamBase& stream, BufMode mode)
6d44bf31 80{
a2bd1fbb
VZ
81 Init();
82
f42c1512 83 m_stream = &stream;
49e399d8
VZ
84 m_mode = mode;
85
f42c1512 86 m_flushable = true;
6d44bf31
GL
87}
88
f42c1512 89wxStreamBuffer::wxStreamBuffer(BufMode mode)
84b46c35 90{
a2bd1fbb
VZ
91 Init();
92
f42c1512 93 m_stream = NULL;
49e399d8
VZ
94 m_mode = mode;
95
f42c1512 96 m_flushable = false;
84b46c35
GL
97}
98
99wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer& buffer)
100{
2b5f62a0 101 // doing this has big chances to lead to a crash when the source buffer is
49e399d8 102 // destroyed (otherwise assume the caller knows what he does)
2b5f62a0 103 wxASSERT_MSG( !buffer.m_destroybuf,
9a83f860 104 wxT("it's a bad idea to copy this buffer") );
49e399d8
VZ
105
106 m_buffer_start = buffer.m_buffer_start;
107 m_buffer_end = buffer.m_buffer_end;
108 m_buffer_pos = buffer.m_buffer_pos;
49e399d8
VZ
109 m_fixed = buffer.m_fixed;
110 m_flushable = buffer.m_flushable;
111 m_stream = buffer.m_stream;
112 m_mode = buffer.m_mode;
d775fa82 113 m_destroybuf = false;
84b46c35
GL
114}
115
49e399d8 116void wxStreamBuffer::FreeBuffer()
6d44bf31 117{
49e399d8 118 if ( m_destroybuf )
0d2ef7a8 119 {
49e399d8 120 free(m_buffer_start);
0d2ef7a8
VZ
121 m_buffer_start = NULL;
122 }
6d44bf31
GL
123}
124
49e399d8 125wxStreamBuffer::~wxStreamBuffer()
6d44bf31 126{
49e399d8 127 FreeBuffer();
6d44bf31
GL
128}
129
47fc03d2
VZ
130wxInputStream *wxStreamBuffer::GetInputStream() const
131{
132 return m_mode == write ? NULL : (wxInputStream *)m_stream;
133}
134
135wxOutputStream *wxStreamBuffer::GetOutputStream() const
136{
137 return m_mode == read ? NULL : (wxOutputStream *)m_stream;
138}
139
49e399d8
VZ
140void wxStreamBuffer::SetBufferIO(void *buffer_start,
141 void *buffer_end,
142 bool takeOwnership)
67c8c225
VZ
143{
144 SetBufferIO(buffer_start, (char *)buffer_end - (char *)buffer_start,
145 takeOwnership);
146}
147
148void wxStreamBuffer::SetBufferIO(void *start,
149 size_t len,
150 bool takeOwnership)
6d44bf31 151{
49e399d8
VZ
152 // start by freeing the old buffer
153 FreeBuffer();
885ee235 154
67c8c225
VZ
155 m_buffer_start = (char *)start;
156 m_buffer_end = m_buffer_start + len;
6d44bf31 157
49e399d8 158 // if we own it, we free it
421db2ad 159 m_destroybuf = takeOwnership;
49e399d8
VZ
160
161 ResetBuffer();
162}
6d44bf31 163
49e399d8
VZ
164void wxStreamBuffer::SetBufferIO(size_t bufsize)
165{
49e399d8
VZ
166 if ( bufsize )
167 {
0d2ef7a8 168 // this will free the old buffer and allocate the new one
d775fa82 169 SetBufferIO(malloc(bufsize), bufsize, true /* take ownership */);
49e399d8
VZ
170 }
171 else // no buffer size => no buffer
172 {
0d2ef7a8
VZ
173 // still free the old one
174 FreeBuffer();
49e399d8
VZ
175 InitBuffer();
176 }
6d44bf31
GL
177}
178
179void wxStreamBuffer::ResetBuffer()
180{
2b5f62a0
VZ
181 if ( m_stream )
182 {
183 m_stream->Reset();
184 m_stream->m_lastcount = 0;
185 }
49e399d8 186
2b5f62a0
VZ
187 m_buffer_pos = m_mode == read && m_flushable
188 ? m_buffer_end
189 : m_buffer_start;
6d44bf31
GL
190}
191
df18cc7a
VZ
192void wxStreamBuffer::Truncate()
193{
194 size_t new_size = m_buffer_pos - m_buffer_start;
b23bc769 195 if ( m_buffer_pos == m_buffer_end )
df18cc7a
VZ
196 return;
197
198 if ( !new_size )
199 {
200 FreeBuffer();
201 InitBuffer();
202 return;
203 }
204
205 char *new_start = (char *)realloc(m_buffer_start, new_size);
9a83f860 206 wxCHECK_RET( new_size, wxT("shrinking buffer shouldn't fail") );
df18cc7a
VZ
207
208 m_buffer_start = new_start;
b23bc769 209 m_buffer_end = m_buffer_start + new_size;
df18cc7a
VZ
210 m_buffer_pos = m_buffer_end;
211}
212
49e399d8 213// fill the buffer with as much data as possible (only for read buffers)
75ed1d15
GL
214bool wxStreamBuffer::FillBuffer()
215{
47fc03d2
VZ
216 wxInputStream *inStream = GetInputStream();
217
d775fa82 218 // It's legal to have no stream, so we don't complain about it just return false
2b5f62a0 219 if ( !inStream )
d775fa82 220 return false;
75ed1d15 221
b23bc769 222 size_t count = inStream->OnSysRead(GetBufferStart(), GetBufferSize());
49e399d8 223 if ( !count )
d775fa82 224 return false;
75ed1d15 225
49e399d8
VZ
226 m_buffer_end = m_buffer_start + count;
227 m_buffer_pos = m_buffer_start;
228
d775fa82 229 return true;
75ed1d15
GL
230}
231
49e399d8 232// write the buffer contents to the stream (only for write buffers)
75ed1d15
GL
233bool wxStreamBuffer::FlushBuffer()
234{
9a83f860 235 wxCHECK_MSG( m_flushable, false, wxT("can't flush this buffer") );
75ed1d15 236
49e399d8
VZ
237 // FIXME: what is this check for? (VZ)
238 if ( m_buffer_pos == m_buffer_start )
d775fa82 239 return false;
75ed1d15 240
47fc03d2
VZ
241 wxOutputStream *outStream = GetOutputStream();
242
9a83f860 243 wxCHECK_MSG( outStream, false, wxT("should have a stream in wxStreamBuffer") );
49e399d8
VZ
244
245 size_t current = m_buffer_pos - m_buffer_start;
47fc03d2 246 size_t count = outStream->OnSysWrite(m_buffer_start, current);
49e399d8 247 if ( count != current )
d775fa82 248 return false;
49e399d8
VZ
249
250 m_buffer_pos = m_buffer_start;
75ed1d15 251
d775fa82 252 return true;
75ed1d15
GL
253}
254
49e399d8
VZ
255size_t wxStreamBuffer::GetDataLeft()
256{
257 /* Why is this done? RR. */
258 if ( m_buffer_pos == m_buffer_end && m_flushable)
259 FillBuffer();
260
261 return GetBytesLeft();
262}
263
264// copy up to size bytes from our buffer into the provided one
75ed1d15
GL
265void wxStreamBuffer::GetFromBuffer(void *buffer, size_t size)
266{
49e399d8
VZ
267 // don't get more bytes than left in the buffer
268 size_t left = GetBytesLeft();
75ed1d15 269
49e399d8
VZ
270 if ( size > left )
271 size = left;
75ed1d15 272
49e399d8
VZ
273 memcpy(buffer, m_buffer_pos, size);
274 m_buffer_pos += size;
75ed1d15
GL
275}
276
49e399d8 277// copy the contents of the provided buffer into this one
75ed1d15
GL
278void wxStreamBuffer::PutToBuffer(const void *buffer, size_t size)
279{
49e399d8 280 size_t left = GetBytesLeft();
2b5f62a0 281
49e399d8
VZ
282 if ( size > left )
283 {
284 if ( m_fixed )
285 {
286 // we can't realloc the buffer, so just copy what we can
287 size = left;
288 }
289 else // !m_fixed
290 {
291 // realloc the buffer to have enough space for the data
b23bc769 292 if ( m_buffer_pos + size > m_buffer_end )
49e399d8 293 {
b23bc769
VZ
294 size_t delta = m_buffer_pos - m_buffer_start;
295 size_t new_size = delta + size;
296
297 char *startOld = m_buffer_start;
298 m_buffer_start = (char *)realloc(m_buffer_start, new_size);
299 if ( !m_buffer_start )
300 {
301 // don't leak memory if realloc() failed
302 m_buffer_start = startOld;
303
304 // what else can we do?
305 return;
306 }
307
308 // adjust the pointers invalidated by realloc()
309 m_buffer_pos = m_buffer_start + delta;
310 m_buffer_end = m_buffer_start + new_size;
311 } // else: the buffer is big enough
49e399d8 312 }
56dc1ffd 313 }
49e399d8
VZ
314
315 memcpy(m_buffer_pos, buffer, size);
316 m_buffer_pos += size;
75ed1d15
GL
317}
318
84b46c35
GL
319void wxStreamBuffer::PutChar(char c)
320{
47fc03d2
VZ
321 wxOutputStream *outStream = GetOutputStream();
322
9a83f860 323 wxCHECK_RET( outStream, wxT("should have a stream in wxStreamBuffer") );
84b46c35 324
49e399d8
VZ
325 // if we don't have buffer at all, just forward this call to the stream,
326 if ( !HasBuffer() )
327 {
2b5f62a0 328 outStream->OnSysWrite(&c, sizeof(c));
49e399d8
VZ
329 }
330 else
331 {
332 // otherwise check we have enough space left
333 if ( !GetDataLeft() && !FlushBuffer() )
334 {
335 // we don't
2b5f62a0 336 SetError(wxSTREAM_WRITE_ERROR);
49e399d8
VZ
337 }
338 else
339 {
2b5f62a0 340 PutToBuffer(&c, sizeof(c));
49e399d8
VZ
341 m_stream->m_lastcount = 1;
342 }
343 }
84b46c35
GL
344}
345
6319afe3
GL
346char wxStreamBuffer::Peek()
347{
49e399d8 348 wxCHECK_MSG( m_stream && HasBuffer(), 0,
9a83f860 349 wxT("should have the stream and the buffer in wxStreamBuffer") );
6319afe3 350
49e399d8
VZ
351 if ( !GetDataLeft() )
352 {
2b5f62a0 353 SetError(wxSTREAM_READ_ERROR);
49e399d8
VZ
354 return 0;
355 }
6319afe3 356
49e399d8 357 char c;
2b5f62a0 358 GetFromBuffer(&c, sizeof(c));
49e399d8 359 m_buffer_pos--;
6319afe3 360
49e399d8 361 return c;
6319afe3
GL
362}
363
84b46c35
GL
364char wxStreamBuffer::GetChar()
365{
47fc03d2
VZ
366 wxInputStream *inStream = GetInputStream();
367
9a83f860 368 wxCHECK_MSG( inStream, 0, wxT("should have a stream in wxStreamBuffer") );
84b46c35 369
49e399d8
VZ
370 char c;
371 if ( !HasBuffer() )
372 {
2b5f62a0 373 inStream->OnSysRead(&c, sizeof(c));
49e399d8
VZ
374 }
375 else
376 {
377 if ( !GetDataLeft() )
378 {
2b5f62a0 379 SetError(wxSTREAM_READ_ERROR);
49e399d8
VZ
380 c = 0;
381 }
382 else
383 {
2b5f62a0 384 GetFromBuffer(&c, sizeof(c));
49e399d8
VZ
385 m_stream->m_lastcount = 1;
386 }
387 }
84b46c35 388
84b46c35 389 return c;
84b46c35
GL
390}
391
8ef6a930 392size_t wxStreamBuffer::Read(void *buffer, size_t size)
6d44bf31 393{
9a83f860 394 wxASSERT_MSG( buffer, wxT("Warning: Null pointer is about to be used") );
8913c40c
RR
395
396 /* Clear buffer first */
397 memset(buffer, 0x00, size);
398
49e399d8 399 // lasterror is reset before all new IO calls
2b5f62a0
VZ
400 if ( m_stream )
401 m_stream->Reset();
49e399d8 402
15dc68b6 403 size_t readBytes;
49e399d8
VZ
404 if ( !HasBuffer() )
405 {
2b5f62a0
VZ
406 wxInputStream *inStream = GetInputStream();
407
9a83f860 408 wxCHECK_MSG( inStream, 0, wxT("should have a stream in wxStreamBuffer") );
2b5f62a0 409
15dc68b6 410 readBytes = inStream->OnSysRead(buffer, size);
49e399d8
VZ
411 }
412 else // we have a buffer, use it
413 {
414 size_t orig_size = size;
415
416 while ( size > 0 )
417 {
418 size_t left = GetDataLeft();
419
420 // if the requested number of bytes if greater than the buffer
421 // size, read data in chunks
422 if ( size > left )
423 {
424 GetFromBuffer(buffer, left);
425 size -= left;
426 buffer = (char *)buffer + left;
427
428 if ( !FillBuffer() )
429 {
2b5f62a0 430 SetError(wxSTREAM_EOF);
49e399d8
VZ
431 break;
432 }
433 }
434 else // otherwise just do it in one gulp
435 {
436 GetFromBuffer(buffer, size);
437 size = 0;
438 }
439 }
440
15dc68b6 441 readBytes = orig_size - size;
6d44bf31 442 }
49e399d8 443
2b5f62a0 444 if ( m_stream )
15dc68b6 445 m_stream->m_lastcount = readBytes;
2b5f62a0 446
15dc68b6 447 return readBytes;
6d44bf31
GL
448}
449
49e399d8
VZ
450// this should really be called "Copy()"
451size_t wxStreamBuffer::Read(wxStreamBuffer *dbuf)
8ef6a930 452{
9a83f860 453 wxCHECK_MSG( m_mode != write, 0, wxT("can't read from this buffer") );
8ef6a930 454
49e399d8
VZ
455 char buf[BUF_TEMP_SIZE];
456 size_t nRead,
457 total = 0;
7f42cff1 458
49e399d8
VZ
459 do
460 {
c15b71ae 461 nRead = Read(buf, WXSIZEOF(buf));
49e399d8
VZ
462 if ( nRead )
463 {
464 nRead = dbuf->Write(buf, nRead);
465 total += nRead;
466 }
467 }
468 while ( nRead );
469
470 return total;
8ef6a930
GL
471}
472
473size_t wxStreamBuffer::Write(const void *buffer, size_t size)
6d44bf31 474{
9a83f860 475 wxASSERT_MSG( buffer, wxT("Warning: Null pointer is about to be send") );
8913c40c 476
2b5f62a0
VZ
477 if (m_stream)
478 {
479 // lasterror is reset before all new IO calls
480 m_stream->Reset();
481 }
6d44bf31 482
999836aa 483 size_t ret;
6d44bf31 484
49e399d8
VZ
485 if ( !HasBuffer() && m_fixed )
486 {
2b5f62a0
VZ
487 wxOutputStream *outStream = GetOutputStream();
488
9a83f860 489 wxCHECK_MSG( outStream, 0, wxT("should have a stream in wxStreamBuffer") );
2b5f62a0 490
49e399d8 491 // no buffer, just forward the call to the stream
2b5f62a0 492 ret = outStream->OnSysWrite(buffer, size);
49e399d8
VZ
493 }
494 else // we [may] have a buffer, use it
495 {
496 size_t orig_size = size;
497
498 while ( size > 0 )
499 {
500 size_t left = GetBytesLeft();
501
502 // if the buffer is too large to fit in the stream buffer, split
503 // it in smaller parts
504 //
505 // NB: If stream buffer isn't fixed (as for wxMemoryOutputStream),
506 // we always go to the second case.
507 //
508 // FIXME: fine, but if it fails we should (re)try writing it by
509 // chunks as this will (hopefully) always work (VZ)
2b5f62a0 510
49e399d8
VZ
511 if ( size > left && m_fixed )
512 {
513 PutToBuffer(buffer, left);
514 size -= left;
515 buffer = (char *)buffer + left;
516
517 if ( !FlushBuffer() )
518 {
2b5f62a0 519 SetError(wxSTREAM_WRITE_ERROR);
49e399d8
VZ
520
521 break;
522 }
523
524 m_buffer_pos = m_buffer_start;
525 }
526 else // we can do it in one gulp
527 {
528 PutToBuffer(buffer, size);
529 size = 0;
530 }
531 }
532
2b5f62a0 533 ret = orig_size - size;
6d44bf31 534 }
49e399d8 535
2b5f62a0
VZ
536 if (m_stream)
537 {
538 // i am not entirely sure what we do this for
539 m_stream->m_lastcount = ret;
540 }
541
542 return ret;
8ef6a930
GL
543}
544
545size_t wxStreamBuffer::Write(wxStreamBuffer *sbuf)
546{
9a83f860
VZ
547 wxCHECK_MSG( m_mode != read, 0, wxT("can't write to this buffer") );
548 wxCHECK_MSG( sbuf->m_mode != write, 0, wxT("can't read from that buffer") );
8ef6a930 549
49e399d8
VZ
550 char buf[BUF_TEMP_SIZE];
551 size_t nWrite,
552 total = 0;
7f42cff1 553
49e399d8
VZ
554 do
555 {
556 size_t nRead = sbuf->Read(buf, WXSIZEOF(buf));
557 if ( nRead )
558 {
559 nWrite = Write(buf, nRead);
560 if ( nWrite < nRead )
561 {
562 // put back data we couldn't copy
563 wxInputStream *in_stream = (wxInputStream *)sbuf->GetStream();
564
565 in_stream->Ungetch(buf + nWrite, nRead - nWrite);
566 }
567
568 total += nWrite;
569 }
570 else
571 {
572 nWrite = 0;
573 }
574 }
575 while ( nWrite == WXSIZEOF(buf) );
fae05df5 576
49e399d8 577 return total;
75ed1d15
GL
578}
579
4004775e 580wxFileOffset wxStreamBuffer::Seek(wxFileOffset pos, wxSeekMode mode)
75ed1d15 581{
4004775e 582 wxFileOffset ret_off, diff;
75ed1d15 583
4004775e 584 wxFileOffset last_access = GetLastAccess();
75ed1d15 585
49e399d8
VZ
586 if ( !m_flushable )
587 {
588 switch (mode)
589 {
590 case wxFromStart:
591 diff = pos;
592 break;
593
594 case wxFromCurrent:
595 diff = pos + GetIntPosition();
596 break;
597
598 case wxFromEnd:
599 diff = pos + last_access;
600 break;
601
602 default:
9a83f860 603 wxFAIL_MSG( wxT("invalid seek mode") );
49e399d8 604
30984dea 605 return wxInvalidOffset;
49e399d8
VZ
606 }
607 if (diff < 0 || diff > last_access)
30984dea 608 return wxInvalidOffset;
17a1ebd1 609 size_t int_diff = wx_truncate_cast(size_t, diff);
287d71d9
WS
610 wxCHECK_MSG( (wxFileOffset)int_diff == diff, wxInvalidOffset, wxT("huge file not supported") );
611 SetIntPosition(int_diff);
49e399d8 612 return diff;
d984207c 613 }
49e399d8
VZ
614
615 switch ( mode )
616 {
617 case wxFromStart:
618 // We'll try to compute an internal position later ...
619 ret_off = m_stream->OnSysSeek(pos, wxFromStart);
620 ResetBuffer();
621 return ret_off;
622
623 case wxFromCurrent:
624 diff = pos + GetIntPosition();
625
626 if ( (diff > last_access) || (diff < 0) )
627 {
628 // We must take into account the fact that we have read
629 // something previously.
630 ret_off = m_stream->OnSysSeek(diff-last_access, wxFromCurrent);
631 ResetBuffer();
632 return ret_off;
633 }
634 else
635 {
17a1ebd1 636 size_t int_diff = wx_truncate_cast(size_t, diff);
287d71d9
WS
637 wxCHECK_MSG( (wxFileOffset)int_diff == diff, wxInvalidOffset, wxT("huge file not supported") );
638 SetIntPosition(int_diff);
c81b7d7d 639 return diff;
49e399d8
VZ
640 }
641
642 case wxFromEnd:
643 // Hard to compute: always seek to the requested position.
644 ret_off = m_stream->OnSysSeek(pos, wxFromEnd);
645 ResetBuffer();
646 return ret_off;
75ed1d15 647 }
49e399d8 648
30984dea 649 return wxInvalidOffset;
75ed1d15
GL
650}
651
4004775e 652wxFileOffset wxStreamBuffer::Tell() const
75ed1d15 653{
30984dea 654 wxFileOffset pos;
915a955c 655
2b5f62a0
VZ
656 // ask the stream for position if we have a real one
657 if ( m_stream )
915a955c
VZ
658 {
659 pos = m_stream->OnSysTell();
660 if ( pos == wxInvalidOffset )
30984dea 661 return wxInvalidOffset;
915a955c
VZ
662 }
663 else // no associated stream
664 {
665 pos = 0;
666 }
c7a9fa36
RR
667
668 pos += GetIntPosition();
cd6ce4a9 669
49e399d8 670 if ( m_mode == read && m_flushable )
c7a9fa36 671 pos -= GetLastAccess();
cd6ce4a9 672
c7a9fa36 673 return pos;
75ed1d15
GL
674}
675
75ed1d15
GL
676// ----------------------------------------------------------------------------
677// wxStreamBase
678// ----------------------------------------------------------------------------
679
6285e36b
VZ
680IMPLEMENT_ABSTRACT_CLASS(wxStreamBase, wxObject)
681
75ed1d15
GL
682wxStreamBase::wxStreamBase()
683{
2b5f62a0 684 m_lasterror = wxSTREAM_NO_ERROR;
c7a9fa36 685 m_lastcount = 0;
75ed1d15
GL
686}
687
688wxStreamBase::~wxStreamBase()
689{
690}
691
588066b7
VZ
692size_t wxStreamBase::GetSize() const
693{
694 wxFileOffset length = GetLength();
653752be 695 if ( length == (wxFileOffset)wxInvalidOffset )
17a1ebd1
VZ
696 return 0;
697
698 const size_t len = wx_truncate_cast(size_t, length);
9a83f860 699 wxASSERT_MSG( len == length + size_t(0), wxT("large files not supported") );
17a1ebd1
VZ
700
701 return len;
588066b7
VZ
702}
703
4004775e 704wxFileOffset wxStreamBase::OnSysSeek(wxFileOffset WXUNUSED(seek), wxSeekMode WXUNUSED(mode))
b9138710 705{
30984dea 706 return wxInvalidOffset;
b9138710
VZ
707}
708
4004775e 709wxFileOffset wxStreamBase::OnSysTell() const
b9138710 710{
30984dea 711 return wxInvalidOffset;
b9138710
VZ
712}
713
1678ad78
GL
714// ----------------------------------------------------------------------------
715// wxInputStream
716// ----------------------------------------------------------------------------
717
6285e36b
VZ
718IMPLEMENT_ABSTRACT_CLASS(wxInputStream, wxStreamBase)
719
3d4c6a21 720wxInputStream::wxInputStream()
3d4c6a21 721{
49e399d8
VZ
722 m_wback = NULL;
723 m_wbacksize =
724 m_wbackcur = 0;
6d44bf31
GL
725}
726
fae05df5 727wxInputStream::~wxInputStream()
6d44bf31 728{
fae05df5 729 free(m_wback);
3d4c6a21
GL
730}
731
2b5f62a0 732bool wxInputStream::CanRead() const
47fc03d2 733{
2b5f62a0
VZ
734 // we don't know if there is anything to read or not and by default we
735 // prefer to be optimistic and try to read data unless we know for sure
736 // there is no more of it
737 return m_lasterror != wxSTREAM_EOF;
47fc03d2
VZ
738}
739
cd6ce4a9
VZ
740bool wxInputStream::Eof() const
741{
2b5f62a0
VZ
742 // the only way the base class can know we're at EOF is when we'd already
743 // tried to read beyond it in which case last error is set accordingly
744 return GetLastError() == wxSTREAM_EOF;
cd6ce4a9
VZ
745}
746
fae05df5 747char *wxInputStream::AllocSpaceWBack(size_t needed_size)
3d4c6a21 748{
49e399d8 749 // get number of bytes left from previous wback buffer
c7a9fa36 750 size_t toget = m_wbacksize - m_wbackcur;
fae05df5 751
49e399d8
VZ
752 // allocate a buffer large enough to hold prev + new data
753 char *temp_b = (char *)malloc(needed_size + toget);
fae05df5 754
c7a9fa36
RR
755 if (!temp_b)
756 return NULL;
fae05df5 757
adc35078 758 // copy previous data (and free old buffer) if needed
c7a9fa36
RR
759 if (m_wback)
760 {
761 memmove(temp_b + needed_size, m_wback + m_wbackcur, toget);
762 free(m_wback);
763 }
5ac8158a 764
adc35078 765 // done
c7a9fa36
RR
766 m_wback = temp_b;
767 m_wbackcur = 0;
768 m_wbacksize = needed_size + toget;
783ff666 769
49e399d8 770 return m_wback;
6d44bf31
GL
771}
772
2b5f62a0 773size_t wxInputStream::GetWBack(void *buf, size_t size)
6d44bf31 774{
9a83f860 775 wxASSERT_MSG( buf, wxT("Warning: Null pointer is about to be used") );
8913c40c
RR
776
777 /* Clear buffer first */
778 memset(buf, 0x00, size);
779
c7a9fa36
RR
780 if (!m_wback)
781 return 0;
a324a7bc 782
e9f69291
VZ
783 // how many bytes do we have in the buffer?
784 size_t toget = m_wbacksize - m_wbackcur;
785
2b5f62a0 786 if ( size < toget )
e9f69291
VZ
787 {
788 // we won't read everything
2b5f62a0 789 toget = size;
e9f69291 790 }
fae05df5 791
2b5f62a0 792 // copy the data from the cache
e9f69291 793 memcpy(buf, m_wback + m_wbackcur, toget);
fae05df5 794
49e399d8 795 m_wbackcur += toget;
e9f69291 796 if ( m_wbackcur == m_wbacksize )
c7a9fa36 797 {
e9f69291 798 // TODO: should we really free it here all the time? maybe keep it?
c7a9fa36 799 free(m_wback);
49e399d8 800 m_wback = NULL;
c7a9fa36
RR
801 m_wbacksize = 0;
802 m_wbackcur = 0;
803 }
cd6ce4a9 804
e9f69291 805 // return the number of bytes copied
49e399d8 806 return toget;
6d44bf31
GL
807}
808
8f7173ab 809size_t wxInputStream::Ungetch(const void *buf, size_t bufsize)
fae05df5 810{
9a83f860 811 wxASSERT_MSG( buf, wxT("Warning: Null pointer is about to be used in Ungetch()") );
8913c40c 812
42a3aedb
VZ
813 if ( m_lasterror != wxSTREAM_NO_ERROR && m_lasterror != wxSTREAM_EOF )
814 {
815 // can't operate on this stream until the error is cleared
816 return 0;
817 }
818
c7a9fa36
RR
819 char *ptrback = AllocSpaceWBack(bufsize);
820 if (!ptrback)
821 return 0;
cd6ce4a9 822
d775fa82 823 // Eof() shouldn't return true any longer
42a3aedb
VZ
824 if ( m_lasterror == wxSTREAM_EOF )
825 m_lasterror = wxSTREAM_NO_ERROR;
826
c7a9fa36
RR
827 memcpy(ptrback, buf, bufsize);
828 return bufsize;
fae05df5
GL
829}
830
831bool wxInputStream::Ungetch(char c)
1e3eca9d 832{
2b5f62a0 833 return Ungetch(&c, sizeof(c)) != 0;
fae05df5
GL
834}
835
6ea48c51 836int wxInputStream::GetC()
fae05df5 837{
6ea48c51 838 unsigned char c;
2b5f62a0 839 Read(&c, sizeof(c));
6ea48c51 840 return LastRead() ? c : wxEOF;
1e3eca9d
GL
841}
842
49e399d8 843wxInputStream& wxInputStream::Read(void *buf, size_t size)
6d44bf31 844{
9a83f860 845 wxASSERT_MSG( buf, wxT("Warning: Null pointer is about to be read") );
8913c40c 846
9a76510b
VZ
847 char *p = (char *)buf;
848 m_lastcount = 0;
849
850 size_t read = GetWBack(buf, size);
851 for ( ;; )
c7a9fa36 852 {
9a76510b
VZ
853 size -= read;
854 m_lastcount += read;
855 p += read;
856
857 if ( !size )
858 {
859 // we read the requested amount of data
860 break;
861 }
862
2b5f62a0
VZ
863 if ( p != buf && !CanRead() )
864 {
865 // we have already read something and we would block in OnSysRead()
866 // now: don't do it but return immediately
867 break;
868 }
869
9b11cb6e 870 read = OnSysRead(p, size);
9a76510b
VZ
871 if ( !read )
872 {
873 // no more data available
874 break;
875 }
c7a9fa36 876 }
fae05df5 877
c7a9fa36 878 return *this;
3d4c6a21
GL
879}
880
75ed1d15
GL
881char wxInputStream::Peek()
882{
c7a9fa36 883 char c;
2b5f62a0
VZ
884 Read(&c, sizeof(c));
885 if (m_lasterror == wxSTREAM_NO_ERROR)
c7a9fa36
RR
886 {
887 Ungetch(c);
888 return c;
889 }
cd6ce4a9 890
c7a9fa36 891 return 0;
75ed1d15
GL
892}
893
3d4c6a21
GL
894wxInputStream& wxInputStream::Read(wxOutputStream& stream_out)
895{
68f14620 896 size_t lastcount = 0;
cd6ce4a9 897 char buf[BUF_TEMP_SIZE];
3d4c6a21 898
2b5f62a0 899 for ( ;; )
c7a9fa36 900 {
2b5f62a0
VZ
901 size_t bytes_read = Read(buf, WXSIZEOF(buf)).LastRead();
902 if ( !bytes_read )
903 break;
904
905 if ( stream_out.Write(buf, bytes_read).LastWrite() != bytes_read )
906 break;
68f14620
VZ
907
908 lastcount += bytes_read;
c7a9fa36 909 }
2b5f62a0 910
68f14620
VZ
911 m_lastcount = lastcount;
912
c7a9fa36 913 return *this;
3d4c6a21
GL
914}
915
4004775e 916wxFileOffset wxInputStream::SeekI(wxFileOffset pos, wxSeekMode mode)
75ed1d15 917{
adc35078
RR
918 // RR: This code is duplicated in wxBufferedInputStream. This is
919 // not really a good design, but buffered stream are different
8faef7cc 920 // from all others in that they handle two stream-related objects:
adc35078
RR
921 // the stream buffer and parent stream.
922
923 // I don't know whether it should be put as well in wxFileInputStream::OnSysSeek
cd6ce4a9 924 if (m_lasterror==wxSTREAM_EOF)
2b5f62a0 925 m_lasterror=wxSTREAM_NO_ERROR;
c7a9fa36 926
8faef7cc
FM
927 // avoid unnecessary seek operations (optimization)
928 wxFileOffset currentPos = TellI(), size = GetLength();
929 if ((mode == wxFromStart && currentPos == pos) ||
930 (mode == wxFromCurrent && pos == 0) ||
931 (mode == wxFromEnd && size != wxInvalidOffset && currentPos == size-pos))
932 return currentPos;
933
934 if (!IsSeekable() && mode == wxFromCurrent && pos > 0)
935 {
936 // rather than seeking, we can just read data and discard it;
937 // this allows to forward-seek also non-seekable streams!
938 char buf[BUF_TEMP_SIZE];
939 size_t bytes_read;
940
941 // read chunks of BUF_TEMP_SIZE bytes until we reach the new position
942 for ( ; pos >= BUF_TEMP_SIZE; pos -= bytes_read)
943 {
944 bytes_read = Read(buf, WXSIZEOF(buf)).LastRead();
945 if ( m_lasterror != wxSTREAM_NO_ERROR )
946 return wxInvalidOffset;
03647350 947
8faef7cc
FM
948 wxASSERT(bytes_read == WXSIZEOF(buf));
949 }
03647350 950
8faef7cc
FM
951 // read the last 'pos' bytes
952 bytes_read = Read(buf, (size_t)pos).LastRead();
953 if ( m_lasterror != wxSTREAM_NO_ERROR )
954 return wxInvalidOffset;
03647350 955
8faef7cc 956 wxASSERT(bytes_read == (size_t)pos);
03647350 957
25e3f0c6 958 // we should now have sought to the right position...
8faef7cc
FM
959 return TellI();
960 }
961
adc35078
RR
962 /* RR: A call to SeekI() will automatically invalidate any previous
963 call to Ungetch(), otherwise it would be possible to SeekI() to
c7a9fa36 964 one position, unread some bytes there, SeekI() to another position
19da7237
GRG
965 and the data would be corrupted.
966
967 GRG: Could add code here to try to navigate within the wback
968 buffer if possible, but is it really needed? It would only work
969 when seeking in wxFromCurrent mode, else it would invalidate
adc35078 970 anyway... */
2b5f62a0 971
cd6ce4a9 972 if (m_wback)
c7a9fa36 973 {
adc35078 974 wxLogDebug( wxT("Seeking in stream which has data written back to it.") );
2b5f62a0 975
c7a9fa36 976 free(m_wback);
49e399d8 977 m_wback = NULL;
c7a9fa36
RR
978 m_wbacksize = 0;
979 m_wbackcur = 0;
980 }
fe8aa971 981
c7a9fa36 982 return OnSysSeek(pos, mode);
75ed1d15
GL
983}
984
4004775e 985wxFileOffset wxInputStream::TellI() const
75ed1d15 986{
30984dea 987 wxFileOffset pos = OnSysTell();
19da7237
GRG
988
989 if (pos != wxInvalidOffset)
990 pos -= (m_wbacksize - m_wbackcur);
991
992 return pos;
75ed1d15
GL
993}
994
1678ad78 995
fae05df5
GL
996// ----------------------------------------------------------------------------
997// wxOutputStream
998// ----------------------------------------------------------------------------
49e399d8 999
6285e36b
VZ
1000IMPLEMENT_ABSTRACT_CLASS(wxOutputStream, wxStreamBase)
1001
fae05df5 1002wxOutputStream::wxOutputStream()
1678ad78 1003{
1678ad78
GL
1004}
1005
fae05df5 1006wxOutputStream::~wxOutputStream()
123a7fdd 1007{
123a7fdd
GL
1008}
1009
47fc03d2
VZ
1010size_t wxOutputStream::OnSysWrite(const void * WXUNUSED(buffer),
1011 size_t WXUNUSED(bufsize))
1012{
1013 return 0;
1014}
1015
7513f9ff
GRG
1016void wxOutputStream::PutC(char c)
1017{
2b5f62a0 1018 Write(&c, sizeof(c));
7513f9ff
GRG
1019}
1020
fae05df5 1021wxOutputStream& wxOutputStream::Write(const void *buffer, size_t size)
1678ad78 1022{
c7a9fa36
RR
1023 m_lastcount = OnSysWrite(buffer, size);
1024 return *this;
1678ad78
GL
1025}
1026
fae05df5 1027wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in)
38830220 1028{
c7a9fa36
RR
1029 stream_in.Read(*this);
1030 return *this;
38830220
RR
1031}
1032
4004775e 1033wxFileOffset wxOutputStream::TellO() const
38830220 1034{
c7a9fa36 1035 return OnSysTell();
38830220
RR
1036}
1037
4004775e 1038wxFileOffset wxOutputStream::SeekO(wxFileOffset pos, wxSeekMode mode)
38830220 1039{
c7a9fa36 1040 return OnSysSeek(pos, mode);
38830220
RR
1041}
1042
fae05df5 1043void wxOutputStream::Sync()
1678ad78 1044{
1678ad78
GL
1045}
1046
123a7fdd 1047
e2acb9ae
RR
1048// ----------------------------------------------------------------------------
1049// wxCountingOutputStream
1050// ----------------------------------------------------------------------------
1051
6285e36b
VZ
1052IMPLEMENT_DYNAMIC_CLASS(wxCountingOutputStream, wxOutputStream)
1053
e2acb9ae 1054wxCountingOutputStream::wxCountingOutputStream ()
e2acb9ae 1055{
c7a9fa36 1056 m_currentPos = 0;
e2acb9ae
RR
1057}
1058
588066b7 1059wxFileOffset wxCountingOutputStream::GetLength() const
e2acb9ae 1060{
c7a9fa36 1061 return m_lastcount;
e2acb9ae
RR
1062}
1063
49e399d8
VZ
1064size_t wxCountingOutputStream::OnSysWrite(const void *WXUNUSED(buffer),
1065 size_t size)
e2acb9ae 1066{
c7a9fa36 1067 m_currentPos += size;
49e399d8
VZ
1068 if (m_currentPos > m_lastcount)
1069 m_lastcount = m_currentPos;
1070
c7a9fa36 1071 return m_currentPos;
e2acb9ae
RR
1072}
1073
4004775e 1074wxFileOffset wxCountingOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
e2acb9ae 1075{
17a1ebd1 1076 ssize_t new_pos = wx_truncate_cast(ssize_t, pos);
287d71d9 1077
49e399d8
VZ
1078 switch ( mode )
1079 {
1080 case wxFromStart:
287d71d9 1081 wxCHECK_MSG( (wxFileOffset)new_pos == pos, wxInvalidOffset, wxT("huge position not supported") );
49e399d8
VZ
1082 break;
1083
1084 case wxFromEnd:
287d71d9
WS
1085 new_pos = m_lastcount + new_pos;
1086 wxCHECK_MSG( (wxFileOffset)new_pos == (wxFileOffset)(m_lastcount + pos), wxInvalidOffset, wxT("huge position not supported") );
49e399d8
VZ
1087 break;
1088
1089 case wxFromCurrent:
287d71d9
WS
1090 new_pos = m_currentPos + new_pos;
1091 wxCHECK_MSG( (wxFileOffset)new_pos == (wxFileOffset)(m_currentPos + pos), wxInvalidOffset, wxT("huge position not supported") );
49e399d8
VZ
1092 break;
1093
1094 default:
9a83f860 1095 wxFAIL_MSG( wxT("invalid seek mode") );
30984dea 1096 return wxInvalidOffset;
49e399d8 1097 }
cd6ce4a9 1098
287d71d9
WS
1099 m_currentPos = new_pos;
1100
49e399d8
VZ
1101 if (m_currentPos > m_lastcount)
1102 m_lastcount = m_currentPos;
cd6ce4a9 1103
c7a9fa36 1104 return m_currentPos;
e2acb9ae
RR
1105}
1106
4004775e 1107wxFileOffset wxCountingOutputStream::OnSysTell() const
e2acb9ae 1108{
c7a9fa36 1109 return m_currentPos;
e2acb9ae 1110}
cd6ce4a9 1111
1678ad78 1112// ----------------------------------------------------------------------------
fae05df5 1113// wxFilterInputStream
1678ad78 1114// ----------------------------------------------------------------------------
e2acb9ae 1115
6285e36b
VZ
1116IMPLEMENT_ABSTRACT_CLASS(wxFilterInputStream, wxInputStream)
1117
fae05df5 1118wxFilterInputStream::wxFilterInputStream()
166c3ef0
MW
1119 : m_parent_i_stream(NULL),
1120 m_owns(false)
3d4c6a21 1121{
6d44bf31
GL
1122}
1123
fae05df5 1124wxFilterInputStream::wxFilterInputStream(wxInputStream& stream)
166c3ef0
MW
1125 : m_parent_i_stream(&stream),
1126 m_owns(false)
1127{
1128}
1129
1130wxFilterInputStream::wxFilterInputStream(wxInputStream *stream)
1131 : m_parent_i_stream(stream),
1132 m_owns(true)
6d44bf31 1133{
3d4c6a21
GL
1134}
1135
fae05df5 1136wxFilterInputStream::~wxFilterInputStream()
3d4c6a21 1137{
166c3ef0
MW
1138 if (m_owns)
1139 delete m_parent_i_stream;
6d44bf31
GL
1140}
1141
fae05df5
GL
1142// ----------------------------------------------------------------------------
1143// wxFilterOutputStream
1144// ----------------------------------------------------------------------------
49e399d8 1145
6285e36b
VZ
1146IMPLEMENT_ABSTRACT_CLASS(wxFilterOutputStream, wxOutputStream)
1147
fae05df5 1148wxFilterOutputStream::wxFilterOutputStream()
166c3ef0
MW
1149 : m_parent_o_stream(NULL),
1150 m_owns(false)
6d44bf31 1151{
3d4c6a21
GL
1152}
1153
fae05df5 1154wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream)
166c3ef0
MW
1155 : m_parent_o_stream(&stream),
1156 m_owns(false)
1157{
1158}
1159
1160wxFilterOutputStream::wxFilterOutputStream(wxOutputStream *stream)
1161 : m_parent_o_stream(stream),
1162 m_owns(true)
1163{
1164}
1165
1166bool wxFilterOutputStream::Close()
3d4c6a21 1167{
166c3ef0
MW
1168 if (m_parent_o_stream && m_owns)
1169 return m_parent_o_stream->Close();
1170 else
1171 return true;
6d44bf31
GL
1172}
1173
fae05df5 1174wxFilterOutputStream::~wxFilterOutputStream()
6d44bf31 1175{
166c3ef0
MW
1176 if (m_owns)
1177 delete m_parent_o_stream;
1178}
1179
1180// ----------------------------------------------------------------------------
58211774 1181// wxFilterClassFactoryBase
166c3ef0
MW
1182// ----------------------------------------------------------------------------
1183
58211774 1184IMPLEMENT_ABSTRACT_CLASS(wxFilterClassFactoryBase, wxObject)
166c3ef0 1185
58211774 1186wxString wxFilterClassFactoryBase::PopExtension(const wxString& location) const
166c3ef0 1187{
a8d2cf4e
MW
1188 return location.substr(0, FindExtension(location));
1189}
166c3ef0 1190
58211774 1191wxString::size_type wxFilterClassFactoryBase::FindExtension(
86501081 1192 const wxString& location) const
a8d2cf4e 1193{
489a164c 1194 for (const wxChar *const *p = GetProtocols(wxSTREAM_FILEEXT); *p; p++)
a8d2cf4e 1195 {
86501081
VS
1196 if ( location.EndsWith(*p) )
1197 return location.length() - wxStrlen(*p);
166c3ef0 1198 }
a8d2cf4e
MW
1199
1200 return wxString::npos;
1201}
1202
86501081 1203bool wxFilterClassFactoryBase::CanHandle(const wxString& protocol,
58211774 1204 wxStreamProtocolType type) const
a8d2cf4e 1205{
489a164c 1206 if (type == wxSTREAM_FILEEXT)
a8d2cf4e 1207 return FindExtension(protocol) != wxString::npos;
166c3ef0 1208 else
489a164c 1209 for (const wxChar *const *p = GetProtocols(type); *p; p++)
86501081 1210 if (protocol == *p)
166c3ef0 1211 return true;
166c3ef0
MW
1212
1213 return false;
1214}
1215
58211774
MW
1216// ----------------------------------------------------------------------------
1217// wxFilterClassFactory
1218// ----------------------------------------------------------------------------
1219
1220IMPLEMENT_ABSTRACT_CLASS(wxFilterClassFactory, wxFilterClassFactoryBase)
1221
1222wxFilterClassFactory *wxFilterClassFactory::sm_first = NULL;
1223
166c3ef0
MW
1224void wxFilterClassFactory::Remove()
1225{
1226 if (m_next != this)
1227 {
1228 wxFilterClassFactory **pp = &sm_first;
1229
1230 while (*pp != this)
1231 pp = &(*pp)->m_next;
1232
1233 *pp = m_next;
1234
1235 m_next = this;
1236 }
6d44bf31
GL
1237}
1238
fae05df5
GL
1239// ----------------------------------------------------------------------------
1240// wxBufferedInputStream
1241// ----------------------------------------------------------------------------
49e399d8 1242
f42c1512
VZ
1243namespace
1244{
1245
1246// helper function used for initializing the buffer used by
1247// wxBufferedInput/OutputStream: it simply returns the provided buffer if it's
1248// not NULL or creates a buffer of the given size otherwise
1249template <typename T>
1250wxStreamBuffer *
1251CreateBufferIfNeeded(T& stream, wxStreamBuffer *buffer, size_t bufsize = 1024)
1252{
36f062d3 1253 return buffer ? buffer : new wxStreamBuffer(bufsize, stream);
f42c1512
VZ
1254}
1255
1256} // anonymous namespace
1257
1258wxBufferedInputStream::wxBufferedInputStream(wxInputStream& stream,
47fc03d2 1259 wxStreamBuffer *buffer)
f42c1512 1260 : wxFilterInputStream(stream)
6d44bf31 1261{
f42c1512
VZ
1262 m_i_streambuf = CreateBufferIfNeeded(*this, buffer);
1263}
49e399d8 1264
f42c1512
VZ
1265wxBufferedInputStream::wxBufferedInputStream(wxInputStream& stream,
1266 size_t bufsize)
1267 : wxFilterInputStream(stream)
1268{
1269 m_i_streambuf = CreateBufferIfNeeded(*this, NULL, bufsize);
6d44bf31
GL
1270}
1271
fae05df5 1272wxBufferedInputStream::~wxBufferedInputStream()
6d44bf31 1273{
4004775e 1274 m_parent_i_stream->SeekI(-(wxFileOffset)m_i_streambuf->GetBytesLeft(),
1fb45475 1275 wxFromCurrent);
672cedf8 1276
c7a9fa36 1277 delete m_i_streambuf;
6d44bf31
GL
1278}
1279
6319afe3
GL
1280char wxBufferedInputStream::Peek()
1281{
c7a9fa36 1282 return m_i_streambuf->Peek();
6319afe3
GL
1283}
1284
49e399d8 1285wxInputStream& wxBufferedInputStream::Read(void *buf, size_t size)
1e3eca9d 1286{
e9f69291 1287 // reset the error flag
2b5f62a0 1288 Reset();
1e3eca9d 1289
e9f69291
VZ
1290 // first read from the already cached data
1291 m_lastcount = GetWBack(buf, size);
1292
1293 // do we have to read anything more?
1294 if ( m_lastcount < size )
c7a9fa36 1295 {
e9f69291
VZ
1296 size -= m_lastcount;
1297 buf = (char *)buf + m_lastcount;
1e3eca9d 1298
6c91ea6f
VZ
1299 // the call to wxStreamBuffer::Read() below may reset our m_lastcount
1300 // (but it also may not do it if the buffer is associated to another
1301 // existing stream and wasn't created by us), so save it
e9f69291
VZ
1302 size_t countOld = m_lastcount;
1303
6c91ea6f
VZ
1304 // the new count of the bytes read is the count of bytes read this time
1305 m_lastcount = m_i_streambuf->Read(buf, size);
e9f69291 1306
6c91ea6f 1307 // plus those we had read before
e9f69291
VZ
1308 m_lastcount += countOld;
1309 }
6d44bf31 1310
c7a9fa36 1311 return *this;
6d44bf31
GL
1312}
1313
4004775e 1314wxFileOffset wxBufferedInputStream::SeekI(wxFileOffset pos, wxSeekMode mode)
6d44bf31 1315{
adc35078
RR
1316 // RR: Look at wxInputStream for comments.
1317
1318 if (m_lasterror==wxSTREAM_EOF)
2b5f62a0 1319 Reset();
adc35078
RR
1320
1321 if (m_wback)
1322 {
1323 wxLogDebug( wxT("Seeking in stream which has data written back to it.") );
2b5f62a0 1324
adc35078
RR
1325 free(m_wback);
1326 m_wback = NULL;
1327 m_wbacksize = 0;
1328 m_wbackcur = 0;
1329 }
2b5f62a0 1330
c7a9fa36 1331 return m_i_streambuf->Seek(pos, mode);
6d44bf31
GL
1332}
1333
4004775e 1334wxFileOffset wxBufferedInputStream::TellI() const
6d44bf31 1335{
30984dea 1336 wxFileOffset pos = m_i_streambuf->Tell();
adc35078
RR
1337
1338 if (pos != wxInvalidOffset)
1339 pos -= (m_wbacksize - m_wbackcur);
2b5f62a0 1340
adc35078 1341 return pos;
38830220 1342}
6d44bf31 1343
fae05df5 1344size_t wxBufferedInputStream::OnSysRead(void *buffer, size_t bufsize)
38830220 1345{
c7a9fa36 1346 return m_parent_i_stream->Read(buffer, bufsize).LastRead();
38830220
RR
1347}
1348
4004775e 1349wxFileOffset wxBufferedInputStream::OnSysSeek(wxFileOffset seek, wxSeekMode mode)
38830220 1350{
c7a9fa36 1351 return m_parent_i_stream->SeekI(seek, mode);
6d44bf31
GL
1352}
1353
4004775e 1354wxFileOffset wxBufferedInputStream::OnSysTell() const
6d44bf31 1355{
c7a9fa36 1356 return m_parent_i_stream->TellI();
38830220 1357}
6d44bf31 1358
47fc03d2
VZ
1359void wxBufferedInputStream::SetInputStreamBuffer(wxStreamBuffer *buffer)
1360{
9a83f860 1361 wxCHECK_RET( buffer, wxT("wxBufferedInputStream needs buffer") );
47fc03d2
VZ
1362
1363 delete m_i_streambuf;
1364 m_i_streambuf = buffer;
1365}
1366
fae05df5
GL
1367// ----------------------------------------------------------------------------
1368// wxBufferedOutputStream
1369// ----------------------------------------------------------------------------
6d44bf31 1370
f42c1512 1371wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& stream,
47fc03d2 1372 wxStreamBuffer *buffer)
f42c1512 1373 : wxFilterOutputStream(stream)
6d44bf31 1374{
f42c1512
VZ
1375 m_o_streambuf = CreateBufferIfNeeded(*this, buffer);
1376}
47fc03d2 1377
f42c1512
VZ
1378wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& stream,
1379 size_t bufsize)
1380 : wxFilterOutputStream(stream)
1381{
1382 m_o_streambuf = CreateBufferIfNeeded(*this, NULL, bufsize);
6d44bf31
GL
1383}
1384
fae05df5 1385wxBufferedOutputStream::~wxBufferedOutputStream()
6d44bf31 1386{
c7a9fa36
RR
1387 Sync();
1388 delete m_o_streambuf;
3d4c6a21
GL
1389}
1390
8f0ff178
RN
1391bool wxBufferedOutputStream::Close()
1392{
1393 Sync();
1394 return IsOk();
1395}
1396
1397
fae05df5 1398wxOutputStream& wxBufferedOutputStream::Write(const void *buffer, size_t size)
123a7fdd 1399{
c7a9fa36
RR
1400 m_lastcount = 0;
1401 m_o_streambuf->Write(buffer, size);
1402 return *this;
123a7fdd
GL
1403}
1404
4004775e 1405wxFileOffset wxBufferedOutputStream::SeekO(wxFileOffset pos, wxSeekMode mode)
f4ada568 1406{
c7a9fa36
RR
1407 Sync();
1408 return m_o_streambuf->Seek(pos, mode);
f4ada568
GL
1409}
1410
4004775e 1411wxFileOffset wxBufferedOutputStream::TellO() const
3d4c6a21 1412{
c7a9fa36 1413 return m_o_streambuf->Tell();
3d4c6a21
GL
1414}
1415
fae05df5 1416void wxBufferedOutputStream::Sync()
3d4c6a21 1417{
c7a9fa36
RR
1418 m_o_streambuf->FlushBuffer();
1419 m_parent_o_stream->Sync();
3d4c6a21 1420}
219f895a 1421
fae05df5 1422size_t wxBufferedOutputStream::OnSysWrite(const void *buffer, size_t bufsize)
f4ada568 1423{
c7a9fa36 1424 return m_parent_o_stream->Write(buffer, bufsize).LastWrite();
f4ada568
GL
1425}
1426
4004775e 1427wxFileOffset wxBufferedOutputStream::OnSysSeek(wxFileOffset seek, wxSeekMode mode)
219f895a 1428{
c7a9fa36 1429 return m_parent_o_stream->SeekO(seek, mode);
219f895a
RR
1430}
1431
4004775e 1432wxFileOffset wxBufferedOutputStream::OnSysTell() const
219f895a 1433{
c7a9fa36
RR
1434 return m_parent_o_stream->TellO();
1435}
1436
588066b7 1437wxFileOffset wxBufferedOutputStream::GetLength() const
c7a9fa36 1438{
588066b7 1439 return m_parent_o_stream->GetLength() + m_o_streambuf->GetIntPosition();
219f895a 1440}
6d44bf31 1441
47fc03d2
VZ
1442void wxBufferedOutputStream::SetOutputStreamBuffer(wxStreamBuffer *buffer)
1443{
9a83f860 1444 wxCHECK_RET( buffer, wxT("wxBufferedOutputStream needs buffer") );
47fc03d2
VZ
1445
1446 delete m_o_streambuf;
1447 m_o_streambuf = buffer;
1448}
1449
f5ef4d69
VZ
1450// ---------------------------------------------------------------------------
1451// wxWrapperInputStream implementation
1452// ---------------------------------------------------------------------------
1453
1454wxWrapperInputStream::wxWrapperInputStream()
1455{
1456 m_lasterror = wxSTREAM_READ_ERROR;
1457}
1458
1459wxWrapperInputStream::wxWrapperInputStream(wxInputStream& stream)
1460 : wxFilterInputStream(stream)
1461{
1462 SynchronizeLastError();
1463}
1464
1465wxWrapperInputStream::wxWrapperInputStream(wxInputStream *stream)
1466 : wxFilterInputStream(stream)
1467{
1468 if ( m_parent_i_stream )
1469 SynchronizeLastError();
1470 else
1471 m_lasterror = wxSTREAM_READ_ERROR;
1472}
1473
1474void wxWrapperInputStream::InitParentStream(wxInputStream& stream)
1475{
1476 wxCHECK_RET( !m_parent_i_stream, "Can't init parent stream twice" );
1477
1478 m_parent_i_stream = &stream;
1479
1480 SynchronizeLastError();
1481}
1482
1483void wxWrapperInputStream::InitParentStream(wxInputStream* stream)
1484{
1485 wxCHECK_RET( !m_parent_i_stream, "Can't init parent stream twice" );
1486
1487 m_parent_i_stream = stream;
1488
1489 if ( m_parent_i_stream )
1490 {
1491 m_owns = true;
1492
1493 SynchronizeLastError();
1494 }
1495}
1496
1497wxFileOffset wxWrapperInputStream::GetLength() const
1498{
1499 wxCHECK_MSG(m_parent_i_stream, wxInvalidOffset, "Stream not valid");
1500
1501 wxON_BLOCK_EXIT_THIS0(wxWrapperInputStream::SynchronizeLastError);
1502 return m_parent_i_stream->GetLength();
1503}
1504
1505bool wxWrapperInputStream::IsSeekable() const
1506{
1507 wxCHECK_MSG(m_parent_i_stream, false, "Stream not valid");
1508 return m_parent_i_stream->IsSeekable();
1509}
1510
1511size_t wxWrapperInputStream::OnSysRead(void *buffer, size_t size)
1512{
1513 wxCHECK_MSG(m_parent_i_stream, false, "Stream not valid");
1514
1515 wxON_BLOCK_EXIT_THIS0(wxWrapperInputStream::SynchronizeLastError);
1516
1517 m_parent_i_stream->Read(buffer, size);
1518 return m_parent_i_stream->LastRead();
1519}
1520
1521wxFileOffset wxWrapperInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
1522{
1523 wxCHECK_MSG(IsSeekable(), false, "Stream not seekable");
1524
1525 wxON_BLOCK_EXIT_THIS0(wxWrapperInputStream::SynchronizeLastError);
1526 return m_parent_i_stream->SeekI (pos, mode);
1527}
1528
1529wxFileOffset wxWrapperInputStream::OnSysTell() const
1530{
1531 wxCHECK_MSG(m_parent_i_stream, false, "Stream not valid");
1532
1533 wxON_BLOCK_EXIT_THIS0(wxWrapperInputStream::SynchronizeLastError);
1534 return m_parent_i_stream->TellI();
1535}
1536
6d44bf31
GL
1537// ----------------------------------------------------------------------------
1538// Some IOManip function
1539// ----------------------------------------------------------------------------
1540
1541wxOutputStream& wxEndL(wxOutputStream& stream)
1542{
733b8ed3
VZ
1543 static const wxChar *eol = wxTextFile::GetEOL();
1544
1545 return stream.Write(eol, wxStrlen(eol));
6d44bf31 1546}
ce4169a4 1547
15dc68b6 1548#endif // wxUSE_STREAMS