]>
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; |
1cb6da2d | 117 | if (m_mode == read && m_flushable) |
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) { | |
56dc1ffd GL |
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 | } | |
75ed1d15 GL |
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 | ||
84b46c35 GL |
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 | ||
9fc0fe37 GL |
196 | if (GetDataLeft() == 0 && !FlushBuffer()) { |
197 | CHECK_ERROR(wxStream_WRITE_ERR); | |
84b46c35 | 198 | return; |
8ef6a930 | 199 | } |
84b46c35 GL |
200 | |
201 | PutToBuffer(&c, 1); | |
202 | m_stream->m_lastcount = 1; | |
203 | } | |
204 | ||
6319afe3 GL |
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 | ||
84b46c35 GL |
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 | ||
8ef6a930 | 233 | if (!GetDataLeft()) { |
926c550d | 234 | CHECK_ERROR(wxStream_READ_ERR); |
84b46c35 | 235 | return 0; |
8ef6a930 | 236 | } |
84b46c35 GL |
237 | |
238 | GetFromBuffer(&c, 1); | |
e57e26dd | 239 | |
84b46c35 GL |
240 | m_stream->m_lastcount = 1; |
241 | return c; | |
242 | } | |
243 | ||
8ef6a930 | 244 | size_t wxStreamBuffer::Read(void *buffer, size_t size) |
6d44bf31 | 245 | { |
75ed1d15 | 246 | wxASSERT(m_stream != NULL); |
6d44bf31 | 247 | |
7f42cff1 GL |
248 | if (m_mode == write) |
249 | return 0; | |
250 | ||
6d44bf31 GL |
251 | // ------------------ |
252 | // Buffering disabled | |
253 | // ------------------ | |
254 | ||
2a040d3f | 255 | m_stream->m_lasterror = wxStream_NOERROR; |
926c550d | 256 | if (!m_buffer_size) |
8ef6a930 | 257 | return (m_stream->m_lastcount += m_stream->OnSysRead(buffer, size)); |
6d44bf31 GL |
258 | |
259 | // ----------------- | |
260 | // Buffering enabled | |
261 | // ----------------- | |
262 | size_t buf_left, orig_size = size; | |
6d44bf31 GL |
263 | |
264 | while (size > 0) { | |
75ed1d15 | 265 | buf_left = GetDataLeft(); |
6d44bf31 GL |
266 | |
267 | // First case: the requested buffer is larger than the stream buffer, | |
84b46c35 | 268 | // we split it. |
6d44bf31 | 269 | if (size > buf_left) { |
75ed1d15 GL |
270 | GetFromBuffer(buffer, buf_left); |
271 | size -= buf_left; | |
6d44bf31 GL |
272 | buffer = (char *)buffer + buf_left; // ANSI C++ violation. |
273 | ||
75ed1d15 | 274 | if (!FillBuffer()) { |
6319afe3 | 275 | CHECK_ERROR(wxStream_EOF); |
8ef6a930 | 276 | return (m_stream->m_lastcount = orig_size-size); |
6d44bf31 GL |
277 | } |
278 | } else { | |
279 | ||
280 | // Second case: we just copy from the stream buffer. | |
75ed1d15 | 281 | GetFromBuffer(buffer, size); |
6d44bf31 GL |
282 | break; |
283 | } | |
284 | } | |
8ef6a930 | 285 | return (m_stream->m_lastcount += orig_size); |
6d44bf31 GL |
286 | } |
287 | ||
8ef6a930 GL |
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 | ||
7f42cff1 GL |
293 | if (m_mode == write) |
294 | return 0; | |
295 | ||
062c4861 | 296 | while (bytes_read != 0) { |
8ef6a930 GL |
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) | |
6d44bf31 | 305 | { |
75ed1d15 | 306 | wxASSERT(m_stream != NULL); |
6d44bf31 | 307 | |
7f42cff1 GL |
308 | if (m_mode == read) |
309 | return 0; | |
310 | ||
6d44bf31 GL |
311 | // ------------------ |
312 | // Buffering disabled | |
313 | // ------------------ | |
314 | ||
2a040d3f | 315 | m_stream->m_lasterror = wxStream_NOERROR; |
56dc1ffd | 316 | if (!m_buffer_size && m_fixed) |
8ef6a930 | 317 | return (m_stream->m_lastcount = m_stream->OnSysWrite(buffer, size)); |
6d44bf31 GL |
318 | |
319 | // ------------------ | |
320 | // Buffering enabled | |
321 | // ------------------ | |
322 | ||
323 | size_t buf_left, orig_size = size; | |
6d44bf31 GL |
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 | |
56dc1ffd GL |
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) { | |
75ed1d15 | 333 | PutToBuffer(buffer, buf_left); |
6d44bf31 GL |
334 | size -= buf_left; |
335 | buffer = (char *)buffer + buf_left; // ANSI C++ violation. | |
336 | ||
75ed1d15 | 337 | if (!FlushBuffer()) { |
926c550d | 338 | CHECK_ERROR(wxStream_WRITE_ERR); |
8ef6a930 | 339 | return (m_stream->m_lastcount = orig_size-size); |
6d44bf31 | 340 | } |
8ef6a930 | 341 | |
6d44bf31 GL |
342 | m_buffer_pos = m_buffer_start; |
343 | ||
344 | } else { | |
345 | ||
346 | // Second case: just copy it in the stream buffer. | |
75ed1d15 | 347 | PutToBuffer(buffer, size); |
6d44bf31 GL |
348 | break; |
349 | } | |
350 | } | |
8ef6a930 GL |
351 | return (m_stream->m_lastcount = orig_size); |
352 | } | |
353 | ||
354 | size_t wxStreamBuffer::Write(wxStreamBuffer *sbuf) | |
355 | { | |
356 | char buf[BUF_TEMP_SIZE]; | |
eb4e516d | 357 | size_t s = 0, bytes_count = BUF_TEMP_SIZE, b_count2; |
fae05df5 | 358 | wxInputStream *in_stream; |
8ef6a930 | 359 | |
7f42cff1 GL |
360 | if (m_mode == read) |
361 | return 0; | |
362 | ||
fae05df5 GL |
363 | in_stream = (wxInputStream *)sbuf->Stream(); |
364 | ||
8ef6a930 | 365 | while (bytes_count == BUF_TEMP_SIZE) { |
eb4e516d GL |
366 | b_count2 = sbuf->Read(buf, bytes_count); |
367 | bytes_count = Write(buf, b_count2); | |
368 | if (b_count2 > bytes_count) | |
fae05df5 | 369 | in_stream->Ungetch(buf+bytes_count, b_count2-bytes_count); |
8ef6a930 GL |
370 | s += bytes_count; |
371 | } | |
372 | return s; | |
75ed1d15 GL |
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 | ||
8ef6a930 | 381 | if (!m_flushable) { |
84b46c35 GL |
382 | diff = pos + GetIntPosition(); |
383 | if (diff < 0 || diff > last_access) | |
384 | return wxInvalidOffset; | |
385 | SetIntPosition(diff); | |
386 | return diff; | |
387 | } | |
388 | ||
75ed1d15 GL |
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) ) { | |
d4feedcf GL |
400 | // We must take into account the fact that we have read something |
401 | // previously. | |
f1f5976f | 402 | ret_off = m_stream->OnSysSeek(diff-last_access, wxFromCurrent); |
75ed1d15 GL |
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 | ||
8ef6a930 | 423 | if (m_flushable) { |
84b46c35 GL |
424 | pos = m_stream->OnSysTell(); |
425 | if (pos == wxInvalidOffset) | |
426 | return wxInvalidOffset; | |
427 | return pos - GetLastAccess() + GetIntPosition(); | |
428 | } else | |
429 | return GetIntPosition(); | |
75ed1d15 GL |
430 | } |
431 | ||
8ef6a930 | 432 | size_t wxStreamBuffer::GetDataLeft() |
75ed1d15 | 433 | { |
8ef6a930 GL |
434 | if (m_buffer_end == m_buffer_pos && m_flushable) |
435 | FillBuffer(); | |
75ed1d15 GL |
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 | ||
df875e59 | 453 | size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer), size_t WXUNUSED(size)) |
75ed1d15 GL |
454 | { |
455 | return 0; | |
456 | } | |
457 | ||
df875e59 | 458 | size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer), size_t WXUNUSED(bufsize)) |
75ed1d15 GL |
459 | { |
460 | return 0; | |
461 | } | |
462 | ||
df875e59 | 463 | off_t wxStreamBase::OnSysSeek(off_t WXUNUSED(seek), wxSeekMode WXUNUSED(mode)) |
75ed1d15 GL |
464 | { |
465 | return wxInvalidOffset; | |
466 | } | |
467 | ||
84b46c35 | 468 | off_t wxStreamBase::OnSysTell() const |
75ed1d15 GL |
469 | { |
470 | return wxInvalidOffset; | |
6d44bf31 GL |
471 | } |
472 | ||
1678ad78 GL |
473 | // ---------------------------------------------------------------------------- |
474 | // wxInputStream | |
475 | // ---------------------------------------------------------------------------- | |
476 | ||
3d4c6a21 | 477 | wxInputStream::wxInputStream() |
fae05df5 GL |
478 | : wxStreamBase(), |
479 | m_wback(NULL), m_wbacksize(0), m_wbackcur(0) | |
3d4c6a21 | 480 | { |
6d44bf31 GL |
481 | } |
482 | ||
fae05df5 | 483 | wxInputStream::~wxInputStream() |
6d44bf31 | 484 | { |
fae05df5 GL |
485 | if (m_wback) |
486 | free(m_wback); | |
3d4c6a21 GL |
487 | } |
488 | ||
fae05df5 | 489 | char *wxInputStream::AllocSpaceWBack(size_t needed_size) |
3d4c6a21 | 490 | { |
fae05df5 | 491 | char *temp_b; |
783ff666 | 492 | size_t old_size; |
fae05df5 | 493 | |
783ff666 | 494 | old_size = m_wbacksize; |
fae05df5 GL |
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; | |
783ff666 GL |
505 | |
506 | memmove(m_wback+needed_size, m_wback, old_size); | |
fae05df5 | 507 | |
783ff666 | 508 | return (char *)(m_wback); |
6d44bf31 GL |
509 | } |
510 | ||
fae05df5 | 511 | size_t wxInputStream::GetWBack(char *buf, size_t bsize) |
6d44bf31 | 512 | { |
56dc1ffd | 513 | size_t s_toget = m_wbacksize-m_wbackcur; |
fae05df5 | 514 | |
a324a7bc GL |
515 | if (!m_wback) |
516 | return 0; | |
517 | ||
fae05df5 GL |
518 | if (bsize < s_toget) |
519 | s_toget = bsize; | |
520 | ||
56dc1ffd | 521 | memcpy(buf, (m_wback+m_wbackcur), s_toget); |
fae05df5 | 522 | |
56dc1ffd GL |
523 | m_wbackcur += s_toget; |
524 | if (m_wbackcur == m_wbacksize) { | |
fae05df5 GL |
525 | free(m_wback); |
526 | m_wback = (char *)NULL; | |
527 | m_wbacksize = 0; | |
528 | m_wbackcur = 0; | |
529 | } | |
530 | ||
531 | return s_toget; | |
6d44bf31 GL |
532 | } |
533 | ||
8f7173ab | 534 | size_t wxInputStream::Ungetch(const void *buf, size_t bufsize) |
fae05df5 GL |
535 | { |
536 | char *ptrback; | |
537 | ||
538 | ptrback = AllocSpaceWBack(bufsize); | |
539 | if (!ptrback) | |
540 | return 0; | |
1e3eca9d | 541 | |
fae05df5 GL |
542 | memcpy(ptrback, buf, bufsize); |
543 | return bufsize; | |
544 | } | |
545 | ||
546 | bool wxInputStream::Ungetch(char c) | |
1e3eca9d | 547 | { |
fae05df5 | 548 | char *ptrback; |
1e3eca9d | 549 | |
fae05df5 GL |
550 | ptrback = AllocSpaceWBack(1); |
551 | if (!ptrback) | |
552 | return FALSE; | |
1e3eca9d | 553 | |
fae05df5 GL |
554 | *ptrback = c; |
555 | return TRUE; | |
556 | } | |
557 | ||
558 | char wxInputStream::GetC() | |
559 | { | |
560 | char c; | |
561 | Read(&c, 1); | |
562 | return c; | |
1e3eca9d GL |
563 | } |
564 | ||
6d44bf31 GL |
565 | wxInputStream& wxInputStream::Read(void *buffer, size_t size) |
566 | { | |
fae05df5 GL |
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); | |
6d44bf31 | 580 | return *this; |
3d4c6a21 GL |
581 | } |
582 | ||
75ed1d15 GL |
583 | char wxInputStream::Peek() |
584 | { | |
fae05df5 | 585 | char c; |
75ed1d15 | 586 | |
fae05df5 GL |
587 | Read(&c, 1); |
588 | if (m_lasterror == wxStream_NOERROR) { | |
589 | Ungetch(c); | |
590 | return c; | |
591 | } | |
592 | return 0; | |
75ed1d15 GL |
593 | } |
594 | ||
3d4c6a21 GL |
595 | wxInputStream& wxInputStream::Read(wxOutputStream& stream_out) |
596 | { | |
597 | char buf[BUF_TEMP_SIZE]; | |
598 | size_t bytes_read = BUF_TEMP_SIZE; | |
599 | ||
8ef6a930 | 600 | while (bytes_read == BUF_TEMP_SIZE) { |
3d4c6a21 | 601 | bytes_read = Read(buf, bytes_read).LastRead(); |
8ef6a930 | 602 | bytes_read = stream_out.Write(buf, bytes_read).LastWrite(); |
3d4c6a21 GL |
603 | } |
604 | return *this; | |
605 | } | |
606 | ||
75ed1d15 GL |
607 | off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode) |
608 | { | |
fe8aa971 SB |
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 | ||
60b6c062 | 613 | return OnSysSeek(pos, mode); |
75ed1d15 GL |
614 | } |
615 | ||
616 | off_t wxInputStream::TellI() const | |
617 | { | |
60b6c062 | 618 | return OnSysTell(); |
75ed1d15 GL |
619 | } |
620 | ||
621 | // -------------------- | |
622 | // Overloaded operators | |
623 | // -------------------- | |
624 | ||
fae05df5 GL |
625 | #if wxUSE_SERIAL |
626 | wxInputStream& wxInputStream::operator>>(wxObject *& obj) | |
1678ad78 | 627 | { |
fae05df5 GL |
628 | wxObjectInputStream obj_s(*this); |
629 | obj = obj_s.LoadObject(); | |
1678ad78 GL |
630 | return *this; |
631 | } | |
fae05df5 | 632 | #endif |
1678ad78 | 633 | |
1678ad78 | 634 | |
fae05df5 GL |
635 | // ---------------------------------------------------------------------------- |
636 | // wxOutputStream | |
637 | // ---------------------------------------------------------------------------- | |
638 | wxOutputStream::wxOutputStream() | |
639 | : wxStreamBase() | |
1678ad78 | 640 | { |
1678ad78 GL |
641 | } |
642 | ||
fae05df5 | 643 | wxOutputStream::~wxOutputStream() |
123a7fdd | 644 | { |
123a7fdd GL |
645 | } |
646 | ||
fae05df5 | 647 | wxOutputStream& wxOutputStream::Write(const void *buffer, size_t size) |
1678ad78 | 648 | { |
fae05df5 | 649 | m_lastcount = OnSysWrite(buffer, size); |
1678ad78 GL |
650 | return *this; |
651 | } | |
652 | ||
fae05df5 | 653 | wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in) |
38830220 | 654 | { |
fae05df5 | 655 | stream_in.Read(*this); |
38830220 RR |
656 | return *this; |
657 | } | |
658 | ||
fae05df5 | 659 | off_t wxOutputStream::TellO() const |
38830220 | 660 | { |
60b6c062 | 661 | return OnSysTell(); |
38830220 RR |
662 | } |
663 | ||
fae05df5 | 664 | off_t wxOutputStream::SeekO(off_t pos, wxSeekMode mode) |
38830220 | 665 | { |
60b6c062 | 666 | return OnSysSeek(pos, mode); |
38830220 RR |
667 | } |
668 | ||
fae05df5 | 669 | void wxOutputStream::Sync() |
1678ad78 | 670 | { |
1678ad78 GL |
671 | } |
672 | ||
47d67540 | 673 | #if wxUSE_SERIAL |
fae05df5 | 674 | wxOutputStream& wxOutputStream::operator<<(wxObject& obj) |
123a7fdd | 675 | { |
fae05df5 GL |
676 | wxObjectOutputStream obj_s(*this); |
677 | obj_s.SaveObject(obj); | |
123a7fdd GL |
678 | return *this; |
679 | } | |
fcc6dddd | 680 | #endif |
123a7fdd | 681 | |
e2acb9ae RR |
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 | ||
5e0201ea | 697 | size_t wxCountingOutputStream::OnSysWrite(const void *WXUNUSED(buffer), size_t size) |
e2acb9ae RR |
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 | ||
1678ad78 | 728 | // ---------------------------------------------------------------------------- |
fae05df5 | 729 | // wxFilterInputStream |
1678ad78 | 730 | // ---------------------------------------------------------------------------- |
e2acb9ae | 731 | |
fae05df5 GL |
732 | wxFilterInputStream::wxFilterInputStream() |
733 | : wxInputStream() | |
3d4c6a21 | 734 | { |
6d44bf31 GL |
735 | } |
736 | ||
fae05df5 GL |
737 | wxFilterInputStream::wxFilterInputStream(wxInputStream& stream) |
738 | : wxInputStream() | |
6d44bf31 | 739 | { |
fae05df5 | 740 | m_parent_i_stream = &stream; |
3d4c6a21 GL |
741 | } |
742 | ||
fae05df5 | 743 | wxFilterInputStream::~wxFilterInputStream() |
3d4c6a21 | 744 | { |
6d44bf31 GL |
745 | } |
746 | ||
fae05df5 GL |
747 | // ---------------------------------------------------------------------------- |
748 | // wxFilterOutputStream | |
749 | // ---------------------------------------------------------------------------- | |
750 | wxFilterOutputStream::wxFilterOutputStream() | |
751 | : wxOutputStream() | |
6d44bf31 | 752 | { |
3d4c6a21 GL |
753 | } |
754 | ||
fae05df5 GL |
755 | wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream) |
756 | : wxOutputStream() | |
3d4c6a21 | 757 | { |
fae05df5 | 758 | m_parent_o_stream = &stream; |
6d44bf31 GL |
759 | } |
760 | ||
fae05df5 | 761 | wxFilterOutputStream::~wxFilterOutputStream() |
6d44bf31 | 762 | { |
6d44bf31 GL |
763 | } |
764 | ||
fae05df5 GL |
765 | // ---------------------------------------------------------------------------- |
766 | // wxBufferedInputStream | |
767 | // ---------------------------------------------------------------------------- | |
768 | wxBufferedInputStream::wxBufferedInputStream(wxInputStream& s) | |
769 | : wxFilterInputStream(s) | |
6d44bf31 | 770 | { |
fae05df5 GL |
771 | m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read); |
772 | m_i_streambuf->SetBufferIO(1024); | |
6d44bf31 GL |
773 | } |
774 | ||
fae05df5 | 775 | wxBufferedInputStream::~wxBufferedInputStream() |
6d44bf31 | 776 | { |
fae05df5 | 777 | delete m_i_streambuf; |
6d44bf31 GL |
778 | } |
779 | ||
6319afe3 GL |
780 | char wxBufferedInputStream::Peek() |
781 | { | |
782 | return m_i_streambuf->Peek(); | |
783 | } | |
784 | ||
fae05df5 | 785 | wxInputStream& wxBufferedInputStream::Read(void *buffer, size_t size) |
1e3eca9d | 786 | { |
fae05df5 GL |
787 | size_t retsize; |
788 | char *buf = (char *)buffer; | |
1e3eca9d | 789 | |
fae05df5 | 790 | retsize = GetWBack(buf, size); |
a324a7bc | 791 | m_lastcount = retsize; |
fae05df5 | 792 | if (retsize == size) { |
fae05df5 GL |
793 | m_lasterror = wxStream_NOERROR; |
794 | return *this; | |
795 | } | |
796 | size -= retsize; | |
797 | buf += retsize; | |
1e3eca9d | 798 | |
fae05df5 | 799 | m_i_streambuf->Read(buf, size); |
6d44bf31 | 800 | |
fae05df5 | 801 | return *this; |
6d44bf31 GL |
802 | } |
803 | ||
fae05df5 | 804 | off_t wxBufferedInputStream::SeekI(off_t pos, wxSeekMode mode) |
6d44bf31 | 805 | { |
fae05df5 | 806 | return m_i_streambuf->Seek(pos, mode); |
6d44bf31 GL |
807 | } |
808 | ||
fae05df5 | 809 | off_t wxBufferedInputStream::TellI() const |
6d44bf31 | 810 | { |
fae05df5 | 811 | return m_i_streambuf->Tell(); |
38830220 | 812 | } |
6d44bf31 | 813 | |
fae05df5 | 814 | size_t wxBufferedInputStream::OnSysRead(void *buffer, size_t bufsize) |
38830220 | 815 | { |
fae05df5 | 816 | return m_parent_i_stream->Read(buffer, bufsize).LastRead(); |
38830220 RR |
817 | } |
818 | ||
fae05df5 | 819 | off_t wxBufferedInputStream::OnSysSeek(off_t seek, wxSeekMode mode) |
38830220 | 820 | { |
fae05df5 | 821 | return m_parent_i_stream->SeekI(seek, mode); |
6d44bf31 GL |
822 | } |
823 | ||
fae05df5 | 824 | off_t wxBufferedInputStream::OnSysTell() const |
6d44bf31 | 825 | { |
fae05df5 | 826 | return m_parent_i_stream->TellI(); |
38830220 | 827 | } |
6d44bf31 | 828 | |
fae05df5 GL |
829 | // ---------------------------------------------------------------------------- |
830 | // wxBufferedOutputStream | |
831 | // ---------------------------------------------------------------------------- | |
6d44bf31 | 832 | |
fae05df5 GL |
833 | wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& s) |
834 | : wxFilterOutputStream(s) | |
6d44bf31 | 835 | { |
fae05df5 GL |
836 | m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write); |
837 | m_o_streambuf->SetBufferIO(1024); | |
6d44bf31 GL |
838 | } |
839 | ||
fae05df5 | 840 | wxBufferedOutputStream::~wxBufferedOutputStream() |
6d44bf31 | 841 | { |
fae05df5 | 842 | delete m_o_streambuf; |
3d4c6a21 GL |
843 | } |
844 | ||
fae05df5 | 845 | wxOutputStream& wxBufferedOutputStream::Write(const void *buffer, size_t size) |
123a7fdd | 846 | { |
a324a7bc | 847 | m_lastcount = 0; |
fae05df5 | 848 | m_o_streambuf->Write(buffer, size); |
123a7fdd GL |
849 | return *this; |
850 | } | |
851 | ||
fae05df5 | 852 | off_t wxBufferedOutputStream::SeekO(off_t pos, wxSeekMode mode) |
f4ada568 | 853 | { |
fae05df5 | 854 | return m_o_streambuf->Seek(pos, mode); |
f4ada568 GL |
855 | } |
856 | ||
fae05df5 | 857 | off_t wxBufferedOutputStream::TellO() const |
3d4c6a21 | 858 | { |
fae05df5 | 859 | return m_o_streambuf->Tell(); |
3d4c6a21 GL |
860 | } |
861 | ||
fae05df5 | 862 | void wxBufferedOutputStream::Sync() |
3d4c6a21 | 863 | { |
fae05df5 GL |
864 | m_o_streambuf->FlushBuffer(); |
865 | m_parent_o_stream->Sync(); | |
3d4c6a21 | 866 | } |
219f895a | 867 | |
fae05df5 | 868 | size_t wxBufferedOutputStream::OnSysWrite(const void *buffer, size_t bufsize) |
f4ada568 | 869 | { |
fae05df5 | 870 | return m_parent_o_stream->Write(buffer, bufsize).LastWrite(); |
f4ada568 GL |
871 | } |
872 | ||
fae05df5 | 873 | off_t wxBufferedOutputStream::OnSysSeek(off_t seek, wxSeekMode mode) |
219f895a | 874 | { |
fae05df5 | 875 | return m_parent_o_stream->SeekO(seek, mode); |
219f895a RR |
876 | } |
877 | ||
fae05df5 | 878 | off_t wxBufferedOutputStream::OnSysTell() const |
219f895a | 879 | { |
fae05df5 | 880 | return m_parent_o_stream->TellO(); |
219f895a | 881 | } |
6d44bf31 | 882 | |
6d44bf31 GL |
883 | // ---------------------------------------------------------------------------- |
884 | // Some IOManip function | |
885 | // ---------------------------------------------------------------------------- | |
886 | ||
887 | wxOutputStream& wxEndL(wxOutputStream& stream) | |
888 | { | |
889 | #ifdef __MSW__ | |
890 | return stream.Write("\r\n", 2); | |
1e3eca9d GL |
891 | #else |
892 | #ifdef __WXMAC__ | |
893 | return stream.Write("\r", 1); | |
6d44bf31 GL |
894 | #else |
895 | return stream.Write("\n", 1); | |
896 | #endif | |
1e3eca9d | 897 | #endif |
6d44bf31 | 898 | } |
ce4169a4 RR |
899 | |
900 | #endif | |
901 | // wxUSE_STREAMS |