* Ooops, I didn't copy the files in the right directory of my repository.
[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 m_i_streambuf->SetBufferIO(0);
72 }
73
74 wxMemoryInputStream::~wxMemoryInputStream()
75 {
76 }
77
78 size_t wxMemoryInputStream::DoRead(void *buffer, size_t size)
79 {
80 if (m_iolimit == 2) {
81 m_eof = TRUE;
82 return 0;
83 }
84 if (m_position_i+size > m_length)
85 size = m_length-m_position_i;
86
87 memcpy((void *)((unsigned long)buffer+m_position_i), m_buffer, size);
88 m_position_i += size;
89
90 return size;
91 }
92
93 off_t wxMemoryInputStream::DoSeekInput(off_t pos, wxSeekMode mode)
94 {
95 if (m_iolimit == 2)
96 return 0;
97
98 switch (mode) {
99 case wxFromStart:
100 if ((size_t)pos > m_length)
101 return m_position_i;
102 return (m_position_i = pos);
103
104 case wxFromCurrent:
105 if ((size_t)(m_position_i+pos) > m_length)
106 return m_position_i;
107
108 return (m_position_i += pos);
109
110 case wxFromEnd:
111 if ((size_t)(m_length-pos) > m_length)
112 return m_position_i;
113
114 return (m_position_i = m_length-pos);
115 }
116
117 return m_position_i;
118 }
119
120 // ----------------------------------------------------------------------------
121 // wxMemoryOutputStream
122 // ----------------------------------------------------------------------------
123
124 wxMemoryOutputStream::wxMemoryOutputStream(char *data, size_t len)
125 {
126 m_persistent = FALSE;
127 m_buffer = data;
128 m_length = len;
129 m_position_o = 0;
130 m_lastwrite = 0;
131 m_bad = FALSE;
132 m_iolimit = 2;
133
134 m_o_streambuf->SetBufferIO(0);
135 }
136
137 wxMemoryOutputStream::~wxMemoryOutputStream()
138 {
139 Sync();
140 }
141
142 size_t wxMemoryOutputStream::DoWrite(const void *buffer, size_t size)
143 {
144 if (m_iolimit == 1) {
145 m_bad = TRUE;
146 return 0;
147 }
148
149 if (m_position_o+size > m_length)
150 if (!ChangeBufferSize(m_position_o+size)) {
151 m_bad = TRUE;
152 return 0;
153 }
154
155 memcpy(m_buffer+m_position_o, buffer, size);
156 m_position_o += size;
157
158 return size;
159 }
160
161 off_t wxMemoryOutputStream::DoSeekOutput(off_t pos, wxSeekMode mode)
162 {
163 if (m_iolimit == 1)
164 return 0;
165
166 switch (mode) {
167 case wxFromStart:
168 if ((size_t)pos > m_length)
169 return m_position_o;
170 return (m_position_o = pos);
171
172 case wxFromCurrent:
173 if ((size_t)(m_position_o+pos) > m_length)
174 return m_position_o;
175
176 return (m_position_o += pos);
177
178 case wxFromEnd:
179 if ((size_t)(m_length-pos) > m_length)
180 return m_position_o;
181
182 return (m_position_o = m_length-pos);
183 }
184
185 return m_position_o;
186 }
187
188 // ----------------------------------------------------------------------------
189 // wxMemoryStream
190 // ----------------------------------------------------------------------------
191
192 wxMemoryStream::wxMemoryStream(char *data, size_t len)
193 : wxMemoryInputStream(NULL, 0), wxMemoryOutputStream(NULL, 0)
194 {
195 m_persistent = FALSE;
196 m_buffer = data;
197 m_length = len;
198 m_iolimit = 0;
199 }
200
201 wxMemoryStream::~wxMemoryStream()
202 {
203 }