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