]>
Commit | Line | Data |
---|---|---|
3d4c6a21 GL |
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 | ||
db138a4c JS |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
ce4169a4 RR |
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 | ||
6d44bf31 | 29 | #include <ctype.h> |
79c3e0e1 | 30 | #include <wx/stream.h> |
6d44bf31 | 31 | #include <wx/datstrm.h> |
123a7fdd | 32 | #include <wx/objstrm.h> |
db138a4c | 33 | |
8ef6a930 GL |
34 | #define BUF_TEMP_SIZE 10000 |
35 | ||
6d44bf31 GL |
36 | // ---------------------------------------------------------------------------- |
37 | // wxStreamBuffer | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
926c550d GL |
40 | #define CHECK_ERROR(err) \ |
41 | if (m_stream->m_lasterror == wxStream_NOERROR) \ | |
42 | m_stream->m_lasterror = err | |
43 | ||
75ed1d15 | 44 | wxStreamBuffer::wxStreamBuffer(wxStreamBase& stream, BufMode mode) |
6d44bf31 | 45 | : m_buffer_start(NULL), m_buffer_end(NULL), m_buffer_pos(NULL), |
fae05df5 | 46 | m_buffer_size(0), m_fixed(TRUE), m_flushable(TRUE), m_stream(&stream), |
8ef6a930 | 47 | m_mode(mode), m_destroybuf(FALSE), m_destroystream(FALSE) |
6d44bf31 GL |
48 | { |
49 | } | |
50 | ||
84b46c35 GL |
51 | wxStreamBuffer::wxStreamBuffer(BufMode mode) |
52 | : m_buffer_start(NULL), m_buffer_end(NULL), m_buffer_pos(NULL), | |
fae05df5 | 53 | m_buffer_size(0), m_fixed(TRUE), m_flushable(FALSE), m_stream(NULL), |
8ef6a930 | 54 | m_mode(mode), m_destroybuf(FALSE), m_destroystream(TRUE) |
84b46c35 | 55 | { |
8ef6a930 | 56 | m_stream = new wxStreamBase(); |
84b46c35 GL |
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; | |
8ef6a930 | 70 | m_destroystream = FALSE; |
84b46c35 GL |
71 | } |
72 | ||
75ed1d15 | 73 | wxStreamBuffer::~wxStreamBuffer() |
6d44bf31 | 74 | { |
84b46c35 GL |
75 | if (m_destroybuf) |
76 | wxDELETEA(m_buffer_start); | |
8ef6a930 GL |
77 | if (m_destroystream) |
78 | delete m_stream; | |
6d44bf31 GL |
79 | } |
80 | ||
6d44bf31 GL |
81 | void wxStreamBuffer::SetBufferIO(char *buffer_start, char *buffer_end) |
82 | { | |
84b46c35 GL |
83 | if (m_destroybuf) |
84 | wxDELETEA(m_buffer_start); | |
885ee235 GL |
85 | m_buffer_start = buffer_start; |
86 | m_buffer_end = buffer_end; | |
6d44bf31 GL |
87 | |
88 | m_buffer_size = m_buffer_end-m_buffer_start; | |
84b46c35 | 89 | m_destroybuf = FALSE; |
75ed1d15 | 90 | ResetBuffer(); |
6d44bf31 GL |
91 | } |
92 | ||
93 | void wxStreamBuffer::SetBufferIO(size_t bufsize) | |
94 | { | |
885ee235 GL |
95 | char *b_start; |
96 | ||
926c550d GL |
97 | if (m_destroybuf) |
98 | wxDELETEA(m_buffer_start); | |
6d44bf31 GL |
99 | |
100 | if (!bufsize) { | |
101 | m_buffer_start = NULL; | |
102 | m_buffer_end = NULL; | |
103 | m_buffer_pos = NULL; | |
104 | m_buffer_size = 0; | |
885ee235 | 105 | return; |
6d44bf31 GL |
106 | } |
107 | ||
885ee235 | 108 | b_start = new char[bufsize]; |
885ee235 | 109 | SetBufferIO(b_start, b_start + bufsize); |
84b46c35 | 110 | m_destroybuf = TRUE; |
6d44bf31 GL |
111 | } |
112 | ||
113 | void wxStreamBuffer::ResetBuffer() | |
114 | { | |
2a040d3f | 115 | m_stream->m_lasterror = wxStream_NOERROR; |
07b73270 | 116 | m_stream->m_lastcount = 0; |
75ed1d15 | 117 | if (m_mode == read) |
6d44bf31 GL |
118 | m_buffer_pos = m_buffer_end; |
119 | else | |
120 | m_buffer_pos = m_buffer_start; | |
121 | } | |
122 | ||
75ed1d15 GL |
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 | ||
84b46c35 | 140 | if (m_buffer_pos == m_buffer_start || !m_flushable) |
75ed1d15 GL |
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 | |
84b46c35 | 170 | m_buffer_size += size; |
75ed1d15 GL |
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 | ||
84b46c35 GL |
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 | ||
9fc0fe37 GL |
189 | if (GetDataLeft() == 0 && !FlushBuffer()) { |
190 | CHECK_ERROR(wxStream_WRITE_ERR); | |
84b46c35 | 191 | return; |
8ef6a930 | 192 | } |
84b46c35 GL |
193 | |
194 | PutToBuffer(&c, 1); | |
195 | m_stream->m_lastcount = 1; | |
196 | } | |
197 | ||
198 | char wxStreamBuffer::GetChar() | |
199 | { | |
200 | char c; | |
201 | ||
202 | wxASSERT(m_stream != NULL); | |
203 | ||
204 | if (!m_buffer_size) { | |
205 | m_stream->OnSysRead(&c, 1); | |
206 | return c; | |
207 | } | |
208 | ||
8ef6a930 | 209 | if (!GetDataLeft()) { |
926c550d | 210 | CHECK_ERROR(wxStream_READ_ERR); |
84b46c35 | 211 | return 0; |
8ef6a930 | 212 | } |
84b46c35 GL |
213 | |
214 | GetFromBuffer(&c, 1); | |
e57e26dd | 215 | |
84b46c35 GL |
216 | m_stream->m_lastcount = 1; |
217 | return c; | |
218 | } | |
219 | ||
8ef6a930 | 220 | size_t wxStreamBuffer::Read(void *buffer, size_t size) |
6d44bf31 | 221 | { |
75ed1d15 | 222 | wxASSERT(m_stream != NULL); |
6d44bf31 | 223 | |
7f42cff1 GL |
224 | if (m_mode == write) |
225 | return 0; | |
226 | ||
6d44bf31 GL |
227 | // ------------------ |
228 | // Buffering disabled | |
229 | // ------------------ | |
230 | ||
2a040d3f | 231 | m_stream->m_lasterror = wxStream_NOERROR; |
926c550d | 232 | if (!m_buffer_size) |
8ef6a930 | 233 | return (m_stream->m_lastcount += m_stream->OnSysRead(buffer, size)); |
6d44bf31 GL |
234 | |
235 | // ----------------- | |
236 | // Buffering enabled | |
237 | // ----------------- | |
238 | size_t buf_left, orig_size = size; | |
6d44bf31 GL |
239 | |
240 | while (size > 0) { | |
75ed1d15 | 241 | buf_left = GetDataLeft(); |
6d44bf31 GL |
242 | |
243 | // First case: the requested buffer is larger than the stream buffer, | |
84b46c35 | 244 | // we split it. |
6d44bf31 | 245 | if (size > buf_left) { |
75ed1d15 GL |
246 | GetFromBuffer(buffer, buf_left); |
247 | size -= buf_left; | |
6d44bf31 GL |
248 | buffer = (char *)buffer + buf_left; // ANSI C++ violation. |
249 | ||
75ed1d15 | 250 | if (!FillBuffer()) { |
926c550d | 251 | CHECK_ERROR(wxStream_READ_ERR); |
8ef6a930 | 252 | return (m_stream->m_lastcount = orig_size-size); |
6d44bf31 GL |
253 | } |
254 | } else { | |
255 | ||
256 | // Second case: we just copy from the stream buffer. | |
75ed1d15 | 257 | GetFromBuffer(buffer, size); |
6d44bf31 GL |
258 | break; |
259 | } | |
260 | } | |
8ef6a930 | 261 | return (m_stream->m_lastcount += orig_size); |
6d44bf31 GL |
262 | } |
263 | ||
8ef6a930 GL |
264 | size_t wxStreamBuffer::Read(wxStreamBuffer *s_buf) |
265 | { | |
266 | char buf[BUF_TEMP_SIZE]; | |
267 | size_t s = 0, bytes_read = BUF_TEMP_SIZE; | |
268 | ||
7f42cff1 GL |
269 | if (m_mode == write) |
270 | return 0; | |
271 | ||
062c4861 | 272 | while (bytes_read != 0) { |
8ef6a930 GL |
273 | bytes_read = Read(buf, bytes_read); |
274 | bytes_read = s_buf->Write(buf, bytes_read); | |
275 | s += bytes_read; | |
276 | } | |
277 | return s; | |
278 | } | |
279 | ||
280 | size_t wxStreamBuffer::Write(const void *buffer, size_t size) | |
6d44bf31 | 281 | { |
75ed1d15 | 282 | wxASSERT(m_stream != NULL); |
6d44bf31 | 283 | |
7f42cff1 GL |
284 | if (m_mode == read) |
285 | return 0; | |
286 | ||
6d44bf31 GL |
287 | // ------------------ |
288 | // Buffering disabled | |
289 | // ------------------ | |
290 | ||
2a040d3f | 291 | m_stream->m_lasterror = wxStream_NOERROR; |
8ef6a930 GL |
292 | if (!m_buffer_size) |
293 | return (m_stream->m_lastcount = m_stream->OnSysWrite(buffer, size)); | |
6d44bf31 GL |
294 | |
295 | // ------------------ | |
296 | // Buffering enabled | |
297 | // ------------------ | |
298 | ||
299 | size_t buf_left, orig_size = size; | |
6d44bf31 GL |
300 | |
301 | while (size > 0) { | |
302 | buf_left = m_buffer_end - m_buffer_pos; | |
303 | ||
304 | // First case: the buffer to write is larger than the stream buffer, | |
305 | // we split it | |
306 | if (size > buf_left) { | |
75ed1d15 | 307 | PutToBuffer(buffer, buf_left); |
6d44bf31 GL |
308 | size -= buf_left; |
309 | buffer = (char *)buffer + buf_left; // ANSI C++ violation. | |
310 | ||
75ed1d15 | 311 | if (!FlushBuffer()) { |
926c550d | 312 | CHECK_ERROR(wxStream_WRITE_ERR); |
8ef6a930 | 313 | return (m_stream->m_lastcount = orig_size-size); |
6d44bf31 | 314 | } |
8ef6a930 | 315 | |
6d44bf31 GL |
316 | m_buffer_pos = m_buffer_start; |
317 | ||
318 | } else { | |
319 | ||
320 | // Second case: just copy it in the stream buffer. | |
75ed1d15 | 321 | PutToBuffer(buffer, size); |
6d44bf31 GL |
322 | break; |
323 | } | |
324 | } | |
8ef6a930 GL |
325 | return (m_stream->m_lastcount = orig_size); |
326 | } | |
327 | ||
328 | size_t wxStreamBuffer::Write(wxStreamBuffer *sbuf) | |
329 | { | |
330 | char buf[BUF_TEMP_SIZE]; | |
eb4e516d | 331 | size_t s = 0, bytes_count = BUF_TEMP_SIZE, b_count2; |
fae05df5 | 332 | wxInputStream *in_stream; |
8ef6a930 | 333 | |
7f42cff1 GL |
334 | if (m_mode == read) |
335 | return 0; | |
336 | ||
fae05df5 GL |
337 | in_stream = (wxInputStream *)sbuf->Stream(); |
338 | ||
8ef6a930 | 339 | while (bytes_count == BUF_TEMP_SIZE) { |
eb4e516d GL |
340 | b_count2 = sbuf->Read(buf, bytes_count); |
341 | bytes_count = Write(buf, b_count2); | |
342 | if (b_count2 > bytes_count) | |
fae05df5 | 343 | in_stream->Ungetch(buf+bytes_count, b_count2-bytes_count); |
8ef6a930 GL |
344 | s += bytes_count; |
345 | } | |
346 | return s; | |
75ed1d15 GL |
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 | ||
8ef6a930 | 355 | if (!m_flushable) { |
84b46c35 GL |
356 | diff = pos + GetIntPosition(); |
357 | if (diff < 0 || diff > last_access) | |
358 | return wxInvalidOffset; | |
359 | SetIntPosition(diff); | |
360 | return diff; | |
361 | } | |
362 | ||
75ed1d15 GL |
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) ) { | |
d4feedcf GL |
374 | // We must take into account the fact that we have read something |
375 | // previously. | |
f1f5976f | 376 | ret_off = m_stream->OnSysSeek(diff-last_access, wxFromCurrent); |
75ed1d15 GL |
377 | ResetBuffer(); |
378 | return ret_off; | |
379 | } else { | |
380 | SetIntPosition(diff); | |
381 | return pos; | |
382 | } | |
383 | } | |
384 | case wxFromEnd: | |
385 | // Hard to compute: always seek to the requested position. | |
386 | ret_off = m_stream->OnSysSeek(pos, wxFromEnd); | |
387 | ResetBuffer(); | |
388 | return ret_off; | |
389 | } | |
390 | return wxInvalidOffset; | |
391 | } | |
392 | ||
393 | off_t wxStreamBuffer::Tell() const | |
394 | { | |
395 | off_t pos; | |
396 | ||
8ef6a930 | 397 | if (m_flushable) { |
84b46c35 GL |
398 | pos = m_stream->OnSysTell(); |
399 | if (pos == wxInvalidOffset) | |
400 | return wxInvalidOffset; | |
401 | return pos - GetLastAccess() + GetIntPosition(); | |
402 | } else | |
403 | return GetIntPosition(); | |
75ed1d15 GL |
404 | } |
405 | ||
8ef6a930 | 406 | size_t wxStreamBuffer::GetDataLeft() |
75ed1d15 | 407 | { |
8ef6a930 GL |
408 | if (m_buffer_end == m_buffer_pos && m_flushable) |
409 | FillBuffer(); | |
75ed1d15 GL |
410 | return m_buffer_end-m_buffer_pos; |
411 | } | |
412 | ||
413 | // ---------------------------------------------------------------------------- | |
414 | // wxStreamBase | |
415 | // ---------------------------------------------------------------------------- | |
416 | ||
417 | wxStreamBase::wxStreamBase() | |
418 | { | |
419 | m_lasterror = wxStream_NOERROR; | |
420 | m_lastcount = 0; | |
421 | } | |
422 | ||
423 | wxStreamBase::~wxStreamBase() | |
424 | { | |
425 | } | |
426 | ||
df875e59 | 427 | size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer), size_t WXUNUSED(size)) |
75ed1d15 GL |
428 | { |
429 | return 0; | |
430 | } | |
431 | ||
df875e59 | 432 | size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer), size_t WXUNUSED(bufsize)) |
75ed1d15 GL |
433 | { |
434 | return 0; | |
435 | } | |
436 | ||
df875e59 | 437 | off_t wxStreamBase::OnSysSeek(off_t WXUNUSED(seek), wxSeekMode WXUNUSED(mode)) |
75ed1d15 GL |
438 | { |
439 | return wxInvalidOffset; | |
440 | } | |
441 | ||
84b46c35 | 442 | off_t wxStreamBase::OnSysTell() const |
75ed1d15 GL |
443 | { |
444 | return wxInvalidOffset; | |
6d44bf31 GL |
445 | } |
446 | ||
1678ad78 GL |
447 | // ---------------------------------------------------------------------------- |
448 | // wxInputStream | |
449 | // ---------------------------------------------------------------------------- | |
450 | ||
3d4c6a21 | 451 | wxInputStream::wxInputStream() |
fae05df5 GL |
452 | : wxStreamBase(), |
453 | m_wback(NULL), m_wbacksize(0), m_wbackcur(0) | |
3d4c6a21 | 454 | { |
6d44bf31 GL |
455 | } |
456 | ||
fae05df5 | 457 | wxInputStream::~wxInputStream() |
6d44bf31 | 458 | { |
fae05df5 GL |
459 | if (m_wback) |
460 | free(m_wback); | |
3d4c6a21 GL |
461 | } |
462 | ||
fae05df5 | 463 | char *wxInputStream::AllocSpaceWBack(size_t needed_size) |
3d4c6a21 | 464 | { |
fae05df5 GL |
465 | char *temp_b; |
466 | ||
467 | m_wbacksize += needed_size; | |
468 | ||
469 | if (!m_wback) | |
470 | temp_b = (char *)malloc(m_wbacksize); | |
471 | else | |
472 | temp_b = (char *)realloc(m_wback, m_wbacksize); | |
473 | ||
474 | if (!temp_b) | |
475 | return NULL; | |
476 | m_wback = temp_b; | |
477 | ||
478 | return (char *)(m_wback+(m_wbacksize-needed_size)); | |
6d44bf31 GL |
479 | } |
480 | ||
fae05df5 | 481 | size_t wxInputStream::GetWBack(char *buf, size_t bsize) |
6d44bf31 | 482 | { |
fae05df5 GL |
483 | size_t s_toget = m_wbacksize-m_wbackcur; |
484 | ||
a324a7bc GL |
485 | if (!m_wback) |
486 | return 0; | |
487 | ||
fae05df5 GL |
488 | if (bsize < s_toget) |
489 | s_toget = bsize; | |
490 | ||
491 | memcpy(buf, (m_wback+m_wbackcur), s_toget); | |
492 | ||
493 | m_wbackcur += s_toget; | |
494 | if (m_wbackcur == m_wbacksize) { | |
495 | free(m_wback); | |
496 | m_wback = (char *)NULL; | |
497 | m_wbacksize = 0; | |
498 | m_wbackcur = 0; | |
499 | } | |
500 | ||
501 | return s_toget; | |
6d44bf31 GL |
502 | } |
503 | ||
8f7173ab | 504 | size_t wxInputStream::Ungetch(const void *buf, size_t bufsize) |
fae05df5 GL |
505 | { |
506 | char *ptrback; | |
507 | ||
508 | ptrback = AllocSpaceWBack(bufsize); | |
509 | if (!ptrback) | |
510 | return 0; | |
1e3eca9d | 511 | |
fae05df5 GL |
512 | memcpy(ptrback, buf, bufsize); |
513 | return bufsize; | |
514 | } | |
515 | ||
516 | bool wxInputStream::Ungetch(char c) | |
1e3eca9d | 517 | { |
fae05df5 | 518 | char *ptrback; |
1e3eca9d | 519 | |
fae05df5 GL |
520 | ptrback = AllocSpaceWBack(1); |
521 | if (!ptrback) | |
522 | return FALSE; | |
1e3eca9d | 523 | |
fae05df5 GL |
524 | *ptrback = c; |
525 | return TRUE; | |
526 | } | |
527 | ||
528 | char wxInputStream::GetC() | |
529 | { | |
530 | char c; | |
531 | Read(&c, 1); | |
532 | return c; | |
1e3eca9d GL |
533 | } |
534 | ||
6d44bf31 GL |
535 | wxInputStream& wxInputStream::Read(void *buffer, size_t size) |
536 | { | |
fae05df5 GL |
537 | size_t retsize; |
538 | char *buf = (char *)buffer; | |
539 | ||
540 | retsize = GetWBack(buf, size); | |
541 | if (retsize == size) { | |
542 | m_lastcount = size; | |
543 | m_lasterror = wxStream_NOERROR; | |
544 | return *this; | |
545 | } | |
546 | size -= retsize; | |
547 | buf += retsize; | |
548 | ||
549 | m_lastcount = OnSysRead(buf, size); | |
6d44bf31 | 550 | return *this; |
3d4c6a21 GL |
551 | } |
552 | ||
75ed1d15 GL |
553 | char wxInputStream::Peek() |
554 | { | |
fae05df5 | 555 | char c; |
75ed1d15 | 556 | |
fae05df5 GL |
557 | Read(&c, 1); |
558 | if (m_lasterror == wxStream_NOERROR) { | |
559 | Ungetch(c); | |
560 | return c; | |
561 | } | |
562 | return 0; | |
75ed1d15 GL |
563 | } |
564 | ||
3d4c6a21 GL |
565 | wxInputStream& wxInputStream::Read(wxOutputStream& stream_out) |
566 | { | |
567 | char buf[BUF_TEMP_SIZE]; | |
568 | size_t bytes_read = BUF_TEMP_SIZE; | |
569 | ||
8ef6a930 | 570 | while (bytes_read == BUF_TEMP_SIZE) { |
3d4c6a21 | 571 | bytes_read = Read(buf, bytes_read).LastRead(); |
8ef6a930 | 572 | bytes_read = stream_out.Write(buf, bytes_read).LastWrite(); |
3d4c6a21 GL |
573 | } |
574 | return *this; | |
575 | } | |
576 | ||
75ed1d15 GL |
577 | off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode) |
578 | { | |
60b6c062 | 579 | return OnSysSeek(pos, mode); |
75ed1d15 GL |
580 | } |
581 | ||
582 | off_t wxInputStream::TellI() const | |
583 | { | |
60b6c062 | 584 | return OnSysTell(); |
75ed1d15 GL |
585 | } |
586 | ||
587 | // -------------------- | |
588 | // Overloaded operators | |
589 | // -------------------- | |
590 | ||
fae05df5 GL |
591 | #if wxUSE_SERIAL |
592 | wxInputStream& wxInputStream::operator>>(wxObject *& obj) | |
1678ad78 | 593 | { |
fae05df5 GL |
594 | wxObjectInputStream obj_s(*this); |
595 | obj = obj_s.LoadObject(); | |
1678ad78 GL |
596 | return *this; |
597 | } | |
fae05df5 | 598 | #endif |
1678ad78 | 599 | |
1678ad78 | 600 | |
fae05df5 GL |
601 | // ---------------------------------------------------------------------------- |
602 | // wxOutputStream | |
603 | // ---------------------------------------------------------------------------- | |
604 | wxOutputStream::wxOutputStream() | |
605 | : wxStreamBase() | |
1678ad78 | 606 | { |
1678ad78 GL |
607 | } |
608 | ||
fae05df5 | 609 | wxOutputStream::~wxOutputStream() |
123a7fdd | 610 | { |
123a7fdd GL |
611 | } |
612 | ||
fae05df5 | 613 | wxOutputStream& wxOutputStream::Write(const void *buffer, size_t size) |
1678ad78 | 614 | { |
fae05df5 | 615 | m_lastcount = OnSysWrite(buffer, size); |
1678ad78 GL |
616 | return *this; |
617 | } | |
618 | ||
fae05df5 | 619 | wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in) |
38830220 | 620 | { |
fae05df5 | 621 | stream_in.Read(*this); |
38830220 RR |
622 | return *this; |
623 | } | |
624 | ||
fae05df5 | 625 | off_t wxOutputStream::TellO() const |
38830220 | 626 | { |
60b6c062 | 627 | return OnSysTell(); |
38830220 RR |
628 | } |
629 | ||
fae05df5 | 630 | off_t wxOutputStream::SeekO(off_t pos, wxSeekMode mode) |
38830220 | 631 | { |
60b6c062 | 632 | return OnSysSeek(pos, mode); |
38830220 RR |
633 | } |
634 | ||
fae05df5 | 635 | void wxOutputStream::Sync() |
1678ad78 | 636 | { |
1678ad78 GL |
637 | } |
638 | ||
47d67540 | 639 | #if wxUSE_SERIAL |
fae05df5 | 640 | wxOutputStream& wxOutputStream::operator<<(wxObject& obj) |
123a7fdd | 641 | { |
fae05df5 GL |
642 | wxObjectOutputStream obj_s(*this); |
643 | obj_s.SaveObject(obj); | |
123a7fdd GL |
644 | return *this; |
645 | } | |
fcc6dddd | 646 | #endif |
123a7fdd | 647 | |
1678ad78 | 648 | // ---------------------------------------------------------------------------- |
fae05df5 | 649 | // wxFilterInputStream |
1678ad78 | 650 | // ---------------------------------------------------------------------------- |
fae05df5 GL |
651 | wxFilterInputStream::wxFilterInputStream() |
652 | : wxInputStream() | |
3d4c6a21 | 653 | { |
6d44bf31 GL |
654 | } |
655 | ||
fae05df5 GL |
656 | wxFilterInputStream::wxFilterInputStream(wxInputStream& stream) |
657 | : wxInputStream() | |
6d44bf31 | 658 | { |
fae05df5 | 659 | m_parent_i_stream = &stream; |
3d4c6a21 GL |
660 | } |
661 | ||
fae05df5 | 662 | wxFilterInputStream::~wxFilterInputStream() |
3d4c6a21 | 663 | { |
6d44bf31 GL |
664 | } |
665 | ||
fae05df5 GL |
666 | // ---------------------------------------------------------------------------- |
667 | // wxFilterOutputStream | |
668 | // ---------------------------------------------------------------------------- | |
669 | wxFilterOutputStream::wxFilterOutputStream() | |
670 | : wxOutputStream() | |
6d44bf31 | 671 | { |
3d4c6a21 GL |
672 | } |
673 | ||
fae05df5 GL |
674 | wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream) |
675 | : wxOutputStream() | |
3d4c6a21 | 676 | { |
fae05df5 | 677 | m_parent_o_stream = &stream; |
6d44bf31 GL |
678 | } |
679 | ||
fae05df5 | 680 | wxFilterOutputStream::~wxFilterOutputStream() |
6d44bf31 | 681 | { |
6d44bf31 GL |
682 | } |
683 | ||
fae05df5 GL |
684 | // ---------------------------------------------------------------------------- |
685 | // wxBufferedInputStream | |
686 | // ---------------------------------------------------------------------------- | |
687 | wxBufferedInputStream::wxBufferedInputStream(wxInputStream& s) | |
688 | : wxFilterInputStream(s) | |
6d44bf31 | 689 | { |
fae05df5 GL |
690 | m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read); |
691 | m_i_streambuf->SetBufferIO(1024); | |
6d44bf31 GL |
692 | } |
693 | ||
fae05df5 | 694 | wxBufferedInputStream::~wxBufferedInputStream() |
6d44bf31 | 695 | { |
fae05df5 | 696 | delete m_i_streambuf; |
6d44bf31 GL |
697 | } |
698 | ||
fae05df5 | 699 | wxInputStream& wxBufferedInputStream::Read(void *buffer, size_t size) |
1e3eca9d | 700 | { |
fae05df5 GL |
701 | size_t retsize; |
702 | char *buf = (char *)buffer; | |
1e3eca9d | 703 | |
fae05df5 | 704 | retsize = GetWBack(buf, size); |
a324a7bc | 705 | m_lastcount = retsize; |
fae05df5 | 706 | if (retsize == size) { |
fae05df5 GL |
707 | m_lasterror = wxStream_NOERROR; |
708 | return *this; | |
709 | } | |
710 | size -= retsize; | |
711 | buf += retsize; | |
1e3eca9d | 712 | |
fae05df5 | 713 | m_i_streambuf->Read(buf, size); |
6d44bf31 | 714 | |
fae05df5 | 715 | return *this; |
6d44bf31 GL |
716 | } |
717 | ||
fae05df5 | 718 | off_t wxBufferedInputStream::SeekI(off_t pos, wxSeekMode mode) |
6d44bf31 | 719 | { |
fae05df5 | 720 | return m_i_streambuf->Seek(pos, mode); |
6d44bf31 GL |
721 | } |
722 | ||
fae05df5 | 723 | off_t wxBufferedInputStream::TellI() const |
6d44bf31 | 724 | { |
fae05df5 | 725 | return m_i_streambuf->Tell(); |
38830220 | 726 | } |
6d44bf31 | 727 | |
fae05df5 | 728 | size_t wxBufferedInputStream::OnSysRead(void *buffer, size_t bufsize) |
38830220 | 729 | { |
fae05df5 | 730 | return m_parent_i_stream->Read(buffer, bufsize).LastRead(); |
38830220 RR |
731 | } |
732 | ||
fae05df5 | 733 | off_t wxBufferedInputStream::OnSysSeek(off_t seek, wxSeekMode mode) |
38830220 | 734 | { |
fae05df5 | 735 | return m_parent_i_stream->SeekI(seek, mode); |
6d44bf31 GL |
736 | } |
737 | ||
fae05df5 | 738 | off_t wxBufferedInputStream::OnSysTell() const |
6d44bf31 | 739 | { |
fae05df5 | 740 | return m_parent_i_stream->TellI(); |
38830220 | 741 | } |
6d44bf31 | 742 | |
fae05df5 GL |
743 | // ---------------------------------------------------------------------------- |
744 | // wxBufferedOutputStream | |
745 | // ---------------------------------------------------------------------------- | |
6d44bf31 | 746 | |
fae05df5 GL |
747 | wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& s) |
748 | : wxFilterOutputStream(s) | |
6d44bf31 | 749 | { |
fae05df5 GL |
750 | m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write); |
751 | m_o_streambuf->SetBufferIO(1024); | |
6d44bf31 GL |
752 | } |
753 | ||
fae05df5 | 754 | wxBufferedOutputStream::~wxBufferedOutputStream() |
6d44bf31 | 755 | { |
fae05df5 | 756 | delete m_o_streambuf; |
3d4c6a21 GL |
757 | } |
758 | ||
fae05df5 | 759 | wxOutputStream& wxBufferedOutputStream::Write(const void *buffer, size_t size) |
123a7fdd | 760 | { |
a324a7bc | 761 | m_lastcount = 0; |
fae05df5 | 762 | m_o_streambuf->Write(buffer, size); |
123a7fdd GL |
763 | return *this; |
764 | } | |
765 | ||
fae05df5 | 766 | off_t wxBufferedOutputStream::SeekO(off_t pos, wxSeekMode mode) |
f4ada568 | 767 | { |
fae05df5 | 768 | return m_o_streambuf->Seek(pos, mode); |
f4ada568 GL |
769 | } |
770 | ||
fae05df5 | 771 | off_t wxBufferedOutputStream::TellO() const |
3d4c6a21 | 772 | { |
fae05df5 | 773 | return m_o_streambuf->Tell(); |
3d4c6a21 GL |
774 | } |
775 | ||
fae05df5 | 776 | void wxBufferedOutputStream::Sync() |
3d4c6a21 | 777 | { |
fae05df5 GL |
778 | m_o_streambuf->FlushBuffer(); |
779 | m_parent_o_stream->Sync(); | |
3d4c6a21 | 780 | } |
219f895a | 781 | |
fae05df5 | 782 | size_t wxBufferedOutputStream::OnSysWrite(const void *buffer, size_t bufsize) |
f4ada568 | 783 | { |
fae05df5 | 784 | return m_parent_o_stream->Write(buffer, bufsize).LastWrite(); |
f4ada568 GL |
785 | } |
786 | ||
fae05df5 | 787 | off_t wxBufferedOutputStream::OnSysSeek(off_t seek, wxSeekMode mode) |
219f895a | 788 | { |
fae05df5 | 789 | return m_parent_o_stream->SeekO(seek, mode); |
219f895a RR |
790 | } |
791 | ||
fae05df5 | 792 | off_t wxBufferedOutputStream::OnSysTell() const |
219f895a | 793 | { |
fae05df5 | 794 | return m_parent_o_stream->TellO(); |
219f895a | 795 | } |
6d44bf31 | 796 | |
6d44bf31 GL |
797 | // ---------------------------------------------------------------------------- |
798 | // Some IOManip function | |
799 | // ---------------------------------------------------------------------------- | |
800 | ||
801 | wxOutputStream& wxEndL(wxOutputStream& stream) | |
802 | { | |
803 | #ifdef __MSW__ | |
804 | return stream.Write("\r\n", 2); | |
1e3eca9d GL |
805 | #else |
806 | #ifdef __WXMAC__ | |
807 | return stream.Write("\r", 1); | |
6d44bf31 GL |
808 | #else |
809 | return stream.Write("\n", 1); | |
810 | #endif | |
1e3eca9d | 811 | #endif |
6d44bf31 | 812 | } |
ce4169a4 RR |
813 | |
814 | #endif | |
815 | // wxUSE_STREAMS |