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