wxStream: wxInputStream and wxOutputStream don't inherit from wxObject anymore.
[wxWidgets.git] / src / common / mstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mstream.cpp
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__
13 #pragma implementation "mstream.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18 #include <stdlib.h>
19 #include <wx/stream.h>
20 #include <wx/mstream.h>
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 // ----------------------------------------------------------------------------
27 // wxMemoryStreamBase
28 // ----------------------------------------------------------------------------
29 wxMemoryStreamBase::wxMemoryStreamBase()
30 {
31 m_buffer = NULL;
32 m_iolimit = 0;
33 m_persistent = FALSE;
34 m_length = 0;
35 }
36
37 wxMemoryStreamBase::~wxMemoryStreamBase()
38 {
39 if (!m_persistent && m_buffer)
40 free(m_buffer);
41 }
42
43 bool wxMemoryStreamBase::ChangeBufferSize(size_t new_size)
44 {
45 if (m_iolimit == 1)
46 return FALSE;
47
48 m_length = new_size;
49 if (!m_buffer)
50 m_buffer = (char *)malloc(m_length);
51 else
52 m_buffer = (char *)realloc(m_buffer, m_length);
53
54 return (m_buffer != NULL);
55 }
56
57 // ----------------------------------------------------------------------------
58 // wxMemoryInputStream
59 // ----------------------------------------------------------------------------
60
61 wxMemoryInputStream::wxMemoryInputStream(const char *data, size_t len)
62 {
63 m_persistent = TRUE;
64 m_length = len;
65 m_buffer = (char *)data; // It's bad.
66 m_position_i = 0;
67 m_lastread = 0;
68 m_eof = FALSE;
69 m_iolimit = 1;
70 }
71
72 wxMemoryInputStream::~wxMemoryInputStream()
73 {
74 }
75
76 wxInputStream& wxMemoryInputStream::Read(void *buffer, size_t size)
77 {
78 if (m_iolimit == 2) {
79 m_eof = TRUE;
80 return *this;
81 }
82 if (m_position_i+size > m_length)
83 size = m_length-m_position_i;
84
85 memcpy((void *)((unsigned long)buffer+m_position_i), m_buffer, size);
86 m_position_i += size;
87 m_lastread = size;
88
89 return *this;
90 }
91
92 off_t wxMemoryInputStream::SeekI(off_t pos, wxSeekMode mode)
93 {
94 if (m_iolimit == 2)
95 return 0;
96
97 switch (mode) {
98 case wxFromStart:
99 if ((size_t)pos > m_length)
100 return m_position_i;
101 return (m_position_i = pos);
102
103 case wxFromCurrent:
104 if ((size_t)(m_position_i+pos) > m_length)
105 return m_position_i;
106
107 return (m_position_i += pos);
108
109 case wxFromEnd:
110 if ((size_t)(m_length-pos) > m_length)
111 return m_position_i;
112
113 return (m_position_i = m_length-pos);
114 }
115
116 return m_position_i;
117 }
118
119 // ----------------------------------------------------------------------------
120 // wxMemoryOutputStream
121 // ----------------------------------------------------------------------------
122
123 wxMemoryOutputStream::wxMemoryOutputStream(char *data, size_t len)
124 {
125 m_persistent = FALSE;
126 m_buffer = data;
127 m_length = len;
128 m_position_o = 0;
129 m_lastwrite = 0;
130 m_bad = FALSE;
131 m_iolimit = 2;
132 }
133
134 wxMemoryOutputStream::~wxMemoryOutputStream()
135 {
136 }
137
138 wxOutputStream& wxMemoryOutputStream::Write(const void *buffer, size_t size)
139 {
140 if (m_iolimit == 1) {
141 m_bad = TRUE;
142 return *this;
143 }
144
145 if (m_position_o+size > m_length)
146 if (!ChangeBufferSize(m_position_o+size)) {
147 m_bad = TRUE;
148 return *this;
149 }
150
151 memcpy(m_buffer+m_position_o, buffer, size);
152 m_position_o += size;
153 m_lastwrite = size;
154
155 return *this;
156 }
157
158 off_t wxMemoryOutputStream::SeekO(off_t pos, wxSeekMode mode)
159 {
160 if (m_iolimit == 1)
161 return 0;
162
163 switch (mode) {
164 case wxFromStart:
165 if ((size_t)pos > m_length)
166 return m_position_o;
167 return (m_position_o = pos);
168
169 case wxFromCurrent:
170 if ((size_t)(m_position_o+pos) > m_length)
171 return m_position_o;
172
173 return (m_position_o += pos);
174
175 case wxFromEnd:
176 if ((size_t)(m_length-pos) > m_length)
177 return m_position_o;
178
179 return (m_position_o = m_length-pos);
180 }
181
182 return m_position_o;
183 }
184
185 // ----------------------------------------------------------------------------
186 // wxMemoryStream
187 // ----------------------------------------------------------------------------
188
189 wxMemoryStream::wxMemoryStream(char *data, size_t len)
190 : wxMemoryInputStream(NULL, 0), wxMemoryOutputStream(NULL, 0)
191 {
192 m_persistent = FALSE;
193 m_buffer = data;
194 m_length = len;
195 m_iolimit = 0;
196 }
197
198 wxMemoryStream::~wxMemoryStream()
199 {
200 }