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