]>
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" | |
3d4c6a21 | 18 | #include <stdlib.h> |
79c3e0e1 GL |
19 | #include <wx/stream.h> |
20 | #include <wx/mstream.h> | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
3d4c6a21 | 25 | |
3cacae09 | 26 | #if !USE_SHARED_LIBRARY |
79c3e0e1 GL |
27 | IMPLEMENT_CLASS(wxMemoryInputStream, wxInputStream) |
28 | IMPLEMENT_CLASS(wxMemoryOutputStream, wxOutputStream) | |
29 | IMPLEMENT_CLASS2(wxMemoryStream, wxInputStream, wxOutputStream) | |
3cacae09 GL |
30 | #endif |
31 | ||
79c3e0e1 GL |
32 | // ---------------------------------------------------------------------------- |
33 | // wxMemoryStreamBase | |
34 | // ---------------------------------------------------------------------------- | |
35 | wxMemoryStreamBase::wxMemoryStreamBase() | |
3d4c6a21 | 36 | { |
79c3e0e1 GL |
37 | m_buffer = NULL; |
38 | m_iolimit = 0; | |
3d4c6a21 | 39 | m_persistent = FALSE; |
79c3e0e1 | 40 | m_length = 0; |
3d4c6a21 GL |
41 | } |
42 | ||
43 | wxMemoryStreamBase::~wxMemoryStreamBase() | |
44 | { | |
79c3e0e1 GL |
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; | |
3d4c6a21 GL |
76 | } |
77 | ||
0cd9bfe8 GL |
78 | wxMemoryInputStream::~wxMemoryInputStream() |
79 | { | |
80 | } | |
81 | ||
79c3e0e1 | 82 | wxInputStream& wxMemoryInputStream::Read(void *buffer, size_t size) |
3d4c6a21 GL |
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 | ||
79c3e0e1 | 98 | off_t wxMemoryInputStream::SeekI(off_t pos, wxSeekMode mode) |
3d4c6a21 GL |
99 | { |
100 | if (m_iolimit == 2) | |
101 | return 0; | |
102 | ||
79c3e0e1 GL |
103 | switch (mode) { |
104 | case wxFromStart: | |
3d4c6a21 GL |
105 | if ((size_t)pos > m_length) |
106 | return m_position_i; | |
107 | return (m_position_i = pos); | |
108 | ||
79c3e0e1 | 109 | case wxFromCurrent: |
3d4c6a21 GL |
110 | if ((size_t)(m_position_i+pos) > m_length) |
111 | return m_position_i; | |
112 | ||
113 | return (m_position_i += pos); | |
114 | ||
79c3e0e1 | 115 | case wxFromEnd: |
3d4c6a21 GL |
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 | ||
79c3e0e1 GL |
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 | ||
0cd9bfe8 GL |
140 | wxMemoryOutputStream::~wxMemoryOutputStream() |
141 | { | |
142 | } | |
143 | ||
79c3e0e1 | 144 | wxOutputStream& wxMemoryOutputStream::Write(const void *buffer, size_t size) |
3d4c6a21 GL |
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 | ||
79c3e0e1 | 164 | off_t wxMemoryOutputStream::SeekO(off_t pos, wxSeekMode mode) |
3d4c6a21 | 165 | { |
79c3e0e1 | 166 | if (m_iolimit == 1) |
3d4c6a21 GL |
167 | return 0; |
168 | ||
79c3e0e1 GL |
169 | switch (mode) { |
170 | case wxFromStart: | |
3d4c6a21 GL |
171 | if ((size_t)pos > m_length) |
172 | return m_position_o; | |
173 | return (m_position_o = pos); | |
174 | ||
79c3e0e1 | 175 | case wxFromCurrent: |
3d4c6a21 GL |
176 | if ((size_t)(m_position_o+pos) > m_length) |
177 | return m_position_o; | |
178 | ||
179 | return (m_position_o += pos); | |
180 | ||
79c3e0e1 | 181 | case wxFromEnd: |
3d4c6a21 GL |
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 | ||
79c3e0e1 GL |
191 | // ---------------------------------------------------------------------------- |
192 | // wxMemoryStream | |
193 | // ---------------------------------------------------------------------------- | |
194 | ||
195 | wxMemoryStream::wxMemoryStream(char *data, size_t len) | |
196 | : wxMemoryInputStream(NULL, 0), wxMemoryOutputStream(NULL, 0) | |
3d4c6a21 | 197 | { |
79c3e0e1 GL |
198 | m_persistent = FALSE; |
199 | m_buffer = data; | |
200 | m_length = len; | |
201 | m_iolimit = 0; | |
202 | } | |
3d4c6a21 | 203 | |
79c3e0e1 GL |
204 | wxMemoryStream::~wxMemoryStream() |
205 | { | |
3d4c6a21 | 206 | } |