]>
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> |
3096bd2f VZ |
30 | #include "wx/stream.h" |
31 | #include "wx/datstrm.h" | |
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 | ||
6319afe3 GL |
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 | ||
84b46c35 GL |
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 | ||
8ef6a930 | 226 | if (!GetDataLeft()) { |
926c550d | 227 | CHECK_ERROR(wxStream_READ_ERR); |
84b46c35 | 228 | return 0; |
8ef6a930 | 229 | } |
84b46c35 GL |
230 | |
231 | GetFromBuffer(&c, 1); | |
e57e26dd | 232 | |
84b46c35 GL |
233 | m_stream->m_lastcount = 1; |
234 | return c; | |
235 | } | |
236 | ||
8ef6a930 | 237 | size_t wxStreamBuffer::Read(void *buffer, size_t size) |
6d44bf31 | 238 | { |
75ed1d15 | 239 | wxASSERT(m_stream != NULL); |
6d44bf31 | 240 | |
7f42cff1 GL |
241 | if (m_mode == write) |
242 | return 0; | |
243 | ||
6d44bf31 GL |
244 | // ------------------ |
245 | // Buffering disabled | |
246 | // ------------------ | |
247 | ||
2a040d3f | 248 | m_stream->m_lasterror = wxStream_NOERROR; |
926c550d | 249 | if (!m_buffer_size) |
8ef6a930 | 250 | return (m_stream->m_lastcount += m_stream->OnSysRead(buffer, size)); |
6d44bf31 GL |
251 | |
252 | // ----------------- | |
253 | // Buffering enabled | |
254 | // ----------------- | |
255 | size_t buf_left, orig_size = size; | |
6d44bf31 GL |
256 | |
257 | while (size > 0) { | |
75ed1d15 | 258 | buf_left = GetDataLeft(); |
6d44bf31 GL |
259 | |
260 | // First case: the requested buffer is larger than the stream buffer, | |
84b46c35 | 261 | // we split it. |
6d44bf31 | 262 | if (size > buf_left) { |
75ed1d15 GL |
263 | GetFromBuffer(buffer, buf_left); |
264 | size -= buf_left; | |
6d44bf31 GL |
265 | buffer = (char *)buffer + buf_left; // ANSI C++ violation. |
266 | ||
75ed1d15 | 267 | if (!FillBuffer()) { |
6319afe3 | 268 | CHECK_ERROR(wxStream_EOF); |
8ef6a930 | 269 | return (m_stream->m_lastcount = orig_size-size); |
6d44bf31 GL |
270 | } |
271 | } else { | |
272 | ||
273 | // Second case: we just copy from the stream buffer. | |
75ed1d15 | 274 | GetFromBuffer(buffer, size); |
6d44bf31 GL |
275 | break; |
276 | } | |
277 | } | |
8ef6a930 | 278 | return (m_stream->m_lastcount += orig_size); |
6d44bf31 GL |
279 | } |
280 | ||
8ef6a930 GL |
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 | ||
7f42cff1 GL |
286 | if (m_mode == write) |
287 | return 0; | |
288 | ||
062c4861 | 289 | while (bytes_read != 0) { |
8ef6a930 GL |
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) | |
6d44bf31 | 298 | { |
75ed1d15 | 299 | wxASSERT(m_stream != NULL); |
6d44bf31 | 300 | |
7f42cff1 GL |
301 | if (m_mode == read) |
302 | return 0; | |
303 | ||
6d44bf31 GL |
304 | // ------------------ |
305 | // Buffering disabled | |
306 | // ------------------ | |
307 | ||
2a040d3f | 308 | m_stream->m_lasterror = wxStream_NOERROR; |
8ef6a930 GL |
309 | if (!m_buffer_size) |
310 | return (m_stream->m_lastcount = m_stream->OnSysWrite(buffer, size)); | |
6d44bf31 GL |
311 | |
312 | // ------------------ | |
313 | // Buffering enabled | |
314 | // ------------------ | |
315 | ||
316 | size_t buf_left, orig_size = size; | |
6d44bf31 GL |
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) { | |
75ed1d15 | 324 | PutToBuffer(buffer, buf_left); |
6d44bf31 GL |
325 | size -= buf_left; |
326 | buffer = (char *)buffer + buf_left; // ANSI C++ violation. | |
327 | ||
75ed1d15 | 328 | if (!FlushBuffer()) { |
926c550d | 329 | CHECK_ERROR(wxStream_WRITE_ERR); |
8ef6a930 | 330 | return (m_stream->m_lastcount = orig_size-size); |
6d44bf31 | 331 | } |
8ef6a930 | 332 | |
6d44bf31 GL |
333 | m_buffer_pos = m_buffer_start; |
334 | ||
335 | } else { | |
336 | ||
337 | // Second case: just copy it in the stream buffer. | |
75ed1d15 | 338 | PutToBuffer(buffer, size); |
6d44bf31 GL |
339 | break; |
340 | } | |
341 | } | |
8ef6a930 GL |
342 | return (m_stream->m_lastcount = orig_size); |
343 | } | |
344 | ||
345 | size_t wxStreamBuffer::Write(wxStreamBuffer *sbuf) | |
346 | { | |
347 | char buf[BUF_TEMP_SIZE]; | |
eb4e516d | 348 | size_t s = 0, bytes_count = BUF_TEMP_SIZE, b_count2; |
fae05df5 | 349 | wxInputStream *in_stream; |
8ef6a930 | 350 | |
7f42cff1 GL |
351 | if (m_mode == read) |
352 | return 0; | |
353 | ||
fae05df5 GL |
354 | in_stream = (wxInputStream *)sbuf->Stream(); |
355 | ||
8ef6a930 | 356 | while (bytes_count == BUF_TEMP_SIZE) { |
eb4e516d GL |
357 | b_count2 = sbuf->Read(buf, bytes_count); |
358 | bytes_count = Write(buf, b_count2); | |
359 | if (b_count2 > bytes_count) | |
fae05df5 | 360 | in_stream->Ungetch(buf+bytes_count, b_count2-bytes_count); |
8ef6a930 GL |
361 | s += bytes_count; |
362 | } | |
363 | return s; | |
75ed1d15 GL |
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 | ||
8ef6a930 | 372 | if (!m_flushable) { |
84b46c35 GL |
373 | diff = pos + GetIntPosition(); |
374 | if (diff < 0 || diff > last_access) | |
375 | return wxInvalidOffset; | |
376 | SetIntPosition(diff); | |
377 | return diff; | |
378 | } | |
379 | ||
75ed1d15 GL |
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) ) { | |
d4feedcf GL |
391 | // We must take into account the fact that we have read something |
392 | // previously. | |
f1f5976f | 393 | ret_off = m_stream->OnSysSeek(diff-last_access, wxFromCurrent); |
75ed1d15 GL |
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 | ||
8ef6a930 | 414 | if (m_flushable) { |
84b46c35 GL |
415 | pos = m_stream->OnSysTell(); |
416 | if (pos == wxInvalidOffset) | |
417 | return wxInvalidOffset; | |
418 | return pos - GetLastAccess() + GetIntPosition(); | |
419 | } else | |
420 | return GetIntPosition(); | |
75ed1d15 GL |
421 | } |
422 | ||
8ef6a930 | 423 | size_t wxStreamBuffer::GetDataLeft() |
75ed1d15 | 424 | { |
8ef6a930 GL |
425 | if (m_buffer_end == m_buffer_pos && m_flushable) |
426 | FillBuffer(); | |
75ed1d15 GL |
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 | ||
df875e59 | 444 | size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer), size_t WXUNUSED(size)) |
75ed1d15 GL |
445 | { |
446 | return 0; | |
447 | } | |
448 | ||
df875e59 | 449 | size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer), size_t WXUNUSED(bufsize)) |
75ed1d15 GL |
450 | { |
451 | return 0; | |
452 | } | |
453 | ||
df875e59 | 454 | off_t wxStreamBase::OnSysSeek(off_t WXUNUSED(seek), wxSeekMode WXUNUSED(mode)) |
75ed1d15 GL |
455 | { |
456 | return wxInvalidOffset; | |
457 | } | |
458 | ||
84b46c35 | 459 | off_t wxStreamBase::OnSysTell() const |
75ed1d15 GL |
460 | { |
461 | return wxInvalidOffset; | |
6d44bf31 GL |
462 | } |
463 | ||
1678ad78 GL |
464 | // ---------------------------------------------------------------------------- |
465 | // wxInputStream | |
466 | // ---------------------------------------------------------------------------- | |
467 | ||
3d4c6a21 | 468 | wxInputStream::wxInputStream() |
fae05df5 GL |
469 | : wxStreamBase(), |
470 | m_wback(NULL), m_wbacksize(0), m_wbackcur(0) | |
3d4c6a21 | 471 | { |
6d44bf31 GL |
472 | } |
473 | ||
fae05df5 | 474 | wxInputStream::~wxInputStream() |
6d44bf31 | 475 | { |
fae05df5 GL |
476 | if (m_wback) |
477 | free(m_wback); | |
3d4c6a21 GL |
478 | } |
479 | ||
fae05df5 | 480 | char *wxInputStream::AllocSpaceWBack(size_t needed_size) |
3d4c6a21 | 481 | { |
fae05df5 | 482 | char *temp_b; |
783ff666 | 483 | size_t old_size; |
fae05df5 | 484 | |
783ff666 | 485 | old_size = m_wbacksize; |
fae05df5 GL |
486 | m_wbacksize += needed_size; |
487 | ||
488 | if (!m_wback) | |
489 | temp_b = (char *)malloc(m_wbacksize); | |
490 | else | |
491 | temp_b = (char *)realloc(m_wback, m_wbacksize); | |
492 | ||
493 | if (!temp_b) | |
494 | return NULL; | |
495 | m_wback = temp_b; | |
783ff666 GL |
496 | m_wbackcur += needed_size; |
497 | ||
498 | memmove(m_wback+needed_size, m_wback, old_size); | |
fae05df5 | 499 | |
783ff666 | 500 | return (char *)(m_wback); |
6d44bf31 GL |
501 | } |
502 | ||
fae05df5 | 503 | size_t wxInputStream::GetWBack(char *buf, size_t bsize) |
6d44bf31 | 504 | { |
783ff666 | 505 | size_t s_toget = m_wbackcur; |
fae05df5 | 506 | |
a324a7bc GL |
507 | if (!m_wback) |
508 | return 0; | |
509 | ||
fae05df5 GL |
510 | if (bsize < s_toget) |
511 | s_toget = bsize; | |
512 | ||
783ff666 | 513 | memcpy(buf, (m_wback+m_wbackcur-bsize), s_toget); |
fae05df5 | 514 | |
783ff666 GL |
515 | m_wbackcur -= s_toget; |
516 | if (m_wbackcur == 0) { | |
fae05df5 GL |
517 | free(m_wback); |
518 | m_wback = (char *)NULL; | |
519 | m_wbacksize = 0; | |
520 | m_wbackcur = 0; | |
521 | } | |
522 | ||
523 | return s_toget; | |
6d44bf31 GL |
524 | } |
525 | ||
8f7173ab | 526 | size_t wxInputStream::Ungetch(const void *buf, size_t bufsize) |
fae05df5 GL |
527 | { |
528 | char *ptrback; | |
529 | ||
530 | ptrback = AllocSpaceWBack(bufsize); | |
531 | if (!ptrback) | |
532 | return 0; | |
1e3eca9d | 533 | |
fae05df5 GL |
534 | memcpy(ptrback, buf, bufsize); |
535 | return bufsize; | |
536 | } | |
537 | ||
538 | bool wxInputStream::Ungetch(char c) | |
1e3eca9d | 539 | { |
fae05df5 | 540 | char *ptrback; |
1e3eca9d | 541 | |
fae05df5 GL |
542 | ptrback = AllocSpaceWBack(1); |
543 | if (!ptrback) | |
544 | return FALSE; | |
1e3eca9d | 545 | |
fae05df5 GL |
546 | *ptrback = c; |
547 | return TRUE; | |
548 | } | |
549 | ||
550 | char wxInputStream::GetC() | |
551 | { | |
552 | char c; | |
553 | Read(&c, 1); | |
554 | return c; | |
1e3eca9d GL |
555 | } |
556 | ||
6d44bf31 GL |
557 | wxInputStream& wxInputStream::Read(void *buffer, size_t size) |
558 | { | |
fae05df5 GL |
559 | size_t retsize; |
560 | char *buf = (char *)buffer; | |
561 | ||
562 | retsize = GetWBack(buf, size); | |
563 | if (retsize == size) { | |
564 | m_lastcount = size; | |
565 | m_lasterror = wxStream_NOERROR; | |
566 | return *this; | |
567 | } | |
568 | size -= retsize; | |
569 | buf += retsize; | |
570 | ||
571 | m_lastcount = OnSysRead(buf, size); | |
6d44bf31 | 572 | return *this; |
3d4c6a21 GL |
573 | } |
574 | ||
75ed1d15 GL |
575 | char wxInputStream::Peek() |
576 | { | |
fae05df5 | 577 | char c; |
75ed1d15 | 578 | |
fae05df5 GL |
579 | Read(&c, 1); |
580 | if (m_lasterror == wxStream_NOERROR) { | |
581 | Ungetch(c); | |
582 | return c; | |
583 | } | |
584 | return 0; | |
75ed1d15 GL |
585 | } |
586 | ||
3d4c6a21 GL |
587 | wxInputStream& wxInputStream::Read(wxOutputStream& stream_out) |
588 | { | |
589 | char buf[BUF_TEMP_SIZE]; | |
590 | size_t bytes_read = BUF_TEMP_SIZE; | |
591 | ||
8ef6a930 | 592 | while (bytes_read == BUF_TEMP_SIZE) { |
3d4c6a21 | 593 | bytes_read = Read(buf, bytes_read).LastRead(); |
8ef6a930 | 594 | bytes_read = stream_out.Write(buf, bytes_read).LastWrite(); |
3d4c6a21 GL |
595 | } |
596 | return *this; | |
597 | } | |
598 | ||
75ed1d15 GL |
599 | off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode) |
600 | { | |
fe8aa971 SB |
601 | //should be check and improve, just to remove a slight bug ! |
602 | // I don't know whether it should be put as well in wxFileInputStream::OnSysSeek ? | |
603 | if (m_lasterror==wxSTREAM_EOF) m_lasterror=wxSTREAM_NOERROR; | |
604 | ||
60b6c062 | 605 | return OnSysSeek(pos, mode); |
75ed1d15 GL |
606 | } |
607 | ||
608 | off_t wxInputStream::TellI() const | |
609 | { | |
60b6c062 | 610 | return OnSysTell(); |
75ed1d15 GL |
611 | } |
612 | ||
613 | // -------------------- | |
614 | // Overloaded operators | |
615 | // -------------------- | |
616 | ||
fae05df5 GL |
617 | #if wxUSE_SERIAL |
618 | wxInputStream& wxInputStream::operator>>(wxObject *& obj) | |
1678ad78 | 619 | { |
fae05df5 GL |
620 | wxObjectInputStream obj_s(*this); |
621 | obj = obj_s.LoadObject(); | |
1678ad78 GL |
622 | return *this; |
623 | } | |
fae05df5 | 624 | #endif |
1678ad78 | 625 | |
1678ad78 | 626 | |
fae05df5 GL |
627 | // ---------------------------------------------------------------------------- |
628 | // wxOutputStream | |
629 | // ---------------------------------------------------------------------------- | |
630 | wxOutputStream::wxOutputStream() | |
631 | : wxStreamBase() | |
1678ad78 | 632 | { |
1678ad78 GL |
633 | } |
634 | ||
fae05df5 | 635 | wxOutputStream::~wxOutputStream() |
123a7fdd | 636 | { |
123a7fdd GL |
637 | } |
638 | ||
fae05df5 | 639 | wxOutputStream& wxOutputStream::Write(const void *buffer, size_t size) |
1678ad78 | 640 | { |
fae05df5 | 641 | m_lastcount = OnSysWrite(buffer, size); |
1678ad78 GL |
642 | return *this; |
643 | } | |
644 | ||
fae05df5 | 645 | wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in) |
38830220 | 646 | { |
fae05df5 | 647 | stream_in.Read(*this); |
38830220 RR |
648 | return *this; |
649 | } | |
650 | ||
fae05df5 | 651 | off_t wxOutputStream::TellO() const |
38830220 | 652 | { |
60b6c062 | 653 | return OnSysTell(); |
38830220 RR |
654 | } |
655 | ||
fae05df5 | 656 | off_t wxOutputStream::SeekO(off_t pos, wxSeekMode mode) |
38830220 | 657 | { |
60b6c062 | 658 | return OnSysSeek(pos, mode); |
38830220 RR |
659 | } |
660 | ||
fae05df5 | 661 | void wxOutputStream::Sync() |
1678ad78 | 662 | { |
1678ad78 GL |
663 | } |
664 | ||
47d67540 | 665 | #if wxUSE_SERIAL |
fae05df5 | 666 | wxOutputStream& wxOutputStream::operator<<(wxObject& obj) |
123a7fdd | 667 | { |
fae05df5 GL |
668 | wxObjectOutputStream obj_s(*this); |
669 | obj_s.SaveObject(obj); | |
123a7fdd GL |
670 | return *this; |
671 | } | |
fcc6dddd | 672 | #endif |
123a7fdd | 673 | |
e2acb9ae RR |
674 | // ---------------------------------------------------------------------------- |
675 | // wxCountingOutputStream | |
676 | // ---------------------------------------------------------------------------- | |
677 | ||
678 | wxCountingOutputStream::wxCountingOutputStream () | |
679 | : wxOutputStream() | |
680 | { | |
681 | m_currentPos = 0; | |
682 | } | |
683 | ||
684 | size_t wxCountingOutputStream::GetSize() const | |
685 | { | |
686 | return m_lastcount; | |
687 | } | |
688 | ||
5e0201ea | 689 | size_t wxCountingOutputStream::OnSysWrite(const void *WXUNUSED(buffer), size_t size) |
e2acb9ae RR |
690 | { |
691 | m_currentPos += size; | |
692 | if (m_currentPos > m_lastcount) m_lastcount = m_currentPos; | |
693 | return m_currentPos; | |
694 | } | |
695 | ||
696 | off_t wxCountingOutputStream::OnSysSeek(off_t pos, wxSeekMode mode) | |
697 | { | |
698 | if (mode == wxFromStart) | |
699 | { | |
700 | m_currentPos = pos; | |
701 | } | |
702 | if (mode == wxFromEnd) | |
703 | { | |
704 | m_currentPos = m_lastcount + pos; | |
705 | } | |
706 | else | |
707 | { | |
708 | m_currentPos += pos; | |
709 | } | |
710 | if (m_currentPos > m_lastcount) m_lastcount = m_currentPos; | |
711 | ||
712 | return m_currentPos; // ? | |
713 | } | |
714 | ||
715 | off_t wxCountingOutputStream::OnSysTell() const | |
716 | { | |
717 | return m_currentPos; // ? | |
718 | } | |
719 | ||
1678ad78 | 720 | // ---------------------------------------------------------------------------- |
fae05df5 | 721 | // wxFilterInputStream |
1678ad78 | 722 | // ---------------------------------------------------------------------------- |
e2acb9ae | 723 | |
fae05df5 GL |
724 | wxFilterInputStream::wxFilterInputStream() |
725 | : wxInputStream() | |
3d4c6a21 | 726 | { |
6d44bf31 GL |
727 | } |
728 | ||
fae05df5 GL |
729 | wxFilterInputStream::wxFilterInputStream(wxInputStream& stream) |
730 | : wxInputStream() | |
6d44bf31 | 731 | { |
fae05df5 | 732 | m_parent_i_stream = &stream; |
3d4c6a21 GL |
733 | } |
734 | ||
fae05df5 | 735 | wxFilterInputStream::~wxFilterInputStream() |
3d4c6a21 | 736 | { |
6d44bf31 GL |
737 | } |
738 | ||
fae05df5 GL |
739 | // ---------------------------------------------------------------------------- |
740 | // wxFilterOutputStream | |
741 | // ---------------------------------------------------------------------------- | |
742 | wxFilterOutputStream::wxFilterOutputStream() | |
743 | : wxOutputStream() | |
6d44bf31 | 744 | { |
3d4c6a21 GL |
745 | } |
746 | ||
fae05df5 GL |
747 | wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream) |
748 | : wxOutputStream() | |
3d4c6a21 | 749 | { |
fae05df5 | 750 | m_parent_o_stream = &stream; |
6d44bf31 GL |
751 | } |
752 | ||
fae05df5 | 753 | wxFilterOutputStream::~wxFilterOutputStream() |
6d44bf31 | 754 | { |
6d44bf31 GL |
755 | } |
756 | ||
fae05df5 GL |
757 | // ---------------------------------------------------------------------------- |
758 | // wxBufferedInputStream | |
759 | // ---------------------------------------------------------------------------- | |
760 | wxBufferedInputStream::wxBufferedInputStream(wxInputStream& s) | |
761 | : wxFilterInputStream(s) | |
6d44bf31 | 762 | { |
fae05df5 GL |
763 | m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read); |
764 | m_i_streambuf->SetBufferIO(1024); | |
6d44bf31 GL |
765 | } |
766 | ||
fae05df5 | 767 | wxBufferedInputStream::~wxBufferedInputStream() |
6d44bf31 | 768 | { |
fae05df5 | 769 | delete m_i_streambuf; |
6d44bf31 GL |
770 | } |
771 | ||
6319afe3 GL |
772 | char wxBufferedInputStream::Peek() |
773 | { | |
774 | return m_i_streambuf->Peek(); | |
775 | } | |
776 | ||
fae05df5 | 777 | wxInputStream& wxBufferedInputStream::Read(void *buffer, size_t size) |
1e3eca9d | 778 | { |
fae05df5 GL |
779 | size_t retsize; |
780 | char *buf = (char *)buffer; | |
1e3eca9d | 781 | |
fae05df5 | 782 | retsize = GetWBack(buf, size); |
a324a7bc | 783 | m_lastcount = retsize; |
fae05df5 | 784 | if (retsize == size) { |
fae05df5 GL |
785 | m_lasterror = wxStream_NOERROR; |
786 | return *this; | |
787 | } | |
788 | size -= retsize; | |
789 | buf += retsize; | |
1e3eca9d | 790 | |
fae05df5 | 791 | m_i_streambuf->Read(buf, size); |
6d44bf31 | 792 | |
fae05df5 | 793 | return *this; |
6d44bf31 GL |
794 | } |
795 | ||
fae05df5 | 796 | off_t wxBufferedInputStream::SeekI(off_t pos, wxSeekMode mode) |
6d44bf31 | 797 | { |
fae05df5 | 798 | return m_i_streambuf->Seek(pos, mode); |
6d44bf31 GL |
799 | } |
800 | ||
fae05df5 | 801 | off_t wxBufferedInputStream::TellI() const |
6d44bf31 | 802 | { |
fae05df5 | 803 | return m_i_streambuf->Tell(); |
38830220 | 804 | } |
6d44bf31 | 805 | |
fae05df5 | 806 | size_t wxBufferedInputStream::OnSysRead(void *buffer, size_t bufsize) |
38830220 | 807 | { |
fae05df5 | 808 | return m_parent_i_stream->Read(buffer, bufsize).LastRead(); |
38830220 RR |
809 | } |
810 | ||
fae05df5 | 811 | off_t wxBufferedInputStream::OnSysSeek(off_t seek, wxSeekMode mode) |
38830220 | 812 | { |
fae05df5 | 813 | return m_parent_i_stream->SeekI(seek, mode); |
6d44bf31 GL |
814 | } |
815 | ||
fae05df5 | 816 | off_t wxBufferedInputStream::OnSysTell() const |
6d44bf31 | 817 | { |
fae05df5 | 818 | return m_parent_i_stream->TellI(); |
38830220 | 819 | } |
6d44bf31 | 820 | |
fae05df5 GL |
821 | // ---------------------------------------------------------------------------- |
822 | // wxBufferedOutputStream | |
823 | // ---------------------------------------------------------------------------- | |
6d44bf31 | 824 | |
fae05df5 GL |
825 | wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& s) |
826 | : wxFilterOutputStream(s) | |
6d44bf31 | 827 | { |
fae05df5 GL |
828 | m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write); |
829 | m_o_streambuf->SetBufferIO(1024); | |
6d44bf31 GL |
830 | } |
831 | ||
fae05df5 | 832 | wxBufferedOutputStream::~wxBufferedOutputStream() |
6d44bf31 | 833 | { |
fae05df5 | 834 | delete m_o_streambuf; |
3d4c6a21 GL |
835 | } |
836 | ||
fae05df5 | 837 | wxOutputStream& wxBufferedOutputStream::Write(const void *buffer, size_t size) |
123a7fdd | 838 | { |
a324a7bc | 839 | m_lastcount = 0; |
fae05df5 | 840 | m_o_streambuf->Write(buffer, size); |
123a7fdd GL |
841 | return *this; |
842 | } | |
843 | ||
fae05df5 | 844 | off_t wxBufferedOutputStream::SeekO(off_t pos, wxSeekMode mode) |
f4ada568 | 845 | { |
fae05df5 | 846 | return m_o_streambuf->Seek(pos, mode); |
f4ada568 GL |
847 | } |
848 | ||
fae05df5 | 849 | off_t wxBufferedOutputStream::TellO() const |
3d4c6a21 | 850 | { |
fae05df5 | 851 | return m_o_streambuf->Tell(); |
3d4c6a21 GL |
852 | } |
853 | ||
fae05df5 | 854 | void wxBufferedOutputStream::Sync() |
3d4c6a21 | 855 | { |
fae05df5 GL |
856 | m_o_streambuf->FlushBuffer(); |
857 | m_parent_o_stream->Sync(); | |
3d4c6a21 | 858 | } |
219f895a | 859 | |
fae05df5 | 860 | size_t wxBufferedOutputStream::OnSysWrite(const void *buffer, size_t bufsize) |
f4ada568 | 861 | { |
fae05df5 | 862 | return m_parent_o_stream->Write(buffer, bufsize).LastWrite(); |
f4ada568 GL |
863 | } |
864 | ||
fae05df5 | 865 | off_t wxBufferedOutputStream::OnSysSeek(off_t seek, wxSeekMode mode) |
219f895a | 866 | { |
fae05df5 | 867 | return m_parent_o_stream->SeekO(seek, mode); |
219f895a RR |
868 | } |
869 | ||
fae05df5 | 870 | off_t wxBufferedOutputStream::OnSysTell() const |
219f895a | 871 | { |
fae05df5 | 872 | return m_parent_o_stream->TellO(); |
219f895a | 873 | } |
6d44bf31 | 874 | |
6d44bf31 GL |
875 | // ---------------------------------------------------------------------------- |
876 | // Some IOManip function | |
877 | // ---------------------------------------------------------------------------- | |
878 | ||
879 | wxOutputStream& wxEndL(wxOutputStream& stream) | |
880 | { | |
881 | #ifdef __MSW__ | |
882 | return stream.Write("\r\n", 2); | |
1e3eca9d GL |
883 | #else |
884 | #ifdef __WXMAC__ | |
885 | return stream.Write("\r", 1); | |
6d44bf31 GL |
886 | #else |
887 | return stream.Write("\n", 1); | |
888 | #endif | |
1e3eca9d | 889 | #endif |
6d44bf31 | 890 | } |
ce4169a4 RR |
891 | |
892 | #endif | |
893 | // wxUSE_STREAMS |