]>
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 = (char*)NULL; | |
102 | m_buffer_end = (char*)NULL; | |
103 | m_buffer_pos = (char*)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 | switch (mode) { | |
383 | case wxFromStart: diff = pos; break; | |
384 | case wxFromCurrent: diff = pos + GetIntPosition(); break; | |
385 | case wxFromEnd: diff = pos + last_access; break; | |
386 | default: return wxInvalidOffset; | |
387 | } | |
388 | if (diff < 0 || diff > last_access) | |
389 | return wxInvalidOffset; | |
390 | SetIntPosition(diff); | |
391 | return diff; | |
392 | } | |
393 | ||
394 | switch (mode) { | |
395 | case wxFromStart: { | |
396 | // We'll try to compute an internal position later ... | |
397 | ret_off = m_stream->OnSysSeek(pos, wxFromStart); | |
398 | ResetBuffer(); | |
399 | return ret_off; | |
400 | } | |
401 | case wxFromCurrent: { | |
402 | diff = pos + GetIntPosition(); | |
403 | ||
404 | if ( (diff > last_access) || (diff < 0) ) { | |
405 | // We must take into account the fact that we have read something | |
406 | // previously. | |
407 | ret_off = m_stream->OnSysSeek(diff-last_access, wxFromCurrent); | |
408 | ResetBuffer(); | |
409 | return ret_off; | |
410 | } else { | |
411 | SetIntPosition(diff); | |
412 | return pos; | |
413 | } | |
414 | } | |
415 | case wxFromEnd: | |
416 | // Hard to compute: always seek to the requested position. | |
417 | ret_off = m_stream->OnSysSeek(pos, wxFromEnd); | |
418 | ResetBuffer(); | |
419 | return ret_off; | |
420 | } | |
421 | return wxInvalidOffset; | |
422 | } | |
423 | ||
424 | off_t wxStreamBuffer::Tell() const | |
425 | { | |
426 | off_t pos; | |
427 | ||
428 | if (m_flushable) { | |
429 | pos = m_stream->OnSysTell(); | |
430 | if (pos == wxInvalidOffset) | |
431 | return wxInvalidOffset; | |
432 | return pos - GetLastAccess() + GetIntPosition(); | |
433 | } else | |
434 | return GetIntPosition(); | |
435 | } | |
436 | ||
437 | size_t wxStreamBuffer::GetDataLeft() | |
438 | { | |
439 | if (m_buffer_end == m_buffer_pos && m_flushable) | |
440 | FillBuffer(); | |
441 | return m_buffer_end-m_buffer_pos; | |
442 | } | |
443 | ||
444 | // ---------------------------------------------------------------------------- | |
445 | // wxStreamBase | |
446 | // ---------------------------------------------------------------------------- | |
447 | ||
448 | wxStreamBase::wxStreamBase() | |
449 | { | |
450 | m_lasterror = wxStream_NOERROR; | |
451 | m_lastcount = 0; | |
452 | } | |
453 | ||
454 | wxStreamBase::~wxStreamBase() | |
455 | { | |
456 | } | |
457 | ||
458 | size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer), size_t WXUNUSED(size)) | |
459 | { | |
460 | return 0; | |
461 | } | |
462 | ||
463 | size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer), size_t WXUNUSED(bufsize)) | |
464 | { | |
465 | return 0; | |
466 | } | |
467 | ||
468 | off_t wxStreamBase::OnSysSeek(off_t WXUNUSED(seek), wxSeekMode WXUNUSED(mode)) | |
469 | { | |
470 | return wxInvalidOffset; | |
471 | } | |
472 | ||
473 | off_t wxStreamBase::OnSysTell() const | |
474 | { | |
475 | return wxInvalidOffset; | |
476 | } | |
477 | ||
478 | // ---------------------------------------------------------------------------- | |
479 | // wxInputStream | |
480 | // ---------------------------------------------------------------------------- | |
481 | ||
482 | wxInputStream::wxInputStream() | |
483 | : wxStreamBase(), | |
484 | m_wback(NULL), m_wbacksize(0), m_wbackcur(0) | |
485 | { | |
486 | } | |
487 | ||
488 | wxInputStream::~wxInputStream() | |
489 | { | |
490 | if (m_wback) | |
491 | free(m_wback); | |
492 | } | |
493 | ||
494 | char *wxInputStream::AllocSpaceWBack(size_t needed_size) | |
495 | { | |
496 | char *temp_b; | |
497 | size_t toget; | |
498 | ||
499 | /* get number of bytes left from previous wback buffer */ | |
500 | toget = m_wbacksize - m_wbackcur; | |
501 | ||
502 | /* allocate a buffer large enough to hold prev + new data */ | |
503 | temp_b = (char *) malloc(needed_size + toget); | |
504 | ||
505 | if (!temp_b) | |
506 | return NULL; | |
507 | ||
508 | /* copy previous data (and free old buffer) if needed */ | |
509 | if (m_wback) | |
510 | { | |
511 | memmove(temp_b + needed_size, m_wback + m_wbackcur, toget); | |
512 | free(m_wback); | |
513 | } | |
514 | ||
515 | /* done */ | |
516 | m_wback = temp_b; | |
517 | m_wbackcur = 0; | |
518 | m_wbacksize = needed_size + toget; | |
519 | ||
520 | return (char *) m_wback; | |
521 | } | |
522 | ||
523 | size_t wxInputStream::GetWBack(char *buf, size_t bsize) | |
524 | { | |
525 | size_t s_toget = m_wbacksize-m_wbackcur; | |
526 | ||
527 | if (!m_wback) | |
528 | return 0; | |
529 | ||
530 | if (bsize < s_toget) | |
531 | s_toget = bsize; | |
532 | ||
533 | memcpy(buf, (m_wback+m_wbackcur), s_toget); | |
534 | ||
535 | m_wbackcur += s_toget; | |
536 | if (m_wbackcur == m_wbacksize) { | |
537 | free(m_wback); | |
538 | m_wback = (char *)NULL; | |
539 | m_wbacksize = 0; | |
540 | m_wbackcur = 0; | |
541 | } | |
542 | ||
543 | return s_toget; | |
544 | } | |
545 | ||
546 | size_t wxInputStream::Ungetch(const void *buf, size_t bufsize) | |
547 | { | |
548 | char *ptrback; | |
549 | ||
550 | ptrback = AllocSpaceWBack(bufsize); | |
551 | if (!ptrback) | |
552 | return 0; | |
553 | ||
554 | memcpy(ptrback, buf, bufsize); | |
555 | return bufsize; | |
556 | } | |
557 | ||
558 | bool wxInputStream::Ungetch(char c) | |
559 | { | |
560 | char *ptrback; | |
561 | ||
562 | ptrback = AllocSpaceWBack(1); | |
563 | if (!ptrback) | |
564 | return FALSE; | |
565 | ||
566 | *ptrback = c; | |
567 | return TRUE; | |
568 | } | |
569 | ||
570 | char wxInputStream::GetC() | |
571 | { | |
572 | char c; | |
573 | Read(&c, 1); | |
574 | return c; | |
575 | } | |
576 | ||
577 | wxInputStream& wxInputStream::Read(void *buffer, size_t size) | |
578 | { | |
579 | size_t retsize; | |
580 | char *buf = (char *)buffer; | |
581 | ||
582 | retsize = GetWBack(buf, size); | |
583 | if (retsize == size) { | |
584 | m_lastcount = size; | |
585 | m_lasterror = wxStream_NOERROR; | |
586 | return *this; | |
587 | } | |
588 | size -= retsize; | |
589 | buf += retsize; | |
590 | ||
591 | // GRG: shouldn't we also add retsize to m_lastcount here? | |
592 | m_lastcount = OnSysRead(buf, size); | |
593 | return *this; | |
594 | } | |
595 | ||
596 | char wxInputStream::Peek() | |
597 | { | |
598 | char c; | |
599 | ||
600 | Read(&c, 1); | |
601 | if (m_lasterror == wxStream_NOERROR) { | |
602 | Ungetch(c); | |
603 | return c; | |
604 | } | |
605 | return 0; | |
606 | } | |
607 | ||
608 | wxInputStream& wxInputStream::Read(wxOutputStream& stream_out) | |
609 | { | |
610 | char buf[BUF_TEMP_SIZE]; | |
611 | size_t bytes_read = BUF_TEMP_SIZE; | |
612 | ||
613 | while (bytes_read == BUF_TEMP_SIZE) { | |
614 | bytes_read = Read(buf, bytes_read).LastRead(); | |
615 | bytes_read = stream_out.Write(buf, bytes_read).LastWrite(); | |
616 | } | |
617 | return *this; | |
618 | } | |
619 | ||
620 | off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode) | |
621 | { | |
622 | // Should be check and improve, just to remove a slight bug ! | |
623 | // I don't know whether it should be put as well in wxFileInputStream::OnSysSeek ? | |
624 | if (m_lasterror==wxSTREAM_EOF) | |
625 | m_lasterror=wxSTREAM_NOERROR; | |
626 | ||
627 | // A call to SeekI() will automatically invalidate any previous call | |
628 | // to Ungetch(), otherwise it would be possible to SeeI() to one | |
629 | // one position, unread some bytes there, SeekI() to another position | |
630 | // and the data would be corrupted. | |
631 | if (m_wback) { | |
632 | free(m_wback); | |
633 | m_wback = (char*) NULL; | |
634 | m_wbacksize = 0; | |
635 | m_wbackcur = 0; | |
636 | } | |
637 | ||
638 | return OnSysSeek(pos, mode); | |
639 | } | |
640 | ||
641 | off_t wxInputStream::TellI() const | |
642 | { | |
643 | return OnSysTell(); | |
644 | } | |
645 | ||
646 | // -------------------- | |
647 | // Overloaded operators | |
648 | // -------------------- | |
649 | ||
650 | #if wxUSE_SERIAL | |
651 | wxInputStream& wxInputStream::operator>>(wxObject *& obj) | |
652 | { | |
653 | wxObjectInputStream obj_s(*this); | |
654 | obj = obj_s.LoadObject(); | |
655 | return *this; | |
656 | } | |
657 | #endif | |
658 | ||
659 | ||
660 | // ---------------------------------------------------------------------------- | |
661 | // wxOutputStream | |
662 | // ---------------------------------------------------------------------------- | |
663 | wxOutputStream::wxOutputStream() | |
664 | : wxStreamBase() | |
665 | { | |
666 | } | |
667 | ||
668 | wxOutputStream::~wxOutputStream() | |
669 | { | |
670 | } | |
671 | ||
672 | wxOutputStream& wxOutputStream::Write(const void *buffer, size_t size) | |
673 | { | |
674 | m_lastcount = OnSysWrite(buffer, size); | |
675 | return *this; | |
676 | } | |
677 | ||
678 | wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in) | |
679 | { | |
680 | stream_in.Read(*this); | |
681 | return *this; | |
682 | } | |
683 | ||
684 | off_t wxOutputStream::TellO() const | |
685 | { | |
686 | return OnSysTell(); | |
687 | } | |
688 | ||
689 | off_t wxOutputStream::SeekO(off_t pos, wxSeekMode mode) | |
690 | { | |
691 | return OnSysSeek(pos, mode); | |
692 | } | |
693 | ||
694 | void wxOutputStream::Sync() | |
695 | { | |
696 | } | |
697 | ||
698 | #if wxUSE_SERIAL | |
699 | wxOutputStream& wxOutputStream::operator<<(wxObject& obj) | |
700 | { | |
701 | wxObjectOutputStream obj_s(*this); | |
702 | obj_s.SaveObject(obj); | |
703 | return *this; | |
704 | } | |
705 | #endif | |
706 | ||
707 | // ---------------------------------------------------------------------------- | |
708 | // wxCountingOutputStream | |
709 | // ---------------------------------------------------------------------------- | |
710 | ||
711 | wxCountingOutputStream::wxCountingOutputStream () | |
712 | : wxOutputStream() | |
713 | { | |
714 | m_currentPos = 0; | |
715 | } | |
716 | ||
717 | size_t wxCountingOutputStream::GetSize() const | |
718 | { | |
719 | return m_lastcount; | |
720 | } | |
721 | ||
722 | size_t wxCountingOutputStream::OnSysWrite(const void *WXUNUSED(buffer), size_t size) | |
723 | { | |
724 | m_currentPos += size; | |
725 | if (m_currentPos > m_lastcount) m_lastcount = m_currentPos; | |
726 | return m_currentPos; | |
727 | } | |
728 | ||
729 | off_t wxCountingOutputStream::OnSysSeek(off_t pos, wxSeekMode mode) | |
730 | { | |
731 | if (mode == wxFromStart) | |
732 | { | |
733 | m_currentPos = pos; | |
734 | } | |
735 | if (mode == wxFromEnd) | |
736 | { | |
737 | m_currentPos = m_lastcount + pos; | |
738 | } | |
739 | else | |
740 | { | |
741 | m_currentPos += pos; | |
742 | } | |
743 | if (m_currentPos > m_lastcount) m_lastcount = m_currentPos; | |
744 | ||
745 | return m_currentPos; // ? | |
746 | } | |
747 | ||
748 | off_t wxCountingOutputStream::OnSysTell() const | |
749 | { | |
750 | return m_currentPos; // ? | |
751 | } | |
752 | ||
753 | // ---------------------------------------------------------------------------- | |
754 | // wxFilterInputStream | |
755 | // ---------------------------------------------------------------------------- | |
756 | ||
757 | wxFilterInputStream::wxFilterInputStream() | |
758 | : wxInputStream() | |
759 | { | |
760 | } | |
761 | ||
762 | wxFilterInputStream::wxFilterInputStream(wxInputStream& stream) | |
763 | : wxInputStream() | |
764 | { | |
765 | m_parent_i_stream = &stream; | |
766 | } | |
767 | ||
768 | wxFilterInputStream::~wxFilterInputStream() | |
769 | { | |
770 | } | |
771 | ||
772 | // ---------------------------------------------------------------------------- | |
773 | // wxFilterOutputStream | |
774 | // ---------------------------------------------------------------------------- | |
775 | wxFilterOutputStream::wxFilterOutputStream() | |
776 | : wxOutputStream() | |
777 | { | |
778 | } | |
779 | ||
780 | wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream) | |
781 | : wxOutputStream() | |
782 | { | |
783 | m_parent_o_stream = &stream; | |
784 | } | |
785 | ||
786 | wxFilterOutputStream::~wxFilterOutputStream() | |
787 | { | |
788 | } | |
789 | ||
790 | // ---------------------------------------------------------------------------- | |
791 | // wxBufferedInputStream | |
792 | // ---------------------------------------------------------------------------- | |
793 | wxBufferedInputStream::wxBufferedInputStream(wxInputStream& s) | |
794 | : wxFilterInputStream(s) | |
795 | { | |
796 | m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read); | |
797 | m_i_streambuf->SetBufferIO(1024); | |
798 | } | |
799 | ||
800 | wxBufferedInputStream::~wxBufferedInputStream() | |
801 | { | |
802 | delete m_i_streambuf; | |
803 | } | |
804 | ||
805 | char wxBufferedInputStream::Peek() | |
806 | { | |
807 | return m_i_streambuf->Peek(); | |
808 | } | |
809 | ||
810 | wxInputStream& wxBufferedInputStream::Read(void *buffer, size_t size) | |
811 | { | |
812 | size_t retsize; | |
813 | char *buf = (char *)buffer; | |
814 | ||
815 | retsize = GetWBack(buf, size); | |
816 | m_lastcount = retsize; | |
817 | if (retsize == size) { | |
818 | m_lasterror = wxStream_NOERROR; | |
819 | return *this; | |
820 | } | |
821 | size -= retsize; | |
822 | buf += retsize; | |
823 | ||
824 | m_i_streambuf->Read(buf, size); | |
825 | ||
826 | return *this; | |
827 | } | |
828 | ||
829 | off_t wxBufferedInputStream::SeekI(off_t pos, wxSeekMode mode) | |
830 | { | |
831 | return m_i_streambuf->Seek(pos, mode); | |
832 | } | |
833 | ||
834 | off_t wxBufferedInputStream::TellI() const | |
835 | { | |
836 | return m_i_streambuf->Tell(); | |
837 | } | |
838 | ||
839 | size_t wxBufferedInputStream::OnSysRead(void *buffer, size_t bufsize) | |
840 | { | |
841 | return m_parent_i_stream->Read(buffer, bufsize).LastRead(); | |
842 | } | |
843 | ||
844 | off_t wxBufferedInputStream::OnSysSeek(off_t seek, wxSeekMode mode) | |
845 | { | |
846 | return m_parent_i_stream->SeekI(seek, mode); | |
847 | } | |
848 | ||
849 | off_t wxBufferedInputStream::OnSysTell() const | |
850 | { | |
851 | return m_parent_i_stream->TellI(); | |
852 | } | |
853 | ||
854 | // ---------------------------------------------------------------------------- | |
855 | // wxBufferedOutputStream | |
856 | // ---------------------------------------------------------------------------- | |
857 | ||
858 | wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& s) | |
859 | : wxFilterOutputStream(s) | |
860 | { | |
861 | m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write); | |
862 | m_o_streambuf->SetBufferIO(1024); | |
863 | } | |
864 | ||
865 | wxBufferedOutputStream::~wxBufferedOutputStream() | |
866 | { | |
867 | Sync(); | |
868 | delete m_o_streambuf; | |
869 | } | |
870 | ||
871 | wxOutputStream& wxBufferedOutputStream::Write(const void *buffer, size_t size) | |
872 | { | |
873 | m_lastcount = 0; | |
874 | m_o_streambuf->Write(buffer, size); | |
875 | return *this; | |
876 | } | |
877 | ||
878 | off_t wxBufferedOutputStream::SeekO(off_t pos, wxSeekMode mode) | |
879 | { | |
880 | Sync(); | |
881 | return m_o_streambuf->Seek(pos, mode); | |
882 | } | |
883 | ||
884 | off_t wxBufferedOutputStream::TellO() const | |
885 | { | |
886 | return m_o_streambuf->Tell(); | |
887 | } | |
888 | ||
889 | void wxBufferedOutputStream::Sync() | |
890 | { | |
891 | m_o_streambuf->FlushBuffer(); | |
892 | m_parent_o_stream->Sync(); | |
893 | } | |
894 | ||
895 | size_t wxBufferedOutputStream::OnSysWrite(const void *buffer, size_t bufsize) | |
896 | { | |
897 | return m_parent_o_stream->Write(buffer, bufsize).LastWrite(); | |
898 | } | |
899 | ||
900 | off_t wxBufferedOutputStream::OnSysSeek(off_t seek, wxSeekMode mode) | |
901 | { | |
902 | return m_parent_o_stream->SeekO(seek, mode); | |
903 | } | |
904 | ||
905 | off_t wxBufferedOutputStream::OnSysTell() const | |
906 | { | |
907 | return m_parent_o_stream->TellO(); | |
908 | } | |
909 | ||
910 | // ---------------------------------------------------------------------------- | |
911 | // Some IOManip function | |
912 | // ---------------------------------------------------------------------------- | |
913 | ||
914 | wxOutputStream& wxEndL(wxOutputStream& stream) | |
915 | { | |
916 | #ifdef __MSW__ | |
917 | return stream.Write("\r\n", 2); | |
918 | #else | |
919 | #ifdef __WXMAC__ | |
920 | return stream.Write("\r", 1); | |
921 | #else | |
922 | return stream.Write("\n", 1); | |
923 | #endif | |
924 | #endif | |
925 | } | |
926 | ||
927 | #endif | |
928 | // wxUSE_STREAMS |