]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/stream.cpp
Changed a few true -> TRUE
[wxWidgets.git] / src / common / stream.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: stream.cpp
3// Purpose: wxStream base classes
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 11/07/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "stream.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24 #include "wx/defs.h"
25#endif
26
27#if wxUSE_STREAMS
28
29#include <ctype.h>
30#include <wx/stream.h>
31#include <wx/datstrm.h>
32#include <wx/objstrm.h>
33
34#define BUF_TEMP_SIZE 10000
35
36// ----------------------------------------------------------------------------
37// wxStreamBuffer
38// ----------------------------------------------------------------------------
39
40#define CHECK_ERROR(err) \
41 if (m_stream->m_lasterror == wxStream_NOERROR) \
42 m_stream->m_lasterror = err
43
44wxStreamBuffer::wxStreamBuffer(wxStreamBase& stream, BufMode mode)
45 : m_buffer_start(NULL), m_buffer_end(NULL), m_buffer_pos(NULL),
46 m_buffer_size(0), m_wback(NULL), m_wbacksize(0), m_wbackcur(0),
47 m_fixed(TRUE), m_flushable(TRUE), m_stream(&stream),
48 m_mode(mode), m_destroybuf(FALSE), m_destroystream(FALSE)
49{
50}
51
52wxStreamBuffer::wxStreamBuffer(BufMode mode)
53 : m_buffer_start(NULL), m_buffer_end(NULL), m_buffer_pos(NULL),
54 m_buffer_size(0), m_wback(NULL), m_wbacksize(0), m_wbackcur(0),
55 m_fixed(TRUE), m_flushable(FALSE), m_stream(NULL),
56 m_mode(mode), m_destroybuf(FALSE), m_destroystream(TRUE)
57{
58 m_stream = new wxStreamBase();
59}
60
61wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer& buffer)
62{
63 m_buffer_start = buffer.m_buffer_start;
64 m_buffer_end = buffer.m_buffer_end;
65 m_buffer_pos = buffer.m_buffer_pos;
66 m_buffer_size = buffer.m_buffer_size;
67 m_fixed = buffer.m_fixed;
68 m_flushable = buffer.m_flushable;
69 m_stream = buffer.m_stream;
70 m_mode = buffer.m_mode;
71 m_destroybuf = FALSE;
72 m_destroystream = FALSE;
73 m_wback = NULL;
74 m_wbacksize = 0;
75 m_wbackcur = 0;
76}
77
78wxStreamBuffer::~wxStreamBuffer()
79{
80 if (m_wback)
81 free(m_wback);
82 if (m_destroybuf)
83 wxDELETEA(m_buffer_start);
84 if (m_destroystream)
85 delete m_stream;
86}
87
88size_t wxStreamBuffer::WriteBack(const char *buf, size_t bufsize)
89{
90 char *ptrback;
91
92 if (m_mode != read)
93 return 0;
94
95 ptrback = AllocSpaceWBack(bufsize);
96 if (!ptrback)
97 return 0;
98
99 memcpy(ptrback, buf, bufsize);
100 return bufsize;
101}
102
103bool wxStreamBuffer::WriteBack(char c)
104{
105 char *ptrback;
106
107 ptrback = AllocSpaceWBack(1);
108 if (!ptrback)
109 return FALSE;
110
111 *ptrback = c;
112 return TRUE;
113}
114
115void wxStreamBuffer::SetBufferIO(char *buffer_start, char *buffer_end)
116{
117 if (m_destroybuf)
118 wxDELETEA(m_buffer_start);
119 m_buffer_start = buffer_start;
120 m_buffer_end = buffer_end;
121
122 m_buffer_size = m_buffer_end-m_buffer_start;
123 m_destroybuf = FALSE;
124 ResetBuffer();
125}
126
127void wxStreamBuffer::SetBufferIO(size_t bufsize)
128{
129 char *b_start;
130
131 if (m_destroybuf)
132 wxDELETEA(m_buffer_start);
133
134 if (!bufsize) {
135 m_buffer_start = NULL;
136 m_buffer_end = NULL;
137 m_buffer_pos = NULL;
138 m_buffer_size = 0;
139 return;
140 }
141
142 b_start = new char[bufsize];
143 SetBufferIO(b_start, b_start + bufsize);
144 m_destroybuf = TRUE;
145}
146
147void wxStreamBuffer::ResetBuffer()
148{
149 m_stream->m_lasterror = wxStream_NOERROR;
150 m_stream->m_lastcount = 0;
151 if (m_mode == read)
152 m_buffer_pos = m_buffer_end;
153 else
154 m_buffer_pos = m_buffer_start;
155}
156
157char *wxStreamBuffer::AllocSpaceWBack(size_t needed_size)
158{
159 char *temp_b;
160
161 m_wbacksize += needed_size;
162
163 if (!m_wback)
164 temp_b = (char *)malloc(m_wbacksize);
165 else
166 temp_b = (char *)realloc(m_wback, m_wbacksize);
167
168 if (!temp_b)
169 return NULL;
170 m_wback = temp_b;
171
172 return (char *)(m_wback+(m_wbacksize-needed_size));
173}
174
175size_t wxStreamBuffer::GetWBack(char *buf, size_t bsize)
176{
177 size_t s_toget = m_wbacksize-m_wbackcur;
178
179 if (bsize < s_toget)
180 s_toget = bsize;
181
182 memcpy(buf, (m_wback+m_wbackcur), s_toget);
183
184 m_wbackcur += s_toget;
185 if (m_wbackcur == m_wbacksize) {
186 free(m_wback);
187 m_wback = (char *)NULL;
188 m_wbacksize = 0;
189 m_wbackcur = 0;
190 }
191
192 return s_toget;
193}
194
195bool wxStreamBuffer::FillBuffer()
196{
197 size_t count;
198
199 count = m_stream->OnSysRead(m_buffer_start, m_buffer_size);
200 m_buffer_end = m_buffer_start+count;
201 m_buffer_pos = m_buffer_start;
202
203 if (count == 0)
204 return FALSE;
205 return TRUE;
206}
207
208bool wxStreamBuffer::FlushBuffer()
209{
210 size_t count, current;
211
212 if (m_buffer_pos == m_buffer_start || !m_flushable)
213 return FALSE;
214
215 current = m_buffer_pos-m_buffer_start;
216 count = m_stream->OnSysWrite(m_buffer_start, current);
217 if (count != current)
218 return FALSE;
219 m_buffer_pos = m_buffer_start;
220
221 return TRUE;
222}
223
224void wxStreamBuffer::GetFromBuffer(void *buffer, size_t size)
225{
226 size_t s_toget = m_buffer_end-m_buffer_pos;
227
228 if (size < s_toget)
229 s_toget = size;
230
231 memcpy(buffer, m_buffer_pos, s_toget);
232 m_buffer_pos += s_toget;
233}
234
235void wxStreamBuffer::PutToBuffer(const void *buffer, size_t size)
236{
237 size_t s_toput = m_buffer_end-m_buffer_pos;
238
239 if (s_toput < size && !m_fixed) {
240 m_buffer_start = (char *)realloc(m_buffer_start, m_buffer_size+size);
241 // I round a bit
242 m_buffer_size += size;
243 m_buffer_end = m_buffer_start+m_buffer_size;
244 s_toput = size;
245 }
246 if (s_toput > size)
247 s_toput = size;
248 memcpy(m_buffer_pos, buffer, s_toput);
249 m_buffer_pos += s_toput;
250}
251
252void wxStreamBuffer::PutChar(char c)
253{
254 wxASSERT(m_stream != NULL);
255
256 if (!m_buffer_size) {
257 m_stream->OnSysWrite(&c, 1);
258 return;
259 }
260
261 if (GetDataLeft() == 0 && !FlushBuffer()) {
262 CHECK_ERROR(wxStream_WRITE_ERR);
263 return;
264 }
265
266 PutToBuffer(&c, 1);
267 m_stream->m_lastcount = 1;
268}
269
270char wxStreamBuffer::GetChar()
271{
272 char c;
273
274 wxASSERT(m_stream != NULL);
275
276 if (!m_buffer_size) {
277 m_stream->OnSysRead(&c, 1);
278 return c;
279 }
280
281 if (!GetDataLeft()) {
282 CHECK_ERROR(wxStream_READ_ERR);
283 return 0;
284 }
285
286 GetFromBuffer(&c, 1);
287 m_stream->m_lastcount = 1;
288 return c;
289}
290
291size_t wxStreamBuffer::Read(void *buffer, size_t size)
292{
293 wxASSERT(m_stream != NULL);
294
295 if (m_mode == write)
296 return 0;
297
298 // ------------------
299 // Buffering disabled
300 // ------------------
301
302 m_stream->m_lasterror = wxStream_NOERROR;
303 m_stream->m_lastcount = GetWBack((char *)buffer, size);
304 size -= m_stream->m_lastcount;
305 if (size == 0)
306 return m_stream->m_lastcount;
307
308 buffer = (void *)((char *)buffer+m_stream->m_lastcount);
309
310 if (!m_buffer_size)
311 return (m_stream->m_lastcount += m_stream->OnSysRead(buffer, size));
312
313 // -----------------
314 // Buffering enabled
315 // -----------------
316 size_t buf_left, orig_size = size;
317
318 while (size > 0) {
319 buf_left = GetDataLeft();
320
321 // First case: the requested buffer is larger than the stream buffer,
322 // we split it.
323 if (size > buf_left) {
324 GetFromBuffer(buffer, buf_left);
325 size -= buf_left;
326 buffer = (char *)buffer + buf_left; // ANSI C++ violation.
327
328 if (!FillBuffer()) {
329 CHECK_ERROR(wxStream_READ_ERR);
330 return (m_stream->m_lastcount = orig_size-size);
331 }
332 } else {
333
334 // Second case: we just copy from the stream buffer.
335 GetFromBuffer(buffer, size);
336 break;
337 }
338 }
339 return (m_stream->m_lastcount += orig_size);
340}
341
342size_t wxStreamBuffer::Read(wxStreamBuffer *s_buf)
343{
344 char buf[BUF_TEMP_SIZE];
345 size_t s = 0, bytes_read = BUF_TEMP_SIZE;
346
347 if (m_mode == write)
348 return 0;
349
350 while (bytes_read != 0) {
351 bytes_read = Read(buf, bytes_read);
352 bytes_read = s_buf->Write(buf, bytes_read);
353 s += bytes_read;
354 }
355 return s;
356}
357
358size_t wxStreamBuffer::Write(const void *buffer, size_t size)
359{
360 wxASSERT(m_stream != NULL);
361
362 if (m_mode == read)
363 return 0;
364
365 // ------------------
366 // Buffering disabled
367 // ------------------
368
369 m_stream->m_lasterror = wxStream_NOERROR;
370 if (!m_buffer_size)
371 return (m_stream->m_lastcount = m_stream->OnSysWrite(buffer, size));
372
373 // ------------------
374 // Buffering enabled
375 // ------------------
376
377 size_t buf_left, orig_size = size;
378
379 while (size > 0) {
380 buf_left = m_buffer_end - m_buffer_pos;
381
382 // First case: the buffer to write is larger than the stream buffer,
383 // we split it
384 if (size > buf_left) {
385 PutToBuffer(buffer, buf_left);
386 size -= buf_left;
387 buffer = (char *)buffer + buf_left; // ANSI C++ violation.
388
389 if (!FlushBuffer()) {
390 CHECK_ERROR(wxStream_WRITE_ERR);
391 return (m_stream->m_lastcount = orig_size-size);
392 }
393
394 m_buffer_pos = m_buffer_start;
395
396 } else {
397
398 // Second case: just copy it in the stream buffer.
399 PutToBuffer(buffer, size);
400 break;
401 }
402 }
403 return (m_stream->m_lastcount = orig_size);
404}
405
406size_t wxStreamBuffer::Write(wxStreamBuffer *sbuf)
407{
408 char buf[BUF_TEMP_SIZE];
409 size_t s = 0, bytes_count = BUF_TEMP_SIZE, b_count2;
410
411 if (m_mode == read)
412 return 0;
413
414 while (bytes_count == BUF_TEMP_SIZE) {
415 b_count2 = sbuf->Read(buf, bytes_count);
416 bytes_count = Write(buf, b_count2);
417 if (b_count2 > bytes_count)
418 sbuf->WriteBack(buf+bytes_count, b_count2-bytes_count);
419 s += bytes_count;
420 }
421 return s;
422}
423
424off_t wxStreamBuffer::Seek(off_t pos, wxSeekMode mode)
425{
426 off_t ret_off, diff, last_access;
427
428 last_access = GetLastAccess();
429
430 if (!m_flushable) {
431 diff = pos + GetIntPosition();
432 if (diff < 0 || diff > last_access)
433 return wxInvalidOffset;
434 SetIntPosition(diff);
435 return diff;
436 }
437
438 switch (mode) {
439 case wxFromStart: {
440 // We'll try to compute an internal position later ...
441 ret_off = m_stream->OnSysSeek(pos, wxFromStart);
442 ResetBuffer();
443 return ret_off;
444 }
445 case wxFromCurrent: {
446 diff = pos + GetIntPosition();
447
448 if ( (diff > last_access) || (diff < 0) ) {
449 ret_off = m_stream->OnSysSeek(pos, wxFromCurrent);
450 ResetBuffer();
451 return ret_off;
452 } else {
453 SetIntPosition(diff);
454 return pos;
455 }
456 }
457 case wxFromEnd:
458 // Hard to compute: always seek to the requested position.
459 ret_off = m_stream->OnSysSeek(pos, wxFromEnd);
460 ResetBuffer();
461 return ret_off;
462 }
463 return wxInvalidOffset;
464}
465
466off_t wxStreamBuffer::Tell() const
467{
468 off_t pos;
469
470 if (m_flushable) {
471 pos = m_stream->OnSysTell();
472 if (pos == wxInvalidOffset)
473 return wxInvalidOffset;
474 return pos - GetLastAccess() + GetIntPosition();
475 } else
476 return GetIntPosition();
477}
478
479size_t wxStreamBuffer::GetDataLeft()
480{
481 if (m_buffer_end == m_buffer_pos && m_flushable)
482 FillBuffer();
483 return m_buffer_end-m_buffer_pos;
484}
485
486// ----------------------------------------------------------------------------
487// wxStreamBase
488// ----------------------------------------------------------------------------
489
490wxStreamBase::wxStreamBase()
491{
492 m_lasterror = wxStream_NOERROR;
493 m_lastcount = 0;
494}
495
496wxStreamBase::~wxStreamBase()
497{
498}
499
500size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer), size_t WXUNUSED(size))
501{
502 return 0;
503}
504
505size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer), size_t WXUNUSED(bufsize))
506{
507 return 0;
508}
509
510off_t wxStreamBase::OnSysSeek(off_t WXUNUSED(seek), wxSeekMode WXUNUSED(mode))
511{
512 return wxInvalidOffset;
513}
514
515off_t wxStreamBase::OnSysTell() const
516{
517 return wxInvalidOffset;
518}
519
520// ----------------------------------------------------------------------------
521// wxInputStream
522// ----------------------------------------------------------------------------
523
524wxInputStream::wxInputStream()
525 : wxStreamBase()
526{
527 m_i_destroybuf = TRUE;
528 m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read);
529}
530
531wxInputStream::wxInputStream(wxStreamBuffer *buffer)
532 : wxStreamBase()
533{
534 m_i_destroybuf = FALSE;
535 m_i_streambuf = buffer;
536}
537
538wxInputStream::~wxInputStream()
539{
540 if (m_i_destroybuf)
541 delete m_i_streambuf;
542}
543
544char wxInputStream::GetC()
545{
546 char c;
547 m_i_streambuf->Read(&c, 1);
548 return c;
549}
550
551wxInputStream& wxInputStream::Read(void *buffer, size_t size)
552{
553 m_i_streambuf->Read(buffer, size);
554 // wxStreamBuffer sets all variables for us
555 return *this;
556}
557
558char wxInputStream::Peek()
559{
560 m_i_streambuf->GetDataLeft();
561
562 return *(m_i_streambuf->GetBufferPos());
563}
564
565
566wxInputStream& wxInputStream::Read(wxOutputStream& stream_out)
567{
568 char buf[BUF_TEMP_SIZE];
569 size_t bytes_read = BUF_TEMP_SIZE;
570
571 while (bytes_read == BUF_TEMP_SIZE) {
572 bytes_read = Read(buf, bytes_read).LastRead();
573 bytes_read = stream_out.Write(buf, bytes_read).LastWrite();
574 }
575 return *this;
576}
577
578off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode)
579{
580 return m_i_streambuf->Seek(pos, mode);
581}
582
583off_t wxInputStream::TellI() const
584{
585 return m_i_streambuf->Tell();
586}
587
588// --------------------
589// Overloaded operators
590// --------------------
591
592wxInputStream& wxInputStream::operator>>(wxString& line)
593{
594 wxDataInputStream s(*this);
595
596 line = s.ReadLine();
597 return *this;
598}
599
600wxInputStream& wxInputStream::operator>>(char& c)
601{
602 c = GetC();
603 return *this;
604}
605
606wxInputStream& wxInputStream::operator>>(short& i)
607{
608 long l;
609
610 *this >> l;
611 i = (short)l;
612 return *this;
613}
614
615wxInputStream& wxInputStream::operator>>(int& i)
616{
617 long l;
618
619 *this >> l;
620 i = (short)l;
621 return *this;
622}
623
624wxInputStream& wxInputStream::operator>>(long& i)
625{
626 /* I only implemented a simple integer parser */
627 int c, sign;
628
629 while (isspace( c = GetC() ) )
630 /* Do nothing */ ;
631
632 i = 0;
633 if (! (c == '-' || isdigit(c)) ) {
634 InputStreamBuffer()->WriteBack(c);
635 return *this;
636 }
637
638 if (c == '-') {
639 sign = -1;
640 c = GetC();
641 } else
642 sign = 1;
643
644 while (isdigit(c)) {
645 i = i*10 + c;
646 c = GetC();
647 }
648
649 i *= sign;
650
651 return *this;
652}
653
654wxInputStream& wxInputStream::operator>>(double& f)
655{
656 /* I only implemented a simple float parser */
657 int c, sign;
658
659 while (isspace( c = GetC() ) )
660 /* Do nothing */ ;
661
662 f = 0.0;
663 if (! (c == '-' || isdigit(c)) ) {
664 InputStreamBuffer()->WriteBack(c);
665 return *this;
666 }
667
668 if (c == '-') {
669 sign = -1;
670 c = GetC();
671 } else
672 sign = 1;
673
674 while (isdigit(c)) {
675 f = f*10 + (c - '0');
676 c = GetC();
677 }
678
679 if (c == '.') {
680 double f_multiplicator = (double) 0.1;
681 c = GetC();
682
683 while (isdigit(c)) {
684 f += (c-'0')*f_multiplicator;
685 f_multiplicator /= 10;
686 c = GetC();
687 }
688 }
689
690 f *= sign;
691
692 return *this;
693}
694
695#if wxUSE_SERIAL
696wxInputStream& wxInputStream::operator>>(wxObject *& obj)
697{
698 wxObjectInputStream obj_s(*this);
699 obj = obj_s.LoadObject();
700 return *this;
701}
702#endif
703
704
705// ----------------------------------------------------------------------------
706// wxOutputStream
707// ----------------------------------------------------------------------------
708wxOutputStream::wxOutputStream()
709 : wxStreamBase()
710{
711 m_o_destroybuf = TRUE;
712 m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write);
713}
714
715wxOutputStream::wxOutputStream(wxStreamBuffer *buffer)
716 : wxStreamBase()
717{
718 m_o_destroybuf = FALSE;
719 m_o_streambuf = buffer;
720}
721
722wxOutputStream::~wxOutputStream()
723{
724 if (m_o_destroybuf)
725 delete m_o_streambuf;
726}
727
728wxOutputStream& wxOutputStream::Write(const void *buffer, size_t size)
729{
730 m_o_streambuf->Write(buffer, size);
731 return *this;
732}
733
734wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in)
735{
736 stream_in.Read(*this);
737 return *this;
738}
739
740off_t wxOutputStream::TellO() const
741{
742 return m_o_streambuf->Tell();
743}
744
745off_t wxOutputStream::SeekO(off_t pos, wxSeekMode mode)
746{
747 return m_o_streambuf->Seek(pos, mode);
748}
749
750void wxOutputStream::Sync()
751{
752 m_o_streambuf->FlushBuffer();
753}
754
755wxOutputStream& wxOutputStream::operator<<(const char *string)
756{
757 return Write(string, strlen(string));
758}
759
760wxOutputStream& wxOutputStream::operator<<(wxString& string)
761{
762#if wxUSE_UNICODE
763 const wxWX2MBbuf buf = string.mb_str();
764 return *this << buf;
765#else
766 return Write(string, string.Len());
767#endif
768}
769
770wxOutputStream& wxOutputStream::operator<<(char c)
771{
772 return Write(&c, 1);
773}
774
775wxOutputStream& wxOutputStream::operator<<(short i)
776{
777 wxString strint;
778
779 strint.Printf(_T("%i"), i);
780 return *this << strint;
781}
782
783wxOutputStream& wxOutputStream::operator<<(int i)
784{
785 wxString strint;
786
787 strint.Printf(_T("%i"), i);
788 return *this << strint;
789}
790
791wxOutputStream& wxOutputStream::operator<<(long i)
792{
793 wxString strlong;
794
795 strlong.Printf(_T("%i"), i);
796 return *this << strlong;
797}
798
799wxOutputStream& wxOutputStream::operator<<(double f)
800{
801 wxString strfloat;
802
803 strfloat.Printf(_T("%f"), f);
804 return *this << strfloat;
805}
806
807#if wxUSE_SERIAL
808wxOutputStream& wxOutputStream::operator<<(wxObject& obj)
809{
810 wxObjectOutputStream obj_s(*this);
811 obj_s.SaveObject(obj);
812 return *this;
813}
814#endif
815
816// ----------------------------------------------------------------------------
817// wxFilterInputStream
818// ----------------------------------------------------------------------------
819wxFilterInputStream::wxFilterInputStream()
820 : wxInputStream(NULL)
821{
822 // WARNING streambuf set to NULL !
823}
824
825wxFilterInputStream::wxFilterInputStream(wxInputStream& stream)
826 : wxInputStream(stream.InputStreamBuffer())
827{
828 m_parent_i_stream = &stream;
829}
830
831wxFilterInputStream::~wxFilterInputStream()
832{
833}
834
835// ----------------------------------------------------------------------------
836// wxFilterOutputStream
837// ----------------------------------------------------------------------------
838wxFilterOutputStream::wxFilterOutputStream()
839 : wxOutputStream(NULL)
840{
841}
842
843wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream)
844 : wxOutputStream(stream.OutputStreamBuffer())
845{
846 m_parent_o_stream = &stream;
847}
848
849wxFilterOutputStream::~wxFilterOutputStream()
850{
851}
852
853// ----------------------------------------------------------------------------
854// Some IOManip function
855// ----------------------------------------------------------------------------
856
857wxOutputStream& wxEndL(wxOutputStream& stream)
858{
859#ifdef __MSW__
860 return stream.Write("\r\n", 2);
861#else
862 return stream.Write("\n", 1);
863#endif
864}
865
866#endif
867 // wxUSE_STREAMS