]>
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" | |
6d44bf31 | 18 | #include <ctype.h> |
79c3e0e1 | 19 | #include <wx/stream.h> |
6d44bf31 | 20 | #include <wx/datstrm.h> |
123a7fdd | 21 | #include <wx/objstrm.h> |
db138a4c JS |
22 | |
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
8ef6a930 GL |
27 | #define BUF_TEMP_SIZE 10000 |
28 | ||
6d44bf31 GL |
29 | // ---------------------------------------------------------------------------- |
30 | // wxStreamBuffer | |
31 | // ---------------------------------------------------------------------------- | |
32 | ||
926c550d GL |
33 | #define CHECK_ERROR(err) \ |
34 | if (m_stream->m_lasterror == wxStream_NOERROR) \ | |
35 | m_stream->m_lasterror = err | |
36 | ||
75ed1d15 | 37 | wxStreamBuffer::wxStreamBuffer(wxStreamBase& stream, BufMode mode) |
6d44bf31 | 38 | : m_buffer_start(NULL), m_buffer_end(NULL), m_buffer_pos(NULL), |
eb4e516d GL |
39 | m_buffer_size(0), m_wback(NULL), m_wbacksize(0), m_wbackcur(0), |
40 | m_fixed(TRUE), m_flushable(TRUE), m_stream(&stream), | |
8ef6a930 | 41 | m_mode(mode), m_destroybuf(FALSE), m_destroystream(FALSE) |
6d44bf31 GL |
42 | { |
43 | } | |
44 | ||
84b46c35 GL |
45 | wxStreamBuffer::wxStreamBuffer(BufMode mode) |
46 | : m_buffer_start(NULL), m_buffer_end(NULL), m_buffer_pos(NULL), | |
eb4e516d GL |
47 | m_buffer_size(0), m_wback(NULL), m_wbacksize(0), m_wbackcur(0), |
48 | m_fixed(TRUE), m_flushable(FALSE), m_stream(NULL), | |
8ef6a930 | 49 | m_mode(mode), m_destroybuf(FALSE), m_destroystream(TRUE) |
84b46c35 | 50 | { |
8ef6a930 | 51 | m_stream = new wxStreamBase(); |
84b46c35 GL |
52 | } |
53 | ||
54 | wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer& buffer) | |
55 | { | |
56 | m_buffer_start = buffer.m_buffer_start; | |
57 | m_buffer_end = buffer.m_buffer_end; | |
58 | m_buffer_pos = buffer.m_buffer_pos; | |
59 | m_buffer_size = buffer.m_buffer_size; | |
60 | m_fixed = buffer.m_fixed; | |
61 | m_flushable = buffer.m_flushable; | |
62 | m_stream = buffer.m_stream; | |
63 | m_mode = buffer.m_mode; | |
64 | m_destroybuf = FALSE; | |
8ef6a930 | 65 | m_destroystream = FALSE; |
eb4e516d GL |
66 | m_wback = NULL; |
67 | m_wbacksize = 0; | |
68 | m_wbackcur = 0; | |
84b46c35 GL |
69 | } |
70 | ||
75ed1d15 | 71 | wxStreamBuffer::~wxStreamBuffer() |
6d44bf31 | 72 | { |
eb4e516d GL |
73 | if (m_wback) |
74 | free(m_wback); | |
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 | ||
8ef6a930 | 81 | size_t wxStreamBuffer::WriteBack(const char *buf, size_t bufsize) |
6d44bf31 | 82 | { |
75ed1d15 GL |
83 | char *ptrback; |
84 | ||
85 | ptrback = AllocSpaceWBack(bufsize); | |
86 | if (!ptrback) | |
8ef6a930 | 87 | return 0; |
75ed1d15 GL |
88 | |
89 | memcpy(ptrback, buf, bufsize); | |
8ef6a930 | 90 | return bufsize; |
6d44bf31 GL |
91 | } |
92 | ||
75ed1d15 | 93 | bool wxStreamBuffer::WriteBack(char c) |
6d44bf31 | 94 | { |
75ed1d15 | 95 | char *ptrback; |
6d44bf31 | 96 | |
75ed1d15 GL |
97 | ptrback = AllocSpaceWBack(1); |
98 | if (!ptrback) | |
99 | return FALSE; | |
6d44bf31 | 100 | |
75ed1d15 GL |
101 | *ptrback = c; |
102 | return TRUE; | |
6d44bf31 GL |
103 | } |
104 | ||
105 | void wxStreamBuffer::SetBufferIO(char *buffer_start, char *buffer_end) | |
106 | { | |
84b46c35 GL |
107 | if (m_destroybuf) |
108 | wxDELETEA(m_buffer_start); | |
885ee235 GL |
109 | m_buffer_start = buffer_start; |
110 | m_buffer_end = buffer_end; | |
6d44bf31 GL |
111 | |
112 | m_buffer_size = m_buffer_end-m_buffer_start; | |
84b46c35 | 113 | m_destroybuf = FALSE; |
75ed1d15 | 114 | ResetBuffer(); |
6d44bf31 GL |
115 | } |
116 | ||
117 | void wxStreamBuffer::SetBufferIO(size_t bufsize) | |
118 | { | |
885ee235 GL |
119 | char *b_start; |
120 | ||
926c550d GL |
121 | if (m_destroybuf) |
122 | wxDELETEA(m_buffer_start); | |
6d44bf31 GL |
123 | |
124 | if (!bufsize) { | |
125 | m_buffer_start = NULL; | |
126 | m_buffer_end = NULL; | |
127 | m_buffer_pos = NULL; | |
128 | m_buffer_size = 0; | |
885ee235 | 129 | return; |
6d44bf31 GL |
130 | } |
131 | ||
885ee235 | 132 | b_start = new char[bufsize]; |
885ee235 | 133 | SetBufferIO(b_start, b_start + bufsize); |
84b46c35 | 134 | m_destroybuf = TRUE; |
6d44bf31 GL |
135 | } |
136 | ||
137 | void wxStreamBuffer::ResetBuffer() | |
138 | { | |
2a040d3f | 139 | m_stream->m_lasterror = wxStream_NOERROR; |
75ed1d15 | 140 | if (m_mode == read) |
6d44bf31 GL |
141 | m_buffer_pos = m_buffer_end; |
142 | else | |
143 | m_buffer_pos = m_buffer_start; | |
144 | } | |
145 | ||
75ed1d15 GL |
146 | char *wxStreamBuffer::AllocSpaceWBack(size_t needed_size) |
147 | { | |
148 | char *temp_b; | |
149 | ||
150 | m_wbacksize += needed_size; | |
151 | ||
152 | if (!m_wback) | |
153 | temp_b = (char *)malloc(m_wbacksize); | |
154 | else | |
155 | temp_b = (char *)realloc(m_wback, m_wbacksize); | |
156 | ||
157 | if (!temp_b) | |
158 | return NULL; | |
eb4e516d GL |
159 | m_wback = temp_b; |
160 | printf("Buffer(0x%x)->Write: 0x%x, %d\n", this, m_wback, m_wbacksize); | |
161 | return (char *)(m_wback+(m_wbacksize-needed_size)); | |
75ed1d15 GL |
162 | } |
163 | ||
164 | size_t wxStreamBuffer::GetWBack(char *buf, size_t bsize) | |
165 | { | |
166 | size_t s_toget = m_wbacksize-m_wbackcur; | |
167 | ||
eb4e516d | 168 | printf("Buffer(0x%x): 0x%x, %d\n", this, m_wback, m_wbacksize); |
75ed1d15 GL |
169 | if (bsize < s_toget) |
170 | s_toget = bsize; | |
171 | ||
172 | memcpy(buf, (m_wback+m_wbackcur), s_toget); | |
173 | ||
174 | m_wbackcur += s_toget; | |
175 | if (m_wbackcur == m_wbacksize) { | |
176 | free(m_wback); | |
177 | m_wback = (char *)NULL; | |
178 | m_wbacksize = 0; | |
179 | m_wbackcur = 0; | |
180 | } | |
181 | ||
182 | return s_toget; | |
183 | } | |
184 | ||
185 | bool wxStreamBuffer::FillBuffer() | |
186 | { | |
187 | size_t count; | |
188 | ||
189 | count = m_stream->OnSysRead(m_buffer_start, m_buffer_size); | |
190 | m_buffer_end = m_buffer_start+count; | |
191 | m_buffer_pos = m_buffer_start; | |
192 | ||
193 | if (count == 0) | |
194 | return FALSE; | |
195 | return TRUE; | |
196 | } | |
197 | ||
198 | bool wxStreamBuffer::FlushBuffer() | |
199 | { | |
200 | size_t count, current; | |
201 | ||
84b46c35 | 202 | if (m_buffer_pos == m_buffer_start || !m_flushable) |
75ed1d15 GL |
203 | return FALSE; |
204 | ||
205 | current = m_buffer_pos-m_buffer_start; | |
206 | count = m_stream->OnSysWrite(m_buffer_start, current); | |
207 | if (count != current) | |
208 | return FALSE; | |
209 | m_buffer_pos = m_buffer_start; | |
210 | ||
211 | return TRUE; | |
212 | } | |
213 | ||
214 | void wxStreamBuffer::GetFromBuffer(void *buffer, size_t size) | |
215 | { | |
216 | size_t s_toget = m_buffer_end-m_buffer_pos; | |
217 | ||
218 | if (size < s_toget) | |
219 | s_toget = size; | |
220 | ||
221 | memcpy(buffer, m_buffer_pos, s_toget); | |
222 | m_buffer_pos += s_toget; | |
223 | } | |
224 | ||
225 | void wxStreamBuffer::PutToBuffer(const void *buffer, size_t size) | |
226 | { | |
227 | size_t s_toput = m_buffer_end-m_buffer_pos; | |
228 | ||
229 | if (s_toput < size && !m_fixed) { | |
230 | m_buffer_start = (char *)realloc(m_buffer_start, m_buffer_size+size); | |
231 | // I round a bit | |
84b46c35 | 232 | m_buffer_size += size; |
75ed1d15 GL |
233 | m_buffer_end = m_buffer_start+m_buffer_size; |
234 | s_toput = size; | |
235 | } | |
236 | if (s_toput > size) | |
237 | s_toput = size; | |
238 | memcpy(m_buffer_pos, buffer, s_toput); | |
239 | m_buffer_pos += s_toput; | |
240 | } | |
241 | ||
84b46c35 GL |
242 | void wxStreamBuffer::PutChar(char c) |
243 | { | |
244 | wxASSERT(m_stream != NULL); | |
245 | ||
246 | if (!m_buffer_size) { | |
247 | m_stream->OnSysWrite(&c, 1); | |
248 | return; | |
249 | } | |
250 | ||
8ef6a930 | 251 | if (!GetDataLeft() && !FlushBuffer()) { |
926c550d | 252 | CHECK_ERROR(wxStream_READ_ERR); |
84b46c35 | 253 | return; |
8ef6a930 | 254 | } |
84b46c35 GL |
255 | |
256 | PutToBuffer(&c, 1); | |
257 | m_stream->m_lastcount = 1; | |
258 | } | |
259 | ||
260 | char wxStreamBuffer::GetChar() | |
261 | { | |
262 | char c; | |
263 | ||
264 | wxASSERT(m_stream != NULL); | |
265 | ||
266 | if (!m_buffer_size) { | |
267 | m_stream->OnSysRead(&c, 1); | |
268 | return c; | |
269 | } | |
270 | ||
8ef6a930 | 271 | if (!GetDataLeft()) { |
926c550d | 272 | CHECK_ERROR(wxStream_READ_ERR); |
84b46c35 | 273 | return 0; |
8ef6a930 | 274 | } |
84b46c35 GL |
275 | |
276 | GetFromBuffer(&c, 1); | |
277 | m_stream->m_lastcount = 1; | |
278 | return c; | |
279 | } | |
280 | ||
8ef6a930 | 281 | size_t wxStreamBuffer::Read(void *buffer, size_t size) |
6d44bf31 | 282 | { |
75ed1d15 | 283 | wxASSERT(m_stream != NULL); |
6d44bf31 GL |
284 | |
285 | // ------------------ | |
286 | // Buffering disabled | |
287 | // ------------------ | |
288 | ||
2a040d3f | 289 | m_stream->m_lasterror = wxStream_NOERROR; |
75ed1d15 GL |
290 | m_stream->m_lastcount = GetWBack((char *)buffer, size); |
291 | size -= m_stream->m_lastcount; | |
292 | if (size == 0) | |
8ef6a930 | 293 | return m_stream->m_lastcount; |
75ed1d15 GL |
294 | |
295 | buffer = (void *)((char *)buffer+m_stream->m_lastcount); | |
296 | ||
926c550d | 297 | if (!m_buffer_size) |
8ef6a930 | 298 | return (m_stream->m_lastcount += m_stream->OnSysRead(buffer, size)); |
6d44bf31 GL |
299 | |
300 | // ----------------- | |
301 | // Buffering enabled | |
302 | // ----------------- | |
303 | size_t buf_left, orig_size = size; | |
6d44bf31 GL |
304 | |
305 | while (size > 0) { | |
75ed1d15 | 306 | buf_left = GetDataLeft(); |
6d44bf31 GL |
307 | |
308 | // First case: the requested buffer is larger than the stream buffer, | |
84b46c35 | 309 | // we split it. |
6d44bf31 | 310 | if (size > buf_left) { |
75ed1d15 GL |
311 | GetFromBuffer(buffer, buf_left); |
312 | size -= buf_left; | |
6d44bf31 GL |
313 | buffer = (char *)buffer + buf_left; // ANSI C++ violation. |
314 | ||
75ed1d15 | 315 | if (!FillBuffer()) { |
926c550d | 316 | CHECK_ERROR(wxStream_READ_ERR); |
8ef6a930 | 317 | return (m_stream->m_lastcount = orig_size-size); |
6d44bf31 GL |
318 | } |
319 | } else { | |
320 | ||
321 | // Second case: we just copy from the stream buffer. | |
75ed1d15 | 322 | GetFromBuffer(buffer, size); |
6d44bf31 GL |
323 | break; |
324 | } | |
325 | } | |
8ef6a930 | 326 | return (m_stream->m_lastcount += orig_size); |
6d44bf31 GL |
327 | } |
328 | ||
8ef6a930 GL |
329 | size_t wxStreamBuffer::Read(wxStreamBuffer *s_buf) |
330 | { | |
331 | char buf[BUF_TEMP_SIZE]; | |
332 | size_t s = 0, bytes_read = BUF_TEMP_SIZE; | |
333 | ||
334 | while (bytes_read == BUF_TEMP_SIZE) { | |
335 | bytes_read = Read(buf, bytes_read); | |
336 | bytes_read = s_buf->Write(buf, bytes_read); | |
337 | s += bytes_read; | |
338 | } | |
339 | return s; | |
340 | } | |
341 | ||
342 | size_t wxStreamBuffer::Write(const void *buffer, size_t size) | |
6d44bf31 | 343 | { |
75ed1d15 | 344 | wxASSERT(m_stream != NULL); |
6d44bf31 GL |
345 | |
346 | // ------------------ | |
347 | // Buffering disabled | |
348 | // ------------------ | |
349 | ||
2a040d3f | 350 | m_stream->m_lasterror = wxStream_NOERROR; |
8ef6a930 GL |
351 | if (!m_buffer_size) |
352 | return (m_stream->m_lastcount = m_stream->OnSysWrite(buffer, size)); | |
6d44bf31 GL |
353 | |
354 | // ------------------ | |
355 | // Buffering enabled | |
356 | // ------------------ | |
357 | ||
358 | size_t buf_left, orig_size = size; | |
6d44bf31 GL |
359 | |
360 | while (size > 0) { | |
361 | buf_left = m_buffer_end - m_buffer_pos; | |
362 | ||
363 | // First case: the buffer to write is larger than the stream buffer, | |
364 | // we split it | |
365 | if (size > buf_left) { | |
75ed1d15 | 366 | PutToBuffer(buffer, buf_left); |
6d44bf31 GL |
367 | size -= buf_left; |
368 | buffer = (char *)buffer + buf_left; // ANSI C++ violation. | |
369 | ||
75ed1d15 | 370 | if (!FlushBuffer()) { |
926c550d | 371 | CHECK_ERROR(wxStream_WRITE_ERR); |
8ef6a930 | 372 | return (m_stream->m_lastcount = orig_size-size); |
6d44bf31 | 373 | } |
8ef6a930 | 374 | |
6d44bf31 GL |
375 | m_buffer_pos = m_buffer_start; |
376 | ||
377 | } else { | |
378 | ||
379 | // Second case: just copy it in the stream buffer. | |
75ed1d15 | 380 | PutToBuffer(buffer, size); |
6d44bf31 GL |
381 | break; |
382 | } | |
383 | } | |
8ef6a930 GL |
384 | return (m_stream->m_lastcount = orig_size); |
385 | } | |
386 | ||
387 | size_t wxStreamBuffer::Write(wxStreamBuffer *sbuf) | |
388 | { | |
389 | char buf[BUF_TEMP_SIZE]; | |
eb4e516d | 390 | size_t s = 0, bytes_count = BUF_TEMP_SIZE, b_count2; |
8ef6a930 GL |
391 | |
392 | while (bytes_count == BUF_TEMP_SIZE) { | |
eb4e516d GL |
393 | b_count2 = sbuf->Read(buf, bytes_count); |
394 | bytes_count = Write(buf, b_count2); | |
395 | if (b_count2 > bytes_count) | |
396 | sbuf->WriteBack(buf+bytes_count, b_count2-bytes_count); | |
8ef6a930 GL |
397 | s += bytes_count; |
398 | } | |
399 | return s; | |
75ed1d15 GL |
400 | } |
401 | ||
402 | off_t wxStreamBuffer::Seek(off_t pos, wxSeekMode mode) | |
403 | { | |
404 | off_t ret_off, diff, last_access; | |
405 | ||
406 | last_access = GetLastAccess(); | |
407 | ||
8ef6a930 | 408 | if (!m_flushable) { |
84b46c35 GL |
409 | diff = pos + GetIntPosition(); |
410 | if (diff < 0 || diff > last_access) | |
411 | return wxInvalidOffset; | |
412 | SetIntPosition(diff); | |
413 | return diff; | |
414 | } | |
415 | ||
75ed1d15 GL |
416 | switch (mode) { |
417 | case wxFromStart: { | |
418 | // We'll try to compute an internal position later ... | |
419 | ret_off = m_stream->OnSysSeek(pos, wxFromStart); | |
420 | ResetBuffer(); | |
421 | return ret_off; | |
422 | } | |
423 | case wxFromCurrent: { | |
424 | diff = pos + GetIntPosition(); | |
425 | ||
426 | if ( (diff > last_access) || (diff < 0) ) { | |
427 | ret_off = m_stream->OnSysSeek(pos, wxFromCurrent); | |
428 | ResetBuffer(); | |
429 | return ret_off; | |
430 | } else { | |
431 | SetIntPosition(diff); | |
432 | return pos; | |
433 | } | |
434 | } | |
435 | case wxFromEnd: | |
436 | // Hard to compute: always seek to the requested position. | |
437 | ret_off = m_stream->OnSysSeek(pos, wxFromEnd); | |
438 | ResetBuffer(); | |
439 | return ret_off; | |
440 | } | |
441 | return wxInvalidOffset; | |
442 | } | |
443 | ||
444 | off_t wxStreamBuffer::Tell() const | |
445 | { | |
446 | off_t pos; | |
447 | ||
8ef6a930 | 448 | if (m_flushable) { |
84b46c35 GL |
449 | pos = m_stream->OnSysTell(); |
450 | if (pos == wxInvalidOffset) | |
451 | return wxInvalidOffset; | |
452 | return pos - GetLastAccess() + GetIntPosition(); | |
453 | } else | |
454 | return GetIntPosition(); | |
75ed1d15 GL |
455 | } |
456 | ||
8ef6a930 | 457 | size_t wxStreamBuffer::GetDataLeft() |
75ed1d15 | 458 | { |
8ef6a930 GL |
459 | if (m_buffer_end == m_buffer_pos && m_flushable) |
460 | FillBuffer(); | |
75ed1d15 GL |
461 | return m_buffer_end-m_buffer_pos; |
462 | } | |
463 | ||
464 | // ---------------------------------------------------------------------------- | |
465 | // wxStreamBase | |
466 | // ---------------------------------------------------------------------------- | |
467 | ||
468 | wxStreamBase::wxStreamBase() | |
469 | { | |
470 | m_lasterror = wxStream_NOERROR; | |
471 | m_lastcount = 0; | |
472 | } | |
473 | ||
474 | wxStreamBase::~wxStreamBase() | |
475 | { | |
476 | } | |
477 | ||
df875e59 | 478 | size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer), size_t WXUNUSED(size)) |
75ed1d15 GL |
479 | { |
480 | return 0; | |
481 | } | |
482 | ||
df875e59 | 483 | size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer), size_t WXUNUSED(bufsize)) |
75ed1d15 GL |
484 | { |
485 | return 0; | |
486 | } | |
487 | ||
df875e59 | 488 | off_t wxStreamBase::OnSysSeek(off_t WXUNUSED(seek), wxSeekMode WXUNUSED(mode)) |
75ed1d15 GL |
489 | { |
490 | return wxInvalidOffset; | |
491 | } | |
492 | ||
84b46c35 | 493 | off_t wxStreamBase::OnSysTell() const |
75ed1d15 GL |
494 | { |
495 | return wxInvalidOffset; | |
6d44bf31 GL |
496 | } |
497 | ||
1678ad78 GL |
498 | // ---------------------------------------------------------------------------- |
499 | // wxInputStream | |
500 | // ---------------------------------------------------------------------------- | |
501 | ||
3d4c6a21 | 502 | wxInputStream::wxInputStream() |
75ed1d15 | 503 | : wxStreamBase() |
3d4c6a21 | 504 | { |
6d44bf31 | 505 | m_i_destroybuf = TRUE; |
75ed1d15 | 506 | m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read); |
6d44bf31 GL |
507 | } |
508 | ||
509 | wxInputStream::wxInputStream(wxStreamBuffer *buffer) | |
75ed1d15 | 510 | : wxStreamBase() |
6d44bf31 GL |
511 | { |
512 | m_i_destroybuf = FALSE; | |
513 | m_i_streambuf = buffer; | |
3d4c6a21 GL |
514 | } |
515 | ||
516 | wxInputStream::~wxInputStream() | |
517 | { | |
6d44bf31 GL |
518 | if (m_i_destroybuf) |
519 | delete m_i_streambuf; | |
520 | } | |
521 | ||
522 | char wxInputStream::GetC() | |
523 | { | |
524 | char c; | |
525 | m_i_streambuf->Read(&c, 1); | |
526 | return c; | |
527 | } | |
528 | ||
529 | wxInputStream& wxInputStream::Read(void *buffer, size_t size) | |
530 | { | |
531 | m_i_streambuf->Read(buffer, size); | |
532 | // wxStreamBuffer sets all variables for us | |
533 | return *this; | |
3d4c6a21 GL |
534 | } |
535 | ||
75ed1d15 GL |
536 | char wxInputStream::Peek() |
537 | { | |
8ef6a930 | 538 | m_i_streambuf->GetDataLeft(); |
75ed1d15 GL |
539 | |
540 | return *(m_i_streambuf->GetBufferPos()); | |
541 | } | |
542 | ||
3d4c6a21 GL |
543 | |
544 | wxInputStream& wxInputStream::Read(wxOutputStream& stream_out) | |
545 | { | |
546 | char buf[BUF_TEMP_SIZE]; | |
547 | size_t bytes_read = BUF_TEMP_SIZE; | |
548 | ||
8ef6a930 | 549 | while (bytes_read == BUF_TEMP_SIZE) { |
3d4c6a21 | 550 | bytes_read = Read(buf, bytes_read).LastRead(); |
8ef6a930 | 551 | bytes_read = stream_out.Write(buf, bytes_read).LastWrite(); |
3d4c6a21 GL |
552 | } |
553 | return *this; | |
554 | } | |
555 | ||
75ed1d15 GL |
556 | off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode) |
557 | { | |
558 | return m_i_streambuf->Seek(pos, mode); | |
559 | } | |
560 | ||
561 | off_t wxInputStream::TellI() const | |
562 | { | |
563 | return m_i_streambuf->Tell(); | |
564 | } | |
565 | ||
566 | // -------------------- | |
567 | // Overloaded operators | |
568 | // -------------------- | |
569 | ||
1678ad78 GL |
570 | wxInputStream& wxInputStream::operator>>(wxString& line) |
571 | { | |
572 | wxDataInputStream s(*this); | |
573 | ||
574 | line = s.ReadLine(); | |
575 | return *this; | |
576 | } | |
577 | ||
578 | wxInputStream& wxInputStream::operator>>(char& c) | |
579 | { | |
580 | c = GetC(); | |
6d44bf31 | 581 | return *this; |
1678ad78 GL |
582 | } |
583 | ||
584 | wxInputStream& wxInputStream::operator>>(short& i) | |
585 | { | |
6d44bf31 GL |
586 | long l; |
587 | ||
588 | *this >> l; | |
589 | i = (short)l; | |
1678ad78 GL |
590 | return *this; |
591 | } | |
592 | ||
123a7fdd GL |
593 | wxInputStream& wxInputStream::operator>>(int& i) |
594 | { | |
595 | long l; | |
596 | ||
597 | *this >> l; | |
598 | i = (short)l; | |
599 | return *this; | |
600 | } | |
601 | ||
1678ad78 GL |
602 | wxInputStream& wxInputStream::operator>>(long& i) |
603 | { | |
6d44bf31 GL |
604 | /* I only implemented a simple integer parser */ |
605 | int c, sign; | |
606 | ||
607 | while (isspace( c = GetC() ) ) | |
608 | /* Do nothing */ ; | |
609 | ||
610 | i = 0; | |
611 | if (! (c == '-' || isdigit(c)) ) { | |
612 | InputStreamBuffer()->WriteBack(c); | |
613 | return *this; | |
614 | } | |
615 | ||
616 | if (c == '-') { | |
617 | sign = -1; | |
618 | c = GetC(); | |
619 | } else | |
620 | sign = 1; | |
621 | ||
622 | while (isdigit(c)) { | |
623 | i = i*10 + c; | |
624 | c = GetC(); | |
625 | } | |
626 | ||
627 | i *= sign; | |
628 | ||
1678ad78 GL |
629 | return *this; |
630 | } | |
631 | ||
75ed1d15 | 632 | wxInputStream& wxInputStream::operator>>(double& f) |
1678ad78 | 633 | { |
6d44bf31 GL |
634 | /* I only implemented a simple float parser */ |
635 | int c, sign; | |
636 | ||
637 | while (isspace( c = GetC() ) ) | |
638 | /* Do nothing */ ; | |
639 | ||
640 | f = 0.0; | |
641 | if (! (c == '-' || isdigit(c)) ) { | |
642 | InputStreamBuffer()->WriteBack(c); | |
643 | return *this; | |
644 | } | |
645 | ||
646 | if (c == '-') { | |
647 | sign = -1; | |
648 | c = GetC(); | |
649 | } else | |
650 | sign = 1; | |
651 | ||
652 | while (isdigit(c)) { | |
75ed1d15 | 653 | f = f*10 + (c - '0'); |
6d44bf31 GL |
654 | c = GetC(); |
655 | } | |
656 | ||
657 | if (c == '.') { | |
75ed1d15 | 658 | double f_multiplicator = (double) 0.1; |
6d44bf31 GL |
659 | c = GetC(); |
660 | ||
661 | while (isdigit(c)) { | |
75ed1d15 | 662 | f += (c-'0')*f_multiplicator; |
6d44bf31 GL |
663 | f_multiplicator /= 10; |
664 | c = GetC(); | |
665 | } | |
666 | } | |
667 | ||
668 | f *= sign; | |
669 | ||
1678ad78 GL |
670 | return *this; |
671 | } | |
672 | ||
47d67540 | 673 | #if wxUSE_SERIAL |
123a7fdd GL |
674 | wxInputStream& wxInputStream::operator>>(wxObject *& obj) |
675 | { | |
676 | wxObjectInputStream obj_s(*this); | |
677 | obj = obj_s.LoadObject(); | |
678 | return *this; | |
679 | } | |
fcc6dddd | 680 | #endif |
123a7fdd | 681 | |
6d44bf31 | 682 | |
1678ad78 GL |
683 | // ---------------------------------------------------------------------------- |
684 | // wxOutputStream | |
685 | // ---------------------------------------------------------------------------- | |
3d4c6a21 | 686 | wxOutputStream::wxOutputStream() |
75ed1d15 | 687 | : wxStreamBase() |
3d4c6a21 | 688 | { |
6d44bf31 | 689 | m_o_destroybuf = TRUE; |
75ed1d15 | 690 | m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write); |
6d44bf31 GL |
691 | } |
692 | ||
693 | wxOutputStream::wxOutputStream(wxStreamBuffer *buffer) | |
75ed1d15 | 694 | : wxStreamBase() |
6d44bf31 GL |
695 | { |
696 | m_o_destroybuf = FALSE; | |
697 | m_o_streambuf = buffer; | |
3d4c6a21 GL |
698 | } |
699 | ||
700 | wxOutputStream::~wxOutputStream() | |
701 | { | |
6d44bf31 GL |
702 | if (m_o_destroybuf) |
703 | delete m_o_streambuf; | |
704 | } | |
705 | ||
706 | wxOutputStream& wxOutputStream::Write(const void *buffer, size_t size) | |
707 | { | |
708 | m_o_streambuf->Write(buffer, size); | |
709 | return *this; | |
3d4c6a21 GL |
710 | } |
711 | ||
712 | wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in) | |
713 | { | |
6d44bf31 GL |
714 | stream_in.Read(*this); |
715 | return *this; | |
716 | } | |
717 | ||
75ed1d15 | 718 | off_t wxOutputStream::TellO() const |
6d44bf31 | 719 | { |
75ed1d15 | 720 | return m_o_streambuf->Tell(); |
6d44bf31 GL |
721 | } |
722 | ||
75ed1d15 | 723 | off_t wxOutputStream::SeekO(off_t pos, wxSeekMode mode) |
6d44bf31 | 724 | { |
75ed1d15 | 725 | return m_o_streambuf->Seek(pos, mode); |
6d44bf31 GL |
726 | } |
727 | ||
728 | void wxOutputStream::Sync() | |
729 | { | |
75ed1d15 | 730 | m_o_streambuf->FlushBuffer(); |
6d44bf31 GL |
731 | } |
732 | ||
733 | wxOutputStream& wxOutputStream::operator<<(const char *string) | |
734 | { | |
735 | return Write(string, strlen(string)); | |
736 | } | |
737 | ||
738 | wxOutputStream& wxOutputStream::operator<<(wxString& string) | |
739 | { | |
740 | return Write(string, string.Len()); | |
741 | } | |
742 | ||
743 | wxOutputStream& wxOutputStream::operator<<(char c) | |
744 | { | |
745 | return Write(&c, 1); | |
746 | } | |
747 | ||
748 | wxOutputStream& wxOutputStream::operator<<(short i) | |
749 | { | |
750 | wxString strint; | |
751 | ||
752 | strint.Printf("%i", i); | |
753 | return Write(strint, strint.Len()); | |
754 | } | |
755 | ||
756 | wxOutputStream& wxOutputStream::operator<<(int i) | |
757 | { | |
758 | wxString strint; | |
759 | ||
760 | strint.Printf("%i", i); | |
761 | return Write(strint, strint.Len()); | |
762 | } | |
763 | ||
764 | wxOutputStream& wxOutputStream::operator<<(long i) | |
765 | { | |
766 | wxString strlong; | |
767 | ||
768 | strlong.Printf("%i", i); | |
769 | return Write((const char *)strlong, strlong.Len()); | |
770 | } | |
771 | ||
772 | wxOutputStream& wxOutputStream::operator<<(double f) | |
773 | { | |
774 | wxString strfloat; | |
775 | ||
776 | strfloat.Printf("%f", f); | |
777 | return Write(strfloat, strfloat.Len()); | |
3d4c6a21 GL |
778 | } |
779 | ||
47d67540 | 780 | #if wxUSE_SERIAL |
123a7fdd GL |
781 | wxOutputStream& wxOutputStream::operator<<(wxObject& obj) |
782 | { | |
783 | wxObjectOutputStream obj_s(*this); | |
784 | obj_s.SaveObject(obj); | |
785 | return *this; | |
786 | } | |
fcc6dddd | 787 | #endif |
123a7fdd | 788 | |
1678ad78 GL |
789 | // ---------------------------------------------------------------------------- |
790 | // wxFilterInputStream | |
791 | // ---------------------------------------------------------------------------- | |
f4ada568 GL |
792 | wxFilterInputStream::wxFilterInputStream() |
793 | : wxInputStream(NULL) | |
794 | { | |
75ed1d15 | 795 | // WARNING streambuf set to NULL ! |
f4ada568 GL |
796 | } |
797 | ||
3d4c6a21 | 798 | wxFilterInputStream::wxFilterInputStream(wxInputStream& stream) |
75ed1d15 | 799 | : wxInputStream(stream.InputStreamBuffer()) |
3d4c6a21 | 800 | { |
219f895a | 801 | m_parent_i_stream = &stream; |
3d4c6a21 GL |
802 | } |
803 | ||
804 | wxFilterInputStream::~wxFilterInputStream() | |
805 | { | |
806 | } | |
219f895a | 807 | |
1678ad78 GL |
808 | // ---------------------------------------------------------------------------- |
809 | // wxFilterOutputStream | |
810 | // ---------------------------------------------------------------------------- | |
f4ada568 GL |
811 | wxFilterOutputStream::wxFilterOutputStream() |
812 | : wxOutputStream(NULL) | |
813 | { | |
814 | } | |
815 | ||
219f895a | 816 | wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream) |
75ed1d15 | 817 | : wxOutputStream(stream.OutputStreamBuffer()) |
219f895a RR |
818 | { |
819 | m_parent_o_stream = &stream; | |
820 | } | |
821 | ||
822 | wxFilterOutputStream::~wxFilterOutputStream() | |
823 | { | |
824 | } | |
6d44bf31 | 825 | |
6d44bf31 GL |
826 | // ---------------------------------------------------------------------------- |
827 | // Some IOManip function | |
828 | // ---------------------------------------------------------------------------- | |
829 | ||
830 | wxOutputStream& wxEndL(wxOutputStream& stream) | |
831 | { | |
832 | #ifdef __MSW__ | |
833 | return stream.Write("\r\n", 2); | |
834 | #else | |
835 | return stream.Write("\n", 1); | |
836 | #endif | |
837 | } |