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