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