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