]>
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 | |
79c3e0e1 GL |
26 | // ---------------------------------------------------------------------------- |
27 | // wxMemoryStreamBase | |
28 | // ---------------------------------------------------------------------------- | |
29 | wxMemoryStreamBase::wxMemoryStreamBase() | |
3d4c6a21 | 30 | { |
79c3e0e1 GL |
31 | m_buffer = NULL; |
32 | m_iolimit = 0; | |
3d4c6a21 | 33 | m_persistent = FALSE; |
79c3e0e1 | 34 | m_length = 0; |
3d4c6a21 GL |
35 | } |
36 | ||
37 | wxMemoryStreamBase::~wxMemoryStreamBase() | |
38 | { | |
79c3e0e1 GL |
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; | |
6d44bf31 GL |
70 | |
71 | m_i_streambuf->SetBufferIO(0); | |
3d4c6a21 GL |
72 | } |
73 | ||
0cd9bfe8 GL |
74 | wxMemoryInputStream::~wxMemoryInputStream() |
75 | { | |
76 | } | |
77 | ||
6d44bf31 | 78 | size_t wxMemoryInputStream::DoRead(void *buffer, size_t size) |
3d4c6a21 GL |
79 | { |
80 | if (m_iolimit == 2) { | |
81 | m_eof = TRUE; | |
6d44bf31 | 82 | return 0; |
3d4c6a21 GL |
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; | |
3d4c6a21 | 89 | |
6d44bf31 | 90 | return size; |
3d4c6a21 GL |
91 | } |
92 | ||
6d44bf31 | 93 | off_t wxMemoryInputStream::DoSeekInput(off_t pos, wxSeekMode mode) |
3d4c6a21 GL |
94 | { |
95 | if (m_iolimit == 2) | |
96 | return 0; | |
97 | ||
79c3e0e1 GL |
98 | switch (mode) { |
99 | case wxFromStart: | |
3d4c6a21 GL |
100 | if ((size_t)pos > m_length) |
101 | return m_position_i; | |
102 | return (m_position_i = pos); | |
103 | ||
79c3e0e1 | 104 | case wxFromCurrent: |
3d4c6a21 GL |
105 | if ((size_t)(m_position_i+pos) > m_length) |
106 | return m_position_i; | |
107 | ||
108 | return (m_position_i += pos); | |
109 | ||
79c3e0e1 | 110 | case wxFromEnd: |
3d4c6a21 GL |
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 | ||
79c3e0e1 GL |
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; | |
6d44bf31 GL |
133 | |
134 | m_o_streambuf->SetBufferIO(0); | |
79c3e0e1 GL |
135 | } |
136 | ||
0cd9bfe8 GL |
137 | wxMemoryOutputStream::~wxMemoryOutputStream() |
138 | { | |
6d44bf31 | 139 | Sync(); |
0cd9bfe8 GL |
140 | } |
141 | ||
6d44bf31 | 142 | size_t wxMemoryOutputStream::DoWrite(const void *buffer, size_t size) |
3d4c6a21 GL |
143 | { |
144 | if (m_iolimit == 1) { | |
145 | m_bad = TRUE; | |
6d44bf31 | 146 | return 0; |
3d4c6a21 GL |
147 | } |
148 | ||
149 | if (m_position_o+size > m_length) | |
150 | if (!ChangeBufferSize(m_position_o+size)) { | |
151 | m_bad = TRUE; | |
6d44bf31 | 152 | return 0; |
3d4c6a21 GL |
153 | } |
154 | ||
155 | memcpy(m_buffer+m_position_o, buffer, size); | |
156 | m_position_o += size; | |
3d4c6a21 | 157 | |
6d44bf31 | 158 | return size; |
3d4c6a21 GL |
159 | } |
160 | ||
6d44bf31 | 161 | off_t wxMemoryOutputStream::DoSeekOutput(off_t pos, wxSeekMode mode) |
3d4c6a21 | 162 | { |
79c3e0e1 | 163 | if (m_iolimit == 1) |
3d4c6a21 GL |
164 | return 0; |
165 | ||
79c3e0e1 GL |
166 | switch (mode) { |
167 | case wxFromStart: | |
3d4c6a21 GL |
168 | if ((size_t)pos > m_length) |
169 | return m_position_o; | |
170 | return (m_position_o = pos); | |
171 | ||
79c3e0e1 | 172 | case wxFromCurrent: |
3d4c6a21 GL |
173 | if ((size_t)(m_position_o+pos) > m_length) |
174 | return m_position_o; | |
175 | ||
176 | return (m_position_o += pos); | |
177 | ||
79c3e0e1 | 178 | case wxFromEnd: |
3d4c6a21 GL |
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 | ||
79c3e0e1 GL |
188 | // ---------------------------------------------------------------------------- |
189 | // wxMemoryStream | |
190 | // ---------------------------------------------------------------------------- | |
191 | ||
192 | wxMemoryStream::wxMemoryStream(char *data, size_t len) | |
193 | : wxMemoryInputStream(NULL, 0), wxMemoryOutputStream(NULL, 0) | |
3d4c6a21 | 194 | { |
79c3e0e1 GL |
195 | m_persistent = FALSE; |
196 | m_buffer = data; | |
197 | m_length = len; | |
198 | m_iolimit = 0; | |
199 | } | |
3d4c6a21 | 200 | |
79c3e0e1 GL |
201 | wxMemoryStream::~wxMemoryStream() |
202 | { | |
3d4c6a21 | 203 | } |