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