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