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