]>
Commit | Line | Data |
---|---|---|
3d4c6a21 | 1 | ///////////////////////////////////////////////////////////////////////////// |
32fc4afb | 2 | // Name: mstream.cpp |
3d4c6a21 GL |
3 | // Purpose: "Memory stream" classes |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
32fc4afb | 13 | #pragma implementation "mstream.h" |
3d4c6a21 GL |
14 | #endif |
15 | ||
79c3e0e1 GL |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
79c3e0e1 GL |
18 | |
19 | #ifdef __BORLANDC__ | |
ce4169a4 | 20 | #pragma hdrstop |
79c3e0e1 | 21 | #endif |
3d4c6a21 | 22 | |
ce4169a4 RR |
23 | #if wxUSE_STREAMS |
24 | ||
25 | #include <stdlib.h> | |
3096bd2f VZ |
26 | #include "wx/stream.h" |
27 | #include "wx/mstream.h" | |
ce4169a4 | 28 | |
79c3e0e1 GL |
29 | // ---------------------------------------------------------------------------- |
30 | // wxMemoryInputStream | |
31 | // ---------------------------------------------------------------------------- | |
32 | ||
33 | wxMemoryInputStream::wxMemoryInputStream(const char *data, size_t len) | |
75ed1d15 | 34 | : wxInputStream() |
79c3e0e1 | 35 | { |
c980c992 | 36 | m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read); |
2d0a075d | 37 | m_i_streambuf->SetBufferIO((char*) data, (char*) (data+len)); |
18c07b73 | 38 | m_i_streambuf->SetIntPosition(0); // seek to start pos |
75ed1d15 | 39 | m_i_streambuf->Fixed(TRUE); |
c980c992 | 40 | |
18c07b73 | 41 | m_length = len; |
3d4c6a21 GL |
42 | } |
43 | ||
0cd9bfe8 GL |
44 | wxMemoryInputStream::~wxMemoryInputStream() |
45 | { | |
c980c992 | 46 | delete m_i_streambuf; |
0cd9bfe8 GL |
47 | } |
48 | ||
32a4b1d5 GL |
49 | char wxMemoryInputStream::Peek() |
50 | { | |
75ed1d15 | 51 | return m_i_streambuf->GetBufferStart()[m_i_streambuf->GetIntPosition()]; |
c980c992 GL |
52 | } |
53 | ||
54 | size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes) | |
c83b0766 VS |
55 | { |
56 | size_t bufsize = m_i_streambuf->GetBufferEnd() - m_i_streambuf->GetBufferStart(); | |
57 | size_t oldpos = m_i_streambuf->GetIntPosition(); | |
58 | m_i_streambuf->Read(buffer, nbytes); | |
59 | size_t newpos = m_i_streambuf->GetIntPosition(); | |
60 | if (newpos == 0) return bufsize - oldpos; | |
61 | else return newpos - oldpos; | |
c980c992 GL |
62 | } |
63 | ||
64 | off_t wxMemoryInputStream::OnSysSeek(off_t pos, wxSeekMode mode) | |
65 | { | |
66 | return m_i_streambuf->Seek(pos, mode); | |
67 | } | |
68 | ||
69 | off_t wxMemoryInputStream::OnSysTell() const | |
70 | { | |
71 | return m_i_streambuf->Tell(); | |
3d4c6a21 GL |
72 | } |
73 | ||
79c3e0e1 GL |
74 | // ---------------------------------------------------------------------------- |
75 | // wxMemoryOutputStream | |
76 | // ---------------------------------------------------------------------------- | |
77 | ||
78 | wxMemoryOutputStream::wxMemoryOutputStream(char *data, size_t len) | |
75ed1d15 | 79 | : wxOutputStream() |
79c3e0e1 | 80 | { |
c980c992 | 81 | m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write); |
75ed1d15 GL |
82 | if (data) |
83 | m_o_streambuf->SetBufferIO(data, data+len); | |
56dc1ffd GL |
84 | m_o_streambuf->Fixed(FALSE); |
85 | m_o_streambuf->Flushable(FALSE); | |
79c3e0e1 GL |
86 | } |
87 | ||
0cd9bfe8 | 88 | wxMemoryOutputStream::~wxMemoryOutputStream() |
79c3e0e1 | 89 | { |
c980c992 | 90 | delete m_o_streambuf; |
3d4c6a21 | 91 | } |
ce4169a4 | 92 | |
c980c992 GL |
93 | size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes) |
94 | { | |
c83b0766 VS |
95 | size_t bufsize = m_o_streambuf->GetBufferEnd() - m_o_streambuf->GetBufferStart(); |
96 | size_t oldpos = m_o_streambuf->GetIntPosition(); | |
97 | m_o_streambuf->Write(buffer, nbytes); | |
98 | size_t newpos = m_o_streambuf->GetIntPosition(); | |
99 | if (newpos == 0) return bufsize - oldpos; | |
100 | else return newpos - oldpos; | |
c980c992 GL |
101 | } |
102 | ||
103 | off_t wxMemoryOutputStream::OnSysSeek(off_t pos, wxSeekMode mode) | |
104 | { | |
105 | return m_o_streambuf->Seek(pos, mode); | |
106 | } | |
107 | ||
108 | off_t wxMemoryOutputStream::OnSysTell() const | |
109 | { | |
110 | return m_o_streambuf->Tell(); | |
111 | } | |
112 | ||
a324a7bc GL |
113 | size_t wxMemoryOutputStream::CopyTo(char *buffer, size_t len) const |
114 | { | |
115 | if (!buffer) | |
116 | return 0; | |
117 | ||
cd25b18c RR |
118 | if (len > GetSize()) |
119 | len = GetSize(); | |
a324a7bc GL |
120 | |
121 | memcpy(buffer, m_o_streambuf->GetBufferStart(), len); | |
122 | return len; | |
123 | } | |
c980c992 | 124 | |
ce4169a4 | 125 | #endif |