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