]>
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); | |
e57e26dd | 287 | |
84b46c35 GL |
288 | m_stream->m_lastcount = 1; |
289 | return c; | |
290 | } | |
291 | ||
8ef6a930 | 292 | size_t wxStreamBuffer::Read(void *buffer, size_t size) |
6d44bf31 | 293 | { |
75ed1d15 | 294 | wxASSERT(m_stream != NULL); |
6d44bf31 | 295 | |
7f42cff1 GL |
296 | if (m_mode == write) |
297 | return 0; | |
298 | ||
6d44bf31 GL |
299 | // ------------------ |
300 | // Buffering disabled | |
301 | // ------------------ | |
302 | ||
2a040d3f | 303 | m_stream->m_lasterror = wxStream_NOERROR; |
75ed1d15 GL |
304 | m_stream->m_lastcount = GetWBack((char *)buffer, size); |
305 | size -= m_stream->m_lastcount; | |
306 | if (size == 0) | |
8ef6a930 | 307 | return m_stream->m_lastcount; |
75ed1d15 GL |
308 | |
309 | buffer = (void *)((char *)buffer+m_stream->m_lastcount); | |
310 | ||
926c550d | 311 | if (!m_buffer_size) |
8ef6a930 | 312 | return (m_stream->m_lastcount += m_stream->OnSysRead(buffer, size)); |
6d44bf31 GL |
313 | |
314 | // ----------------- | |
315 | // Buffering enabled | |
316 | // ----------------- | |
317 | size_t buf_left, orig_size = size; | |
6d44bf31 GL |
318 | |
319 | while (size > 0) { | |
75ed1d15 | 320 | buf_left = GetDataLeft(); |
6d44bf31 GL |
321 | |
322 | // First case: the requested buffer is larger than the stream buffer, | |
84b46c35 | 323 | // we split it. |
6d44bf31 | 324 | if (size > buf_left) { |
75ed1d15 GL |
325 | GetFromBuffer(buffer, buf_left); |
326 | size -= buf_left; | |
6d44bf31 GL |
327 | buffer = (char *)buffer + buf_left; // ANSI C++ violation. |
328 | ||
75ed1d15 | 329 | if (!FillBuffer()) { |
926c550d | 330 | CHECK_ERROR(wxStream_READ_ERR); |
8ef6a930 | 331 | return (m_stream->m_lastcount = orig_size-size); |
6d44bf31 GL |
332 | } |
333 | } else { | |
334 | ||
335 | // Second case: we just copy from the stream buffer. | |
75ed1d15 | 336 | GetFromBuffer(buffer, size); |
6d44bf31 GL |
337 | break; |
338 | } | |
339 | } | |
8ef6a930 | 340 | return (m_stream->m_lastcount += orig_size); |
6d44bf31 GL |
341 | } |
342 | ||
8ef6a930 GL |
343 | size_t wxStreamBuffer::Read(wxStreamBuffer *s_buf) |
344 | { | |
345 | char buf[BUF_TEMP_SIZE]; | |
346 | size_t s = 0, bytes_read = BUF_TEMP_SIZE; | |
347 | ||
7f42cff1 GL |
348 | if (m_mode == write) |
349 | return 0; | |
350 | ||
062c4861 | 351 | while (bytes_read != 0) { |
8ef6a930 GL |
352 | bytes_read = Read(buf, bytes_read); |
353 | bytes_read = s_buf->Write(buf, bytes_read); | |
354 | s += bytes_read; | |
355 | } | |
356 | return s; | |
357 | } | |
358 | ||
359 | size_t wxStreamBuffer::Write(const void *buffer, size_t size) | |
6d44bf31 | 360 | { |
75ed1d15 | 361 | wxASSERT(m_stream != NULL); |
6d44bf31 | 362 | |
7f42cff1 GL |
363 | if (m_mode == read) |
364 | return 0; | |
365 | ||
6d44bf31 GL |
366 | // ------------------ |
367 | // Buffering disabled | |
368 | // ------------------ | |
369 | ||
2a040d3f | 370 | m_stream->m_lasterror = wxStream_NOERROR; |
8ef6a930 GL |
371 | if (!m_buffer_size) |
372 | return (m_stream->m_lastcount = m_stream->OnSysWrite(buffer, size)); | |
6d44bf31 GL |
373 | |
374 | // ------------------ | |
375 | // Buffering enabled | |
376 | // ------------------ | |
377 | ||
378 | size_t buf_left, orig_size = size; | |
6d44bf31 GL |
379 | |
380 | while (size > 0) { | |
381 | buf_left = m_buffer_end - m_buffer_pos; | |
382 | ||
383 | // First case: the buffer to write is larger than the stream buffer, | |
384 | // we split it | |
385 | if (size > buf_left) { | |
75ed1d15 | 386 | PutToBuffer(buffer, buf_left); |
6d44bf31 GL |
387 | size -= buf_left; |
388 | buffer = (char *)buffer + buf_left; // ANSI C++ violation. | |
389 | ||
75ed1d15 | 390 | if (!FlushBuffer()) { |
926c550d | 391 | CHECK_ERROR(wxStream_WRITE_ERR); |
8ef6a930 | 392 | return (m_stream->m_lastcount = orig_size-size); |
6d44bf31 | 393 | } |
8ef6a930 | 394 | |
6d44bf31 GL |
395 | m_buffer_pos = m_buffer_start; |
396 | ||
397 | } else { | |
398 | ||
399 | // Second case: just copy it in the stream buffer. | |
75ed1d15 | 400 | PutToBuffer(buffer, size); |
6d44bf31 GL |
401 | break; |
402 | } | |
403 | } | |
8ef6a930 GL |
404 | return (m_stream->m_lastcount = orig_size); |
405 | } | |
406 | ||
407 | size_t wxStreamBuffer::Write(wxStreamBuffer *sbuf) | |
408 | { | |
409 | char buf[BUF_TEMP_SIZE]; | |
eb4e516d | 410 | size_t s = 0, bytes_count = BUF_TEMP_SIZE, b_count2; |
8ef6a930 | 411 | |
7f42cff1 GL |
412 | if (m_mode == read) |
413 | return 0; | |
414 | ||
8ef6a930 | 415 | while (bytes_count == BUF_TEMP_SIZE) { |
eb4e516d GL |
416 | b_count2 = sbuf->Read(buf, bytes_count); |
417 | bytes_count = Write(buf, b_count2); | |
418 | if (b_count2 > bytes_count) | |
419 | sbuf->WriteBack(buf+bytes_count, b_count2-bytes_count); | |
8ef6a930 GL |
420 | s += bytes_count; |
421 | } | |
422 | return s; | |
75ed1d15 GL |
423 | } |
424 | ||
425 | off_t wxStreamBuffer::Seek(off_t pos, wxSeekMode mode) | |
426 | { | |
427 | off_t ret_off, diff, last_access; | |
428 | ||
429 | last_access = GetLastAccess(); | |
430 | ||
8ef6a930 | 431 | if (!m_flushable) { |
84b46c35 GL |
432 | diff = pos + GetIntPosition(); |
433 | if (diff < 0 || diff > last_access) | |
434 | return wxInvalidOffset; | |
435 | SetIntPosition(diff); | |
436 | return diff; | |
437 | } | |
438 | ||
75ed1d15 GL |
439 | switch (mode) { |
440 | case wxFromStart: { | |
441 | // We'll try to compute an internal position later ... | |
442 | ret_off = m_stream->OnSysSeek(pos, wxFromStart); | |
443 | ResetBuffer(); | |
444 | return ret_off; | |
445 | } | |
446 | case wxFromCurrent: { | |
447 | diff = pos + GetIntPosition(); | |
448 | ||
449 | if ( (diff > last_access) || (diff < 0) ) { | |
450 | ret_off = m_stream->OnSysSeek(pos, wxFromCurrent); | |
451 | ResetBuffer(); | |
452 | return ret_off; | |
453 | } else { | |
454 | SetIntPosition(diff); | |
455 | return pos; | |
456 | } | |
457 | } | |
458 | case wxFromEnd: | |
459 | // Hard to compute: always seek to the requested position. | |
460 | ret_off = m_stream->OnSysSeek(pos, wxFromEnd); | |
461 | ResetBuffer(); | |
462 | return ret_off; | |
463 | } | |
464 | return wxInvalidOffset; | |
465 | } | |
466 | ||
467 | off_t wxStreamBuffer::Tell() const | |
468 | { | |
469 | off_t pos; | |
470 | ||
8ef6a930 | 471 | if (m_flushable) { |
84b46c35 GL |
472 | pos = m_stream->OnSysTell(); |
473 | if (pos == wxInvalidOffset) | |
474 | return wxInvalidOffset; | |
475 | return pos - GetLastAccess() + GetIntPosition(); | |
476 | } else | |
477 | return GetIntPosition(); | |
75ed1d15 GL |
478 | } |
479 | ||
8ef6a930 | 480 | size_t wxStreamBuffer::GetDataLeft() |
75ed1d15 | 481 | { |
8ef6a930 GL |
482 | if (m_buffer_end == m_buffer_pos && m_flushable) |
483 | FillBuffer(); | |
75ed1d15 GL |
484 | return m_buffer_end-m_buffer_pos; |
485 | } | |
486 | ||
487 | // ---------------------------------------------------------------------------- | |
488 | // wxStreamBase | |
489 | // ---------------------------------------------------------------------------- | |
490 | ||
491 | wxStreamBase::wxStreamBase() | |
492 | { | |
493 | m_lasterror = wxStream_NOERROR; | |
494 | m_lastcount = 0; | |
495 | } | |
496 | ||
497 | wxStreamBase::~wxStreamBase() | |
498 | { | |
499 | } | |
500 | ||
df875e59 | 501 | size_t wxStreamBase::OnSysRead(void *WXUNUSED(buffer), size_t WXUNUSED(size)) |
75ed1d15 GL |
502 | { |
503 | return 0; | |
504 | } | |
505 | ||
df875e59 | 506 | size_t wxStreamBase::OnSysWrite(const void *WXUNUSED(buffer), size_t WXUNUSED(bufsize)) |
75ed1d15 GL |
507 | { |
508 | return 0; | |
509 | } | |
510 | ||
df875e59 | 511 | off_t wxStreamBase::OnSysSeek(off_t WXUNUSED(seek), wxSeekMode WXUNUSED(mode)) |
75ed1d15 GL |
512 | { |
513 | return wxInvalidOffset; | |
514 | } | |
515 | ||
84b46c35 | 516 | off_t wxStreamBase::OnSysTell() const |
75ed1d15 GL |
517 | { |
518 | return wxInvalidOffset; | |
6d44bf31 GL |
519 | } |
520 | ||
1678ad78 GL |
521 | // ---------------------------------------------------------------------------- |
522 | // wxInputStream | |
523 | // ---------------------------------------------------------------------------- | |
524 | ||
3d4c6a21 | 525 | wxInputStream::wxInputStream() |
75ed1d15 | 526 | : wxStreamBase() |
3d4c6a21 | 527 | { |
6d44bf31 | 528 | m_i_destroybuf = TRUE; |
75ed1d15 | 529 | m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read); |
6d44bf31 GL |
530 | } |
531 | ||
532 | wxInputStream::wxInputStream(wxStreamBuffer *buffer) | |
75ed1d15 | 533 | : wxStreamBase() |
6d44bf31 GL |
534 | { |
535 | m_i_destroybuf = FALSE; | |
536 | m_i_streambuf = buffer; | |
3d4c6a21 GL |
537 | } |
538 | ||
539 | wxInputStream::~wxInputStream() | |
540 | { | |
6d44bf31 GL |
541 | if (m_i_destroybuf) |
542 | delete m_i_streambuf; | |
543 | } | |
544 | ||
545 | char wxInputStream::GetC() | |
546 | { | |
547 | char c; | |
548 | m_i_streambuf->Read(&c, 1); | |
549 | return c; | |
550 | } | |
551 | ||
552 | wxInputStream& wxInputStream::Read(void *buffer, size_t size) | |
553 | { | |
554 | m_i_streambuf->Read(buffer, size); | |
555 | // wxStreamBuffer sets all variables for us | |
556 | return *this; | |
3d4c6a21 GL |
557 | } |
558 | ||
75ed1d15 GL |
559 | char wxInputStream::Peek() |
560 | { | |
8ef6a930 | 561 | m_i_streambuf->GetDataLeft(); |
75ed1d15 GL |
562 | |
563 | return *(m_i_streambuf->GetBufferPos()); | |
564 | } | |
565 | ||
3d4c6a21 GL |
566 | |
567 | wxInputStream& wxInputStream::Read(wxOutputStream& stream_out) | |
568 | { | |
569 | char buf[BUF_TEMP_SIZE]; | |
570 | size_t bytes_read = BUF_TEMP_SIZE; | |
571 | ||
8ef6a930 | 572 | while (bytes_read == BUF_TEMP_SIZE) { |
3d4c6a21 | 573 | bytes_read = Read(buf, bytes_read).LastRead(); |
8ef6a930 | 574 | bytes_read = stream_out.Write(buf, bytes_read).LastWrite(); |
3d4c6a21 GL |
575 | } |
576 | return *this; | |
577 | } | |
578 | ||
75ed1d15 GL |
579 | off_t wxInputStream::SeekI(off_t pos, wxSeekMode mode) |
580 | { | |
581 | return m_i_streambuf->Seek(pos, mode); | |
582 | } | |
583 | ||
584 | off_t wxInputStream::TellI() const | |
585 | { | |
586 | return m_i_streambuf->Tell(); | |
587 | } | |
588 | ||
589 | // -------------------- | |
590 | // Overloaded operators | |
591 | // -------------------- | |
592 | ||
1678ad78 GL |
593 | wxInputStream& wxInputStream::operator>>(wxString& line) |
594 | { | |
595 | wxDataInputStream s(*this); | |
596 | ||
597 | line = s.ReadLine(); | |
598 | return *this; | |
599 | } | |
600 | ||
601 | wxInputStream& wxInputStream::operator>>(char& c) | |
602 | { | |
603 | c = GetC(); | |
6d44bf31 | 604 | return *this; |
1678ad78 GL |
605 | } |
606 | ||
38830220 | 607 | wxInputStream& wxInputStream::operator>>(signed short& i) |
1678ad78 | 608 | { |
38830220 | 609 | signed long l; |
6d44bf31 GL |
610 | |
611 | *this >> l; | |
38830220 | 612 | i = (signed short)l; |
1678ad78 GL |
613 | return *this; |
614 | } | |
615 | ||
38830220 | 616 | wxInputStream& wxInputStream::operator>>(signed int& i) |
123a7fdd | 617 | { |
38830220 | 618 | signed long l; |
123a7fdd GL |
619 | |
620 | *this >> l; | |
38830220 | 621 | i = (signed int)l; |
123a7fdd GL |
622 | return *this; |
623 | } | |
624 | ||
38830220 | 625 | wxInputStream& wxInputStream::operator>>(signed long& i) |
1678ad78 | 626 | { |
6d44bf31 | 627 | /* I only implemented a simple integer parser */ |
e57e26dd | 628 | int c; |
38830220 | 629 | int sign; |
6d44bf31 GL |
630 | |
631 | while (isspace( c = GetC() ) ) | |
632 | /* Do nothing */ ; | |
633 | ||
634 | i = 0; | |
635 | if (! (c == '-' || isdigit(c)) ) { | |
636 | InputStreamBuffer()->WriteBack(c); | |
637 | return *this; | |
638 | } | |
639 | ||
640 | if (c == '-') { | |
641 | sign = -1; | |
642 | c = GetC(); | |
e57e26dd | 643 | } else if (c == '+') { |
6d44bf31 | 644 | sign = 1; |
e57e26dd RR |
645 | c = GetC(); |
646 | } else { | |
647 | sign = 1; | |
648 | } | |
6d44bf31 GL |
649 | |
650 | while (isdigit(c)) { | |
b6bff301 | 651 | i = i*10 + (c - (int)'0'); |
6d44bf31 GL |
652 | c = GetC(); |
653 | } | |
654 | ||
655 | i *= sign; | |
656 | ||
1678ad78 GL |
657 | return *this; |
658 | } | |
659 | ||
38830220 RR |
660 | wxInputStream& wxInputStream::operator>>(unsigned short& i) |
661 | { | |
662 | unsigned long l; | |
663 | ||
664 | *this >> l; | |
665 | i = (unsigned short)l; | |
666 | return *this; | |
667 | } | |
668 | ||
669 | wxInputStream& wxInputStream::operator>>(unsigned int& i) | |
670 | { | |
671 | unsigned long l; | |
672 | ||
673 | *this >> l; | |
674 | i = (unsigned int)l; | |
675 | return *this; | |
676 | } | |
677 | ||
678 | wxInputStream& wxInputStream::operator>>(unsigned long& i) | |
679 | { | |
680 | /* I only implemented a simple integer parser */ | |
e57e26dd | 681 | int c; |
38830220 RR |
682 | |
683 | while (isspace( c = GetC() ) ) | |
684 | /* Do nothing */ ; | |
685 | ||
686 | i = 0; | |
687 | if (!isdigit(c)) { | |
688 | InputStreamBuffer()->WriteBack(c); | |
689 | return *this; | |
690 | } | |
691 | ||
692 | while (isdigit(c)) { | |
b6bff301 | 693 | i = i*10 + c - '0'; |
38830220 RR |
694 | c = GetC(); |
695 | } | |
696 | ||
697 | return *this; | |
698 | } | |
699 | ||
75ed1d15 | 700 | wxInputStream& wxInputStream::operator>>(double& f) |
1678ad78 | 701 | { |
6d44bf31 GL |
702 | /* I only implemented a simple float parser */ |
703 | int c, sign; | |
704 | ||
705 | while (isspace( c = GetC() ) ) | |
706 | /* Do nothing */ ; | |
707 | ||
708 | f = 0.0; | |
709 | if (! (c == '-' || isdigit(c)) ) { | |
710 | InputStreamBuffer()->WriteBack(c); | |
711 | return *this; | |
712 | } | |
713 | ||
714 | if (c == '-') { | |
715 | sign = -1; | |
716 | c = GetC(); | |
e57e26dd | 717 | } else if (c == '+') { |
6d44bf31 | 718 | sign = 1; |
e57e26dd RR |
719 | c = GetC(); |
720 | } else { | |
721 | sign = 1; | |
722 | } | |
6d44bf31 GL |
723 | |
724 | while (isdigit(c)) { | |
75ed1d15 | 725 | f = f*10 + (c - '0'); |
6d44bf31 GL |
726 | c = GetC(); |
727 | } | |
728 | ||
729 | if (c == '.') { | |
75ed1d15 | 730 | double f_multiplicator = (double) 0.1; |
6d44bf31 GL |
731 | c = GetC(); |
732 | ||
733 | while (isdigit(c)) { | |
75ed1d15 | 734 | f += (c-'0')*f_multiplicator; |
6d44bf31 GL |
735 | f_multiplicator /= 10; |
736 | c = GetC(); | |
737 | } | |
738 | } | |
739 | ||
740 | f *= sign; | |
741 | ||
1678ad78 GL |
742 | return *this; |
743 | } | |
744 | ||
47d67540 | 745 | #if wxUSE_SERIAL |
123a7fdd GL |
746 | wxInputStream& wxInputStream::operator>>(wxObject *& obj) |
747 | { | |
748 | wxObjectInputStream obj_s(*this); | |
749 | obj = obj_s.LoadObject(); | |
750 | return *this; | |
751 | } | |
fcc6dddd | 752 | #endif |
123a7fdd | 753 | |
6d44bf31 | 754 | |
1678ad78 GL |
755 | // ---------------------------------------------------------------------------- |
756 | // wxOutputStream | |
757 | // ---------------------------------------------------------------------------- | |
3d4c6a21 | 758 | wxOutputStream::wxOutputStream() |
75ed1d15 | 759 | : wxStreamBase() |
3d4c6a21 | 760 | { |
6d44bf31 | 761 | m_o_destroybuf = TRUE; |
75ed1d15 | 762 | m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write); |
6d44bf31 GL |
763 | } |
764 | ||
765 | wxOutputStream::wxOutputStream(wxStreamBuffer *buffer) | |
75ed1d15 | 766 | : wxStreamBase() |
6d44bf31 GL |
767 | { |
768 | m_o_destroybuf = FALSE; | |
769 | m_o_streambuf = buffer; | |
3d4c6a21 GL |
770 | } |
771 | ||
772 | wxOutputStream::~wxOutputStream() | |
773 | { | |
6d44bf31 GL |
774 | if (m_o_destroybuf) |
775 | delete m_o_streambuf; | |
776 | } | |
777 | ||
778 | wxOutputStream& wxOutputStream::Write(const void *buffer, size_t size) | |
779 | { | |
780 | m_o_streambuf->Write(buffer, size); | |
781 | return *this; | |
3d4c6a21 GL |
782 | } |
783 | ||
784 | wxOutputStream& wxOutputStream::Write(wxInputStream& stream_in) | |
785 | { | |
6d44bf31 GL |
786 | stream_in.Read(*this); |
787 | return *this; | |
788 | } | |
789 | ||
75ed1d15 | 790 | off_t wxOutputStream::TellO() const |
6d44bf31 | 791 | { |
75ed1d15 | 792 | return m_o_streambuf->Tell(); |
6d44bf31 GL |
793 | } |
794 | ||
75ed1d15 | 795 | off_t wxOutputStream::SeekO(off_t pos, wxSeekMode mode) |
6d44bf31 | 796 | { |
75ed1d15 | 797 | return m_o_streambuf->Seek(pos, mode); |
6d44bf31 GL |
798 | } |
799 | ||
800 | void wxOutputStream::Sync() | |
801 | { | |
75ed1d15 | 802 | m_o_streambuf->FlushBuffer(); |
6d44bf31 GL |
803 | } |
804 | ||
805 | wxOutputStream& wxOutputStream::operator<<(const char *string) | |
806 | { | |
807 | return Write(string, strlen(string)); | |
808 | } | |
809 | ||
810 | wxOutputStream& wxOutputStream::operator<<(wxString& string) | |
811 | { | |
84fff0b3 OK |
812 | #if wxUSE_UNICODE |
813 | const wxWX2MBbuf buf = string.mb_str(); | |
814 | return *this << buf; | |
815 | #else | |
6d44bf31 | 816 | return Write(string, string.Len()); |
84fff0b3 | 817 | #endif |
6d44bf31 GL |
818 | } |
819 | ||
820 | wxOutputStream& wxOutputStream::operator<<(char c) | |
821 | { | |
822 | return Write(&c, 1); | |
823 | } | |
824 | ||
38830220 | 825 | wxOutputStream& wxOutputStream::operator<<(signed short i) |
6d44bf31 | 826 | { |
38830220 RR |
827 | signed long l = (signed long)i; |
828 | return *this << l; | |
829 | } | |
6d44bf31 | 830 | |
38830220 RR |
831 | wxOutputStream& wxOutputStream::operator<<(signed int i) |
832 | { | |
833 | signed long l = (signed long)i; | |
834 | return *this << l; | |
835 | } | |
836 | ||
837 | wxOutputStream& wxOutputStream::operator<<(signed long i) | |
838 | { | |
839 | wxString strlong; | |
840 | strlong.Printf(_T("%ld"), i); | |
841 | return *this << strlong; | |
6d44bf31 GL |
842 | } |
843 | ||
38830220 | 844 | wxOutputStream& wxOutputStream::operator<<(unsigned short i) |
6d44bf31 | 845 | { |
38830220 RR |
846 | unsigned long l = (unsigned long)i; |
847 | return *this << l; | |
848 | } | |
6d44bf31 | 849 | |
38830220 RR |
850 | wxOutputStream& wxOutputStream::operator<<(unsigned int i) |
851 | { | |
852 | unsigned long l = (unsigned long)i; | |
853 | return *this << l; | |
6d44bf31 GL |
854 | } |
855 | ||
38830220 | 856 | wxOutputStream& wxOutputStream::operator<<(unsigned long i) |
6d44bf31 GL |
857 | { |
858 | wxString strlong; | |
38830220 | 859 | strlong.Printf(_T("%lu"), i); |
84fff0b3 | 860 | return *this << strlong; |
6d44bf31 GL |
861 | } |
862 | ||
863 | wxOutputStream& wxOutputStream::operator<<(double f) | |
864 | { | |
865 | wxString strfloat; | |
866 | ||
84fff0b3 OK |
867 | strfloat.Printf(_T("%f"), f); |
868 | return *this << strfloat; | |
3d4c6a21 GL |
869 | } |
870 | ||
47d67540 | 871 | #if wxUSE_SERIAL |
123a7fdd GL |
872 | wxOutputStream& wxOutputStream::operator<<(wxObject& obj) |
873 | { | |
874 | wxObjectOutputStream obj_s(*this); | |
875 | obj_s.SaveObject(obj); | |
876 | return *this; | |
877 | } | |
fcc6dddd | 878 | #endif |
123a7fdd | 879 | |
1678ad78 GL |
880 | // ---------------------------------------------------------------------------- |
881 | // wxFilterInputStream | |
882 | // ---------------------------------------------------------------------------- | |
f4ada568 GL |
883 | wxFilterInputStream::wxFilterInputStream() |
884 | : wxInputStream(NULL) | |
885 | { | |
75ed1d15 | 886 | // WARNING streambuf set to NULL ! |
f4ada568 GL |
887 | } |
888 | ||
3d4c6a21 | 889 | wxFilterInputStream::wxFilterInputStream(wxInputStream& stream) |
75ed1d15 | 890 | : wxInputStream(stream.InputStreamBuffer()) |
3d4c6a21 | 891 | { |
219f895a | 892 | m_parent_i_stream = &stream; |
3d4c6a21 GL |
893 | } |
894 | ||
895 | wxFilterInputStream::~wxFilterInputStream() | |
896 | { | |
897 | } | |
219f895a | 898 | |
1678ad78 GL |
899 | // ---------------------------------------------------------------------------- |
900 | // wxFilterOutputStream | |
901 | // ---------------------------------------------------------------------------- | |
f4ada568 GL |
902 | wxFilterOutputStream::wxFilterOutputStream() |
903 | : wxOutputStream(NULL) | |
904 | { | |
905 | } | |
906 | ||
219f895a | 907 | wxFilterOutputStream::wxFilterOutputStream(wxOutputStream& stream) |
75ed1d15 | 908 | : wxOutputStream(stream.OutputStreamBuffer()) |
219f895a RR |
909 | { |
910 | m_parent_o_stream = &stream; | |
911 | } | |
912 | ||
913 | wxFilterOutputStream::~wxFilterOutputStream() | |
914 | { | |
915 | } | |
6d44bf31 | 916 | |
6d44bf31 GL |
917 | // ---------------------------------------------------------------------------- |
918 | // Some IOManip function | |
919 | // ---------------------------------------------------------------------------- | |
920 | ||
921 | wxOutputStream& wxEndL(wxOutputStream& stream) | |
922 | { | |
923 | #ifdef __MSW__ | |
924 | return stream.Write("\r\n", 2); | |
925 | #else | |
926 | return stream.Write("\n", 1); | |
927 | #endif | |
928 | } | |
ce4169a4 RR |
929 | |
930 | #endif | |
931 | // wxUSE_STREAMS |